Skip to content

Commit

Permalink
Respect locked coins (#2618)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot13maxi authored Nov 8, 2023
1 parent 5af2393 commit cb576ec
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 18 deletions.
41 changes: 28 additions & 13 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use {
Database, MultimapTable, MultimapTableDefinition, ReadableMultimapTable, ReadableTable, Table,
TableDefinition, WriteTransaction,
},
std::collections::HashMap,
std::collections::{BTreeSet, HashMap},
std::io::{BufWriter, Read, Write},
};

Expand Down Expand Up @@ -331,12 +331,28 @@ impl Index {
})
}

pub(crate) fn get_locked_outputs(&self, _wallet: Wallet) -> Result<BTreeSet<OutPoint>> {
#[derive(Deserialize)]
pub(crate) struct JsonOutPoint {
txid: bitcoin::Txid,
vout: u32,
}

Ok(
self
.client
.call::<Vec<JsonOutPoint>>("listlockunspent", &[])?
.into_iter()
.map(|outpoint| OutPoint::new(outpoint.txid, outpoint.vout))
.collect(),
)
}
#[cfg(test)]
fn set_durability(&mut self, durability: redb::Durability) {
self.durability = durability;
}

pub(crate) fn get_unspent_outputs(&self, _wallet: Wallet) -> 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 All @@ -351,19 +367,18 @@ impl Index {
}),
);

#[derive(Deserialize)]
pub(crate) struct JsonOutPoint {
txid: bitcoin::Txid,
vout: u32,
}
let locked_utxos: BTreeSet<OutPoint> = self.get_locked_outputs(wallet)?;

