Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix size cache for size(true) when tx is not segregated. #1606

Merged
merged 4 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/bitcoin/system/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class BC_API block

void assign_data(reader& source, bool witness) NOEXCEPT;
static block from_data(reader& source, bool witness) NOEXCEPT;
static sizes serialized_size(const chain::transaction_cptrs& txs) NOEXCEPT;
static sizes serialized_size(const transaction_cptrs& txs) NOEXCEPT;

// context free
hash_digest generate_merkle_root(bool witness) const NOEXCEPT;
Expand Down
11 changes: 6 additions & 5 deletions include/bitcoin/system/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class BC_API transaction

/// Native properties.
bool is_valid() const NOEXCEPT;
size_t spends() const NOEXCEPT;
size_t inputs() const NOEXCEPT;
size_t outputs() const NOEXCEPT;
uint32_t version() const NOEXCEPT;
Expand Down Expand Up @@ -166,8 +167,8 @@ class BC_API transaction
code confirm(const context& ctx) const NOEXCEPT;

protected:
transaction(uint32_t version, const chain::inputs_cptr& inputs,
const chain::outputs_cptr& outputs, uint32_t locktime, bool segregated,
transaction(uint32_t version, const inputs_cptr& inputs,
const outputs_cptr& outputs, uint32_t locktime, bool segregated,
bool valid) NOEXCEPT;

/// Guard (context free).
Expand Down Expand Up @@ -236,9 +237,9 @@ class BC_API transaction
} sighash_cache;

static bool segregated(const chain::inputs& inputs) NOEXCEPT;
static bool segregated(const chain::input_cptrs& inputs) NOEXCEPT;
static sizes serialized_size(const chain::input_cptrs& inputs,
const chain::output_cptrs& outputs, bool segregated) NOEXCEPT;
static bool segregated(const input_cptrs& inputs) NOEXCEPT;
static sizes serialized_size(const input_cptrs& inputs,
const output_cptrs& outputs, bool segregated) NOEXCEPT;

void assign_data(reader& source, bool witness) NOEXCEPT;

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/system/chain/witness.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class BC_API witness
const chunk_cptrs& stack() const NOEXCEPT;

/// Computed properties.
/// serialized_size(true) returns one for an empty witness stack.
size_t serialized_size(bool prefix) const NOEXCEPT;

/// Utilities.
Expand Down
9 changes: 4 additions & 5 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ void block::set_hashes(const data_chunk& data) NOEXCEPT
}

