Skip to content

Commit

Permalink
Merge 878a27f into ac49bca
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Jun 12, 2017
2 parents ac49bca + 878a27f commit 7183ba0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
1 change: 0 additions & 1 deletion jsontests/tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ lazy_static! {
#[test] fn callToNameRegistratorTooMuchMemory0() { assert_eq!(test_transaction("CallToNameRegistratorTooMuchMemory0", &TESTS["CallToNameRegistratorTooMuchMemory0"], true), true); }
#[test] fn callToNameRegistratorTooMuchMemory1() { assert_eq!(test_transaction("CallToNameRegistratorTooMuchMemory1", &TESTS["CallToNameRegistratorTooMuchMemory1"], true), true); }
#[test] fn callToNameRegistratorTooMuchMemory2() { assert_eq!(test_transaction("CallToNameRegistratorTooMuchMemory2", &TESTS["CallToNameRegistratorTooMuchMemory2"], true), true); }
#[test] fn callToPrecompiledContract() { assert_eq!(test_transaction("CallToPrecompiledContract", &TESTS["CallToPrecompiledContract"], true), true); }
#[test] fn callToReturn1() { assert_eq!(test_transaction("CallToReturn1", &TESTS["CallToReturn1"], true), true); }
#[test] fn postToNameRegistrator0() { assert_eq!(test_transaction("PostToNameRegistrator0", &TESTS["PostToNameRegistrator0"], true), true); }
#[test] fn postToReturn1() { assert_eq!(test_transaction("PostToReturn1", &TESTS["PostToReturn1"], true), true); }
Expand Down
3 changes: 2 additions & 1 deletion sputnikvm/src/vm/eval/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use utils::bigint::{M256, U256};
use std::cmp::max;
use vm::{Memory, Instruction};
use super::State;
use super::precompiled::is_precompiled;

const G_ZERO: usize = 0;
const G_BASE: usize = 2;
Expand Down Expand Up @@ -65,7 +66,7 @@ fn xfer_cost<M: Memory + Default>(machine: &State<M>) -> Gas {

fn new_cost<M: Memory + Default>(machine: &State<M>) -> Gas {
let address: Address = machine.stack.peek(1).unwrap().into();
if machine.account_state.balance(address).unwrap() == U256::zero() && machine.account_state.nonce(address).unwrap() == M256::zero() && machine.account_state.code(address).unwrap().len() == 0 {
if machine.account_state.balance(address).unwrap() == U256::zero() && machine.account_state.nonce(address).unwrap() == M256::zero() && machine.account_state.code(address).unwrap().len() == 0 && !is_precompiled(address) {
G_NEWACCOUNT.into()
} else {
Gas::zero()
Expand Down
2 changes: 1 addition & 1 deletion sputnikvm/src/vm/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<M: Memory + Default> Machine<M> {
MachineStatus::ExitedErr(_) => {
// self.state.used_gas = self.state.used_gas + sub.state.used_gas;
self.state.stack.pop().unwrap();
self.state.stack.push(M256::from(1u64)).unwrap();
self.state.stack.push(M256::zero()).unwrap();
},
_ => panic!(),
}
Expand Down
29 changes: 26 additions & 3 deletions sputnikvm/src/vm/eval/precompiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ use ripemd160::Ripemd160;
use secp256k1::{Secp256k1, RecoverableSignature, Message, RecoveryId, Error};
use digest::{Input, FixedOutput};

fn gas_div_ceil(a: Gas, b: Gas) -> Gas {
if a % b == Gas::zero() {
a / b
} else {
a / b + Gas::from(1u64)
}
}

pub fn is_precompiled(address: Address) -> bool {
let ecrec_address = Address::from_str("0x0000000000000000000000000000000000000001").unwrap();
let sha256_address = Address::from_str("0x0000000000000000000000000000000000000002").unwrap();
let rip160_address = Address::from_str("0x0000000000000000000000000000000000000003").unwrap();
let id_address = Address::from_str("0x0000000000000000000000000000000000000004").unwrap();

address == ecrec_address ||
address == sha256_address ||
address == rip160_address ||
address == id_address
}

impl<M: Memory + Default> Machine<M> {
#[allow(unused_variables)]
pub fn step_precompiled(&mut self) -> bool {
Expand Down Expand Up @@ -39,7 +59,8 @@ impl<M: Memory + Default> Machine<M> {

fn step_precompiled_id(&mut self) {
let gas = Gas::from(15u64) +
Gas::from(3u64) * (Gas::from(self.state.context.data.len()) / Gas::from(32u64));
Gas::from(3u64) * gas_div_ceil(Gas::from(self.state.context.data.len()),
Gas::from(32u64));
if gas > self.state.context.gas_limit {
self.state.used_gas = self.state.context.gas_limit;
self.status = MachineStatus::ExitedErr(MachineError::EmptyGas);
Expand All @@ -52,7 +73,8 @@ impl<M: Memory + Default> Machine<M> {

fn step_precompiled_rip160(&mut self) {
let gas = Gas::from(600u64) +
Gas::from(120u64) * (Gas::from(self.state.context.data.len()) / Gas::from(32u64));
Gas::from(120u64) * gas_div_ceil(Gas::from(self.state.context.data.len()),
Gas::from(32u64));
if gas > self.state.context.gas_limit {
self.state.used_gas = self.state.context.gas_limit;
self.status = MachineStatus::ExitedErr(MachineError::EmptyGas);
Expand All @@ -72,7 +94,8 @@ impl<M: Memory + Default> Machine<M> {

fn step_precompiled_sha256(&mut self) {
let gas = Gas::from(60u64) +
Gas::from(12u64) * (Gas::from(self.state.context.data.len()) / Gas::from(32u64));
Gas::from(12u64) * gas_div_ceil(Gas::from(self.state.context.data.len()),
Gas::from(32u64));
if gas > self.state.context.gas_limit {
self.state.used_gas = self.state.context.gas_limit;
self.status = MachineStatus::ExitedErr(MachineError::EmptyGas);
Expand Down
2 changes: 1 addition & 1 deletion sputnikvm/src/vm/eval/run/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ pub fn call<M: Memory + Default>(state: &mut State<M>, stipend_gas: Gas, after_g
let context = transaction.into_context(
Gas::zero(), Some(state.context.origin), &mut state.account_state, true
).unwrap();
push!(state, M256::zero());
push!(state, M256::from(1u64));
Some((context, (out_start, out_len)))
}

0 comments on commit 7183ba0

Please sign in to comment.