blockchain, chaincfg, rpcserver: implement bip54#2537
Draft
kcalvinalvin wants to merge 8 commits into
Draft
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Code Style and Documentation
📝 Please see our Contribution Guidelines for further guidance.