Skip to content

feat(sdk): rs-sdk fetch current epoch #1604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/rs-sdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum Error {
/// Dependency not found, for example data contract for a document not found
#[error("Required {0} not found: {1}")]
MissingDependency(String, String),
/// Epoch not found; we must have at least one epoch
#[error("No epoch found on the Platform; it should never happen")]
EpochNotFound,
}

impl<T: Debug> From<DapiClientError<T>> for Error {
Expand Down
6 changes: 3 additions & 3 deletions packages/rs-sdk/src/platform/fetch_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ impl FetchMany<KeyID> for IdentityPublicKey {
///
/// ## Supported query types
///
/// * [EpochQuery](super::types::epoch::EpochQuery) - query that specifies epoch matching criteria
/// * [EpochIndex](dpp::block::epoch::EpochIndex) - epoch index of first object to find; will return up to
/// [DEFAULT_EPOCH_QUERY_LIMIT](super::query::DEFAULT_EPOCH_QUERY_LIMIT) objects starting from this index
/// * [`LimitQuery<EpochIndex>`](super::LimitQuery) - limit query that allows to specify maximum number of objects
/// to fetch; see also [FetchMany::fetch_many_with_limit()].
///
/// * [`LimitQuery<EpochQuery>`](super::LimitQuery), [`LimitQuery<EpochIndex>`](super::LimitQuery) - limit query
/// that allows to specify maximum number of objects to fetch; see also [FetchMany::fetch_many_with_limit()].
impl FetchMany<EpochIndex> for ExtendedEpochInfo {
type Request = GetEpochsInfoRequest;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/rs-sdk/src/platform/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use rs_dapi_client::transport::TransportRequest;

use crate::{error::Error, platform::document_query::DocumentQuery};

use super::types::epoch::EpochQuery;

/// Default limit of epoch records returned by the platform.
pub const DEFAULT_EPOCH_QUERY_LIMIT: u32 = 100;
/// Default limit of epoch records returned by the platform.
Expand Down Expand Up @@ -178,18 +180,19 @@ impl<Q> From<Q> for LimitQuery<Q> {
}
}

impl Query<GetEpochsInfoRequest> for LimitQuery<EpochIndex> {
impl<E: Into<EpochQuery> + Clone + Debug + Send> Query<GetEpochsInfoRequest> for LimitQuery<E> {
fn query(self, prove: bool) -> Result<GetEpochsInfoRequest, Error> {
if !prove {
unimplemented!("queries without proofs are not supported yet");
}
let inner: EpochQuery = self.query.into();
Ok(GetEpochsInfoRequest {
version: Some(proto::get_epochs_info_request::Version::V0(
proto::get_epochs_info_request::GetEpochsInfoRequestV0 {
prove,
start_epoch: Some(self.query.into()),
start_epoch: inner.start.map(|v| v as u32),
count: self.limit.unwrap_or(DEFAULT_EPOCH_QUERY_LIMIT),
ascending: true,
ascending: inner.ascending,
},
)),
})
Expand Down
1 change: 1 addition & 0 deletions packages/rs-sdk/src/platform/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Type-specific implementation for various dpp object types to make queries more convenient and intuitive.
pub mod epoch;
pub mod identity;
pub mod version_votes;
73 changes: 73 additions & 0 deletions packages/rs-sdk/src/platform/types/epoch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Epoch-related types and helpers
use async_trait::async_trait;
use dapi_grpc::platform::v0::GetEpochsInfoRequest;
use dpp::block::{epoch::EpochIndex, extended_epoch_info::ExtendedEpochInfo};

use crate::{
platform::{Fetch, LimitQuery, Query},
Error, Sdk,
};

#[async_trait]

/// Helper trait for managing Epoch information
pub trait ExtendedEpochInfoEx: Sized {
/// Fetch current (the latest) epoch from the platform.
async fn fetch_current(sdk: &mut Sdk) -> Result<Self, Error>;
}

#[async_trait]
impl ExtendedEpochInfoEx for ExtendedEpochInfo {
async fn fetch_current(sdk: &mut Sdk) -> Result<Self, Error> {
let query = LimitQuery {
query: EpochQuery {
start: None,
ascending: false,
},
limit: Some(1),
};

let epoch = Self::fetch(sdk, query).await?;

epoch.ok_or(Error::EpochNotFound)
}
}
/// Query used to fetch multiple epochs from the platform.
#[derive(Clone, Debug)]
pub struct EpochQuery {
/// Starting number of epoch to fetch.
///
/// It is first returned epoch in the set.
///
/// Value of `None` has the following meaning:
///
/// * if ascending is true, then it is the first epoch on the Platform (eg. epoch 0).
/// * if ascending is false, then it is the last epoch on the Platform (eg. most recent epoch).
pub start: Option<EpochIndex>,
/// Sort order. Default is ascending (true), which means that the first returned epoch is the oldest one.
pub ascending: bool,
}

impl Default for EpochQuery {
fn default() -> Self {
Self {
start: None,
ascending: true,
}
}
}

impl From<EpochIndex> for EpochQuery {
fn from(start: EpochIndex) -> Self {
Self {
start: Some(start),
ascending: true,
}
}
}

impl Query<GetEpochsInfoRequest> for EpochQuery {
fn query(self, prove: bool) -> Result<GetEpochsInfoRequest, Error> {
LimitQuery::from(self).query(prove)
}
}
30 changes: 27 additions & 3 deletions packages/rs-sdk/tests/fetch/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0Getters;
use dpp::block::extended_epoch_info::ExtendedEpochInfo;
use rs_dapi_client::{Dapi, RequestSettings};
use rs_sdk::{
platform::{Fetch, FetchMany, LimitQuery, DEFAULT_EPOCH_QUERY_LIMIT},
platform::{
types::epoch::ExtendedEpochInfoEx, Fetch, FetchMany, LimitQuery, DEFAULT_EPOCH_QUERY_LIMIT,
},
Sdk,
};

Expand Down Expand Up @@ -81,7 +83,8 @@ async fn test_epoch_list() {
let mut sdk = cfg.setup_api().await;

// Given some starting epoch and current epoch
let starting_epoch: EpochIndex = 0;
// Note the devnet does not necessarily start with epoch 0
let starting_epoch: EpochIndex = 3;
let current_epoch = get_current_epoch(&mut sdk, &cfg).await;

// When we fetch epochs from the server, starting with `starting_epoch`
Expand All @@ -107,7 +110,8 @@ async fn test_epoch_list_limit() {
let mut sdk = cfg.setup_api().await;

// Given some starting epoch and current epoch
let starting_epoch: EpochIndex = 1;
// Note the devnet does not necessarily start with epoch 0
let starting_epoch: EpochIndex = 3;
let current_epoch = get_current_epoch(&mut sdk, &cfg).await;
let limit = 2;

Expand Down Expand Up @@ -159,3 +163,23 @@ async fn test_epoch_fetch_future() {

assert!(epoch.is_none());
}

/// Fetch current epoch from the platform.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_epoch_fetch_current() {
setup_logs();

let cfg = Config::new();
let mut sdk = cfg.setup_api().await;

// Given some current epoch
let expected_epoch = get_current_epoch(&mut sdk, &cfg).await;

let epoch = ExtendedEpochInfo::fetch_current(&mut sdk)
.await
.expect("fetch current epoch");

assert_eq!(epoch.index(), expected_epoch);

tracing::info!(epoch = ?epoch, "current epoch");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"request":{"version":{"v0":{"id":[48,18,193,155,152,236,0,51,173,219,54,205,100,183,245,16,103,15,42,53,26,67,4,181,246,153,65,68,40,110,253,172],"prove":true}}},"response":{"version":{"v0":{"metadata":{"height":"399","core_chain_locked_height":1588,"epoch":17,"time_ms":"1701186367912","protocol_version":1,"chain_id":"dashmate_local_35"},"result":{"proof":{"grovedb_proof":[1,1,198,2,1,98,47,81,170,142,110,105,167,245,70,76,165,89,129,240,133,176,58,44,104,42,111,4,126,71,140,147,66,193,24,255,114,4,1,32,0,36,2,1,32,159,101,165,10,103,89,107,118,134,35,62,205,14,245,130,168,86,190,41,247,139,113,170,202,91,69,135,242,242,219,97,152,0,170,248,227,132,247,178,161,4,232,51,133,61,28,97,218,226,181,20,216,124,38,73,160,174,147,91,5,84,177,199,9,213,16,1,180,197,101,53,206,21,50,38,152,10,25,240,13,129,131,128,210,135,203,6,130,149,228,81,190,116,110,230,129,134,53,155,17,2,204,254,253,193,45,87,201,63,108,140,98,61,225,223,229,34,208,158,221,141,103,228,168,52,16,123,83,68,64,77,155,5,16,1,195,54,53,19,110,80,46,154,197,36,75,21,162,10,117,126,7,89,206,10,144,130,60,211,127,137,63,106,73,85,109,38,4,1,96,0,45,4,1,32,159,101,165,10,103,89,107,118,134,35,62,205,14,245,130,168,86,190,41,247,139,113,170,202,91,69,135,242,242,219,97,152,253,0,116,252,183,75,154,0,0,0,113,74,127,73,101,189,31,217,214,4,57,100,210,215,114,108,162,3,2,148,223,117,106,188,172,177,141,123,227,81,211,177,16,1,128,96,231,162,226,108,2,94,140,120,176,49,177,10,7,157,195,202,27,37,131,207,174,36,82,148,129,116,70,158,156,2,17,17,1,247,1,4,32,48,18,193,155,152,236,0,51,173,219,54,205,100,183,245,16,103,15,42,53,26,67,4,181,246,153,65,68,40,110,253,172,0,9,2,1,1,2,1,3,0,0,0,147,44,169,41,89,138,187,109,179,223,164,173,251,188,30,5,193,17,125,230,165,79,95,129,192,255,247,183,248,130,11,127,1,207,145,33,155,181,151,153,234,247,10,107,147,191,8,250,89,1,87,208,72,46,99,152,219,69,191,153,38,116,87,240,26,17,2,188,56,238,209,103,207,163,74,91,179,80,187,245,244,119,112,46,53,5,55,185,116,123,148,188,110,10,156,255,184,161,230,16,1,165,11,155,131,238,228,237,231,206,198,117,129,22,130,89,177,203,28,178,48,127,9,232,55,224,144,114,109,195,236,102,196,17,2,71,134,252,43,28,34,125,43,96,20,45,216,7,117,56,163,2,54,249,33,30,8,97,181,133,247,198,4,14,168,222,180,16,1,253,149,23,63,99,83,217,70,73,145,56,150,148,41,134,194,159,29,139,8,25,21,70,157,96,71,231,72,225,115,151,178,17,1,127,3,1,0,0,11,0,8,0,0,0,0,0,0,0,0,0,4,1,1,0,5,2,1,1,1,0,16,225,21,118,101,134,70,47,103,158,127,6,179,151,197,124,233,214,249,18,65,72,139,138,50,18,118,221,182,13,131,146,16,2,194,9,205,84,213,124,81,201,239,161,86,228,38,75,126,158,141,25,64,89,251,71,61,126,130,248,159,225,40,157,158,188,16,1,70,25,123,162,209,216,154,230,95,14,56,180,32,113,102,210,182,213,32,20,204,112,76,86,112,130,191,29,189,101,249,223,17,2,101,3,1,0,0,45,0,42,0,0,0,0,0,0,0,33,2,67,47,12,14,41,81,212,255,222,72,159,230,209,27,71,17,62,219,159,235,141,240,77,10,88,255,146,191,158,42,3,116,0,0,3,1,1,0,45,0,42,0,1,0,2,0,0,0,33,2,239,132,159,154,2,108,129,219,17,98,41,180,68,164,141,141,196,24,179,198,239,112,12,170,24,76,39,60,130,6,64,245,0,0,16,2,241,1,4,32,48,18,193,155,152,236,0,51,173,219,54,205,100,183,245,16,103,15,42,53,26,67,4,181,246,153,65,68,40,110,253,172,0,3,3,0,0,40,216,51,144,146,203,150,14,133,148,245,43,195,81,47,10,14,155,208,214,193,1,83,251,228,85,71,23,134,145,122,217,1,37,155,21,15,26,155,182,157,94,11,89,145,155,140,155,110,36,232,229,5,196,191,250,6,77,83,25,51,98,20,105,73,17,2,137,55,32,169,123,196,47,111,55,237,142,237,212,151,35,145,129,149,32,86,123,54,68,188,209,146,213,120,53,25,205,45,16,1,130,30,123,13,165,76,200,110,180,125,15,29,148,215,119,226,252,19,105,124,251,151,73,162,22,119,179,249,171,70,68,38,17,2,200,105,64,185,61,22,4,50,132,43,124,194,103,27,115,224,42,137,43,3,126,236,116,160,160,163,177,241,106,191,211,113,16,1,142,88,58,139,31,171,189,155,188,152,82,29,125,16,59,205,139,30,155,166,123,174,220,72,92,252,205,34,221,235,160,96,17],"quorum_hash":[123,144,176,163,196,242,87,123,128,135,83,39,239,120,32,29,65,52,87,220,144,181,236,210,79,66,137,151,63,54,118,157],"signature":[135,79,175,135,193,164,119,151,151,131,174,141,163,69,181,24,215,204,61,237,255,196,70,134,97,121,7,76,147,43,53,250,205,78,147,152,89,222,11,4,154,93,54,197,21,5,99,252,25,79,86,31,22,214,27,140,137,211,133,232,3,222,209,123,214,14,13,3,183,33,37,173,168,248,45,164,69,68,20,188,229,210,130,6,194,49,120,112,19,187,20,221,0,37,34,56],"round":0,"block_id_hash":[0,12,135,39,153,35,126,250,186,177,14,99,228,202,68,226,184,146,126,77,169,28,238,236,54,202,65,171,20,63,25,2],"quorum_type":106}}}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"request":{"version":{"v0":{"id":[48,18,193,155,152,236,0,51,173,219,54,205,100,183,245,16,103,15,42,53,26,67,4,181,246,153,65,68,40,110,253,172],"prove":true}}},"response":{"version":{"v0":{"metadata":{"height":"399","core_chain_locked_height":1588,"epoch":17,"time_ms":"1701186367912","protocol_version":1,"chain_id":"dashmate_local_35"},"result":{"proof":{"grovedb_proof":[1,1,198,2,1,98,47,81,170,142,110,105,167,245,70,76,165,89,129,240,133,176,58,44,104,42,111,4,126,71,140,147,66,193,24,255,114,4,1,32,0,36,2,1,32,159,101,165,10,103,89,107,118,134,35,62,205,14,245,130,168,86,190,41,247,139,113,170,202,91,69,135,242,242,219,97,152,0,170,248,227,132,247,178,161,4,232,51,133,61,28,97,218,226,181,20,216,124,38,73,160,174,147,91,5,84,177,199,9,213,16,1,180,197,101,53,206,21,50,38,152,10,25,240,13,129,131,128,210,135,203,6,130,149,228,81,190,116,110,230,129,134,53,155,17,2,204,254,253,193,45,87,201,63,108,140,98,61,225,223,229,34,208,158,221,141,103,228,168,52,16,123,83,68,64,77,155,5,16,1,195,54,53,19,110,80,46,154,197,36,75,21,162,10,117,126,7,89,206,10,144,130,60,211,127,137,63,106,73,85,109,38,4,1,96,0,45,4,1,32,159,101,165,10,103,89,107,118,134,35,62,205,14,245,130,168,86,190,41,247,139,113,170,202,91,69,135,242,242,219,97,152,253,0,116,252,183,75,154,0,0,0,113,74,127,73,101,189,31,217,214,4,57,100,210,215,114,108,162,3,2,148,223,117,106,188,172,177,141,123,227,81,211,177,16,1,128,96,231,162,226,108,2,94,140,120,176,49,177,10,7,157,195,202,27,37,131,207,174,36,82,148,129,116,70,158,156,2,17,17,1,247,1,4,32,48,18,193,155,152,236,0,51,173,219,54,205,100,183,245,16,103,15,42,53,26,67,4,181,246,153,65,68,40,110,253,172,0,9,2,1,1,2,1,3,0,0,0,147,44,169,41,89,138,187,109,179,223,164,173,251,188,30,5,193,17,125,230,165,79,95,129,192,255,247,183,248,130,11,127,1,207,145,33,155,181,151,153,234,247,10,107,147,191,8,250,89,1,87,208,72,46,99,152,219,69,191,153,38,116,87,240,26,17,2,188,56,238,209,103,207,163,74,91,179,80,187,245,244,119,112,46,53,5,55,185,116,123,148,188,110,10,156,255,184,161,230,16,1,165,11,155,131,238,228,237,231,206,198,117,129,22,130,89,177,203,28,178,48,127,9,232,55,224,144,114,109,195,236,102,196,17,2,71,134,252,43,28,34,125,43,96,20,45,216,7,117,56,163,2,54,249,33,30,8,97,181,133,247,198,4,14,168,222,180,16,1,253,149,23,63,99,83,217,70,73,145,56,150,148,41,134,194,159,29,139,8,25,21,70,157,96,71,231,72,225,115,151,178,17,1,127,3,1,0,0,11,0,8,0,0,0,0,0,0,0,0,0,4,1,1,0,5,2,1,1,1,0,16,225,21,118,101,134,70,47,103,158,127,6,179,151,197,124,233,214,249,18,65,72,139,138,50,18,118,221,182,13,131,146,16,2,194,9,205,84,213,124,81,201,239,161,86,228,38,75,126,158,141,25,64,89,251,71,61,126,130,248,159,225,40,157,158,188,16,1,70,25,123,162,209,216,154,230,95,14,56,180,32,113,102,210,182,213,32,20,204,112,76,86,112,130,191,29,189,101,249,223,17,2,101,3,1,0,0,45,0,42,0,0,0,0,0,0,0,33,2,67,47,12,14,41,81,212,255,222,72,159,230,209,27,71,17,62,219,159,235,141,240,77,10,88,255,146,191,158,42,3,116,0,0,3,1,1,0,45,0,42,0,1,0,2,0,0,0,33,2,239,132,159,154,2,108,129,219,17,98,41,180,68,164,141,141,196,24,179,198,239,112,12,170,24,76,39,60,130,6,64,245,0,0,16,2,241,1,4,32,48,18,193,155,152,236,0,51,173,219,54,205,100,183,245,16,103,15,42,53,26,67,4,181,246,153,65,68,40,110,253,172,0,3,3,0,0,40,216,51,144,146,203,150,14,133,148,245,43,195,81,47,10,14,155,208,214,193,1,83,251,228,85,71,23,134,145,122,217,1,37,155,21,15,26,155,182,157,94,11,89,145,155,140,155,110,36,232,229,5,196,191,250,6,77,83,25,51,98,20,105,73,17,2,137,55,32,169,123,196,47,111,55,237,142,237,212,151,35,145,129,149,32,86,123,54,68,188,209,146,213,120,53,25,205,45,16,1,130,30,123,13,165,76,200,110,180,125,15,29,148,215,119,226,252,19,105,124,251,151,73,162,22,119,179,249,171,70,68,38,17,2,200,105,64,185,61,22,4,50,132,43,124,194,103,27,115,224,42,137,43,3,126,236,116,160,160,163,177,241,106,191,211,113,16,1,142,88,58,139,31,171,189,155,188,152,82,29,125,16,59,205,139,30,155,166,123,174,220,72,92,252,205,34,221,235,160,96,17],"quorum_hash":[123,144,176,163,196,242,87,123,128,135,83,39,239,120,32,29,65,52,87,220,144,181,236,210,79,66,137,151,63,54,118,157],"signature":[135,79,175,135,193,164,119,151,151,131,174,141,163,69,181,24,215,204,61,237,255,196,70,134,97,121,7,76,147,43,53,250,205,78,147,152,89,222,11,4,154,93,54,197,21,5,99,252,25,79,86,31,22,214,27,140,137,211,133,232,3,222,209,123,214,14,13,3,183,33,37,173,168,248,45,164,69,68,20,188,229,210,130,6,194,49,120,112,19,187,20,221,0,37,34,56],"round":0,"block_id_hash":[0,12,135,39,153,35,126,250,186,177,14,99,228,202,68,226,184,146,126,77,169,28,238,236,54,202,65,171,20,63,25,2],"quorum_type":106}}}}}}
Loading