Skip to content

script: cache transaction weight per-transaction #1032

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

Merged
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
17 changes: 9 additions & 8 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,12 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
}
case OP_TXWEIGHT:
{
push8_le(stack, checker.GetTxWeight());
const PrecomputedTransactionData *cache = checker.GetPrecomputedTransactionData();
// Return error if the evaluation context is unavailable
// TODO: Handle accoding to MissingDataBehavior
if (!cache || !cache->m_bip341_taproot_ready)
return set_error(serror, SCRIPT_ERR_INTROSPECT_CONTEXT_UNAVAILABLE);
push8_le(stack, cache->m_tx_weight);
break;
}
default: assert(!"invalid opcode"); break;
Expand Down Expand Up @@ -2581,6 +2586,9 @@ void PrecomputedTransactionData::Init(const T& txTo, std::vector<CTxOut>&& spent
m_bip143_segwit_ready = true;
}
if (uses_bip341_taproot) {
// line copied from GetTransactionWeight() in src/consensus/validation.h
// (we cannot directly use that function for type reasons)
m_tx_weight = ::GetSerializeSize(txTo, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txTo, PROTOCOL_VERSION);
m_outpoints_flag_single_hash = GetOutpointFlagsSHA256(txTo);
m_spent_asset_amounts_single_hash = GetSpentAssetsAmountsSHA256(m_spent_outputs);
m_issuance_rangeproofs_single_hash = GetIssuanceRangeproofsSHA256(txTo);
Expand Down Expand Up @@ -2993,13 +3001,6 @@ const std::vector<CTxOut>* GenericTransactionSignatureChecker<T>::GetTxvOut() co
return &(txTo->vout);
}

template <class T>
uint64_t GenericTransactionSignatureChecker<T>::GetTxWeight() const
{
// line copied from GetTransactionWeight() in src/consensus/validation.h
return ::GetSerializeSize(*txTo, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(*txTo, PROTOCOL_VERSION);
}

template <class T>
const PrecomputedTransactionData* GenericTransactionSignatureChecker<T>::GetPrecomputedTransactionData() const
{
Expand Down
7 changes: 1 addition & 6 deletions src/script/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ struct PrecomputedTransactionData
uint256 m_spent_amounts_single_hash;
uint256 m_spent_scripts_single_hash;
// Elements
uint64_t m_tx_weight;
uint256 m_outpoints_flag_single_hash;
uint256 m_spent_asset_amounts_single_hash;
uint256 m_issuances_single_hash;
Expand Down Expand Up @@ -293,11 +294,6 @@ class BaseSignatureChecker
return 0;
}

virtual uint64_t GetTxWeight() const
{
return 0;
}

virtual const PrecomputedTransactionData* GetPrecomputedTransactionData() const
{
return nullptr;
Expand Down Expand Up @@ -335,7 +331,6 @@ class GenericTransactionSignatureChecker : public BaseSignatureChecker
const std::vector<CTxOut>* GetTxvOut() const override;
uint32_t GetLockTime() const override;
int32_t GetTxVersion() const override;
uint64_t GetTxWeight() const override;

const PrecomputedTransactionData* GetPrecomputedTransactionData() const override;
uint32_t GetnIn() const override;
Expand Down