|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + gethtypes "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/gobitfly/beaconchain/pkg/commons/types" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + aliceAddress = common.HexToAddress("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5") |
| 15 | + bobAddress = common.HexToAddress("0x388C818CA8B9251b393131C08a736A67ccB19297") |
| 16 | +) |
| 17 | + |
| 18 | +func TestGetReceiver(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + tx *gethtypes.Transaction |
| 22 | + expected []byte |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "transaction with recipient", |
| 26 | + tx: gethtypes.NewTransaction(0, aliceAddress, big.NewInt(100), 100000, big.NewInt(10), []byte{}), |
| 27 | + expected: aliceAddress.Bytes(), |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "contract creation transaction", |
| 31 | + tx: gethtypes.NewContractCreation(0, big.NewInt(100), 100000, big.NewInt(10), []byte{}), |
| 32 | + expected: nil, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + result := getReceiver(tt.tx) |
| 39 | + if !bytes.Equal(result, tt.expected) { |
| 40 | + t.Errorf("got %v, want %v", result, tt.expected) |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestGetBlockWithdrawals(t *testing.T) { |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + blkWithdrawals gethtypes.Withdrawals |
| 50 | + expected []*types.Eth1Withdrawal |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "block with one withdrawal", |
| 54 | + blkWithdrawals: gethtypes.Withdrawals{ |
| 55 | + { |
| 56 | + Index: 1, |
| 57 | + Validator: 2, |
| 58 | + Address: aliceAddress, |
| 59 | + Amount: 100, |
| 60 | + }, |
| 61 | + }, |
| 62 | + expected: []*types.Eth1Withdrawal{ |
| 63 | + { |
| 64 | + Index: 1, |
| 65 | + ValidatorIndex: 2, |
| 66 | + Address: aliceAddress.Bytes(), |
| 67 | + Amount: big.NewInt(100).Bytes(), |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "block with two withdrawals", |
| 73 | + blkWithdrawals: gethtypes.Withdrawals{ |
| 74 | + { |
| 75 | + Index: 1, |
| 76 | + Validator: 2, |
| 77 | + Address: aliceAddress, |
| 78 | + Amount: 100, |
| 79 | + }, |
| 80 | + { |
| 81 | + Index: 2, |
| 82 | + Validator: 3, |
| 83 | + Address: bobAddress, |
| 84 | + Amount: 200, |
| 85 | + }, |
| 86 | + }, |
| 87 | + expected: []*types.Eth1Withdrawal{ |
| 88 | + { |
| 89 | + Index: 1, |
| 90 | + ValidatorIndex: 2, |
| 91 | + Address: aliceAddress.Bytes(), |
| 92 | + Amount: big.NewInt(100).Bytes(), |
| 93 | + }, |
| 94 | + { |
| 95 | + Index: 2, |
| 96 | + ValidatorIndex: 3, |
| 97 | + Address: bobAddress.Bytes(), |
| 98 | + Amount: big.NewInt(200).Bytes(), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "block with no withdrawals", |
| 104 | + blkWithdrawals: gethtypes.Withdrawals{}, |
| 105 | + expected: nil, |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + for _, tt := range tests { |
| 110 | + t.Run(tt.name, func(t *testing.T) { |
| 111 | + result := getBlockWithdrawals(tt.blkWithdrawals) |
| 112 | + if len(result) != len(tt.expected) { |
| 113 | + t.Fatalf("got %v withdrawals, want %v withdrawals", len(result), len(tt.expected)) |
| 114 | + } |
| 115 | + for i, withdrawal := range result { |
| 116 | + if withdrawal.Index != tt.expected[i].Index { |
| 117 | + t.Errorf("got Index %v, want %v", withdrawal.Index, tt.expected[i].Index) |
| 118 | + } |
| 119 | + if withdrawal.ValidatorIndex != tt.expected[i].ValidatorIndex { |
| 120 | + t.Errorf("got ValidatorIndex %v, want %v", withdrawal.ValidatorIndex, tt.expected[i].ValidatorIndex) |
| 121 | + } |
| 122 | + if !bytes.Equal(withdrawal.Address, tt.expected[i].Address) { |
| 123 | + t.Errorf("got Address %v, want %v", withdrawal.Address, tt.expected[i].Address) |
| 124 | + } |
| 125 | + if !bytes.Equal(withdrawal.Amount, tt.expected[i].Amount) { |
| 126 | + t.Errorf("got Amount %v, want %v", withdrawal.Amount, tt.expected[i].Amount) |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestGetLogsFromReceipts(t *testing.T) { |
| 134 | + tests := []struct { |
| 135 | + name string |
| 136 | + logs []*gethtypes.Log |
| 137 | + expected []*types.Eth1Log |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "receipt with one log", |
| 141 | + logs: []*gethtypes.Log{ |
| 142 | + { |
| 143 | + Address: aliceAddress, |
| 144 | + Topics: []common.Hash{common.HexToHash("0x123"), common.HexToHash("0x234")}, |
| 145 | + Data: []byte("data"), |
| 146 | + Removed: false, |
| 147 | + }, |
| 148 | + }, |
| 149 | + expected: []*types.Eth1Log{ |
| 150 | + { |
| 151 | + Address: aliceAddress.Bytes(), |
| 152 | + Topics: [][]byte{common.HexToHash("0x123").Bytes(), common.HexToHash("0x234").Bytes()}, |
| 153 | + Data: []byte("data"), |
| 154 | + Removed: false, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "receipt with two logs", |
| 160 | + logs: []*gethtypes.Log{ |
| 161 | + { |
| 162 | + Address: aliceAddress, |
| 163 | + Topics: []common.Hash{common.HexToHash("0x123"), common.HexToHash("0x234")}, |
| 164 | + Data: []byte("data"), |
| 165 | + Removed: false, |
| 166 | + }, |
| 167 | + { |
| 168 | + Address: bobAddress, |
| 169 | + Topics: []common.Hash{common.HexToHash("0x123"), common.HexToHash("0x234")}, |
| 170 | + Data: []byte("data"), |
| 171 | + Removed: false, |
| 172 | + }, |
| 173 | + }, |
| 174 | + expected: []*types.Eth1Log{ |
| 175 | + { |
| 176 | + Address: aliceAddress.Bytes(), |
| 177 | + Topics: [][]byte{common.HexToHash("0x123").Bytes(), common.HexToHash("0x234").Bytes()}, |
| 178 | + Data: []byte("data"), |
| 179 | + Removed: false, |
| 180 | + }, |
| 181 | + { |
| 182 | + Address: bobAddress.Bytes(), |
| 183 | + Topics: [][]byte{common.HexToHash("0x123").Bytes(), common.HexToHash("0x234").Bytes()}, |
| 184 | + Data: []byte("data"), |
| 185 | + Removed: false, |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "receipt with no logs", |
| 191 | + logs: []*gethtypes.Log{}, |
| 192 | + expected: nil, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "receipt with logs but no topics", |
| 196 | + logs: []*gethtypes.Log{ |
| 197 | + { |
| 198 | + Address: aliceAddress, |
| 199 | + Topics: []common.Hash{}, |
| 200 | + Data: []byte("data"), |
| 201 | + Removed: false, |
| 202 | + }, |
| 203 | + }, |
| 204 | + expected: []*types.Eth1Log{ |
| 205 | + { |
| 206 | + Address: aliceAddress.Bytes(), |
| 207 | + Topics: [][]byte{}, |
| 208 | + Data: []byte("data"), |
| 209 | + Removed: false, |
| 210 | + }, |
| 211 | + }, |
| 212 | + }, |
| 213 | + } |
| 214 | + |
| 215 | + for _, tt := range tests { |
| 216 | + t.Run(tt.name, func(t *testing.T) { |
| 217 | + result := getLogsFromReceipts(tt.logs) |
| 218 | + if len(result) != len(tt.expected) { |
| 219 | + t.Fatalf("got %v logs, want %v logs", len(result), len(tt.expected)) |
| 220 | + } |
| 221 | + for i, log := range result { |
| 222 | + if !bytes.Equal(log.Address, tt.expected[i].Address) { |
| 223 | + t.Errorf("got Address %v, want %v", log.Address, tt.expected[i].Address) |
| 224 | + } |
| 225 | + if !bytes.Equal(log.Data, tt.expected[i].Data) { |
| 226 | + t.Errorf("got Data %v, want %v", log.Data, tt.expected[i].Data) |
| 227 | + } |
| 228 | + if log.Removed != tt.expected[i].Removed { |
| 229 | + t.Errorf("got Removed %v, want %v", log.Removed, tt.expected[i].Removed) |
| 230 | + } |
| 231 | + for j, topic := range log.Topics { |
| 232 | + if !bytes.Equal(topic, tt.expected[i].Topics[j]) { |
| 233 | + t.Errorf("got Topic %v, want %v", topic, tt.expected[i].Topics[j]) |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + }) |
| 238 | + } |
| 239 | +} |
0 commit comments