|
| 1 | +package ethmonitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/0xsequence/ethkit/go-ethereum/common" |
| 10 | + "github.com/0xsequence/ethkit/go-ethereum/core/types" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func canonicalTestBlock(num int64) *Block { |
| 15 | + return &Block{ |
| 16 | + Block: types.NewBlockWithHeader(&types.Header{ |
| 17 | + Number: big.NewInt(num), |
| 18 | + BlockHash: common.BigToHash(big.NewInt(num)), |
| 19 | + ParentHash: common.BigToHash(big.NewInt(num - 1)), |
| 20 | + Time: uint64(num), |
| 21 | + }), |
| 22 | + Event: Added, |
| 23 | + OK: true, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func pushCanonicalTestBlock(t *testing.T, chain *Chain, input *Block) *Block { |
| 28 | + t.Helper() |
| 29 | + chain.push(input) |
| 30 | + block := chain.Head() |
| 31 | + require.NotNil(t, block) |
| 32 | + require.Equal(t, input.Hash(), block.Hash()) |
| 33 | + return block |
| 34 | +} |
| 35 | + |
| 36 | +// The interface keeps the before-fix proof executable without the new method. |
| 37 | +func canonicalTestState(t *testing.T, block *Block) (uint64, bool) { |
| 38 | + t.Helper() |
| 39 | + state, ok := any(block).(interface{ CanonicalState() (uint64, bool) }) |
| 40 | + if !ok { |
| 41 | + return 0, false |
| 42 | + } |
| 43 | + return state.CanonicalState() |
| 44 | +} |
| 45 | + |
| 46 | +func TestBlockCanonicalStateRetention(t *testing.T) { |
| 47 | + for _, depth := range []int64{1, 3} { |
| 48 | + t.Run(fmt.Sprintf("evictionDepth=%d", depth), func(t *testing.T) { |
| 49 | + chain := newChain(10, false) |
| 50 | + block := pushCanonicalTestBlock(t, chain, canonicalTestBlock(100)) |
| 51 | + copy := chain.Blocks().Copy()[0] |
| 52 | + incarnation, canonical := canonicalTestState(t, block) |
| 53 | + require.Positive(t, incarnation) |
| 54 | + require.True(t, canonical) |
| 55 | + for num := int64(101); num <= 109+depth; num++ { |
| 56 | + pushCanonicalTestBlock(t, chain, canonicalTestBlock(num)) |
| 57 | + } |
| 58 | + require.Nil(t, chain.GetBlock(block.Hash())) |
| 59 | + for _, snapshot := range []*Block{block, copy} { |
| 60 | + got, canonical := canonicalTestState(t, snapshot) |
| 61 | + require.Equal(t, incarnation, got) |
| 62 | + require.True(t, canonical, "retention eviction was mistaken for removal") |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestBlockCanonicalStateReadoption(t *testing.T) { |
| 69 | + chain := newChain(10, false) |
| 70 | + input := canonicalTestBlock(100) |
| 71 | + block := pushCanonicalTestBlock(t, chain, input) |
| 72 | + shallow := *block |
| 73 | + copy := chain.Blocks().Copy()[0] |
| 74 | + incarnation, canonical := canonicalTestState(t, block) |
| 75 | + require.Positive(t, incarnation) |
| 76 | + require.True(t, canonical) |
| 77 | + removed := *chain.pop() |
| 78 | + removed.Event = Removed |
| 79 | + for _, snapshot := range []*Block{block, &shallow, copy, &removed} { |
| 80 | + got, canonical := canonicalTestState(t, snapshot) |
| 81 | + require.Equal(t, incarnation, got) |
| 82 | + require.False(t, canonical, "snapshot did not observe the actual removal") |
| 83 | + } |
| 84 | + |
| 85 | + // Reusing either the original input or a removed snapshot must create a fresh |
| 86 | + // owned incarnation without mutating old Added/Removed event copies. |
| 87 | + for _, reused := range []*Block{input, copy} { |
| 88 | + fresh := pushCanonicalTestBlock(t, chain, reused) |
| 89 | + got, canonical := canonicalTestState(t, fresh) |
| 90 | + require.Greater(t, got, incarnation) |
| 91 | + require.True(t, canonical) |
| 92 | + for _, snapshot := range []*Block{block, &shallow, copy, &removed} { |
| 93 | + got, canonical := canonicalTestState(t, snapshot) |
| 94 | + require.Equal(t, incarnation, got) |
| 95 | + require.False(t, canonical, "fresh readoption revived an old event") |
| 96 | + } |
| 97 | + chain.pop() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestBlockCanonicalStateBootstrap(t *testing.T) { |
| 102 | + for _, count := range []int{1, 3} { |
| 103 | + for _, serialized := range []bool{false, true} { |
| 104 | + t.Run(fmt.Sprintf("blocks=%d/JSON=%v", count, serialized), func(t *testing.T) { |
| 105 | + inputs := make(Blocks, count) |
| 106 | + for i := range inputs { |
| 107 | + inputs[i] = canonicalTestBlock(int64(100 + i)) |
| 108 | + } |
| 109 | + chain := newChain(10, true) |
| 110 | + if serialized { |
| 111 | + data, err := json.Marshal(inputs) |
| 112 | + require.NoError(t, err) |
| 113 | + require.NoError(t, chain.BootstrapFromBlocksJSON(data)) |
| 114 | + } else { |
| 115 | + require.NoError(t, chain.BootstrapFromBlocks(inputs)) |
| 116 | + } |
| 117 | + for _, block := range chain.Blocks().Copy() { |
| 118 | + incarnation, canonical := canonicalTestState(t, block) |
| 119 | + require.Positive(t, incarnation) |
| 120 | + require.True(t, canonical) |
| 121 | + data, err := json.Marshal(block) |
| 122 | + require.NoError(t, err) |
| 123 | + require.NotContains(t, string(data), "incarnation") |
| 124 | + require.NoError(t, json.Unmarshal(data, block)) |
| 125 | + incarnation, canonical = canonicalTestState(t, block) |
| 126 | + require.Zero(t, incarnation, "serialized state preserved runtime ownership") |
| 127 | + require.False(t, canonical) |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestBlockCanonicalStateConcurrentRemoval(t *testing.T) { |
| 135 | + chain := newChain(10, false) |
| 136 | + input := canonicalTestBlock(100) |
| 137 | + block := pushCanonicalTestBlock(t, chain, input) |
| 138 | + incarnation, _ := canonicalTestState(t, block) |
| 139 | + require.Positive(t, incarnation) |
| 140 | + state := any(block).(interface{ CanonicalState() (uint64, bool) }) |
| 141 | + start, done := make(chan struct{}), make(chan struct{}) |
| 142 | + result := make(chan error, 1) |
| 143 | + go func() { |
| 144 | + close(start) |
| 145 | + removed := false |
| 146 | + for { |
| 147 | + got, canonical := state.CanonicalState() |
| 148 | + if got != incarnation || (removed && canonical) { |
| 149 | + result <- fmt.Errorf("old incarnation changed or revived: id=%d canonical=%v", got, canonical) |
| 150 | + return |
| 151 | + } |
| 152 | + removed = removed || !canonical |
| 153 | + select { |
| 154 | + case <-done: |
| 155 | + result <- nil |
| 156 | + return |
| 157 | + default: |
| 158 | + } |
| 159 | + } |
| 160 | + }() |
| 161 | + <-start |
| 162 | + for i := 0; i < 100; i++ { |
| 163 | + chain.pop() |
| 164 | + pushCanonicalTestBlock(t, chain, input) |
| 165 | + } |
| 166 | + close(done) |
| 167 | + require.NoError(t, <-result) |
| 168 | + got, canonical := state.CanonicalState() |
| 169 | + require.Equal(t, incarnation, got) |
| 170 | + require.False(t, canonical) |
| 171 | +} |
| 172 | + |
| 173 | +func TestBlockCanonicalStateUntracked(t *testing.T) { |
| 174 | + for _, block := range []*Block{nil, canonicalTestBlock(100)} { |
| 175 | + incarnation, canonical := canonicalTestState(t, block) |
| 176 | + require.Zero(t, incarnation) |
| 177 | + require.False(t, canonical) |
| 178 | + } |
| 179 | +} |
0 commit comments