Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs until block number 51921 #193

Merged
merged 15 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gethrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ pub struct RPCFilter {
pub struct GethRPCClient {
endpoint: String,
free_id: usize,
http: Client,
}

impl GethRPCClient {
pub fn new(endpoint: &str) -> Self {
GethRPCClient {
endpoint: endpoint.to_string(),
free_id: 1,
http: Client::new(),
}
}

Expand All @@ -151,8 +153,7 @@ impl GethRPCClient {
};
self.free_id = self.free_id + 1;

let client = Client::new();
let mut response_raw = client.post(&self.endpoint)
let mut response_raw = self.http.post(&self.endpoint)
.header(ContentType::json())
.body(&serde_json::to_string(&request).unwrap())
.send().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion jsontests/src/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sputnikvm::{Gas, M256, U256, Address, read_hex};
use sputnikvm::vm::{Machine, Log, Context,
Account, Storage, AccountCommitment,
BlockHeader};
BlockHeader, ExecutionMode};

use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -267,5 +267,6 @@ pub fn create_context(v: &Value) -> Context {
gas_price: gas_price,
origin: origin,
value: value,
mode: ExecutionMode::None,
}
}
11 changes: 6 additions & 5 deletions regtests/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn handle_fire(client: &mut GethRPCClient, vm: &mut SeqTransactionVM, last_block
address: address,
balance: balance,
code: code,
});
}).unwrap();
},
Err(RequireError::AccountStorage(address, index)) => {
println!("Feeding VM account storage at 0x{:x} with index 0x{:x} ...", address, index);
Expand All @@ -93,7 +93,7 @@ fn handle_fire(client: &mut GethRPCClient, vm: &mut SeqTransactionVM, last_block
address: address,
index: index,
value: value,
});
}).unwrap();
},
Err(RequireError::AccountCode(address)) => {
println!("Feeding VM account code at 0x{:x} ...", address);
Expand All @@ -102,7 +102,7 @@ fn handle_fire(client: &mut GethRPCClient, vm: &mut SeqTransactionVM, last_block
vm.commit_account(AccountCommitment::Code {
address: address,
code: code,
});
}).unwrap();
}
Err(err) => {
println!("Unhandled require: {:?}", err);
Expand All @@ -120,8 +120,9 @@ fn is_miner_or_uncle(address: Address, block: &RPCBlock, client: &mut GethRPCCli
return true;
}
if block.uncles.len() > 0 {
for uncle_hash in &block.uncles {
let uncle = client.get_block_by_hash(&uncle_hash);
for i in 0..block.uncles.len() {
let uncle = client.get_uncle_by_block_number_and_index(&block.number,
&format!("0x{:x}", i));
let uncle_miner = Address::from_str(&uncle.miner).unwrap();
if uncle_miner == address {
return true;
Expand Down
129 changes: 114 additions & 15 deletions sputnikvm/src/vm/commit/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,20 @@ impl AccountState {
self.accounts.values()
}

pub fn is_removed(&self, address: Address) -> bool {
match self.accounts.get(&address) {
Some(&Account::Remove(_)) => true,
_ => false,
}
}

/// Returns Ok(()) if a full account is in this account
/// state. Otherwise raise a `RequireError`.
pub fn require(&self, address: Address) -> Result<(), RequireError> {
match self.accounts.get(&address) {
Some(&Account::Full { .. }) => return Ok(()),
Some(&Account::Create { .. }) => return Ok(()),
Some(&Account::Remove(_)) => panic!(),
_ => return Err(RequireError::Account(address)),
}
}
Expand All @@ -140,6 +149,8 @@ impl AccountState {
}
match self.accounts.get(&address) {
Some(&Account::Full { .. }) => return Ok(()),
Some(&Account::Create { .. }) => return Ok(()),
Some(&Account::Remove(_)) => panic!(),
_ => return Err(RequireError::AccountCode(address)),
}
}
Expand All @@ -159,17 +170,41 @@ impl AccountState {
balance,
code
} => {
if self.accounts.contains_key(&address) {
return Err(CommitError::AlreadyCommitted);
}
let account = if self.accounts.contains_key(&address) {
match self.accounts.remove(&address).unwrap() {
Account::Full { .. } => return Err(CommitError::AlreadyCommitted),
Account::Create { .. } => return Err(CommitError::AlreadyCommitted),
Account::Remove(address) => Account::Remove(address),
Account::IncreaseBalance(address, topup) => {
Account::Full {
nonce,
address,
balance: balance + topup,
changing_storage: Storage::new(address, true),
code,
}
},
Account::DecreaseBalance(address, withdraw) => {
Account::Full {
nonce,
address,
balance: balance - withdraw,
changing_storage: Storage::new(address, true),
code,
}
},
}
} else {
Account::Full {
nonce,
address,
balance,
changing_storage: Storage::new(address, true),
code,
}
};

self.accounts.insert(address, Account::Full {
nonce,
address,
balance,
changing_storage: Storage::new(address, true),
code,
});
self.accounts.insert(address, account);
},
AccountCommitment::Code {
address,
Expand Down Expand Up @@ -215,7 +250,13 @@ impl AccountState {
ref code,
..
} => return Ok(code.as_slice()),
_ => (),
&Account::Create {
ref code,
..
} => return Ok(code.as_slice()),
&Account::Remove(_) => panic!(),
&Account::IncreaseBalance(address, _) => return Err(RequireError::Account(address)),
&Account::DecreaseBalance(address, _) => return Err(RequireError::Account(address)),
}
}

Expand All @@ -231,6 +272,11 @@ impl AccountState {
nonce,
..
} => return Ok(nonce),
&Account::Create {
nonce,
..
} => return Ok(nonce),
&Account::Remove(_) => panic!(),
_ => (),
}
}
Expand All @@ -247,6 +293,11 @@ impl AccountState {
balance,
..
} => return Ok(balance),
&Account::Create {
balance,
..
} => return Ok(balance),
&Account::Remove(_) => return Ok(U256::zero()),
_ => (),
}
}
Expand All @@ -267,6 +318,7 @@ impl AccountState {
ref storage,
..
} => return Ok(storage),
&Account::Remove(_) => panic!(),
_ => (),
}
}
Expand All @@ -287,6 +339,7 @@ impl AccountState {
ref mut storage,
..
} => return Ok(storage),
&mut Account::Remove(_) => panic!(),
_ => (),
}
}
Expand All @@ -296,10 +349,48 @@ impl AccountState {

/// Create a new account (that should not yet have existed
/// before).
pub fn create(&mut self, address: Address, balance: U256, code: &[u8]) {
self.accounts.insert(address, Account::Full {
address, balance, changing_storage: Storage::new(address, false), code: code.into(), nonce: M256::zero(),
});
pub fn create(&mut self, address: Address, balance: U256) {
let account = if self.accounts.contains_key(&address) {
match self.accounts.remove(&address).unwrap() {
Account::Full { .. } => panic!(),
Account::Create { .. } => panic!(),
Account::Remove(address) => {
Account::Create {
address, balance, code: Vec::new(), nonce: M256::zero(),
storage: Storage::new(address, false)
}
},
Account::IncreaseBalance(address, topup) => {
Account::Create {
address, code: Vec::new(), nonce: M256::zero(),
balance: balance + topup, storage: Storage::new(address, false)
}
},
Account::DecreaseBalance(address, withdraw) => {
Account::Create {
address, code: Vec::new(), nonce: M256::zero(),
balance: balance - withdraw, storage: Storage::new(address, false)
}
},
}
} else {
Account::Create {
address, balance, code: Vec::new(), nonce: M256::zero(),
storage: Storage::new(address, false)
}
};

self.accounts.insert(address, account);
}

/// Deposit code in to a created account.
pub fn code_deposit(&mut self, address: Address, new_code: &[u8]) {
match self.accounts.get_mut(&address).unwrap() {
&mut Account::Create { ref mut code, .. } => {
*code = new_code.into();
},
_ => panic!(),
}
}

/// Increase the balance of an account.
Expand Down Expand Up @@ -429,6 +520,14 @@ impl AccountState {
*nonce = new_nonce;
Ok(())
},
Some(&mut Account::Create {
ref mut nonce,
..
}) => {
*nonce = new_nonce;
Ok(())
},
Some(&mut Account::Remove(_)) => panic!(),
_ => {
Err(RequireError::Account(address))
},
Expand Down
11 changes: 0 additions & 11 deletions sputnikvm/src/vm/eval/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ use vm::errors::{MachineError, EvalError};
use vm::eval::{State, ControlCheck};
use super::utils::{check_range, check_memory_write_range};

const CALLSTACK_LIMIT_DEFAULT: usize = 1024;
const CALLSTACK_LIMIT_TEST: usize = 2;

fn check_callstack_overflow<M: Memory>(state: &State<M>) -> Result<(), MachineError> {
if state.depth >= state.patch.callstack_limit {
return Err(MachineError::CallstackOverflow);
} else {
return Ok(());
}
}

pub fn extra_check_opcode<M: Memory + Default>(instruction: Instruction, state: &State<M>, stipend_gas: Gas, after_gas: Gas) -> Result<(), EvalError> {
match instruction {
Instruction::CALL => {
Expand Down
8 changes: 6 additions & 2 deletions sputnikvm/src/vm/eval/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,12 @@ pub fn gas_refund<M: Memory + Default>(instruction: Instruction, state: &State<M
}
},
Instruction::SUICIDE => {
// TODO: check whether I_a belongs to A_s
Gas::zero()
let address: Address = state.stack.peek(0).unwrap().into();
if state.account_state.is_removed(address) {
Gas::zero()
} else {
Gas::from(R_SUICIDE)
}
},
_ => Gas::zero()
}
Expand Down
Loading