Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle ledger recovery #952

Open
wants to merge 1 commit into
base: sharding
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions core/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Honglei-Cong marked this conversation as resolved.
Show resolved Hide resolved
} 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 {
Expand Down
8 changes: 5 additions & 3 deletions core/store/ledgerstore/block_cache_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down