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

Optimize block about assert immutability #585

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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
6 changes: 5 additions & 1 deletion core/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ func (b *BlockChain) maybeAcceptBlock(block *types.SerializedBlock, flags Behavi
//
// Also, store the associated block index entry.
if !b.DB().HasBlock(block.Hash()) {
err := dbMaybeStoreBlock(b.DB(), block)
err := block.AssertImmutability()
if err != nil {
return nil, err
}
err = dbMaybeStoreBlock(b.DB(), block)
if err != nil {
panic(err.Error())
}
Expand Down
9 changes: 8 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,17 @@ func NewBlockDeepCopyCoinbase(msgBlock *Block) *SerializedBlock {
// calling BlockHash on the underlying Block, however it caches the
// result so subsequent calls are more efficient.
func (sb *SerializedBlock) Hash() *hash.Hash {
//TODO, might need to assertBlockImmutability
return &sb.hash
}

func (sb *SerializedBlock) AssertImmutability() error {
h := sb.block.BlockHash()
if (&h).IsEqual(&sb.hash) {
return nil
}
return fmt.Errorf("block hash inconsistent:%s != %s", h.String(), sb.hash.String())
}

func (sb *SerializedBlock) Block() *Block {
return sb.block
}
Expand Down
Loading