Skip to content

Commit

Permalink
Merge pull request #47 from txpipe/utxo-tx-fields
Browse files Browse the repository at this point in the history
Add extra fields to utxo events
  • Loading branch information
SupernaviX authored Jan 22, 2025
2 parents cf2791d + bc400c1 commit dd56cd0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
55 changes: 51 additions & 4 deletions balius-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ pub enum Tx {
}

impl Tx {
pub fn hash(&self) -> Vec<u8> {
match self {
Self::Cardano(tx) => tx.hash.clone().into(),
}
}
pub fn outputs(&self) -> Vec<Utxo> {
match self {
Self::Cardano(tx) => tx
Expand All @@ -153,6 +158,16 @@ pub enum Block {
}

impl Block {
pub fn hash(&self) -> Vec<u8> {
match self {
Self::Cardano(block) => block.header.as_ref().unwrap().hash.clone().into(),
}
}
pub fn height(&self) -> u64 {
match self {
Self::Cardano(block) => block.header.as_ref().unwrap().height,
}
}
pub fn txs(&self) -> Vec<Tx> {
match self {
Self::Cardano(block) => block
Expand Down Expand Up @@ -245,11 +260,27 @@ impl LoadedWorker {
}

async fn apply_block(&mut self, block: &Block) -> Result<(), Error> {
let block_hash = block.hash();
let block_height = block.height();
for tx in block.txs() {
for utxo in tx.outputs() {
let tx_hash = tx.hash();
for (index, utxo) in tx.outputs().into_iter().enumerate() {
let channels = self.wasm_store.data().router.find_utxo_targets(&utxo)?;
if channels.is_empty() {
continue;
}

let event = wit::Event::Utxo(utxo.to_bytes());
let event = wit::Event::Utxo(wit::balius::app::driver::Utxo {
block: wit::balius::app::driver::BlockRef {
block_hash: block_hash.clone(),
block_height,
},
body: utxo.to_bytes(),
ref_: wit::balius::app::driver::TxoRef {
tx_hash: tx_hash.clone(),
txo_index: index as u32,
},
});

for channel in channels {
self.acknowledge_event(channel, &event).await?;
Expand All @@ -261,11 +292,27 @@ impl LoadedWorker {
}

async fn undo_block(&mut self, block: &Block) -> Result<(), Error> {
let block_hash = block.hash();
let block_height = block.height();
for tx in block.txs() {
for utxo in tx.outputs() {
let tx_hash = tx.hash();
for (index, utxo) in tx.outputs().into_iter().enumerate() {
let channels = self.wasm_store.data().router.find_utxo_targets(&utxo)?;
if channels.is_empty() {
continue;
}

let event = wit::Event::UtxoUndo(utxo.to_bytes());
let event = wit::Event::UtxoUndo(wit::balius::app::driver::Utxo {
block: wit::balius::app::driver::BlockRef {
block_hash: block_hash.clone(),
block_height,
},
body: utxo.to_bytes(),
ref_: wit::balius::app::driver::TxoRef {
tx_hash: tx_hash.clone(),
txo_index: index as u32,
},
});

for channel in channels {
self.acknowledge_event(channel, &event).await?;
Expand Down
23 changes: 19 additions & 4 deletions balius-sdk/src/qol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ impl<T> std::ops::Deref for Json<T> {
}

pub struct Utxo<D> {
pub block_hash: Vec<u8>,
pub block_height: u64,
pub tx_hash: Vec<u8>,
pub index: u64,
pub utxo: utxorpc_spec::utxorpc::v1alpha::cardano::TxOutput,
pub datum: Option<D>,
}
Expand All @@ -230,15 +234,26 @@ impl<D> TryFrom<wit::Event> for Utxo<D> {
fn try_from(value: wit::Event) -> Result<Self, Self::Error> {
use prost::Message;

let bytes = match value {
let utxo = match value {
wit::Event::Utxo(x) => x,
wit::Event::UtxoUndo(x) => x,
_ => return Err(Error::EventMismatch("utxo|utxoundo".to_owned())),
};

let utxo = Message::decode(bytes.as_slice()).map_err(|_| Self::Error::BadUtxo)?;

Ok(Utxo { utxo, datum: None })
let block_hash = utxo.block.block_hash;
let block_height = utxo.block.block_height;
let tx_hash = utxo.ref_.tx_hash;
let index = utxo.ref_.txo_index as u64;
let utxo = Message::decode(utxo.body.as_slice()).map_err(|_| Self::Error::BadUtxo)?;

Ok(Utxo {
block_hash,
block_height,
tx_hash,
index,
utxo,
datum: None,
})
}
}

Expand Down
20 changes: 18 additions & 2 deletions wit/balius.wit
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,25 @@ interface driver {
type timestamp = u64;
type params = json;

record txo-ref {
tx-hash: list<u8>,
txo-index: u32,
}

record block-ref {
block-hash: list<u8>,
block-height: u64,
}

record utxo {
body: cbor,
ref: txo-ref,
block: block-ref,
}

variant event {
utxo(cbor),
utxo-undo(cbor),
utxo(utxo),
utxo-undo(utxo),
timer(timestamp),
request(params),
message(json),
Expand Down

0 comments on commit dd56cd0

Please sign in to comment.