From a1fe56c18b751e71c2f6d9caaf68cef63bd25b7d Mon Sep 17 00:00:00 2001 From: wanyvic Date: Mon, 15 May 2023 20:55:44 +0800 Subject: [PATCH] fix brc20 zero inscription number --- src/brc20/updater.rs | 31 ++++++++++++++++--------------- src/index.rs | 9 ++++----- src/index/entry.rs | 2 +- src/index/updater.rs | 3 ++- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/brc20/updater.rs b/src/brc20/updater.rs index aa33192c61..cec46b4f39 100644 --- a/src/brc20/updater.rs +++ b/src/brc20/updater.rs @@ -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 { @@ -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( @@ -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> = match operation.action { Action::Inscribe(inscribe) => match inscribe.operation { Operation::Deploy(deploy) => { diff --git a/src/index.rs b/src/index.rs index 2ff1b9bda4..d1dcb9a6bb 100644 --- a/src/index.rs +++ b/src/index.rs @@ -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::*, @@ -21,6 +18,8 @@ use { std::sync::atomic::{self, AtomicBool}, }; +pub(super) use self::entry::{InscriptionEntryValue, InscriptionIdValue}; + mod entry; mod fetcher; mod rtx; @@ -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 { diff --git a/src/index/entry.rs b/src/index/entry.rs index 15ff3d8ecb..5ce55ecd30 100644 --- a/src/index/entry.rs +++ b/src/index/entry.rs @@ -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; diff --git a/src/index/updater.rs b/src/index/updater.rs index 224993128d..34a02bfacc 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -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