@@ -8,40 +8,15 @@ import (
88 "testing"
99)
1010
11- func TestChainstateManagerCreation (t * testing.T ) {
12- // Create a temporary directory for testing
13- tempDir , err := os .MkdirTemp ("" , "bitcoin_kernel_test" )
14- if err != nil {
15- t .Fatalf ("Failed to create temp dir: %v" , err )
16- }
17- defer os .RemoveAll (tempDir )
18-
19- dataDir := filepath .Join (tempDir , "data" )
20- blocksDir := filepath .Join (tempDir , "blocks" )
21-
22- ctx , err := NewDefaultContext ()
23- if err != nil {
24- t .Fatalf ("NewDefaultContext() error = %v" , err )
25- }
26- defer ctx .Destroy ()
27-
28- opts , err := NewChainstateManagerOptions (ctx , dataDir , blocksDir )
29- if err != nil {
30- t .Fatalf ("NewChainstateManagerOptions() error = %v" , err )
31- }
32- defer opts .Destroy ()
11+ func TestChainstateManager (t * testing.T ) {
12+ suite := SetupChainstateManagerTestSuite (t )
3313
34- opts .SetWorkerThreads (1 )
35- opts .SetBlockTreeDBInMemory (true )
36- opts .SetChainstateDBInMemory (true )
37-
38- manager , err := NewChainstateManager (ctx , opts )
39- if err != nil {
40- t .Fatalf ("NewChainstateManager() error = %v" , err )
41- }
42- defer manager .Destroy ()
14+ t .Run ("genesis validation" , suite .TestGenesis )
15+ t .Run ("tip validation" , suite .TestTip )
16+ }
4317
44- genesisIndex , err := manager .GetBlockIndexFromGenesis ()
18+ func (s * ChainstateManagerTestSuite ) TestGenesis (t * testing.T ) {
19+ genesisIndex , err := s .Manager .GetBlockIndexFromGenesis ()
4520 if err != nil {
4621 t .Fatalf ("GetBlockIndexFromGenesis() error = %v" , err )
4722 }
@@ -64,51 +39,75 @@ func TestChainstateManagerCreation(t *testing.T) {
6439 }
6540}
6641
67- // TestLoadAndValidateBlock demonstrates the following workflow:
68- // 1. Setting up a chainstate manager with regtest parameters
69- // 2. Loading blocks from data/regtest/blocks.txt
70- // 3. Validating and processing multiple blocks in sequence
71- func TestLoadAndValidateBlock (t * testing.T ) {
72- // Create a temporary directory for testing
42+ func (s * ChainstateManagerTestSuite ) TestTip (t * testing.T ) {
43+ tipIndex , err := s .Manager .GetBlockIndexFromTip ()
44+ if err != nil {
45+ t .Fatalf ("GetBlockIndexFromTip() error = %v" , err )
46+ }
47+ defer tipIndex .Destroy ()
48+
49+ height := tipIndex .Height ()
50+ if height <= 0 {
51+ t .Errorf ("Expected tip height > 0, got %d" , height )
52+ }
53+
54+ tipHash , err := tipIndex .Hash ()
55+ if err != nil {
56+ t .Fatalf ("Failed to get tip hash: %v" , err )
57+ }
58+ defer tipHash .Destroy ()
59+
60+ hashBytes := tipHash .Bytes ()
61+ if len (hashBytes ) != 32 {
62+ t .Errorf ("Expected hash length 32, got %d" , len (hashBytes ))
63+ }
64+
65+ if tipIndex .Height () != s .ImportedBlocksCount {
66+ t .Errorf ("Expected tip height %d, got %d" , s .ImportedBlocksCount , tipIndex .Height ())
67+ }
68+ }
69+
70+ type ChainstateManagerTestSuite struct {
71+ Manager * ChainstateManager
72+ ImportedBlocksCount int32
73+ }
74+
75+ func SetupChainstateManagerTestSuite (t * testing.T ) * ChainstateManagerTestSuite {
7376 tempDir , err := os .MkdirTemp ("" , "bitcoin_kernel_test" )
7477 if err != nil {
7578 t .Fatalf ("Failed to create temp dir: %v" , err )
7679 }
77- defer os .RemoveAll (tempDir )
80+ t . Cleanup ( func () { os .RemoveAll (tempDir ) } )
7881
7982 dataDir := filepath .Join (tempDir , "data" )
8083 blocksDir := filepath .Join (tempDir , "blocks" )
8184
82- // Create context with regtest chain parameters
8385 contextOpts , err := NewContextOptions ()
8486 if err != nil {
8587 t .Fatalf ("NewContextOptions() error = %v" , err )
8688 }
87- defer contextOpts .Destroy ()
89+ t . Cleanup ( func () { contextOpts .Destroy () } )
8890
89- // Set regtest chain parameters
9091 chainParams , err := NewChainParameters (ChainTypeRegtest )
9192 if err != nil {
9293 t .Fatalf ("NewChainParameters() error = %v" , err )
9394 }
94- defer chainParams .Destroy ()
95+ t . Cleanup ( func () { chainParams .Destroy () } )
9596
9697 contextOpts .SetChainParams (chainParams )
9798
9899 ctx , err := NewContext (contextOpts )
99100 if err != nil {
100101 t .Fatalf ("NewContext() error = %v" , err )
101102 }
102- defer ctx .Destroy ()
103+ t . Cleanup ( func () { ctx .Destroy () } )
103104
104- // Create chainstate manager options
105105 opts , err := NewChainstateManagerOptions (ctx , dataDir , blocksDir )
106106 if err != nil {
107107 t .Fatalf ("NewChainstateManagerOptions() error = %v" , err )
108108 }
109- defer opts .Destroy ()
109+ t . Cleanup ( func () { opts .Destroy () } )
110110
111- // Configure for in-memory operation
112111 opts .SetWorkerThreads (1 )
113112 opts .SetBlockTreeDBInMemory (true )
114113 opts .SetChainstateDBInMemory (true )
@@ -118,7 +117,7 @@ func TestLoadAndValidateBlock(t *testing.T) {
118117 if err != nil {
119118 t .Fatalf ("NewChainstateManager() error = %v" , err )
120119 }
121- defer manager .Destroy ()
120+ t . Cleanup ( func () { manager .Destroy () } )
122121
123122 // Initialize empty databases
124123 err = manager .ImportBlocks (nil )
@@ -151,63 +150,31 @@ func TestLoadAndValidateBlock(t *testing.T) {
151150 }
152151 t .Logf ("Found %d blocks in regtest data" , len (blockLines ))
153152
154- // Process all blocks from the data file
155153 for i := 0 ; i < len (blockLines ); i ++ {
156154 blockHex := blockLines [i ]
157155
158- // Decode hex data
159156 blockBytes , err := hex .DecodeString (blockHex )
160157 if err != nil {
161158 t .Fatalf ("Failed to decode block %d hex: %v" , i + 1 , err )
162159 }
163160
164- // Create block from raw data
165161 block , err := NewBlockFromRaw (blockBytes )
166162 if err != nil {
167163 t .Fatalf ("NewBlockFromRaw() failed for block %d: %v" , i + 1 , err )
168164 }
169165 defer block .Destroy ()
170166
171- // Process the block (validate it)
172167 success , isNewBlock , err := manager .ProcessBlock (block )
173168 if err != nil {
174169 t .Fatalf ("ProcessBlock() failed for block %d: %v" , i + 1 , err )
175170 }
176171 if ! success || ! isNewBlock {
177172 t .Fatalf ("ProcessBlock() failed for block %d" , i + 1 )
178173 }
174+ }
179175
180- // Assert tip height
181- tipIndex , err := manager .GetBlockIndexFromTip ()
182- if err != nil {
183- t .Fatalf ("GetBlockIndexFromTip() error = %v" , err )
184- }
185- defer tipIndex .Destroy ()
186-
187- expectedTipHeight := int32 (i ) + 1
188- if tipIndex .Height () != expectedTipHeight {
189- t .Fatalf ("Expected tip height %d; got %d" , expectedTipHeight , tipIndex .Height ())
190- }
191-
192- // Assert tip hash
193- hash , err := block .Hash ()
194- if err != nil {
195- t .Fatalf ("Failed to get last block hash: %v" , err )
196- }
197- defer hash .Destroy ()
198-
199- hashHex := hex .EncodeToString (hash .Bytes ())
200-
201- tipHash , err := tipIndex .Hash ()
202- if err != nil {
203- t .Fatalf ("Failed to get tip hash: %v" , err )
204- }
205- defer tipHash .Destroy ()
206-
207- tipHashHex := hex .EncodeToString (tipHash .Bytes ())
208-
209- if hashHex != tipHashHex {
210- t .Fatalf ("Expected tip hash %s; got %s" , hashHex , tipHashHex )
211- }
176+ return & ChainstateManagerTestSuite {
177+ Manager : manager ,
178+ ImportedBlocksCount : int32 (len (blockLines )),
212179 }
213180}
0 commit comments