Skip to content

Commit a57eee3

Browse files
authored
[blockchain] add ContextAtHeight() (#4524)
1 parent caa1c8c commit a57eee3

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

blockchain/blockchain.go

+38-23
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ type (
8787
Genesis() genesis.Genesis
8888
// Context returns current context
8989
Context(context.Context) (context.Context, error)
90+
// ContextAtHeight returns context at given height
91+
ContextAtHeight(context.Context, uint64) (context.Context, error)
9092

9193
// For block operations
9294
// MintNewBlock creates a new block with given actions
@@ -228,10 +230,14 @@ func (bc *blockchain) Start(ctx context.Context) error {
228230
defer bc.mu.Unlock()
229231

230232
// 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))
235241
return bc.lifecycle.OnStart(ctx)
236242
}
237243

@@ -281,7 +287,11 @@ func (bc *blockchain) ValidateBlock(blk *block.Block, opts ...BlockValidationOpt
281287
if blk == nil {
282288
return ErrInvalidBlock
283289
}
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)
285295
if err != nil {
286296
return err
287297
}
@@ -322,7 +332,7 @@ func (bc *blockchain) ValidateBlock(blk *block.Block, opts ...BlockValidationOpt
322332
if producerAddr == nil {
323333
return errors.New("failed to get address")
324334
}
325-
ctx, err := bc.context(context.Background(), true)
335+
ctx, err := bc.context(context.Background(), tipHeight)
326336
if err != nil {
327337
return err
328338
}
@@ -351,8 +361,17 @@ func (bc *blockchain) ValidateBlock(blk *block.Block, opts ...BlockValidationOpt
351361
func (bc *blockchain) Context(ctx context.Context) (context.Context, error) {
352362
bc.mu.RLock()
353363
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+
}
354370

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)
356375
}
357376

358377
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
368387
})
369388
}
370389

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
379394
}
380395

381396
ctx = genesis.WithGenesisContext(
382397
protocol.WithBlockchainCtx(
383398
ctx,
384399
protocol.BlockchainCtx{
385-
Tip: tip,
400+
Tip: *tip,
386401
ChainID: bc.ChainID(),
387402
EvmNetworkID: bc.EvmNetworkID(),
388403
},
@@ -402,7 +417,7 @@ func (bc *blockchain) MintNewBlock(timestamp time.Time) (*block.Block, error) {
402417
return nil, err
403418
}
404419
newblockHeight := tipHeight + 1
405-
ctx, err := bc.context(context.Background(), true)
420+
ctx, err := bc.context(context.Background(), tipHeight)
406421
if err != nil {
407422
return nil, err
408423
}
@@ -463,11 +478,7 @@ func (bc *blockchain) Genesis() genesis.Genesis {
463478
// private functions
464479
//=====================================
465480

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) {
471482
if tipHeight == 0 {
472483
return &protocol.TipInfo{
473484
Height: 0,
@@ -493,7 +504,11 @@ func (bc *blockchain) tipInfo() (*protocol.TipInfo, error) {
493504

494505
// commitBlock commits a block to the chain
495506
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)
497512
if err != nil {
498513
return err
499514
}

test/mock/mock_blockchain/mock_blockchain.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)