Skip to content

Commit ad51e2d

Browse files
foriequal0mergify[bot]
authored andcommitted
Add era to CommonParams
1 parent a48eafe commit ad51e2d

File tree

2 files changed

+201
-11
lines changed

2 files changed

+201
-11
lines changed

json/src/scheme/params.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ pub struct Params {
6666
pub delegation_threshold: Option<Uint>,
6767
pub min_deposit: Option<Uint>,
6868
pub max_candidate_metadata_size: Option<Uint>,
69+
70+
/// A monotonically increasing number to denote the consensus version.
71+
/// It is increased when we fork.
72+
pub era: Option<Uint>,
6973
}
7074

7175
#[cfg(test)]
@@ -277,4 +281,79 @@ mod tests {
277281
assert_eq!(deserialized.min_deposit, Some(32.into()));
278282
assert_eq!(deserialized.max_candidate_metadata_size, Some(33.into()));
279283
}
284+
285+
#[test]
286+
#[allow(clippy::cognitive_complexity)]
287+
fn params_deserialization_with_era() {
288+
let s = r#"{
289+
"maxExtraDataSize": "0x20",
290+
"maxAssetSchemeMetadataSize": "0x0400",
291+
"maxTransferMetadataSize": "0x0100",
292+
"maxTextContentSize": "0x0200",
293+
"networkID" : "tc",
294+
"minPayCost" : 10,
295+
"minSetRegularKeyCost" : 11,
296+
"minCreateShardCost" : 12,
297+
"minSetShardOwnersCost" : 13,
298+
"minSetShardUsersCost" : 14,
299+
"minWrapCccCost" : 15,
300+
"minCustomCost" : 16,
301+
"minStoreCost" : 17,
302+
"minRemoveCost" : 18,
303+
"minMintAssetCost" : 19,
304+
"minTransferAssetCost" : 20,
305+
"minChangeAssetSchemeCost" : 21,
306+
"minComposeAssetCost" : 22,
307+
"minDecomposeAssetCost" : 23,
308+
"minUnwrapCccCost" : 24,
309+
"minIncreaseAssetSupplyCost": 25,
310+
"maxBodySize" : 4194304,
311+
"snapshotPeriod": 16384,
312+
"termSeconds": 3600,
313+
"nominationExpiration": 26,
314+
"custodyPeriod": 27,
315+
"releasePeriod": 28,
316+
"maxNumOfValidators": 29,
317+
"minNumOfValidators": 30,
318+
"delegationThreshold": 31,
319+
"minDeposit": 32,
320+
"maxCandidateMetadataSize": 33,
321+
"era": 34
322+
}"#;
323+
324+
let deserialized: Params = serde_json::from_str(s).unwrap();
325+
assert_eq!(deserialized.max_extra_data_size, 0x20.into());
326+
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400.into());
327+
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100.into());
328+
assert_eq!(deserialized.max_text_content_size, 0x0200.into());
329+
assert_eq!(deserialized.network_id, "tc".into());
330+
assert_eq!(deserialized.min_pay_cost, 10.into());
331+
assert_eq!(deserialized.min_set_regular_key_cost, 11.into());
332+
assert_eq!(deserialized.min_create_shard_cost, 12.into());
333+
assert_eq!(deserialized.min_set_shard_owners_cost, 13.into());
334+
assert_eq!(deserialized.min_set_shard_users_cost, 14.into());
335+
assert_eq!(deserialized.min_wrap_ccc_cost, 15.into());
336+
assert_eq!(deserialized.min_custom_cost, 16.into());
337+
assert_eq!(deserialized.min_store_cost, 17.into());
338+
assert_eq!(deserialized.min_remove_cost, 18.into());
339+
assert_eq!(deserialized.min_mint_asset_cost, 19.into());
340+
assert_eq!(deserialized.min_transfer_asset_cost, 20.into());
341+
assert_eq!(deserialized.min_change_asset_scheme_cost, 21.into());
342+
assert_eq!(deserialized.min_compose_asset_cost, 22.into());
343+
assert_eq!(deserialized.min_decompose_asset_cost, 23.into());
344+
assert_eq!(deserialized.min_unwrap_ccc_cost, 24.into());
345+
assert_eq!(deserialized.min_increase_asset_supply_cost, 25.into());
346+
assert_eq!(deserialized.max_body_size, 4_194_304.into());
347+
assert_eq!(deserialized.snapshot_period, 16_384.into());
348+
assert_eq!(deserialized.term_seconds, Some(3600.into()));
349+
assert_eq!(deserialized.nomination_expiration, Some(26.into()));
350+
assert_eq!(deserialized.custody_period, Some(27.into()));
351+
assert_eq!(deserialized.release_period, Some(28.into()));
352+
assert_eq!(deserialized.max_num_of_validators, Some(29.into()));
353+
assert_eq!(deserialized.min_num_of_validators, Some(30.into()));
354+
assert_eq!(deserialized.delegation_threshold, Some(31.into()));
355+
assert_eq!(deserialized.min_deposit, Some(32.into()));
356+
assert_eq!(deserialized.max_candidate_metadata_size, Some(33.into()));
357+
assert_eq!(deserialized.era, Some(34.into()));
358+
}
280359
}

