Skip to content

Commit 910dc46

Browse files
authored
Single node rest settings test (#3962)
* Added first test for setting rest api * did some cleaning up * adding test to settings rest api * adding setter functions for reward_parameters and treasury_parameters * adding more parameters to test_custom_settings and did some cleanup * fixing errors after PR * fixing newline errors after PR * adding extensions for Block0Configuration and BlockchainConfiguration * fixing trailing spaces * fixing clippy errors
1 parent fe45259 commit 910dc46

File tree

10 files changed

+196
-6
lines changed

10 files changed

+196
-6
lines changed

jormungandr-lib/src/interfaces/reward_parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use chain_impl_mockchain::{block::Epoch, config::RewardParams as RewardParamsStd
33
use serde::{Deserialize, Serialize};
44
use std::num::NonZeroU32;
55

6-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
6+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Copy)]
77
#[serde(deny_unknown_fields, rename_all = "snake_case")]
88
pub enum RewardParams {
99
Linear {

jormungandr-lib/src/interfaces/tax_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use chain_impl_mockchain::rewards;
33
use serde::{Deserialize, Serialize};
44
use std::num::NonZeroU64;
55

6-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
6+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Copy)]
77
#[serde(deny_unknown_fields, rename_all = "snake_case")]
88
pub struct TaxType {
99
pub fixed: Value,

testing/jormungandr-automation/src/jormungandr/configuration/block0_config_builder.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rand::SeedableRng;
1616
use rand_chacha::ChaChaRng;
1717
use serde_derive::{Deserialize, Serialize};
1818

19-
use std::num::NonZeroU32;
19+
use std::num::{NonZeroU32, NonZeroU64};
2020
use std::vec::Vec;
2121

2222
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -56,7 +56,7 @@ impl Block0ConfigurationBuilder {
5656
treasury_parameters: Some(TaxType {
5757
fixed: 10.into(),
5858
ratio: Ratio::new_checked(1, 1_000).unwrap(),
59-
max_limit: None,
59+
max_limit: NonZeroU64::new(123),
6060
}),
6161
total_reward_supply: Some(1_000_000_000.into()),
6262
reward_parameters: Some(RewardParams::Linear {
@@ -93,18 +93,22 @@ impl Block0ConfigurationBuilder {
9393
self.blockchain_configuration.consensus_leader_ids = leaders_ids;
9494
self
9595
}
96+
9697
pub fn with_block0_consensus(&mut self, block0_consensus: ConsensusVersion) -> &mut Self {
9798
self.blockchain_configuration.block0_consensus = block0_consensus;
9899
self
99100
}
101+
100102
pub fn with_kes_update_speed(&mut self, kes_update_speed: KesUpdateSpeed) -> &mut Self {
101103
self.blockchain_configuration.kes_update_speed = kes_update_speed;
102104
self
103105
}
106+
104107
pub fn with_slots_per_epoch(&mut self, slots_per_epoch: NumberOfSlotsPerEpoch) -> &mut Self {
105108
self.blockchain_configuration.slots_per_epoch = slots_per_epoch;
106109
self
107110
}
111+
108112
pub fn with_slot_duration(&mut self, slot_duration: SlotDuration) -> &mut Self {
109113
self.blockchain_configuration.slot_duration = slot_duration;
110114
self
@@ -122,6 +126,7 @@ impl Block0ConfigurationBuilder {
122126
self.blockchain_configuration.epoch_stability_depth = epoch_stability_depth;
123127
self
124128
}
129+
125130
pub fn with_active_slot_coeff(
126131
&mut self,
127132
consensus_genesis_praos_active_slot_coeff: ActiveSlotCoefficient,
@@ -136,6 +141,11 @@ impl Block0ConfigurationBuilder {
136141
self
137142
}
138143

144+
pub fn with_reward_parameters(&mut self, reward_parameters: Option<RewardParams>) -> &mut Self {
145+
self.blockchain_configuration.reward_parameters = reward_parameters;
146+
self
147+
}
148+
139149
pub fn with_total_rewards_supply(&mut self, total_reward_supply: Option<Value>) -> &mut Self {
140150
self.blockchain_configuration.total_reward_supply = total_reward_supply;
141151
self
@@ -174,6 +184,11 @@ impl Block0ConfigurationBuilder {
174184
self
175185
}
176186

187+
pub fn with_treasury_parameters(&mut self, treasury_parameters: Option<TaxType>) -> &mut Self {
188+
self.blockchain_configuration.treasury_parameters = treasury_parameters;
189+
self
190+
}
191+
177192
pub fn with_tx_max_expiry_epochs(&mut self, tx_max_expiry_epochs: u8) -> &mut Self {
178193
self.blockchain_configuration.tx_max_expiry_epochs = Some(tx_max_expiry_epochs);
179194
self

testing/jormungandr-automation/src/jormungandr/configuration/configuration_builder.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use jormungandr_lib::interfaces::Block0Configuration;
1313
use jormungandr_lib::interfaces::BlockContentMaxSize;
1414
use jormungandr_lib::interfaces::InitialToken;
1515
use jormungandr_lib::interfaces::ProposalExpiration;
16+
use jormungandr_lib::interfaces::RewardParams;
17+
use jormungandr_lib::interfaces::TaxType;
1618
use jormungandr_lib::interfaces::{
1719
ActiveSlotCoefficient, CommitteeIdDef, ConsensusLeaderId, Cors, EpochStabilityDepth, FeesGoTo,
1820
Initial, InitialUTxO, KesUpdateSpeed, Log, LogEntry, LogOutput, Mempool, NodeConfig,
@@ -39,7 +41,9 @@ pub struct ConfigurationBuilder {
3941
secret: Option<NodeSecret>,
4042
fees_go_to: Option<FeesGoTo>,
4143
total_reward_supply: Option<Value>,
44+
reward_parameters: Option<RewardParams>,
4245
treasury: Option<Value>,
46+
treasury_parameters: Option<TaxType>,
4347
node_config_builder: NodeConfigBuilder,
4448
rewards_history: bool,
4549
configure_default_log: bool,
@@ -82,7 +86,9 @@ impl ConfigurationBuilder {
8286
proposal_expiry_epochs: Default::default(),
8387
fees_go_to: None,
8488
treasury: None,
89+
treasury_parameters: None,
8590
total_reward_supply: None,
91+
reward_parameters: None,
8692
discrimination: Discrimination::Test,
8793
block_content_max_size: 4092.into(),
8894
tx_max_expiry_epochs: None,
@@ -294,6 +300,16 @@ impl ConfigurationBuilder {
294300
self
295301
}
296302

303+
pub fn with_treasury_parameters(&mut self, treasury_parameters: TaxType) -> &mut Self {
304+
self.treasury_parameters = Some(treasury_parameters);
305+
self
306+
}
307+
308+
pub fn with_reward_parameters(&mut self, reward_parameters: RewardParams) -> &mut Self {
309+
self.reward_parameters = Some(reward_parameters);
310+
self
311+
}
312+
297313
pub fn with_total_rewards_supply(&mut self, total_reward_supply: Value) -> &mut Self {
298314
self.total_reward_supply = Some(total_reward_supply);
299315
self
@@ -326,6 +342,18 @@ impl ConfigurationBuilder {
326342
.to_owned();
327343
}
328344

345+
if let Some(treasury_parameters) = self.treasury_parameters {
346+
block0_config_builder = block0_config_builder
347+
.with_treasury_parameters(Some(treasury_parameters))
348+
.to_owned();
349+
}
350+
351+
if let Some(reward_parameters) = self.reward_parameters {
352+
block0_config_builder = block0_config_builder
353+
.with_reward_parameters(Some(reward_parameters))
354+
.to_owned();
355+
}
356+
329357
block0_config_builder
330358
.with_discrimination(self.discrimination)
331359
.with_initial(initial)

testing/jormungandr-automation/src/testing/block0.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
use super::blockchain_config::BlockchainConfigurationExtension;
12
use chain_core::{
23
packer::Codec,
34
property::{Deserialize, DeserializeFromSlice, ReadError, Serialize, WriteError},
45
};
56
use chain_impl_mockchain::block::Block;
67
use chain_impl_mockchain::certificate::VotePlan;
78
use chain_impl_mockchain::ledger::Ledger;
8-
use jormungandr_lib::interfaces::Block0Configuration;
9-
use jormungandr_lib::interfaces::Block0ConfigurationError;
109
use jormungandr_lib::interfaces::Initial;
10+
use jormungandr_lib::interfaces::{Block0Configuration, SettingsDto};
11+
use jormungandr_lib::{interfaces::Block0ConfigurationError, time::SystemTime};
1112
use std::io::BufReader;
1213
use std::path::Path;
1314
use thiserror::Error;
@@ -38,6 +39,7 @@ pub fn get_block<S: Into<String>>(block0: S) -> Result<Block0Configuration, Bloc
3839

3940
pub trait Block0ConfigurationExtension {
4041
fn vote_plans(&self) -> Vec<VotePlan>;
42+
fn settings(&self) -> SettingsDto;
4143
}
4244

4345
impl Block0ConfigurationExtension for Block0Configuration {
@@ -54,6 +56,25 @@ impl Block0ConfigurationExtension for Block0Configuration {
5456
}
5557
vote_plans
5658
}
59+
60+
fn settings(&self) -> SettingsDto {
61+
let blockchain_configuration = &self.blockchain_configuration;
62+
SettingsDto {
63+
block0_hash: self.to_block().header().id().to_string(),
64+
block0_time: blockchain_configuration.block0_date.into(),
65+
curr_slot_start_time: Some(SystemTime::from(blockchain_configuration.block0_date)),
66+
consensus_version: blockchain_configuration.block0_consensus.to_string(),
67+
fees: blockchain_configuration.linear_fees,
68+
block_content_max_size: blockchain_configuration.block_content_max_size.into(),
69+
epoch_stability_depth: blockchain_configuration.epoch_stability_depth.into(),
70+
slot_duration: u8::from(blockchain_configuration.slot_duration).into(),
71+
slots_per_epoch: blockchain_configuration.slots_per_epoch.into(),
72+
treasury_tax: blockchain_configuration.treasury_parameters.unwrap().into(),
73+
reward_params: blockchain_configuration.reward_parameters().unwrap(),
74+
discrimination: blockchain_configuration.discrimination,
75+
tx_max_expiry_epochs: blockchain_configuration.tx_max_expiry_epochs.unwrap(),
76+
}
77+
}
5778
}
5879

5980
pub fn read_genesis_yaml<P: AsRef<Path>>(genesis: P) -> Result<Block0Configuration, Block0Error> {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use chain_impl_mockchain::rewards::{CompoundingType, Limit, Parameters};
2+
use jormungandr_lib::interfaces::{BlockchainConfiguration, RewardParams};
3+
4+
pub trait BlockchainConfigurationExtension {
5+
fn reward_parameters(&self) -> Option<Parameters>;
6+
}
7+
8+
impl BlockchainConfigurationExtension for BlockchainConfiguration {
9+
fn reward_parameters(&self) -> Option<Parameters> {
10+
let reward_param = match self.reward_parameters {
11+
None => return None,
12+
Some(r) => r,
13+
};
14+
15+
let reward_drawing = match self.reward_constraints.reward_drawing_limit_max {
16+
None => Limit::None,
17+
Some(r) => Limit::ByStakeAbsolute(r.into()),
18+
};
19+
20+
let pool_participation = self
21+
.reward_constraints
22+
.pool_participation_capping
23+
.map(|p| (p.min, p.max));
24+
25+
match reward_param {
26+
RewardParams::Linear {
27+
constant,
28+
ratio,
29+
epoch_start,
30+
epoch_rate,
31+
} => Some(Parameters {
32+
initial_value: constant,
33+
compounding_ratio: ratio.into(),
34+
compounding_type: CompoundingType::Linear,
35+
epoch_rate,
36+
epoch_start,
37+
reward_drawing_limit_max: reward_drawing,
38+
pool_participation_capping: pool_participation,
39+
}),
40+
RewardParams::Halving {
41+
constant,
42+
ratio,
43+
epoch_start,
44+
epoch_rate,
45+
} => Some(Parameters {
46+
initial_value: constant,
47+
compounding_ratio: ratio.into(),
48+
compounding_type: CompoundingType::Halvening,
49+
epoch_rate,
50+
epoch_start,
51+
reward_drawing_limit_max: reward_drawing,
52+
pool_participation_capping: pool_participation,
53+
}),
54+
}
55+
}
56+
}

testing/jormungandr-automation/src/testing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod asserts;
22
pub mod benchmark;
33
pub mod block0;
4+
pub mod blockchain_config;
45
pub mod collector;
56
pub mod configuration;
67
pub mod keys;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod fragments;
2+
pub mod utils;
23
pub mod votes;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod settings;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use crate::startup;
2+
use chain_impl_mockchain::fee::{LinearFee, PerCertificateFee, PerVoteCertificateFee};
3+
use jormungandr_automation::jormungandr::ConfigurationBuilder;
4+
use jormungandr_automation::testing::block0::Block0ConfigurationExtension;
5+
use jormungandr_lib::interfaces::RewardParams;
6+
use jormungandr_lib::interfaces::{Ratio, TaxType};
7+
use std::num::{NonZeroU32, NonZeroU64};
8+
9+
#[test]
10+
pub fn test_default_settings() {
11+
let alice = thor::Wallet::default();
12+
let bob = thor::Wallet::default();
13+
let (jormungandr, _stake_pools) =
14+
startup::start_stake_pool(&[alice], &[bob], &mut ConfigurationBuilder::new()).unwrap();
15+
16+
let rest_settings = jormungandr.rest().settings().expect("Rest settings error");
17+
let block0_settings = jormungandr.block0_configuration().settings();
18+
assert_eq!(rest_settings, block0_settings);
19+
}
20+
21+
#[test]
22+
pub fn test_custom_settings() {
23+
let alice = thor::Wallet::default();
24+
25+
let mut linear_fees = LinearFee::new(1, 2, 1);
26+
linear_fees.per_certificate_fees(PerCertificateFee::new(
27+
NonZeroU64::new(2),
28+
NonZeroU64::new(3),
29+
NonZeroU64::new(1),
30+
));
31+
32+
linear_fees.per_vote_certificate_fees(PerVoteCertificateFee::new(
33+
NonZeroU64::new(3),
34+
NonZeroU64::new(3),
35+
));
36+
37+
let treasury_parameters = TaxType {
38+
fixed: 200.into(),
39+
ratio: Ratio::new_checked(10, 500).unwrap(),
40+
max_limit: NonZeroU64::new(200),
41+
};
42+
43+
let reward_parameters = RewardParams::Linear {
44+
constant: 500_000,
45+
ratio: Ratio::new_checked(5, 2_00).unwrap(),
46+
epoch_start: 2,
47+
epoch_rate: NonZeroU32::new(4).unwrap(),
48+
};
49+
50+
let jormungandr = startup::start_bft(
51+
vec![&alice],
52+
ConfigurationBuilder::new()
53+
.with_linear_fees(linear_fees)
54+
.with_block_content_max_size(2000.into())
55+
.with_epoch_stability_depth(2000)
56+
.with_slot_duration(1)
57+
.with_slots_per_epoch(6)
58+
.with_treasury_parameters(treasury_parameters)
59+
.with_reward_parameters(reward_parameters)
60+
.with_tx_max_expiry_epochs(50),
61+
)
62+
.unwrap();
63+
64+
let rest_settings = jormungandr.rest().settings().unwrap();
65+
let block0_settings = jormungandr.block0_configuration().settings();
66+
assert_eq!(rest_settings, block0_settings);
67+
}

0 commit comments

Comments
 (0)