Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use outpoint_to_value table #1172

Merged
merged 6 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl From<Block> for BlockData {
}

pub(crate) struct Updater {
cache: HashMap<OutPointArray, Vec<u8>>,
range_cache: HashMap<OutPointArray, Vec<u8>>,
height: u64,
index_sats: bool,
sat_ranges_since_flush: u64,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Updater {
)?;

let mut updater = Self {
cache: HashMap::new(),
range_cache: HashMap::new(),
height,
index_sats: index.has_sat_index()?,
sat_ranges_since_flush: 0,
Expand Down Expand Up @@ -93,13 +93,14 @@ impl Updater {
let rx = Self::fetch_blocks_from(index, self.height, self.index_sats)?;

let mut uncommitted = 0;
let mut value_cache = HashMap::new();
loop {
let block = match rx.recv() {
Ok(block) => block,
Err(mpsc::RecvError) => break,
};

self.index_block(index, &mut wtx, block)?;
self.index_block(index, &mut wtx, block, &mut value_cache)?;

if let Some(progress_bar) = &mut progress_bar {
progress_bar.inc(1);
Expand All @@ -112,7 +113,8 @@ impl Updater {
uncommitted += 1;

if uncommitted == 5000 {
self.commit(wtx)?;
self.commit(wtx, value_cache)?;
value_cache = HashMap::new();
uncommitted = 0;
wtx = index.begin_write()?;
let height = wtx
Expand Down Expand Up @@ -144,7 +146,7 @@ impl Updater {
}

if uncommitted > 0 {
self.commit(wtx)?;
self.commit(wtx, value_cache)?;
}

if let Some(progress_bar) = &mut progress_bar {
Expand Down Expand Up @@ -245,6 +247,7 @@ impl Updater {
index: &Index,
wtx: &mut WriteTransaction,
block: BlockData,
value_cache: &mut HashMap<OutPoint, u64>,
) -> Result<()> {
let mut height_to_block_hash = wtx.open_table(HEIGHT_TO_BLOCK_HASH)?;

Expand Down Expand Up @@ -296,6 +299,7 @@ impl Updater {
&mut outpoint_to_value,
&mut sat_to_inscription_id,
&mut satpoint_to_inscription_id,
value_cache,
)?;

if self.index_sats {
Expand All @@ -319,7 +323,7 @@ impl Updater {
for input in &tx.input {
let key = encode_outpoint(input.previous_output);

let sat_ranges = match self.cache.remove(&key) {
let sat_ranges = match self.range_cache.remove(&key) {
Some(sat_ranges) => {
self.outputs_cached += 1;
sat_ranges
Expand Down Expand Up @@ -460,40 +464,47 @@ impl Updater {

*outputs_traversed += 1;

self.cache.insert(encode_outpoint(outpoint), sats);
self.range_cache.insert(encode_outpoint(outpoint), sats);
self.outputs_inserted_since_flush += 1;
}

Ok(())
}

fn commit(&mut self, wtx: WriteTransaction) -> Result {
fn commit(&mut self, wtx: WriteTransaction, value_cache: HashMap<OutPoint, u64>) -> Result {
log::info!(
"Committing at block height {}, {} outputs traversed, {} in map, {} cached",
self.height,
self.outputs_traversed,
self.cache.len(),
self.range_cache.len(),
self.outputs_cached
);

if self.index_sats {
log::info!(
"Flushing {} entries ({:.1}% resulting from {} insertions) from memory to database",
self.cache.len(),
self.cache.len() as f64 / self.outputs_inserted_since_flush as f64 * 100.,
self.range_cache.len(),
self.range_cache.len() as f64 / self.outputs_inserted_since_flush as f64 * 100.,
self.outputs_inserted_since_flush,
);

let mut outpoint_to_sat_ranges = wtx.open_table(OUTPOINT_TO_SAT_RANGES)?;

for (k, v) in &self.cache {
outpoint_to_sat_ranges.insert(k, v)?;
for (outpoint, sat_range) in self.range_cache.drain() {
outpoint_to_sat_ranges.insert(&outpoint, &sat_range)?;
}

self.cache.clear();
self.outputs_inserted_since_flush = 0;
}

{
let mut outpoint_to_value = wtx.open_table(OUTPOINT_TO_VALUE)?;

for (outpoint, value) in value_cache {
outpoint_to_value.insert(&encode_outpoint(outpoint), &value)?;
}
}

Index::increment_statistic(&wtx, Statistic::OutputsTraversed, self.outputs_traversed)?;
self.outputs_traversed = 0;
Index::increment_statistic(&wtx, Statistic::SatRanges, self.sat_ranges_since_flush)?;
Expand Down
26 changes: 13 additions & 13 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(super) struct InscriptionUpdater<'a, 'db, 'tx> {
reward: u64,
sat_to_inscription_id: &'a mut Table<'db, 'tx, u64, &'tx InscriptionIdArray>,
satpoint_to_id: &'a mut Table<'db, 'tx, &'tx SatPointArray, &'tx InscriptionIdArray>,
value_cache: &'a mut HashMap<OutPoint, u64>,
}

impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
Expand All @@ -39,6 +40,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
outpoint_to_value: &'a mut Table<'db, 'tx, &'tx OutPointArray, u64>,
sat_to_inscription_id: &'a mut Table<'db, 'tx, u64, &'tx InscriptionIdArray>,
satpoint_to_id: &'a mut Table<'db, 'tx, &'tx SatPointArray, &'tx InscriptionIdArray>,
value_cache: &'a mut HashMap<OutPoint, u64>,
) -> Result<Self> {
let next_number = number_to_id
.iter()?
Expand All @@ -61,6 +63,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
reward: Height(height).subsidy(),
sat_to_inscription_id,
satpoint_to_id,
value_cache,
})
}

Expand Down Expand Up @@ -94,13 +97,12 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
origin: Origin::Old(old_satpoint),
});
}
self
.outpoint_to_value
.remove(&encode_outpoint(tx_in.previous_output))?;

input_value += if let Some(value) = self
input_value += if let Some(value) = self.value_cache.remove(&tx_in.previous_output) {
value
} else if let Some(value) = self
.outpoint_to_value
.get(&encode_outpoint(tx_in.previous_output))?
.remove(&encode_outpoint(tx_in.previous_output))?
{
value.value()
} else {
Expand All @@ -115,7 +117,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
})?
.output[usize::try_from(tx_in.previous_output.vout).unwrap()]
.value
}
};
}
}

Expand Down Expand Up @@ -157,16 +159,14 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
}

output_value = end;
}

for (vout, tx_out) in tx.output.iter().enumerate() {
self.outpoint_to_value.insert(
&encode_outpoint(OutPoint {
self.value_cache.insert(
OutPoint {
vout: vout.try_into().unwrap(),
txid,
}),
&tx_out.value,
)?;
},
tx_out.value,
);
}

if is_coinbase {
Expand Down