Skip to content

Commit

Permalink
Optimize block about assert immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
lochjin committed Dec 17, 2023
1 parent a41c5fc commit f90dc13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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

0 comments on commit f90dc13

Please sign in to comment.