feat: add discountweight to TxToUniv

This commit is contained in:
Byron Hambly 2024-09-20 14:39:13 +02:00
parent 99ca072a94
commit d0f714638a
No known key found for this signature in database
GPG key ID: DE8F6EA20A661697
3 changed files with 24 additions and 4 deletions

View file

@ -237,11 +237,12 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
entry.pushKV("version", static_cast<int64_t>(static_cast<uint32_t>(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};

View file

@ -12,9 +12,9 @@
#include <version.h>
/**
* 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;

View file

@ -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)