|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math" |
| 6 | + "testing" |
| 7 | + |
| 8 | + ethtypes "github.com/ethereum/go-ethereum/core/types" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + commonclient "github.com/smartcontractkit/chainlink/v2/common/client" |
| 12 | + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/chaintype" |
| 13 | + "github.com/smartcontractkit/chainlink/v2/core/logger" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRPCClient_MakeLogsValid(t *testing.T) { |
| 17 | + testCases := []struct { |
| 18 | + Name string |
| 19 | + TxIndex uint |
| 20 | + LogIndex uint |
| 21 | + ExpectedLogIndex uint |
| 22 | + ExpectedError error |
| 23 | + }{ |
| 24 | + { |
| 25 | + Name: "TxIndex = 0 LogIndex = 0", |
| 26 | + TxIndex: 0, |
| 27 | + LogIndex: 0, |
| 28 | + ExpectedLogIndex: 0, |
| 29 | + ExpectedError: nil, |
| 30 | + }, |
| 31 | + { |
| 32 | + Name: "TxIndex = 0 LogIndex = 1", |
| 33 | + TxIndex: 0, |
| 34 | + LogIndex: 1, |
| 35 | + ExpectedLogIndex: 1, |
| 36 | + ExpectedError: nil, |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "TxIndex = 0 LogIndex = MaxUint32", |
| 40 | + TxIndex: 0, |
| 41 | + LogIndex: math.MaxUint32, |
| 42 | + ExpectedLogIndex: math.MaxUint32, |
| 43 | + ExpectedError: nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "LogIndex = MaxUint32 + 1 => returns an error", |
| 47 | + TxIndex: 0, |
| 48 | + LogIndex: math.MaxUint32 + 1, |
| 49 | + ExpectedLogIndex: 0, |
| 50 | + ExpectedError: errors.New("log's index 4294967296 of tx 0x0000000000000000000000000000000000000000000000000000000000000000 exceeds max supported value of 4294967295"), |
| 51 | + }, |
| 52 | + { |
| 53 | + Name: "TxIndex = 1 LogIndex = 0", |
| 54 | + TxIndex: 1, |
| 55 | + LogIndex: 0, |
| 56 | + ExpectedLogIndex: math.MaxUint32 + 1, |
| 57 | + ExpectedError: nil, |
| 58 | + }, |
| 59 | + { |
| 60 | + Name: "TxIndex = MaxUint32 LogIndex = MaxUint32", |
| 61 | + TxIndex: math.MaxUint32, |
| 62 | + LogIndex: math.MaxUint32, |
| 63 | + ExpectedLogIndex: math.MaxUint64, |
| 64 | + ExpectedError: nil, |
| 65 | + }, |
| 66 | + { |
| 67 | + Name: "TxIndex = MaxUint32 + 1 => returns an error", |
| 68 | + TxIndex: math.MaxUint32 + 1, |
| 69 | + LogIndex: 0, |
| 70 | + ExpectedLogIndex: 0, |
| 71 | + ExpectedError: errors.New("TxIndex of tx 0x0000000000000000000000000000000000000000000000000000000000000000 exceeds max supported value of 4294967295"), |
| 72 | + }, |
| 73 | + } |
| 74 | + for _, tc := range testCases { |
| 75 | + t.Run(tc.Name, func(t *testing.T) { |
| 76 | + rpc := NewRPCClient(TestNodePoolConfig{}, logger.TestLogger(t), nil, nil, "eth-primary-rpc-0", 0, nil, commonclient.Primary, commonclient.QueryTimeout, commonclient.QueryTimeout, "") |
| 77 | + log, err := rpc.makeLogValid(ethtypes.Log{TxIndex: tc.TxIndex, Index: tc.LogIndex}) |
| 78 | + // non sei should return as is |
| 79 | + require.NoError(t, err) |
| 80 | + require.Equal(t, tc.TxIndex, log.TxIndex) |
| 81 | + require.Equal(t, tc.LogIndex, log.Index) |
| 82 | + seiRPC := NewRPCClient(TestNodePoolConfig{}, logger.TestLogger(t), nil, nil, "eth-primary-rpc-0", 0, nil, commonclient.Primary, commonclient.QueryTimeout, commonclient.QueryTimeout, chaintype.ChainSei) |
| 83 | + log, err = seiRPC.makeLogValid(ethtypes.Log{TxIndex: tc.TxIndex, Index: tc.LogIndex}) |
| 84 | + if tc.ExpectedError != nil { |
| 85 | + require.EqualError(t, err, tc.ExpectedError.Error()) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + require.Equal(t, tc.ExpectedLogIndex, log.Index) |
| 90 | + require.Equal(t, tc.TxIndex, log.TxIndex) |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
0 commit comments