Skip to content

Commit 4bc9ac9

Browse files
committed
Add test code showcasing congestion control
1 parent b81bd85 commit 4bc9ac9

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

test/functional/feature_outputshashverify.py

+46-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ def random_real_outputs_and_script(n):
2828
outputs = [CTxOut((x+1)*100, CScript(bytes([OP_RETURN, 0x20]) + random_bytes(32))) for x in range(n)]
2929
return outputs, CScript(bytes([OP_CHECKOUTPUTSHASHVERIFY, 0x20]) + hash256(b"".join(o.serialize() for o in outputs)))
3030

31+
def random_tapscript_tree(depth):
32+
33+
sec1 = ECKey()
34+
sec1.generate()
35+
pubkey1 = sec1.get_pubkey()
36+
outputs_tree = [[CTxOut()]*(2**i) for i in range(depth)]
37+
control_tree = [[0]*(2**i) for i in range(depth+1)]
38+
outputs_tree += [[CTxOut(100, CScript(bytes([OP_RETURN, 0x20]) + random_bytes(32))) for x in range(2**depth)]]
39+
for d in range(1, depth+2):
40+
idxs =zip(range(0, len(outputs_tree[-d]),2), range(1, len(outputs_tree[-d]), 2))
41+
for (idx, (a,b)) in enumerate([(outputs_tree[-d][i], outputs_tree[-d][j]) for (i,j) in idxs]):
42+
s = CScript(bytes([OP_CHECKOUTPUTSHASHVERIFY, 0x20]) + hash256(b"".join(o.serialize() for o in [a,b])))
43+
a = sum(o.nValue for o in [a,b])
44+
taproot, tweak, controls = taproot_construct(pubkey1, [s])
45+
t = CTxOut(a+1000, taproot)
46+
outputs_tree[-d-1][idx] = t
47+
control_tree[-d-1][idx] = [s, controls[s]]
48+
return outputs_tree, control_tree
49+
3150
def get_taproot_bech32(spk):
3251
return program_to_witness(1, spk[2:])
3352
class COSHVTest(BitcoinTestFramework):
@@ -51,6 +70,9 @@ def run_test(self):
5170
sec1.generate()
5271
pubkey1 = sec1.get_pubkey()
5372
taproot, tweak, controls = taproot_construct(pubkey1, [script])
73+
# Small Tree for test speed, can be set to a large value like 16 (i.e., 65K txns)
74+
TREE_SIZE = 4
75+
congestion_tree_txo, congestion_tree_ctl = random_tapscript_tree(TREE_SIZE)
5476
# Add some fee satoshis
5577
amount = (sum(out.nValue for out in outputs)+200*500) /100e6
5678

@@ -59,15 +81,19 @@ def run_test(self):
5981
get_taproot_bech32(taproot), amount=amount)
6082
spendtx2 = create_transaction(self.nodes[0], self.coinbase_txids[1],
6183
script_to_p2sh(CScript([OP_TRUE])), amount=amount)
62-
print(spendtx2.serialize().hex())
84+
spendtx3 = create_transaction(self.nodes[0], self.coinbase_txids[2],
85+
get_taproot_bech32(congestion_tree_txo[0][0].scriptPubKey), amount=congestion_tree_txo[0][0].nValue/100e6)
6386
outpoint = COutPoint(int(spendtx.rehash(),16), 0)
6487
outpoint2 = COutPoint(int(spendtx2.rehash(),16), 0)
88+
outpoint3 = COutPoint(int(spendtx3.rehash(),16), 0)
89+
6590

6691
tip = self.nodes[0].getbestblockhash()
6792
height = self.nodes[0].getblockcount()
6893
block = create_block(int(tip, 16), create_coinbase(height))
6994
block.vtx.append(spendtx)
7095
block.vtx.append(spendtx2)
96+
block.vtx.append(spendtx3)
7197
block.hashMerkleRoot = block.calc_merkle_root()
7298
block.solve()
7399
self.nodes[0].submitblock(block.serialize(False).hex())
@@ -76,7 +102,6 @@ def run_test(self):
76102

77103
# Test sendrawtransaction
78104
coshvTx = CTransaction()
79-
coshvTx.nVersion = 2
80105
coshvTx.vin += [CTxIn(outpoint)]
81106
coshvTx.vout += outputs
82107
coshvTx.wit.vtxinwit += [CTxInWitness()]
@@ -145,6 +170,25 @@ def run_test(self):
145170
self.nodes[0].submitblock(block.serialize(True).hex())
146171
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
147172

173+
# Expand Congestion Control Tree to one specific input
174+
tip = self.nodes[0].getbestblockhash()
175+
height = self.nodes[0].getblockcount()
176+
block = create_block(int(tip, 16), create_coinbase(height))
177+
out = outpoint3
178+
for x in range(TREE_SIZE):
179+
spendtx = CTransaction()
180+
spendtx.vin += [CTxIn(out)]
181+
spendtx.wit.vtxinwit += [CTxInWitness()]
182+
spendtx.wit.vtxinwit[0].scriptWitness.stack = congestion_tree_ctl[x][0]
183+
spendtx.vout += congestion_tree_txo[x+1][:2]
184+
out = COutPoint(int(spendtx.rehash(),16), 0)
185+
block.vtx.append(spendtx)
186+
add_witness_commitment(block)
187+
block.hashMerkleRoot = block.calc_merkle_root()
188+
block.solve()
189+
self.nodes[0].submitblock(block.serialize(True).hex())
190+
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
191+
148192

149193
if __name__ == '__main__':
150194
COSHVTest().main()

0 commit comments

Comments
 (0)