Skip to content

Commit

Permalink
Merge pull request #291 from ethereumproject/develop
Browse files Browse the repository at this point in the history
Minor interface fixes for dependency and used_gas
  • Loading branch information
sorpaas authored Nov 24, 2017
2 parents 4d8fe8a + 1e60f53 commit 328033b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sputnikvm"
version = "0.8.1"
version = "0.9.0"
license = "Apache-2.0"
authors = ["Wei Tang <[email protected]>"]
description = "SputnikVM - a Portable Blockchain Virtual Machine"
Expand All @@ -19,7 +19,7 @@ etcommon-block-core = { version = "0.1", default-features = false }
etcommon-rlp = { version = "0.2", default-features = false }
etcommon-bigint = { version = "0.2", default-features = false, features = ["rlp"] }

etcommon-block = { version = "0.3", optional = true }
etcommon-block = { version = "0.3", default-features = false, optional = true }
secp256k1-plus = { version = "0.5.7", optional = true }
libsecp256k1 = { version = "0.1", optional = true }

Expand All @@ -28,8 +28,8 @@ etcommon-hexutil = "0.2"

[features]
default = ["std", "c-secp256k1"]
c-secp256k1 = ["secp256k1-plus"]
rust-secp256k1 = ["libsecp256k1"]
c-secp256k1 = ["secp256k1-plus", "etcommon-block/c-secp256k1"]
rust-secp256k1 = ["libsecp256k1", "etcommon-block/rust-secp256k1"]
std = ["etcommon-block-core/std", "etcommon-rlp/std", "etcommon-bigint/std", "etcommon-block"]

[workspace]
Expand Down
2 changes: 1 addition & 1 deletion regtests/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn test_block<T: GethRPCClient, P: Patch>(client: &mut T, number: usize) {

handle_fire(client, &mut vm, last_id);

assert!(Gas::from_str(&receipt.gasUsed).unwrap() == vm.real_used_gas());
assert!(Gas::from_str(&receipt.gasUsed).unwrap() == vm.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
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ pub use self::commit::{AccountCommitment, AccountChange, AccountState, Blockhash
pub use self::transaction::{ValidTransaction, TransactionVM};
pub use self::errors::{OnChainError, NotSupportedError, RequireError, CommitError, PreExecutionError};
pub use self::util::opcode::Opcode;
pub use block_core::TransactionAction;

#[cfg(not(feature = "std"))]
use alloc::Vec;

#[cfg(feature = "std")] use std::collections::{HashSet as Set, hash_map as map};
#[cfg(not(feature = "std"))] use alloc::{BTreeSet as Set, btree_map as map};
#[cfg(feature = "std")] use std::cmp::min;
#[cfg(not(feature = "std"))] use core::cmp::min;
use bigint::{U256, H256, Gas, Address};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -125,6 +128,10 @@ pub trait VM {
fn logs(&self) -> &[Log];
/// Returns all removed account addresses as for current VM execution.
fn removed(&self) -> &[Address];
/// Returns the real used gas by the transaction or the VM
/// context. Only available when the status of the VM is
/// exited. Otherwise returns zero.
fn used_gas(&self) -> Gas;
}

/// A sequencial VM. It uses sequencial memory representation and hash
Expand Down Expand Up @@ -288,4 +295,18 @@ impl<M: Memory + Default, P: Patch> VM for ContextVM<M, P> {
fn removed(&self) -> &[Address] {
self.machines[0].state().removed.as_slice()
}

fn used_gas(&self) -> Gas {
match self.machines[0].status() {
MachineStatus::ExitedErr(_) =>
self.machines[0].state().context.gas_limit,
MachineStatus::ExitedOk => {
let total_used = self.machines[0].state().memory_gas() + self.machines[0].state().used_gas;
let refund_cap = total_used / Gas::from(2u64);
let refunded = min(refund_cap, self.machines[0].state().refunded_gas);
total_used - refunded
}
_ => Gas::zero(),
}
}
}
42 changes: 20 additions & 22 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,6 @@ impl<M: Memory + Default, P: Patch> TransactionVM<M, P> {
})
}

/// Returns the real used gas by the transaction. This is what is
/// recorded in the transaction receipt.
pub fn real_used_gas(&self) -> Gas {
match self.0 {
TransactionVMState::Running { ref vm, intrinsic_gas, .. } => {
match vm.machines[0].status() {
MachineStatus::ExitedErr(_) =>
vm.machines[0].state().context.gas_limit + intrinsic_gas,
MachineStatus::ExitedOk => {
let total_used = vm.machines[0].state().memory_gas() + vm.machines[0].state().used_gas + intrinsic_gas;
let refund_cap = total_used / Gas::from(2u64);
let refunded = min(refund_cap, vm.machines[0].state().refunded_gas);
total_used - refunded
}
_ => Gas::zero(),
}
}
TransactionVMState::Constructing { .. } => Gas::zero(),
}
}

/// Returns the current state of the VM.
pub fn current_state(&self) -> Option<&State<M, P>> {
self.current_machine().map(|m| m.state())
Expand Down Expand Up @@ -326,7 +305,7 @@ impl<M: Memory + Default, P: Patch> VM for TransactionVM<M, P> {
let ccode_deposit: bool;
let cpreclaimed_value: U256;

let real_used_gas = self.real_used_gas();
let real_used_gas = self.used_gas();

match self.0 {
TransactionVMState::Running {
Expand Down Expand Up @@ -454,6 +433,25 @@ impl<M: Memory + Default, P: Patch> VM for TransactionVM<M, P> {
TransactionVMState::Constructing { .. } => &[],
}
}

fn used_gas(&self) -> Gas {
match self.0 {
TransactionVMState::Running { ref vm, intrinsic_gas, .. } => {
match vm.machines[0].status() {
MachineStatus::ExitedErr(_) =>
vm.machines[0].state().context.gas_limit + intrinsic_gas,
MachineStatus::ExitedOk => {
let total_used = vm.machines[0].state().memory_gas() + vm.machines[0].state().used_gas + intrinsic_gas;
let refund_cap = total_used / Gas::from(2u64);
let refunded = min(refund_cap, vm.machines[0].state().refunded_gas);
total_used - refunded
}
_ => Gas::zero(),
}
}
TransactionVMState::Constructing { .. } => Gas::zero(),
}
}
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions stateful/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "sputnikvm-stateful"
version = "0.8.0"
version = "0.9.1"
license = "Apache-2.0"
description = "Stateful SputnikVM wrapped with tries."
authors = ["Wei Tang <[email protected]>"]

[dependencies]
sputnikvm = { version = "0.8", path = '..' }
sputnikvm = { version = "0.9", path = '..' }
etcommon-bigint = "0.2"
etcommon-trie = "0.3"
etcommon-block = "0.3"
Expand Down

0 comments on commit 328033b

Please sign in to comment.