Skip to content

Commit

Permalink
test: fix dbcrash and fee_estimation extended tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delta1 committed Apr 4, 2024
1 parent 2d298f7 commit 1ed7f84
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
11 changes: 8 additions & 3 deletions test/functional/feature_dbcrash.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,18 @@ def verify_utxo_hash(self):
assert_equal(nodei_utxo_hash, node3_utxo_hash)

def generate_small_transactions(self, node, count, utxo_list):
FEE = 1000 # TODO: replace this with node relay fee based calculation
num_transactions = 0
random.shuffle(utxo_list)
while len(utxo_list) >= 2 and num_transactions < count:
fee = 1000
tx = CTransaction()
input_amount = 0
for _ in range(2):
utxo = utxo_list.pop()
tx.vin.append(CTxIn(COutPoint(int(utxo['txid'], 16), utxo['vout'])))
input_amount += int(utxo['amount'] * COIN)
output_amount = (input_amount - FEE) // 3
output_amount = (input_amount - fee) // 3
fee = input_amount - (3 * output_amount)

if output_amount <= 0:
# Sanity check -- if we chose inputs that are too small, skip
Expand All @@ -205,6 +206,9 @@ def generate_small_transactions(self, node, count, utxo_list):
for _ in range(3):
tx.vout.append(CTxOut(output_amount, bytes.fromhex(utxo['scriptPubKey'])))

# ELEMENTS: add fee output
tx.vout.append(CTxOut(fee))

# Sign and send the transaction to get into the mempool
tx_signed_hex = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
node.sendrawtransaction(tx_signed_hex)
Expand Down Expand Up @@ -234,7 +238,8 @@ def run_test(self):
# Main test loop:
# each time through the loop, generate a bunch of transactions,
# and then either mine a single new block on the tip, or some-sized reorg.
for i in range(40):
# ELEMENTS: reduced iters to run in some "reasonable" amount of time (~6 hours)
for i in range(4):
self.log.info(f"Iteration {i}, generating 2500 transactions {self.restart_counts}")
# Generate a bunch of small-ish transactions
self.generate_small_transactions(self.nodes[3], 2500, utxo_list)
Expand Down
9 changes: 6 additions & 3 deletions test/functional/feature_fee_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ def send_tx(node, utxo, feerate):
"""Broadcast a 1in-1out transaction with a specific input and feerate (sat/vb)."""
tx = CTransaction()
tx.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]), REDEEM_SCRIPT)]
tx.vout = [CTxOut(int(utxo["amount"] * COIN), P2SH)]
tx.vout = [CTxOut(int(utxo["amount"] * COIN), P2SH), CTxOut(int(utxo["amount"] * COIN))]

# vbytes == bytes as we are using legacy transactions
fee = tx.get_vsize() * feerate
tx.vout[0].nValue -= fee
amount = tx.vout[0].nValue.getAmount()
tx.vout[0].nValue.setToAmount(amount - fee)
tx.vout[1].nValue.setToAmount(fee)

return node.sendrawtransaction(tx.serialize().hex())

Expand Down Expand Up @@ -208,7 +210,7 @@ def transact_and_mine(self, numblocks, mining_node):

def initial_split(self, node):
"""Split two coinbase UTxOs into many small coins"""
utxo_count = 2048
utxo_count = 1450 # ELEMENTS reduced to fit into max tx weight
self.confutxo = []
splitted_amount = Decimal("0.04")
fee = Decimal("0.1")
Expand All @@ -220,6 +222,7 @@ def initial_split(self, node):
]
tx.vout = [CTxOut(int(splitted_amount * COIN), P2SH) for _ in range(utxo_count)]
tx.vout.append(CTxOut(int(change * COIN), P2SH))
tx.vout.append(CTxOut(int(fee * COIN)))
txhex = node.signrawtransactionwithwallet(tx.serialize().hex())["hex"]
txid = node.sendrawtransaction(txhex)
self.confutxo = [
Expand Down
5 changes: 4 additions & 1 deletion test/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,10 @@ def create_confirmed_utxos(test_framework, fee, node, count, **kwargs):
inputs = []
inputs.append({"txid": t["txid"], "vout": t["vout"]})
send_value = t['amount'] - fee
outputs = [{addr1: satoshi_round(send_value / 2)}, {addr2: satoshi_round(send_value / 2)}, {"fee": fee}]
# ELEMENTS: ensure outputs balance with inputs
val1 = satoshi_round(send_value / 2)
val2 = send_value - val1
outputs = [{addr1: val1}, {addr2: val2}, {"fee": fee}]
raw_tx = node.createrawtransaction(inputs, outputs)
signed_tx = node.signrawtransactionwithwallet(raw_tx)["hex"]
node.sendrawtransaction(signed_tx)
Expand Down

0 comments on commit 1ed7f84

Please sign in to comment.