Skip to content

Commit

Permalink
Merge branch 'tweak-outputs-return' into output_recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Dec 20, 2024
2 parents b2d8b20 + d6dc51f commit c81655d
Show file tree
Hide file tree
Showing 25 changed files with 229 additions and 167 deletions.
8 changes: 4 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,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 Expand Up @@ -239,7 +239,7 @@ pub struct SatInscriptions {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct AddressInfo {
pub outputs: Vec<OutPoint>,
pub inscriptions: Vec<InscriptionId>,
pub inscriptions: Option<Vec<InscriptionId>>,
pub sat_balance: u64,
pub runes_balances: Vec<(SpacedRune, Decimal, Option<char>)>,
pub runes_balances: Option<Vec<(SpacedRune, Decimal, Option<char>)>>,
}
85 changes: 58 additions & 27 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub(crate) enum Statistic {
Runes = 13,
SatRanges = 14,
UnboundInscriptions = 16,
LastSavepointHeight = 17,
}

impl Statistic {
Expand Down Expand Up @@ -1019,15 +1020,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 +1055,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 @@ -1533,7 +1538,11 @@ impl Index {
pub fn get_inscriptions_on_output_with_satpoints(
&self,
outpoint: OutPoint,
) -> Result<Vec<(SatPoint, InscriptionId)>> {
) -> Result<Option<Vec<(SatPoint, InscriptionId)>>> {
if !self.index_inscriptions {
return Ok(None);
}

let rtx = self.database.begin_read()?;
let outpoint_to_utxo_entry = rtx.open_table(OUTPOINT_TO_UTXO_ENTRY)?;
let sequence_number_to_inscription_entry =
Expand All @@ -1546,31 +1555,40 @@ impl Index {
)
}

pub fn get_inscriptions_for_output(&self, outpoint: OutPoint) -> Result<Vec<InscriptionId>> {
Ok(
self
.get_inscriptions_on_output_with_satpoints(outpoint)?
pub fn get_inscriptions_for_output(
&self,
outpoint: OutPoint,
) -> Result<Option<Vec<InscriptionId>>> {
let Some(inscriptions) = self.get_inscriptions_on_output_with_satpoints(outpoint)? else {
return Ok(None);
};

Ok(Some(
inscriptions
.iter()
.map(|(_satpoint, inscription_id)| *inscription_id)
.collect(),
)
))
}

pub fn get_inscriptions_for_outputs(
&self,
outpoints: &Vec<OutPoint>,
) -> Result<Vec<InscriptionId>> {
let mut inscriptions = Vec::new();
) -> Result<Option<Vec<InscriptionId>>> {
let mut result = Vec::new();
for outpoint in outpoints {
inscriptions.extend(
self
.get_inscriptions_on_output_with_satpoints(*outpoint)?
let Some(inscriptions) = self.get_inscriptions_on_output_with_satpoints(*outpoint)? else {
return Ok(None);
};

result.extend(
inscriptions
.iter()
.map(|(_satpoint, inscription_id)| *inscription_id),
);
}

Ok(inscriptions)
Ok(Some(result))
}

pub fn get_transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
Expand Down Expand Up @@ -2246,13 +2264,13 @@ impl Index {
outpoint_to_utxo_entry: &'a impl ReadableTable<&'static OutPointValue, &'static UtxoEntry>,
sequence_number_to_inscription_entry: &'a impl ReadableTable<u32, InscriptionEntryValue>,
outpoint: OutPoint,
) -> Result<Vec<(SatPoint, InscriptionId)>> {
) -> Result<Option<Vec<(SatPoint, InscriptionId)>>> {
if !self.index_inscriptions {
return Ok(Vec::new());
return Ok(None);
}

let Some(utxo_entry) = outpoint_to_utxo_entry.get(&outpoint.store())? else {
return Ok(Vec::new());
return Ok(Some(Vec::new()));
};

let mut inscriptions = utxo_entry.value().parse(self).parse_inscriptions();
Expand All @@ -2265,10 +2283,13 @@ impl Index {
let entry = sequence_number_to_inscription_entry
.get(sequence_number)?
.unwrap();

let satpoint = SatPoint { outpoint, offset };

Ok((satpoint, InscriptionEntry::load(entry.value()).id))
})
.collect::<Result<_>>()
.map(Some)
}

pub fn get_address_info(&self, address: &Address) -> Result<Vec<OutPoint>> {
Expand All @@ -2288,11 +2309,13 @@ impl Index {
pub(crate) fn get_aggregated_rune_balances_for_outputs(
&self,
outputs: &Vec<OutPoint>,
) -> Result<Vec<(SpacedRune, Decimal, Option<char>)>> {
) -> Result<Option<Vec<(SpacedRune, Decimal, Option<char>)>>> {
let mut runes = BTreeMap::new();

for output in outputs {
let rune_balances = self.get_rune_balances_for_output(*output)?;
let Some(rune_balances) = self.get_rune_balances_for_output(*output)? else {
return Ok(None);
};

for (spaced_rune, pile) in rune_balances {
runes
Expand All @@ -2311,12 +2334,12 @@ impl Index {
}
}

Ok(
Ok(Some(
runes
.into_iter()
.map(|(spaced_rune, (decimal, symbol))| (spaced_rune, decimal, symbol))
.collect(),
)
))
}

pub(crate) fn get_sat_balances_for_outputs(&self, outputs: &Vec<OutPoint>) -> Result<u64> {
Expand Down Expand Up @@ -3475,7 +3498,8 @@ mod tests {
context
.index
.get_inscriptions_for_output(OutPoint { txid, vout: 0 })
.unwrap(),
.unwrap()
.unwrap_or_default(),
[]
);

Expand All @@ -3485,7 +3509,8 @@ mod tests {
context
.index
.get_inscriptions_for_output(OutPoint { txid, vout: 0 })
.unwrap(),
.unwrap()
.unwrap_or_default(),
[inscription_id]
);

Expand All @@ -3500,7 +3525,8 @@ mod tests {
context
.index
.get_inscriptions_for_output(OutPoint { txid, vout: 0 })
.unwrap(),
.unwrap()
.unwrap_or_default(),
[]
);

Expand All @@ -3511,7 +3537,8 @@ mod tests {
txid: send_id,
vout: 0,
})
.unwrap(),
.unwrap()
.unwrap_or_default(),
[inscription_id]
);
}
Expand Down Expand Up @@ -3541,7 +3568,8 @@ mod tests {
txid: first,
vout: 0
})
.unwrap(),
.unwrap()
.unwrap_or_default(),
[inscription_id]
);

Expand Down Expand Up @@ -4419,6 +4447,7 @@ mod tests {
.index
.get_inscriptions_on_output_with_satpoints(OutPoint { txid, vout: 0 })
.unwrap()
.unwrap_or_default()
.iter()
.map(|(_satpoint, inscription_id)| *inscription_id)
.collect::<Vec<InscriptionId>>()
Expand Down Expand Up @@ -4483,6 +4512,7 @@ mod tests {
.index
.get_inscriptions_on_output_with_satpoints(OutPoint { txid, vout: 0 })
.unwrap()
.unwrap_or_default()
)
}
}
Expand Down Expand Up @@ -4532,6 +4562,7 @@ mod tests {
vout: 0
})
.unwrap()
.unwrap_or_default()
)
}
}
Expand Down
30 changes: 19 additions & 11 deletions src/index/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ impl Reorg {
return Ok(());
}

