github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/precompiles/precompile_test.go (about) 1 package precompiles_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/onflow/flow-go/fvm/evm/precompiles" 9 "github.com/onflow/flow-go/fvm/evm/testutils" 10 ) 11 12 func TestMutiFunctionContract(t *testing.T) { 13 t.Parallel() 14 15 address := testutils.RandomAddress(t) 16 sig := precompiles.FunctionSelector{1, 2, 3, 4} 17 data := "data" 18 input := append(sig[:], data...) 19 gas := uint64(20) 20 output := []byte("output") 21 22 pc := precompiles.MultiFunctionPrecompileContract(address, []precompiles.Function{ 23 &mockedFunction{ 24 FunctionSelectorFunc: func() precompiles.FunctionSelector { 25 return sig 26 }, 27 ComputeGasFunc: func(inp []byte) uint64 { 28 require.Equal(t, []byte(data), inp) 29 return gas 30 }, 31 RunFunc: func(inp []byte) ([]byte, error) { 32 require.Equal(t, []byte(data), inp) 33 return output, nil 34 }, 35 }}) 36 37 require.Equal(t, address, pc.Address()) 38 require.Equal(t, gas, pc.RequiredGas(input)) 39 ret, err := pc.Run(input) 40 require.NoError(t, err) 41 require.Equal(t, output, ret) 42 43 input2 := []byte("non existing signature and data") 44 _, err = pc.Run(input2) 45 require.Equal(t, precompiles.ErrInvalidMethodCall, err) 46 } 47 48 type mockedFunction struct { 49 FunctionSelectorFunc func() precompiles.FunctionSelector 50 ComputeGasFunc func(input []byte) uint64 51 RunFunc func(input []byte) ([]byte, error) 52 } 53 54 func (mf *mockedFunction) FunctionSelector() precompiles.FunctionSelector { 55 if mf.FunctionSelectorFunc == nil { 56 panic("method not set for mocked function") 57 } 58 return mf.FunctionSelectorFunc() 59 } 60 61 func (mf *mockedFunction) ComputeGas(input []byte) uint64 { 62 if mf.ComputeGasFunc == nil { 63 panic("method not set for mocked function") 64 } 65 return mf.ComputeGasFunc(input) 66 } 67 68 func (mf *mockedFunction) Run(input []byte) ([]byte, error) { 69 if mf.RunFunc == nil { 70 panic("method not set for mocked function") 71 } 72 return mf.RunFunc(input) 73 }