Skip to content

Commit 65a2992

Browse files
committed
State: refactor tests
1 parent b1e2185 commit 65a2992

4 files changed

Lines changed: 187 additions & 224 deletions

File tree

lib/state/mod.rs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -683,22 +683,23 @@ impl Watchable<()> for State {
683683
}
684684

685685
#[cfg(test)]
686-
mod tests {
686+
mod test {
687687
use ed25519_dalek::SigningKey;
688688

689689
use crate::{
690690
authorization,
691691
state::{Error, State, error},
692692
types::{
693-
Address, AuthorizedTransaction, BitName, BitcoinOutputContent,
694-
FilledOutput, FilledOutputContent, FilledTransaction, Hash,
695-
InPoint, MutableBitNameData, OutPoint, OutPointKey, Output,
696-
OutputContent, SpentOutput, Transaction, TxData, Txid,
697-
VerifyingKey,
693+
Address, AuthorizedTransaction, BitName, FilledOutput,
694+
FilledOutputContent, FilledTransaction, Hash, InPoint,
695+
MutableBitNameData, OutPoint, OutPointKey, Output, OutputContent,
696+
SpentOutput, Transaction, TxData, Txid, VerifyingKey,
698697
},
699698
};
700699

701-
fn temp_env_path(test_name: &str) -> anyhow::Result<std::path::PathBuf> {
700+
pub fn temp_env_path(
701+
test_name: &str,
702+
) -> anyhow::Result<std::path::PathBuf> {
702703
let mut path = std::env::temp_dir();
703704
let nanos = std::time::SystemTime::now()
704705
.duration_since(std::time::UNIX_EPOCH)?
@@ -711,39 +712,42 @@ mod tests {
711712
}
712713

713714
// open a fresh state-backed env in a unique temp dir
714-
fn temp_env(test_name: &str) -> anyhow::Result<sneed::Env> {
715+
pub fn temp_env(test_name: &str) -> anyhow::Result<sneed::Env> {
715716
let path = temp_env_path(test_name)?;
716717
std::fs::create_dir_all(&path)?;
717718
let mut opts = heed::EnvOpenOptions::new();
718-
opts.map_size(16 * 1024 * 1024).max_dbs(State::NUM_DBS);
719+
opts.map_size(64 * 1024 * 1024).max_dbs(State::NUM_DBS);
719720
let res = unsafe { sneed::Env::open(&opts, &path) }?;
720721
Ok(res)
721722
}
722723

723-
fn fresh_state(test_name: &str) -> anyhow::Result<(sneed::Env, State)> {
724+
pub fn fresh_state(test_name: &str) -> anyhow::Result<(sneed::Env, State)> {
724725
let env = temp_env(test_name)?;
725726
let state = State::new(&env)?;
726727
Ok((env, state))
727728
}
728729

730+
/// Create a bitcoin filled output
731+
pub fn bitcoin_filled_output(address: Address, sats: u64) -> FilledOutput {
732+
FilledOutput::new_bitcoin_value(
733+
address,
734+
bitcoin::Amount::from_sat(sats),
735+
)
736+
}
737+
729738
/// Fund `address` with a single bitcoin UTXO of `value` sats, returning its
730739
/// outpoint.
731740
fn fund(
732741
env: &sneed::Env,
733742
state: &State,
734743
address: Address,
735-
value: u64,
744+
value_sats: u64,
736745
) -> OutPoint {
737746
let outpoint = OutPoint::Regular {
738747
txid: Default::default(),
739748
vout: 0,
740749
};
741-
let output = FilledOutput::new(
742-
address,
743-
FilledOutputContent::Bitcoin(BitcoinOutputContent(
744-
bitcoin::Amount::from_sat(value),
745-
)),
746-
);
750+
let output = bitcoin_filled_output(address, value_sats);
747751
let mut rwtxn = env.write_txn().unwrap();
748752
state
749753
.utxos
@@ -802,12 +806,7 @@ mod tests {
802806

803807
let transaction = Transaction::new(
804808
vec![outpoint],
805-
vec![Output::new(
806-
address,
807-
OutputContent::Bitcoin(BitcoinOutputContent(
808-
bitcoin::Amount::from_sat(900),
809-
)),
810-
)],
809+
vec![bitcoin_filled_output(address, 900).into()],
811810
);
812811

813812
// The attack: spend the input while providing no authorization for it.
@@ -885,13 +884,6 @@ mod tests {
885884

886885
use bitcoin::hashes::Hash as _;
887886

888-
let value_output = |sats: u64| FilledOutput {
889-
address: Address::ALL_ZEROS,
890-
content: FilledOutputContent::Bitcoin(BitcoinOutputContent(
891-
bitcoin::Amount::from_sat(sats),
892-
)),
893-
memo: Vec::new(),
894-
};
895887
let (env, state) = fresh_state("sidechain-wealth")?;
896888
{
897889
let mut rwtxn = env.write_txn()?;
@@ -906,7 +898,7 @@ mod tests {
906898
state.utxos.put(
907899
&mut rwtxn,
908900
&OutPointKey::from(&deposit_utxo_op),
909-
&value_output(50),
901+
&bitcoin_filled_output(Address::ALL_ZEROS, 50),
910902
)?;
911903

912904
// Two spent DEPOSIT STXOs: 100 + 100 sats.
@@ -916,7 +908,7 @@ mod tests {
916908
vout: 0,
917909
});
918910
let stxo = SpentOutput {
919-
output: value_output(sats),
911+
output: bitcoin_filled_output(Address::ALL_ZEROS, sats),
920912
inpoint: InPoint::Regular {
921913
txid: [i; 32].into(),
922914
vin: 0,
@@ -934,7 +926,7 @@ mod tests {
934926
vout: 0,
935927
};
936928
let stxo = SpentOutput {
937-
output: value_output(sats),
929+
output: bitcoin_filled_output(Address::ALL_ZEROS, sats),
938930
inpoint: InPoint::Withdrawal {
939931
m6id: crate::types::M6id(
940932
bitcoin::Txid::from_byte_array([i; 32]),

0 commit comments

Comments
 (0)