|
| 1 | +package arbtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/binary" |
| 7 | + "math/big" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/params" |
| 12 | + |
| 13 | + "github.com/offchainlabs/nitro/statetransfer" |
| 14 | +) |
| 15 | + |
| 16 | +func TestHistoricalBlockHash(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + ctx, cancel := context.WithCancel(context.Background()) |
| 19 | + defer cancel() |
| 20 | + |
| 21 | + builder := NewNodeBuilder(ctx).DefaultConfig(t, true) |
| 22 | + contractInfo := &statetransfer.AccountInitContractInfo{ |
| 23 | + Code: params.HistoryStorageCodeArbitrum, |
| 24 | + ContractStorage: make(map[common.Hash]common.Hash), |
| 25 | + } |
| 26 | + accountInfo := statetransfer.AccountInitializationInfo{ |
| 27 | + Addr: params.HistoryStorageAddress, |
| 28 | + EthBalance: big.NewInt(0), |
| 29 | + Nonce: 1, |
| 30 | + ContractInfo: contractInfo, |
| 31 | + } |
| 32 | + builder.L2Info.ArbInitData.Accounts = append(builder.L2Info.ArbInitData.Accounts, accountInfo) |
| 33 | + cleanup := builder.Build(t) |
| 34 | + defer cleanup() |
| 35 | + |
| 36 | + for { |
| 37 | + builder.L2.TransferBalance(t, "Faucet", "Faucet", common.Big1, builder.L2Info) |
| 38 | + number, err := builder.L2.Client.BlockNumber(ctx) |
| 39 | + Require(t, err) |
| 40 | + if number > 300 { |
| 41 | + break |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + block, err := builder.L2.Client.BlockByNumber(ctx, nil) |
| 46 | + Require(t, err) |
| 47 | + |
| 48 | + for i := uint64(0); i < block.Number().Uint64(); i++ { |
| 49 | + var key common.Hash |
| 50 | + binary.BigEndian.PutUint64(key[24:], i) |
| 51 | + expectedBlock, err := builder.L2.Client.BlockByNumber(ctx, new(big.Int).SetUint64(i)) |
| 52 | + Require(t, err) |
| 53 | + blockHash := sendContractCall(t, ctx, params.HistoryStorageAddress, builder.L2.Client, key.Bytes()) |
| 54 | + if !bytes.Equal(blockHash, expectedBlock.Hash().Bytes()) { |
| 55 | + t.Fatalf("Expected block hash %s, got %s", expectedBlock.Hash(), blockHash) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments