mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-04 04:45:10 +02:00
Merge #19940: rpc: Return fee and vsize from testmempoolaccept
23c35bf005[test] add get_vsize util for more programmatic testing (gzhao408)2233a93a10[rpc] Return fee and vsize from testmempoolaccept (codeShark149) Pull request description: From #19093 and resolves #19057. Difference from #19093: return `vsize` and `fees` object (similar to `getmempoolentry`) when the test accept is successful. Updates release-notes.md. ACKs for top commit: jnewbery: utACK23c35bf005fjahr: utACK23c35bfinstagibbs: reACK23c35bf005Tree-SHA512: dcb81b7b817a4684e9076bc5d427a6f2d549d2edc66544e718260c4b5f8f1d5ae1d47b754175e9f0c8a3bd8371ce116c2dca0583588d513a7d733d5d614f2b04
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test mempool acceptance of raw transactions."""
|
||||
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
import math
|
||||
|
||||
@@ -91,20 +92,22 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
|
||||
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
|
||||
txid_0 = tx.rehash()
|
||||
self.check_mempool_result(
|
||||
result_expected=[{'txid': txid_0, 'allowed': True}],
|
||||
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(fee))}}],
|
||||
rawtxs=[raw_tx_0],
|
||||
)
|
||||
|
||||
self.log.info('A final transaction not in the mempool')
|
||||
coin = coins.pop() # Pick a random coin(base) to spend
|
||||
output_amount = 0.025
|
||||
raw_tx_final = node.signrawtransactionwithwallet(node.createrawtransaction(
|
||||
inputs=[{'txid': coin['txid'], 'vout': coin['vout'], "sequence": 0xffffffff}], # SEQUENCE_FINAL
|
||||
outputs=[{node.getnewaddress(): 0.025}],
|
||||
outputs=[{node.getnewaddress(): output_amount}],
|
||||
locktime=node.getblockcount() + 2000, # Can be anything
|
||||
))['hex']
|
||||
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_final)))
|
||||
fee_expected = int(coin['amount']) - output_amount
|
||||
self.check_mempool_result(
|
||||
result_expected=[{'txid': tx.rehash(), 'allowed': True}],
|
||||
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(fee_expected))}}],
|
||||
rawtxs=[tx.serialize().hex()],
|
||||
maxfeerate=0,
|
||||
)
|
||||
@@ -127,7 +130,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
|
||||
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
|
||||
txid_0 = tx.rehash()
|
||||
self.check_mempool_result(
|
||||
result_expected=[{'txid': txid_0, 'allowed': True}],
|
||||
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(2 * fee))}}],
|
||||
rawtxs=[raw_tx_0],
|
||||
)
|
||||
|
||||
@@ -187,7 +190,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
|
||||
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference)))
|
||||
# Reference tx should be valid on itself
|
||||
self.check_mempool_result(
|
||||
result_expected=[{'txid': tx.rehash(), 'allowed': True}],
|
||||
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': { 'base': Decimal(str(0.1 - 0.05))}}],
|
||||
rawtxs=[tx.serialize().hex()],
|
||||
maxfeerate=0,
|
||||
)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test segwit transactions and blocks on P2P network."""
|
||||
from decimal import Decimal
|
||||
import math
|
||||
import random
|
||||
import struct
|
||||
@@ -695,13 +696,13 @@ class SegWitTest(BitcoinTestFramework):
|
||||
if not self.segwit_active:
|
||||
# Just check mempool acceptance, but don't add the transaction to the mempool, since witness is disallowed
|
||||
# in blocks and the tx is impossible to mine right now.
|
||||
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True}])
|
||||
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': tx3.get_vsize(), 'fees': { 'base': Decimal('0.00001000')}}])
|
||||
# Create the same output as tx3, but by replacing tx
|
||||
tx3_out = tx3.vout[0]
|
||||
tx3 = tx
|
||||
tx3.vout = [tx3_out]
|
||||
tx3.rehash()
|
||||
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True}])
|
||||
assert_equal(self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()]), [{'txid': tx3.hash, 'allowed': True, 'vsize': tx3.get_vsize(), 'fees': { 'base': Decimal('0.00011000')}}])
|
||||
test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True)
|
||||
|
||||
self.nodes[0].generate(1)
|
||||
|
||||
@@ -22,6 +22,7 @@ from codecs import encode
|
||||
import copy
|
||||
import hashlib
|
||||
from io import BytesIO
|
||||
import math
|
||||
import random
|
||||
import socket
|
||||
import struct
|
||||
@@ -67,6 +68,8 @@ MSG_WITNESS_TX = MSG_TX | MSG_WITNESS_FLAG
|
||||
|
||||
FILTER_TYPE_BASIC = 0
|
||||
|
||||
WITNESS_SCALE_FACTOR = 4
|
||||
|
||||
# Serialization/deserialization tools
|
||||
def sha256(s):
|
||||
return hashlib.new('sha256', s).digest()
|
||||
@@ -537,6 +540,13 @@ class CTransaction:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Calculate the virtual transaction size using witness and non-witness
|
||||
# serialization size (does NOT use sigops).
|
||||
def get_vsize(self):
|
||||
with_witness_size = len(self.serialize_with_witness())
|
||||
without_witness_size = len(self.serialize_without_witness())
|
||||
return math.ceil(((WITNESS_SCALE_FACTOR - 1) * without_witness_size + with_witness_size) / WITNESS_SCALE_FACTOR)
|
||||
|
||||
def __repr__(self):
|
||||
return "CTransaction(nVersion=%i vin=%s vout=%s wit=%s nLockTime=%i)" \
|
||||
% (self.nVersion, repr(self.vin), repr(self.vout), repr(self.wit), self.nLockTime)
|
||||
|
||||
Reference in New Issue
Block a user