|
| 1 | +// Copyright 2023 Coinbase, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package optimism |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "math/big" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + RosettaTypes "github.com/coinbase/rosetta-sdk-go/types" |
| 25 | + "github.com/ethereum-optimism/optimism/l2geth/params" |
| 26 | + "github.com/ethereum-optimism/optimism/l2geth/rpc" |
| 27 | + EthCommon "github.com/ethereum/go-ethereum/common" |
| 28 | + mocks "github.com/inphi/optimism-rosetta/mocks/optimism" |
| 29 | + "github.com/stretchr/testify/mock" |
| 30 | + "github.com/stretchr/testify/suite" |
| 31 | + "golang.org/x/sync/semaphore" |
| 32 | +) |
| 33 | + |
| 34 | +type ClientEcotoneTestSuite struct { |
| 35 | + suite.Suite |
| 36 | + |
| 37 | + mockJSONRPC *mocks.JSONRPC |
| 38 | + mockGraphQL *mocks.GraphQL |
| 39 | + mockCurrencyFetcher *mocks.CurrencyFetcher |
| 40 | +} |
| 41 | + |
| 42 | +func (t *ClientEcotoneTestSuite) MockJSONRPC() *mocks.JSONRPC { |
| 43 | + return t.mockJSONRPC |
| 44 | +} |
| 45 | + |
| 46 | +func TestClientEcotone(t *testing.T) { |
| 47 | + suite.Run(t, new(ClientEcotoneTestSuite)) |
| 48 | +} |
| 49 | + |
| 50 | +func (testSuite *ClientEcotoneTestSuite) SetupTest() { |
| 51 | + testSuite.mockJSONRPC = &mocks.JSONRPC{} |
| 52 | + testSuite.mockGraphQL = &mocks.GraphQL{} |
| 53 | + testSuite.mockCurrencyFetcher = &mocks.CurrencyFetcher{} |
| 54 | +} |
| 55 | + |
| 56 | +func (testSuite *ClientEcotoneTestSuite) TestEcotoneBlock() { |
| 57 | + c := &Client{ |
| 58 | + c: testSuite.mockJSONRPC, |
| 59 | + g: testSuite.mockGraphQL, |
| 60 | + currencyFetcher: testSuite.mockCurrencyFetcher, |
| 61 | + tc: testBedrockTraceConfig, |
| 62 | + p: params.TestnetChainConfig, |
| 63 | + traceSemaphore: semaphore.NewWeighted(100), |
| 64 | + filterTokens: false, |
| 65 | + bedrockBlock: big.NewInt(0), |
| 66 | + } |
| 67 | + |
| 68 | + ctx := context.Background() |
| 69 | + testSuite.mockJSONRPC.On( |
| 70 | + "CallContext", |
| 71 | + ctx, |
| 72 | + mock.Anything, |
| 73 | + "eth_getBlockByNumber", |
| 74 | + "latest", |
| 75 | + true, |
| 76 | + ).Return( |
| 77 | + nil, |
| 78 | + ).Run( |
| 79 | + func(args mock.Arguments) { |
| 80 | + r := args.Get(1).(*json.RawMessage) |
| 81 | + file, err := os.ReadFile("testdata/sepolia_ecotone_block_4089330.json") |
| 82 | + testSuite.NoError(err) |
| 83 | + *r = json.RawMessage(file) |
| 84 | + }, |
| 85 | + ).Once() |
| 86 | + testSuite.mockJSONRPC.On( |
| 87 | + "CallContext", |
| 88 | + ctx, |
| 89 | + mock.Anything, |
| 90 | + "eth_getBlockByNumber", |
| 91 | + []interface{}{"latest", true}, |
| 92 | + ).Return( |
| 93 | + nil, |
| 94 | + ).Run( |
| 95 | + func(args mock.Arguments) { |
| 96 | + r := args.Get(1).(*json.RawMessage) |
| 97 | + file, err := os.ReadFile("testdata/sepolia_ecotone_block_4089330.json") |
| 98 | + testSuite.NoError(err) |
| 99 | + *r = json.RawMessage(file) |
| 100 | + }, |
| 101 | + ).Once() |
| 102 | + |
| 103 | + tx1 := EthCommon.HexToHash("0xd62fb327dc8df4e8f6a1ad70c9ff03099d2f85c75b9d943d5d6885e62be31069") |
| 104 | + |
| 105 | + // Execute the transaction trace |
| 106 | + mockTraceTransaction(ctx, testSuite, "testdata/sepolia_ecotone_tx_trace_5003318_1.json") |
| 107 | + mockGetEcotoneTransactionReceipt(ctx, testSuite, []EthCommon.Hash{tx1}, []string{"testdata/sepolia_ecotone_tx_receipt_4089330_1.json"}) |
| 108 | + |
| 109 | + correctRaw, err := os.ReadFile("testdata/sepolia_ecotone_block_response_4089330.json") |
| 110 | + testSuite.NoError(err) |
| 111 | + var correct *RosettaTypes.BlockResponse |
| 112 | + testSuite.NoError(json.Unmarshal(correctRaw, &correct)) |
| 113 | + |
| 114 | + // Fetch the latest block and validate |
| 115 | + resp, err := c.Block(ctx, nil) |
| 116 | + testSuite.NoError(err) |
| 117 | + testSuite.Equal(correct.Block, resp) |
| 118 | +} |
| 119 | + |
| 120 | +func mockGetEcotoneTransactionReceipt(ctx context.Context, testSuite *ClientEcotoneTestSuite, txhashes []EthCommon.Hash, txFileData []string) { |
| 121 | + testSuite.Equal(len(txhashes), len(txFileData)) |
| 122 | + numReceipts := len(txhashes) |
| 123 | + testSuite.mockJSONRPC.On( |
| 124 | + "BatchCallContext", |
| 125 | + ctx, |
| 126 | + mock.MatchedBy(func(rpcs []rpc.BatchElem) bool { |
| 127 | + return len(rpcs) == numReceipts && rpcs[0].Method == "eth_getTransactionReceipt" |
| 128 | + }), |
| 129 | + ).Return( |
| 130 | + nil, |
| 131 | + ).Run( |
| 132 | + func(args mock.Arguments) { |
| 133 | + r := args.Get(1).([]rpc.BatchElem) |
| 134 | + testSuite.Len(r, numReceipts) |
| 135 | + for i := range txhashes { |
| 136 | + testSuite.Equal( |
| 137 | + txhashes[i].Hex(), |
| 138 | + r[i].Args[0], |
| 139 | + ) |
| 140 | + file, err := os.ReadFile(txFileData[i]) |
| 141 | + testSuite.NoError(err) |
| 142 | + *(r[i].Result.(*json.RawMessage)) = json.RawMessage(file) |
| 143 | + } |
| 144 | + }, |
| 145 | + ).Once() |
| 146 | +} |
0 commit comments