2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-15 16:42:49 +02:00
|
|
|
// 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};
|
2017-01-03 17:05:27 +01:00
|
|
|
use basic_account::BasicAccount;
|
2016-09-01 14:29:59 +02:00
|
|
|
use snapshot::Error;
|
|
|
|
|
2017-03-11 19:58:15 +01:00
|
|
|
use util::{U256, H256, Bytes, HashDB, SHA3_EMPTY, SHA3_NULL_RLP};
|
2016-08-24 16:53:36 +02:00
|
|
|
use util::trie::{TrieDB, Trie};
|
2017-03-22 14:41:46 +01:00
|
|
|
use rlp::{RlpStream, UntrustedRlp};
|
2016-06-15 16:42:49 +02:00
|
|
|
|
2016-11-11 17:18:31 +01:00
|
|
|
use std::collections::HashSet;
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
// An empty account -- these were replaced with RLP null data for a space optimization in v1.
|
2017-01-03 17:05:27 +01:00
|
|
|
const ACC_EMPTY: BasicAccount = BasicAccount {
|
2016-10-14 14:45:45 +02:00
|
|
|
nonce: U256([0, 0, 0, 0]),
|
|
|
|
balance: U256([0, 0, 0, 0]),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: SHA3_EMPTY,
|
|
|
|
};
|
|
|
|
|
2016-08-25 14:28:45 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 13:26:20 +02:00
|
|
|
// walk the account's storage trie, returning a vector of RLP items containing the
|
2017-03-28 16:23:09 +02:00
|
|
|
// account address hash, account properties and the storage. Each item contains at most `max_storage_items`
|
2017-03-26 13:26:20 +02:00
|
|
|
// storage records split according to snapshot format definition.
|
2017-03-28 16:23:09 +02:00
|
|
|
pub fn to_fat_rlps(account_hash: &H256, acc: &BasicAccount, acct_db: &AccountDB, used_code: &mut HashSet<H256>, first_chunk_size: usize, max_chunk_size: usize) -> Result<Vec<Bytes>, Error> {
|
2017-01-03 17:05:27 +01:00
|
|
|
let db = TrieDB::new(acct_db, &acc.storage_root)?;
|
2017-03-28 16:23:09 +02:00
|
|
|
let mut chunks = Vec::new();
|
|
|
|
let mut db_iter = db.iter()?;
|
|
|
|
let mut target_chunk_size = first_chunk_size;
|
|
|
|
let mut account_stream = RlpStream::new_list(2);
|
|
|
|
let mut leftover: Option<Vec<u8>> = None;
|
|
|
|
loop {
|
|
|
|
account_stream.append(account_hash);
|
|
|
|
account_stream.begin_list(5);
|
2016-06-15 16:42:49 +02:00
|
|
|
|
2017-03-24 14:02:04 +01:00
|
|
|
account_stream.append(&acc.nonce)
|
|
|
|
.append(&acc.balance);
|
|
|
|
|
|
|
|
// [has_code, code_hash].
|
|
|
|
if acc.code_hash == SHA3_EMPTY {
|
|
|
|
account_stream.append(&CodeState::Empty.raw()).append_empty_data();
|
|
|
|
} else if used_code.contains(&acc.code_hash) {
|
|
|
|
account_stream.append(&CodeState::Hash.raw()).append(&acc.code_hash);
|
|
|
|
} else {
|
|
|
|
match acct_db.get(&acc.code_hash) {
|
|
|
|
Some(c) => {
|
|
|
|
used_code.insert(acc.code_hash.clone());
|
|
|
|
account_stream.append(&CodeState::Inline.raw()).append(&&*c);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
warn!("code lookup failed during snapshot");
|
|
|
|
account_stream.append(&false).append_empty_data();
|
|
|
|
}
|
2016-06-15 16:42:49 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 14:45:45 +02:00
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
account_stream.begin_unbounded_list();
|
|
|
|
if account_stream.len() > target_chunk_size {
|
|
|
|
// account does not fit, push an empty record to mark a new chunk
|
|
|
|
target_chunk_size = max_chunk_size;
|
|
|
|
chunks.push(Vec::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(pair) = leftover.take() {
|
|
|
|
account_stream.append_raw_checked(&pair, 1, target_chunk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match db_iter.next() {
|
|
|
|
Some(Ok((k, v))) => {
|
|
|
|
let pair = {
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
|
|
|
stream.append(&k).append(&&*v);
|
|
|
|
stream.drain()
|
|
|
|
};
|
|
|
|
if !account_stream.append_raw_checked(&pair, 1, target_chunk_size) {
|
|
|
|
account_stream.complete_unbounded_list();
|
|
|
|
let stream = ::std::mem::replace(&mut account_stream, RlpStream::new_list(2));
|
|
|
|
chunks.push(stream.out());
|
|
|
|
target_chunk_size = max_chunk_size;
|
|
|
|
leftover = Some(pair.to_vec());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(Err(e)) => {
|
|
|
|
return Err(e.into());
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
account_stream.complete_unbounded_list();
|
|
|
|
let stream = ::std::mem::replace(&mut account_stream, RlpStream::new_list(2));
|
|
|
|
chunks.push(stream.out());
|
|
|
|
return Ok(chunks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 17:05:27 +01:00
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
// decode a fat rlp, and rebuild the storage trie as we go.
|
|
|
|
// returns the account structure along with its newly recovered code,
|
|
|
|
// if it exists.
|
|
|
|
pub fn from_fat_rlp(
|
|
|
|
acct_db: &mut AccountDBMut,
|
|
|
|
rlp: UntrustedRlp,
|
2017-03-24 14:02:04 +01:00
|
|
|
mut storage_root: H256,
|
2017-01-03 17:05:27 +01:00
|
|
|
) -> Result<(BasicAccount, Option<Bytes>), Error> {
|
|
|
|
use util::{TrieDBMut, TrieMut};
|
|
|
|
|
|
|
|
// check for special case of empty account.
|
|
|
|
if rlp.is_empty() {
|
|
|
|
return Ok((ACC_EMPTY, None));
|
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let nonce = rlp.val_at(0)?;
|
|
|
|
let balance = rlp.val_at(1)?;
|
|
|
|
let code_state: CodeState = {
|
|
|
|
let raw: u8 = rlp.val_at(2)?;
|
|
|
|
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 = rlp.val_at(3)?;
|
|
|
|
let code_hash = acct_db.insert(&code);
|
|
|
|
|
|
|
|
(code_hash, Some(code))
|
|
|
|
}
|
|
|
|
CodeState::Hash => {
|
|
|
|
let code_hash = rlp.val_at(3)?;
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
(code_hash, None)
|
|
|
|
}
|
|
|
|
};
|
2016-06-15 16:42:49 +02:00
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
{
|
2017-03-24 14:02:04 +01:00
|
|
|
let mut storage_trie = if storage_root.is_zero() {
|
|
|
|
TrieDBMut::new(acct_db, &mut storage_root)
|
|
|
|
} else {
|
|
|
|
TrieDBMut::from_existing(acct_db, &mut storage_root)?
|
|
|
|
};
|
2017-01-03 17:05:27 +01:00
|
|
|
let pairs = rlp.at(4)?;
|
|
|
|
for pair_rlp in pairs.iter() {
|
|
|
|
let k: Bytes = pair_rlp.val_at(0)?;
|
|
|
|
let v: Bytes = pair_rlp.val_at(1)?;
|
2016-06-15 16:42:49 +02:00
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
storage_trie.insert(&k, &v)?;
|
2016-06-15 16:42:49 +02:00
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let acc = BasicAccount {
|
|
|
|
nonce: nonce,
|
|
|
|
balance: balance,
|
|
|
|
storage_root: storage_root,
|
|
|
|
code_hash: code_hash,
|
|
|
|
};
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
Ok((acc, new_code))
|
2016-07-11 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use account_db::{AccountDB, AccountDBMut};
|
2017-01-03 17:05:27 +01:00
|
|
|
use basic_account::BasicAccount;
|
2016-09-27 18:02:11 +02:00
|
|
|
use tests::helpers::get_temp_state_db;
|
2016-08-05 17:00:46 +02:00
|
|
|
use snapshot::tests::helpers::fill_storage;
|
2016-07-11 16:54:50 +02:00
|
|
|
|
2016-09-01 14:29:59 +02:00
|
|
|
use util::sha3::{SHA3_EMPTY, SHA3_NULL_RLP};
|
2017-03-28 16:23:09 +02:00
|
|
|
use util::{Address, H256, HashDB, DBValue, Hashable};
|
2017-03-22 14:41:46 +01:00
|
|
|
use rlp::UntrustedRlp;
|
2016-07-11 16:54:50 +02:00
|
|
|
|
2016-11-11 18:24:12 +01:00
|
|
|
use std::collections::HashSet;
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-03-24 14:02:04 +01:00
|
|
|
use super::{ACC_EMPTY, to_fat_rlps, from_fat_rlp};
|
2016-07-11 16:54:50 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_basic() {
|
2016-09-27 18:02:11 +02:00
|
|
|
let mut db = get_temp_state_db();
|
2016-07-11 16:54:50 +02:00
|
|
|
let addr = Address::random();
|
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let account = BasicAccount {
|
2016-07-11 16:54:50 +02:00
|
|
|
nonce: 50.into(),
|
|
|
|
balance: 123456789.into(),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: SHA3_EMPTY,
|
|
|
|
};
|
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let thin_rlp = ::rlp::encode(&account);
|
|
|
|
assert_eq!(::rlp::decode::<BasicAccount>(&thin_rlp), account);
|
2016-07-11 16:54:50 +02:00
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
let fat_rlps = to_fat_rlps(&addr.sha3(), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap();
|
|
|
|
let fat_rlp = UntrustedRlp::new(&fat_rlps[0]).at(1).unwrap();
|
2017-03-24 14:02:04 +01:00
|
|
|
assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account);
|
2016-07-11 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_storage() {
|
2016-09-27 18:02:11 +02:00
|
|
|
let mut db = get_temp_state_db();
|
2016-07-11 16:54:50 +02:00
|
|
|
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());
|
2017-01-03 17:05:27 +01:00
|
|
|
BasicAccount {
|
2016-08-05 17:00:46 +02:00
|
|
|
nonce: 25.into(),
|
|
|
|
balance: 987654321.into(),
|
|
|
|
storage_root: root,
|
|
|
|
code_hash: SHA3_EMPTY,
|
|
|
|
}
|
2016-07-11 16:54:50 +02:00
|
|
|
};
|
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let thin_rlp = ::rlp::encode(&account);
|
|
|
|
assert_eq!(::rlp::decode::<BasicAccount>(&thin_rlp), account);
|
2016-07-11 16:54:50 +02:00
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
let fat_rlp = to_fat_rlps(&addr.sha3(), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap();
|
|
|
|
let fat_rlp = UntrustedRlp::new(&fat_rlp[0]).at(1).unwrap();
|
2017-03-24 14:02:04 +01:00
|
|
|
assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_storage_split() {
|
|
|
|
let mut db = get_temp_state_db();
|
|
|
|
let addr = Address::random();
|
|
|
|
|
|
|
|
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());
|
|
|
|
BasicAccount {
|
|
|
|
nonce: 25.into(),
|
|
|
|
balance: 987654321.into(),
|
|
|
|
storage_root: root,
|
|
|
|
code_hash: SHA3_EMPTY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let thin_rlp = ::rlp::encode(&account);
|
|
|
|
assert_eq!(::rlp::decode::<BasicAccount>(&thin_rlp), account);
|
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
let fat_rlps = to_fat_rlps(&addr.sha3(), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), 500, 1000).unwrap();
|
2017-03-24 14:02:04 +01:00
|
|
|
let mut root = SHA3_NULL_RLP;
|
|
|
|
let mut restored_account = None;
|
|
|
|
for rlp in fat_rlps {
|
2017-03-28 16:23:09 +02:00
|
|
|
let fat_rlp = UntrustedRlp::new(&rlp).at(1).unwrap();
|
2017-03-24 14:02:04 +01:00
|
|
|
restored_account = Some(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, root).unwrap().0);
|
|
|
|
root = restored_account.as_ref().unwrap().storage_root.clone();
|
|
|
|
}
|
|
|
|
assert_eq!(restored_account, Some(account));
|
2016-08-25 14:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_code() {
|
2016-09-27 18:02:11 +02:00
|
|
|
let mut db = get_temp_state_db();
|
2016-08-25 14:28:45 +02:00
|
|
|
|
|
|
|
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);
|
2016-10-26 13:53:47 +02:00
|
|
|
acct_db.emplace(code_hash.clone(), DBValue::from_slice(b"this is definitely code"));
|
2016-08-25 14:28:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let account1 = BasicAccount {
|
2016-08-25 14:28:45 +02:00
|
|
|
nonce: 50.into(),
|
|
|
|
balance: 123456789.into(),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: code_hash,
|
|
|
|
};
|
|
|
|
|
2017-01-03 17:05:27 +01:00
|
|
|
let account2 = BasicAccount {
|
2016-08-25 14:28:45 +02:00
|
|
|
nonce: 400.into(),
|
|
|
|
balance: 98765432123456789usize.into(),
|
|
|
|
storage_root: SHA3_NULL_RLP,
|
|
|
|
code_hash: code_hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut used_code = HashSet::new();
|
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
let fat_rlp1 = to_fat_rlps(&addr1.sha3(), &account1, &AccountDB::new(db.as_hashdb(), &addr1), &mut used_code, usize::max_value(), usize::max_value()).unwrap();
|
|
|
|
let fat_rlp2 = to_fat_rlps(&addr2.sha3(), &account2, &AccountDB::new(db.as_hashdb(), &addr2), &mut used_code, usize::max_value(), usize::max_value()).unwrap();
|
2016-08-25 14:28:45 +02:00
|
|
|
assert_eq!(used_code.len(), 1);
|
|
|
|
|
2017-03-28 16:23:09 +02:00
|
|
|
let fat_rlp1 = UntrustedRlp::new(&fat_rlp1[0]).at(1).unwrap();
|
|
|
|
let fat_rlp2 = UntrustedRlp::new(&fat_rlp2[0]).at(1).unwrap();
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2017-03-24 14:02:04 +01:00
|
|
|
let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr2), fat_rlp2, H256::zero()).unwrap();
|
2016-08-25 14:28:45 +02:00
|
|
|
assert!(maybe_code.is_none());
|
|
|
|
assert_eq!(acc, account2);
|
|
|
|
|
2017-03-24 14:02:04 +01:00
|
|
|
let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr1), fat_rlp1, H256::zero()).unwrap();
|
2016-08-25 14:28:45 +02:00
|
|
|
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-10-14 14:45:45 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encoding_empty_acc() {
|
|
|
|
let mut db = get_temp_state_db();
|
2017-03-24 14:02:04 +01:00
|
|
|
assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &Address::default()), UntrustedRlp::new(&::rlp::NULL_RLP), H256::zero()).unwrap(), (ACC_EMPTY, None));
|
2016-10-14 14:45:45 +02:00
|
|
|
}
|
2016-08-03 18:05:17 +02:00
|
|
|
}
|