Skip to content

Commit

Permalink
Return None if index does not have index
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Dec 19, 2024
1 parent afe27e3 commit af8a598
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 59 deletions.
8 changes: 4 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ pub struct Inscriptions {
pub struct Output {
pub address: Option<Address<NetworkUnchecked>>,
pub indexed: bool,
pub inscriptions: Vec<InscriptionId>,
pub inscriptions: Option<Vec<InscriptionId>>,
pub outpoint: OutPoint,
pub runes: BTreeMap<SpacedRune, Pile>,
pub runes: Option<BTreeMap<SpacedRune, Pile>>,
pub sat_ranges: Option<Vec<(u64, u64)>>,
pub script_pubkey: ScriptBuf,
pub spent: bool,
Expand All @@ -168,11 +168,11 @@ pub struct Output {
impl Output {
pub fn new(
chain: Chain,
inscriptions: Vec<InscriptionId>,
inscriptions: Option<Vec<InscriptionId>>,
outpoint: OutPoint,
tx_out: TxOut,
indexed: bool,
runes: BTreeMap<SpacedRune, Pile>,
runes: Option<BTreeMap<SpacedRune, Pile>>,
sat_ranges: Option<Vec<(u64, u64)>>,
spent: bool,
) -> Self {
Expand Down
55 changes: 33 additions & 22 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,15 +1019,19 @@ impl Index {
pub fn get_rune_balances_for_output(
&self,
outpoint: OutPoint,
) -> Result<BTreeMap<SpacedRune, Pile>> {
) -> Result<Option<BTreeMap<SpacedRune, Pile>>> {
if !self.index_runes {
return Ok(None);
}

let rtx = self.database.begin_read()?;

let outpoint_to_balances = rtx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;

let id_to_rune_entries = rtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;

let Some(balances) = outpoint_to_balances.get(&outpoint.store())? else {
return Ok(BTreeMap::new());
return Ok(Some(BTreeMap::new()));
};

let balances_buffer = balances.value();
Expand All @@ -1050,7 +1054,7 @@ impl Index {
);
}

Ok(balances)
Ok(Some(balances))
}

pub fn get_rune_balance_map(&self) -> Result<BTreeMap<SpacedRune, BTreeMap<OutPoint, Pile>>> {
Expand Down Expand Up @@ -1546,14 +1550,21 @@ impl Index {
)
}

pub fn get_inscriptions_for_output(&self, outpoint: OutPoint) -> Result<Vec<InscriptionId>> {
Ok(
pub fn get_inscriptions_for_output(
&self,
outpoint: OutPoint,
) -> Result<Option<Vec<InscriptionId>>> {
if !self.index_inscriptions {
return Ok(None);
}

Ok(Some(
self
.get_inscriptions_on_output_with_satpoints(outpoint)?
.iter()
.map(|(_satpoint, inscription_id)| *inscription_id)
.collect(),
)
))
}

pub fn get_inscriptions_for_outputs(
Expand Down Expand Up @@ -2292,22 +2303,22 @@ impl Index {
let mut runes = BTreeMap::new();

for output in outputs {
let rune_balances = self.get_rune_balances_for_output(*output)?;

for (spaced_rune, pile) in rune_balances {
runes
.entry(spaced_rune)
.and_modify(|(decimal, _symbol): &mut (Decimal, Option<char>)| {
assert_eq!(decimal.scale, pile.divisibility);
decimal.value += pile.amount;
})
.or_insert((
Decimal {
value: pile.amount,
scale: pile.divisibility,
},
pile.symbol,
));
if let Some(rune_balances) = self.get_rune_balances_for_output(*output)? {
for (spaced_rune, pile) in rune_balances {
runes
.entry(spaced_rune)
.and_modify(|(decimal, _symbol): &mut (Decimal, Option<char>)| {
assert_eq!(decimal.scale, pile.divisibility);
decimal.value += pile.amount;
})
.or_insert((
Decimal {
value: pile.amount,
scale: pile.divisibility,
},
pile.symbol,
));
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub(crate) struct List {
pub struct Output {
pub address: Option<Address<NetworkUnchecked>>,
pub indexed: bool,
pub inscriptions: Vec<InscriptionId>,
pub runes: BTreeMap<SpacedRune, Pile>,
pub inscriptions: Option<Vec<InscriptionId>>,
pub runes: Option<BTreeMap<SpacedRune, Pile>>,
pub sat_ranges: Option<Vec<Range>>,
pub script_pubkey: String,
pub spent: bool,
Expand Down
40 changes: 24 additions & 16 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,12 +759,18 @@ impl Server {
index
.get_inscriptions_on_output_with_satpoints(output)?
.is_empty()
&& index.get_rune_balances_for_output(output)?.is_empty()
&& index
.get_rune_balances_for_output(output)?
.unwrap_or_default()
.is_empty()
}
OutputType::Inscribed => !index
.get_inscriptions_on_output_with_satpoints(output)?
.is_empty(),
OutputType::Runic => !index.get_rune_balances_for_output(output)?.is_empty(),
OutputType::Runic => !index
.get_rune_balances_for_output(output)?
.unwrap_or_default()
.is_empty(),
};

if include {
Expand Down Expand Up @@ -3728,21 +3734,23 @@ mod tests {
transaction: txid,
sat_ranges: None,
indexed: true,
inscriptions: Vec::new(),
inscriptions: None,
outpoint: output,
runes: vec![(
SpacedRune {
rune: Rune(RUNE),
spacers: 0
},
Pile {
amount: 340282366920938463463374607431768211455,
divisibility: 1,
symbol: None,
}
)]
.into_iter()
.collect(),
runes: Some(
vec![(
SpacedRune {
rune: Rune(RUNE),
spacers: 0
},
Pile {
amount: 340282366920938463463374607431768211455,
divisibility: 1,
symbol: None,
}
)]
.into_iter()
.collect()
),
spent: false,
}
);
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/wallet/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
Some(
wallet
.get_runes_balances_in_output(output)?
.unwrap_or_default()
.iter()
.map(|(rune, pile)| {
(
Expand Down
4 changes: 3 additions & 1 deletion src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +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_in_output(output)?;
let rune_balances = wallet
.get_runes_balances_in_output(output)?
.unwrap_or_default();

let is_ordinal = inscription_outputs.contains(output);
let is_runic = !rune_balances.is_empty();
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/wallet/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Outputs {
Some(
wallet
.get_runes_balances_in_output(output)?
.unwrap_or_default()
.iter()
.map(|(rune, pile)| {
(
Expand Down
6 changes: 5 additions & 1 deletion src/subcommand/wallet/runics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
.iter()
.filter_map(|(output, _)| {
if runic_utxos.contains(output) {
let rune_balances = wallet.get_runes_balances_in_output(output).ok()?;
let rune_balances = wallet
.get_runes_balances_in_output(output)
.ok()?
.unwrap_or_default();

let mut runes = BTreeMap::new();

for (spaced_rune, pile) in rune_balances {
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/wallet/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Split {
(
output,
balance
.unwrap_or_default()
.into_iter()
.map(|(spaced_rune, pile)| (spaced_rune.rune, pile.amount))
.collect(),
Expand Down
8 changes: 4 additions & 4 deletions src/templates/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use super::*;
#[derive(Boilerplate)]
pub(crate) struct OutputHtml {
pub(crate) chain: Chain,
pub(crate) inscriptions: Vec<InscriptionId>,
pub(crate) inscriptions: Option<Vec<InscriptionId>>,
pub(crate) outpoint: OutPoint,
pub(crate) output: TxOut,
pub(crate) runes: BTreeMap<SpacedRune, Pile>,
pub(crate) runes: Option<BTreeMap<SpacedRune, Pile>>,
pub(crate) sat_ranges: Option<Vec<(u64, u64)>>,
pub(crate) spent: bool,
}
Expand All @@ -29,10 +29,10 @@ mod tests {
assert_regex_match!(
OutputHtml {
chain: Chain::Mainnet,
inscriptions: Vec::new(),
inscriptions: Some(Vec::new()),
outpoint: outpoint(1),
output: TxOut { value: Amount::from_sat(3), script_pubkey: ScriptBuf::new_p2pkh(&PubkeyHash::all_zeros()), },
runes: BTreeMap::new(),
runes: Some(BTreeMap::new()),
sat_ranges: Some(vec![(0, 1), (1, 3)]),
spent: false,
},
Expand Down
15 changes: 11 additions & 4 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ impl Wallet {
}

pub(crate) fn get_inscriptions_in_output(&self, output: &OutPoint) -> Vec<InscriptionId> {
self.output_info.get(output).unwrap().inscriptions.clone()
self
.output_info
.get(output)
.unwrap()
.inscriptions
.clone()
.unwrap_or_default()
}

pub(crate) fn get_parent_info(&self, parents: &[InscriptionId]) -> Result<Vec<ParentInfo>> {
Expand Down Expand Up @@ -253,8 +259,8 @@ impl Wallet {

pub(crate) fn get_runic_outputs(&self) -> Result<BTreeSet<OutPoint>> {
let mut runic_outputs = BTreeSet::new();
for (output, info) in self.output_info.iter() {
if !info.runes.is_empty() {
for (output, info) in &self.output_info {
if !info.runes.clone().unwrap_or_default().is_empty() {
runic_outputs.insert(*output);
}
}
Expand All @@ -265,7 +271,7 @@ impl Wallet {
pub(crate) fn get_runes_balances_in_output(
&self,
output: &OutPoint,
) -> Result<BTreeMap<SpacedRune, Pile>> {
) -> Result<Option<BTreeMap<SpacedRune, Pile>>> {
Ok(
self
.output_info
Expand Down Expand Up @@ -947,6 +953,7 @@ impl Wallet {
(
output,
balance
.unwrap_or_default()
.into_iter()
.map(|(spaced_rune, pile)| (spaced_rune.rune, pile.amount))
.collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl WalletConstructor {

let inscriptions = output_info
.iter()
.flat_map(|(_output, info)| info.inscriptions.clone())
.flat_map(|(_output, info)| info.inscriptions.clone().unwrap_or_default())
.collect::<Vec<InscriptionId>>();

let (inscriptions, inscription_info) = self.get_inscriptions(&inscriptions)?;
Expand Down
8 changes: 4 additions & 4 deletions templates/output.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<h1>Output <span class=monospace>{{self.outpoint}}</span></h1>
<dl>
%% if !self.inscriptions.is_empty() {
%% if let Some(inscriptions) = &self.inscriptions {
<dt>inscriptions</dt>
<dd class=thumbnails>
%% for inscription in &self.inscriptions {
%% for inscription in inscriptions {
{{Iframe::thumbnail(*inscription)}}
%% }
</dd>
%% }
%% if !self.runes.is_empty() {
%% if let Some(runes) = &self.runes {
<dt>runes</dt>
<dd>
<table>
<tr>
<th>rune</th>
<th>balance</th>
</tr>
%% for (rune, balance) in &self.runes {
%% for (rune, balance) in runes {
<tr>
<td><a href=/rune/{{ rune }}>{{ rune }}</a></td>
<td>{{ balance }}</td>
Expand Down

0 comments on commit af8a598

Please sign in to comment.