2016-01-14 16:46:32 +01:00
|
|
|
use util::*;
|
|
|
|
use pod_account::*;
|
|
|
|
|
2016-01-25 18:56:36 +01:00
|
|
|
#[derive(Debug,Clone,PartialEq,Eq,Default)]
|
2016-01-19 17:02:01 +01:00
|
|
|
/// TODO [Gav Wood] Please document me
|
2016-01-14 16:46:32 +01:00
|
|
|
pub struct PodState (BTreeMap<Address, PodAccount>);
|
|
|
|
|
|
|
|
impl PodState {
|
|
|
|
/// Contruct a new object from the `m`.
|
2016-01-25 18:56:36 +01:00
|
|
|
pub fn new() -> PodState { Default::default() }
|
|
|
|
|
|
|
|
/// Contruct a new object from the `m`.
|
|
|
|
pub fn from(m: BTreeMap<Address, PodAccount>) -> PodState { PodState(m) }
|
2016-01-14 16:46:32 +01:00
|
|
|
|
2016-01-14 21:58:37 +01:00
|
|
|
/// Get the underlying map.
|
|
|
|
pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 }
|
|
|
|
|
2016-01-25 18:56:36 +01:00
|
|
|
/// Get the root hash of the trie of the RLP of this.
|
|
|
|
pub fn root(&self) -> H256 {
|
|
|
|
sec_trie_root(self.0.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect())
|
|
|
|
}
|
|
|
|
|
2016-01-14 21:58:37 +01:00
|
|
|
/// Drain object to get the underlying map.
|
|
|
|
pub fn drain(self) -> BTreeMap<Address, PodAccount> { self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromJson for PodState {
|
2016-01-14 16:46:32 +01:00
|
|
|
/// Translate the JSON object into a hash map of account information ready for insertion into State.
|
2016-01-14 21:58:37 +01:00
|
|
|
fn from_json(json: &Json) -> PodState {
|
2016-01-14 16:46:32 +01:00
|
|
|
PodState(json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut state, (address, acc)| {
|
2016-01-14 21:23:46 +01:00
|
|
|
let balance = acc.find("balance").map(&U256::from_json);
|
|
|
|
let nonce = acc.find("nonce").map(&U256::from_json);
|
2016-01-14 21:58:37 +01:00
|
|
|
let storage = acc.find("storage").map(&BTreeMap::from_json);
|
|
|
|
let code = acc.find("code").map(&Bytes::from_json);
|
2016-01-14 16:46:32 +01:00
|
|
|
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {
|
|
|
|
state.insert(address_from_hex(address), PodAccount{
|
2016-01-19 13:47:30 +01:00
|
|
|
balance: balance.unwrap_or_else(U256::zero),
|
|
|
|
nonce: nonce.unwrap_or_else(U256::zero),
|
|
|
|
storage: storage.unwrap_or_else(BTreeMap::new),
|
|
|
|
code: code.unwrap_or_else(Vec::new)
|
2016-01-14 16:46:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
state
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for PodState {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
for (add, acc) in &self.0 {
|
|
|
|
try!(writeln!(f, "{} => {}", add, acc));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2016-01-30 12:53:16 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
extern crate rustc_serialize;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use rustc_serialize::*;
|
|
|
|
use util::from_json::FromJson;
|
|
|
|
use util::hash::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_serializes_form_json() {
|
|
|
|
let pod_state = PodState::from_json(&json::Json::from_str(
|
|
|
|
r#"
|
|
|
|
{
|
|
|
|
"0000000000000000000000000000000000000000": {
|
|
|
|
"balance": "1000",
|
|
|
|
"nonce": "100",
|
|
|
|
"storage": {},
|
|
|
|
"code" : []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#
|
|
|
|
).unwrap());
|
|
|
|
|
|
|
|
assert!(pod_state.get().get(&ZERO_ADDRESS).is_some());
|
|
|
|
}
|
|
|
|
}
|