87
87
Genesis () genesis.Genesis
88
88
// Context returns current context
89
89
Context (context.Context ) (context.Context , error )
90
+ // ContextAtHeight returns context at given height
91
+ ContextAtHeight (context.Context , uint64 ) (context.Context , error )
90
92
91
93
// For block operations
92
94
// MintNewBlock creates a new block with given actions
@@ -228,10 +230,14 @@ func (bc *blockchain) Start(ctx context.Context) error {
228
230
defer bc .mu .Unlock ()
229
231
230
232
// pass registry to be used by state factory's initialization
231
- ctx , err := bc .context (ctx , false )
232
- if err != nil {
233
- return err
234
- }
233
+ ctx = protocol .WithFeatureWithHeightCtx (genesis .WithGenesisContext (
234
+ protocol .WithBlockchainCtx (
235
+ ctx ,
236
+ protocol.BlockchainCtx {
237
+ ChainID : bc .ChainID (),
238
+ EvmNetworkID : bc .EvmNetworkID (),
239
+ },
240
+ ), bc .genesis ))
235
241
return bc .lifecycle .OnStart (ctx )
236
242
}
237
243
@@ -281,7 +287,11 @@ func (bc *blockchain) ValidateBlock(blk *block.Block, opts ...BlockValidationOpt
281
287
if blk == nil {
282
288
return ErrInvalidBlock
283
289
}
284
- tip , err := bc .tipInfo ()
290
+ tipHeight , err := bc .dao .Height ()
291
+ if err != nil {
292
+ return err
293
+ }
294
+ tip , err := bc .tipInfo (tipHeight )
285
295
if err != nil {
286
296
return err
287
297
}
@@ -322,7 +332,7 @@ func (bc *blockchain) ValidateBlock(blk *block.Block, opts ...BlockValidationOpt
322
332
if producerAddr == nil {
323
333
return errors .New ("failed to get address" )
324
334
}
325
- ctx , err := bc .context (context .Background (), true )
335
+ ctx , err := bc .context (context .Background (), tipHeight )
326
336
if err != nil {
327
337
return err
328
338
}
@@ -351,8 +361,17 @@ func (bc *blockchain) ValidateBlock(blk *block.Block, opts ...BlockValidationOpt
351
361
func (bc * blockchain ) Context (ctx context.Context ) (context.Context , error ) {
352
362
bc .mu .RLock ()
353
363
defer bc .mu .RUnlock ()
364
+ tipHeight , err := bc .dao .Height ()
365
+ if err != nil {
366
+ return nil , err
367
+ }
368
+ return bc .context (ctx , tipHeight )
369
+ }
354
370
355
- return bc .context (ctx , true )
371
+ func (bc * blockchain ) ContextAtHeight (ctx context.Context , height uint64 ) (context.Context , error ) {
372
+ bc .mu .RLock ()
373
+ defer bc .mu .RUnlock ()
374
+ return bc .context (ctx , height )
356
375
}
357
376
358
377
func (bc * blockchain ) contextWithBlock (ctx context.Context , producer address.Address , height uint64 , timestamp time.Time , baseFee * big.Int , blobgas uint64 ) context.Context {
@@ -368,21 +387,17 @@ func (bc *blockchain) contextWithBlock(ctx context.Context, producer address.Add
368
387
})
369
388
}
370
389
371
- func (bc * blockchain ) context (ctx context.Context , tipInfoFlag bool ) (context.Context , error ) {
372
- var tip protocol.TipInfo
373
- if tipInfoFlag {
374
- if tipInfoValue , err := bc .tipInfo (); err == nil {
375
- tip = * tipInfoValue
376
- } else {
377
- return nil , err
378
- }
390
+ func (bc * blockchain ) context (ctx context.Context , height uint64 ) (context.Context , error ) {
391
+ tip , err := bc .tipInfo (height )
392
+ if err != nil {
393
+ return nil , err
379
394
}
380
395
381
396
ctx = genesis .WithGenesisContext (
382
397
protocol .WithBlockchainCtx (
383
398
ctx ,
384
399
protocol.BlockchainCtx {
385
- Tip : tip ,
400
+ Tip : * tip ,
386
401
ChainID : bc .ChainID (),
387
402
EvmNetworkID : bc .EvmNetworkID (),
388
403
},
@@ -402,7 +417,7 @@ func (bc *blockchain) MintNewBlock(timestamp time.Time) (*block.Block, error) {
402
417
return nil , err
403
418
}
404
419
newblockHeight := tipHeight + 1
405
- ctx , err := bc .context (context .Background (), true )
420
+ ctx , err := bc .context (context .Background (), tipHeight )
406
421
if err != nil {
407
422
return nil , err
408
423
}
@@ -463,11 +478,7 @@ func (bc *blockchain) Genesis() genesis.Genesis {
463
478
// private functions
464
479
//=====================================
465
480
466
- func (bc * blockchain ) tipInfo () (* protocol.TipInfo , error ) {
467
- tipHeight , err := bc .dao .Height ()
468
- if err != nil {
469
- return nil , err
470
- }
481
+ func (bc * blockchain ) tipInfo (tipHeight uint64 ) (* protocol.TipInfo , error ) {
471
482
if tipHeight == 0 {
472
483
return & protocol.TipInfo {
473
484
Height : 0 ,
@@ -493,7 +504,11 @@ func (bc *blockchain) tipInfo() (*protocol.TipInfo, error) {
493
504
494
505
// commitBlock commits a block to the chain
495
506
func (bc * blockchain ) commitBlock (blk * block.Block ) error {
496
- ctx , err := bc .context (context .Background (), true )
507
+ tipHeight , err := bc .dao .Height ()
508
+ if err != nil {
509
+ return err
510
+ }
511
+ ctx , err := bc .context (context .Background (), tipHeight )
497
512
if err != nil {
498
513
return err
499
514
}
0 commit comments