Merge pull request #96 from gavofyork/gav

State testing framework. First test is failing.
This commit is contained in:
Marek Kotewicz 2016-01-13 13:22:01 +01:00
commit 1c43d9ab59
8 changed files with 232 additions and 49 deletions

View File

@ -2,8 +2,19 @@ use util::*;
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
/// Single account in the system.
#[derive(Debug)]
/// Genesis account data. Does not have a DB overlay cache.
pub struct PodAccount {
// Balance of the account.
pub balance: U256,
// Nonce of the account.
pub nonce: U256,
pub code: Bytes,
pub storage: BTreeMap<H256, H256>,
}
/// Single account in the system.
#[derive(Clone)]
pub struct Account {
// Balance of the account.
balance: U256,
@ -19,6 +30,29 @@ pub struct Account {
code_cache: Bytes,
}
impl PodAccount {
/// Convert Account to a PodAccount.
/// NOTE: This will silently fail unless the account is fully cached.
pub fn from_account(acc: &Account) -> PodAccount {
PodAccount {
balance: acc.balance.clone(),
nonce: acc.nonce.clone(),
storage: acc.storage_overlay.borrow().iter().fold(BTreeMap::new(), |mut m, (k, v)| {m.insert(k.clone(), v.clone()); m}),
code: acc.code_cache.clone()
}
}
pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
// TODO.
stream.append(&SHA3_NULL_RLP);
stream.append(&self.code.sha3());
stream.out()
}
}
impl Account {
/// General constructor.
pub fn new(balance: U256, nonce: U256, storage: HashMap<H256, H256>, code: Bytes) -> Account {
@ -32,6 +66,18 @@ impl Account {
}
}
/// General constructor.
pub fn from_pod(pod: PodAccount) -> Account {
Account {
balance: pod.balance,
nonce: pod.nonce,
storage_root: SHA3_NULL_RLP,
storage_overlay: RefCell::new(pod.storage.into_iter().fold(HashMap::new(), |mut m, (k, v)| {m.insert(k, v); m})),
code_hash: Some(pod.code.sha3()),
code_cache: pod.code
}
}
/// Create a new account with the given balance.
pub fn new_basic(balance: U256, nonce: U256) -> Account {
Account {
@ -205,6 +251,12 @@ impl Account {
}
}
impl fmt::Debug for Account {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", PodAccount::from_account(self))
}
}
#[cfg(test)]
mod tests {

View File

@ -6,6 +6,7 @@ use header::BlockNumber;
pub type LastHashes = Vec<H256>;
/// Information concerning the execution environment for a message-call/contract-creation.
#[derive(Debug)]
pub struct EnvInfo {
/// The block number.
pub number: BlockNumber,
@ -35,6 +36,18 @@ impl EnvInfo {
gas_used: U256::zero()
}
}
pub fn from_json(json: &Json) -> EnvInfo {
EnvInfo {
number: u64_from_json(&json["currentNumber"]),
author: address_from_json(&json["currentCoinbase"]),
difficulty: u256_from_json(&json["currentDifficulty"]),
gas_limit: u256_from_json(&json["currentGasLimit"]),
timestamp: u64_from_json(&json["currentTimestamp"]),
last_hashes: vec![h256_from_json(&json["previousHash"])],
gas_used: U256::zero(),
}
}
}
/// TODO: it should be the other way around.

View File

@ -5,6 +5,7 @@ use executive::Executive;
pub type ApplyResult = Result<Receipt, Error>;
/// Representation of the entire state of all accounts in the system.
#[derive(Clone)]
pub struct State {
db: OverlayDB,
root: H256,
@ -179,9 +180,33 @@ impl State {
self.root = Self::commit_into(&mut self.db, r, self.cache.borrow_mut().deref_mut());
}
/// Populate the state from `accounts`. Just uses `commit_into`.
pub fn populate_from(&mut self, _accounts: &mut HashMap<Address, Option<Account>>) {
unimplemented!();
/// Populate the state from `accounts`.
pub fn populate_from(&mut self, accounts: BTreeMap<Address, PodAccount>) {
for (add, acc) in accounts.into_iter() {
self.cache.borrow_mut().insert(add, Some(Account::from_pod(acc)));
}
}
/// Populate a PodAccount map from this state.
pub fn to_hashmap_pod(&self) -> HashMap<Address, PodAccount> {
// TODO: handle database rather than just the cache.
self.cache.borrow().iter().fold(HashMap::new(), |mut m, (add, opt)| {
if let &Some(ref acc) = opt {
m.insert(add.clone(), PodAccount::from_account(acc));
}
m
})
}
/// Populate a PodAccount map from this state.
pub fn to_pod_map(&self) -> BTreeMap<Address, PodAccount> {
// TODO: handle database rather than just the cache.
self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
if let &Some(ref acc) = opt {
m.insert(add.clone(), PodAccount::from_account(acc));
}
m
})
}
/// Pull account `a` in our cache from the trie DB and return it.
@ -224,9 +249,9 @@ impl State {
}
}
impl Clone for State {
fn clone(&self) -> Self {
State::from_existing(self.db.clone(), self.root.clone(), self.account_start_nonce.clone())
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.cache.borrow())
}
}

