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

Make all wallet commands load wallet #1524

Merged
merged 9 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 28 additions & 3 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
updater::Updater,
},
super::*,
crate::wallet::Wallet,
bitcoin::BlockHeader,
bitcoincore_rpc::{json::GetBlockHeaderResult, Auth, Client},
chrono::SubsecRound,
Expand Down Expand Up @@ -242,7 +243,7 @@ impl Index {
})
}

pub(crate) fn get_unspent_outputs(&self) -> Result<BTreeMap<OutPoint, Amount>> {
pub(crate) fn get_unspent_outputs(&self, _wallet: Wallet) -> Result<BTreeMap<OutPoint, Amount>> {
let mut utxos = BTreeMap::new();
utxos.extend(
self
Expand Down Expand Up @@ -285,6 +286,21 @@ impl Index {
Ok(utxos)
}

pub(crate) fn get_unspent_output_ranges(
&self,
wallet: Wallet,
) -> Result<Vec<(OutPoint, Vec<(u64, u64)>)>> {
self
.get_unspent_outputs(wallet)?
.into_keys()
.map(|outpoint| match self.list(outpoint)? {
Some(List::Unspent(sat_ranges)) => Ok((outpoint, sat_ranges)),
Some(List::Spent) => bail!("output {outpoint} in wallet but is spent according to index"),
None => bail!("index has not seen {outpoint}"),
})
.collect()
}

pub(crate) fn has_sat_index(&self) -> Result<bool> {
match self.begin_read()?.0.open_table(OUTPOINT_TO_SAT_RANGES) {
Ok(_) => Ok(true),
Expand Down Expand Up @@ -900,7 +916,10 @@ mod tests {
}

fn try_build(self) -> Result<Context> {
let rpc_server = test_bitcoincore_rpc::spawn();
let rpc_server = test_bitcoincore_rpc::builder()
.network(Network::Regtest)
.descriptors(vec!["tr()".into(), "tr()".into()])
.build();

let tempdir = self.tempdir.unwrap_or_else(|| TempDir::new().unwrap());
let cookie_file = tempdir.path().join("cookie");
Expand All @@ -922,6 +941,7 @@ mod tests {
index.update().unwrap();

Ok(Context {
options,
rpc_server,
tempdir,
index,
Expand All @@ -945,6 +965,7 @@ mod tests {
}

struct Context {
options: Options,
rpc_server: test_bitcoincore_rpc::Handle,
#[allow(unused)]
tempdir: TempDir,
Expand Down Expand Up @@ -2101,7 +2122,11 @@ mod tests {
for context in Context::configurations() {
context.rpc_server.mine_blocks(1);
assert_regex_match!(
context.index.get_unspent_outputs().unwrap_err().to_string(),
context
.index
.get_unspent_outputs(Wallet::load(&context.options).unwrap())
.unwrap_err()
.to_string(),
r"output in Bitcoin Core wallet but not in ord index: [[:xdigit:]]{64}:\d+"
);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ mod sat_point;
pub mod subcommand;
mod tally;
mod templates;
mod wallet;

type Result<T = (), E = Error> = std::result::Result<T, E>;

Expand Down
12 changes: 0 additions & 12 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ impl Wallet {
}
}

fn get_unspent_output_ranges(index: &Index) -> Result<Vec<(OutPoint, Vec<(u64, u64)>)>> {
index
.get_unspent_outputs()?
.into_keys()
.map(|outpoint| match index.list(outpoint)? {
Some(List::Unspent(sat_ranges)) => Ok((outpoint, sat_ranges)),
Some(List::Spent) => bail!("output {outpoint} in wallet but is spent according to index"),
None => bail!("index has not seen {outpoint}"),
})
.collect()
}

fn get_change_address(client: &Client) -> Result<Address> {
client
.call("getrawchangeaddress", &["bech32m".into()])
Expand Down
5 changes: 2 additions & 3 deletions src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use std::collections::BTreeSet;
use {super::*, crate::wallet::Wallet, std::collections::BTreeSet};

#[derive(Serialize, Deserialize)]
pub struct Output {
Expand All @@ -17,7 +16,7 @@ pub(crate) fn run(options: Options) -> Result {
.collect::<BTreeSet<OutPoint>>();

let mut balance = 0;
for (outpoint, amount) in index.get_unspent_outputs()? {
for (outpoint, amount) in index.get_unspent_outputs(Wallet::load(&options)?)? {
if !inscription_outputs.contains(&outpoint) {
balance += amount.to_sat()
}
Expand Down
3 changes: 2 additions & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
super::*,
crate::wallet::Wallet,
bitcoin::{
blockdata::{opcodes, script},
policy::MAX_STANDARD_TX_WEIGHT,
Expand Down Expand Up @@ -57,7 +58,7 @@ impl Inscribe {
let index = Index::open(&options)?;
index.update()?;

let mut utxos = index.get_unspent_outputs()?;
let mut utxos = index.get_unspent_outputs(Wallet::load(&options)?)?;

let inscriptions = index.get_inscriptions(None)?;

Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, crate::wallet::Wallet};

#[derive(Serialize, Deserialize)]
pub struct Output {
Expand All @@ -12,7 +12,7 @@ pub(crate) fn run(options: Options) -> Result {
index.update()?;

let inscriptions = index.get_inscriptions(None)?;
let unspent_outputs = index.get_unspent_outputs()?;
let unspent_outputs = index.get_unspent_outputs(Wallet::load(&options)?)?;

let explorer = match options.chain() {
Chain::Mainnet => "https://ordinals.com/inscription/",
Expand Down
6 changes: 4 additions & 2 deletions src/subcommand/wallet/outputs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, crate::wallet::Wallet};

#[derive(Serialize, Deserialize)]
pub struct Output {
Expand All @@ -10,8 +10,10 @@ pub(crate) fn run(options: Options) -> Result {
let index = Index::open(&options)?;
index.update()?;

options.bitcoin_rpc_client_for_wallet_command(false)?;

let mut outputs = Vec::new();
for (output, amount) in index.get_unspent_outputs()? {
for (output, amount) in index.get_unspent_outputs(Wallet::load(&options)?)? {
outputs.push(Output {
output,
amount: amount.to_sat(),
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/sats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, crate::wallet::Wallet};

#[derive(Debug, Parser)]
pub(crate) struct Sats {
Expand Down Expand Up @@ -28,7 +28,7 @@ impl Sats {
let index = Index::open(&options)?;
index.update()?;

let utxos = get_unspent_output_ranges(&index)?;
let utxos = index.get_unspent_output_ranges(Wallet::load(&options)?)?;

if let Some(path) = &self.tsv {
let mut output = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use {super::*, crate::wallet::Wallet};

#[derive(Debug, Parser)]
pub(crate) struct Send {
Expand Down Expand Up @@ -32,7 +32,7 @@ impl Send {
let index = Index::open(&options)?;
index.update()?;

let unspent_outputs = index.get_unspent_outputs()?;
let unspent_outputs = index.get_unspent_outputs(Wallet::load(&options)?)?;

let inscriptions = index.get_inscriptions(None)?;

Expand Down
13 changes: 13 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

pub(crate) struct Wallet {
_foo: (),
}

impl Wallet {
pub(crate) fn load(options: &Options) -> Result<Self> {
options.bitcoin_rpc_client_for_wallet_command(false)?;

Ok(Self { _foo: () })
}
}
10 changes: 10 additions & 0 deletions test-bitcoincore-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod state;

pub fn builder() -> Builder {
Builder {
descriptors: Vec::new(),
fail_lock_unspent: false,
network: Network::Bitcoin,
version: 240000,
Expand All @@ -47,6 +48,7 @@ pub fn builder() -> Builder {
}

pub struct Builder {
descriptors: Vec<String>,
fail_lock_unspent: bool,
network: Network,
version: usize,
Expand Down Expand Up @@ -76,8 +78,16 @@ impl Builder {
}
}

pub fn descriptors(self, descriptors: Vec<String>) -> Self {
Self {
descriptors,
..self
}
}

pub fn build(self) -> Handle {
let state = Arc::new(Mutex::new(State::new(
self.descriptors,
self.network,
self.version,
self.wallet_name,
Expand Down
10 changes: 8 additions & 2 deletions test-bitcoincore-rpc/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) struct State {

impl State {
pub(crate) fn new(
descriptors: Vec<String>,
network: Network,
version: usize,
wallet_name: &str,
Expand All @@ -33,9 +34,14 @@ impl State {
hashes.push(genesis_block_hash);
blocks.insert(genesis_block_hash, genesis_block);

let mut wallets = BTreeSet::new();
if !descriptors.is_empty() {
wallets.insert(wallet_name.to_string());
}

Self {
blocks,
descriptors: Vec::new(),
descriptors,
fail_lock_unspent,
hashes,
locked: BTreeSet::new(),
Expand All @@ -47,7 +53,7 @@ impl State {
utxos: BTreeMap::new(),
version,
wallet_name: wallet_name.to_string(),
wallets: BTreeSet::new(),
wallets,
loaded_wallets: BTreeSet::new(),
}
}
Expand Down