From 5df05575569b3901205cd04c03cb415c0faa3c32 Mon Sep 17 00:00:00 2001 From: laizy Date: Thu, 13 Jun 2019 16:44:20 +0800 Subject: [PATCH] handle ledger recovery --- core/ledger/ledger.go | 75 +++++++++++---------- core/store/ledgerstore/block_cache_store.go | 8 ++- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/core/ledger/ledger.go b/core/ledger/ledger.go index 169cf58afb..d305b14941 100644 --- a/core/ledger/ledger.go +++ b/core/ledger/ledger.go @@ -190,53 +190,60 @@ func (self *Ledger) AddBlock(block *types.Block, stateMerkleRoot common.Uint256) func (self *Ledger) ExecuteBlock(b *types.Block) (store.ExecuteResult, error) { if !self.ShardID.IsRootShard() { - parentBlock, merkleRoot, err := self.ParentBlockCache.GetBlock(b.Header.ParentHeight) - if err != nil { - if err == scommon.ErrNotFound { - return self.ldgStore.ExecuteBlock(b) - } else { - log.Errorf("Ledger ExecuteBlock GetBlock sharad height:%d,ParentHeight:%d error:%s", b.Header.Height, b.Header.ParentHeight, err) - return store.ExecuteResult{}, err + parentHeight := self.ParentLedger.GetCurrentBlockHeight() + if parentHeight != b.Header.ParentHeight { + parentBlock, merkleRoot, err := self.ParentBlockCache.GetBlock(b.Header.ParentHeight) + if err != nil { + if err == scommon.ErrNotFound { + return self.ldgStore.ExecuteBlock(b) + } else { + log.Errorf("Ledger ExecuteBlock GetBlock sharad height:%d,ParentHeight:%d error:%s", b.Header.Height, b.Header.ParentHeight, err) + return store.ExecuteResult{}, err + } } + result, err := self.ParentLedger.ldgStore.ExecuteBlock(parentBlock) + if err != nil { + return result, err + } + if merkleRoot != result.MerkleRoot { + log.Errorf("ExecuteBlock check parentblock cache MerkleRoot blocknum:%d,MerkleRoot:%s,execute MerkleRoot:%s", b.Header.ParentHeight, merkleRoot.ToHexString(), result.MerkleRoot.ToHexString()) + return store.ExecuteResult{}, fmt.Errorf("merkleroot not match") + } + self.ParentBlockCache.SaveBlockExecuteResult(b.Header.ParentHeight, result) + } - result, err := self.ParentLedger.ldgStore.ExecuteBlock(parentBlock) - if err != nil { - return result, err - } - if merkleRoot != result.MerkleRoot { - log.Errorf("ExecuteBlock check parentblock cache MerkleRoot blocknum:%d,MerkleRoot:%s,execute MerkleRoot:%s", b.Header.ParentHeight, merkleRoot.ToHexString(), result.MerkleRoot.ToHexString()) - return store.ExecuteResult{}, fmt.Errorf("merkleroot not match") - } - self.ParentBlockCache.SaveBlockExecuteResult(b.Header.ParentHeight, result) } return self.ldgStore.ExecuteBlock(b) } func (self *Ledger) SubmitBlock(b *types.Block, exec store.ExecuteResult) error { if !self.ShardID.IsRootShard() { - parentBlock, _, err := self.ParentBlockCache.GetBlock(b.Header.ParentHeight) - if err != nil { - if err == scommon.ErrNotFound { - err := self.ldgStore.SubmitBlock(b, exec) - if err != nil { - log.Errorf("Ledger SubmitBlock BlockHeight:%d BlockHash:%x error:%s", b.Header.Height, b.Hash(), err) + parentHeight := self.ParentLedger.GetCurrentBlockHeight() + if parentHeight != b.Header.ParentHeight { + parentBlock, _, err := self.ParentBlockCache.GetBlock(b.Header.ParentHeight) + if err != nil { + if err == scommon.ErrNotFound { + err := self.ldgStore.SubmitBlock(b, exec) + if err != nil { + log.Errorf("Ledger SubmitBlock BlockHeight:%d BlockHash:%x error:%s", b.Header.Height, b.Hash(), err) + return err + } + return nil + } else { + log.Errorf("Ledger SubmitBlock GetBlock sharad height:%d,ParentHeight:%d error:%s", b.Header.Height, b.Header.ParentHeight, err) return err } - return nil + } + result, err := self.ParentBlockCache.GetBlockExecuteResult(b.Header.ParentHeight) + if err != nil { + return fmt.Errorf("SubmitBlock: get parent exec result failed, err: %s", err) + } + if err := self.ParentLedger.ldgStore.SubmitBlock(parentBlock, result); err != nil { + return fmt.Errorf("SubmitBlock: submit parent block failed, err: %s", err) } else { - log.Errorf("Ledger SubmitBlock GetBlock sharad height:%d,ParentHeight:%d error:%s", b.Header.Height, b.Header.ParentHeight, err) - return err + self.ParentBlockCache.DelBlock(b.Header.ParentHeight) } } - result, err := self.ParentBlockCache.GetBlockExecuteResult(b.Header.ParentHeight) - if err != nil { - return fmt.Errorf("SubmitBlock: get parent exec result failed, err: %s", err) - } - if err := self.ParentLedger.ldgStore.SubmitBlock(parentBlock, result); err != nil { - return fmt.Errorf("SubmitBlock: submit parent block failed, err: %s", err) - } else { - self.ParentBlockCache.DelBlock(b.Header.ParentHeight) - } } err := self.ldgStore.SubmitBlock(b, exec) if err != nil { diff --git a/core/store/ledgerstore/block_cache_store.go b/core/store/ledgerstore/block_cache_store.go index 80480a5ebc..a87d80870b 100644 --- a/core/store/ledgerstore/block_cache_store.go +++ b/core/store/ledgerstore/block_cache_store.go @@ -70,9 +70,11 @@ func (this *BlockCacheStore) PutBlock(block *types.Block, stateMerkleRoot common blkKey := fmt.Sprintf("blk-%d-%d", this.shardID.ToUint64(), block.Header.Height) sink := common.NewZeroCopySink(0) block.Serialization(sink) - this.store.Put([]byte(mklKey), stateMerkleRoot[:]) - this.store.Put([]byte(blkKey), sink.Bytes()) - return nil + err := this.store.Put([]byte(mklKey), stateMerkleRoot[:]) + if err != nil { + return err + } + return this.store.Put([]byte(blkKey), sink.Bytes()) } func (this *BlockCacheStore) GetBlock(height uint32) (*types.Block, common.Uint256, error) {