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

Add dry run flag #1265

Merged
merged 5 commits into from
Jan 17, 2023
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
1 change: 1 addition & 0 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl Preview {
file,
no_backup: true,
satpoint: None,
dry_run: false,
},
)),
}
Expand Down
67 changes: 48 additions & 19 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct Output {
commit: Txid,
inscription: InscriptionId,
reveal: Txid,
fees: u64,
}

#[derive(Debug, Parser)]
Expand All @@ -37,6 +38,8 @@ pub(crate) struct Inscribe {
pub(crate) file: PathBuf,
#[clap(long, help = "Do not back up recovery key.")]
pub(crate) no_backup: bool,
#[clap(long, help = "Don't sign or broadcast transactions.")]
pub(crate) dry_run: bool,
}

impl Inscribe {
Expand All @@ -48,7 +51,7 @@ impl Inscribe {
let index = Index::open(&options)?;
index.update()?;

let utxos = get_unspent_outputs(&options)?;
let mut utxos = get_unspent_outputs(&options)?;

let inscriptions = index.get_inscriptions(None)?;

Expand All @@ -62,37 +65,63 @@ impl Inscribe {
inscription,
inscriptions,
options.chain().network(),
utxos,
utxos.clone(),
commit_tx_change,
reveal_tx_destination,
self.fee_rate,
)?;

if !self.no_backup {
Inscribe::backup_recovery_key(&client, recovery_key_pair, options.chain().network())?;
}
utxos.insert(
reveal_tx.input[0].previous_output,
Amount::from_sat(unsigned_commit_tx.output[0].value),
);

let fees =
Self::calculate_fee(&unsigned_commit_tx, &utxos) + Self::calculate_fee(&reveal_tx, &utxos);

let signed_raw_commit_tx = client
.sign_raw_transaction_with_wallet(&unsigned_commit_tx, None, None)?
.hex;
if self.dry_run {
print_json(Output {
commit: unsigned_commit_tx.txid(),
reveal: reveal_tx.txid(),
inscription: reveal_tx.txid().into(),
fees,
})?;
} else {
if !self.no_backup {
Inscribe::backup_recovery_key(&client, recovery_key_pair, options.chain().network())?;
}

let commit = client
.send_raw_transaction(&signed_raw_commit_tx)
.context("Failed to send commit transaction")?;
let signed_raw_commit_tx = client
.sign_raw_transaction_with_wallet(&unsigned_commit_tx, None, None)?
.hex;

let reveal = client
.send_raw_transaction(&reveal_tx)
.context("Failed to send reveal transaction")?;
let commit = client
.send_raw_transaction(&signed_raw_commit_tx)
.context("Failed to send commit transaction")?;

print_json(Output {
commit,
reveal,
inscription: reveal.into(),
})?;
let reveal = client
.send_raw_transaction(&reveal_tx)
.context("Failed to send reveal transaction")?;

print_json(Output {
commit,
reveal,
inscription: reveal.into(),
fees,
})?;
};

Ok(())
}

fn calculate_fee(tx: &Transaction, utxos: &BTreeMap<OutPoint, Amount>) -> u64 {
tx.input
.iter()
.map(|txin| utxos.get(&txin.previous_output).unwrap().to_sat())
.sum::<u64>()
- tx.output.iter().map(|txout| txout.value).sum::<u64>()
}

fn create_inscription_transactions(
satpoint: Option<SatPoint>,
inscription: Inscription,
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct Inscribe {
commit: Txid,
inscription: String,
reveal: Txid,
fees: u64,
}

fn inscribe(rpc_server: &test_bitcoincore_rpc::Handle) -> Inscribe {
Expand Down
43 changes: 43 additions & 0 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,46 @@ fn inscribe_with_wallet_named_foo() {
.rpc_server(&rpc_server)
.output::<Inscribe>();
}

#[test]
fn inscribe_with_dry_run_flag() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);
rpc_server.mine_blocks(1);

CommandBuilder::new("wallet inscribe --dry-run degenerate.png")
.write("degenerate.png", [1; 520])
.rpc_server(&rpc_server)
.output::<Inscribe>();

assert!(rpc_server.mempool().is_empty());

CommandBuilder::new("wallet inscribe degenerate.png")
.write("degenerate.png", [1; 520])
.rpc_server(&rpc_server)
.output::<Inscribe>();

assert_eq!(rpc_server.mempool().len(), 2);
}

#[test]
fn inscribe_with_dry_run_flag_fees_inscrease() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);
rpc_server.mine_blocks(1);

let total_fee_dry_run = CommandBuilder::new("wallet inscribe --dry-run degenerate.png")
.write("degenerate.png", [1; 520])
.rpc_server(&rpc_server)
.output::<Inscribe>()
.fees;

let total_fee_normal =
CommandBuilder::new("wallet inscribe --dry-run degenerate.png --fee-rate 1.1")
.write("degenerate.png", [1; 520])
.rpc_server(&rpc_server)
.output::<Inscribe>()
.fees;

assert!(total_fee_dry_run < total_fee_normal);
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}