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

Extend events channel to Runes #3219

Merged
merged 9 commits into from
Apr 14, 2024
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
239 changes: 238 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6273,7 +6273,7 @@ mod tests {
}

#[test]
fn event_sender_channel() {
fn inscription_event_sender_channel() {
let (event_sender, mut event_receiver) = tokio::sync::mpsc::channel(1024);
let context = Context::builder().event_sender(event_sender).build();

Expand Down Expand Up @@ -6347,4 +6347,241 @@ mod tests {
}
);
}

#[test]
fn rune_event_sender_channel() {
const RUNE: u128 = 99246114928149462;

let (event_sender, mut event_receiver) = tokio::sync::mpsc::channel(1024);
let context = Context::builder()
.arg("--index-runes")
.event_sender(event_sender)
.build();

let (txid0, id) = context.etch(
Runestone {
etching: Some(Etching {
rune: Some(Rune(RUNE)),
terms: Some(Terms {
amount: Some(1000),
cap: Some(100),
..default()
}),
..default()
}),
..default()
},
1,
);

context.assert_runes(
[(
id,
RuneEntry {
block: id.block,
etching: txid0,
spaced_rune: SpacedRune {
rune: Rune(RUNE),
spacers: 0,
},
timestamp: id.block,
mints: 0,
terms: Some(Terms {
amount: Some(1000),
cap: Some(100),
..default()
}),
..default()
},
)],
[],
);

assert_eq!(
event_receiver.blocking_recv().unwrap(),
Event::RuneEtched {
block_height: 8,
txid: txid0,
rune_id: id,
}
);

let txid1 = context.core.broadcast_tx(TransactionTemplate {
inputs: &[(2, 0, 0, Witness::new())],
op_return: Some(
Runestone {
mint: Some(id),
..default()
}
.encipher(),
),
..default()
});

context.mine_blocks(1);

context.assert_runes(
[(
id,
RuneEntry {
block: id.block,
etching: txid0,
terms: Some(Terms {
amount: Some(1000),
cap: Some(100),
..default()
}),
mints: 1,
spaced_rune: SpacedRune {
rune: Rune(RUNE),
spacers: 0,
},
premine: 0,
timestamp: id.block,
..default()
},
)],
[(
OutPoint {
txid: txid1,
vout: 0,
},
vec![(id, 1000)],
)],
);

assert_eq!(
event_receiver.blocking_recv().unwrap(),
Event::RuneMinted {
block_height: 9,
txid: txid1,
rune_id: id,
amount: 1000,
}
);

let txid2 = context.core.broadcast_tx(TransactionTemplate {
inputs: &[(9, 1, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id,
amount: 1000,
output: 0,
}],
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

context.assert_runes(
[(
id,
RuneEntry {
block: 8,
etching: txid0,
spaced_rune: SpacedRune {
rune: Rune(RUNE),
..default()
},
terms: Some(Terms {
amount: Some(1000),
cap: Some(100),
..Default::default()
}),
timestamp: 8,
mints: 1,
..Default::default()
},
)],
[(
OutPoint {
txid: txid2,
vout: 0,
},
vec![(id, 1000)],
)],
);

event_receiver.blocking_recv().unwrap();

pretty_assert_eq!(
event_receiver.blocking_recv().unwrap(),
Event::RuneTransferred {
block_height: 10,
txid: txid2,
rune_id: id,
amount: 1000,
outpoint: OutPoint {
txid: txid2,
vout: 0,
},
}
);

let txid3 = context.core.broadcast_tx(TransactionTemplate {
inputs: &[(10, 1, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id,
amount: 111,
output: 0,
}],
..Default::default()
}
.encipher(),
),
op_return_index: Some(0),
..Default::default()
});

context.mine_blocks(1);

context.assert_runes(
[(
id,
RuneEntry {
block: 8,
etching: txid0,
spaced_rune: SpacedRune {
rune: Rune(RUNE),
..default()
},
terms: Some(Terms {
amount: Some(1000),
cap: Some(100),
..Default::default()
}),
timestamp: 8,
mints: 1,
burned: 111,
..Default::default()
},
)],
[(
OutPoint {
txid: txid3,
vout: 1,
},
vec![(id, 889)],
)],
);

event_receiver.blocking_recv().unwrap();

pretty_assert_eq!(
event_receiver.blocking_recv().unwrap(),
Event::RuneBurned {
block_height: 11,
txid: txid3,
amount: 111,
rune_id: id,
}
);
}
}
26 changes: 25 additions & 1 deletion src/index/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{InscriptionId, SatPoint};
use super::*;

