|
| 1 | +// Copyright (C) 2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package blocks |
| 5 | + |
| 6 | +import ( |
| 7 | + "math/big" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ava-labs/avalanchego/utils/logging" |
| 11 | + "github.com/ava-labs/libevm/core/types" |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/ava-labs/strevm/saetest" |
| 17 | +) |
| 18 | + |
| 19 | +func newEthBlock(num, time uint64, parent *types.Block) *types.Block { |
| 20 | + hdr := &types.Header{ |
| 21 | + Number: new(big.Int).SetUint64(num), |
| 22 | + Time: time, |
| 23 | + } |
| 24 | + if parent != nil { |
| 25 | + hdr.ParentHash = parent.Hash() |
| 26 | + } |
| 27 | + return types.NewBlockWithHeader(hdr) |
| 28 | +} |
| 29 | + |
| 30 | +func newBlock(tb testing.TB, eth *types.Block, parent, lastSettled *Block) *Block { |
| 31 | + tb.Helper() |
| 32 | + b, err := New(eth, parent, lastSettled, saetest.NewTBLogger(tb, logging.Warn)) |
| 33 | + require.NoError(tb, err, "New()") |
| 34 | + return b |
| 35 | +} |
| 36 | + |
| 37 | +func newChain(tb testing.TB, startHeight, total uint64, lastSettledAtHeight map[uint64]uint64) []*Block { |
| 38 | + tb.Helper() |
| 39 | + |
| 40 | + var ( |
| 41 | + ethParent *types.Block |
| 42 | + parent *Block |
| 43 | + blocks []*Block |
| 44 | + ) |
| 45 | + byNum := make(map[uint64]*Block) |
| 46 | + |
| 47 | + for i := range total { |
| 48 | + n := startHeight + i |
| 49 | + |
| 50 | + var ( |
| 51 | + settle *Block |
| 52 | + synchronous bool |
| 53 | + ) |
| 54 | + if s, ok := lastSettledAtHeight[n]; ok { |
| 55 | + if s == n { |
| 56 | + require.Zero(tb, s, "Only genesis block is self-settling") |
| 57 | + synchronous = true |
| 58 | + } else { |
| 59 | + require.Less(tb, s, n, "Last-settled height MUST be <= current height") |
| 60 | + settle = byNum[s] |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + b := newBlock(tb, newEthBlock(n, n /*time*/, ethParent), parent, settle) |
| 65 | + byNum[n] = b |
| 66 | + blocks = append(blocks, b) |
| 67 | + if synchronous { |
| 68 | + require.NoError(tb, b.MarkSynchronous(), "MarkSynchronous()") |
| 69 | + } |
| 70 | + |
| 71 | + parent = byNum[n] |
| 72 | + ethParent = parent.EthBlock() |
| 73 | + } |
| 74 | + |
| 75 | + return blocks |
| 76 | +} |
| 77 | + |
| 78 | +func TestSetAncestors(t *testing.T) { |
| 79 | + parent := newBlock(t, newEthBlock(5, 5, nil), nil, nil) |
| 80 | + lastSettled := newBlock(t, newEthBlock(3, 0, nil), nil, nil) |
| 81 | + child := newEthBlock(6, 6, parent.EthBlock()) |
| 82 | + |
| 83 | + t.Run("incorrect_parent", func(t *testing.T) { |
| 84 | + // Note that the arguments to [New] are inverted. |
| 85 | + _, err := New(child, lastSettled, parent, logging.NoLog{}) |
| 86 | + require.ErrorIs(t, err, errParentHashMismatch, "New() with inverted parent and last-settled blocks") |
| 87 | + }) |
| 88 | + |
| 89 | + source := newBlock(t, child, parent, lastSettled) |
| 90 | + dest := newBlock(t, child, nil, nil) |
| 91 | + |
| 92 | + t.Run("destination_before_copy", func(t *testing.T) { |
| 93 | + assert.Nilf(t, dest.ParentBlock(), "%T.ParentBlock()", dest) |
| 94 | + assert.Nilf(t, dest.LastSettled(), "%T.LastSettled()", dest) |
| 95 | + }) |
| 96 | + if t.Failed() { |
| 97 | + t.FailNow() |
| 98 | + } |
| 99 | + |
| 100 | + require.NoError(t, dest.CopyAncestorsFrom(source), "CopyAncestorsFrom()") |
| 101 | + if diff := cmp.Diff(source, dest, CmpOpt()); diff != "" { |
| 102 | + t.Errorf("After %T.CopyAncestorsFrom(); diff (-want +got):\n%s", dest, diff) |
| 103 | + } |
| 104 | + |
| 105 | + t.Run("incompatible_destination_block", func(t *testing.T) { |
| 106 | + ethB := newEthBlock(dest.Height()+1 /*mismatch*/, dest.BuildTime(), parent.EthBlock()) |
| 107 | + dest := newBlock(t, ethB, nil, nil) |
| 108 | + require.ErrorIs(t, dest.CopyAncestorsFrom(source), errHashMismatch) |
| 109 | + }) |
| 110 | +} |
0 commit comments