Skip to content
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
36 changes: 36 additions & 0 deletions blockchain/blake2b_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -281,3 +282,38 @@ func TestCheckHeaderV2Sanity(t *testing.T) {
t.Fatalf("v2 header without BLAKE2b configured: got %v, want ErrUnsupportedProofOfWork", code)
}
}

// TestProcessBlockHeaderV2Sanity checks that ProcessBlock applies
// CheckHeaderV2Sanity before a block is kept as an orphan, where the real
// height is not known yet: a v2 block committing to a height below the
// activation is refused, and the same block committing to the activation
// height is kept as an orphan.
func TestProcessBlockHeaderV2Sanity(t *testing.T) {
params := chaincfg.RegressionNetParams
params.Blake2bHeight = 100
chain, teardown, err := chainSetup("processv2sanity", &params)
if err != nil {
t.Fatalf("chain setup: %v", err)
}
defer teardown()

orphanAt := func(height int32) *btcutil.Block {
msg := *params.GenesisBlock
msg.Header.HeaderV2 = true
msg.Header.Height = height
msg.Header.PrevBlock = chainhash.Hash{0x01}
msg.Header.Timestamp = time.Unix(time.Now().Unix(), 0)
return btcutil.NewBlock(&msg)
}

_, _, err = chain.ProcessBlock(orphanAt(99), BFNoPoWCheck)
if code, _ := ruleCode(err); code != ErrBadHeaderV2 {
t.Fatalf("v2 orphan committing to the block below the activation: "+
"got %v, want ErrBadHeaderV2", err)
}
_, isOrphan, err := chain.ProcessBlock(orphanAt(100), BFNoPoWCheck)
if err != nil || !isOrphan {
t.Fatalf("v2 orphan committing to the activation height: orphan %v, %v",
isOrphan, err)
}
}
5 changes: 4 additions & 1 deletion blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ func checkProofOfWork(header *wire.BlockHeader, powLimit *big.Int, flags Behavio
// GetHash() either way. The one-off target shift at the BLAKE2b
// activation belongs to the difficulty rules (calcNextRequiredDifficulty),
// not to this comparison. Whether a v2 header is allowed at its height
// at all is a contextual question, answered in CheckBlockHeaderContext.
// is checked in two places: CheckHeaderV2Sanity refuses one committing
// to a height below the activation before the real height is known (an
// orphan), and checkHeaderV2Context compares the committed height with
// the real one.
if header.IsV2() && header.Flags&header2ReservedFlags != 0 {
// Knots' bad-flags-highbits: the top two bits of flags are
// reserved for a future hard fork.
Expand Down