Skip to content

Commit 29a732b

Browse files
committed
Add era to CommonParams
1 parent 1768f75 commit 29a732b

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 {
@@ -218,13 +224,21 @@ impl CommonParams {
218224

219225
const DEFAULT_PARAMS_SIZE: usize = 23;
220226
const NUMBER_OF_STAKE_PARAMS: usize = 9;
227+
const NUMBER_OF_ERA_PARAMS: usize = 1;
228+
const STAKE_PARAM_SIZE: usize = DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS;
229+
const ERA_PARAM_SIZE: usize = STAKE_PARAM_SIZE + NUMBER_OF_ERA_PARAMS;
230+
231+
const VALID_SIZE: &[usize] = &[DEFAULT_PARAMS_SIZE, STAKE_PARAM_SIZE, ERA_PARAM_SIZE];
221232

222233
impl From<Params> for CommonParams {
223234
fn from(p: Params) -> Self {
224-
let mut size = DEFAULT_PARAMS_SIZE;
225-
if p.term_seconds.is_some() {
226-
size += NUMBER_OF_STAKE_PARAMS;
227-
}
235+
let size = if p.era.is_some() {
236+
ERA_PARAM_SIZE
237+
} else if p.term_seconds.is_some() {
238+
STAKE_PARAM_SIZE
239+
} else {
240+
DEFAULT_PARAMS_SIZE
241+
};
228242
Self {
229243
size,
230244
max_extra_data_size: p.max_extra_data_size.into(),
@@ -259,6 +273,7 @@ impl From<Params> for CommonParams {
259273
delegation_threshold: p.delegation_threshold.map(From::from).unwrap_or_default(),
260274
min_deposit: p.min_deposit.map(From::from).unwrap_or_default(),
261275
max_candidate_metadata_size: p.max_candidate_metadata_size.map(From::from).unwrap_or_default(),
276+
era: p.era.map(From::from).unwrap_or_default(),
262277
}
263278
}
264279
}
@@ -292,7 +307,7 @@ impl From<CommonParams> for Params {
292307
snapshot_period: p.snapshot_period().into(),
293308
..Default::default()
294309
};
295-
if p.size == DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS {
310+
if p.size >= STAKE_PARAM_SIZE {
296311
result.term_seconds = Some(p.term_seconds().into());
297312
result.nomination_expiration = Some(p.nomination_expiration().into());
298313
result.custody_period = Some(p.custody_period().into());
@@ -303,13 +318,15 @@ impl From<CommonParams> for Params {
303318
result.min_deposit = Some(p.min_deposit().into());
304319
result.max_candidate_metadata_size = Some(p.max_candidate_metadata_size().into());
305320
}
321+
if p.size >= ERA_PARAM_SIZE {
322+
result.era = Some(p.era().into());
323+
}
306324
result
307325
}
308326
}
309327

310328
impl Encodable for CommonParams {
311329
fn rlp_append(&self, s: &mut RlpStream) {
312-
const VALID_SIZE: &[usize] = &[DEFAULT_PARAMS_SIZE, DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS];
313330
assert!(VALID_SIZE.contains(&self.size), "{} must be in {:?}", self.size, VALID_SIZE);
314331
s.begin_list(self.size)
315332
.append(&self.max_extra_data_size)
@@ -335,7 +352,7 @@ impl Encodable for CommonParams {
335352
.append(&self.min_asset_unwrap_ccc_cost)
336353
.append(&self.max_body_size)
337354
.append(&self.snapshot_period);
338-
if self.size == DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS {
355+
if self.size >= STAKE_PARAM_SIZE {
339356
s.append(&self.term_seconds)
340357
.append(&self.nomination_expiration)
341358
.append(&self.custody_period)
@@ -346,13 +363,15 @@ impl Encodable for CommonParams {
346363
.append(&self.min_deposit)
347364
.append(&self.max_candidate_metadata_size);
348365
}
366+
if self.size >= ERA_PARAM_SIZE {
367+
s.append(&self.era);
368+
}
349369
}
350370
}
351371

352372
impl Decodable for CommonParams {
353373
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
354374
let size = rlp.item_count()?;
355-
const VALID_SIZE: &[usize] = &[DEFAULT_PARAMS_SIZE, DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS];
356375
if !VALID_SIZE.contains(&size) {
357376
return Err(DecoderError::RlpIncorrectListLen {
358377
expected: DEFAULT_PARAMS_SIZE,
@@ -394,7 +413,7 @@ impl Decodable for CommonParams {
394413
delegation_threshold,
395414
min_deposit,
396415
max_candidate_metadata_size,
397-
) = if size >= DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS {
416+
) = if size >= STAKE_PARAM_SIZE {
398417
(
399418
rlp.val_at(23)?,
400419
rlp.val_at(24)?,
@@ -409,6 +428,13 @@ impl Decodable for CommonParams {
409428
} else {
410429
Default::default()
411430
};
431+
432+
let era = if size >= ERA_PARAM_SIZE {
433+
rlp.val_at(32)?
434+
} else {
435+
Default::default()
436+
};
437+
412438
Ok(Self {
413439
size,
414440
max_extra_data_size,
@@ -443,6 +469,7 @@ impl Decodable for CommonParams {
443469
delegation_threshold,
444470
min_deposit,
445471
max_candidate_metadata_size,
472+
era,
446473
})
447474
}
448475
}
@@ -514,7 +541,7 @@ mod tests {
514541
#[test]
515542
fn rlp_with_extra_fields() {
516543
let mut params = CommonParams::default_for_test();
517-
params.size = DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS;
544+
params.size = ERA_PARAM_SIZE;
518545
params.term_seconds = 100;
519546
params.min_deposit = 123;
520547
rlp_encode_and_decode_test!(params);
@@ -524,7 +551,7 @@ mod tests {
524551
fn rlp_encoding_are_different_if_the_size_are_different() {
525552
let origin = CommonParams::default_for_test();
526553
let mut params = origin;
527-
params.size = DEFAULT_PARAMS_SIZE + NUMBER_OF_STAKE_PARAMS;
554+
params.size = ERA_PARAM_SIZE;
528555
assert_ne!(rlp::encode(&origin), rlp::encode(&params));
529556
}
530557

@@ -591,6 +618,7 @@ mod tests {
591618
assert_eq!(deserialized.delegation_threshold, 0);
592619
assert_eq!(deserialized.min_deposit, 0);
593620
assert_eq!(deserialized.max_candidate_metadata_size, 0);
621+
assert_eq!(deserialized.era, 0);
594622

595623
assert_eq!(params, deserialized.into());
596624
}
@@ -627,6 +655,7 @@ mod tests {
627655

628656
let params = serde_json::from_str::<Params>(s).unwrap();
629657
let deserialized = CommonParams::from(params.clone());
658+
assert_eq!(deserialized.size, STAKE_PARAM_SIZE);
630659
assert_eq!(deserialized.max_extra_data_size, 0x20);
631660
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400);
632661
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100);
@@ -659,6 +688,7 @@ mod tests {
659688
assert_eq!(deserialized.delegation_threshold, 0);
660689
assert_eq!(deserialized.min_deposit, 0);
661690
assert_eq!(deserialized.max_candidate_metadata_size, 0);
691+
assert_eq!(deserialized.era, 0);
662692

663693
assert_eq!(
664694
Params {
@@ -670,6 +700,7 @@ mod tests {
670700
delegation_threshold: Some(0.into()),
671701
min_deposit: Some(0.into()),
672702
max_candidate_metadata_size: Some(0.into()),
703+
era: None,
673704
..params
674705
},
675706
deserialized.into(),
@@ -716,6 +747,85 @@ mod tests {
716747
}"#;
717748
let params = serde_json::from_str::<Params>(s).unwrap();
718749
let deserialized = CommonParams::from(params.clone());
750+
assert_eq!(deserialized.size, STAKE_PARAM_SIZE);
751+
assert_eq!(deserialized.max_extra_data_size, 0x20);
752+
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400);
753+
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100);
754+
assert_eq!(deserialized.max_text_content_size, 0x0200);
755+
assert_eq!(deserialized.network_id, "tc".into());
756+
assert_eq!(deserialized.min_pay_transaction_cost, 10);
757+
assert_eq!(deserialized.min_set_regular_key_transaction_cost, 11);
758+
assert_eq!(deserialized.min_create_shard_transaction_cost, 12);
759+
assert_eq!(deserialized.min_set_shard_owners_transaction_cost, 13);
760+
assert_eq!(deserialized.min_set_shard_users_transaction_cost, 14);
761+
assert_eq!(deserialized.min_wrap_ccc_transaction_cost, 15);
762+
assert_eq!(deserialized.min_custom_transaction_cost, 16);
763+
assert_eq!(deserialized.min_store_transaction_cost, 17);
764+
assert_eq!(deserialized.min_remove_transaction_cost, 18);
765+
assert_eq!(deserialized.min_asset_mint_cost, 19);
766+
assert_eq!(deserialized.min_asset_transfer_cost, 20);
767+
assert_eq!(deserialized.min_asset_scheme_change_cost, 21);
768+
assert_eq!(deserialized.min_asset_compose_cost, 22);
769+
assert_eq!(deserialized.min_asset_decompose_cost, 23);
770+
assert_eq!(deserialized.min_asset_unwrap_ccc_cost, 24);
771+
assert_eq!(deserialized.min_asset_supply_increase_cost, 25);
772+
assert_eq!(deserialized.max_body_size, 4_194_304);
773+
assert_eq!(deserialized.snapshot_period, 16_384);
774+
assert_eq!(deserialized.term_seconds, 3600);
775+
assert_eq!(deserialized.nomination_expiration, 26);
776+
assert_eq!(deserialized.custody_period, 27);
777+
assert_eq!(deserialized.release_period, 28);
778+
assert_eq!(deserialized.max_num_of_validators, 29);
779+
assert_eq!(deserialized.min_num_of_validators, 30);
780+
assert_eq!(deserialized.delegation_threshold, 31);
781+
assert_eq!(deserialized.min_deposit, 32);
782+
assert_eq!(deserialized.max_candidate_metadata_size, 33);
783+
assert_eq!(deserialized.era, 0);
784+
785+
assert_eq!(params, deserialized.into());
786+
}
787+
788+
#[test]
789+
#[allow(clippy::cognitive_complexity)]
790+
fn params_from_json_with_era() {
791+
let s = r#"{
792+
"maxExtraDataSize": "0x20",
793+
"maxAssetSchemeMetadataSize": "0x0400",
794+
"maxTransferMetadataSize": "0x0100",
795+
"maxTextContentSize": "0x0200",
796+
"networkID" : "tc",
797+
"minPayCost" : 10,
798+
"minSetRegularKeyCost" : 11,
799+
"minCreateShardCost" : 12,
800+
"minSetShardOwnersCost" : 13,
801+
"minSetShardUsersCost" : 14,
802+
"minWrapCccCost" : 15,
803+
"minCustomCost" : 16,
804+
"minStoreCost" : 17,
805+
"minRemoveCost" : 18,
806+
"minMintAssetCost" : 19,
807+
"minTransferAssetCost" : 20,
808+
"minChangeAssetSchemeCost" : 21,
809+
"minComposeAssetCost" : 22,
810+
"minDecomposeAssetCost" : 23,
811+
"minUnwrapCccCost" : 24,
812+
"minIncreaseAssetSupplyCost": 25,
813+
"maxBodySize" : 4194304,
814+
"snapshotPeriod": 16384,
815+
"termSeconds": 3600,
816+
"nominationExpiration": 26,
817+
"custodyPeriod": 27,
818+
"releasePeriod": 28,
819+
"maxNumOfValidators": 29,
820+
"minNumOfValidators": 30,
821+
"delegationThreshold": 31,
822+
"minDeposit": 32,
823+
"maxCandidateMetadataSize": 33,
824+
"era": 34
825+
}"#;
826+
let params = serde_json::from_str::<Params>(s).unwrap();
827+
let deserialized = CommonParams::from(params.clone());
828+
assert_eq!(deserialized.size, ERA_PARAM_SIZE);
719829
assert_eq!(deserialized.max_extra_data_size, 0x20);
720830
assert_eq!(deserialized.max_asset_scheme_metadata_size, 0x0400);
721831
assert_eq!(deserialized.max_transfer_metadata_size, 0x0100);
@@ -748,6 +858,7 @@ mod tests {
748858
assert_eq!(deserialized.delegation_threshold, 31);
749859
assert_eq!(deserialized.min_deposit, 32);
750860
assert_eq!(deserialized.max_candidate_metadata_size, 33);
861+
assert_eq!(deserialized.era, 34);
751862

752863
assert_eq!(params, deserialized.into());
753864
}

0 commit comments

Comments
 (0)