Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/wallet.py
Show All 25 Lines | def generate(self, num_blocks): | ||||
and append the outputs to the internal list""" | and append the outputs to the internal list""" | ||||
blocks = self._test_node.generatetoaddress(num_blocks, self._address) | blocks = self._test_node.generatetoaddress(num_blocks, self._address) | ||||
for b in blocks: | for b in blocks: | ||||
cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0] | cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0] | ||||
self._utxos.append( | self._utxos.append( | ||||
{'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']}) | {'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']}) | ||||
return blocks | return blocks | ||||
def send_self_transfer(self, *, fee_rate, from_node): | def send_self_transfer(self, *, fee_rate=Decimal("3000.00"), from_node, | ||||
utxo_to_spend=None): | |||||
"""Create and send a tx with the specified fee_rate. Fee may be exact | """Create and send a tx with the specified fee_rate. Fee may be exact | ||||
or at most one satoshi higher than needed.""" | or at most one satoshi higher than needed.""" | ||||
self._utxos = sorted(self._utxos, key=lambda k: k['value']) | self._utxos = sorted(self._utxos, key=lambda k: k['value']) | ||||
# Pick the largest utxo and hope it covers the fee | # Pick the largest utxo (if none provided) and hope it covers the fee | ||||
largest_utxo = self._utxos.pop() | utxo_to_spend = utxo_to_spend or self._utxos.pop() | ||||
# The size will be enforced by pad_tx() | # The size will be enforced by pad_tx() | ||||
size = 101 | size = 101 | ||||
send_value = satoshi_round( | send_value = satoshi_round( | ||||
largest_utxo['value'] - fee_rate * (Decimal(size) / 1000)) | utxo_to_spend['value'] - fee_rate * (Decimal(size) / 1000)) | ||||
fee = largest_utxo['value'] - send_value | fee = utxo_to_spend['value'] - send_value | ||||
assert send_value > 0 | assert send_value > 0 | ||||
tx = CTransaction() | tx = CTransaction() | ||||
tx.vin = [CTxIn(COutPoint(int(largest_utxo['txid'], 16), | tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), | ||||
largest_utxo['vout']))] | utxo_to_spend['vout']))] | ||||
tx.vout = [CTxOut(int(send_value * XEC), self._scriptPubKey)] | tx.vout = [CTxOut(int(send_value * XEC), self._scriptPubKey)] | ||||
tx.vin[0].scriptSig = SCRIPTSIG_OP_TRUE | tx.vin[0].scriptSig = SCRIPTSIG_OP_TRUE | ||||
# FIXME: pad_tx overpads by 1 byte, and the fix is non-trivial | # FIXME: pad_tx overpads by 1 byte, and the fix is non-trivial | ||||
# (cf git history for txtools.py) | # (cf git history for txtools.py) | ||||
pad_tx(tx, size - 1) | pad_tx(tx, size - 1) | ||||
tx_hex = tx.serialize().hex() | tx_hex = tx.serialize().hex() | ||||
txid = from_node.sendrawtransaction(tx_hex) | txid = from_node.sendrawtransaction(tx_hex) | ||||
self._utxos.append({'txid': txid, 'vout': 0, 'value': send_value}) | self._utxos.append({'txid': txid, 'vout': 0, 'value': send_value}) | ||||
tx_info = from_node.getmempoolentry(txid) | tx_info = from_node.getmempoolentry(txid) | ||||
assert_equal(tx_info['size'], size) | assert_equal(tx_info['size'], size) | ||||
assert_equal(tx_info['fee'], fee) | assert_equal(tx_info['fee'], fee) | ||||
return {'txid': txid, 'hex': tx_hex} | return {'txid': txid, 'hex': tx_hex} |