1717use cjson:: scheme:: Params ;
1818use ckey:: NetworkId ;
1919use rlp:: { Decodable , DecoderError , Encodable , RlpStream , UntrustedRlp } ;
20+ use std:: ops:: RangeInclusive ;
2021
2122#[ derive( Copy , Clone , Debug , PartialEq ) ]
2223pub struct CommonParams {
@@ -52,6 +53,8 @@ pub struct CommonParams {
5253 max_body_size : usize ,
5354 /// Snapshot creation period in unit of block numbers.
5455 snapshot_period : u64 ,
56+
57+ term_seconds : u64 ,
5558}
5659
5760impl CommonParams {
@@ -127,12 +130,19 @@ impl CommonParams {
127130 pub fn snapshot_period ( & self ) -> u64 {
128131 self . snapshot_period
129132 }
133+ pub fn term_seconds ( & self ) -> u64 {
134+ self . term_seconds
135+ }
130136}
131137
132138impl From < Params > for CommonParams {
133139 fn from ( p : Params ) -> Self {
140+ let mut size = 23 ;
141+ if p. term_seconds . is_some ( ) {
142+ size += 1 ;
143+ }
134144 Self {
135- size : 23 ,
145+ size,
136146 max_extra_data_size : p. max_extra_data_size . into ( ) ,
137147 max_asset_scheme_metadata_size : p. max_asset_scheme_metadata_size . into ( ) ,
138148 max_transfer_metadata_size : p. max_transfer_metadata_size . into ( ) ,
@@ -156,13 +166,15 @@ impl From<Params> for CommonParams {
156166 min_asset_unwrap_ccc_cost : p. min_unwrap_ccc_cost . into ( ) ,
157167 max_body_size : p. max_body_size . into ( ) ,
158168 snapshot_period : p. snapshot_period . into ( ) ,
169+ term_seconds : p. term_seconds . map ( From :: from) . unwrap_or_default ( ) ,
159170 }
160171 }
161172}
162173
163174impl Encodable for CommonParams {
164175 fn rlp_append ( & self , s : & mut RlpStream ) {
165- assert_eq ! ( 23 , self . size) ;
176+ const VALID_SIZE : RangeInclusive < usize > = 23 ..=24 ;
177+ assert ! ( VALID_SIZE . contains( & self . size) , "{} must be in {:?}" , self . size, VALID_SIZE ) ;
166178 s. begin_list ( self . size )
167179 . append ( & self . max_extra_data_size )
168180 . append ( & self . max_asset_scheme_metadata_size )
@@ -187,13 +199,17 @@ impl Encodable for CommonParams {
187199 . append ( & self . min_asset_unwrap_ccc_cost )
188200 . append ( & self . max_body_size )
189201 . append ( & self . snapshot_period ) ;
202+ if self . size == 24 {
203+ s. append ( & self . term_seconds ) ;
204+ }
190205 }
191206}
192207
193208impl Decodable for CommonParams {
194209 fn decode ( rlp : & UntrustedRlp ) -> Result < Self , DecoderError > {
195210 let size = rlp. item_count ( ) ?;
196- if size != 23 {
211+ const VALID_SIZE : RangeInclusive < usize > = 23 ..=24 ;
212+ if !VALID_SIZE . contains ( & size) {
197213 return Err ( DecoderError :: RlpIncorrectListLen {
198214 expected : 23 ,
199215 got : size,
@@ -223,6 +239,11 @@ impl Decodable for CommonParams {
223239 let min_asset_unwrap_ccc_cost = rlp. val_at ( 20 ) ?;
224240 let max_body_size = rlp. val_at ( 21 ) ?;
225241 let snapshot_period = rlp. val_at ( 22 ) ?;
242+ let term_seconds = if size >= 24 {
243+ rlp. val_at ( 23 ) ?
244+ } else {
245+ 0
246+ } ;
226247 Ok ( Self {
227248 size,
228249 max_extra_data_size,
@@ -248,6 +269,7 @@ impl Decodable for CommonParams {
248269 min_asset_unwrap_ccc_cost,
249270 max_body_size,
250271 snapshot_period,
272+ term_seconds,
251273 } )
252274 }
253275}
@@ -282,4 +304,139 @@ mod tests {
282304 fn encode_and_decode_default ( ) {
283305 rlp_encode_and_decode_test ! ( CommonParams :: default_for_test( ) ) ;
284306 }
307+
308+ #[ test]
309+ fn changing_parameters_dont_change_the_rlp_if_the_size_is_not_updated ( ) {
310+ let origin = CommonParams :: default_for_test ( ) ;
311+ let mut params = origin;
312+ params. term_seconds = 100 ;
313+ assert_eq ! ( rlp:: encode( & origin) , rlp:: encode( & params) ) ;
314+ }
315+
316+ #[ test]
317+ fn rlp_with_term_seconds ( ) {
318+ let mut params = CommonParams :: default_for_test ( ) ;
319+ params. size = 24 ;
320+ params. term_seconds = 100 ;
321+ rlp_encode_and_decode_test ! ( params) ;
322+ }
323+
324+ #[ test]
325+ fn rlp_encoding_are_different_if_the_size_are_different ( ) {
326+ let origin = CommonParams :: default_for_test ( ) ;
327+ let mut params = origin;
328+ params. size = 24 ;
329+ assert_ne ! ( rlp:: encode( & origin) , rlp:: encode( & params) ) ;
330+ }
331+
332+ #[ test]
333+ fn params_from_json ( ) {
334+ let s = r#"{
335+ "maxExtraDataSize": "0x20",
336+ "maxAssetSchemeMetadataSize": "0x0400",
337+ "maxTransferMetadataSize": "0x0100",
338+ "maxTextContentSize": "0x0200",
339+ "networkID" : "tc",
340+ "minPayCost" : 10,
341+ "minSetRegularKeyCost" : 11,
342+ "minCreateShardCost" : 12,
343+ "minSetShardOwnersCost" : 13,
344+ "minSetShardUsersCost" : 14,
345+ "minWrapCccCost" : 15,
346+ "minCustomCost" : 16,
347+ "minStoreCost" : 17,
348+ "minRemoveCost" : 18,
349+ "minMintAssetCost" : 19,
350+ "minTransferAssetCost" : 20,
351+ "minChangeAssetSchemeCost" : 21,
352+ "minComposeAssetCost" : 22,
353+ "minDecomposeAssetCost" : 23,
354+ "minUnwrapCccCost" : 24,
355+ "minIncreaseAssetSupplyCost": 25,
356+ "maxBodySize" : 4194304,
357+ "snapshotPeriod": 16384
358+ }"# ;
359+
360+ let deserialized = CommonParams :: from ( serde_json:: from_str :: < Params > ( s) . unwrap ( ) ) ;
361+ assert_eq ! ( deserialized. max_extra_data_size, 0x20 ) ;
362+ assert_eq ! ( deserialized. max_asset_scheme_metadata_size, 0x0400 ) ;
363+ assert_eq ! ( deserialized. max_transfer_metadata_size, 0x0100 ) ;
364+ assert_eq ! ( deserialized. max_text_content_size, 0x0200 ) ;
365+ assert_eq ! ( deserialized. network_id, "tc" . into( ) ) ;
366+ assert_eq ! ( deserialized. min_pay_transaction_cost, 10 ) ;
367+ assert_eq ! ( deserialized. min_set_regular_key_transaction_cost, 11 ) ;
368+ assert_eq ! ( deserialized. min_create_shard_transaction_cost, 12 ) ;
369+ assert_eq ! ( deserialized. min_set_shard_owners_transaction_cost, 13 ) ;
370+ assert_eq ! ( deserialized. min_set_shard_users_transaction_cost, 14 ) ;
371+ assert_eq ! ( deserialized. min_wrap_ccc_transaction_cost, 15 ) ;
372+ assert_eq ! ( deserialized. min_custom_transaction_cost, 16 ) ;
373+ assert_eq ! ( deserialized. min_store_transaction_cost, 17 ) ;
374+ assert_eq ! ( deserialized. min_remove_transaction_cost, 18 ) ;
375+ assert_eq ! ( deserialized. min_asset_mint_cost, 19 ) ;
376+ assert_eq ! ( deserialized. min_asset_transfer_cost, 20 ) ;
377+ assert_eq ! ( deserialized. min_asset_scheme_change_cost, 21 ) ;
378+ assert_eq ! ( deserialized. min_asset_compose_cost, 22 ) ;
379+ assert_eq ! ( deserialized. min_asset_decompose_cost, 23 ) ;
380+ assert_eq ! ( deserialized. min_asset_unwrap_ccc_cost, 24 ) ;
381+ assert_eq ! ( deserialized. min_asset_supply_increase_cost, 25 ) ;
382+ assert_eq ! ( deserialized. max_body_size, 4_194_304 ) ;
383+ assert_eq ! ( deserialized. snapshot_period, 16_384 ) ;
384+ assert_eq ! ( deserialized. term_seconds, 0 ) ;
385+ }
386+
387+ #[ test]
388+ fn params_from_json_with_term_seconds ( ) {
389+ let s = r#"{
390+ "maxExtraDataSize": "0x20",
391+ "maxAssetSchemeMetadataSize": "0x0400",
392+ "maxTransferMetadataSize": "0x0100",
393+ "maxTextContentSize": "0x0200",
394+ "networkID" : "tc",
395+ "minPayCost" : 10,
396+ "minSetRegularKeyCost" : 11,
397+ "minCreateShardCost" : 12,
398+ "minSetShardOwnersCost" : 13,
399+ "minSetShardUsersCost" : 14,
400+ "minWrapCccCost" : 15,
401+ "minCustomCost" : 16,
402+ "minStoreCost" : 17,
403+ "minRemoveCost" : 18,
404+ "minMintAssetCost" : 19,
405+ "minTransferAssetCost" : 20,
406+ "minChangeAssetSchemeCost" : 21,
407+ "minComposeAssetCost" : 22,
408+ "minDecomposeAssetCost" : 23,
409+ "minUnwrapCccCost" : 24,
410+ "minIncreaseAssetSupplyCost": 25,
411+ "maxBodySize" : 4194304,
412+ "snapshotPeriod": 16384,
413+ "termSeconds": 3600
414+ }"# ;
415+
416+ let deserialized = CommonParams :: from ( serde_json:: from_str :: < Params > ( s) . unwrap ( ) ) ;
417+ assert_eq ! ( deserialized. max_extra_data_size, 0x20 ) ;
418+ assert_eq ! ( deserialized. max_asset_scheme_metadata_size, 0x0400 ) ;
419+ assert_eq ! ( deserialized. max_transfer_metadata_size, 0x0100 ) ;
420+ assert_eq ! ( deserialized. max_text_content_size, 0x0200 ) ;
421+ assert_eq ! ( deserialized. network_id, "tc" . into( ) ) ;
422+ assert_eq ! ( deserialized. min_pay_transaction_cost, 10 ) ;
423+ assert_eq ! ( deserialized. min_set_regular_key_transaction_cost, 11 ) ;
424+ assert_eq ! ( deserialized. min_create_shard_transaction_cost, 12 ) ;
425+ assert_eq ! ( deserialized. min_set_shard_owners_transaction_cost, 13 ) ;
426+ assert_eq ! ( deserialized. min_set_shard_users_transaction_cost, 14 ) ;
427+ assert_eq ! ( deserialized. min_wrap_ccc_transaction_cost, 15 ) ;
428+ assert_eq ! ( deserialized. min_custom_transaction_cost, 16 ) ;
429+ assert_eq ! ( deserialized. min_store_transaction_cost, 17 ) ;
430+ assert_eq ! ( deserialized. min_remove_transaction_cost, 18 ) ;
431+ assert_eq ! ( deserialized. min_asset_mint_cost, 19 ) ;
432+ assert_eq ! ( deserialized. min_asset_transfer_cost, 20 ) ;
433+ assert_eq ! ( deserialized. min_asset_scheme_change_cost, 21 ) ;
434+ assert_eq ! ( deserialized. min_asset_compose_cost, 22 ) ;
435+ assert_eq ! ( deserialized. min_asset_decompose_cost, 23 ) ;
436+ assert_eq ! ( deserialized. min_asset_unwrap_ccc_cost, 24 ) ;
437+ assert_eq ! ( deserialized. min_asset_supply_increase_cost, 25 ) ;
438+ assert_eq ! ( deserialized. max_body_size, 4_194_304 ) ;
439+ assert_eq ! ( deserialized. snapshot_period, 16_384 ) ;
440+ assert_eq ! ( deserialized. term_seconds, 3600 ) ;
441+ }
285442}
0 commit comments