Skip to content

Commit 007be39

Browse files
Seulgi Kimsgkim126
authored andcommitted
Add the json spec of World
1 parent fb4745b commit 007be39

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

json/src/spec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod solo_authority;
2828
mod spec;
2929
mod state;
3030
mod tendermint;
31+
mod world;
3132

3233
pub use self::account::Account;
3334
pub use self::blake_pow::{BlakePoW, BlakePoWParams};
@@ -43,3 +44,4 @@ pub use self::solo_authority::{SoloAuthority, SoloAuthorityParams};
4344
pub use self::spec::Spec;
4445
pub use self::state::{Accounts, Shards};
4546
pub use self::tendermint::{Tendermint, TendermintParams};
47+
pub use self::world::World;

json/src/spec/world.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2018 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use primitives::H160;
18+
19+
use super::super::uint::Uint;
20+
21+
#[derive(Debug, PartialEq, Deserialize)]
22+
pub struct World {
23+
pub nonce: Option<Uint>,
24+
pub owners: Option<Vec<H160>>,
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use serde_json;
30+
31+
use super::*;
32+
33+
#[test]
34+
fn deserialization() {
35+
let s = r#"{
36+
"nonce": 0,
37+
"owners": ["0x01234567890abcdef0123456789abcdef0123456"]
38+
}"#;
39+
let world: World = serde_json::from_str(s).unwrap();
40+
assert_eq!(
41+
World {
42+
nonce: Some(Uint(0.into())),
43+
owners: Some(vec![H160::from("01234567890abcdef0123456789abcdef0123456")]),
44+
},
45+
world
46+
);
47+
}
48+
49+
#[test]
50+
fn with_non_zero_nonce_deserialization() {
51+
let s = r#"{
52+
"nonce": 100,
53+
"owners": ["0x01234567890abcdef0123456789abcdef0123456"]
54+
}"#;
55+
let world: World = serde_json::from_str(s).unwrap();
56+
assert_eq!(
57+
World {
58+
nonce: Some(Uint(100.into())),
59+
owners: Some(vec![H160::from("01234567890abcdef0123456789abcdef0123456")]),
60+
},
61+
world
62+
);
63+
}
64+
65+
66+
#[test]
67+
fn deserialization_of_empty_world() {
68+
let s = r#"{
69+
}"#;
70+
let world: World = serde_json::from_str(s).unwrap();
71+
assert_eq!(
72+
World {
73+
nonce: None,
74+
owners: None,
75+
},
76+
world
77+
);
78+
}
79+
80+
#[test]
81+
fn world_without_nonce_deserialization() {
82+
let s = r#"{
83+
"owners": ["0x01234567890abcdef0123456789abcdef0123456"]
84+
}"#;
85+
let world: World = serde_json::from_str(s).unwrap();
86+
assert_eq!(
87+
World {
88+
nonce: None,
89+
owners: Some(vec![H160::from("01234567890abcdef0123456789abcdef0123456")]),
90+
},
91+
world
92+
);
93+
}
94+
}

0 commit comments

Comments
 (0)