Skip to content

Commit 1e9f1a1

Browse files
committed
feat: return transactions and transactions cbor
1 parent 704ad6a commit 1e9f1a1

File tree

2 files changed

+114
-63
lines changed

2 files changed

+114
-63
lines changed

common/src/queries/blocks.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::KeyHash;
1+
use crate::{KeyHash, TxHash};
22

33
pub const DEFAULT_BLOCKS_QUERY_TOPIC: (&str, &str) =
44
("blocks-state-query-topic", "cardano.query.blocks");
@@ -88,10 +88,20 @@ pub struct PreviousBlocks {
8888
}
8989

9090
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
91-
pub struct BlockTransactions {}
91+
pub struct BlockTransactions {
92+
pub hashes: Vec<TxHash>,
93+
}
9294

9395
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
94-
pub struct BlockTransactionsCBOR {}
96+
pub struct BlockTransactionsCBOR {
97+
pub txs: Vec<BlockTransaction>,
98+
}
99+
100+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
101+
pub struct BlockTransaction {
102+
pub hash: TxHash,
103+
pub cbor: Vec<u8>,
104+
}
95105

96106
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
97107
pub struct BlockInvolvedAddresses {}

modules/chain_store/src/chain_store.rs

Lines changed: 101 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use acropolis_common::{
44
crypto::keyhash,
55
messages::{CardanoMessage, Message, StateQuery, StateQueryResponse},
66
queries::blocks::{
7-
BlockInfo, BlocksStateQuery, BlocksStateQueryResponse, NextBlocks, PreviousBlocks,
8-
DEFAULT_BLOCKS_QUERY_TOPIC,
7+
BlockInfo, BlockTransaction, BlockTransactions, BlockTransactionsCBOR, BlocksStateQuery,
8+
BlocksStateQueryResponse, NextBlocks, PreviousBlocks, DEFAULT_BLOCKS_QUERY_TOPIC,
99
},
10+
TxHash,
1011
};
1112
use anyhow::{bail, Result};
1213
use caryatid_sdk::{module, Context, Module};
@@ -84,37 +85,47 @@ impl ChainStore {
8485
query: &BlocksStateQuery,
8586
) -> Result<BlocksStateQueryResponse> {
8687
match query {
87-
BlocksStateQuery::GetLatestBlock => match store.get_latest_block()? {
88-
Some(block) => {
89-
let info = Self::to_block_info(block, store, true)?;
90-
Ok(BlocksStateQueryResponse::LatestBlock(info))
91-
}
92-
None => Ok(BlocksStateQueryResponse::NotFound),
93-
},
88+
BlocksStateQuery::GetLatestBlock => {
89+
let Some(block) = store.get_latest_block()? else {
90+
return Ok(BlocksStateQueryResponse::NotFound);
91+
};
92+
let info = Self::to_block_info(block, store, true)?;
93+
Ok(BlocksStateQueryResponse::LatestBlock(info))
94+
}
95+
BlocksStateQuery::GetLatestBlockTransactions => {
96+
let Some(block) = store.get_latest_block()? else {
97+
return Ok(BlocksStateQueryResponse::NotFound);
98+
};
99+
let txs = Self::to_block_transactions(block)?;
100+
Ok(BlocksStateQueryResponse::LatestBlockTransactions(txs))
101+
}
102+
BlocksStateQuery::GetLatestBlockTransactionsCBOR => {
103+
let Some(block) = store.get_latest_block()? else {
104+
return Ok(BlocksStateQueryResponse::NotFound);
105+
};
106+
let txs = Self::to_block_transactions_cbor(block)?;
107+
Ok(BlocksStateQueryResponse::LatestBlockTransactionsCBOR(txs))
108+
}
94109
BlocksStateQuery::GetBlockInfo { block_key } => {
95-
match store.get_block_by_hash(block_key)? {
96-
Some(block) => {
97-
let info = Self::to_block_info(block, store, false)?;
98-
Ok(BlocksStateQueryResponse::BlockInfo(info))
99-
}
100-
None => Ok(BlocksStateQueryResponse::NotFound),
101-
}
110+
let Some(block) = store.get_block_by_hash(block_key)? else {
111+
return Ok(BlocksStateQueryResponse::NotFound);
112+
};
113+
let info = Self::to_block_info(block, store, false)?;
114+
Ok(BlocksStateQueryResponse::BlockInfo(info))
115+
}
116+
BlocksStateQuery::GetBlockBySlot { slot } => {
117+
let Some(block) = store.get_block_by_slot(*slot)? else {
118+
return Ok(BlocksStateQueryResponse::NotFound);
119+
};
120+
let info = Self::to_block_info(block, store, false)?;
121+
Ok(BlocksStateQueryResponse::BlockBySlot(info))
102122
}
103-
BlocksStateQuery::GetBlockBySlot { slot } => match store.get_block_by_slot(*slot)? {
104-
Some(block) => {
105-
let info = Self::to_block_info(block, store, false)?;
106-
Ok(BlocksStateQueryResponse::BlockBySlot(info))
107-
}
108-
None => Ok(BlocksStateQueryResponse::NotFound),
109-
},
110123
BlocksStateQuery::GetBlockByEpochSlot { epoch, slot } => {
111-
match store.get_block_by_epoch_slot(*epoch, *slot)? {
112-
Some(block) => {
113-
let info = Self::to_block_info(block, store, false)?;
114-
Ok(BlocksStateQueryResponse::BlockByEpochSlot(info))
115-
}
116-
None => Ok(BlocksStateQueryResponse::NotFound),
117-
}
124+
let Some(block) = store.get_block_by_epoch_slot(*epoch, *slot)? else {
125+
return Ok(BlocksStateQueryResponse::NotFound);
126+
};
127+
let info = Self::to_block_info(block, store, false)?;
128+
Ok(BlocksStateQueryResponse::BlockByEpochSlot(info))
118129
}
119130
BlocksStateQuery::GetNextBlocks {
120131
block_key,
@@ -126,19 +137,17 @@ impl ChainStore {
126137
blocks: vec![],
127138
}));
128139
}
129-
match store.get_block_by_hash(&block_key)? {
130-
Some(block) => {
131-
let number = Self::get_block_number(&block)?;
132-
let min_number = number + 1 + skip;
133-
let max_number = min_number + limit - 1;
134-
let blocks = store.get_blocks_by_number_range(min_number, max_number)?;
135-
let info = Self::to_block_info_bulk(blocks, store, false)?;
136-
Ok(BlocksStateQueryResponse::NextBlocks(NextBlocks {
137-
blocks: info,
138-
}))
139-
}
140-
None => Ok(BlocksStateQueryResponse::NotFound),
141-
}
140+
let Some(block) = store.get_block_by_hash(&block_key)? else {
141+
return Ok(BlocksStateQueryResponse::NotFound);
142+
};
143+
let number = Self::get_block_number(&block)?;
144+
let min_number = number + 1 + skip;
145+
let max_number = min_number + limit - 1;
146+
let blocks = store.get_blocks_by_number_range(min_number, max_number)?;
147+
let info = Self::to_block_info_bulk(blocks, store, false)?;
148+
Ok(BlocksStateQueryResponse::NextBlocks(NextBlocks {
149+
blocks: info,
150+
}))
142151
}
143152
BlocksStateQuery::GetPreviousBlocks {
144153
block_key,
@@ -150,23 +159,35 @@ impl ChainStore {
150159
blocks: vec![],
151160
}));
152161
}
153-
match store.get_block_by_hash(&block_key)? {
154-
Some(block) => {
155-
let number = Self::get_block_number(&block)?;
156-
let Some(max_number) = number.checked_sub(1 + skip) else {
157-
return Ok(BlocksStateQueryResponse::PreviousBlocks(PreviousBlocks {
158-
blocks: vec![],
159-
}));
160-
};
161-
let min_number = max_number.saturating_sub(limit - 1);
162-
let blocks = store.get_blocks_by_number_range(min_number, max_number)?;
163-
let info = Self::to_block_info_bulk(blocks, store, false)?;
164-
Ok(BlocksStateQueryResponse::PreviousBlocks(PreviousBlocks {
165-
blocks: info,
166-
}))
167-
}
168-
None => Ok(BlocksStateQueryResponse::NotFound),
169-
}
162+
let Some(block) = store.get_block_by_hash(&block_key)? else {
163+
return Ok(BlocksStateQueryResponse::NotFound);
164+
};
165+
let number = Self::get_block_number(&block)?;
166+
let Some(max_number) = number.checked_sub(1 + skip) else {
167+
return Ok(BlocksStateQueryResponse::PreviousBlocks(PreviousBlocks {
168+
blocks: vec![],
169+
}));
170+
};
171+
let min_number = max_number.saturating_sub(limit - 1);
172+
let blocks = store.get_blocks_by_number_range(min_number, max_number)?;
173+
let info = Self::to_block_info_bulk(blocks, store, false)?;
174+
Ok(BlocksStateQueryResponse::PreviousBlocks(PreviousBlocks {
175+
blocks: info,
176+
}))
177+
}
178+
BlocksStateQuery::GetBlockTransactions { block_key } => {
179+
let Some(block) = store.get_block_by_hash(block_key)? else {
180+
return Ok(BlocksStateQueryResponse::NotFound);
181+
};
182+
let txs = Self::to_block_transactions(block)?;
183+
Ok(BlocksStateQueryResponse::BlockTransactions(txs))
184+
}
185+
BlocksStateQuery::GetBlockTransactionsCBOR { block_key } => {
186+
let Some(block) = store.get_block_by_hash(block_key)? else {
187+
return Ok(BlocksStateQueryResponse::NotFound);
188+
};
189+
let txs = Self::to_block_transactions_cbor(block)?;
190+
Ok(BlocksStateQueryResponse::BlockTransactionsCBOR(txs))
170191
}
171192

172193
other => bail!("{other:?} not yet supported"),
@@ -279,4 +300,24 @@ impl ChainStore {
279300
block_info.reverse();
280301
Ok(block_info)
281302
}
303+
304+
fn to_block_transactions(block: Block) -> Result<BlockTransactions> {
305+
let decoded = pallas_traverse::MultiEraBlock::decode(&block.bytes)?;
306+
let hashes = decoded.txs().iter().map(|tx| TxHash::from(*tx.hash())).collect();
307+
Ok(BlockTransactions { hashes })
308+
}
309+
310+
fn to_block_transactions_cbor(block: Block) -> Result<BlockTransactionsCBOR> {
311+
let decoded = pallas_traverse::MultiEraBlock::decode(&block.bytes)?;
312+
let txs = decoded
313+
.txs()
314+
.iter()
315+
.map(|tx| {
316+
let hash = TxHash::from(*tx.hash());
317+
let cbor = tx.encode();
318+
BlockTransaction { hash, cbor }
319+
})
320+
.collect();
321+
Ok(BlockTransactionsCBOR { txs })
322+
}
282323
}

0 commit comments

Comments
 (0)