Skip to content

Commit

Permalink
feat(network): implement GetChainBlockNo local state query (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
falcucci committed Apr 16, 2024
1 parent 2d6b6fd commit 97a32c9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/n2c-miniprotocols/src/main.rs
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion pallas-codec/src/utils.rs
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
41 changes: 40 additions & 1 deletion pallas-network/tests/protocols.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 97a32c9

Please sign in to comment.