feat(network): implement GetChainBlockNo local state query (#441)

This commit is contained in:
Alexsander Falcucci 2024-04-16 14:27:23 +02:00 committed by GitHub
parent 2d6b6fda1e
commit 97a32c9af5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 2 deletions

View file

@ -25,6 +25,9 @@ async fn do_localstate_query(client: &mut NodeClient) {
let result = queries_v16::get_system_start(client).await.unwrap();
info!("result: {:?}", result);
let result = queries_v16::get_chain_block_no(client).await.unwrap();
info!("result: {:?}", result);
let era = queries_v16::get_current_era(client).await.unwrap();
info!("result: {:?}", era);

View file

@ -938,7 +938,8 @@ impl<C> minicbor::encode::Encode<C> for PositiveCoin {
/// Introduced in Conway
/// negInt64 = -9223372036854775808 .. -1
/// posInt64 = 1 .. 9223372036854775807
/// nonZeroInt64 = negInt64 / posInt64 ; this is the same as the current int64 definition but without zero
/// nonZeroInt64 = negInt64 / posInt64 ; this is the same as the current int64
/// definition but without zero
#[derive(Debug, PartialEq, Copy, Clone, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct NonZeroInt(i64);

View file

@ -91,6 +91,15 @@ pub struct SystemStart {
pub picoseconds_of_day: u64,
}
#[derive(Debug, Encode, Decode, PartialEq)]
pub struct ChainBlockNumber {
#[n(0)]
pub slot_timeline: u32,
#[n(1)]
pub block_number: u32,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RationalNumber {
pub numerator: u64,
@ -368,6 +377,14 @@ pub async fn get_system_start(client: &mut Client) -> Result<SystemStart, Client
Ok(result)
}
/// Get the block number for the current tip.
pub async fn get_chain_block_no(client: &mut Client) -> Result<ChainBlockNumber, ClientError> {
let query = Request::GetChainBlockNo;
let result = client.query(query).await?;
Ok(result)
}
/// Get the current protocol parameters.
pub async fn get_current_pparams(
client: &mut Client,

View file

@ -10,7 +10,8 @@ use pallas_network::miniprotocols::blockfetch::BlockRequest;
use pallas_network::miniprotocols::chainsync::{ClientRequest, HeaderContent, Tip};
use pallas_network::miniprotocols::handshake::n2n::VersionData;
use pallas_network::miniprotocols::localstate::queries_v16::{
Addr, Addrs, Fraction, Genesis, Snapshots, Stakes, SystemStart, UnitInterval, Value,
Addr, Addrs, ChainBlockNumber, Fraction, Genesis, Snapshots, Stakes, SystemStart, UnitInterval,
Value,
};
use pallas_network::miniprotocols::localstate::ClientQueryRequest;
use pallas_network::miniprotocols::txsubmission::{EraTxBody, TxIdAndSize};
@ -508,6 +509,25 @@ pub async fn local_state_query_server_and_client_happy_path() {
assert_eq!(*server.statequery().state(), localstate::State::Acquired);
// server receives query from client
let query: localstate::queries_v16::Request =
match server.statequery().recv_while_acquired().await.unwrap() {
ClientQueryRequest::Query(q) => q.into_decode().unwrap(),
x => panic!("unexpected message from client: {x:?}"),
};
assert_eq!(query, localstate::queries_v16::Request::GetChainBlockNo);
assert_eq!(*server.statequery().state(), localstate::State::Querying);
let result = AnyCbor::from_encode(ChainBlockNumber {
slot_timeline: 1,
block_number: 2143789,
});
server.statequery().send_result(result).await.unwrap();
assert_eq!(*server.statequery().state(), localstate::State::Acquired);
// server receives query from client
let query: localstate::queries_v16::Request =
@ -816,6 +836,25 @@ pub async fn local_state_query_server_and_client_happy_path() {
}
);
let request = AnyCbor::from_encode(localstate::queries_v16::Request::GetChainBlockNo);
client.statequery().send_query(request).await.unwrap();
let result: ChainBlockNumber = client
.statequery()
.recv_while_querying()
.await
.unwrap()
.into_decode()
.unwrap();
assert_eq!(
result,
localstate::queries_v16::ChainBlockNumber {
slot_timeline: 1, // current
block_number: 2143789,
}
);
let request = AnyCbor::from_encode(localstate::queries_v16::Request::LedgerQuery(
localstate::queries_v16::LedgerQuery::BlockQuery(
5,