Skip to content

blockchain, chaincfg, rpcserver: implement bip54#2537

Draft
kcalvinalvin wants to merge 8 commits into
btcsuite:masterfrom
kcalvinalvin:2026-05-28-implement-bip54
Draft

blockchain, chaincfg, rpcserver: implement bip54#2537
kcalvinalvin wants to merge 8 commits into
btcsuite:masterfrom
kcalvinalvin:2026-05-28-implement-bip54

Conversation

@kcalvinalvin

Copy link
Copy Markdown
Collaborator

Change Description

The PR is to implement bip54.

I've put it in draft since it's not landed in bitcoin core either. It's just a PR on the BIP authors own fork.

Obviously speaking there's still discourse on how to go about fixing some of the things BIP54 is trying to address. Because of this, I don't think it's ready for merging but I'm putting it here as a draft BIP for visibility. I should also note that BIP54 addresses the incomplete fix for BIP34, which I like because of my work on the Utreexo project as it's annoying for Utreexo nodes to perform the BIP30 checks.

Steps to Test

BIP54 implements 4 rules. The commit structure is made so that 1 commit is implementing 1 rule. The earlier changes in chaincfg and rpcserver only there to add activation code for bip54. The commit for skipping BIP30 and BIP34 checks are not listed in BIP54 but I believe it was just an accidental omission.

The reviewer should check if the implementation matches the rule listed on bip54.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Reserve the DeploymentBIP54 deployment ID and add a corresponding
ConsensusDeployment entry to MainNet, RegTest, TestNet3, TestNet4,
SimNet, and SigNet params. The starter uses the zero time (always
started) and the ender uses time.Unix(0, 0), which forces the
deployment to transition immediately to ThresholdFailed on every
network.

BIP54 has no scheduled activation. These entries exist so downstream
code can reference DeploymentBIP54 without claiming activation
parameters that have not been decided.
Handle chaincfg.DeploymentBIP54 in handleGetBlockChainInfo's fork name
switch so getblockchaininfo reports the deployment as "bip54" rather
than returning an "Unknown deployment" RPC error.

The DeploymentBIP54 constant lives in the local chaincfg submodule but
is not yet present in any published chaincfg/v2 release, so this commit
also adds a local replace directive:

    replace github.com/btcsuite/btcd/chaincfg/v2 => ./chaincfg

This replace directive is temporary. Drop it once chaincfg is
republished with the new DeploymentBIP54 constant.
Copy the upstream BIP-54 test vectors verbatim from bitcoin/bips into
testdata/bip54/ (timestamps, sigops, txsize, coinbases) and add
bip54_test.go: JSON loaders, decoders for hex-encoded transactions /
headers / blocks / TxOuts, and a tree-flattener for the prefix-tree
encoding of timestamps.json. Each test decodes its vectors so malformed
JSON or hex is caught, then t.Skip pending the corresponding consensus
rules.
Reject a coinbase whose nLockTime != blockHeight-1 or whose input's
nSequence == 0xffffffff, gated on DeploymentBIP54 being Active. Adds
CheckBIP54Coinbase, the ErrBadCoinbaseLockTime code, and wires the
check into checkBlockContext after the BIP-34 height check.

Adds a table-driven unit test for CheckBIP54Coinbase and turns
TestBIP54Coinbases on against coinbases.json by feeding each chain
through ProcessBlock with a Params copy whose
DeploymentBIP54.AlwaysActiveHeight is set to 1.
BIP-54's coinbase nLockTime+nSequence rule already commits each
coinbase to its block height, making both BIP-30 (duplicate-coinbase
lookup) and BIP-34 (height encoded in coinbase scriptSig) redundant.
Reject any transaction whose stripped (no-witness) serialization is
exactly 64 bytes, gated on DeploymentBIP54 being Active. A 64-byte
transaction at the leaf level of the Merkle tree is indistinguishable
from an internal node hash and allows Merkle-tree malleability.

Adds CheckBIP54TxSize, the ErrBadTxSize code, calls it for every
transaction in checkBlockContext when BIP-54 is active, and turns
TestBIP54TxSize on against CheckBIP54TxSize for all 13 upstream
vectors.
Reject any non-coinbase transaction whose input scripts contain more
than 2500 potentially-executed legacy signature operations, gated on
DeploymentBIP54 being Active. For each input, CHECKSIG and CHECKMULTISIG
opcodes in the scriptSig, the spent output's scriptPubKey, and the
P2SH redeemScript (when applicable) are summed using BIP-16 precise
accounting.

Adds MaxBIP54LegacySigOpsPerTx, defaultCheckMultiSigSigOps, the
ErrTooManyBIP54SigOps code, a small preciseLegacySigOps tokenizer-walker,
and CountBIP54SigOps / CheckBIP54SigOps. checkConnectBlock invokes the
check after the existing block-wide sigop cost accounting. The redeem-
script piece reuses txscript.GetPreciseSigOpCount.

Turns TestBIP54Sigops on against CheckBIP54SigOps; all 39 upstream
vectors pass.
Add two header-timestamp checks at difficulty-period boundaries, gated
on DeploymentBIP54 being Active. Letting N be the block height and TN
its timestamp:

  - If N % 2016 == 0, TN must satisfy TN >= TN-1 - 7200.
  - If N % 2016 == 2015, TN must satisfy TN >= TN-2015.

Adds CheckBIP54Timestamps and the ErrBadBIP54Timestamp code, and wires
the check into CheckBlockHeaderContext alongside the existing BIP-94
timewarp call. ChainCtx gains a BIP54Active(prevNode) method so the
header-level check can resolve the deployment state without touching
*BlockChain directly; stubChainCtx returns false. Turns
TestBIP54Timestamps on against ProcessBlockHeader, asserting that the
vector's first header matches the mainnet genesis before stepping
through the rest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant