Skip to content

Commit

Permalink
Only store transactions with inscriptions in the database (ordinals#2926
Browse files Browse the repository at this point in the history
)
  • Loading branch information
casey committed Jan 1, 2024
1 parent 8d2ffd6 commit e95e6d5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
17 changes: 4 additions & 13 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ impl<'index> Updater<'_> {
wtx.open_table(SEQUENCE_NUMBER_TO_INSCRIPTION_ENTRY)?;
let mut sequence_number_to_satpoint = wtx.open_table(SEQUENCE_NUMBER_TO_SATPOINT)?;
let mut statistic_to_count = wtx.open_table(STATISTIC_TO_COUNT)?;
let mut transaction_id_to_transaction = wtx.open_table(TRANSACTION_ID_TO_TRANSACTION)?;

let mut lost_sats = statistic_to_count
.get(&Statistic::LostSats.key())?
Expand Down Expand Up @@ -430,6 +431,7 @@ impl<'index> Updater<'_> {
home_inscription_count,
home_inscriptions: &mut home_inscriptions,
id_to_sequence_number: &mut inscription_id_to_sequence_number,
index_transactions: self.index.index_transactions,
inscription_number_to_sequence_number: &mut inscription_number_to_sequence_number,
lost_sats,
next_sequence_number,
Expand All @@ -441,6 +443,8 @@ impl<'index> Updater<'_> {
sequence_number_to_entry: &mut sequence_number_to_inscription_entry,
sequence_number_to_satpoint: &mut sequence_number_to_satpoint,
timestamp: block.header.time,
transaction_buffer: Vec::new(),
transaction_id_to_transaction: &mut transaction_id_to_transaction,
unbound_inscriptions,
value_cache,
value_receiver,
Expand Down Expand Up @@ -618,19 +622,6 @@ impl<'index> Updater<'_> {
}
}

if index.index_transactions {
let mut transaction_id_to_transaction = wtx.open_table(TRANSACTION_ID_TO_TRANSACTION)?;

let mut buffer = Vec::new();
for (transaction, txid) in block.txdata {
transaction
.consensus_encode(&mut buffer)
.expect("in-memory writers don't error");
transaction_id_to_transaction.insert(&txid.store(), buffer.as_slice())?;
buffer.clear();
}
}

height_to_block_header.insert(&self.height, &block.header.store())?;

self.height += 1;
Expand Down
20 changes: 19 additions & 1 deletion src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ pub(super) struct InscriptionUpdater<'a, 'db, 'tx> {
pub(super) home_inscription_count: u64,
pub(super) home_inscriptions: &'a mut Table<'db, 'tx, u32, InscriptionIdValue>,
pub(super) id_to_sequence_number: &'a mut Table<'db, 'tx, InscriptionIdValue, u32>,
pub(super) index_transactions: bool,
pub(super) inscription_number_to_sequence_number: &'a mut Table<'db, 'tx, i32, u32>,
pub(super) lost_sats: u64,
pub(super) next_sequence_number: u32,
pub(super) outpoint_to_value: &'a mut Table<'db, 'tx, &'static OutPointValue, u64>,
pub(super) reward: u64,
pub(super) transaction_buffer: Vec<u8>,
pub(super) transaction_id_to_transaction:
&'a mut Table<'db, 'tx, &'static TxidValue, &'static [u8]>,
pub(super) sat_to_sequence_number: &'a mut MultimapTable<'db, 'tx, u64, u32>,
pub(super) satpoint_to_sequence_number:
&'a mut MultimapTable<'db, 'tx, &'static SatPointValue, u32>,
Expand All @@ -69,13 +73,16 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
txid: Txid,
input_sat_ranges: Option<&VecDeque<(u64, u64)>>,
) -> Result {
let mut envelopes = ParsedEnvelope::from_transaction(tx).into_iter().peekable();
let mut floating_inscriptions = Vec::new();
let mut id_counter = 0;
let mut inscribed_offsets = BTreeMap::new();
let mut total_input_value = 0;
let total_output_value = tx.output.iter().map(|txout| txout.value).sum::<u64>();

let envelopes = ParsedEnvelope::from_transaction(tx);
let inscriptions = !envelopes.is_empty();
let mut envelopes = envelopes.into_iter().peekable();

for (input_index, tx_in) in tx.input.iter().enumerate() {
// skip subsidy since no inscriptions possible
if tx_in.previous_output.is_null() {
Expand Down Expand Up @@ -214,6 +221,17 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
}
}

if self.index_transactions && inscriptions {
tx.consensus_encode(&mut self.transaction_buffer)
.expect("in-memory writers don't error");

self
.transaction_id_to_transaction
.insert(&txid.store(), self.transaction_buffer.as_slice())?;

self.transaction_buffer.clear();
}

let potential_parents = floating_inscriptions
.iter()
.map(|flotsam| flotsam.inscription_id)
Expand Down
28 changes: 17 additions & 11 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,28 +461,34 @@ fn sat_recursive_endpoints_without_sat_index_return_404() {
}

#[test]
fn transactions_are_stored_with_transaction_index() {
fn inscription_transactions_are_stored_with_transaction_index() {
let rpc_server = test_bitcoincore_rpc::spawn();

rpc_server.mine_blocks(2);
create_wallet(&rpc_server);
let (_inscription, reveal) = inscribe(&rpc_server);

let server = TestServer::spawn_with_args(&rpc_server, &["--index-transactions"]);

let coinbase = rpc_server.tx(1, 0).txid();

assert_eq!(
server
.request("/tx/f02151fda57850f323fa22b3d74c1e7039658ac788566677725fe682efb1fe3b")
.status(),
server.request(format!("/tx/{reveal}")).status(),
StatusCode::OK,
);

rpc_server.clear_state();
assert_eq!(
server.request(format!("/tx/{coinbase}")).status(),
StatusCode::OK,
);

rpc_server.mine_blocks(1);
rpc_server.clear_state();

assert_eq!(
server
.request("/tx/f02151fda57850f323fa22b3d74c1e7039658ac788566677725fe682efb1fe3b")
.status(),
server.request(format!("/tx/{reveal}")).status(),
StatusCode::OK,
);

assert_eq!(
server.request(format!("/tx/{coinbase}")).status(),
StatusCode::NOT_FOUND,
);
}

0 comments on commit e95e6d5

Please sign in to comment.