View File

@ -1,4 +1,5 @@
#[macro_use]
mod test_common;
mod transaction;
mod transaction;
mod state;

80
src/tests/state.rs Normal file
View File

@ -0,0 +1,80 @@
use super::test_common::*;
use state::*;
use ethereum;
pub fn hashmap_h256_h256_from_json(json: &Json) -> HashMap<H256, H256> {
json.as_object().unwrap().iter().fold(HashMap::new(), |mut m, (key, value)| {
m.insert(H256::from(&u256_from_hex(key)), H256::from(&u256_from_json(value)));
m
})
}
pub fn map_h256_h256_from_json(json: &Json) -> BTreeMap<H256, H256> {
json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut m, (key, value)| {
m.insert(H256::from(&u256_from_hex(key)), H256::from(&u256_from_json(value)));
m
})
}
/// Translate the JSON object into a hash map of account information ready for insertion into State.
pub fn pod_map_from_json(json: &Json) -> BTreeMap<Address, PodAccount> {
json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut state, (address, acc)| {
let balance = acc.find("balance").map(&u256_from_json);
let nonce = acc.find("nonce").map(&u256_from_json);
let storage = acc.find("storage").map(&map_h256_h256_from_json);;
let code = acc.find("code").map(&bytes_from_json);
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {
state.insert(address_from_hex(address), PodAccount{
balance: balance.unwrap_or(U256::zero()),
nonce: nonce.unwrap_or(U256::zero()),
storage: storage.unwrap_or(BTreeMap::new()),
code: code.unwrap_or(Vec::new())
});
}
state
})
}
fn do_json_test(json_data: &[u8]) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new();
let engine = ethereum::new_frontier_test().to_engine().unwrap();
for (name, test) in json.as_object().unwrap() {
let mut fail = false;
let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true; true } else {false};
let t = Transaction::from_json(&test["transaction"]);
let env = EnvInfo::from_json(&test["env"]);
let out = bytes_from_json(&test["out"]);
let post_state_root = h256_from_json(&test["postStateRoot"]);
let pre = pod_map_from_json(&test["pre"]);
let post = pod_map_from_json(&test["post"]);
// TODO: read test["logs"]
println!("Transaction: {:?}", t);
println!("Env: {:?}", env);
println!("Out: {:?}", out);
println!("Pre: {:?}", pre);
println!("Post: {:?}", post);
let mut s = State::new_temp();
s.populate_from(pre);
s.apply(&env, engine.deref(), &t).unwrap();
let our_post = s.to_pod_map();
if fail_unless(s.root() == &post_state_root) {
println!("EXPECTED:\n{:?}", post);
println!("GOT:\n{:?}", our_post);
}
// TODO: Compare logs.
}
for f in failed.iter() {
println!("FAILED: {:?}", f);
}
failed
}
declare_test!{StateTests_stExample, "StateTests/stExample"}

View File

@ -1,43 +1,5 @@
pub use common::*;
pub fn clean(s: &str) -> &str {
if s.len() >= 2 && &s[0..2] == "0x" {
&s[2..]
} else {
s
}
}
pub fn bytes_from_json(json: &Json) -> Bytes {
let s = json.as_string().unwrap();
if s.len() % 2 == 1 {
FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![])
} else {
FromHex::from_hex(clean(s)).unwrap_or(vec![])
}
}
pub fn address_from_json(json: &Json) -> Address {
let s = json.as_string().unwrap();
if s.len() % 2 == 1 {
address_from_hex(&("0".to_string() + &(clean(s).to_string()))[..])
} else {
address_from_hex(clean(s))
}
}
pub fn u256_from_json(json: &Json) -> U256 {
let s = json.as_string().unwrap();
if s.len() >= 2 && &s[0..2] == "0x" {
// hex
U256::from_str(&s[2..]).unwrap()
}
else {
// dec
U256::from_dec_str(s).unwrap()
}
}
#[macro_export]
macro_rules! declare_test {
($id: ident, $name: expr) => {

View File

@ -6,15 +6,16 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
let mut failed = Vec::new();
let old_schedule = evm::Schedule::new_frontier();
let new_schedule = evm::Schedule::new_homestead();
let ot = RefCell::new(Transaction::new());
for (name, test) in json.as_object().unwrap() {
let mut fail = false;
let mut fail_unless = |cond: bool| if !cond && fail { failed.push(name.to_string()); fail = true };
let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); println!("Transaction: {:?}", ot.borrow()); fail = true };
let schedule = match test.find("blocknumber")
.and_then(|j| j.as_string())
.and_then(|s| BlockNumber::from_str(s).ok())
.unwrap_or(0) { x if x < 900000 => &old_schedule, _ => &new_schedule };
let rlp = bytes_from_json(&test["rlp"]);
let res = UntrustedRlp::new(&rlp).as_val().map_err(|e| From::from(e)).and_then(|t: Transaction| t.validate(schedule));
let res = UntrustedRlp::new(&rlp).as_val().map_err(|e| From::from(e)).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call));
fail_unless(test.find("transaction").is_none() == res.is_err());
if let (Some(&Json::Object(ref tx)), Some(&Json::String(ref expect_sender))) = (test.find("transaction"), test.find("sender")) {
let t = res.unwrap();
@ -25,8 +26,10 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
fail_unless(t.nonce == u256_from_json(&tx["nonce"]));
fail_unless(t.value == u256_from_json(&tx["value"]));
if let Action::Call(ref to) = t.action {
*ot.borrow_mut() = t.clone();
fail_unless(to == &address_from_json(&tx["to"]));
} else {
*ot.borrow_mut() = t.clone();
fail_unless(bytes_from_json(&tx["to"]).len() == 0);
}
}

