rename AccountReader to Account, give a separate module
This commit is contained in:
parent
a2bb3f2832
commit
d7498c1dd5
139
ethcore/src/snapshot/account.rs
Normal file
139
ethcore/src/snapshot/account.rs
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
// 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};
|
||||||
|
use client::BlockChainClient;
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
|
use util::{Bytes, HashDB, SHA3_EMPTY, TrieDB};
|
||||||
|
use util::hash::{FixedHash, H256};
|
||||||
|
use util::numbers::U256;
|
||||||
|
use util::rlp::{DecoderError, Rlp, RlpStream, Stream, UntrustedRlp, View};
|
||||||
|
|
||||||
|
// An alternate account structure from ::account::Account.
|
||||||
|
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.
|
||||||
|
pub fn to_fat_rlp(&self, acct_db: &AccountDB, addr_hash: H256) -> Result<Bytes, Error> {
|
||||||
|
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 {
|
||||||
|
account_stream.append(&false).append_empty_data();
|
||||||
|
} else {
|
||||||
|
match acct_db.lookup(&self.code_hash) {
|
||||||
|
Some(c) => {
|
||||||
|
account_stream.append(&true).append(&c);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
warn!("code lookup failed for account with address hash {}, code hash {}", addr_hash, self.code_hash);
|
||||||
|
account_stream.append(&false).append_empty_data();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
account_stream.append(&pairs_rlp);
|
||||||
|
|
||||||
|
Ok(account_stream.out())
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode a fat rlp, and rebuild the storage trie as we go.
|
||||||
|
pub fn from_fat_rlp(acct_db: &mut AccountDBMut, rlp: Bytes) -> Result<Self, DecoderError> {
|
||||||
|
use util::{TrieDBMut, TrieMut};
|
||||||
|
|
||||||
|
let rlp = UntrustedRlp::new(&rlp);
|
||||||
|
|
||||||
|
let nonce = try!(rlp.val_at(0));
|
||||||
|
let balance = try!(rlp.val_at(1));
|
||||||
|
let code_hash = if try!(rlp.val_at(2)) {
|
||||||
|
let code: Bytes = try!(rlp.val_at(3));
|
||||||
|
acct_db.insert(&code)
|
||||||
|
} else {
|
||||||
|
SHA3_EMPTY
|
||||||
|
};
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
storage_trie.insert(&k, &v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Account {
|
||||||
|
nonce: nonce,
|
||||||
|
balance: balance,
|
||||||
|
storage_root: storage_root,
|
||||||
|
code_hash: code_hash,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -21,19 +21,20 @@ use std::fs::File;
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use account_db::{AccountDB, AccountDBMut};
|
use account_db::{AccountDB};
|
||||||
use client::BlockChainClient;
|
use client::BlockChainClient;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use ids::BlockID;
|
use ids::BlockID;
|
||||||
use views::BlockView;
|
use views::BlockView;
|
||||||
|
|
||||||
use util::{Bytes, Hashable, HashDB, SHA3_EMPTY, snappy, TrieDB};
|
use util::{Bytes, Hashable, HashDB, snappy, TrieDB};
|
||||||
use util::hash::{FixedHash, H256};
|
use util::hash::{FixedHash, H256};
|
||||||
use util::numbers::U256;
|
use util::rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View};
|
||||||
use util::rlp::{DecoderError, Rlp, RlpStream, Stream, UntrustedRlp, View};
|
|
||||||
|
|
||||||
|
use self::account::Account;
|
||||||
use self::block::AbridgedBlock;
|
use self::block::AbridgedBlock;
|
||||||
|
|
||||||
|
mod account;
|
||||||
mod block;
|
mod block;
|
||||||
|
|
||||||
// Try to have chunks be around 16MB (before compression)
|
// Try to have chunks be around 16MB (before compression)
|
||||||
@ -231,7 +232,7 @@ pub fn chunk_state(db: &HashDB, root: &H256, path: &Path) -> Result<Vec<H256>, E
|
|||||||
|
|
||||||
// account_key here is the address' hash.
|
// account_key here is the address' hash.
|
||||||
for (account_key, account_data) in account_view.iter() {
|
for (account_key, account_data) in account_view.iter() {
|
||||||
let account = AccountReader::from_thin_rlp(account_data);
|
let account = Account::from_thin_rlp(account_data);
|
||||||
let account_key_hash = H256::from_slice(&account_key);
|
let account_key_hash = H256::from_slice(&account_key);
|
||||||
|
|
||||||
let account_db = AccountDB::from_hash(db, account_key_hash);
|
let account_db = AccountDB::from_hash(db, account_key_hash);
|
||||||
@ -247,120 +248,6 @@ pub fn chunk_state(db: &HashDB, root: &H256, path: &Path) -> Result<Vec<H256>, E
|
|||||||
Ok(chunker.hashes)
|
Ok(chunker.hashes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// An alternate account structure, only used for reading the storage values
|
|
||||||
// out of the account as opposed to writing any.
|
|
||||||
struct AccountReader {
|
|
||||||
nonce: U256,
|
|
||||||
balance: U256,
|
|
||||||
storage_root: H256,
|
|
||||||
code_hash: H256,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AccountReader {
|
|
||||||
// decode the account from rlp.
|
|
||||||
fn from_thin_rlp(rlp: &[u8]) -> Self {
|
|
||||||
let r: Rlp = Rlp::new(rlp);
|
|
||||||
|
|
||||||
AccountReader {
|
|
||||||
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.
|
|
||||||
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.
|
|
||||||
fn to_fat_rlp(&self, acct_db: &AccountDB, addr_hash: H256) -> Result<Bytes, Error> {
|
|
||||||
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 {
|
|
||||||
account_stream.append(&false).append_empty_data();
|
|
||||||
} else {
|
|
||||||
match acct_db.lookup(&self.code_hash) {
|
|
||||||
Some(c) => {
|
|
||||||
account_stream.append(&true).append(&c);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
warn!("code lookup failed for account with address hash {}, code hash {}", addr_hash, self.code_hash);
|
|
||||||
account_stream.append(&false).append_empty_data();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
account_stream.append(&pairs_rlp);
|
|
||||||
|
|
||||||
Ok(account_stream.out())
|
|
||||||
}
|
|
||||||
|
|
||||||
// decode a fat rlp, and rebuild the storage trie as we go.
|
|
||||||
fn from_fat_rlp(acct_db: &mut AccountDBMut, rlp: Bytes) -> Result<Self, DecoderError> {
|
|
||||||
use util::{TrieDBMut, TrieMut};
|
|
||||||
|
|
||||||
let rlp = UntrustedRlp::new(&rlp);
|
|
||||||
|
|
||||||
let nonce = try!(rlp.val_at(0));
|
|
||||||
let balance = try!(rlp.val_at(1));
|
|
||||||
let code_hash = if try!(rlp.val_at(2)) {
|
|
||||||
let code: Bytes = try!(rlp.val_at(3));
|
|
||||||
acct_db.insert(&code)
|
|
||||||
} else {
|
|
||||||
SHA3_EMPTY
|
|
||||||
};
|
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
storage_trie.insert(&k, &v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(AccountReader {
|
|
||||||
nonce: nonce,
|
|
||||||
balance: balance,
|
|
||||||
storage_root: storage_root,
|
|
||||||
code_hash: code_hash,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Manifest data.
|
/// Manifest data.
|
||||||
pub struct ManifestData {
|
pub struct ManifestData {
|
||||||
/// List of state chunk hashes.
|
/// List of state chunk hashes.
|
||||||
|
Loading…
Reference in New Issue
Block a user