// static/private
block::sizes block::serialized_size(
const chain::transaction_cptrs& txs) NOEXCEPT
block::sizes block::serialized_size(const transaction_cptrs& txs) NOEXCEPT
{
sizes size{};
std::for_each(txs.begin(), txs.end(), [&](const auto& tx) NOEXCEPT
Expand All @@ -342,11 +341,11 @@ block::sizes block::serialized_size(
size.witnessed = ceilinged_add(size.witnessed, tx->serialized_size(true));
});

const auto common_size = ceilinged_add(header::serialized_size(),
const auto base_size = ceilinged_add(header::serialized_size(),
variable_size(txs.size()));

const auto nominal_size = ceilinged_add(common_size, size.nominal);
const auto witnessed_size = ceilinged_add(common_size, size.witnessed);
const auto nominal_size = ceilinged_add(base_size, size.nominal);
const auto witnessed_size = ceilinged_add(base_size, size.witnessed);

return { nominal_size, witnessed_size };
}
Expand Down
5 changes: 5 additions & 0 deletions src/chain/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,14 @@ input::sizes input::serialized_size(const chain::script& script,
const auto nominal_size = ceilinged_add(const_size,
script.serialized_size(true));

// The input has no way to determine if its tx is segregated, as an empty
// witness applies to segregated tx as a one byte (zero) serialization.
// This determination is therefore left to the transaction, and witness
// retains both serialization options from construction.
const auto witnessed_size = ceilinged_add(nominal_size,
witness.serialized_size(true));

// Values are the same for non-segregated transactions.
return { nominal_size, witnessed_size };
}

Expand Down
47 changes: 19 additions & 28 deletions src/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ transaction::transaction(uint32_t version, const chain::inputs& inputs,
{
}

transaction::transaction(uint32_t version, const chain::inputs_cptr& inputs,
const chain::outputs_cptr& outputs, uint32_t locktime) NOEXCEPT
transaction::transaction(uint32_t version, const inputs_cptr& inputs,
const outputs_cptr& outputs, uint32_t locktime) NOEXCEPT
: transaction(version, inputs, outputs, locktime, segregated(*inputs), true)
{
}
Expand Down Expand Up @@ -284,31 +284,17 @@ void transaction::to_data(writer& sink, bool witness) const NOEXCEPT
}

// static/private
transaction::sizes transaction::serialized_size(
const chain::input_cptrs& inputs,
const chain::output_cptrs& outputs, bool segregated) NOEXCEPT
transaction::sizes transaction::serialized_size(const input_cptrs& inputs,
const output_cptrs& outputs, bool segregated) NOEXCEPT
{
sizes size{ zero, zero };

// Keep the condition outside of the loop.
if (segregated)
std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
{
std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
{
size.nominal = ceilinged_add(size.nominal, in->nominal_size());
size.nominal = ceilinged_add(size.nominal, in->nominal_size());
if (segregated)
size.witnessed = ceilinged_add(size.witnessed, in->witnessed_size());
});
}
else
{
// Witness must be zeroed because witnesses have nonzero size when they
// are zero-valued, so they can be archived easily. Also it would be
// wasteful to to count mutiple zero sizes, so exclude them here.
std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
{
size.nominal = ceilinged_add(size.nominal, in->nominal_size());
});
}
});

const auto outs = [](size_t total, const auto& output) NOEXCEPT
{
Expand All @@ -317,20 +303,20 @@ transaction::sizes transaction::serialized_size(

constexpr auto base_const_size = ceilinged_add(sizeof(version_),
sizeof(locktime_));

constexpr auto witness_const_size = ceilinged_add(sizeof(witness_marker),
sizeof(witness_enabled));

const auto base_size = ceilinged_add(ceilinged_add(ceilinged_add(
base_const_size, variable_size(inputs.size())),
variable_size(outputs.size())),
std::accumulate(outputs.begin(), outputs.end(), zero, outs));

const auto nominal_size = ceilinged_add(base_size, size.nominal);
const auto witnessed_size = ceilinged_add(ceilinged_add(base_size,
witness_const_size),
size.witnessed);

// witnessed_size is nominal_size for non-segregated transactions.
const auto witnessed_size = segregated ? ceilinged_add(ceilinged_add(
base_size, witness_const_size), size.witnessed) : nominal_size;

// Values are the same for non-segregated transactions.
return { nominal_size, witnessed_size };
}

Expand All @@ -349,6 +335,11 @@ bool transaction::is_valid() const NOEXCEPT
return valid_;
}

size_t transaction::spends() const NOEXCEPT
{
return is_coinbase() ? zero : inputs_->size();
}

size_t transaction::inputs() const NOEXCEPT
{
return inputs_->size();
Expand Down Expand Up @@ -983,7 +974,7 @@ bool transaction::segregated(const chain::inputs& inputs) NOEXCEPT
}

