Skip to content

Commit

Permalink
refactor(TransactionDetails): Add and use nop-tx constructor
Browse files Browse the repository at this point in the history
Nop transactions ("nop-tx"s) have no effect on balances: they have
zero inputs, zero outputs, no coinbase, and zero fee. They are useful
for setting the merge-bit when the mempool is empty, so that a
block-transaction can be mined.

Co-authored-by: Alan Szepieniec <[email protected]>
  • Loading branch information
Sword-Smith and aszepieniec committed Jan 3, 2025
1 parent 763f4ce commit 5faac40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use composer_parameters::ComposerParameters;
use difficulty_control::Difficulty;
use futures::channel::oneshot;
use num_traits::CheckedSub;
use num_traits::Zero;
use primitive_witness::PrimitiveWitness;
use rand::rngs::StdRng;
use rand::Rng;
Expand All @@ -35,7 +34,6 @@ use crate::models::blockchain::block::difficulty_control::difficulty_control;
use crate::models::blockchain::block::*;
use crate::models::blockchain::transaction::validity::single_proof::SingleProof;
use crate::models::blockchain::transaction::*;
use crate::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
use crate::models::channel::*;
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::TritonVmProofJobOptions;
Expand All @@ -44,12 +42,10 @@ use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::shared::SIZE_20MB_IN_BYTES;
use crate::models::state::transaction_details::TransactionDetails;
use crate::models::state::tx_proving_capability::TxProvingCapability;
use crate::models::state::wallet::address::ReceivingAddress;
use crate::models::state::wallet::expected_utxo::ExpectedUtxo;
use crate::models::state::wallet::expected_utxo::UtxoNotifier;
use crate::models::state::wallet::transaction_output::TxOutput;
use crate::models::state::wallet::transaction_output::TxOutputList;
use crate::models::state::wallet::utxo_notification::UtxoNotifyMethod;
use crate::models::state::GlobalState;
use crate::models::state::GlobalStateLock;
use crate::prelude::twenty_first;
Expand Down Expand Up @@ -467,34 +463,27 @@ pub(crate) async fn create_block_transaction_stateless(
)
.await?;

let mut rng = StdRng::from_seed(shuffle_seed);

let mut block_transaction = coinbase_transaction;
if selected_mempool_txs.is_empty() {
// create the nop-gobbler and merge into the coinbase transaction to
// set the merge bit to allow the tx to be included in a block.
let nop_gobbler = TransactionDetails::fee_gobbler(
NeptuneCoins::zero(),
rng.gen(),
predecessor_block.mutator_set_accumulator_after(),
timestamp,
UtxoNotifyMethod::None,
);
let nop_gobbler = PrimitiveWitness::from_transaction_details(nop_gobbler);
// create the nop-tx and merge into the coinbase transaction to set the
// merge bit to allow the tx to be included in a block.
let nop =
TransactionDetails::nop(predecessor_block.mutator_set_accumulator_after(), timestamp);
let nop = PrimitiveWitness::from_transaction_details(nop);
let proof_job_options = TritonVmProofJobOptions {
job_priority: TritonVmJobPriority::High,
job_settings: Default::default(),
};
let nop_gobbler_proof =
SingleProof::produce(&nop_gobbler, vm_job_queue, proof_job_options).await?;
let nop_gobbler = Transaction {
kernel: nop_gobbler.kernel,
proof: TransactionProof::SingleProof(nop_gobbler_proof),
let nop_proof = SingleProof::produce(&nop, vm_job_queue, proof_job_options).await?;
let nop = Transaction {
kernel: nop.kernel,
proof: TransactionProof::SingleProof(nop_proof),
};

selected_mempool_txs = vec![nop_gobbler];
selected_mempool_txs = vec![nop];
}

let mut rng = StdRng::from_seed(shuffle_seed);
let num_merges = selected_mempool_txs.len();
for (i, tx_to_include) in selected_mempool_txs.into_iter().enumerate() {
info!("Merging transaction {} / {}", i + 1, num_merges);
Expand Down
12 changes: 12 additions & 0 deletions src/models/state/transaction_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ pub(crate) struct TransactionDetails {
}

impl TransactionDetails {
/// Create (`TransactionDetails` for) a nop-transaction, with no inputs and
/// no outputs. Can be used if a merge bit needs to be flipped.
pub(crate) fn nop(mutator_set_accumulator: MutatorSetAccumulator, now: Timestamp) -> Self {
Self::fee_gobbler(
NeptuneCoins::zero(),
Digest::default(),
mutator_set_accumulator,
now,
UtxoNotifyMethod::None,
)
}

/// Create (`TransactionDetails` for) a new fee-gobbler transaction.
///
/// The produced transaction has no inputs, sets a negative fee, and
Expand Down

0 comments on commit 5faac40

Please sign in to comment.