Skip to content

Commit

Permalink
Don't fetch all the inscriptions from the DB. Only fetch the ones we …
Browse files Browse the repository at this point in the history
…care about.
  • Loading branch information
gmart7t2 committed May 23, 2023
1 parent 9403707 commit 8b10581
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
19 changes: 8 additions & 11 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,18 +869,15 @@ impl Index {

pub(crate) fn get_inscriptions(
&self,
n: Option<usize>,
utxos: BTreeMap<OutPoint, Amount>,
) -> Result<BTreeMap<SatPoint, InscriptionId>> {
Ok(
self
.database
.begin_read()?
.open_table(SATPOINT_TO_INSCRIPTION_ID)?
.range::<&[u8; 44]>(&[0; 44]..)?
.map(|(satpoint, id)| (Entry::load(*satpoint.value()), Entry::load(*id.value())))
.take(n.unwrap_or(usize::MAX))
.collect(),
)
let mut inscriptions = BTreeMap::new();
let rtx = self.database.begin_read()?;
let table = rtx.open_table(SATPOINT_TO_INSCRIPTION_ID)?;
for utxo in utxos.keys() {
inscriptions.extend(Self::inscriptions_on_output(&table, *utxo)?);
}
Ok(inscriptions)
}

pub(crate) fn get_homepage_inscriptions(&self) -> Result<Vec<InscriptionId>> {
Expand Down
6 changes: 4 additions & 2 deletions src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ pub(crate) fn run(options: Options) -> Result {
let index = Index::open(&options)?;
index.update()?;

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

let inscription_outputs = index
.get_inscriptions(None)?
.get_inscriptions(unspent_outputs.clone())?
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<BTreeSet<OutPoint>>();

let mut balance = 0;
for (outpoint, amount) in index.get_unspent_outputs(Wallet::load(&options)?)? {
for (outpoint, amount) in unspent_outputs {
if !inscription_outputs.contains(&outpoint) {
balance += amount.to_sat()
}
Expand Down
8 changes: 5 additions & 3 deletions src/subcommand/wallet/cardinals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ pub(crate) fn run(options: Options) -> Result {
let index = Index::open(&options)?;
index.update()?;

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

let inscribed_utxos = index
.get_inscriptions(None)?
.get_inscriptions(unspent_outputs.clone())?
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<BTreeSet<OutPoint>>();

let cardinal_utxos = index
.get_unspent_outputs(Wallet::load(&options)?)?
let cardinal_utxos = unspent_outputs
.iter()
.filter_map(|(output, amount)| {
if inscribed_utxos.contains(output) {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Inscribe {
}

tprintln!("[get inscriptions]");
let inscriptions = index.get_inscriptions(None)?;
let inscriptions = index.get_inscriptions(utxos.clone())?;

tprintln!("[get change]");
let commit_tx_change = [
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub(crate) fn run(options: Options) -> Result {

let index_has_sats = index.has_sat_index()?;

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

let explorer = match options.chain() {
Chain::Mainnet => "https://ordinals.com/inscription/",
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Send {
);
}

let inscriptions = index.get_inscriptions(None)?;
let inscriptions = index.get_inscriptions(unspent_outputs.clone())?;

let satpoint = match self.outgoing {
Outgoing::SatPoint(satpoint) => {
Expand Down

0 comments on commit 8b10581

Please sign in to comment.