Skip to content

Commit

Permalink
Merge pull request #42 from okx/fix/btc20_inscription_number
Browse files Browse the repository at this point in the history
fix brc20 zero inscription number
  • Loading branch information
wanyvic authored May 15, 2023
2 parents 13977a7 + a1fe56c commit 1e683ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
31 changes: 16 additions & 15 deletions src/brc20/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use super::{
use crate::brc20::params::BIGDECIMAL_TEN;
use crate::{
brc20::{error::BRC20Error, params::MAX_DECIMAL_WIDTH, ScriptKey},
index::{InscriptionEntryValue, InscriptionIdValue},
Index, InscriptionId, SatPoint, Txid,
};
use anyhow::anyhow;
use bigdecimal::num_bigint::Sign;
use redb::Table;

#[derive(Clone)]
pub enum Action {
Expand All @@ -34,13 +35,19 @@ pub struct InscriptionData {
pub action: Action,
}

pub(crate) struct BRC20Updater<'a, L: LedgerReadWrite> {
pub(crate) struct BRC20Updater<'a, 'db, 'tx, L: LedgerReadWrite> {
ledger: &'a L,
index: &'a Index,
id_to_entry: &'a Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
}
impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> {
pub fn new(ledger: &'a L, index: &'a Index) -> Self {
Self { ledger, index }
impl<'a, 'db, 'tx, L: LedgerReadWrite> BRC20Updater<'a, 'db, 'tx, L> {
pub fn new(
ledger: &'a L,
id_to_entry: &'a Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
) -> Self {
Self {
ledger,
id_to_entry,
}
}

pub fn index_transaction(
Expand All @@ -54,15 +61,9 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> {
for operation in operations {
let op: EventType;

let inscription_number = self
.index
.get_inscription_entry(operation.inscription_id)
.map_err(|e| Error::Others(e))?
.ok_or(Error::Others(anyhow!(format!(
"inscription number not found for {}",
operation.inscription_id
))))?
.number;
let inscription_number =
Index::get_number_by_inscription_id(self.id_to_entry, operation.inscription_id)
.map_err(|e| Error::Others(e))?;
let result: Result<BRC20Event, Error<L>> = match operation.action {
Action::Inscribe(inscribe) => match inscribe.operation {
Operation::Deploy(deploy) => {
Expand Down
9 changes: 4 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use crate::brc20::ScriptKey;

use {
self::{
entry::{
BlockHashValue, Entry, InscriptionEntry, InscriptionEntryValue, InscriptionIdValue,
OutPointValue, SatPointValue, SatRange,
},
entry::{BlockHashValue, Entry, InscriptionEntry, OutPointValue, SatPointValue, SatRange},
updater::Updater,
},
super::*,
Expand All @@ -21,6 +18,8 @@ use {
std::sync::atomic::{self, AtomicBool},
};

pub(super) use self::entry::{InscriptionEntryValue, InscriptionIdValue};

mod entry;
mod fetcher;
mod rtx;
Expand Down Expand Up @@ -979,7 +978,7 @@ impl Index {
)
}

fn get_inscription_number_by_inscription_id<'a>(
pub(crate) fn get_number_by_inscription_id<'a>(
id_to_entry: &'a impl ReadableTable<&'static InscriptionIdValue, InscriptionEntryValue>,
inscription_id: InscriptionId,
) -> Result<u64> {
Expand Down
2 changes: 1 addition & 1 deletion src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Entry for InscriptionEntry {
}
}

pub(super) type InscriptionIdValue = [u8; 36];
pub(crate) type InscriptionIdValue = [u8; 36];

impl Entry for InscriptionId {
type Value = InscriptionIdValue;
Expand Down
3 changes: 2 additions & 1 deletion src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ impl Updater {
}
inscription_collects.pop();
}
let mut brc20_updater = BRC20Updater::new(&brc20_database, &index);
let mut brc20_updater =
BRC20Updater::new(&brc20_database, &inscription_id_to_inscription_entry);

for (txid, brc20_transaction) in inscription_collects {
brc20_action_count += brc20_updater
Expand Down

0 comments on commit 1e683ae

Please sign in to comment.