Skip to content

Commit 2c21d6b

Browse files
committed
feat: accept hash or number in state query
1 parent 1e9f1a1 commit 2c21d6b

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

common/src/queries/blocks.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ pub enum BlocksStateQuery {
99
GetLatestBlockTransactions,
1010
GetLatestBlockTransactionsCBOR,
1111
GetBlockInfo {
12-
block_key: Vec<u8>,
12+
block_key: BlockKey,
1313
},
1414
GetNextBlocks {
15-
block_key: Vec<u8>,
15+
block_key: BlockKey,
1616
limit: u64,
1717
skip: u64,
1818
},
1919
GetPreviousBlocks {
20-
block_key: Vec<u8>,
20+
block_key: BlockKey,
2121
limit: u64,
2222
skip: u64,
2323
},
@@ -29,16 +29,22 @@ pub enum BlocksStateQuery {
2929
slot: u64,
3030
},
3131
GetBlockTransactions {
32-
block_key: Vec<u8>,
32+
block_key: BlockKey,
3333
},
3434
GetBlockTransactionsCBOR {
35-
block_key: Vec<u8>,
35+
block_key: BlockKey,
3636
},
3737
GetBlockInvolvedAddresses {
38-
block_key: Vec<u8>,
38+
block_key: BlockKey,
3939
},
4040
}
4141

42+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
43+
pub enum BlockKey {
44+
Hash(Vec<u8>),
45+
Number(u64),
46+
}
47+
4248
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4349
pub enum BlocksStateQueryResponse {
4450
LatestBlock(BlockInfo),

modules/chain_store/src/chain_store.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use acropolis_common::{
44
crypto::keyhash,
55
messages::{CardanoMessage, Message, StateQuery, StateQueryResponse},
66
queries::blocks::{
7-
BlockInfo, BlockTransaction, BlockTransactions, BlockTransactionsCBOR, BlocksStateQuery,
8-
BlocksStateQueryResponse, NextBlocks, PreviousBlocks, DEFAULT_BLOCKS_QUERY_TOPIC,
7+
BlockInfo, BlockKey, BlockTransaction, BlockTransactions, BlockTransactionsCBOR,
8+
BlocksStateQuery, BlocksStateQueryResponse, NextBlocks, PreviousBlocks,
9+
DEFAULT_BLOCKS_QUERY_TOPIC,
910
},
1011
TxHash,
1112
};
@@ -107,7 +108,7 @@ impl ChainStore {
107108
Ok(BlocksStateQueryResponse::LatestBlockTransactionsCBOR(txs))
108109
}
109110
BlocksStateQuery::GetBlockInfo { block_key } => {
110-
let Some(block) = store.get_block_by_hash(block_key)? else {
111+
let Some(block) = Self::get_block_by_key(store, block_key)? else {
111112
return Ok(BlocksStateQueryResponse::NotFound);
112113
};
113114
let info = Self::to_block_info(block, store, false)?;
@@ -137,7 +138,7 @@ impl ChainStore {
137138
blocks: vec![],
138139
}));
139140
}
140-
let Some(block) = store.get_block_by_hash(&block_key)? else {
141+
let Some(block) = Self::get_block_by_key(store, &block_key)? else {
141142
return Ok(BlocksStateQueryResponse::NotFound);
142143
};
143144
let number = Self::get_block_number(&block)?;
@@ -159,7 +160,7 @@ impl ChainStore {
159160
blocks: vec![],
160161
}));
161162
}
162-
let Some(block) = store.get_block_by_hash(&block_key)? else {
163+
let Some(block) = Self::get_block_by_key(store, &block_key)? else {
163164
return Ok(BlocksStateQueryResponse::NotFound);
164165
};
165166
let number = Self::get_block_number(&block)?;
@@ -176,14 +177,14 @@ impl ChainStore {
176177
}))
177178
}
178179
BlocksStateQuery::GetBlockTransactions { block_key } => {
179-
let Some(block) = store.get_block_by_hash(block_key)? else {
180+
let Some(block) = Self::get_block_by_key(store, block_key)? else {
180181
return Ok(BlocksStateQueryResponse::NotFound);
181182
};
182183
let txs = Self::to_block_transactions(block)?;
183184
Ok(BlocksStateQueryResponse::BlockTransactions(txs))
184185
}
185186
BlocksStateQuery::GetBlockTransactionsCBOR { block_key } => {
186-
let Some(block) = store.get_block_by_hash(block_key)? else {
187+
let Some(block) = Self::get_block_by_key(store, block_key)? else {
187188
return Ok(BlocksStateQueryResponse::NotFound);
188189
};
189190
let txs = Self::to_block_transactions_cbor(block)?;
@@ -194,6 +195,13 @@ impl ChainStore {
194195
}
195196
}
196197

198+
fn get_block_by_key(store: &Arc<dyn Store>, block_key: &BlockKey) -> Result<Option<Block>> {
199+
match block_key {
200+
BlockKey::Hash(hash) => store.get_block_by_hash(hash),
201+
BlockKey::Number(number) => store.get_block_by_number(*number),
202+
}
203+
}
204+
197205
fn get_block_number(block: &Block) -> Result<u64> {
198206
Ok(pallas_traverse::MultiEraBlock::decode(&block.bytes)?.number())
199207
}

0 commit comments

Comments
 (0)