2016-06-15 16:42:49 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Account state encoding and decoding
|
|
|
|
|
|
|
|
use account_db::{AccountDB, AccountDBMut};
|
2016-08-24 16:53:36 +02:00
|
|
|
use util::{U256, FixedHash, H256, Bytes, HashDB, SHA3_EMPTY};
|
2016-08-03 18:35:48 +02:00
|
|
|
use util::rlp::{Rlp, RlpStream, Stream, UntrustedRlp, View};
|
2016-08-24 16:53:36 +02:00
|
|
|
use util::trie::{TrieDB, Trie};
|
2016-08-06 00:03:07 +02:00
|
|
|
use snapshot::Error;
|
2016-06-15 16:42:49 +02:00
|
|
|
|
2016-08-25 14:28:45 +02:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
|
|
|
// whether an encoded account has code and how it is referred to.
|
|
|
|
#[repr(u8)]
|
|
|
|
enum CodeState {
|
|
|
|
// the account has no code.
|
|
|
|
Empty = 0,
|
|
|
|
// raw code is encoded.
|
|
|
|
Inline = 1,
|
|
|
|
// the code is referred to by hash.
|
|
|
|
Hash = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CodeState {
|
|
|
|
fn from(x: u8) -> Result<Self, Error> {
|
|
|
|
match x {
|
|
|
|
0 => Ok(CodeState::Empty),
|
|
|
|
1 => Ok(CodeState::Inline),
|
|
|
|
2 => Ok(CodeState::Hash),
|
|
|
|
_ => Err(Error::UnrecognizedCodeState(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raw(self) -> u8 {
|
|
|
|
self as u8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 16:42:49 +02:00
|
|
|
// An alternate account structure from ::account::Account.
|
2016-07-11 16:54:50 +02:00
|
|
|
#[derive(PartialEq, Clone, Debug)]
|
2016-06-15 16:42:49 +02:00
|
|
|
pub struct Account {
|
|
|
|
nonce: U256,
|
|
|
|
balance: U256,
|
|
|
|
storage_root: H256,
|
|
|
|
code_hash: H256,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Account {
|
|
|
|
// decode the account from rlp.
|
|
|
|
pub fn from_thin_rlp(rlp: &[u8]) -> Self {
|
|
|
|
let r: Rlp = Rlp::new(rlp);
|
|
|
|
|
|
|
|
Account {
|
|
|
|
nonce: r.val_at(0),
|
|
|
|
balance: r.val_at(1),
|
|
|
|
storage_root: r.val_at(2),
|
|
|
|
code_hash: r.val_at(3),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode the account to a standard rlp.
|
|
|
|
pub fn to_thin_rlp(&self) -> Bytes {
|
|
|
|
let mut stream = RlpStream::new_list(4);
|
|
|
|
stream
|
|
|
|
.append(&self.nonce)
|
|
|
|
.append(&self.balance)
|
|
|
|
.append(&self.storage_root)
|
|
|
|
.append(&self.code_hash);
|
|
|
|
|
|
|
|
stream.out()
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk the account's storage trie, returning an RLP item containing the
|
|
|
|
// account properties and the storage.
|
2016-08-25 14:28:45 +02:00
|
|
|
pub fn to_fat_rlp(&self, acct_db: &AccountDB, used_code: &mut HashSet<H256>) -> Result<Bytes, Error> {
|
2016-06-15 16:42:49 +02:00
|
|
|
let db = try!(TrieDB::new(acct_db, &self.storage_root));
|
|
|
|
|
|
|
|
let mut pairs = Vec::new();
|
|
|
|
|
|
|
|
for (k, v) in db.iter() {
|
|
|
|
pairs.push((k, v));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut stream = RlpStream::new_list(pairs.len());
|
|
|
|
|
|
|
|
for (k, v) in pairs {
|
|
|
|
stream.begin_list(2).append(&k).append(&v);
|
|
|
|
}
|
|
|
|
|
|
|
|
let pairs_rlp = stream.out();
|
|
|
|
|
|
|
|
let mut account_stream = RlpStream::new_list(5);
|
|
|
|
account_stream.append(&self.nonce)
|
|
|
|
.append(&self.balance);
|
|
|
|
|
|
|
|
// [has_code, code_hash].
|
|
|
|
if self.code_hash == SHA3_EMPTY {
|
2016-08-25 14:28:45 +02:00
|
|
|
account_stream.append(&CodeState::Empty.raw()).append_empty_data();
|
|
|
|
} else if used_code.contains(&self.code_hash) {
|
|
|
|
account_stream.append(&CodeState::Hash.raw()).append(&self.code_hash);
|
2016-06-15 16:42:49 +02:00
|
|
|
} else {
|
2016-06-29 14:46:29 +02:00
|
|
|
match acct_db.get(&self.code_hash) {
|
2016-06-15 16:42:49 +02:00
|
|
|
Some(c) => {
|
2016-08-25 14:28:45 +02:00
|
|
|
used_code.insert(self.code_hash.clone());
|
|
|
|
account_stream.append(&CodeState::Inline.raw()).append(&c);
|
2016-06-15 16:42:49 +02:00
|
|
|
}
|
|
|
|
None => {
|
2016-07-11 16:54:50 +02:00
|
|
|
warn!("code lookup failed during snapshot");
|
2016-06-15 16:42:49 +02:00
|
|
|
account_stream.append(&false).append_empty_data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:47:05 +02:00
|
|
|
account_stream.append_raw(&pairs_rlp, 1);
|
2016-06-15 16:42:49 +02:00
|
|
|
|
|
|
|
Ok(account_stream.out())
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode a fat rlp, and rebuild the storage trie as we go.
|
2016-08-25 14:28:45 +02:00
|
|
|
// returns the account structure along with its newly recovered code,
|
|
|
|
// if it exists.
|
|
|
|
pub fn from_fat_rlp(
|
|
|
|
acct_db: &mut AccountDBMut,
|
|
|
|
rlp: UntrustedRlp,
|
|
|
|
code_map: &HashMap<H256, Bytes>,
|
|
|
|
) -> Result<(Self, Option<Bytes>), Error> {
|
2016-06-15 16:42:49 +02:00
|
|
|
use util::{TrieDBMut, TrieMut};
|
|
|
|
|
|
|
|
let nonce = try!(rlp.val_at(0));
|
|
|
|
let balance = try!(rlp.val_at(1));
|
2016-08-25 14:28:45 +02:00
|
|
|
let code_state: CodeState = {
|
|
|
|
let raw: u8 = try!(rlp.val_at(2));
|
|
|
|
try!(CodeState::from(raw))
|
|
|
|
};
|
|
|
|
|
|
|
|
// load the code if it exists.
|
|
|
|
let (code_hash, new_code) = match code_state {
|
|
|
|
CodeState::Empty => (SHA3_EMPTY, None),
|
|
|
|
CodeState::Inline => {
|
|
|
|
let code: Bytes = try!(rlp.val_at(3));
|
|
|
|
let code_hash = acct_db.insert(&code);
|
|
|
|
|
|
|
|
(code_hash, Some(code))
|
|
|
|
}
|
|
|
|
CodeState::Hash => {
|
|
|
|
let code_hash = try!(rlp.val_at(3));
|
|
|
|
if let Some(code) = code_map.get(&code_hash) {
|
|
|
|
acct_db.emplace(code_hash.clone(), code.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
(code_hash, None)
|
|
|
|
}
|
2016-06-15 16:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut storage_root = H256::zero();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut storage_trie = TrieDBMut::new(acct_db, &mut storage_root);
|
|
|
|
let pairs = try!(rlp.at(4));
|
|
|
|
for pair_rlp in pairs.iter() {
|
|
|
|
let k: Bytes = try!(pair_rlp.val_at(0));
|
|
|
|
let v: Bytes = try!(pair_rlp.val_at(1));
|
|
|
|
|
2016-08-03 18:35:48 +02:00
|
|
|
try!(storage_trie.insert(&k, &v));
|
2016-06-15 16:42:49 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
|
|
|
let acc = Account {
|
2016-06-15 16:42:49 +02:00
|
|
|
nonce: nonce,
|
|
|
|
balance: balance,
|
|
|
|
storage_root: storage_root,
|
|
|
|
code_hash: code_hash,
|
2016-08-25 14:28:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((acc, new_code))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the account's code hash.
|
|
|
|
pub fn code_hash(&self) -> &H256 {
|
|
|
|
&self.code_hash
|
2016-06-15 16:42:49 +02:00
|
|
|
}
|
2016-08-05 17:00:46 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn storage_root_mut(&mut self) -> &mut H256 {
|
|
|
|
&mut self.storage_root
|
|
|
|
}
|
2016-07-11 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use account_db::{AccountDB, AccountDBMut};
|
|
|
|
use tests::helpers::get_temp_journal_db;
|
2016-08-05 17:00:46 +02:00
|
|
|
use snapshot::tests::helpers::fill_storage;
|
2016-07-11 16:54:50 +02:00
|
|
|
|
|
|
|
use util::{SHA3_NULL_RLP, SHA3_EMPTY};
|
2016-08-25 14:28:45 +02:00
|
|
|
use util::{Address, FixedHash, H256, HashDB};
|
2016-07-11 16:54:50 +02:00
|
|
|
use util::rlp::{UntrustedRlp, View};
|
|
|
|
|
2016-08-25 14:28:45 +02:00
|
|
|
use std::collections::{HashSet, HashMap};
|
|
|
|
|
2016-07-11 16:54:50 +02:00
|
|
|
use super::Account;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_basic() {
|
|
|
|
let mut db = get_temp_journal_db();
|
|
|
|
let mut db = &mut **db;
|
|
|
|
let addr = Address::random();
|
|
|
|
|
|
|
|
let account = Account {
|
|
|
|
nonce: 50.into(),
|
|
|
|
balance: 123456789.into(),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: SHA3_EMPTY,
|
|
|
|
};
|
|
|
|
|
|
|
|
let thin_rlp = account.to_thin_rlp();
|
|
|
|
assert_eq!(Account::from_thin_rlp(&thin_rlp), account);
|
|
|
|
|
2016-08-25 14:28:45 +02:00
|
|
|
let fat_rlp = account.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr), &mut Default::default()).unwrap();
|
2016-07-11 16:54:50 +02:00
|
|
|
let fat_rlp = UntrustedRlp::new(&fat_rlp);
|
2016-08-25 14:28:45 +02:00
|
|
|
assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, &Default::default()).unwrap().0, account);
|
2016-07-11 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_storage() {
|
|
|
|
let mut db = get_temp_journal_db();
|
|
|
|
let mut db = &mut **db;
|
|
|
|
let addr = Address::random();
|
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
let account = {
|
|
|
|
let acct_db = AccountDBMut::new(db.as_hashdb_mut(), &addr);
|
|
|
|
let mut root = SHA3_NULL_RLP;
|
|
|
|
fill_storage(acct_db, &mut root, &mut H256::zero());
|
|
|
|
Account {
|
|
|
|
nonce: 25.into(),
|
|
|
|
balance: 987654321.into(),
|
|
|
|
storage_root: root,
|
|
|
|
code_hash: SHA3_EMPTY,
|
|
|
|
}
|
2016-07-11 16:54:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let thin_rlp = account.to_thin_rlp();
|
|
|
|
assert_eq!(Account::from_thin_rlp(&thin_rlp), account);
|
|
|
|
|
2016-08-25 14:28:45 +02:00
|
|
|
let fat_rlp = account.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr), &mut Default::default()).unwrap();
|
2016-07-11 16:54:50 +02:00
|
|
|
let fat_rlp = UntrustedRlp::new(&fat_rlp);
|
2016-08-25 14:28:45 +02:00
|
|
|
assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, &Default::default()).unwrap().0, account);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_code() {
|
|
|
|
let mut db = get_temp_journal_db();
|
|
|
|
let mut db = &mut **db;
|
|
|
|
|
|
|
|
let addr1 = Address::random();
|
|
|
|
let addr2 = Address::random();
|
|
|
|
|
|
|
|
let code_hash = {
|
|
|
|
let mut acct_db = AccountDBMut::new(db.as_hashdb_mut(), &addr1);
|
|
|
|
acct_db.insert(b"this is definitely code")
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut acct_db = AccountDBMut::new(db.as_hashdb_mut(), &addr2);
|
|
|
|
acct_db.emplace(code_hash.clone(), b"this is definitely code".to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
let account1 = Account {
|
|
|
|
nonce: 50.into(),
|
|
|
|
balance: 123456789.into(),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: code_hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
let account2 = Account {
|
|
|
|
nonce: 400.into(),
|
|
|
|
balance: 98765432123456789usize.into(),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: code_hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut used_code = HashSet::new();
|
|
|
|
|
|
|
|
let fat_rlp1 = account1.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr1), &mut used_code).unwrap();
|
|
|
|
let fat_rlp2 = account2.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr2), &mut used_code).unwrap();
|
|
|
|
assert_eq!(used_code.len(), 1);
|
|
|
|
|
|
|
|
let fat_rlp1 = UntrustedRlp::new(&fat_rlp1);
|
|
|
|
let fat_rlp2 = UntrustedRlp::new(&fat_rlp2);
|
|
|
|
|
|
|
|
let code_map = HashMap::new();
|
|
|
|
let (acc, maybe_code) = Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr2), fat_rlp2, &code_map).unwrap();
|
|
|
|
assert!(maybe_code.is_none());
|
|
|
|
assert_eq!(acc, account2);
|
|
|
|
|
|
|
|
let (acc, maybe_code) = Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr1), fat_rlp1, &code_map).unwrap();
|
|
|
|
assert_eq!(maybe_code, Some(b"this is definitely code".to_vec()));
|
|
|
|
assert_eq!(acc, account1);
|
2016-07-11 16:54:50 +02:00
|
|
|
}
|
2016-08-03 18:05:17 +02:00
|
|
|
}
|