github.com/lmittmann/w3@v0.20.0/w3vm/fetcher_test.go (about) 1 package w3vm 2 3 import ( 4 "errors" 5 "strconv" 6 "testing" 7 8 "github.com/ethereum/go-ethereum/common" 9 "github.com/ethereum/go-ethereum/common/hexutil" 10 "github.com/google/go-cmp/cmp" 11 "github.com/holiman/uint256" 12 "github.com/lmittmann/w3/internal" 13 w3hexutil "github.com/lmittmann/w3/internal/hexutil" 14 ) 15 16 func TestTestdataContractsMerge(t *testing.T) { 17 tests := []struct { 18 Contracts testdataContracts 19 Other testdataContracts 20 WantErr error 21 WantLen int 22 }{ 23 { 24 Contracts: testdataContracts{}, 25 Other: testdataContracts{}, 26 WantLen: 0, 27 }, 28 { 29 Contracts: testdataContracts{}, 30 Other: testdataContracts{ 31 common.Hash{0x11}: []byte("code1"), 32 common.Hash{0x22}: []byte("code2"), 33 }, 34 WantLen: 2, 35 }, 36 { 37 Contracts: testdataContracts{ 38 common.Hash{0x11}: []byte("code1"), 39 }, 40 Other: testdataContracts{ 41 common.Hash{0x22}: []byte("code2"), 42 }, 43 WantLen: 2, 44 }, 45 { 46 Contracts: testdataContracts{ 47 common.Hash{0x11}: []byte("code1"), 48 }, 49 Other: testdataContracts{ 50 common.Hash{0x11}: []byte("code1"), 51 }, 52 WantLen: 1, 53 }, 54 { 55 Contracts: testdataContracts{ 56 common.Hash{0x11}: []byte("code1"), 57 }, 58 Other: testdataContracts{ 59 common.Hash{0x11}: []byte("different_code"), 60 }, 61 WantErr: errors.New("bytecode conflict for code hash 0x1100000000000000000000000000000000000000000000000000000000000000"), 62 WantLen: 1, 63 }, 64 } 65 66 for i, test := range tests { 67 t.Run(strconv.Itoa(i), func(t *testing.T) { 68 err := test.Contracts.Merge(test.Other) 69 if diff := cmp.Diff(test.WantErr, err, internal.EquateErrors()); diff != "" { 70 t.Fatalf("Err: (-want +got):\n%s", diff) 71 } 72 if len(test.Contracts) != test.WantLen { 73 t.Fatalf("Len: want %d, got %d", test.WantLen, len(test.Contracts)) 74 } 75 }) 76 } 77 } 78 79 func TestTestdataHeaderHashesMerge(t *testing.T) { 80 tests := []struct { 81 HeaderHashes testdataHeaderHashes 82 Other testdataHeaderHashes 83 WantErr error 84 WantLen int 85 }{ 86 { 87 HeaderHashes: testdataHeaderHashes{}, 88 Other: testdataHeaderHashes{}, 89 WantLen: 0, 90 }, 91 { 92 HeaderHashes: testdataHeaderHashes{}, 93 Other: testdataHeaderHashes{ 94 1: common.Hash{0x11}, 95 2: common.Hash{0x22}, 96 }, 97 WantLen: 2, 98 }, 99 { 100 HeaderHashes: testdataHeaderHashes{ 101 1: common.Hash{0x11}, 102 }, 103 Other: testdataHeaderHashes{ 104 2: common.Hash{0x22}, 105 }, 106 WantLen: 2, 107 }, 108 { 109 HeaderHashes: testdataHeaderHashes{ 110 1: common.Hash{0x11}, 111 }, 112 Other: testdataHeaderHashes{ 113 1: common.Hash{0x11}, 114 }, 115 WantLen: 1, 116 }, 117 { 118 HeaderHashes: testdataHeaderHashes{ 119 1: common.Hash{0x11}, 120 }, 121 Other: testdataHeaderHashes{ 122 1: common.Hash{0x22}, 123 }, 124 WantErr: errors.New("header hash conflict for block 1"), 125 WantLen: 1, 126 }, 127 } 128 129 for i, test := range tests { 130 t.Run(strconv.Itoa(i), func(t *testing.T) { 131 err := test.HeaderHashes.Merge(test.Other) 132 if diff := cmp.Diff(test.WantErr, err, internal.EquateErrors()); diff != "" { 133 t.Fatalf("Err: (-want +got):\n%s", diff) 134 } 135 if len(test.HeaderHashes) != test.WantLen { 136 t.Fatalf("Len: want %d, got %d", test.WantLen, len(test.HeaderHashes)) 137 } 138 }) 139 } 140 } 141 142 func TestTestdataAccountMerge(t *testing.T) { 143 tests := []struct { 144 Account *testdataAccount 145 Other *testdataAccount 146 WantErr error 147 }{ 148 { 149 Account: &testdataAccount{ 150 Nonce: 1, 151 Balance: (*hexutil.U256)(uint256.NewInt(1)), 152 CodeHash: common.Hash{0x11}, 153 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 154 {0x01}: {0xaa}, 155 }, 156 }, 157 Other: &testdataAccount{ 158 Nonce: 1, 159 Balance: (*hexutil.U256)(uint256.NewInt(1)), 160 CodeHash: common.Hash{0x11}, 161 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 162 {0x02}: {0xbb}, 163 }, 164 }, 165 }, 166 { 167 Account: &testdataAccount{ 168 Nonce: 1, 169 Balance: (*hexutil.U256)(uint256.NewInt(1)), 170 CodeHash: common.Hash{0x11}, 171 }, 172 Other: &testdataAccount{ 173 Nonce: 2, 174 Balance: (*hexutil.U256)(uint256.NewInt(1)), 175 CodeHash: common.Hash{0x11}, 176 }, 177 WantErr: errors.New("nonce conflict: 1 != 2"), 178 }, 179 { 180 Account: &testdataAccount{ 181 Nonce: 1, 182 Balance: (*hexutil.U256)(uint256.NewInt(1)), 183 CodeHash: common.Hash{0x11}, 184 }, 185 Other: &testdataAccount{ 186 Nonce: 1, 187 Balance: (*hexutil.U256)(uint256.NewInt(2)), 188 CodeHash: common.Hash{0x11}, 189 }, 190 WantErr: errors.New("balance conflict: 0x1 != 0x2"), 191 }, 192 { 193 Account: &testdataAccount{ 194 Nonce: 1, 195 Balance: (*hexutil.U256)(uint256.NewInt(1)), 196 CodeHash: common.Hash{0x11}, 197 }, 198 Other: &testdataAccount{ 199 Nonce: 1, 200 Balance: (*hexutil.U256)(uint256.NewInt(1)), 201 CodeHash: common.Hash{0x22}, 202 }, 203 WantErr: errors.New("code hash conflict: 0x1100000000000000000000000000000000000000000000000000000000000000 != 0x2200000000000000000000000000000000000000000000000000000000000000"), 204 }, 205 { 206 Account: &testdataAccount{ 207 Nonce: 1, 208 Balance: (*hexutil.U256)(uint256.NewInt(1)), 209 CodeHash: common.Hash{0x11}, 210 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 211 {0x01}: {0xaa}, 212 }, 213 }, 214 Other: &testdataAccount{ 215 Nonce: 1, 216 Balance: (*hexutil.U256)(uint256.NewInt(1)), 217 CodeHash: common.Hash{0x11}, 218 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 219 {0x01}: {0xbb}, 220 }, 221 }, 222 WantErr: errors.New("storage conflict at slot 0x0100000000000000000000000000000000000000000000000000000000000000: 0xaa00000000000000000000000000000000000000000000000000000000000000 != 0xbb00000000000000000000000000000000000000000000000000000000000000"), 223 }, 224 { 225 Account: &testdataAccount{ 226 Nonce: 1, 227 Balance: (*hexutil.U256)(uint256.NewInt(1)), 228 CodeHash: common.Hash{0x11}, 229 Storage: nil, 230 }, 231 Other: &testdataAccount{ 232 Nonce: 1, 233 Balance: (*hexutil.U256)(uint256.NewInt(1)), 234 CodeHash: common.Hash{0x11}, 235 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 236 {0x01}: {0xaa}, 237 }, 238 }, 239 }, 240 } 241 242 for i, test := range tests { 243 t.Run(strconv.Itoa(i), func(t *testing.T) { 244 err := test.Account.Merge(test.Other) 245 if diff := cmp.Diff(test.WantErr, err, internal.EquateErrors()); diff != "" { 246 t.Fatalf("Err: (-want +got):\n%s", diff) 247 } 248 249 if test.WantErr != nil { 250 return 251 } 252 if test.Account.Storage != nil && test.Other.Storage != nil { 253 for slot, value := range test.Other.Storage { 254 if test.Account.Storage[slot] != value { 255 t.Fatalf("Storage slot %s not properly merged", common.Hash(slot)) 256 } 257 } 258 } 259 }) 260 } 261 } 262 263 func TestTestdataStateMerge(t *testing.T) { 264 addr1 := common.Address{0x11} 265 addr2 := common.Address{0x22} 266 267 tests := []struct { 268 State testdataState 269 Other testdataState 270 WantErr error 271 WantLen int 272 }{ 273 { 274 State: testdataState{}, 275 Other: testdataState{}, 276 WantLen: 0, 277 }, 278 { 279 State: testdataState{}, 280 Other: testdataState{ 281 addr1: &testdataAccount{ 282 Nonce: hexutil.Uint64(1), 283 Balance: (*hexutil.U256)(uint256.NewInt(1)), 284 CodeHash: common.Hash{0x11}, 285 }, 286 }, 287 WantLen: 1, 288 }, 289 { 290 State: testdataState{ 291 addr1: &testdataAccount{ 292 Nonce: hexutil.Uint64(1), 293 Balance: (*hexutil.U256)(uint256.NewInt(1)), 294 CodeHash: common.Hash{0x11}, 295 }, 296 }, 297 Other: testdataState{ 298 addr2: &testdataAccount{ 299 Nonce: hexutil.Uint64(2), 300 Balance: (*hexutil.U256)(uint256.NewInt(1)), 301 CodeHash: common.Hash{0x22}, 302 }, 303 }, 304 WantLen: 2, 305 }, 306 { 307 State: testdataState{ 308 addr1: &testdataAccount{ 309 Nonce: hexutil.Uint64(1), 310 Balance: (*hexutil.U256)(uint256.NewInt(1)), 311 CodeHash: common.Hash{0x11}, 312 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 313 {0x01}: {0xaa}, 314 }, 315 }, 316 }, 317 Other: testdataState{ 318 addr1: &testdataAccount{ 319 Nonce: hexutil.Uint64(1), 320 Balance: (*hexutil.U256)(uint256.NewInt(1)), 321 CodeHash: common.Hash{0x11}, 322 Storage: map[w3hexutil.Hash]w3hexutil.Hash{ 323 {0x02}: {0xbb}, 324 }, 325 }, 326 }, 327 WantLen: 1, 328 }, 329 { 330 State: testdataState{ 331 addr1: &testdataAccount{ 332 Nonce: hexutil.Uint64(1), 333 Balance: (*hexutil.U256)(uint256.NewInt(1)), 334 CodeHash: common.Hash{0x11}, 335 }, 336 }, 337 Other: testdataState{ 338 addr1: &testdataAccount{ 339 Nonce: hexutil.Uint64(2), 340 Balance: (*hexutil.U256)(uint256.NewInt(1)), 341 CodeHash: common.Hash{0x11}, 342 }, 343 }, 344 WantErr: errors.New("account conflict for address 0x1100000000000000000000000000000000000000: nonce conflict: 1 != 2"), 345 WantLen: 1, 346 }, 347 } 348 349 for i, test := range tests { 350 t.Run(strconv.Itoa(i), func(t *testing.T) { 351 err := test.State.Merge(test.Other) 352 if diff := cmp.Diff(test.WantErr, err, internal.EquateErrors()); diff != "" { 353 t.Fatalf("Err: (-want +got):\n%s", diff) 354 } 355 if len(test.State) != test.WantLen { 356 t.Fatalf("Len: want %d, got %d", test.WantLen, len(test.State)) 357 } 358 }) 359 } 360 }