Skip to content
Open
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
14 changes: 13 additions & 1 deletion btsim/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,26 @@ pub(crate) struct BlockTemplate {

impl<'a> BlockTemplate {
pub(crate) fn mine(self, rewards_to: AddressId, sim: &'a mut Simulation) -> BlockHandle<'a> {
self.mine_with_reward(rewards_to, None, sim)
}

/// Mine a block, optionally overriding the coinbase output amount. `reward_override`
/// is used to seed wallets with arbitrary funding amounts during universe setup;
/// `None` pays the normal subsidy + fees.
pub(crate) fn mine_with_reward(
self,
rewards_to: AddressId,
reward_override: Option<Amount>,
sim: &'a mut Simulation,
) -> BlockHandle<'a> {
let parent_block = self.parent.with(sim);

let height = 1 + parent_block.info().height;
let subsidy = ChainParams::default().subsidy(height); // TODO make parameter of simulation

let fees = self.txs.iter().map(|tx| tx.with(sim).info().fee).sum();

let block_rewards = subsidy + fees;
let block_rewards = reward_override.unwrap_or(subsidy + fees);

let mut confirmed_txs = OrdSet::from(&self.txs);

Expand Down
10 changes: 8 additions & 2 deletions btsim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ mod transaction;
mod tx_contruction;
mod wallet;

/// Inclusive range (in satoshis) for the random amount funded to each wallet coinbase
/// during universe setup.
const WALLET_FUNDING_RANGE_SATS: std::ops::RangeInclusive<u64> = 10_000..=10_000_000;

#[derive(Debug, Clone)]
struct PrngFactory(Pcg64);

Expand Down Expand Up @@ -325,16 +329,18 @@ impl<'a> Simulation {
.map(|&id| id.with_mut(self).new_address())
.collect::<Vec<_>>();

// For now we just mine a coinbase transaction for each wallet
// For now we just mine a coinbase transaction for each wallet, funding each with a
// random amount instead of the full block subsidy so wallets start with varied UTXOs.
let mut i = 0;
for address in addresses.iter() {
for _ in 0..prng.random_range(5..10) {
let funding = Amount::from_sat(prng.random_range(WALLET_FUNDING_RANGE_SATS));
let _ = BroadcastSetHandleMut {
id: BroadcastSetId(i),
sim: self,
}
.construct_block_template(Weight::MAX_BLOCK)
.mine(*address, self);
.mine_with_reward(*address, Some(funding), self);

self.assert_invariants();
i += 1;
Expand Down
Loading