View File

@ -3,6 +3,7 @@ use basic_types::*;
use error::*;
use evm::Schedule;
#[derive(Debug,Clone)]
pub enum Action {
Create,
Call(Address),
@ -10,6 +11,7 @@ pub enum Action {
/// A set of information describing an externally-originating message call
/// or contract creation operation.
#[derive(Debug,Clone)]
pub struct Transaction {
pub nonce: U256,
pub gas_price: U256,
@ -28,6 +30,21 @@ pub struct Transaction {
}
impl Transaction {
pub fn new() -> Self {
Transaction {
nonce: U256::zero(),
gas_price: U256::zero(),
gas: U256::zero(),
action: Action::Create,
value: U256::zero(),
data: vec![],
v: 0,
r: U256::zero(),
s: U256::zero(),
hash: RefCell::new(None),
sender: RefCell::new(None),
}
}
/// Create a new message-call transaction.
pub fn new_call(to: Address, value: U256, data: Bytes, gas: U256, gas_price: U256, nonce: U256) -> Transaction {
Transaction {
@ -62,6 +79,32 @@ impl Transaction {
}
}
pub fn from_json(json: &Json) -> Transaction {
let mut r = Transaction {
nonce: u256_from_json(&json["nonce"]),
gas_price: u256_from_json(&json["gasPrice"]),
gas: u256_from_json(&json["gasLimit"]),
action: match bytes_from_json(&json["to"]) {
ref x if x.len() == 0 => Action::Create,
ref x => Action::Call(Address::from_slice(x)),
},
value: u256_from_json(&json["value"]),
data: bytes_from_json(&json["data"]),
v: match json.find("v") { Some(ref j) => u8_from_json(j), None => 0 },
r: match json.find("r") { Some(ref j) => u256_from_json(j), None => U256::zero() },
s: match json.find("s") { Some(ref j) => u256_from_json(j), None => U256::zero() },
hash: RefCell::new(None),
sender: match json.find("sender") {
Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))),
_ => RefCell::new(None),
},
};
if let Some(&Json::String(ref secret_key)) = json.find("secretKey") {
r.sign(&h256_from_hex(clean(secret_key)));
}
r
}
/// Get the nonce of the transaction.
pub fn nonce(&self) -> &U256 { &self.nonce }
/// Get the gas price of the transaction.
@ -146,6 +189,7 @@ impl Transaction {
/// Signs the transaction as coming from `sender`.
pub fn sign(&mut self, secret: &Secret) {
// TODO: make always low.
let sig = ec::sign(secret, &self.message_hash());
let (r, s, v) = sig.unwrap().to_rsv();
self.r = r;
@ -170,7 +214,10 @@ impl Transaction {
}
/// Do basic validation, checking for valid signature and minimum gas,
pub fn validate(self, schedule: &Schedule) -> Result<Transaction, Error> {
pub fn validate(self, schedule: &Schedule, require_low: bool) -> Result<Transaction, Error> {
if require_low && !ec::is_low_s(&self.s) {
return Err(Error::Util(UtilError::Crypto(CryptoError::InvalidSignature)));
}
try!(self.sender());
if self.gas < U256::from(self.gas_required(&schedule)) {
Err(From::from(TransactionError::InvalidGasLimit(OutOfBounds{min: Some(U256::from(self.gas_required(&schedule))), max: None, found: self.gas})))