Skip to content

Commit 68a4918

Browse files
committed
Add unit test for trie clean re-journaling
1 parent 15b98f1 commit 68a4918

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

core/blockchain_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package core
66
import (
77
"fmt"
88
"math/big"
9+
"os"
910
"testing"
11+
"time"
1012

1113
"github.com/ava-labs/subnet-evm/consensus/dummy"
1214
"github.com/ava-labs/subnet-evm/core/rawdb"
@@ -70,6 +72,77 @@ func TestArchiveBlockChain(t *testing.T) {
7072
}
7173
}
7274

75+
func TestTrieCleanJournal(t *testing.T) {
76+
trieCleanJournal := t.TempDir()
77+
78+
create := func(db ethdb.Database, chainConfig *params.ChainConfig, lastAcceptedHash common.Hash) (*BlockChain, error) {
79+
config := *archiveConfig
80+
config.TrieCleanJournal = trieCleanJournal
81+
config.TrieCleanRejournal = 100 * time.Millisecond
82+
return createBlockChain(db, &config, chainConfig, lastAcceptedHash)
83+
}
84+
85+
var (
86+
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
87+
key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
88+
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
89+
addr2 = crypto.PubkeyToAddress(key2.PublicKey)
90+
// We use two separate databases since GenerateChain commits the state roots to its underlying
91+
// database.
92+
genDB = rawdb.NewMemoryDatabase()
93+
chainDB = rawdb.NewMemoryDatabase()
94+
)
95+
96+
// Ensure that key1 has some funds in the genesis block.
97+
genesisBalance := big.NewInt(1000000)
98+
gspec := &Genesis{
99+
Config: &params.ChainConfig{HomesteadBlock: new(big.Int)},
100+
Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}},
101+
}
102+
genesis := gspec.MustCommit(genDB)
103+
_ = gspec.MustCommit(chainDB)
104+
105+
blockchain, err := create(chainDB, gspec.Config, common.Hash{})
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
defer blockchain.Stop()
110+
111+
// This call generates a chain of 3 blocks.
112+
signer := types.HomesteadSigner{}
113+
// Generate chain of blocks using [genDB] instead of [chainDB] to avoid writing
114+
// to the BlockChain's database while generating blocks.
115+
chain, _, err := GenerateChain(gspec.Config, genesis, blockchain.engine, genDB, 3, 10, func(i int, gen *BlockGen) {
116+
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1)
117+
gen.AddTx(tx)
118+
})
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
123+
// Insert and accept the generated chain
124+
if _, err := blockchain.InsertChain(chain); err != nil {
125+
t.Fatal(err)
126+
}
127+
for _, block := range chain {
128+
if err := blockchain.Accept(block); err != nil {
129+
t.Fatal(err)
130+
}
131+
}
132+
blockchain.DrainAcceptorQueue()
133+
134+
time.Sleep(2 * time.Second) // Sleep for 2 seconds to ensure that there is time for a clean trie rejournal
135+
136+
dirEntries, err := os.ReadDir(trieCleanJournal)
137+
if err != nil {
138+
t.Fatal(err)
139+
}
140+
141+
if len(dirEntries) == 0 {
142+
t.Fatalf("Expected trie clean journal to generate non-zero number of entries in the trie clean journal directory")
143+
}
144+
}
145+
73146
func TestArchiveBlockChainSnapsDisabled(t *testing.T) {
74147
create := func(db ethdb.Database, chainConfig *params.ChainConfig, lastAcceptedHash common.Hash) (*BlockChain, error) {
75148
return createBlockChain(

0 commit comments

Comments
 (0)