@@ -28,6 +28,25 @@ def random_real_outputs_and_script(n):
28
28
outputs = [CTxOut ((x + 1 )* 100 , CScript (bytes ([OP_RETURN , 0x20 ]) + random_bytes (32 ))) for x in range (n )]
29
29
return outputs , CScript (bytes ([OP_CHECKOUTPUTSHASHVERIFY , 0x20 ]) + hash256 (b"" .join (o .serialize () for o in outputs )))
30
30
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
+
31
50
def get_taproot_bech32 (spk ):
32
51
return program_to_witness (1 , spk [2 :])
33
52
class COSHVTest (BitcoinTestFramework ):
@@ -51,6 +70,9 @@ def run_test(self):
51
70
sec1 .generate ()
52
71
pubkey1 = sec1 .get_pubkey ()
53
72
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 )
54
76
# Add some fee satoshis
55
77
amount = (sum (out .nValue for out in outputs )+ 200 * 500 ) / 100e6
56
78
@@ -59,15 +81,19 @@ def run_test(self):
59
81
get_taproot_bech32 (taproot ), amount = amount )
60
82
spendtx2 = create_transaction (self .nodes [0 ], self .coinbase_txids [1 ],
61
83
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 )
63
86
outpoint = COutPoint (int (spendtx .rehash (),16 ), 0 )
64
87
outpoint2 = COutPoint (int (spendtx2 .rehash (),16 ), 0 )
88
+ outpoint3 = COutPoint (int (spendtx3 .rehash (),16 ), 0 )
89
+
65
90
66
91
tip = self .nodes [0 ].getbestblockhash ()
67
92
height = self .nodes [0 ].getblockcount ()
68
93
block = create_block (int (tip , 16 ), create_coinbase (height ))
69
94
block .vtx .append (spendtx )
70
95
block .vtx .append (spendtx2 )
96
+ block .vtx .append (spendtx3 )
71
97
block .hashMerkleRoot = block .calc_merkle_root ()
72
98
block .solve ()
73
99
self .nodes [0 ].submitblock (block .serialize (False ).hex ())
@@ -76,7 +102,6 @@ def run_test(self):
76
102
77
103
# Test sendrawtransaction
78
104
coshvTx = CTransaction ()
79
- coshvTx .nVersion = 2
80
105
coshvTx .vin += [CTxIn (outpoint )]
81
106
coshvTx .vout += outputs
82
107
coshvTx .wit .vtxinwit += [CTxInWitness ()]
@@ -145,6 +170,25 @@ def run_test(self):
145
170
self .nodes [0 ].submitblock (block .serialize (True ).hex ())
146
171
assert_equal (self .nodes [0 ].getbestblockhash (), block .hash )
147
172
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
+
148
192
149
193
if __name__ == '__main__' :
150
194
COSHVTest ().main ()
0 commit comments