Skip to content

Commit 2a1090d

Browse files
committed
Merge pull request #6351
65ef372 Add BIP65 to getblockchaininfo softforks list (Peter Todd) cde7ab2 Add RPC tests for the CHECKLOCKTIMEVERIFY (BIP65) soft-fork (Peter Todd) 287f54f Add CHECKLOCKTIMEVERIFY (BIP65) soft-fork logic (Peter Todd)
2 parents bf7c195 + 65ef372 commit 2a1090d

File tree

7 files changed

+285
-5
lines changed

7 files changed

+285
-5
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
'blockchain.py',
7171
]
7272
testScriptsExt = [
73+
'bip65-cltv.py',
74+
'bip65-cltv-p2p.py',
7375
'bipdersig-p2p.py',
7476
'bipdersig.py',
7577
'getblocktemplate_longpoll.py',

qa/rpc-tests/bip65-cltv-p2p.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env python2
2+
#
3+
# Distributed under the MIT/X11 software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
#
6+
7+
from test_framework.test_framework import ComparisonTestFramework
8+
from test_framework.util import *
9+
from test_framework.mininode import CTransaction, NetworkThread
10+
from test_framework.blocktools import create_coinbase, create_block
11+
from test_framework.comptool import TestInstance, TestManager
12+
from test_framework.script import CScript, OP_1NEGATE, OP_NOP2, OP_DROP
13+
from binascii import hexlify, unhexlify
14+
import cStringIO
15+
import time
16+
17+
def cltv_invalidate(tx):
18+
'''Modify the signature in vin 0 of the tx to fail CLTV
19+
20+
Prepends -1 CLTV DROP in the scriptSig itself.
21+
'''
22+
tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_NOP2, OP_DROP] +
23+
list(CScript(tx.vin[0].scriptSig)))
24+
25+
'''
26+
This test is meant to exercise BIP65 (CHECKLOCKTIMEVERIFY)
27+
Connect to a single node.
28+
Mine 2 (version 3) blocks (save the coinbases for later).
29+
Generate 98 more version 3 blocks, verify the node accepts.
30+
Mine 749 version 4 blocks, verify the node accepts.
31+
Check that the new CLTV rules are not enforced on the 750th version 4 block.
32+
Check that the new CLTV rules are enforced on the 751st version 4 block.
33+
Mine 199 new version blocks.
34+
Mine 1 old-version block.
35+
Mine 1 new version block.
36+
Mine 1 old version block, see that the node rejects.
37+
'''
38+
39+
class BIP65Test(ComparisonTestFramework):
40+
41+
def __init__(self):
42+
self.num_nodes = 1
43+
44+
def setup_network(self):
45+
# Must set the blockversion for this test
46+
self.nodes = start_nodes(1, self.options.tmpdir,
47+
extra_args=[['-debug', '-whitelist=127.0.0.1', '-blockversion=3']],
48+
binary=[self.options.testbinary])
49+
50+
def run_test(self):
51+
test = TestManager(self, self.options.tmpdir)
52+
test.add_all_connections(self.nodes)
53+
NetworkThread().start() # Start up network handling in another thread
54+
test.run()
55+
56+
def create_transaction(self, node, coinbase, to_address, amount):
57+
from_txid = node.getblock(coinbase)['tx'][0]
58+
inputs = [{ "txid" : from_txid, "vout" : 0}]
59+
outputs = { to_address : amount }
60+
rawtx = node.createrawtransaction(inputs, outputs)
61+
signresult = node.signrawtransaction(rawtx)
62+
tx = CTransaction()
63+
f = cStringIO.StringIO(unhexlify(signresult['hex']))
64+
tx.deserialize(f)
65+
return tx
66+
67+
def get_tests(self):
68+
69+
self.coinbase_blocks = self.nodes[0].generate(2)
70+
self.tip = int ("0x" + self.nodes[0].getbestblockhash() + "L", 0)
71+
self.nodeaddress = self.nodes[0].getnewaddress()
72+
self.last_block_time = time.time()
73+
74+
''' 98 more version 3 blocks '''
75+
test_blocks = []
76+
for i in xrange(98):
77+
block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1)
78+
block.nVersion = 3
79+
block.rehash()
80+
block.solve()
81+
test_blocks.append([block, True])
82+
self.last_block_time += 1
83+
self.tip = block.sha256
84+
yield TestInstance(test_blocks, sync_every_block=False)
85+
86+
''' Mine 749 version 4 blocks '''
87+
test_blocks = []
88+
for i in xrange(749):
89+
block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1)
90+
block.nVersion = 4
91+
block.rehash()
92+
block.solve()
93+
test_blocks.append([block, True])
94+
self.last_block_time += 1
95+
self.tip = block.sha256
96+
yield TestInstance(test_blocks, sync_every_block=False)
97+
98+
'''
99+
Check that the new CLTV rules are not enforced in the 750th
100+
version 3 block.
101+
'''
102+
spendtx = self.create_transaction(self.nodes[0],
103+
self.coinbase_blocks[0], self.nodeaddress, 1.0)
104+
cltv_invalidate(spendtx)
105+
spendtx.rehash()
106+
107+
block = create_block(self.tip, create_coinbase(2), self.last_block_time + 1)
108+
block.nVersion = 4
109+
block.vtx.append(spendtx)
110+
block.hashMerkleRoot = block.calc_merkle_root()
111+
block.rehash()
112+
block.solve()
113+
114+
self.last_block_time += 1
115+
self.tip = block.sha256
116+
yield TestInstance([[block, True]])
117+
118+
'''
119+
Check that the new CLTV rules are enforced in the 751st version 4
120+
block.
121+
'''
122+
spendtx = self.create_transaction(self.nodes[0],
123+
self.coinbase_blocks[1], self.nodeaddress, 1.0)
124+
cltv_invalidate(spendtx)
125+
spendtx.rehash()
126+
127+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
128+
block.nVersion = 4
129+
block.vtx.append(spendtx)
130+
block.hashMerkleRoot = block.calc_merkle_root()
131+
block.rehash()
132+
block.solve()
133+
self.last_block_time += 1
134+
yield TestInstance([[block, False]])
135+
136+
''' Mine 199 new version blocks on last valid tip '''
137+
test_blocks = []
138+
for i in xrange(199):
139+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
140+
block.nVersion = 4
141+
block.rehash()
142+
block.solve()
143+
test_blocks.append([block, True])
144+
self.last_block_time += 1
145+
self.tip = block.sha256
146+
yield TestInstance(test_blocks, sync_every_block=False)
147+
148+
''' Mine 1 old version block '''
149+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
150+
block.nVersion = 3
151+
block.rehash()
152+
block.solve()
153+
self.last_block_time += 1
154+
self.tip = block.sha256
155+
yield TestInstance([[block, True]])
156+
157+
''' Mine 1 new version block '''
158+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
159+
block.nVersion = 4
160+
block.rehash()
161+
block.solve()
162+
self.last_block_time += 1
163+
self.tip = block.sha256
164+
yield TestInstance([[block, True]])
165+
166+
''' Mine 1 old version block, should be invalid '''
167+
block = create_block(self.tip, create_coinbase(1), self.last_block_time + 1)
168+
block.nVersion = 3
169+
block.rehash()
170+
block.solve()
171+
self.last_block_time += 1
172+
yield TestInstance([[block, False]])
173+
174+
if __name__ == '__main__':
175+
BIP65Test().main()

