Skip to content

Commit ead8ba7

Browse files
committed
blockchain: Always check size and merkle root.
This modifies the assumevalid fast add path used during initial sync to also check the block size and merkle roots. These checks are now negligible in terms of the overall sync time since the hashing has been significantly optimized and retaining them prevents certain classes of potential harassment during initial sync.
1 parent 036b709 commit ead8ba7

1 file changed

Lines changed: 20 additions & 23 deletions

File tree

internal/blockchain/validate.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,15 +1922,12 @@ func (b *BlockChain) checkMerkleRoots(block *wire.MsgBlock, prevNode *blockNode)
19221922
// The flags modify the behavior of this function as follows:
19231923
//
19241924
// BFFastAdd:
1925-
// - The max block size is not checked
1926-
// - The calculated merkle root(s) of the transaction trees are not checked
1927-
// against the associated entries in the header
19281925
// - Transactions are not checked to see if they are finalized
19291926
// - The included votes, revocations, and treasury spend transactions are
19301927
// not verified to be allowed
19311928
//
1932-
// The flags are also passed to checkBlockHeaderContext. See its documentation
1933-
// for how the flags modify its behavior.
1929+
// The flags are also passed to [BlockChain.checkBlockHeaderContext]. See its
1930+
// documentation for how the flags modify its behavior.
19341931
func (b *BlockChain) checkBlockContext(block *dcrutil.Block, prevNode *blockNode, flags BehaviorFlags) error {
19351932
// The genesis block is valid by definition.
19361933
if prevNode == nil {
@@ -2216,26 +2213,26 @@ func (b *BlockChain) checkBlockContext(block *dcrutil.Block, prevNode *blockNode
22162213
}
22172214
}
22182215

2216+
// A block must not exceed the maximum allowed size as defined by the
2217+
// network parameters and the current status of any consensus votes to
2218+
// change it when serialized.
2219+
maxBlockSize := b.maxBlockSize(prevNode)
2220+
serializedSize := int64(block.MsgBlock().Header.Size)
2221+
if serializedSize > maxBlockSize {
2222+
str := fmt.Sprintf("serialized block is too big - got %d, max %d",
2223+
serializedSize, maxBlockSize)
2224+
return ruleError(ErrBlockTooBig, str)
2225+
}
2226+
2227+
// The calculated merkle root(s) of the transaction trees must match the
2228+
// associated entries in the header.
2229+
err = b.checkMerkleRoots(block.MsgBlock(), prevNode)
2230+
if err != nil {
2231+
return err
2232+
}
2233+
22192234
fastAdd := flags&BFFastAdd == BFFastAdd
22202235
if !fastAdd {
2221-
// A block must not exceed the maximum allowed size as defined by the
2222-
// network parameters and the current status of any hard fork votes to
2223-
// change it when serialized.
2224-
maxBlockSize := b.maxBlockSize(prevNode)
2225-
serializedSize := int64(block.MsgBlock().Header.Size)
2226-
if serializedSize > maxBlockSize {
2227-
str := fmt.Sprintf("serialized block is too big - got %d, max %d",
2228-
serializedSize, maxBlockSize)
2229-
return ruleError(ErrBlockTooBig, str)
2230-
}
2231-
2232-
// The calculated merkle root(s) of the transaction trees must match
2233-
// the associated entries in the header.
2234-
err = b.checkMerkleRoots(block.MsgBlock(), prevNode)
2235-
if err != nil {
2236-
return err
2237-
}
2238-
22392236
// Switch to using the past median time of the block prior to the block
22402237
// being checked for all checks related to lock times once the stake
22412238
// vote for the agenda is active.

0 commit comments

Comments
 (0)