if (height < SAVEPOINT_INTERVAL || height % SAVEPOINT_INTERVAL == 0)
&& u32::try_from(
index
.settings
.bitcoin_rpc_client(None)?
.get_blockchain_info()?
.headers,
)
.unwrap()
.saturating_sub(height)
<= CHAIN_TIP_DISTANCE
let height = u64::from(height);

let last_savepoint_height = index
.begin_read()?
.0
.open_table(STATISTIC_TO_COUNT)?
.get(&Statistic::LastSavepointHeight.key())?
.map(|last_savepoint_height| last_savepoint_height.value())
.unwrap_or(0);

let blocks = index.client.get_blockchain_info()?.headers;

if (height < SAVEPOINT_INTERVAL.into()
|| height.saturating_sub(last_savepoint_height) >= SAVEPOINT_INTERVAL.into())
&& blocks.saturating_sub(height) <= CHAIN_TIP_DISTANCE.into()
{
let wtx = index.begin_write()?;

Expand All @@ -111,6 +115,10 @@ impl Reorg {
log::debug!("creating savepoint at height {}", height);
wtx.persistent_savepoint()?;

wtx
.open_table(STATISTIC_TO_COUNT)?
.insert(&Statistic::LastSavepointHeight.key(), &height)?;

Index::increment_statistic(&wtx, Statistic::Commits, 1)?;
wtx.commit()?;
}
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
42 changes: 26 additions & 16 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,13 +774,21 @@ impl Server {
OutputType::Cardinal => {
index
.get_inscriptions_on_output_with_satpoints(output)?
.unwrap_or_default()
.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)?
.unwrap_or_default()
.is_empty(),
OutputType::Runic => !index
.get_rune_balances_for_output(output)?
.unwrap_or_default()
.is_empty(),
OutputType::Runic => !index.get_rune_balances_for_output(output)?.is_empty(),
};

if include {
Expand Down Expand Up @@ -3744,21 +3752,23 @@ mod tests {
transaction: txid,
sat_ranges: None,
indexed: true,
inscriptions: Vec::new(),
inscriptions: Some(Vec::new()),
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
Loading

0 comments on commit c81655d

Please sign in to comment.