Skip to content

Commit

Permalink
fix(network): adjust PoolDistr & ProtocolParam structs used for state…
Browse files Browse the repository at this point in the history
… queries (#551)

Signed-off-by: Pedro Sánchez Terraf <[email protected]>
  • Loading branch information
sterraf authored Dec 14, 2024
1 parent 227b578 commit 9747be3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
5 changes: 2 additions & 3 deletions examples/n2c-miniprotocols/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use pallas::{
crypto::hash::Hash,
};
use tracing::info;
use hex::FromHex;

async fn do_localstate_query(client: &mut NodeClient) {
let client = client.statequery();
Expand Down Expand Up @@ -114,8 +113,8 @@ async fn do_localstate_query(client: &mut NodeClient) {

async fn do_chainsync(client: &mut NodeClient) {
let known_points = vec![Point::Specific(
43847831u64,
hex::decode("15b9eeee849dd6386d3770b0745e0450190f7560e5159b1b3ab13b14b2684a45").unwrap(),
77110778u64,
hex::decode("18e6eeaa592c42113280ba47a0829355e6bed1c9ce67cce4be502d6031d0679a").unwrap(),
)];

let (point, _) = client
Expand Down
31 changes: 16 additions & 15 deletions pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub type PositiveInterval = RationalNumber;

pub type ProtocolVersionMajor = u64;
pub type ProtocolVersionMinor = u64;
pub type ProtocolVersion = (ProtocolVersionMajor, ProtocolVersionMinor);

pub type CostModel = Vec<i64>;

Expand Down Expand Up @@ -169,26 +170,24 @@ pub struct ProtocolParam {
#[n(11)]
pub treasury_growth_rate: Option<UnitInterval>,
#[n(12)]
pub protocol_version_major: Option<ProtocolVersionMajor>,
pub protocol_version: Option<ProtocolVersion>,
#[n(13)]
pub protocol_version_minor: Option<ProtocolVersionMinor>,
#[n(14)]
pub min_pool_cost: Option<Coin>,
#[n(15)]
#[n(14)]
pub ada_per_utxo_byte: Option<Coin>,
#[n(16)]
#[n(15)]
pub cost_models_for_script_languages: Option<CostMdls>,
#[n(17)]
#[n(16)]
pub execution_costs: Option<ExUnitPrices>,
#[n(18)]
#[n(17)]
pub max_tx_ex_units: Option<ExUnits>,
#[n(19)]
#[n(18)]
pub max_block_ex_units: Option<ExUnits>,
#[n(20)]
#[n(19)]
pub max_value_size: Option<u32>,
#[n(21)]
#[n(20)]
pub collateral_percentage: Option<u32>,
#[n(22)]
#[n(21)]
pub max_collateral_inputs: Option<u32>,
}

Expand All @@ -201,19 +200,21 @@ pub struct StakeDistribution {
#[derive(Debug, Encode, Decode, PartialEq, Clone)]
pub struct Pool {
#[n(0)]
pub stakes: Fraction,
pub stakes: RationalNumber,

#[n(1)]
pub hashes: Bytes,
}

/// Type used at [GenesisConfig], which is a fraction that is CBOR-encoded
/// as an untagged array.
#[derive(Debug, Encode, Decode, PartialEq, Clone)]
pub struct Fraction {
#[n(0)]
pub num: u64,

#[n(1)]
pub dem: u64,
pub den: u64,
}

pub type Addr = Bytes;
Expand Down Expand Up @@ -356,7 +357,7 @@ pub struct Stakes {
}

#[derive(Debug, Encode, Decode, PartialEq)]
pub struct Genesis {
pub struct GenesisConfig {
#[n(0)]
pub system_start: SystemStart,

Expand Down Expand Up @@ -509,7 +510,7 @@ pub async fn get_cbor(
pub async fn get_genesis_config(
client: &mut Client,
era: u16,
) -> Result<Vec<Genesis>, ClientError> {
) -> Result<Vec<GenesisConfig>, ClientError> {
let query = BlockQuery::GetGenesisConfig;
let query = LedgerQuery::BlockQuery(era, query);
let query = Request::LedgerQuery(query);
Expand Down
32 changes: 15 additions & 17 deletions pallas-network/tests/protocols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use pallas_network::{
},
};
use pallas_network::miniprotocols::localstate::queries_v16::{
self, Addr, Addrs, ChainBlockNumber, Fraction, Genesis, Snapshots, Stakes,
SystemStart, UnitInterval, Value, StakeAddr,
self, Addr, Addrs, ChainBlockNumber, Fraction, GenesisConfig, Snapshots, Stakes,
SystemStart, UnitInterval, Value, StakeAddr, RationalNumber,
};
use hex::FromHex;

Expand Down Expand Up @@ -551,9 +551,9 @@ pub async fn local_state_query_server_and_client_happy_path() {
);
assert_eq!(*server.statequery().state(), localstate::State::Querying);

let fraction = Fraction { num: 10, dem: 20 };
let pool = queries_v16::Pool {
stakes: fraction.clone(),
let rational = RationalNumber { numerator: 10, denominator: 20 };
let pool = localstate::queries_v16::Pool {
stakes: rational.clone(),
hashes: b"pool1qv4qgv62s3ha74p0643nexee9zvcdydcyahqqnavhj90zheuykz"
.to_vec()
.into(),
Expand Down Expand Up @@ -671,8 +671,7 @@ pub async fn local_state_query_server_and_client_happy_path() {
numerator: 3,
denominator: 1000000,
}),
protocol_version_major: Some(5),
protocol_version_minor: Some(0),
protocol_version: Some((5,0)),
min_pool_cost: Some(AnyUInt::U32(340000000)),
ada_per_utxo_byte: Some(AnyUInt::U16(44)),
cost_models_for_script_languages: None,
Expand Down Expand Up @@ -751,15 +750,15 @@ pub async fn local_state_query_server_and_client_happy_path() {

assert_eq!(*server.statequery().state(), localstate::State::Querying);

let genesis = vec![Genesis {
let genesis = vec![GenesisConfig {
system_start: SystemStart {
year: 2021,
day_of_year: 150,
picoseconds_of_day: 0,
},
network_magic: 42,
network_id: 42,
active_slots_coefficient: Fraction { num: 6, dem: 10 },
active_slots_coefficient: Fraction { num: 6, den: 10 },
security_param: 2160,
epoch_length: 432000,
slots_per_kes_period: 129600,
Expand Down Expand Up @@ -910,9 +909,9 @@ pub async fn local_state_query_server_and_client_happy_path() {
.into_decode()
.unwrap();

let fraction = Fraction { num: 10, dem: 20 };
let pool = queries_v16::Pool {
stakes: fraction.clone(),
let rational = RationalNumber { numerator: 10, denominator: 20 };
let pool = localstate::queries_v16::Pool {
stakes: rational.clone(),
hashes: b"pool1qv4qgv62s3ha74p0643nexee9zvcdydcyahqqnavhj90zheuykz"
.to_vec()
.into(),
Expand Down Expand Up @@ -1024,8 +1023,7 @@ pub async fn local_state_query_server_and_client_happy_path() {
numerator: 3,
denominator: 1000000,
}),
protocol_version_major: Some(5),
protocol_version_minor: Some(0),
protocol_version: Some((5,0)),
min_pool_cost: Some(AnyUInt::U32(340000000)),
ada_per_utxo_byte: Some(AnyUInt::U16(44)),
cost_models_for_script_languages: None,
Expand Down Expand Up @@ -1087,23 +1085,23 @@ pub async fn local_state_query_server_and_client_happy_path() {

client.statequery().send_query(request).await.unwrap();

let result: Vec<Genesis> = client
let result: Vec<GenesisConfig> = client
.statequery()
.recv_while_querying()
.await
.unwrap()
.into_decode()
.unwrap();

let genesis = vec![Genesis {
let genesis = vec![GenesisConfig {
system_start: SystemStart {
year: 2021,
day_of_year: 150,
picoseconds_of_day: 0,
},
network_magic: 42,
network_id: 42,
active_slots_coefficient: Fraction { num: 6, dem: 10 },
active_slots_coefficient: Fraction { num: 6, den: 10 },
security_param: 2160,
epoch_length: 432000,
slots_per_kes_period: 129600,
Expand Down

0 comments on commit 9747be3

Please sign in to comment.