types/src/common_params.rs

Lines changed: 122 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub struct CommonParams {
6464
delegation_threshold: u64,
6565
min_deposit: u64,
6666
max_candidate_metadata_size: usize,
67+
68+
era: u64,
6769
}
6870

6971
impl CommonParams {
@@ -170,6 +172,10 @@ impl CommonParams {
170172
self.max_candidate_metadata_size
171173
}
172174

175+
pub fn era(&self) -> u64 {
176+
self.era
177+
}
178+
173179
pub fn verify(&self) -> Result<(), String> {
174180
if self.term_seconds != 0 {
175181
if self.nomination_expiration == 0 {
@@ -231,13 +237,21 @@ impl CommonParams {
231237

232238
const DEFAULT_PARAMS_SIZE: usize = 23;
233239
const NUMBER_OF_STAKE_PARAMS: usize = 9;
240+
const NUMBER_OF_ERA_PARAMS: usize = 1;
241+
const STAKE_PARAM_SIZE: usize = DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS;
242+
const ERA_PARAM_SIZE: usize = STAKE_PARAM_SIZE + NUMBER_OF_ERA_PARAMS;
243+
244+
const VALID_SIZE: &[usize] = &[DEFAULT_PARAMS_SIZE, STAKE_PARAM_SIZE, ERA_PARAM_SIZE];
234245

235246
impl From<Params> for CommonParams {
236247
fn from(p: Params) -> Self {
237-
let mut size = DEFAULT_PARAMS_SIZE;
238-
if p.term_seconds.is_some() {
239-
size += NUMBER_OF_STAKE_PARAMS;
240-
}
248+
let size = if p.era.is_some() {
249+
ERA_PARAM_SIZE
250+
} else if p.term_seconds.is_some() {
251+
STAKE_PARAM_SIZE
252+
} else {
253+
DEFAULT_PARAMS_SIZE
254+
};
241255
Self {
242256
size,
243257
max_extra_data_size: p.max_extra_data_size.into(),
@@ -272,6 +286,7 @@ impl From<Params> for CommonParams {
272286
delegation_threshold: p.delegation_threshold.map(From::from).unwrap_or_default(),
273287
min_deposit: p.min_deposit.map(From::from).unwrap_or_default(),
274288
max_candidate_metadata_size: p.max_candidate_metadata_size.map(From::from).unwrap_or_default(),
289+
era: p.era.map(From::from).unwrap_or_default(),
275290
}
276291
}
277292
}
@@ -305,7 +320,7 @@ impl From<CommonParams> for Params {
305320
snapshot_period: p.snapshot_period().into(),
306321
..Default::default()
307322
};
308-
if p.size == DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS {
323+
if p.size >= STAKE_PARAM_SIZE {
309324
result.term_seconds = Some(p.term_seconds().into());
310325
result.nomination_expiration = Some(p.nomination_expiration().into());
311326
result.custody_period = Some(p.custody_period().into());
@@ -316,13 +331,15 @@ impl From<CommonParams> for Params {
316331
result.min_deposit = Some(p.min_deposit().into());
317332
result.max_candidate_metadata_size = Some(p.max_candidate_metadata_size().into());
318333
}
334+
if p.size >= ERA_PARAM_SIZE {
335+
result.era = Some(p.era().into());
336+
}
319337
result
320338
}
321339
}
322340

323341
impl Encodable for CommonParams {
324342
fn rlp_append(&self, s: &mut RlpStream) {
325-
const VALID_SIZE: &[usize] = &[DEFAULT_PARAMS_SIZE, DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS];
326343
assert!(VALID_SIZE.contains(&self.size), "{} must be in {:?}", self.size, VALID_SIZE);
327344
s.begin_list(self.size)
328345
.append(&self.max_extra_data_size)
@@ -348,7 +365,7 @@ impl Encodable for CommonParams {
348365
.append(&self.min_asset_unwrap_ccc_cost)
349366
.append(&self.max_body_size)
350367
.append(&self.snapshot_period);
351-
if self.size == DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS {
368+
if self.size >= STAKE_PARAM_SIZE {
352369
s.append(&self.term_seconds)
353370
.append(&self.nomination_expiration)
354371
.append(&self.custody_period)
@@ -359,13 +376,15 @@ impl Encodable for CommonParams {
359376
.append(&self.min_deposit)
360377
.append(&self.max_candidate_metadata_size);
361378
}
379+
if self.size >= ERA_PARAM_SIZE {
380+
s.append(&self.era);
381+
}
362382
}
363383
}
364384

365385
impl Decodable for CommonParams {
366386
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
367387
let size = rlp.item_count()?;
368-
const VALID_SIZE: &[usize] = &[DEFAULT_PARAMS_SIZE, DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS];
369388
if !VALID_SIZE.contains(&size) {
370389
return Err(DecoderError::RlpIncorrectListLen {
371390
expected: DEFAULT_PARAMS_SIZE,
@@ -407,7 +426,7 @@ impl Decodable for CommonParams {
407426
delegation_threshold,
408427
min_deposit,
409428
max_candidate_metadata_size,
410-
) = if size >= DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS {
429+
) = if size >= STAKE_PARAM_SIZE {
411430
(
412431
rlp.val_at(23)?,
413432
rlp.val_at(24)?,
@@ -422,6 +441,13 @@ impl Decodable for CommonParams {
422441
} else {
423442
Default::default()
424443
};
444+
445+
let era = if size >= ERA_PARAM_SIZE {
446+
rlp.val_at(32)?
447+
} else {
448+
Default::default()
449+
};
450+
425451
Ok(Self {
426452
size,
427453
max_extra_data_size,
@@ -456,6 +482,7 @@ impl Decodable for CommonParams {
456482
delegation_threshold,
457483
min_deposit,
458484
max_candidate_metadata_size,
485+
era,
459486
})
460487
}
461488
}
@@ -527,7 +554,7 @@ mod tests {
527554
#[test]
528555
fn rlp_with_extra_fields() {
529556
let mut params = CommonParams::default_for_test();
530-
params.size = DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS;
557+
params.size = ERA_PARAM_SIZE;
531558
params.term_seconds = 100;
532559
params.min_deposit = 123;
533560
rlp_encode_and_decode_test!(params);
@@ -537,7 +564,7 @@ mod tests {
537564
fn rlp_encoding_are_different_if_the_size_are_different() {
538565
let origin = CommonParams::default_for_test();
539566
let mut params = origin;
540-
params.size = DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS;
567+
params.size = ERA_PARAM_SIZE;
541568
assert_ne!(rlp::encode(&origin), rlp::encode(&params));
542569
}
543570

@@ -604,6 +631,7 @@ mod tests {
604631
assert_eq!(deserialized.delegation_threshold, 0);
605632
assert_eq!(deserialized.min_deposit, 0);
606633
assert_eq!(deserialized.max_candidate_metadata_size, 0);
634+
assert_eq!(deserialized.era, 0);
607635

608636
assert_eq!(params, deserialized.into());
609637
}
@@ -640,6 +668,7 @@ mod tests {
640668

641669
let params = serde_json::from_str::<Params>(s).unwrap();
642670
let deserialized = CommonParams::from(params.clone());
671+
assert_eq!(deserialized.size, STAKE_PARAM_SIZE);
643672
assert_eq!(deserialized.max_extra_data_size, 0x20);
644673
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400);
645674
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100);
@@ -672,6 +701,7 @@ mod tests {
672701
assert_eq!(deserialized.delegation_threshold, 0);
673702
assert_eq!(deserialized.min_deposit, 0);
674703
assert_eq!(deserialized.max_candidate_metadata_size, 0);
704+
assert_eq!(deserialized.era, 0);
675705

676706
assert_eq!(
677707
Params {
@@ -683,6 +713,7 @@ mod tests {
683713
delegation_threshold: Some(0.into()),
684714
min_deposit: Some(0.into()),
685715
max_candidate_metadata_size: Some(0.into()),
716+
era: None,
686717
..params
687718
},
688719
deserialized.into(),
@@ -729,6 +760,85 @@ mod tests {
729760
}"#;
730761
let params = serde_json::from_str::<Params>(s).unwrap();
731762
let deserialized = CommonParams::from(params.clone());
763+
assert_eq!(deserialized.size, STAKE_PARAM_SIZE);
764+
assert_eq!(deserialized.max_extra_data_size, 0x20);
765+
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400);
766+
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100);
767+
assert_eq!(deserialized.max_text_content_size, 0x0200);
768+
assert_eq!(deserialized.network_id, "tc".into());
769+
assert_eq!(deserialized.min_pay_transaction_cost, 10);
770+
assert_eq!(deserialized.min_set_regular_key_transaction_cost, 11);
771+
assert_eq!(deserialized.min_create_shard_transaction_cost, 12);
772+
assert_eq!(deserialized.min_set_shard_owners_transaction_cost, 13);
773+
assert_eq!(deserialized.min_set_shard_users_transaction_cost, 14);
774+
assert_eq!(deserialized.min_wrap_ccc_transaction_cost, 15);
775+
assert_eq!(deserialized.min_custom_transaction_cost, 16);
776+
assert_eq!(deserialized.min_store_transaction_cost, 17);
777+
assert_eq!(deserialized.min_remove_transaction_cost, 18);
778+
assert_eq!(deserialized.min_asset_mint_cost, 19);
779+
assert_eq!(deserialized.min_asset_transfer_cost, 20);
780+
assert_eq!(deserialized.min_asset_scheme_change_cost, 21);
781+
assert_eq!(deserialized.min_asset_compose_cost, 22);
782+
assert_eq!(deserialized.min_asset_decompose_cost, 23);
783+
assert_eq!(deserialized.min_asset_unwrap_ccc_cost, 24);
784+
assert_eq!(deserialized.min_asset_supply_increase_cost, 25);
785+
assert_eq!(deserialized.max_body_size, 4_194_304);
786+
assert_eq!(deserialized.snapshot_period, 16_384);
787+
assert_eq!(deserialized.term_seconds, 3600);
788+
assert_eq!(deserialized.nomination_expiration, 26);
789+
assert_eq!(deserialized.custody_period, 27);
790+
assert_eq!(deserialized.release_period, 28);
791+
assert_eq!(deserialized.max_num_of_validators, 29);
792+
assert_eq!(deserialized.min_num_of_validators, 30);
793+
assert_eq!(deserialized.delegation_threshold, 31);
794+
assert_eq!(deserialized.min_deposit, 32);
795+
assert_eq!(deserialized.max_candidate_metadata_size, 33);
796+
assert_eq!(deserialized.era, 0);
797+
798+
assert_eq!(params, deserialized.into());
799+
}
800+
801+
#[test]
802+
#[allow(clippy::cognitive_complexity)]
803+
fn params_from_json_with_era() {
804+
let s = r#"{
805+
"maxExtraDataSize": "0x20",
806+
"maxAssetSchemeMetadataSize": "0x0400",
807+
"maxTransferMetadataSize": "0x0100",
808+
"maxTextContentSize": "0x0200",
809+
"networkID" : "tc",
810+
"minPayCost" : 10,
811+
"minSetRegularKeyCost" : 11,
812+
"minCreateShardCost" : 12,
813+
"minSetShardOwnersCost" : 13,
814+
"minSetShardUsersCost" : 14,
815+
"minWrapCccCost" : 15,
816+
"minCustomCost" : 16,
817+
"minStoreCost" : 17,
818+
"minRemoveCost" : 18,
819+
"minMintAssetCost" : 19,
820+
"minTransferAssetCost" : 20,
821+
"minChangeAssetSchemeCost" : 21,
822+
"minComposeAssetCost" : 22,
823+
"minDecomposeAssetCost" : 23,
824+
"minUnwrapCccCost" : 24,
825+
"minIncreaseAssetSupplyCost": 25,
826+
"maxBodySize" : 4194304,
827+
"snapshotPeriod": 16384,
828+
"termSeconds": 3600,
829+
"nominationExpiration": 26,
830+
"custodyPeriod": 27,
831+
"releasePeriod": 28,
832+
"maxNumOfValidators": 29,
833+
"minNumOfValidators": 30,
834+
"delegationThreshold": 31,
835+
"minDeposit": 32,
836+
"maxCandidateMetadataSize": 33,
837+
"era": 34
838+
}"#;
839+
let params = serde_json::from_str::<Params>(s).unwrap();
840+
let deserialized = CommonParams::from(params.clone());
841+
assert_eq!(deserialized.size, ERA_PARAM_SIZE);
732842
assert_eq!(deserialized.max_extra_data_size, 0x20);
733843
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400);
734844
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100);
@@ -761,6 +871,7 @@ mod tests {
761871
assert_eq!(deserialized.delegation_threshold, 31);
762872
assert_eq!(deserialized.min_deposit, 32);
763873
assert_eq!(deserialized.max_candidate_metadata_size, 33);
874+
assert_eq!(deserialized.era, 34);
764875

765876
assert_eq!(params, deserialized.into());
766877
}

0 commit comments

Comments
 (0)