qa/rpc-tests/bip65-cltv.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2015 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#
7+
# Test the CHECKLOCKTIMEVERIFY (BIP65) soft-fork logic
8+
#
9+
10+
from test_framework.test_framework import BitcoinTestFramework
11+
from test_framework.util import *
12+
import os
13+
import shutil
14+
15+
class BIP65Test(BitcoinTestFramework):
16+
17+
def setup_network(self):
18+
self.nodes = []
19+
self.nodes.append(start_node(0, self.options.tmpdir, []))
20+
self.nodes.append(start_node(1, self.options.tmpdir, ["-blockversion=3"]))
21+
self.nodes.append(start_node(2, self.options.tmpdir, ["-blockversion=4"]))
22+
connect_nodes(self.nodes[1], 0)
23+
connect_nodes(self.nodes[2], 0)
24+
self.is_network_split = False
25+
self.sync_all()
26+
27+
def run_test(self):
28+
cnt = self.nodes[0].getblockcount()
29+
30+
# Mine some old-version blocks
31+
self.nodes[1].generate(100)
32+
self.sync_all()
33+
if (self.nodes[0].getblockcount() != cnt + 100):
34+
raise AssertionError("Failed to mine 100 version=3 blocks")
35+
36+
# Mine 750 new-version blocks
37+
for i in xrange(15):
38+
self.nodes[2].generate(50)
39+
self.sync_all()
40+
if (self.nodes[0].getblockcount() != cnt + 850):
41+
raise AssertionError("Failed to mine 750 version=4 blocks")
42+
43+
# TODO: check that new CHECKLOCKTIMEVERIFY rules are not enforced
44+
45+
# Mine 1 new-version block
46+
self.nodes[2].generate(1)
47+
self.sync_all()
48+
if (self.nodes[0].getblockcount() != cnt + 851):
49+
raise AssertionFailure("Failed to mine a version=4 blocks")
50+
51+
# TODO: check that new CHECKLOCKTIMEVERIFY rules are enforced
52+
53+
# Mine 198 new-version blocks
54+
for i in xrange(2):
55+
self.nodes[2].generate(99)
56+
self.sync_all()
57+
if (self.nodes[0].getblockcount() != cnt + 1049):
58+
raise AssertionError("Failed to mine 198 version=4 blocks")
59+
60+
# Mine 1 old-version block
61+
self.nodes[1].generate(1)
62+
self.sync_all()
63+
if (self.nodes[0].getblockcount() != cnt + 1050):
64+
raise AssertionError("Failed to mine a version=3 block after 949 version=4 blocks")
65+
66+
# Mine 1 new-version blocks
67+
self.nodes[2].generate(1)
68+
self.sync_all()
69+
if (self.nodes[0].getblockcount() != cnt + 1051):
70+
raise AssertionError("Failed to mine a version=4 block")
71+
72+
# Mine 1 old-version blocks
73+
try:
74+
self.nodes[1].generate(1)
75+
raise AssertionError("Succeeded to mine a version=3 block after 950 version=4 blocks")
76+
except JSONRPCException:
77+
pass
78+
self.sync_all()
79+
if (self.nodes[0].getblockcount() != cnt + 1051):
80+
raise AssertionError("Accepted a version=3 block after 950 version=4 blocks")
81+
82+
# Mine 1 new-version blocks
83+
self.nodes[2].generate(1)
84+
self.sync_all()
85+
if (self.nodes[0].getblockcount() != cnt + 1052):
86+
raise AssertionError("Failed to mine a version=4 block")
87+
88+
if __name__ == '__main__':
89+
BIP65Test().main()

