Skip to content

Commit

Permalink
fix eth_call_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhoonbang committed Feb 14, 2024
1 parent 7e6e498 commit ef212f6
Showing 1 changed file with 70 additions and 113 deletions.
183 changes: 70 additions & 113 deletions core/services/pipeline/task.eth_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -54,42 +53,25 @@ func TestETHCallTask(t *testing.T) {
expectedErrorContains string
}{
{
"happy with empty from", // name
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF", // contract
"", // from
"$(foo)", // data
"0", // evmChainID
"", // gas
"", // block
nil, // specGasLimit
pipeline.NewVarsFrom(map[string]interface{}{ // vars
"happy with empty from",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
"",
"$(foo)",
"0",
"",
"",
nil,
pipeline.NewVarsFrom(map[string]interface{}{
"foo": []byte("foo bar"),
}),
nil, // inputs
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
ethClient.
On("CallContext",
mock.Anything,
mock.Anything,
"eth_call",
map[string]interface{}{
"from": common.Address{},
"to": &contractAddr,
"gas": hexutil.Uint64(uint64(drJobTypeGasLimit)),
"input": hexutil.Bytes([]byte("foo bar")),
"gasPrice": (*hexutil.Big)(nil),
"maxFeePerGas": (*hexutil.Big)(nil),
"maxPriorityFeePerGas": (*hexutil.Big)(nil),
},
"latest").Return(nil).Run(func(args mock.Arguments) {
resp := args.Get(1).(*hexutil.Bytes)
*resp = []byte("baz quux")
})
On("CallContract", mock.Anything, ethereum.CallMsg{To: &contractAddr, Gas: uint64(drJobTypeGasLimit), Data: []byte("foo bar")}, (*big.Int)(nil)).
Return([]byte("baz quux"), nil)
},
[]byte("baz quux"), // expected
nil, // expecedErrorCause
"", // expectedErrorContains
[]byte("baz quux"), nil, "",
},
{
"happy with gas limit per task",
Expand All @@ -98,37 +80,20 @@ func TestETHCallTask(t *testing.T) {
"$(foo)",
"0",
"$(gasLimit)",
"", // block
"",
nil,
pipeline.NewVarsFrom(map[string]interface{}{
"foo": []byte("foo bar"),
"gasLimit": 100_000,
}),
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
ethClient.
On("CallContext",
mock.Anything,
mock.Anything,
"eth_call",
map[string]interface{}{
"from": common.Address{},
"to": &contractAddr,
"gas": hexutil.Uint64(uint64(100_000)),
"input": hexutil.Bytes([]byte("foo bar")),
"gasPrice": (*hexutil.Big)(nil),
"maxFeePerGas": (*hexutil.Big)(nil),
"maxPriorityFeePerGas": (*hexutil.Big)(nil),
},
"latest").Return(nil).Run(func(args mock.Arguments) {
resp := args.Get(1).(*hexutil.Bytes)
*resp = []byte("baz quux")
})
On("CallContract", mock.Anything, ethereum.CallMsg{To: &contractAddr, Gas: 100_000, Data: []byte("foo bar")}, (*big.Int)(nil)).
Return([]byte("baz quux"), nil)
},
[]byte("baz quux"),
nil,
"",
[]byte("baz quux"), nil, "",
},
{
"happy with gas limit per spec",
Expand All @@ -137,36 +102,19 @@ func TestETHCallTask(t *testing.T) {
"$(foo)",
"0",
"",
"pending", // block
"",
&specGasLimit,
pipeline.NewVarsFrom(map[string]interface{}{
"foo": []byte("foo bar"),
}),
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
ethClient.
On("CallContext",
mock.Anything,
mock.Anything,
"eth_call",
map[string]interface{}{
"from": common.Address{},
"to": &contractAddr,
"gas": hexutil.Uint64(uint64(specGasLimit)),
"input": hexutil.Bytes([]byte("foo bar")),
"gasPrice": (*hexutil.Big)(nil),
"maxFeePerGas": (*hexutil.Big)(nil),
"maxPriorityFeePerGas": (*hexutil.Big)(nil),
},
"pending").Return(nil).Run(func(args mock.Arguments) {
resp := args.Get(1).(*hexutil.Bytes)
*resp = []byte("baz quux")
})
On("CallContract", mock.Anything, ethereum.CallMsg{To: &contractAddr, Gas: uint64(specGasLimit), Data: []byte("foo bar")}, (*big.Int)(nil)).
Return([]byte("baz quux"), nil)
},
[]byte("baz quux"), // expected
nil,
"",
[]byte("baz quux"), nil, "",
},
{
"happy with from addr",
Expand All @@ -175,33 +123,18 @@ func TestETHCallTask(t *testing.T) {
"$(foo)",
"0",
"",
"pending", // block
"",
nil,
pipeline.NewVarsFrom(map[string]interface{}{
"foo": []byte("foo bar"),
}),
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
fromAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
ethClient.
On("CallContext",
mock.Anything,
mock.Anything,
"eth_call",
map[string]interface{}{
"from": fromAddr,
"to": &contractAddr,
"gas": hexutil.Uint64(drJobTypeGasLimit),
"input": hexutil.Bytes([]byte("foo bar")),
"gasPrice": (*hexutil.Big)(nil),
"maxFeePerGas": (*hexutil.Big)(nil),
"maxPriorityFeePerGas": (*hexutil.Big)(nil),
},
"pending").Return(nil).Run(func(args mock.Arguments) {
resp := args.Get(1).(*hexutil.Bytes)
*resp = []byte("baz quux")
})
On("CallContract", mock.Anything, ethereum.CallMsg{To: &contractAddr, Gas: uint64(drJobTypeGasLimit), From: fromAddr, Data: []byte("foo bar")}, (*big.Int)(nil)).
Return([]byte("baz quux"), nil)
},
[]byte("baz quux"), nil, "",
},
Expand Down Expand Up @@ -285,24 +218,6 @@ func TestETHCallTask(t *testing.T) {
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {},
nil, pipeline.ErrTooManyErrors, "task inputs",
},
{
"invalid block", // name
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF", // contract
"", // from
"$(foo)", // data
"0", // evmChainID
"", // gas
"invalidblock", // block
nil, // specGasLimit
pipeline.NewVarsFrom(map[string]interface{}{ // vars
"foo": []byte("foo bar"),
}),
nil, // inputs
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {}, // setupClientMocks
nil, // expected
nil, // expectedErrorCause
"unsupported block param", // expectedErrorContains
},
{
"missing chainID",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
Expand All @@ -325,6 +240,48 @@ func TestETHCallTask(t *testing.T) {
},
nil, nil, chains.ErrNoSuchChainID.Error(),
},
{
"simulate using latest block",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
"",
"$(foo)",
"0",
"",
"latest",
nil,
pipeline.NewVarsFrom(map[string]interface{}{
"foo": []byte("foo bar"),
}),
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
ethClient.
On("CallContract", mock.Anything, ethereum.CallMsg{To: &contractAddr, Gas: uint64(drJobTypeGasLimit), Data: []byte("foo bar")}, (*big.Int)(nil)).
Return([]byte("baz quux"), nil)
},
[]byte("baz quux"), nil, "",
},
{
"simulate using pending block",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
"",
"$(foo)",
"0",
"",
"pending",
nil,
pipeline.NewVarsFrom(map[string]interface{}{
"foo": []byte("foo bar"),
}),
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) {
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
ethClient.
On("PendingCallContract", mock.Anything, ethereum.CallMsg{To: &contractAddr, Gas: uint64(drJobTypeGasLimit), Data: []byte("foo bar")}).
Return([]byte("baz quux"), nil)
},
[]byte("baz quux"), nil, "",
},
}

for _, test := range tests {
Expand Down

0 comments on commit ef212f6

Please sign in to comment.