#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Expand All @@ -17,4 +17,28 @@ pub enum Event {
old_location: SatPoint,
sequence_number: u32,
},
RuneBurned {
amount: u128,
block_height: u32,
rune_id: RuneId,
txid: Txid,
},
RuneEtched {
block_height: u32,
rune_id: RuneId,
txid: Txid,
},
RuneMinted {
amount: u128,
block_height: u32,
rune_id: RuneId,
txid: Txid,
},
RuneTransferred {
amount: u128,
block_height: u32,
outpoint: OutPoint,
rune_id: RuneId,
txid: Txid,
},
}
2 changes: 1 addition & 1 deletion src/index/lot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
};

#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Default, Serialize, Deserialize)]
pub(super) struct Lot(pub(super) u128);
pub struct Lot(pub u128);

impl Lot {
#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ impl<'index> Updater<'index> {
.unwrap_or(0);

let mut rune_updater = RuneUpdater {
event_sender: self.index.event_sender.as_ref(),
block_time: block.header.time,
burned: HashMap::new(),
client: &self.index.client,
Expand Down
53 changes: 45 additions & 8 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(super) struct RuneUpdater<'a, 'tx, 'client> {
pub(super) block_time: u32,
pub(super) burned: HashMap<RuneId, Lot>,
pub(super) client: &'client Client,
pub(super) event_sender: Option<&'a Sender<Event>>,
pub(super) height: u32,
pub(super) id_to_entry: &'a mut Table<'tx, RuneIdValue, RuneEntryValue>,
pub(super) inscription_id_to_sequence_number: &'a Table<'tx, InscriptionIdValue, u32>,
Expand All @@ -28,6 +29,15 @@ impl<'a, 'tx, 'client> RuneUpdater<'a, 'tx, 'client> {
if let Some(id) = artifact.mint() {
if let Some(amount) = self.mint(id)? {
*unallocated.entry(id).or_default() += amount;

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::RuneMinted {
block_height: self.height,
txid,
rune_id: id,
amount: amount.n(),
})?;
}
}
}

Expand Down Expand Up @@ -179,23 +189,42 @@ impl<'a, 'tx, 'client> RuneUpdater<'a, 'tx, 'client> {
// Sort balances by id so tests can assert balances in a fixed order
balances.sort();

let outpoint = OutPoint {
txid,
vout: vout.try_into().unwrap(),
};

for (id, balance) in balances {
Index::encode_rune_balance(id, balance.n(), &mut buffer);
}

self.outpoint_to_balances.insert(
&OutPoint {
txid,
vout: vout.try_into().unwrap(),
if let Some(sender) = self.event_sender {
sender.blocking_send(Event::RuneTransferred {
outpoint,
block_height: self.height,
txid,
rune_id: id,
amount: balance.0,
})?;
}
.store(),
buffer.as_slice(),
)?;
}

self
.outpoint_to_balances
.insert(&outpoint.store(), buffer.as_slice())?;
}

// increment entries with burned runes
for (id, amount) in burned {
*self.burned.entry(id).or_default() += amount;

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::RuneBurned {
block_height: self.height,
txid,
rune_id: id,
amount: amount.n(),
})?;
}
}

Ok(())
Expand Down Expand Up @@ -278,6 +307,14 @@ impl<'a, 'tx, 'client> RuneUpdater<'a, 'tx, 'client> {

self.id_to_entry.insert(id.store(), entry.store())?;

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::RuneEtched {
block_height: self.height,
txid,
rune_id: id,
})?;
}

let inscription_id = InscriptionId { txid, index: 0 };

if let Some(sequence_number) = self
Expand Down
Loading