Skip to content

Commit a0da0c1

Browse files
authored
Return None for assets when asset index does not exist (ordinals#4141)
1 parent 83d87d4 commit a0da0c1

22 files changed

+229
-188
lines changed

src/api.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ pub struct Inscriptions {
155155
pub struct Output {
156156
pub address: Option<Address<NetworkUnchecked>>,
157157
pub indexed: bool,
158-
pub inscriptions: Vec<InscriptionId>,
158+
pub inscriptions: Option<Vec<InscriptionId>>,
159159
pub outpoint: OutPoint,
160-
pub runes: BTreeMap<SpacedRune, Pile>,
160+
pub runes: Option<BTreeMap<SpacedRune, Pile>>,
161161
pub sat_ranges: Option<Vec<(u64, u64)>>,
162162
pub script_pubkey: ScriptBuf,
163163
pub spent: bool,
@@ -168,11 +168,11 @@ pub struct Output {
168168
impl Output {
169169
pub fn new(
170170
chain: Chain,
171-
inscriptions: Vec<InscriptionId>,
171+
inscriptions: Option<Vec<InscriptionId>>,
172172
outpoint: OutPoint,
173173
tx_out: TxOut,
174174
indexed: bool,
175-
runes: BTreeMap<SpacedRune, Pile>,
175+
runes: Option<BTreeMap<SpacedRune, Pile>>,
176176
sat_ranges: Option<Vec<(u64, u64)>>,
177177
spent: bool,
178178
) -> Self {
@@ -229,7 +229,7 @@ pub struct SatInscriptions {
229229
#[derive(Debug, PartialEq, Serialize, Deserialize)]
230230
pub struct AddressInfo {
231231
pub outputs: Vec<OutPoint>,
232-
pub inscriptions: Vec<InscriptionId>,
232+
pub inscriptions: Option<Vec<InscriptionId>>,
233233
pub sat_balance: u64,
234-
pub runes_balances: Vec<(SpacedRune, Decimal, Option<char>)>,
234+
pub runes_balances: Option<Vec<(SpacedRune, Decimal, Option<char>)>>,
235235
}

src/index.rs

+57-27
Original file line numberDiff line numberDiff line change
@@ -1020,15 +1020,19 @@ impl Index {
10201020
pub fn get_rune_balances_for_output(
10211021
&self,
10221022
outpoint: OutPoint,
1023-
) -> Result<BTreeMap<SpacedRune, Pile>> {
1023+
) -> Result<Option<BTreeMap<SpacedRune, Pile>>> {
1024+
if !self.index_runes {
1025+
return Ok(None);
1026+
}
1027+
10241028
let rtx = self.database.begin_read()?;
10251029

10261030
let outpoint_to_balances = rtx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;
10271031

10281032
let id_to_rune_entries = rtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;
10291033

10301034
let Some(balances) = outpoint_to_balances.get(&outpoint.store())? else {
1031-
return Ok(BTreeMap::new());
1035+
return Ok(Some(BTreeMap::new()));
10321036
};
10331037

10341038
let balances_buffer = balances.value();
@@ -1051,7 +1055,7 @@ impl Index {
10511055
);
10521056
}
10531057

1054-
Ok(balances)
1058+
Ok(Some(balances))
10551059
}
10561060

10571061
pub fn get_rune_balance_map(&self) -> Result<BTreeMap<SpacedRune, BTreeMap<OutPoint, Pile>>> {
@@ -1534,7 +1538,11 @@ impl Index {
15341538
pub fn get_inscriptions_on_output_with_satpoints(
15351539
&self,
15361540
outpoint: OutPoint,
1537-
) -> Result<Vec<(SatPoint, InscriptionId)>> {
1541+
) -> Result<Option<Vec<(SatPoint, InscriptionId)>>> {
1542+
if !self.index_inscriptions {
1543+
return Ok(None);
1544+
}
1545+
15381546
let rtx = self.database.begin_read()?;
15391547
let outpoint_to_utxo_entry = rtx.open_table(OUTPOINT_TO_UTXO_ENTRY)?;
15401548
let sequence_number_to_inscription_entry =
@@ -1547,31 +1555,40 @@ impl Index {
15471555
)
15481556
}
15491557

1550-
pub fn get_inscriptions_for_output(&self, outpoint: OutPoint) -> Result<Vec<InscriptionId>> {
1551-
Ok(
1552-
self
1553-
.get_inscriptions_on_output_with_satpoints(outpoint)?
1558+
pub fn get_inscriptions_for_output(
1559+
&self,
1560+
outpoint: OutPoint,
1561+
) -> Result<Option<Vec<InscriptionId>>> {
1562+
let Some(inscriptions) = self.get_inscriptions_on_output_with_satpoints(outpoint)? else {
1563+
return Ok(None);
1564+
};
1565+
1566+
Ok(Some(
1567+
inscriptions
15541568
.iter()
15551569
.map(|(_satpoint, inscription_id)| *inscription_id)
15561570
.collect(),
1557-
)
1571+
))
15581572
}
15591573

15601574
pub fn get_inscriptions_for_outputs(
15611575
&self,
15621576
outpoints: &Vec<OutPoint>,
1563-
) -> Result<Vec<InscriptionId>> {
1564-
let mut inscriptions = Vec::new();
1577+
) -> Result<Option<Vec<InscriptionId>>> {
1578+
let mut result = Vec::new();
15651579
for outpoint in outpoints {
1566-
inscriptions.extend(
1567-
self
1568-
.get_inscriptions_on_output_with_satpoints(*outpoint)?
1580+
let Some(inscriptions) = self.get_inscriptions_on_output_with_satpoints(*outpoint)? else {
1581+
return Ok(None);
1582+
};
1583+
1584+
result.extend(
1585+
inscriptions
15691586
.iter()
15701587
.map(|(_satpoint, inscription_id)| *inscription_id),
15711588
);
15721589
}
15731590

1574-
Ok(inscriptions)
1591+
Ok(Some(result))
15751592
}
15761593

15771594
pub fn get_transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
@@ -2247,13 +2264,13 @@ impl Index {
22472264
outpoint_to_utxo_entry: &'a impl ReadableTable<&'static OutPointValue, &'static UtxoEntry>,
22482265
sequence_number_to_inscription_entry: &'a impl ReadableTable<u32, InscriptionEntryValue>,
22492266
outpoint: OutPoint,
2250-
) -> Result<Vec<(SatPoint, InscriptionId)>> {
2267+
) -> Result<Option<Vec<(SatPoint, InscriptionId)>>> {
22512268
if !self.index_inscriptions {
2252-
return Ok(Vec::new());
2269+
return Ok(None);
22532270
}
22542271

22552272
let Some(utxo_entry) = outpoint_to_utxo_entry.get(&outpoint.store())? else {
2256-
return Ok(Vec::new());
2273+
return Ok(Some(Vec::new()));
22572274
};
22582275

22592276
let mut inscriptions = utxo_entry.value().parse(self).parse_inscriptions();
@@ -2266,10 +2283,13 @@ impl Index {
22662283
let entry = sequence_number_to_inscription_entry
22672284
.get(sequence_number)?
22682285
.unwrap();
2286+
22692287
let satpoint = SatPoint { outpoint, offset };
2288+
22702289
Ok((satpoint, InscriptionEntry::load(entry.value()).id))
22712290
})
22722291
.collect::<Result<_>>()
2292+
.map(Some)
22732293
}
22742294

22752295
pub fn get_address_info(&self, address: &Address) -> Result<Vec<OutPoint>> {
@@ -2289,11 +2309,13 @@ impl Index {
22892309
pub(crate) fn get_aggregated_rune_balances_for_outputs(
22902310
&self,
22912311
outputs: &Vec<OutPoint>,
2292-
) -> Result<Vec<(SpacedRune, Decimal, Option<char>)>> {
2312+
) -> Result<Option<Vec<(SpacedRune, Decimal, Option<char>)>>> {
22932313
let mut runes = BTreeMap::new();
22942314

22952315
for output in outputs {
2296-
let rune_balances = self.get_rune_balances_for_output(*output)?;
2316+
let Some(rune_balances) = self.get_rune_balances_for_output(*output)? else {
2317+
return Ok(None);
2318+
};
22972319

22982320
for (spaced_rune, pile) in rune_balances {
22992321
runes
@@ -2312,12 +2334,12 @@ impl Index {
23122334
}
23132335
}
23142336

2315-
Ok(
2337+
Ok(Some(
23162338
runes
23172339
.into_iter()
23182340
.map(|(spaced_rune, (decimal, symbol))| (spaced_rune, decimal, symbol))
23192341
.collect(),
2320-
)
2342+
))
23212343
}
23222344

23232345
pub(crate) fn get_sat_balances_for_outputs(&self, outputs: &Vec<OutPoint>) -> Result<u64> {
@@ -3476,7 +3498,8 @@ mod tests {
34763498
context
34773499
.index
34783500
.get_inscriptions_for_output(OutPoint { txid, vout: 0 })
3479-
.unwrap(),
3501+
.unwrap()
3502+
.unwrap_or_default(),
34803503
[]
34813504
);
34823505

@@ -3486,7 +3509,8 @@ mod tests {
34863509
context
34873510
.index
34883511
.get_inscriptions_for_output(OutPoint { txid, vout: 0 })
3489-
.unwrap(),
3512+
.unwrap()
3513+
.unwrap_or_default(),
34903514
[inscription_id]
34913515
);
34923516

@@ -3501,7 +3525,8 @@ mod tests {
35013525
context
35023526
.index
35033527
.get_inscriptions_for_output(OutPoint { txid, vout: 0 })
3504-
.unwrap(),
3528+
.unwrap()
3529+
.unwrap_or_default(),
35053530
[]
35063531
);
35073532

@@ -3512,7 +3537,8 @@ mod tests {
35123537
txid: send_id,
35133538
vout: 0,
35143539
})
3515-
.unwrap(),
3540+
.unwrap()
3541+
.unwrap_or_default(),
35163542
[inscription_id]
35173543
);
35183544
}
@@ -3542,7 +3568,8 @@ mod tests {
35423568
txid: first,
35433569
vout: 0
35443570
})
3545-
.unwrap(),
3571+
.unwrap()
3572+
.unwrap_or_default(),
35463573
[inscription_id]
35473574
);
35483575

@@ -4420,6 +4447,7 @@ mod tests {
44204447
.index
44214448
.get_inscriptions_on_output_with_satpoints(OutPoint { txid, vout: 0 })
44224449
.unwrap()
4450+
.unwrap_or_default()
44234451
.iter()
44244452
.map(|(_satpoint, inscription_id)| *inscription_id)
44254453
.collect::<Vec<InscriptionId>>()
@@ -4484,6 +4512,7 @@ mod tests {
44844512
.index
44854513
.get_inscriptions_on_output_with_satpoints(OutPoint { txid, vout: 0 })
44864514
.unwrap()
4515+
.unwrap_or_default()
44874516
)
44884517
}
44894518
}
@@ -4533,6 +4562,7 @@ mod tests {
45334562
vout: 0
45344563
})
45354564
.unwrap()
4565+
.unwrap_or_default()
45364566
)
45374567
}
45384568
}

src/subcommand/list.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ pub(crate) struct List {
1010
pub struct Output {
1111
pub address: Option<Address<NetworkUnchecked>>,
1212
pub indexed: bool,
13-
pub inscriptions: Vec<InscriptionId>,
14-
pub runes: BTreeMap<SpacedRune, Pile>,
13+
pub inscriptions: Option<Vec<InscriptionId>>,
14+
pub runes: Option<BTreeMap<SpacedRune, Pile>>,
1515
pub sat_ranges: Option<Vec<Range>>,
1616
pub script_pubkey: String,
1717
pub spent: bool,

src/subcommand/server.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,21 @@ impl Server {
758758
OutputType::Cardinal => {
759759
index
760760
.get_inscriptions_on_output_with_satpoints(output)?
761+
.unwrap_or_default()
761762
.is_empty()
762-
&& index.get_rune_balances_for_output(output)?.is_empty()
763+
&& index
764+
.get_rune_balances_for_output(output)?
765+
.unwrap_or_default()
766+
.is_empty()
763767
}
764768
OutputType::Inscribed => !index
765769
.get_inscriptions_on_output_with_satpoints(output)?
770+
.unwrap_or_default()
771+
.is_empty(),
772+
OutputType::Runic => !index
773+
.get_rune_balances_for_output(output)?
774+
.unwrap_or_default()
766775
.is_empty(),
767-
OutputType::Runic => !index.get_rune_balances_for_output(output)?.is_empty(),
768776
};
769777

770778
if include {
@@ -3728,21 +3736,23 @@ mod tests {
37283736
transaction: txid,
37293737
sat_ranges: None,
37303738
indexed: true,
3731-
inscriptions: Vec::new(),
3739+
inscriptions: Some(Vec::new()),
37323740
outpoint: output,
3733-
runes: vec![(
3734-
SpacedRune {
3735-
rune: Rune(RUNE),
3736-
spacers: 0
3737-
},
3738-
Pile {
3739-
amount: 340282366920938463463374607431768211455,
3740-
divisibility: 1,
3741-
symbol: None,
3742-
}
3743-
)]
3744-
.into_iter()
3745-
.collect(),
3741+
runes: Some(
3742+
vec![(
3743+
SpacedRune {
3744+
rune: Rune(RUNE),
3745+
spacers: 0
3746+
},
3747+
Pile {
3748+
amount: 340282366920938463463374607431768211455,
3749+
divisibility: 1,
3750+
symbol: None,
3751+
}
3752+
)]
3753+
.into_iter()
3754+
.collect()
3755+
),
37463756
spent: false,
37473757
}
37483758
);

src/subcommand/wallet/addresses.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
1616
for (output, txout) in wallet.utxos() {
1717
let address = wallet.chain().address_from_script(&txout.script_pubkey)?;
1818

19-
let inscriptions = if wallet.has_inscription_index() {
20-
Some(wallet.get_inscriptions_in_output(output))
21-
} else {
22-
None
23-
};
19+
let inscriptions = wallet.get_inscriptions_in_output(output);
2420

25-
let runes = if wallet.has_rune_index() {
26-
Some(
27-
wallet
28-
.get_runes_balances_in_output(output)?
21+
let runes = wallet
22+
.get_runes_balances_in_output(output)?
23+
.map(|balances| {
24+
balances
2925
.iter()
3026
.map(|(rune, pile)| {
3127
(
@@ -36,11 +32,8 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
3632
},
3733
)
3834
})
39-
.collect(),
40-
)
41-
} else {
42-
None
43-
};
35+
.collect()
36+
});
4437

4538
let output = Output {
4639
output: *output,

src/subcommand/wallet/balance.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
2626
let mut runic = 0;
2727

2828
for (output, txout) in unspent_outputs {
29-
let rune_balances = wallet.get_runes_balances_in_output(output)?;
29+
let rune_balances = wallet
30+
.get_runes_balances_in_output(output)?
31+
.unwrap_or_default();
3032

3133
let is_ordinal = inscription_outputs.contains(output);
3234
let is_runic = !rune_balances.is_empty();

src/subcommand/wallet/batch_command.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Batch {
6464
}
6565
.inscribe(
6666
&locked_utxos.into_keys().collect(),
67-
wallet.get_runic_outputs()?,
67+
wallet.get_runic_outputs()?.unwrap_or_default(),
6868
utxos,
6969
&wallet,
7070
)

0 commit comments

Comments
 (0)