Skip to content

Commit

Permalink
fix tests after 66e169a
Browse files Browse the repository at this point in the history
  • Loading branch information
Inphi committed Jan 23, 2024
1 parent 582e236 commit 49095a9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 228 deletions.
30 changes: 6 additions & 24 deletions optimism/client_bedrock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -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(
Expand Down
27 changes: 4 additions & 23 deletions optimism/client_blocks_bedrock_tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}
Expand Down Expand Up @@ -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()
}
159 changes: 9 additions & 150 deletions optimism/client_blocks_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 1 addition & 31 deletions optimism/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions optimism/suite_mocks.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 49095a9

Please sign in to comment.