Skip to content

Commit

Permalink
Fix old owner cache key (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-yu committed Mar 11, 2024
1 parent 66102fa commit 79f153d
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/index/updater/inscription_updater/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::str::FromStr;

lazy_static! {
static ref CLIENT: StreamClient = StreamClient::new();
static ref OLD_OWNER_CACHE: Cache<Txid, Option<Address>> =
static ref OLD_OWNER_CACHE: Cache<OutPoint, Option<Address>> =
Cache::new(Some(Duration::from_secs(60)));
static ref IS_BRC20: bool = env::var("KAFKA_TOPIC")
.map(|kafka_topic| kafka_topic.to_lowercase().contains("brc20"))
Expand Down Expand Up @@ -298,28 +298,29 @@ impl StreamEvent {
pub(crate) fn with_transfer(&mut self, old_satpoint: SatPoint, index: &Index) -> &mut Self {
self.old_location = Some(old_satpoint);

let txid = old_satpoint.outpoint.txid;
self.old_owner = OLD_OWNER_CACHE.get(&txid).unwrap_or_else(|| {
let old_owner = index
.get_transaction(old_satpoint.outpoint.txid)
.unwrap_or(None)
.and_then(|tx| {
tx.output
.get(old_satpoint.outpoint.vout as usize)
.and_then(|txout| {
Address::from_script(&txout.script_pubkey, StreamEvent::get_network())
.map_err(|e| {
warn!(
"StreamEvent::with_transfer could not parse old_owner address: {}",
e
);
})
.ok()
})
});
OLD_OWNER_CACHE.set(txid, old_owner.clone());
old_owner
});
self.old_owner = OLD_OWNER_CACHE
.get(&old_satpoint.outpoint)
.unwrap_or_else(|| {
let old_owner = index
.get_transaction(old_satpoint.outpoint.txid)
.unwrap_or(None)
.and_then(|tx| {
tx.output
.get(old_satpoint.outpoint.vout as usize)
.and_then(|txout| {
Address::from_script(&txout.script_pubkey, StreamEvent::get_network())
.map_err(|e| {
warn!(
"StreamEvent::with_transfer could not parse old_owner address: {}",
e
);
})
.ok()
})
});
OLD_OWNER_CACHE.set(old_satpoint.outpoint, old_owner.clone());
old_owner
});

// Only enrich the inscription if it is a BRC20 transfer
if *IS_BRC20 {
Expand Down

0 comments on commit 79f153d

Please sign in to comment.