-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ingress Filtering for Interop Enabled Mempool
- Loading branch information
1 parent
98b8443
commit 91583bf
Showing
8 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package txpool | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/types/interoptypes" | ||
) | ||
|
||
// IngressFilter is an interface that allows filtering of transactions before they are added to the transaction pool. | ||
// Implementations of this interface can be used to filter transactions based on various criteria. | ||
// FilterTx will return true if the transaction should be allowed, and false if it should be rejected. | ||
type IngressFilter interface { | ||
FilterTx(tx *types.Transaction) bool | ||
} | ||
|
||
type interopFilter struct { | ||
logsFn func(tx *types.Transaction) ([]*types.Log, error) | ||
checkFn func(ctx context.Context, ems []interoptypes.Message, safety interoptypes.SafetyLevel) error | ||
} | ||
|
||
func NewInteropFilter( | ||
logsFn func(tx *types.Transaction) ([]*types.Log, error), | ||
checkFn func(ctx context.Context, ems []interoptypes.Message, safety interoptypes.SafetyLevel) error) IngressFilter { | ||
return &interopFilter{ | ||
logsFn: logsFn, | ||
checkFn: checkFn, | ||
} | ||
} | ||
|
||
// FilterTx implements IngressFilter.FilterTx | ||
// it gets logs checks for message safety based on the function provided | ||
func (f *interopFilter) FilterTx(tx *types.Transaction) bool { | ||
logs, err := f.logsFn(tx) | ||
if err != nil { | ||
return true // default to allow if logs cannot be retrieved | ||
} | ||
if len(logs) == 0 { | ||
return true // default to allow if there are no logs | ||
} | ||
ems, err := interoptypes.ExecutingMessagesFromLogs(logs) | ||
if err != nil { | ||
return true // default to allow if logs cannot be parsed | ||
} | ||
if len(ems) == 0 { | ||
return true // default to allow if there are no executing messages | ||
} | ||
|
||
// check with the supervisor if the transaction should be allowed given the executing messages | ||
ctx := context.Background() | ||
return f.checkFn(ctx, ems, interoptypes.Unsafe) == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package txpool | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/types/interoptypes" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInteropFilter(t *testing.T) { | ||
t.Run("Tx has no logs", func(t *testing.T) { | ||
logFn := func(tx *types.Transaction) ([]*types.Log, error) { | ||
return []*types.Log{}, nil | ||
} | ||
checkFn := func(ctx context.Context, ems []interoptypes.Message, safety interoptypes.SafetyLevel) error { | ||
// make this return error, but it won't be called because logs are empty | ||
return errors.New("error") | ||
} | ||
// when there are no logs to process, the transaction should be allowed | ||
filter := NewInteropFilter(logFn, checkFn) | ||
require.True(t, filter.FilterTx(&types.Transaction{})) | ||
}) | ||
t.Run("Tx errored when getting logs", func(t *testing.T) { | ||
logFn := func(tx *types.Transaction) ([]*types.Log, error) { | ||
return []*types.Log{}, errors.New("error") | ||
} | ||
checkFn := func(ctx context.Context, ems []interoptypes.Message, safety interoptypes.SafetyLevel) error { | ||
// make this return error, but it won't be called because logs retrieval errored | ||
return errors.New("error") | ||
} | ||
// when log retrieval errors, the transaction should be allowed | ||
filter := NewInteropFilter(logFn, checkFn) | ||
require.True(t, filter.FilterTx(&types.Transaction{})) | ||
}) | ||
t.Run("Tx has no executing messages", func(t *testing.T) { | ||
logFn := func(tx *types.Transaction) ([]*types.Log, error) { | ||
l1 := &types.Log{ | ||
Topics: []common.Hash{common.BytesToHash([]byte("topic1"))}, | ||
} | ||
return []*types.Log{l1}, errors.New("error") | ||
} | ||
checkFn := func(ctx context.Context, ems []interoptypes.Message, safety interoptypes.SafetyLevel) error { | ||
// make this return error, but it won't be called because logs retrieval doesn't have executing messages | ||
return errors.New("error") | ||
} | ||
// when no executing messages are included, the transaction should be allowed | ||
filter := NewInteropFilter(logFn, checkFn) | ||
require.True(t, filter.FilterTx(&types.Transaction{})) | ||
}) | ||
t.Run("Tx has valid executing message", func(t *testing.T) { | ||
logFn := func(tx *types.Transaction) ([]*types.Log, error) { | ||
// TODO: make executing messages here | ||
l1 := &types.Log{ | ||
Topics: []common.Hash{common.BytesToHash([]byte("topic1"))}, | ||
} | ||
return []*types.Log{l1}, errors.New("error") | ||
} | ||
checkFn := func(ctx context.Context, ems []interoptypes.Message, safety interoptypes.SafetyLevel) error { | ||
// make this return error, but it won't be called because logs retrieval doesn't have executing messages | ||
return nil | ||
} | ||
// when no executing messages are included, the transaction should be allowed | ||
filter := NewInteropFilter(logFn, checkFn) | ||
require.True(t, filter.FilterTx(&types.Transaction{})) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters