54
54
protocolView * protocol.Views
55
55
skipBlockValidationOnPut bool
56
56
ps * patchStore
57
+ erigonDB * erigonDB
57
58
}
58
59
)
59
60
@@ -118,6 +119,10 @@ func NewStateDB(cfg Config, dao db.KVStore, opts ...StateDBOption) (Factory, err
118
119
log .L ().Error ("Failed to generate prometheus timer factory." , zap .Error (err ))
119
120
}
120
121
sdb .timerFactory = timerFactory
122
+ if len (cfg .Chain .HistoryIndexPath ) > 0 {
123
+ sdb .erigonDB = newErigonDB (cfg .Chain .HistoryIndexPath )
124
+ }
125
+
121
126
return & sdb , nil
122
127
}
123
128
@@ -126,6 +131,11 @@ func (sdb *stateDB) Start(ctx context.Context) error {
126
131
if err := sdb .dao .Start (ctx ); err != nil {
127
132
return err
128
133
}
134
+ if sdb .erigonDB != nil {
135
+ if err := sdb .erigonDB .Start (ctx ); err != nil {
136
+ return err
137
+ }
138
+ }
129
139
// check factory height
130
140
h , err := sdb .dao .getHeight ()
131
141
switch errors .Cause (err ) {
@@ -166,6 +176,9 @@ func (sdb *stateDB) Stop(ctx context.Context) error {
166
176
sdb .mutex .Lock ()
167
177
defer sdb .mutex .Unlock ()
168
178
sdb .workingsets .Clear ()
179
+ if sdb .erigonDB != nil {
180
+ sdb .erigonDB .Stop (ctx )
181
+ }
169
182
return sdb .dao .Stop (ctx )
170
183
}
171
184
@@ -223,6 +236,35 @@ func (sdb *stateDB) createWorkingSetStore(ctx context.Context, height uint64, kv
223
236
return newStateDBWorkingSetStore (flusher , g .IsNewfoundland (height )), nil
224
237
}
225
238
239
+ func (sdb * stateDB ) newWorkingSetWithErigonOutput (ctx context.Context , height uint64 ) (* workingSet , error ) {
240
+ ws , err := sdb .newWorkingSet (ctx , height )
241
+ if err != nil {
242
+ return nil , err
243
+ }
244
+ e , err := sdb .erigonDB .newErigonStore (ctx , height )
245
+ if err != nil {
246
+ return nil , err
247
+ }
248
+ ws .store = newWorkingSetStoreWithSecondary (
249
+ ws .store .(* stateDBWorkingSetStore ),
250
+ e ,
251
+ )
252
+ return ws , nil
253
+ }
254
+
255
+ func (sdb * stateDB ) newWorkingSetWithErigonDryrun (ctx context.Context , height uint64 ) (* workingSet , error ) {
256
+ ws , err := sdb .newWorkingSet (ctx , height )
257
+ if err != nil {
258
+ return nil , err
259
+ }
260
+ e , err := sdb .erigonDB .newErigonStoreDryrun (ctx , height + 1 )
261
+ if err != nil {
262
+ return nil , err
263
+ }
264
+ ws .store = newErigonWorkingSetStoreForSimulate (ws .store .(* stateDBWorkingSetStore ), e )
265
+ return ws , nil
266
+ }
267
+
226
268
func (sdb * stateDB ) Register (p protocol.Protocol ) error {
227
269
return p .Register (sdb .registry )
228
270
}
@@ -274,7 +316,11 @@ func (sdb *stateDB) Mint(
274
316
case currHeight + 1 > expectedBlockHeight :
275
317
return nil , errors .Wrapf (ErrNotSupported , "cannot create block at height %d, current height is %d" , expectedBlockHeight , sdb .currentChainHeight )
276
318
default :
277
- ws , err = sdb .newWorkingSet (ctx , currHeight + 1 )
319
+ if sdb .erigonDB != nil {
320
+ ws , err = sdb .newWorkingSetWithErigonOutput (ctx , currHeight + 1 )
321
+ } else {
322
+ ws , err = sdb .newWorkingSet (ctx , currHeight + 1 )
323
+ }
278
324
}
279
325
if err != nil {
280
326
return nil , err
@@ -315,7 +361,15 @@ func (sdb *stateDB) WorkingSet(ctx context.Context) (protocol.StateManager, erro
315
361
}
316
362
317
363
func (sdb * stateDB ) WorkingSetAtHeight (ctx context.Context , height uint64 , preacts ... * action.SealedEnvelope ) (protocol.StateManager , error ) {
318
- ws , err := sdb .newReadOnlyWorkingSet (ctx , height )
364
+ var (
365
+ ws * workingSet
366
+ err error
367
+ )
368
+ if sdb .erigonDB != nil {
369
+ ws , err = sdb .newWorkingSetWithErigonDryrun (ctx , height )
370
+ } else {
371
+ ws , err = sdb .newReadOnlyWorkingSet (ctx , height )
372
+ }
319
373
if err != nil {
320
374
return nil , errors .Wrap (err , "failed to obtain working set from state db" )
321
375
}
@@ -353,6 +407,7 @@ func (sdb *stateDB) PutBlock(ctx context.Context, blk *block.Block) error {
353
407
}
354
408
if err != nil {
355
409
log .L ().Error ("Failed to update state." , zap .Error (err ))
410
+ ws .Close ()
356
411
return err
357
412
}
358
413
}
@@ -461,7 +516,15 @@ func (sdb *stateDB) state(h uint64, ns string, addr []byte, s interface{}) error
461
516
}
462
517
463
518
func (sdb * stateDB ) createGenesisStates (ctx context.Context ) error {
464
- ws , err := sdb .newWorkingSet (ctx , 0 )
519
+ var (
520
+ ws * workingSet
521
+ err error
522
+ )
523
+ if sdb .erigonDB != nil {
524
+ ws , err = sdb .newWorkingSetWithErigonOutput (ctx , 0 )
525
+ } else {
526
+ ws , err = sdb .newWorkingSet (ctx , 0 )
527
+ }
465
528
if err != nil {
466
529
return err
467
530
}
@@ -488,6 +551,14 @@ func (sdb *stateDB) getFromWorkingSets(ctx context.Context, key hash.Hash256) (*
488
551
sdb .mutex .RLock ()
489
552
currHeight := sdb .currentChainHeight
490
553
sdb .mutex .RUnlock ()
491
- tx , err := sdb .newWorkingSet (ctx , currHeight + 1 )
554
+ var (
555
+ tx * workingSet
556
+ err error
557
+ )
558
+ if sdb .erigonDB != nil {
559
+ tx , err = sdb .newWorkingSetWithErigonOutput (ctx , currHeight + 1 )
560
+ } else {
561
+ tx , err = sdb .newWorkingSet (ctx , currHeight + 1 )
562
+ }
492
563
return tx , false , err
493
564
}
0 commit comments