src/main.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,11 +1751,18 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17511751

17521752
unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
17531753

1754-
// Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded:
1754+
// Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks,
1755+
// when 75% of the network has upgraded:
17551756
if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus())) {
17561757
flags |= SCRIPT_VERIFY_DERSIG;
17571758
}
17581759

1760+
// Start enforcing CHECKLOCKTIMEVERIFY, (BIP65) for block.nVersion=4
1761+
// blocks, when 75% of the network has upgraded:
1762+
if (block.nVersion >= 4 && IsSuperMajority(4, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus())) {
1763+
flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
1764+
}
1765+
17591766
CBlockUndo blockundo;
17601767

17611768
CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
@@ -2702,6 +2709,11 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
27022709
return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
27032710
REJECT_OBSOLETE, "bad-version");
27042711

2712+
// Reject block.nVersion=3 blocks when 95% (75% on testnet) of the network has upgraded:
2713+
if (block.nVersion < 4 && IsSuperMajority(4, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
2714+
return state.Invalid(error("%s : rejected nVersion=3 block", __func__),
2715+
REJECT_OBSOLETE, "bad-version");
2716+
27052717
return true;
27062718
}
27072719

src/primitives/block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CBlockHeader
2121
{
2222
public:
2323
// header
24-
static const int32_t CURRENT_VERSION=3;
24+
static const int32_t CURRENT_VERSION=4;
2525
int32_t nVersion;
2626
uint256 hashPrevBlock;
2727
uint256 hashMerkleRoot;

src/rpcblockchain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
648648
UniValue softforks(UniValue::VARR);
649649
softforks.push_back(SoftForkDesc("bip34", 2, tip, consensusParams));
650650
softforks.push_back(SoftForkDesc("bip66", 3, tip, consensusParams));
651+
softforks.push_back(SoftForkDesc("bip65", 4, tip, consensusParams));
651652
obj.push_back(Pair("softforks", softforks));
652653

653654
if (fPruneMode)

src/script/bitcoinconsensus.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ typedef enum bitcoinconsensus_error_t
4444
/** Script verification flags */
4545
enum
4646
{
47-
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_NONE = 0,
48-
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_P2SH = (1U << 0), // evaluate P2SH (BIP16) subscripts
49-
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_DERSIG = (1U << 2), // enforce strict DER (BIP66) compliance
47+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_NONE = 0,
48+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_P2SH = (1U << 0), // evaluate P2SH (BIP16) subscripts
49+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_DERSIG = (1U << 2), // enforce strict DER (BIP66) compliance
50+
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
5051
};
5152

5253
/// Returns 1 if the input nIn of the serialized transaction pointed to by

0 commit comments

Comments
 (0)