Account for malloc overhead

This commit is contained in:
Eirik Ogilvie-Wigley 2019-11-14 16:21:37 -07:00
parent 88dff18a09
commit ecce2c9672
4 changed files with 30 additions and 20 deletions

View File

@ -25,14 +25,6 @@ static inline size_t RecursiveDynamicUsage(const CTxOut& out) {
return RecursiveDynamicUsage(out.scriptPubKey);
}
// These constants are defined in the protocol § 7.1:
// https://zips.z.cash/protocol/protocol.pdf#txnencoding
#define OUTPUTDESCRIPTION_SIZE 948
#define SPENDDESCRIPTION_SIZE 384
static inline size_t JOINSPLIT_SIZE(int transactionVersion) {
return transactionVersion >= SAPLING_TX_VERSION ? 1698 : 1802;
}
static inline size_t RecursiveDynamicUsage(const CTransaction& tx) {
size_t mem = memusage::DynamicUsage(tx.vin) + memusage::DynamicUsage(tx.vout);
for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); it++) {
@ -41,9 +33,9 @@ static inline size_t RecursiveDynamicUsage(const CTransaction& tx) {
for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); it++) {
mem += RecursiveDynamicUsage(*it);
}
mem += tx.vJoinSplit.size() * JOINSPLIT_SIZE(tx.nVersion);
mem += tx.vShieldedOutput.size() * OUTPUTDESCRIPTION_SIZE;
mem += tx.vShieldedSpend.size() * SPENDDESCRIPTION_SIZE;
mem += memusage::DynamicUsage(tx.vJoinSplit);
mem += memusage::DynamicUsage(tx.vShieldedSpend);
mem += memusage::DynamicUsage(tx.vShieldedOutput);
return mem;
}
@ -55,9 +47,9 @@ static inline size_t RecursiveDynamicUsage(const CMutableTransaction& tx) {
for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); it++) {
mem += RecursiveDynamicUsage(*it);
}
mem += tx.vJoinSplit.size() * JOINSPLIT_SIZE(tx.nVersion);
mem += tx.vShieldedOutput.size() * OUTPUTDESCRIPTION_SIZE;
mem += tx.vShieldedSpend.size() * SPENDDESCRIPTION_SIZE;
mem += memusage::DynamicUsage(tx.vJoinSplit);
mem += memusage::DynamicUsage(tx.vShieldedSpend);
mem += memusage::DynamicUsage(tx.vShieldedOutput);
return mem;
}

View File

@ -27,6 +27,8 @@ TEST(RecursiveDynamicUsageTests, TestTransactionTransparent)
builder.AddTransparentOutput(taddr, 40000);
auto tx = builder.Build().GetTxOrThrow();
// 1 vin + 1 vout
// (96 + 128) + 64
EXPECT_EQ(288, RecursiveDynamicUsage(tx));
RegtestDeactivateSapling();
@ -39,7 +41,9 @@ TEST(RecursiveDynamicUsageTests, TestTransactionJoinSplit)
auto sproutSk = libzcash::SproutSpendingKey::random();
auto wtx = GetValidSproutReceive(*params, sproutSk, 25000, true);
EXPECT_EQ(2806, RecursiveDynamicUsage(wtx));
// 2 vin + 1 vJoinSplit + 1 vShieldedOutput
// 160 + 1856 + 976
EXPECT_EQ(2992, RecursiveDynamicUsage(wtx));
RegtestDeactivateSapling();
}
@ -56,7 +60,9 @@ TEST(RecursiveDynamicUsageTests, TestTransactionSaplingToSapling)
builder.AddSaplingOutput(sk.full_viewing_key().ovk, sk.default_address(), 5000, {});
auto tx = builder.Build().GetTxOrThrow();
EXPECT_EQ(2280, RecursiveDynamicUsage(tx));
// 1 vShieldedSpend + 2 vShieldedOutput
// 400 + 1920
EXPECT_EQ(2320, RecursiveDynamicUsage(tx));
RegtestDeactivateSapling();
}
@ -75,7 +81,9 @@ TEST(RecursiveDynamicUsageTests, TestTransactionTransparentToSapling)
builder.AddSaplingOutput(sk.full_viewing_key().ovk, sk.default_address(), 40000, {});
auto tx = builder.Build().GetTxOrThrow();
EXPECT_EQ(1172, RecursiveDynamicUsage(tx));
// 1 vin + 1 vShieldedOutput
// (96 + 128) + 976
EXPECT_EQ(1200, RecursiveDynamicUsage(tx));
RegtestDeactivateSapling();
}
@ -95,7 +103,9 @@ TEST(RecursiveDynamicUsageTests, TestTransactionSaplingToTransparent)
builder.AddTransparentOutput(taddr, 40000);
auto tx = builder.Build().GetTxOrThrow();
EXPECT_EQ(448, RecursiveDynamicUsage(tx));
// 1 vShieldedSpend + 1 vout
// 400 + 64
EXPECT_EQ(464, RecursiveDynamicUsage(tx));
RegtestDeactivateSapling();
}

View File

@ -156,8 +156,8 @@ TEST(MempoolLimitTests, WeightedTxInfoFromTx)
std::cerr << result.GetError() << std::endl;
}
WeightedTxInfo info = WeightedTxInfo::from(result.GetTxOrThrow(), 10000);
EXPECT_EQ(5124, info.txWeight.cost);
EXPECT_EQ(5124, info.txWeight.evictionWeight);
EXPECT_EQ(5168, info.txWeight.cost);
EXPECT_EQ(5168, info.txWeight.evictionWeight);
}
RegtestDeactivateSapling();

View File

@ -37,6 +37,14 @@ static_assert(SAPLING_TX_VERSION >= SAPLING_MIN_TX_VERSION,
static_assert(SAPLING_TX_VERSION <= SAPLING_MAX_TX_VERSION,
"Sapling tx version must not be higher than maximum");
// These constants are defined in the protocol § 7.1:
// https://zips.z.cash/protocol/protocol.pdf#txnencoding
#define OUTPUTDESCRIPTION_SIZE 948
#define SPENDDESCRIPTION_SIZE 384
static inline size_t JOINSPLIT_SIZE(int transactionVersion) {
return transactionVersion >= SAPLING_TX_VERSION ? 1698 : 1802;
}
/**
* A shielded input to a transaction. It contains data that describes a Spend transfer.
*/