|
| 1 | +package arbtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccessingPathSchemeState(t *testing.T) { |
| 15 | + ctx, cancel := context.WithCancel(context.Background()) |
| 16 | + defer cancel() |
| 17 | + builder := NewNodeBuilder(ctx).DefaultConfig(t, true) |
| 18 | + |
| 19 | + // This test is PathScheme specific, it shouldn't be run with HashScheme |
| 20 | + builder.RequireScheme(t, rawdb.PathScheme) |
| 21 | + |
| 22 | + // Build a node with history past the 128 block diff threshold |
| 23 | + cancelNode := buildWithHistory(t, ctx, builder, 150) |
| 24 | + execNode, l2client := builder.L2.ExecNode, builder.L2.Client |
| 25 | + defer cancelNode() |
| 26 | + bc := execNode.Backend.ArbInterface().BlockChain() |
| 27 | + |
| 28 | + header := bc.CurrentBlock() |
| 29 | + if header == nil { |
| 30 | + Fatal(t, "failed to get current block header") |
| 31 | + } |
| 32 | + |
| 33 | + head := header.Number.Uint64() |
| 34 | + if head < 129 { |
| 35 | + t.Fatalf("chain height (%d) too low — need at least 129 blocks to check last 128", head) |
| 36 | + } |
| 37 | + start := head - 128 |
| 38 | + |
| 39 | + for height := head; height > start; height-- { |
| 40 | + _, err := l2client.BalanceAt(ctx, GetTestAddressForAccountName(t, "User2"), new(big.Int).SetUint64(height)) |
| 41 | + Require(t, err, "Failed to get balance at height", height) |
| 42 | + } |
| 43 | + |
| 44 | + // Now try to access state older than 128 blocks ago, which should be missing |
| 45 | + // |
| 46 | + // We don't want to see the error |
| 47 | + // `missing trie node X (path ) state Y is not available, not found` |
| 48 | + // because that indicates a failure to find data that should exist. Implying our state backend has a bug. |
| 49 | + heightWhereStateShouldBeMissing := head - 129 |
| 50 | + _, err := l2client.BalanceAt(ctx, GetTestAddressForAccountName(t, "User2"), new(big.Int).SetUint64(heightWhereStateShouldBeMissing)) |
| 51 | + require.Error(t, err, "expected BalanceAt to fail for missing historical state") |
| 52 | + require.Contains(t, err.Error(), "historical state", "unexpected error message: %v", err) |
| 53 | + require.Contains(t, err.Error(), "is not available", "unexpected error message: %v", err) |
| 54 | +} |
| 55 | + |
| 56 | +func TestAccessingPathSchemeArchivalState(t *testing.T) { |
| 57 | + ctx, cancel := context.WithCancel(context.Background()) |
| 58 | + defer cancel() |
| 59 | + builder := NewNodeBuilder(ctx).DefaultConfig(t, true) |
| 60 | + builder.execConfig.Caching.Archive = true |
| 61 | + builder.execConfig.Caching.StateHistory = 2 |
| 62 | + |
| 63 | + // This test is PathScheme specific, it shouldn't be run with HashScheme |
| 64 | + builder.RequireScheme(t, rawdb.PathScheme) |
| 65 | + |
| 66 | + // Build a node with history past the 128 block diff threshold |
| 67 | + cancelNode := buildWithHistory(t, ctx, builder, 150) |
| 68 | + fmt.Println("bluebird 5-3", builder.execConfig.Caching.StateScheme) |
| 69 | + execNode, l2client := builder.L2.ExecNode, builder.L2.Client |
| 70 | + defer cancelNode() |
| 71 | + bc := execNode.Backend.ArbInterface().BlockChain() |
| 72 | + |
| 73 | + header := bc.CurrentBlock() |
| 74 | + if header == nil { |
| 75 | + Fatal(t, "failed to get current block header") |
| 76 | + } |
| 77 | + |
| 78 | + head := header.Number.Uint64() |
| 79 | + if head < 132 { |
| 80 | + t.Fatalf("chain height (%d) too low — need at least 129 blocks to check last 128", head) |
| 81 | + } |
| 82 | + start := head - 131 |
| 83 | + |
| 84 | + for height := head; height > start; height-- { |
| 85 | + _, err := l2client.BalanceAt(ctx, GetTestAddressForAccountName(t, "User2"), new(big.Int).SetUint64(height)) |
| 86 | + Require(t, err, "Failed to get balance at height", height) |
| 87 | + } |
| 88 | + |
| 89 | + // Now try to access state older than 131 blocks ago, which should be missing |
| 90 | + heightWhereStateShouldBeMissing := head - 132 |
| 91 | + _, err := l2client.BalanceAt(ctx, GetTestAddressForAccountName(t, "User2"), new(big.Int).SetUint64(heightWhereStateShouldBeMissing)) |
| 92 | + require.Error(t, err, "expected BalanceAt to fail for missing historical state") |
| 93 | + // `metadata is not found` is the error returned when archival data is pruned for some reason |
| 94 | + require.Contains(t, err.Error(), "metadata is not found", "unexpected error message: %v", err) |
| 95 | +} |
0 commit comments