for JsonOutPoint { txid, vout } in self
.client
.call::<Vec<JsonOutPoint>>("listlockunspent", &[])?
{
for outpoint in locked_utxos {
utxos.insert(
OutPoint { txid, vout },
Amount::from_sat(self.client.get_raw_transaction(&txid, None)?.output[vout as usize].value),
outpoint,
Amount::from_sat(
self
.client
.get_raw_transaction(&outpoint.txid, None)?
.output[TryInto::<usize>::try_into(outpoint.vout).unwrap()]
.value,
),
);
}
let rtx = self.database.begin_read()?;
Expand Down
19 changes: 18 additions & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ impl Inscribe {

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

let locked_utxos = index.get_locked_outputs(Wallet::load(&options)?)?;

let client = options.bitcoin_rpc_client_for_wallet_command(false)?;

let chain = options.chain();
Expand Down Expand Up @@ -183,7 +185,7 @@ impl Inscribe {
reveal_fee_rate: self.fee_rate,
satpoint: self.satpoint,
}
.inscribe(chain, &index, &client, &utxos)
.inscribe(chain, &index, &client, &locked_utxos, &utxos)
}

fn parse_metadata(cbor: Option<PathBuf>, json: Option<PathBuf>) -> Result<Option<Vec<u8>>> {
Expand Down Expand Up @@ -271,6 +273,7 @@ mod tests {
.create_batch_inscription_transactions(
BTreeMap::new(),
Chain::Mainnet,
BTreeSet::new(),
utxos.into_iter().collect(),
change,
)
Expand Down Expand Up @@ -310,6 +313,7 @@ mod tests {
.create_batch_inscription_transactions(
BTreeMap::new(),
Chain::Mainnet,
BTreeSet::new(),
utxos.into_iter().collect(),
change,
)
Expand Down Expand Up @@ -352,6 +356,7 @@ mod tests {
.create_batch_inscription_transactions(
inscriptions,
Chain::Mainnet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(1)],
)
Expand Down Expand Up @@ -401,6 +406,7 @@ mod tests {
.create_batch_inscription_transactions(
inscriptions,
Chain::Mainnet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(1)],
)
Expand Down Expand Up @@ -444,6 +450,7 @@ mod tests {
.create_batch_inscription_transactions(
inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(1)],
)
Expand Down Expand Up @@ -524,6 +531,7 @@ mod tests {
.create_batch_inscription_transactions(
inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
)
Expand Down Expand Up @@ -604,6 +612,7 @@ mod tests {
.create_batch_inscription_transactions(
inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(1)],
)
Expand Down Expand Up @@ -660,6 +669,7 @@ mod tests {
.create_batch_inscription_transactions(
BTreeMap::new(),
Chain::Mainnet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(1)],
)
Expand Down Expand Up @@ -698,6 +708,7 @@ mod tests {
.create_batch_inscription_transactions(
BTreeMap::new(),
Chain::Mainnet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(1)],
)
Expand Down Expand Up @@ -866,6 +877,7 @@ inscriptions:
.create_batch_inscription_transactions(
wallet_inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
)
Expand Down Expand Up @@ -961,6 +973,7 @@ inscriptions:
.create_batch_inscription_transactions(
wallet_inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
)
Expand Down Expand Up @@ -1034,6 +1047,7 @@ inscriptions:
.create_batch_inscription_transactions(
wallet_inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
);
Expand Down Expand Up @@ -1070,6 +1084,7 @@ inscriptions:
.create_batch_inscription_transactions(
wallet_inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
)
Expand Down Expand Up @@ -1121,6 +1136,7 @@ inscriptions:
.create_batch_inscription_transactions(
wallet_inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
)
Expand Down Expand Up @@ -1196,6 +1212,7 @@ inscriptions:
.create_batch_inscription_transactions(
wallet_inscriptions,
Chain::Signet,
BTreeSet::new(),
utxos.into_iter().collect(),
[commit_address, change(2)],
)
Expand Down
6 changes: 5 additions & 1 deletion src/subcommand/wallet/inscribe/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Batch {
chain: Chain,
index: &Index,
client: &Client,
locked_utxos: &BTreeSet<OutPoint>,
utxos: &BTreeMap<OutPoint, Amount>,
) -> SubcommandResult {
let wallet_inscriptions = index.get_inscriptions(utxos)?;
Expand All @@ -53,6 +54,7 @@ impl Batch {
.create_batch_inscription_transactions(
wallet_inscriptions,
chain,
locked_utxos.clone(),
utxos.clone(),
commit_tx_change,
)?;
Expand Down Expand Up @@ -176,6 +178,7 @@ impl Batch {
&self,
wallet_inscriptions: BTreeMap<SatPoint, InscriptionId>,
chain: Chain,
locked_utxos: BTreeSet<OutPoint>,
mut utxos: BTreeMap<OutPoint, Amount>,
change: [Address; 2],
) -> Result<(Transaction, Transaction, TweakedKeyPair, u64)> {
Expand Down Expand Up @@ -217,7 +220,7 @@ impl Batch {

utxos
.keys()
.find(|outpoint| !inscribed_utxos.contains(outpoint))
.find(|outpoint| !inscribed_utxos.contains(outpoint) && !locked_utxos.contains(outpoint))
.map(|outpoint| SatPoint {
outpoint: *outpoint,
offset: 0,
Expand Down Expand Up @@ -321,6 +324,7 @@ impl Batch {
satpoint,
wallet_inscriptions,
utxos.clone(),
locked_utxos.clone(),
commit_tx_address.clone(),
change,
self.commit_fee_rate,
Expand Down
3 changes: 3 additions & 0 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ impl Send {

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

let locked_outputs = index.get_locked_outputs(Wallet::load(&options)?)?;

let inscriptions = index.get_inscriptions(&unspent_outputs)?;

let satpoint = match self.outgoing {
Expand Down Expand Up @@ -70,6 +72,7 @@ impl Send {
satpoint,
inscriptions,
unspent_outputs,
locked_outputs,
address.clone(),
change,
self.fee_rate,
Expand Down
Loading

0 comments on commit cb576ec

Please sign in to comment.