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 1 commit
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
254 changes: 252 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,10 @@ impl Index {

#[cfg(test)]
mod tests {
use {super::*, crate::index::testing::Context};
use {
super::*,
crate::{index::testing::Context, runes::Mint},
};

#[test]
fn height_limit() {
Expand Down Expand Up @@ -5696,7 +5699,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 @@ -5770,4 +5773,251 @@ 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();

context.mine_blocks(1);

// rune etch should emit RuneEtched
let txid0 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, Witness::new())],
op_return: Some(
Runestone {
etching: Some(Etching {
rune: Some(Rune(RUNE)),
mint: Some(Mint {
limit: Some(1000),
..Default::default()
}),
..Default::default()
}),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let id = RuneId {
height: 2,
index: 1,
};

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

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
timestamp: 2,
mints: 0,
mint: Some(MintEntry {
limit: Some(1000),
..Default::default()
}),
..Default::default()
},
)],
[],
);

// rune claim should emit RuneClaimed and RuneTransferred
let txid1 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 0, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: u128::from(id),
amount: 1000,
output: 0,
}],
claim: Some(u128::from(id)),
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let event = event_receiver.blocking_recv().unwrap();
assert_eq!(
event,
Event::RuneClaimed {
block_height: 3,
txid: txid1,
rune_id: id,
amount: 1000,
}
);
let event = event_receiver.blocking_recv().unwrap();
assert_eq!(
event,
Event::RuneTransferred {
block_height: 3,
txid: txid1,
rune_id: id,
amount: 1000,
outpoint: OutPoint {
txid: txid1,
vout: 0,
},
}
);

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
mint: Some(MintEntry {
limit: Some(1000),
..Default::default()
}),
supply: 1000,
timestamp: 2,
mints: 1,
..Default::default()
},
)],
[(
OutPoint {
txid: txid1,
vout: 0,
},
vec![(id, 1000)],
)],
);

// rune transfer should emit RuneTransferred
let txid2 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(3, 1, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: id.into(),
amount: 1000,
output: 0,
}],
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let event = event_receiver.blocking_recv().unwrap();
assert_eq!(
event,
Event::RuneTransferred {
block_height: 4,
txid: txid2,
rune_id: id,
amount: 1000,
outpoint: OutPoint {
txid: txid2,
vout: 0,
},
}
);

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
mint: Some(MintEntry {
limit: Some(1000),
..Default::default()
}),
supply: 1000,
timestamp: 2,
mints: 1,
..Default::default()
},
)],
[(
OutPoint {
txid: txid2,
vout: 0,
},
vec![(id, 1000)],
)],
);

// rune burn should emit RuneBurned
let txid3 = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(4, 1, 0, Witness::new())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: id.into(),
amount: 1000,
output: 1,
}],
..Default::default()
}
.encipher(),
),
..Default::default()
});

context.mine_blocks(1);

let event = event_receiver.blocking_recv().unwrap();
assert_eq!(
event,
Event::RuneBurned {
block_height: 5,
txid: txid3,
amount: 1000,
rune_id: id,
}
);

context.assert_runes(
[(
id,
RuneEntry {
etching: txid0,
rune: Rune(RUNE),
mint: Some(MintEntry {
limit: Some(1000),
..Default::default()
}),
supply: 1000,
timestamp: 2,
mints: 1,
burned: 1000,
..Default::default()
},
)],
[],
);
}
}
28 changes: 27 additions & 1 deletion src/index/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{InscriptionId, SatPoint};
use bitcoin::{OutPoint, Txid};

use crate::{InscriptionId, RuneId, SatPoint};

#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Expand All @@ -17,4 +19,28 @@ pub enum Event {
old_location: SatPoint,
sequence_number: u32,
},
RuneEtched {
block_height: u32,
txid: Txid,
rune_id: RuneId,
},
RuneClaimed {
block_height: u32,
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
txid: Txid,
rune_id: RuneId,
amount: u128,
},
RuneTransferred {
block_height: u32,
txid: Txid,
rune_id: RuneId,
amount: u128,
outpoint: OutPoint,
},
RuneBurned {
block_height: u32,
txid: Txid,
rune_id: RuneId,
amount: u128,
},
}
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ impl<'index> Updater<'index> {
.unwrap_or(0);

let mut rune_updater = RuneUpdater {
event_sender: self.index.event_sender.as_ref(),
height: self.height,
id_to_entry: &mut rune_id_to_rune_entry,
inscription_id_to_sequence_number: &mut inscription_id_to_sequence_number,
Expand Down
40 changes: 40 additions & 0 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(crate) struct RuneUpdate {
}

pub(super) struct RuneUpdater<'a, 'db, 'tx> {
pub(super) event_sender: Option<&'a Sender<Event>>,
pub(super) height: u32,
pub(super) id_to_entry: &'a mut Table<'db, 'tx, RuneIdValue, RuneEntryValue>,
pub(super) inscription_id_to_sequence_number: &'a Table<'db, 'tx, InscriptionIdValue, u32>,
Expand Down Expand Up @@ -72,6 +73,15 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
.entry(RuneId::try_from(claim.id).unwrap())
.or_default();

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::RuneClaimed {
block_height: self.height,
txid,
rune_id: RuneId::try_from(claim.id).unwrap(),
amount: claim.limit,
})?;
}

update.mints += 1;
update.supply += claim.limit;
}
Expand Down Expand Up @@ -219,6 +229,19 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
for (id, balance) in balances {
varint::encode_to_vec(id, &mut buffer);
varint::encode_to_vec(balance, &mut buffer);

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::RuneTransferred {
block_height: self.height,
txid,
amount: balance,
rune_id: RuneId::try_from(id).unwrap(),
outpoint: OutPoint {
txid,
vout: vout.try_into().unwrap(),
},
})?;
}
}

self.outpoint_to_balances.insert(
Expand All @@ -238,6 +261,15 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
.entry(RuneId::try_from(id).unwrap())
.or_default()
.burned += amount;

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

Ok(())
Expand Down Expand Up @@ -288,6 +320,14 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
.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