Skip to content

Commit

Permalink
feat(network): implement GetFilteredDelegationsAndRewardAccounts qu…
Browse files Browse the repository at this point in the history
…ery (#552)

Signed-off-by: Santiago Carmuega <santiago@carmuega.me>
Co-authored-by: Santiago Carmuega <santiago@carmuega.me>
  • Loading branch information
sterraf and scarmuega authored Dec 12, 2024
1 parent 57157b6 commit cfc840f
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 99 deletions.
24 changes: 21 additions & 3 deletions examples/n2c-miniprotocols/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use pallas::{
facades::NodeClient,
miniprotocols::{
chainsync,
localstate::queries_v16::{
self, Addr, Addrs, TransactionInput,
},
localstate::queries_v16::{self, Addr, Addrs, StakeAddr, TransactionInput},
Point, PRE_PRODUCTION_MAGIC,
},
},
Expand All @@ -18,6 +16,8 @@ use pallas::{
use tracing::info;
use hex::FromHex;

use hex::FromHex;

async fn do_localstate_query(client: &mut NodeClient) {
let client = client.statequery();

Expand Down Expand Up @@ -46,6 +46,24 @@ async fn do_localstate_query(client: &mut NodeClient) {
let era = queries_v16::get_current_era(client).await.unwrap();
info!("result: {:?}", era);

// Getting delegation and rewards for preprod stake addresses:
let mut addrs = BTreeSet::new();
// 1. `stake_test1uqfp3atrunssjk8a4w7lk3ct97wnscs4wc7v3ynnmx7ll7s2ea9p2`
let addr: Addr = <[u8; 28]>::from_hex(
"1218F563E4E10958FDABBDFB470B2F9D386215763CC89273D9BDFFFA"
).unwrap().to_vec().into();
addrs.insert(StakeAddr::from((0x00, addr)));
// 2. `stake_test1uq2pnumhfrnnse0t3uwj4n0lhz58ehfhkdhr64ylptjhq9cyney6d`
let addr: Addr = <[u8; 28]>::from_hex(
"1419F37748E73865EB8F1D2ACDFFB8A87CDD37B36E3D549F0AE57017"
).unwrap().to_vec().into();
addrs.insert(StakeAddr::from((0x00, addr)));

let result = queries_v16::get_filtered_delegations_rewards(client, era, addrs)
.await
.unwrap();
info!("result: {:?}", result);

let result = queries_v16::get_block_epoch_number(client, era)
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions pallas-network/src/miniprotocols/localstate/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl GenericClient {
{
let request = AnyCbor::from_encode(request);
let response = self.query_any(request).await?;

response.into_decode().map_err(ClientError::InvalidCbor)
}
}
Expand Down
28 changes: 27 additions & 1 deletion pallas-network/src/miniprotocols/localstate/queries_v16/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'b> Decode<'b, ()> for BlockQuery {
// 7 => Ok(Self::GetUTxOWhole),
// 8 => Ok(Self::DebugEpochState),
9 => Ok(Self::GetCBOR(d.decode()?)),
// 10 => Ok(Self::GetFilteredDelegationsAndRewardAccounts(())),
10 => Ok(Self::GetFilteredDelegationsAndRewardAccounts(d.decode()?)),
11 => Ok(Self::GetGenesisConfig),
// 12 => Ok(Self::DebugNewEpochState),
13 => Ok(Self::DebugChainDepState),
Expand Down Expand Up @@ -369,3 +369,29 @@ impl<C> minicbor::encode::Encode<C> for TransactionOutput {
Ok(())
}
}

impl<'b, C> minicbor::decode::Decode<'b, C> for FilteredDelegsRewards {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
d.array()?;
d.array()?;
Ok(FilteredDelegsRewards {
delegs: d.decode_with(ctx)?,
rewards: d.decode_with(ctx)?,
})
}
}

impl<C> minicbor::encode::Encode<C> for FilteredDelegsRewards {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.array(1)?;
e.array(2)?;
e.encode_with(self.delegs.clone(), ctx)?;
e.encode_with(self.rewards.clone(), ctx)?;

Ok(())
}
}
40 changes: 39 additions & 1 deletion pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum BlockQuery {
GetUTxOWhole,
DebugEpochState,
GetCBOR(Box<BlockQuery>),
GetFilteredDelegationsAndRewardAccounts(AnyCbor),
GetFilteredDelegationsAndRewardAccounts(StakeAddrs),
GetGenesisConfig,
DebugNewEpochState,
DebugChainDepState,
Expand Down Expand Up @@ -220,6 +220,30 @@ pub type Addr = Bytes;

pub type Addrs = Vec<Addr>;

#[derive(Debug, Encode, Decode, PartialEq, Eq, Clone, PartialOrd, Ord)]
pub struct StakeAddr {
#[n(0)]
addr_type: u8,
#[n(1)]
payload: Addr,
}

impl From<(u8, Bytes)> for StakeAddr {
fn from((addr_type, payload): (u8, Bytes)) -> Self {
Self { addr_type, payload }
}
}

pub type StakeAddrs = BTreeSet<StakeAddr>;
pub type Delegations = KeyValuePairs<StakeAddr, Bytes>;
pub type RewardAccounts = KeyValuePairs<StakeAddr, u64>;

#[derive(Debug, PartialEq, Clone)]
pub struct FilteredDelegsRewards {
pub delegs: Delegations,
pub rewards: RewardAccounts,
}

pub type Pools = BTreeSet<Bytes>;

pub type Coin = AnyUInt;
Expand Down Expand Up @@ -494,6 +518,20 @@ pub async fn get_genesis_config(
Ok(result)
}

/// Get the delegations and rewards for the given stake addresses.
pub async fn get_filtered_delegations_rewards(
client: &mut Client,
era: u16,
addrs: StakeAddrs,
) -> Result<FilteredDelegsRewards, ClientError> {
let query = BlockQuery::GetFilteredDelegationsAndRewardAccounts(addrs);
let query = LedgerQuery::BlockQuery(era, query);
let query = Request::LedgerQuery(query);
let result = client.query(query).await?;

Ok(result)
}

/// Get a subset of the UTxO, filtered by transaction input.
pub async fn get_utxo_by_txin(
client: &mut Client,
Expand Down
Loading

0 comments on commit cfc840f

Please sign in to comment.