Skip to content

Commit

Permalink
Added precompile-with-invalid-function-selector test
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Feb 6, 2024
1 parent cfa8b0d commit a705155
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions precompile/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func NewStatefulPrecompileContract(functions []*StatefulPrecompileFunction) (Sta
functions: make(map[string]*StatefulPrecompileFunction),
}
for _, function := range functions {
if len(function.selector) != SelectorLen {
return nil, fmt.Errorf("invalid length of function selector, want: %v, got: %v", SelectorLen, len(function.selector))
}

_, exists := contract.functions[string(function.selector)]
if exists {
return nil, fmt.Errorf("cannot create stateful precompile with duplicated function selector: %q", function.selector)
Expand Down
34 changes: 34 additions & 0 deletions precompile/contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,37 @@ func TestPrecompileWithDuplicatedFunctionSelector(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "cannot create stateful precompile with duplicated function selector")
}

func TestPrecompileWithInvalidFunctionSelector(t *testing.T) {
for _, tc := range []struct {
desc string
fnSelector []byte
}{
{
desc: "empty selector",
fnSelector: []byte{},
},
{
desc: "short selector",
fnSelector: []byte("abc"),
},
{
desc: "long selector",
fnSelector: []byte("acbde"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
functions := []*StatefulPrecompileFunction{
NewStatefulPrecompileFunction(
tc.fnSelector,
test,
),
}

// Construct the contract with no fallback function.
_, err := NewStatefulPrecompileContract(functions)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid length of function selector")
})
}
}

0 comments on commit a705155

Please sign in to comment.