From d0f714638ac565fbfeccd9a7940d827c19ce0993 Mon Sep 17 00:00:00 2001 From: Byron Hambly Date: Fri, 20 Sep 2024 14:39:13 +0200 Subject: [PATCH] feat: add discountweight to TxToUniv --- src/core_write.cpp | 3 ++- src/policy/discount.h | 13 ++++++++++--- test/functional/feature_discount_ct.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/core_write.cpp b/src/core_write.cpp index d9fc2a7a59..d61b89900f 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -237,11 +237,12 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, entry.pushKV("version", static_cast(static_cast(tx.nVersion))); entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION)); entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR); + entry.pushKV("weight", GetTransactionWeight(tx)); // ELEMENTS: add discountvsize if (Params().GetAcceptDiscountCT()) { entry.pushKV("discountvsize", GetDiscountVirtualTransactionSize(tx)); + entry.pushKV("discountweight", GetDiscountTransactionWeight(tx)); } - entry.pushKV("weight", GetTransactionWeight(tx)); entry.pushKV("locktime", (int64_t)tx.nLockTime); UniValue vin{UniValue::VARR}; diff --git a/src/policy/discount.h b/src/policy/discount.h index e8c3ece6e0..1cfec410af 100644 --- a/src/policy/discount.h +++ b/src/policy/discount.h @@ -12,9 +12,9 @@ #include /** - * Calculate a smaller virtual size for discounted Confidential Transactions. + * Calculate a smaller weight for discounted Confidential Transactions. */ -static inline int64_t GetDiscountVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost = 0, unsigned int bytes_per_sig_op = 0) +static inline int64_t GetDiscountTransactionWeight(const CTransaction& tx, int64_t nSigOpCost = 0, unsigned int bytes_per_sig_op = 0) { int64_t size_bytes = ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION); int64_t sigop_bytes = nSigOpCost * bytes_per_sig_op; @@ -40,8 +40,15 @@ static inline int64_t GetDiscountVirtualTransactionSize(const CTransaction& tx, } } assert(weight > 0); + return weight; +} - size_t discountvsize = (weight + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR; +/** + * Calculate a smaller virtual size for discounted Confidential Transactions. + */ +static inline int64_t GetDiscountVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost = 0, unsigned int bytes_per_sig_op = 0) +{ + size_t discountvsize = (GetDiscountTransactionWeight(tx, nSigOpCost, bytes_per_sig_op) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR; assert(discountvsize > 0); return discountvsize; diff --git a/test/functional/feature_discount_ct.py b/test/functional/feature_discount_ct.py index b0b43361e6..85a341df5b 100755 --- a/test/functional/feature_discount_ct.py +++ b/test/functional/feature_discount_ct.py @@ -79,8 +79,10 @@ class CTTest(BitcoinTestFramework): assert_equal(len(vout), 3) assert_equal(tx['fee']['bitcoin'], Decimal('-0.00000326')) assert_equal(decoded['vsize'], 326) + assert_equal(decoded['weight'], 1302) self.generate(node0, 1) tx = node1.getrawtransaction(txid, True) + assert_equal(tx['discountweight'], 1302) assert_equal(tx['discountvsize'], 326) self.log.info("Send confidential tx to node 0") @@ -95,8 +97,10 @@ class CTTest(BitcoinTestFramework): assert_equal(len(vout), 3) assert_equal(tx['fee']['bitcoin'], Decimal('-0.00002575')) assert_equal(decoded['vsize'], 2575) + assert_equal(decoded['weight'], 10300) self.generate(node0, 1) tx = node1.getrawtransaction(txid, True) + assert_equal(tx['discountweight'], 1638) assert_equal(tx['discountvsize'], 410) # node1 has discountvsize self.log.info("Send explicit tx to node 1") @@ -111,8 +115,10 @@ class CTTest(BitcoinTestFramework): assert_equal(len(vout), 3) assert_equal(tx['fee']['bitcoin'], Decimal('-0.00000326')) assert_equal(decoded['vsize'], 326) + assert_equal(decoded['weight'], 1302) self.generate(node0, 1) tx = node1.getrawtransaction(txid, True) + assert_equal(tx['discountweight'], 1302) assert_equal(tx['discountvsize'], 326) self.log.info("Send confidential (undiscounted) tx to node 1") @@ -127,8 +133,10 @@ class CTTest(BitcoinTestFramework): assert_equal(len(vout), 3) assert_equal(tx['fee']['bitcoin'], Decimal('-0.00002575')) assert_equal(decoded['vsize'], 2575) + assert_equal(decoded['weight'], 10300) self.generate(node0, 1) tx = node1.getrawtransaction(txid, True) + assert_equal(tx['discountweight'], 1638) assert_equal(tx['discountvsize'], 410) # node1 has discountvsize self.log.info("Send confidential (discounted) tx to node 1") @@ -152,6 +160,8 @@ class CTTest(BitcoinTestFramework): else: assert_equal(decoded['fee'][bitcoin], Decimal('0.00000410')) assert_equal(decoded['vsize'], 2575) + assert_equal(decoded['weight'], 10300) + assert_equal(decoded['discountweight'], 1638) assert_equal(decoded['discountvsize'], 410) # node0 only has vsize @@ -180,7 +190,9 @@ class CTTest(BitcoinTestFramework): else: assert_equal(decoded['fee'][bitcoin], Decimal('0.00000041')) assert_equal(decoded['vsize'], 2575) + assert_equal(decoded['weight'], 10300) assert_equal(decoded['discountvsize'], 410) + assert_equal(decoded['discountweight'], 1638) # node0 only has vsize tx = node0.getrawtransaction(txid, True) assert_equal(tx['vsize'], 2575)