Skip to content

Commit 328033b

Browse files
authored
Merge pull request #291 from ethereumproject/develop
Minor interface fixes for dependency and used_gas
2 parents 4d8fe8a + 1e60f53 commit 328033b

5 files changed

Lines changed: 48 additions & 29 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sputnikvm"
3-
version = "0.8.1"
3+
version = "0.9.0"
44
license = "Apache-2.0"
55
authors = ["Wei Tang <hi@that.world>"]
66
description = "SputnikVM - a Portable Blockchain Virtual Machine"
@@ -19,7 +19,7 @@ etcommon-block-core = { version = "0.1", default-features = false }
1919
etcommon-rlp = { version = "0.2", default-features = false }
2020
etcommon-bigint = { version = "0.2", default-features = false, features = ["rlp"] }
2121

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

@@ -28,8 +28,8 @@ etcommon-hexutil = "0.2"
2828

2929
[features]
3030
default = ["std", "c-secp256k1"]
31-
c-secp256k1 = ["secp256k1-plus"]
32-
rust-secp256k1 = ["libsecp256k1"]
31+
c-secp256k1 = ["secp256k1-plus", "etcommon-block/c-secp256k1"]
32+
rust-secp256k1 = ["libsecp256k1", "etcommon-block/rust-secp256k1"]
3333
std = ["etcommon-block-core/std", "etcommon-rlp/std", "etcommon-bigint/std", "etcommon-block"]
3434

3535
[workspace]

regtests/src/bin/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn test_block<T: GethRPCClient, P: Patch>(client: &mut T, number: usize) {
163163

164164
handle_fire(client, &mut vm, last_id);
165165

166-
assert!(Gas::from_str(&receipt.gasUsed).unwrap() == vm.real_used_gas());
166+
assert!(Gas::from_str(&receipt.gasUsed).unwrap() == vm.used_gas());
167167
assert!(receipt.logs.len() == vm.logs().len());
168168
for i in 0..receipt.logs.len() {
169169
assert!(from_rpc_log(&receipt.logs[i]) == vm.logs()[i]);

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ pub use self::commit::{AccountCommitment, AccountChange, AccountState, Blockhash
6060
pub use self::transaction::{ValidTransaction, TransactionVM};
6161
pub use self::errors::{OnChainError, NotSupportedError, RequireError, CommitError, PreExecutionError};
6262
pub use self::util::opcode::Opcode;
63+
pub use block_core::TransactionAction;
6364

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

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

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

130137
/// A sequencial VM. It uses sequencial memory representation and hash
@@ -288,4 +295,18 @@ impl<M: Memory + Default, P: Patch> VM for ContextVM<M, P> {
288295
fn removed(&self) -> &[Address] {
289296
self.machines[0].state().removed.as_slice()
290297
}
298+
299+
fn used_gas(&self) -> Gas {
300+
match self.machines[0].status() {
301+
MachineStatus::ExitedErr(_) =>
302+
self.machines[0].state().context.gas_limit,
303+
MachineStatus::ExitedOk => {
304+
let total_used = self.machines[0].state().memory_gas() + self.machines[0].state().used_gas;
305+
let refund_cap = total_used / Gas::from(2u64);
306+
let refunded = min(refund_cap, self.machines[0].state().refunded_gas);
307+
total_used - refunded
308+
}
309+
_ => Gas::zero(),
310+
}
311+
}
291312
}

src/transaction.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,27 +252,6 @@ impl<M: Memory + Default, P: Patch> TransactionVM<M, P> {
252252
})
253253
}
254254

255-
/// Returns the real used gas by the transaction. This is what is
256-
/// recorded in the transaction receipt.
257-
pub fn real_used_gas(&self) -> Gas {
258-
match self.0 {
259-
TransactionVMState::Running { ref vm, intrinsic_gas, .. } => {
260-
match vm.machines[0].status() {
261-
MachineStatus::ExitedErr(_) =>
262-
vm.machines[0].state().context.gas_limit + intrinsic_gas,
263-
MachineStatus::ExitedOk => {
264-
let total_used = vm.machines[0].state().memory_gas() + vm.machines[0].state().used_gas + intrinsic_gas;
265-
let refund_cap = total_used / Gas::from(2u64);
266-
let refunded = min(refund_cap, vm.machines[0].state().refunded_gas);
267-
total_used - refunded
268-
}
269-
_ => Gas::zero(),
270-
}
271-
}
272-
TransactionVMState::Constructing { .. } => Gas::zero(),
273-
}
274-
}
275-
276255
/// Returns the current state of the VM.
277256
pub fn current_state(&self) -> Option<&State<M, P>> {
278257
self.current_machine().map(|m| m.state())
@@ -326,7 +305,7 @@ impl<M: Memory + Default, P: Patch> VM for TransactionVM<M, P> {
326305
let ccode_deposit: bool;
327306
let cpreclaimed_value: U256;
328307

329-
let real_used_gas = self.real_used_gas();
308+
let real_used_gas = self.used_gas();
330309

331310
match self.0 {
332311
TransactionVMState::Running {
@@ -454,6 +433,25 @@ impl<M: Memory + Default, P: Patch> VM for TransactionVM<M, P> {
454433
TransactionVMState::Constructing { .. } => &[],
455434
}
456435
}
436+
437+
fn used_gas(&self) -> Gas {
438+
match self.0 {
439+
TransactionVMState::Running { ref vm, intrinsic_gas, .. } => {
440+
match vm.machines[0].status() {
441+
MachineStatus::ExitedErr(_) =>
442+
vm.machines[0].state().context.gas_limit + intrinsic_gas,
443+
MachineStatus::ExitedOk => {
444+
let total_used = vm.machines[0].state().memory_gas() + vm.machines[0].state().used_gas + intrinsic_gas;
445+
let refund_cap = total_used / Gas::from(2u64);
446+
let refunded = min(refund_cap, vm.machines[0].state().refunded_gas);
447+
total_used - refunded
448+
}
449+
_ => Gas::zero(),
450+
}
451+
}
452+
TransactionVMState::Constructing { .. } => Gas::zero(),
453+
}
454+
}
457455
}
458456

459457
#[cfg(test)]

stateful/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "sputnikvm-stateful"
3-
version = "0.8.0"
3+
version = "0.9.1"
44
license = "Apache-2.0"
55
description = "Stateful SputnikVM wrapped with tries."
66
authors = ["Wei Tang <hi@that.world>"]
77

88
[dependencies]
9-
sputnikvm = { version = "0.8", path = '..' }
9+
sputnikvm = { version = "0.9", path = '..' }
1010
etcommon-bigint = "0.2"
1111
etcommon-trie = "0.3"
1212
etcommon-block = "0.3"

0 commit comments

Comments
 (0)