This repository was archived by the owner on Jun 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecovery_test.go
More file actions
150 lines (132 loc) · 4.68 KB
/
Copy pathrecovery_test.go
File metadata and controls
150 lines (132 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (C) 2025-2026, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package sae
import (
"context"
"math/big"
"math/rand/v2"
"testing"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/libevm/options"
"github.com/ava-labs/libevm/params"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"github.com/ava-labs/strevm/blocks"
"github.com/ava-labs/strevm/cmputils"
saeparams "github.com/ava-labs/strevm/params"
"github.com/ava-labs/strevm/saedb"
"github.com/ava-labs/strevm/saetest"
)
func TestRecoverFromDatabase(t *testing.T) {
t.Parallel()
sutOpt, vmTime := withVMTime(t, time.Unix(saeparams.TauSeconds, 0))
var srcDB database.Database
srcHDB := saetest.NewHeightIndexDB()
ctx, src := newSUT(t, 1, sutOpt, withExecResultsDB(srcHDB), options.Func[sutConfig](func(c *sutConfig) {
srcDB = c.db
c.logLevel = logging.Warn
}))
srcCtx := ctx
rng := rand.New(rand.NewPCG(0, 0)) //nolint:gosec // Deterministic replay for tests
for final := false; !final; {
// We need to test rebuilding from trie roots reflecting (a) the last
// synchronous block; (b) some committed state root; and (c) a few
// blocks before/after the thresholds. Everything in between is merely
// to advance the block number so is treated as a "quick" loop
// iteration.
last := src.lastAcceptedBlock(t)
height := last.Height()
quick := height < saedb.CommitTrieDBEvery && src.rawVM.last.settled.Load().Height() > 1
final = height > saedb.CommitTrieDBEvery
if !quick {
src.mustSendTx(t, src.wallet.SetNonceAndSign(t, 0, &types.LegacyTx{
To: nil, // execute `Data` as code for contract "construction"
Data: []byte{byte(vm.INVALID)}, // revert and consume all gas
Gas: params.TxGas + params.CreateGas + params.TxDataNonZeroGasFrontier + rng.Uint64N(2e6),
GasPrice: big.NewInt(100),
}))
}
vmTime.advance(850 * time.Millisecond)
b := src.runConsensusLoop(t)
if !quick {
require.Len(t, b.Transactions(), 1, "transactions in block")
}
require.NoErrorf(t, b.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", b)
if quick {
continue
}
t.Run("recover", func(t *testing.T) {
newDB := memdb.New()
it := srcDB.NewIterator()
for it.Next() {
require.NoError(t, newDB.Put(it.Key(), it.Value()))
}
require.NoError(t, it.Error())
sutCtx, sut := newSUT(t, 1, sutOpt, withExecResultsDB(srcHDB.Clone()), options.Func[sutConfig](func(c *sutConfig) {
c.db = newDB
c.logLevel = logging.Warn
}))
if final {
t.Run("build_on_recovered_VM", func(t *testing.T) {
srcLast := src.lastAcceptedBlock(t)
sutLast := sut.lastAcceptedBlock(t)
if diff := cmp.Diff(srcLast, sutLast, blocks.CmpOpt()); diff != "" {
t.Fatal(diff)
}
srcSDB := src.stateAt(t, srcLast.PostExecutionStateRoot())
sutSDB := sut.stateAt(t, sutLast.PostExecutionStateRoot())
if diff := cmp.Diff(srcSDB, sutSDB, cmputils.StateDBs()); diff != "" {
t.Fatal(diff)
}
tx := src.wallet.SetNonceAndSign(t, 0, &types.LegacyTx{
To: &common.Address{},
Gas: params.TxGas,
GasPrice: big.NewInt(100),
})
for _, sys := range []struct {
name string
ctx context.Context //nolint:containedctx // Ephemeral so not in contravention of https://go.dev/blog/context-and-structs
*SUT
}{
{"source", srcCtx, src},
{"recovered", sutCtx, sut},
} {
t.Run(sys.name, func(t *testing.T) {
sys.mustSendTx(t, tx)
b := sys.runConsensusLoop(t)
require.Len(t, b.Transactions(), 1)
require.NoError(t, b.WaitUntilExecuted(sys.ctx))
})
}
})
if t.Failed() {
t.FailNow()
}
}
t.Run("last", func(t *testing.T) {
for name, fn := range map[string](func(vm *VM) *blocks.Block){
"accepted": func(vm *VM) *blocks.Block { return vm.last.accepted.Load() },
"executed": func(vm *VM) *blocks.Block { return vm.exec.LastExecuted() },
"settled": func(vm *VM) *blocks.Block { return vm.last.settled.Load() },
} {
t.Run(name, func(t *testing.T) {
got := fn(sut.rawVM)
want := fn(src.rawVM)
if diff := cmp.Diff(want, got, blocks.CmpOpt()); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
})
if diff := cmp.Diff(src.rawVM.blocks.m, sut.rawVM.blocks.m, blocks.CmpOpt()); diff != "" {
t.Errorf("%T.blocks diff (-source +recovered):\n%s", src.rawVM, diff)
}
})
}
}