From 49095a974303fe2d19d3ecf72fc52a0c524285ec Mon Sep 17 00:00:00 2001 From: inphi Date: Mon, 22 Jan 2024 20:35:21 -0500 Subject: [PATCH] fix tests after 66e169aa6418f70b22abced33a3335c81d299927 --- optimism/client_bedrock_test.go | 30 +--- .../client_blocks_bedrock_tracers_test.go | 27 +-- optimism/client_blocks_handler_test.go | 159 +----------------- optimism/client_test.go | 32 +--- optimism/suite_mocks.go | 49 ++++++ 5 files changed, 69 insertions(+), 228 deletions(-) create mode 100644 optimism/suite_mocks.go diff --git a/optimism/client_bedrock_test.go b/optimism/client_bedrock_test.go index 2153452..d3228b6 100644 --- a/optimism/client_bedrock_test.go +++ b/optimism/client_bedrock_test.go @@ -49,6 +49,10 @@ type ClientBedrockTestSuite struct { mockCurrencyFetcher *mocks.CurrencyFetcher } +func (t *ClientBedrockTestSuite) MockJSONRPC() *mocks.JSONRPC { + return t.mockJSONRPC +} + func TestClientBedrock(t *testing.T) { suite.Run(t, new(ClientBedrockTestSuite)) } @@ -284,8 +288,8 @@ func (testSuite *ClientBedrockTestSuite) TestBedrockBlockCurrent() { tx2 := EthCommon.HexToHash("0x6103c9a945fabd69b2cfe25cd0f5c9ebe73b7f68f4fed2c68b2cfdd8429a6a88") // Execute the transaction trace - mockBedrockTraceTransaction(ctx, testSuite, "testdata/goerli_bedrock_tx_trace_5003318_1.json") - mockBedrockTraceTransaction(ctx, testSuite, "testdata/goerli_bedrock_tx_trace_5003318_2.json") + mockTraceTransaction(ctx, testSuite, "testdata/goerli_bedrock_tx_trace_5003318_1.json") + mockTraceTransaction(ctx, testSuite, "testdata/goerli_bedrock_tx_trace_5003318_2.json") // mockDebugTraceBedrockBlock(ctx, testSuite, "testdata/goerli_bedrock_block_trace_5003318.json") mockGetBedrockTransactionReceipt(ctx, testSuite, []EthCommon.Hash{tx1, tx2}, []string{"testdata/goerli_bedrock_tx_receipt_5003318_1.json", "testdata/goerli_bedrock_tx_receipt_5003318_2.json"}) @@ -319,28 +323,6 @@ func (testSuite *ClientBedrockTestSuite) TestBedrockBlockCurrent() { runTest(client) } -func mockBedrockTraceTransaction(ctx context.Context, testSuite *ClientBedrockTestSuite, txFileData string) { - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - file, err := os.ReadFile(txFileData) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() -} - //nolint:unused func mockDebugTraceBedrockBlock(ctx context.Context, testSuite *ClientBedrockTestSuite, txFileData string) { testSuite.mockJSONRPC.On( diff --git a/optimism/client_blocks_bedrock_tracers_test.go b/optimism/client_blocks_bedrock_tracers_test.go index 1832235..9667005 100644 --- a/optimism/client_blocks_bedrock_tracers_test.go +++ b/optimism/client_blocks_bedrock_tracers_test.go @@ -25,7 +25,6 @@ import ( "github.com/ethereum-optimism/optimism/l2geth/eth" "github.com/ethereum-optimism/optimism/l2geth/params" - "github.com/ethereum-optimism/optimism/l2geth/rpc" EthCommon "github.com/ethereum/go-ethereum/common" EthHexutil "github.com/ethereum/go-ethereum/common/hexutil" mocks "github.com/inphi/optimism-rosetta/mocks/optimism" @@ -52,6 +51,10 @@ type BedrockTracersTestSuite struct { mockCurrencyFetcher *mocks.CurrencyFetcher } +func (testSuite *BedrockTracersTestSuite) MockJSONRPC() *mocks.JSONRPC { + return testSuite.mockJSONRPC +} + func (testSuite *BedrockTracersTestSuite) SetupTest() { testSuite.mockJSONRPC = &mocks.JSONRPC{} testSuite.mockGraphQL = &mocks.GraphQL{} @@ -645,25 +648,3 @@ func constructTxTwoExpectedCalls(m map[string][]*FlatCall, txTwoHash EthCommon.H }, } } - -func mockTraceTransaction(ctx context.Context, testSuite *BedrockTracersTestSuite, txFileData string) { - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - file, err := os.ReadFile(txFileData) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() -} diff --git a/optimism/client_blocks_handler_test.go b/optimism/client_blocks_handler_test.go index 3fac42b..e468ed0 100644 --- a/optimism/client_blocks_handler_test.go +++ b/optimism/client_blocks_handler_test.go @@ -44,6 +44,10 @@ type ClientBlocksHandlerTestSuite struct { client *Client } +func (t *ClientBlocksHandlerTestSuite) MockJSONRPC() *mocks.JSONRPC { + return t.mockJSONRPC +} + // SetupTest configures the test suite. func (testSuite *ClientBlocksHandlerTestSuite) SetupTest() { testSuite.mockJSONRPC = &mocks.JSONRPC{} @@ -106,36 +110,7 @@ func (testSuite *ClientBlocksHandlerTestSuite) TestBlock_ERC20Mint() { *r = json.RawMessage(file) }, ).Once() - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - - testSuite.Len(r, 1) - testSuite.Len(r[0].Args, 2) - testSuite.Equal( - common.HexToHash("0xd919fe87c4bc24f767d1b7a165266658d542af9e3f9bc11dd1a2d1f4695df009").Hex(), - r[0].Args[0], - ) - testSuite.Equal(tc, r[0].Args[1]) - - file, err := os.ReadFile( - "testdata/tx_trace_1241186.json", - ) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() + mockTraceTransaction(ctx, testSuite, "testdata/tx_trace_1241186.json", common.HexToHash("0xd919fe87c4bc24f767d1b7a165266658d542af9e3f9bc11dd1a2d1f4695df009").Hex(), tc) testSuite.mockJSONRPC.On( "BatchCallContext", ctx, @@ -231,36 +206,7 @@ func (testSuite *ClientBlocksHandlerTestSuite) TestBlock_1502839_OPCriticalBug() *r = json.RawMessage(file) }, ).Once() - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - - testSuite.Len(r, 1) - testSuite.Len(r[0].Args, 2) - testSuite.Equal( - common.HexToHash("0x3ff079ba4ea0745401e9661d623550d24c9412ea9ad578bfbb0d441dadcce9bc").Hex(), - r[0].Args[0], - ) - testSuite.Equal(tc, r[0].Args[1]) - - file, err := os.ReadFile( - "testdata/tx_trace_1502839.json", - ) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() + mockTraceTransaction(ctx, testSuite, "testdata/tx_trace_1502839.json", common.HexToHash("0x3ff079ba4ea0745401e9661d623550d24c9412ea9ad578bfbb0d441dadcce9bc").Hex(), tc) testSuite.mockJSONRPC.On( "BatchCallContext", ctx, @@ -439,36 +385,7 @@ func (testSuite *ClientBlocksHandlerTestSuite) TestBlock_ERC20TransferFailed() { *r = json.RawMessage(file) }, ).Once() - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - - testSuite.Len(r, 1) - testSuite.Len(r[0].Args, 2) - testSuite.Equal( - common.HexToHash("0x5a1ec671315432cf8b6a67d95b857109fcafae277ae2c673db40b44ca8dd5c1b").Hex(), - r[0].Args[0], - ) - testSuite.Equal(tc, r[0].Args[1]) - - file, err := os.ReadFile( - "testdata/tx_trace_14930491.json", - ) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() + mockTraceTransaction(ctx, testSuite, "testdata/tx_trace_14930491.json", common.HexToHash("0x5a1ec671315432cf8b6a67d95b857109fcafae277ae2c673db40b44ca8dd5c1b").Hex()) testSuite.mockJSONRPC.On( "BatchCallContext", ctx, @@ -562,36 +479,7 @@ func (testSuite *ClientBlocksHandlerTestSuite) TestBlock_GoerliNoFeeEnforcement( *r = json.RawMessage(file) }, ).Once() - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - - testSuite.Len(r, 1) - testSuite.Len(r[0].Args, 2) - testSuite.Equal( - common.HexToHash("0x2992c7d87b09484c5940f7d649bd9957c629a43ac477473b655dbb07d8c742a5").Hex(), - r[0].Args[0], - ) - testSuite.Equal(tc, r[0].Args[1]) - - file, err := os.ReadFile( - "testdata/tx_trace_goerli_367675.json", - ) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() + mockTraceTransaction(ctx, testSuite, "testdata/tx_trace_goerli_367675.json", common.HexToHash("0x2992c7d87b09484c5940f7d649bd9957c629a43ac477473b655dbb07d8c742a5").Hex(), tc) testSuite.mockJSONRPC.On( "BatchCallContext", ctx, @@ -672,36 +560,7 @@ func (testSuite *ClientBlocksHandlerTestSuite) TestBlock_OVMSelfDestruct() { *r = json.RawMessage(file) }, ).Once() - testSuite.mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - - testSuite.Len(r, 1) - testSuite.Len(r[0].Args, 2) - testSuite.Equal( - common.HexToHash("0xfa6db346b928db4c98ebf72a14ac52d0c884e2cfa70cf40816542c9d7d1caf13").Hex(), - r[0].Args[0], - ) - testSuite.Equal(tc, r[0].Args[1]) - - file, err := os.ReadFile( - "testdata/tx_trace_1909952.json", - ) - testSuite.NoError(err) - - call := new(Call) - testSuite.NoError(call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() + mockTraceTransaction(ctx, testSuite, "testdata/tx_trace_1909952.json", common.HexToHash("0xfa6db346b928db4c98ebf72a14ac52d0c884e2cfa70cf40816542c9d7d1caf13").Hex(), tc) testSuite.mockJSONRPC.On( "BatchCallContext", ctx, diff --git a/optimism/client_test.go b/optimism/client_test.go index 8a70650..06c0bea 100644 --- a/optimism/client_test.go +++ b/optimism/client_test.go @@ -509,37 +509,7 @@ func TestBlock_Current(t *testing.T) { *r = json.RawMessage(file) }, ).Once() - mockJSONRPC.On( - "BatchCallContext", - ctx, - mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { - return len(rpcs) == 1 && rpcs[0].Method == "debug_traceTransaction" - }), - ).Return( - nil, - ).Run( - func(args mock.Arguments) { - r := args.Get(1).([]rpc.BatchElem) - - assert.Len(t, r, 1) - assert.Len(t, r[0].Args, 2) - assert.Equal( - t, - common.HexToHash("0x5e77a04531c7c107af1882d76cbff9486d0a9aa53701c30888509d4f5f2b003a").Hex(), - r[0].Args[0], - ) - assert.Equal(t, tc, r[0].Args[1]) - - file, err := os.ReadFile( - "testdata/tx_trace_1.json", - ) - assert.NoError(t, err) - - call := new(Call) - assert.NoError(t, call.UnmarshalJSON(file)) - *(r[0].Result.(**Call)) = call - }, - ).Once() + mockTraceTransaction(ctx, &simpleMocking{mockJSONRPC, assert.New(t)}, "testdata/tx_trace_1.json", common.HexToHash("0x5e77a04531c7c107af1882d76cbff9486d0a9aa53701c30888509d4f5f2b003a").Hex(), tc) mockJSONRPC.On( "BatchCallContext", ctx, diff --git a/optimism/suite_mocks.go b/optimism/suite_mocks.go new file mode 100644 index 0000000..9ef09a9 --- /dev/null +++ b/optimism/suite_mocks.go @@ -0,0 +1,49 @@ +package optimism + +import ( + "os" + + mocks "github.com/inphi/optimism-rosetta/mocks/optimism" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +type mocking interface { + MockJSONRPC() *mocks.JSONRPC + NoError(error error, msgAndArgs ...interface{}) bool +} + +type simpleMocking struct { + m *mocks.JSONRPC + assertions *assert.Assertions +} + +func (m *simpleMocking) MockJSONRPC() *mocks.JSONRPC { + return m.m +} + +func (m *simpleMocking) NoError(err error, msgAndArgs ...interface{}) bool { + return m.assertions.NoError(err, msgAndArgs...) +} + +func mockTraceTransaction(ctx interface{}, m mocking, txFileData string, rpcArgs ...interface{}) { + rpc := m.MockJSONRPC() + args := []interface{}{ctx, mock.Anything, "debug_traceTransaction"} + args = append(args, rpcArgs...) + if len(rpcArgs) == 0 { + args = append(args, mock.Anything, mock.Anything) + } else if len(rpcArgs) == 1 { + args = append(args, mock.Anything) + } + + rpc.On("CallContext", args...).Return(nil).Run( + func(args mock.Arguments) { + r := args.Get(1).(**Call) + file, err := os.ReadFile(txFileData) + m.NoError(err) + call := new(Call) + m.NoError(call.UnmarshalJSON(file)) + *r = call + }, + ).Once() +}