// static/private
bool transaction::segregated(const chain::input_cptrs& inputs) NOEXCEPT
bool transaction::segregated(const input_cptrs& inputs) NOEXCEPT
{
const auto witnessed = [](const auto& input) NOEXCEPT
{
Expand Down
60 changes: 33 additions & 27 deletions test/config/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ static const std::string encoded_genesis_block =
"4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac"
"00000000";

static const config::block genesis_config()
{
return { encoded_genesis_block };
};

static const config::block genesis_config{ encoded_genesis_block };
static const chain::block genesis_chain{ genesis_config };
static const chain::block genesis_chain()
{
return { genesis_config() };
};

BOOST_AUTO_TEST_CASE(block__construct__default)
{
Expand All @@ -57,28 +63,28 @@ BOOST_AUTO_TEST_CASE(block__construct__default)

BOOST_AUTO_TEST_CASE(block__construct__copy__expected)
{
const block block(genesis_config);
BOOST_REQUIRE_EQUAL(block, genesis_config);
const block block(genesis_config());
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__construct__move__expected)
{
auto genesis = genesis_config;
auto genesis = genesis_config();
const block block(std::move(genesis));
BOOST_REQUIRE_EQUAL(block, genesis_config);
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__construct__chain_copy__expected)
{
const block block(genesis_chain);
BOOST_REQUIRE_EQUAL(block, genesis_config);
const block block(genesis_chain());
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__construct__chain_move__expected)
{
chain::block genesis = genesis_config;
chain::block genesis = genesis_config();
const block block(std::move(genesis));
BOOST_REQUIRE_EQUAL(block, genesis_config);
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__construct__string__expected)
Expand All @@ -90,61 +96,61 @@ BOOST_AUTO_TEST_CASE(block__construct__string__expected)
BOOST_AUTO_TEST_CASE(block__assign__copy__expected)
{
block block{};
block = genesis_config;
BOOST_REQUIRE_EQUAL(block, genesis_config);
block = genesis_config();
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__assign__move__expected)
{
auto genesis = genesis_config;
auto genesis = genesis_config();
block block{};
block = std::move(genesis);
BOOST_REQUIRE_EQUAL(block, genesis_config);
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__assign__chain_copy__expected)
{
block block{};
block = genesis_chain;
BOOST_REQUIRE_EQUAL(block, genesis_config);
block = genesis_chain();
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__assign__chain_move__expected)
{
auto genesis = genesis_chain;
auto genesis = genesis_chain();
block block{};
block = std::move(genesis);
BOOST_REQUIRE_EQUAL(block, genesis_config);
BOOST_REQUIRE_EQUAL(block, genesis_config());
}

BOOST_AUTO_TEST_CASE(block__equality__equal__true)
{
const block block{ genesis_config };
BOOST_REQUIRE(block == genesis_config);
const block block{ genesis_config() };
BOOST_REQUIRE(block == genesis_config());
}

BOOST_AUTO_TEST_CASE(block__equality__not_equal__false)
{
const block block{};
BOOST_REQUIRE(!(block == genesis_config));
BOOST_REQUIRE(!(block == genesis_config()));
}

BOOST_AUTO_TEST_CASE(block__inequality__equal__false)
{
const block block{ genesis_config };
BOOST_REQUIRE(!(block != genesis_config));
const block block{ genesis_config() };
BOOST_REQUIRE(!(block != genesis_config()));
}

BOOST_AUTO_TEST_CASE(block__inequality__not_equal__true)
{
const block block{};
BOOST_REQUIRE(block != genesis_config);
BOOST_REQUIRE(block != genesis_config());
}

BOOST_AUTO_TEST_CASE(block__equality__chain__expected)
{
const block block{};
BOOST_REQUIRE(block != genesis_chain);
BOOST_REQUIRE(block != genesis_chain());
}

BOOST_AUTO_TEST_CASE(block__istream__populated__expected)
Expand All @@ -165,13 +171,13 @@ BOOST_AUTO_TEST_CASE(block__ostream__empty__expected)
BOOST_AUTO_TEST_CASE(block__ostream__populated__expected)
{
std::stringstream serialized;
serialized << genesis_config;
serialized << genesis_config();
BOOST_REQUIRE_EQUAL(serialized.str(), encoded_genesis_block);
}

BOOST_AUTO_TEST_CASE(block__ostream__boost_lexical_cast__expected)
{
const auto serialized = boost::lexical_cast<std::string>(genesis_config);
const auto serialized = boost::lexical_cast<std::string>(genesis_config());
BOOST_REQUIRE_EQUAL(serialized, encoded_genesis_block);
}

Expand Down
Loading