From 4945ae1427399d9a0f6b3561306f50f360011613 Mon Sep 17 00:00:00 2001 From: StephenButtolph Date: Wed, 12 Aug 2020 22:50:17 -0400 Subject: read blocks with caching --- plugin/evm/block.go | 6 +----- plugin/evm/vm.go | 13 +++++-------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/plugin/evm/block.go b/plugin/evm/block.go index eaa78a5..2912d2c 100644 --- a/plugin/evm/block.go +++ b/plugin/evm/block.go @@ -50,11 +50,7 @@ func (b *Block) Status() choices.Status { // Parent implements the snowman.Block interface func (b *Block) Parent() snowman.Block { parentID := ids.NewID(b.ethBlock.ParentHash()) - block := &Block{ - id: parentID, - ethBlock: b.vm.getCachedBlock(parentID), - vm: b.vm, - } + block := b.vm.getBlock(parentID) b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", block.ID(), block.Status()) return block } diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index c22d9d1..6c96870 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -422,10 +422,6 @@ func (vm *VM) updateStatus(blockID ids.ID, status choices.Status) { vm.blockStatusCache.Put(blockID, status) } -func (vm *VM) getCachedBlock(blockID ids.ID) *types.Block { - return vm.chain.GetBlockByHash(blockID.Key()) -} - func (vm *VM) tryBlockGen() error { vm.bdlock.Lock() defer vm.bdlock.Unlock() @@ -474,10 +470,11 @@ func (vm *VM) getCachedStatus(blockID ids.ID) choices.Status { if statusIntf, ok := vm.blockStatusCache.Get(blockID); ok { status = statusIntf.(choices.Status) } else { - blk := vm.chain.GetBlockByHash(blockID.Key()) - if blk == nil { + wrappedBlk := vm.getBlock(blockID) + if wrappedBlk == nil { return choices.Unknown } + blk := wrappedBlk.ethBlock acceptedBlk := vm.lastAccepted.ethBlock // TODO: There must be a better way of doing this. @@ -488,7 +485,7 @@ func (vm *VM) getCachedStatus(blockID ids.ID) choices.Status { highBlock, lowBlock = lowBlock, highBlock } for highBlock.Number().Cmp(lowBlock.Number()) > 0 { - highBlock = vm.chain.GetBlockByHash(highBlock.ParentHash()) + highBlock = vm.getBlock(ids.NewID(highBlock.ParentHash())).ethBlock } if highBlock.Hash() == lowBlock.Hash() { // on the same branch @@ -508,7 +505,7 @@ func (vm *VM) getBlock(id ids.ID) *Block { if blockIntf, ok := vm.blockCache.Get(id); ok { return blockIntf.(*Block) } - ethBlock := vm.getCachedBlock(id) + ethBlock := vm.chain.GetBlockByHash(id.Key()) if ethBlock == nil { return nil } -- cgit v1.2.3