diff options
-rw-r--r-- | core/blockchain.go | 4 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | plugin/evm/block.go | 12 | ||||
-rw-r--r-- | plugin/evm/vm.go | 32 |
4 files changed, 28 insertions, 22 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 64c2451..6e316c7 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -732,6 +732,8 @@ func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { // GetBlockByHash retrieves a block from the database by hash, caching it if found. func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { + bc.chainmu.Lock() + defer bc.chainmu.Unlock() number := bc.hc.GetBlockNumber(hash) if number == nil { return nil @@ -742,6 +744,8 @@ func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { // GetBlockByNumber retrieves a block from the database by number, caching it // (associated with its hash) if found. func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { + bc.chainmu.Lock() + defer bc.chainmu.Unlock() hash := rawdb.ReadCanonicalHash(bc.db, number) if hash == (common.Hash{}) { return nil @@ -3,7 +3,7 @@ module github.com/ava-labs/coreth go 1.14 require ( - github.com/ava-labs/gecko v0.5.7 + github.com/ava-labs/gecko v0.6.1 github.com/ava-labs/go-ethereum v1.9.3 github.com/cespare/cp v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 diff --git a/plugin/evm/block.go b/plugin/evm/block.go index 0779e17..375dc6d 100644 --- a/plugin/evm/block.go +++ b/plugin/evm/block.go @@ -13,6 +13,7 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow/choices" "github.com/ava-labs/gecko/snow/consensus/snowman" + "github.com/ava-labs/gecko/vms/components/missing" ) // Block implements the snowman.Block interface @@ -63,13 +64,12 @@ 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, + if block := b.vm.getBlock(parentID); block != nil { + b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", parentID, block.Status()) + return block } - b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", block.ID(), block.Status()) - return block + b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", parentID, choices.Unknown) + return &missing.Block{BlkID: parentID} } // Verify implements the snowman.Block interface diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index f97de09..08d7bcb 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -62,11 +62,11 @@ const ( ) const ( - minBlockTime = 250 * time.Millisecond - maxBlockTime = 1000 * time.Millisecond - batchSize = 250 - + minBlockTime = 250 * time.Millisecond + maxBlockTime = 1000 * time.Millisecond + batchSize = 250 maxUTXOsToFetch = 1024 + blockCacheSize = 1 << 17 // 131072 ) const ( @@ -303,9 +303,9 @@ func (vm *VM) Initialize( } return tx.UnsignedTx.(UnsignedAtomicTx).EVMStateTransfer(state) }) - vm.blockCache = cache.LRU{Size: 2048} - vm.blockStatusCache = cache.LRU{Size: 1024} - vm.blockAtomicInputCache = cache.LRU{Size: 4096} + vm.blockCache = cache.LRU{Size: blockCacheSize} + vm.blockStatusCache = cache.LRU{Size: blockCacheSize} + vm.blockAtomicInputCache = cache.LRU{Size: blockCacheSize} vm.newBlockChan = make(chan *Block) vm.networkChan = toEngine vm.blockDelayTimer = timer.NewTimer(func() { @@ -553,10 +553,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() @@ -605,10 +601,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. @@ -619,7 +616,12 @@ 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()) + parentBlock := vm.getBlock(ids.NewID(highBlock.ParentHash())) + if parentBlock == nil { + vm.blockStatusCache.Put(blockID, choices.Processing) + return choices.Processing + } + highBlock = parentBlock.ethBlock } if highBlock.Hash() == lowBlock.Hash() { // on the same branch @@ -639,7 +641,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 } |