Skip to content

Commit

Permalink
Merge pull request #176 from sorpaas/master
Browse files Browse the repository at this point in the history
Problem: block 46402, gas cost mismatch
  • Loading branch information
sjmackenzie committed Jun 7, 2017
2 parents 24be58d + 70111e7 commit 2ceb15b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions regtests/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn handle_fire(client: &mut GethRPCClient, vm: &mut SeqTransactionVM, last_block
loop {
match vm.fire() {
Ok(()) => {
println!("VM exited successfully, checking results ...");
println!("VM exited with {:?}.", vm.status());
break;
},
Err(RequireError::Account(address)) => {
Expand Down Expand Up @@ -132,7 +132,7 @@ fn test_block(client: &mut GethRPCClient, number: usize) {

handle_fire(client, &mut vm, &format!("0x{:x}", last_number));

assert!(Gas::from_str(&receipt.gasUsed).unwrap() == vm.used_gas());
assert!(Gas::from_str(&receipt.gasUsed).unwrap() == vm.real_used_gas());
assert!(receipt.logs.len() == vm.logs().len());
for i in 0..receipt.logs.len() {
assert!(from_rpc_log(&receipt.logs[i]) == vm.logs()[i]);
Expand Down
10 changes: 10 additions & 0 deletions sputnikvm/src/vm/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,14 @@ impl<M: Memory + Default> Machine<M> {
pub fn status(&self) -> MachineStatus {
self.status.clone()
}

/// If the machine is exited with EmptyGas, return gas_limit,
/// otherwise return the sum of memory gas and used gas.
pub fn real_used_gas(&self) -> Gas {
match self.status() {
MachineStatus::ExitedErr(MachineError::EmptyGas) =>
self.state.context.gas_limit,
_ => self.state.memory_gas() + self.state.used_gas,
}
}
}
9 changes: 5 additions & 4 deletions sputnikvm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub trait VM {
fn accounts(&self) -> hash_map::Values<Address, Account>;
fn out(&self) -> &[u8];
fn available_gas(&self) -> Gas;
fn used_gas(&self) -> Gas;
fn real_used_gas(&self) -> Gas;
fn refunded_gas(&self) -> Gas;
fn logs(&self) -> &[Log];
}
Expand Down Expand Up @@ -199,9 +199,10 @@ impl<M: Memory + Default> VM for ContextVM<M> {
self.machines[0].state().available_gas()
}

/// Returns the used gas of this VM.
fn used_gas(&self) -> Gas {
self.machines[0].state().memory_gas() + self.machines[0].state().used_gas
/// If the VM is exited with EmptyGas, return gas_limit,
/// otherwise return the sum of memory gas and used gas.
fn real_used_gas(&self) -> Gas {
self.machines[0].real_used_gas()
}

/// Returns the refunded gas of this VM.
Expand Down
4 changes: 2 additions & 2 deletions sputnikvm/src/vm/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ impl<M: Memory + Default> VM for TransactionVM<M> {
}
}

fn used_gas(&self) -> Gas {
fn real_used_gas(&self) -> Gas {
match self.0 {
TransactionVMState::Running { ref vm, intrinsic_gas, .. } => vm.used_gas() + intrinsic_gas,
TransactionVMState::Running { ref vm, intrinsic_gas, .. } => vm.real_used_gas() + intrinsic_gas,
TransactionVMState::Constructing { .. } => Gas::zero(),
}
}
Expand Down

0 comments on commit 2ceb15b

Please sign in to comment.