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

Build wallet state asynchronously #3142

Merged
merged 19 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion src/inscriptions/inscription_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq)]
#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq, PartialOrd, Ord)]
pub struct InscriptionId {
pub txid: Txid,
pub index: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/runes/pile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy)]
pub struct Pile {
pub amount: u128,
pub divisibility: u8,
Expand Down
19 changes: 12 additions & 7 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,32 @@ pub(crate) enum Subcommand {

impl WalletCommand {
pub(crate) fn run(self, options: Options) -> SubcommandResult {
let wallet = Wallet {
name: self.name.clone(),
no_sync: self.no_sync,
options,
ord_url: self.server_url,
match self.subcommand {
Subcommand::Create(create) => return create.run(self.name, &options),
Subcommand::Restore(restore) => return restore.run(self.name, &options),
_ => {}
};

let wallet = Wallet::build(
self.name.clone(),
self.no_sync,
options.clone(),
self.server_url,
)?;

match self.subcommand {
Subcommand::Balance => balance::run(wallet),
Subcommand::Create(create) => create.run(wallet),
Subcommand::Dump => dump::run(wallet),
Subcommand::Etch(etch) => etch.run(wallet),
Subcommand::Inscribe(inscribe) => inscribe.run(wallet),
Subcommand::Inscriptions => inscriptions::run(wallet),
Subcommand::Receive => receive::run(wallet),
Subcommand::Restore(restore) => restore.run(wallet),
Subcommand::Sats(sats) => sats.run(wallet),
Subcommand::Send(send) => send.run(wallet),
Subcommand::Transactions(transactions) => transactions.run(wallet),
Subcommand::Outputs => outputs::run(wallet),
Subcommand::Cardinals => cardinals::run(wallet),
Subcommand::Create(_) | Subcommand::Restore(_) => unreachable!(),
}
}
}
12 changes: 6 additions & 6 deletions src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub struct Output {
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let unspent_outputs = wallet.get_unspent_outputs()?;
let unspent_outputs = wallet.utxos();

let inscription_outputs = wallet
.get_inscriptions()?
.inscriptions()
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<BTreeSet<OutPoint>>();
Expand All @@ -26,9 +26,9 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let mut runic = 0;

for (output, txout) in unspent_outputs {
let rune_balances = wallet.get_runes_balances_for_output(&output)?;
let rune_balances = wallet.get_runes_balances_for_output(output)?;

if inscription_outputs.contains(&output) {
if inscription_outputs.contains(output) {
ordinal += txout.value;
} else if !rune_balances.is_empty() {
for (spaced_rune, pile) in rune_balances {
Expand All @@ -43,8 +43,8 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
Ok(Some(Box::new(Output {
cardinal,
ordinal,
runes: wallet.has_rune_index()?.then_some(runes),
runic: wallet.has_rune_index()?.then_some(runic),
runes: wallet.has_rune_index().then_some(runes),
runic: wallet.has_rune_index().then_some(runic),
total: cardinal + ordinal + runic,
})))
}
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/cardinals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ pub struct CardinalUtxo {
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let unspent_outputs = wallet.get_unspent_outputs()?;
let unspent_outputs = wallet.utxos();

let inscribed_utxos = wallet
.get_inscriptions()?
.inscriptions()
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<BTreeSet<OutPoint>>();
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ pub(crate) struct Create {
}

impl Create {
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult {
pub(crate) fn run(self, name: String, options: &Options) -> SubcommandResult {
let mut entropy = [0; 16];
rand::thread_rng().fill_bytes(&mut entropy);

let mnemonic = Mnemonic::from_entropy(&entropy)?;

wallet.initialize(mnemonic.to_seed(&self.passphrase))?;
Wallet::initialize(name, options, mnemonic.to_seed(&self.passphrase))?;

Ok(Some(Box::new(Output {
mnemonic,
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
);

Ok(Some(Box::new(
wallet.bitcoin_client()?.list_descriptors(Some(true))?,
wallet.bitcoin_client().list_descriptors(Some(true))?,
)))
}
8 changes: 4 additions & 4 deletions src/subcommand/wallet/etch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub struct Output {
impl Etch {
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult {
ensure!(
wallet.has_rune_index()?,
wallet.has_rune_index(),
"`ord wallet etch` requires index created with `--index-runes` flag",
);

let SpacedRune { rune, spacers } = self.rune;

let bitcoin_client = wallet.bitcoin_client()?;
let bitcoin_client = wallet.bitcoin_client();

let count = bitcoin_client.get_block_count()?;

Expand Down Expand Up @@ -99,7 +99,7 @@ impl Etch {
};

let inscriptions = wallet
.get_inscriptions()?
.inscriptions()
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<Vec<OutPoint>>();
Expand All @@ -109,7 +109,7 @@ impl Etch {
}

let unsigned_transaction =
fund_raw_transaction(&bitcoin_client, self.fee_rate, &unfunded_transaction)?;
fund_raw_transaction(bitcoin_client, self.fee_rate, &unfunded_transaction)?;

let signed_transaction = bitcoin_client
.sign_raw_transaction_with_wallet(&unsigned_transaction, None, None)?
Expand Down
23 changes: 14 additions & 9 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ impl Inscribe {
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult {
let metadata = Inscribe::parse_metadata(self.cbor_metadata, self.json_metadata)?;

let utxos = wallet.get_unspent_outputs()?;
let utxos = wallet.utxos();

let mut locked_utxos = wallet.get_locked_outputs()?;
let mut locked_utxos = wallet.locked_utxos().clone();

let runic_utxos = wallet.get_runic_outputs()?;

Expand All @@ -91,7 +91,7 @@ impl Inscribe {

let satpoint = match (self.file, self.batch) {
(Some(file), None) => {
parent_info = wallet.get_parent_info(self.parent, &utxos)?;
parent_info = wallet.get_parent_info(self.parent)?;

postages = vec![self.postage.unwrap_or(TARGET_POSTAGE)];

Expand Down Expand Up @@ -123,33 +123,33 @@ impl Inscribe {
}];

if let Some(sat) = self.sat {
Some(wallet.find_sat_in_outputs(sat, &utxos)?)
Some(wallet.find_sat_in_outputs(sat)?)
} else {
self.satpoint
}
}
(None, Some(batch)) => {
let batchfile = Batchfile::load(&batch)?;

parent_info = wallet.get_parent_info(batchfile.parent, &utxos)?;
parent_info = wallet.get_parent_info(batchfile.parent)?;

(inscriptions, reveal_satpoints, postages, destinations) = batchfile.inscriptions(
&wallet,
&utxos,
utxos,
parent_info.as_ref().map(|info| info.tx_out.value),
self.compress,
)?;

locked_utxos.extend(
reveal_satpoints
.iter()
.map(|(satpoint, _)| satpoint.outpoint),
.map(|(satpoint, txout)| (satpoint.outpoint, txout.clone())),
);

mode = batchfile.mode;

if let Some(sat) = batchfile.sat {
Some(wallet.find_sat_in_outputs(sat, &utxos)?)
Some(wallet.find_sat_in_outputs(sat)?)
} else {
batchfile.satpoint
}
Expand All @@ -172,7 +172,12 @@ impl Inscribe {
reveal_satpoints,
satpoint,
}
.inscribe(&locked_utxos, runic_utxos, &utxos, &wallet)
.inscribe(
&locked_utxos.into_keys().collect(),
runic_utxos,
utxos,
&wallet,
)
}

fn parse_metadata(cbor: Option<PathBuf>, json: Option<PathBuf>) -> Result<Option<Vec<u8>>> {
Expand Down
12 changes: 4 additions & 8 deletions src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ pub struct Output {
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let unspent_outputs = wallet.get_unspent_outputs()?;

let inscriptions = wallet.get_inscriptions()?;

let explorer = match wallet.chain() {
Chain::Mainnet => "https://ordinals.com/inscription/",
Chain::Regtest => "http://localhost/inscription/",
Expand All @@ -22,12 +18,12 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {

let mut output = Vec::new();

for (location, inscriptions) in inscriptions {
if let Some(txout) = unspent_outputs.get(&location.outpoint) {
for (location, inscriptions) in wallet.inscriptions() {
if let Some(txout) = wallet.utxos().get(&location.outpoint) {
for inscription in inscriptions {
output.push(Output {
location,
inscription,
location: *location,
inscription: *inscription,
explorer: format!("{explorer}{inscription}"),
postage: txout.value,
})
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ pub struct Output {

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let mut outputs = Vec::new();
for (output, txout) in wallet.get_unspent_outputs()? {
for (output, txout) in wallet.utxos() {
outputs.push(Output {
output,
output: *output,
amount: txout.value,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Output {

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let address = wallet
.bitcoin_client()?
.bitcoin_client()
.get_new_address(None, Some(bitcoincore_rpc::json::AddressType::Bech32m))?;

Ok(Some(Box::new(Output { address })))
Expand Down
20 changes: 16 additions & 4 deletions src/subcommand/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ enum Source {
}

impl Restore {
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult {
ensure!(!wallet.exists()?, "wallet `{}` already exists", wallet.name);
pub(crate) fn run(self, name: String, options: &Options) -> SubcommandResult {
ensure!(
!options
.bitcoin_rpc_client(None)?
.list_wallet_dir()?
.iter()
.any(|wallet_name| wallet_name == &name),
"wallet `{}` already exists",
name
);

let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer)?;
Expand All @@ -28,11 +36,15 @@ impl Restore {
"descriptor does not take a passphrase"
);
let wallet_descriptors: ListDescriptorsResult = serde_json::from_str(&buffer)?;
wallet.initialize_from_descriptors(wallet_descriptors.descriptors)?;
Wallet::initialize_from_descriptors(name, options, wallet_descriptors.descriptors)?;
}
Source::Mnemonic => {
let mnemonic = Mnemonic::from_str(&buffer)?;
wallet.initialize(mnemonic.to_seed(self.passphrase.unwrap_or_default()))?;
Wallet::initialize(
name,
options,
mnemonic.to_seed(self.passphrase.unwrap_or_default()),
)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/sats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct OutputRare {
impl Sats {
pub(crate) fn run(&self, wallet: Wallet) -> SubcommandResult {
ensure!(
wallet.has_sat_index()?,
wallet.has_sat_index(),
"sats requires index created with `--index-sats` flag"
);

Expand Down
Loading
Loading