Skip to content

Commit fc97fb7

Browse files
authored
Compliance fix 3 (#220)
1 parent 9f5645d commit fc97fb7

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

.github/workflows/rust.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ jobs:
3939
jsontests/res/ethtests/GeneralStateTests/stExample/ \
4040
jsontests/res/ethtests/GeneralStateTests/stSLoadTest/ \
4141
jsontests/res/ethtests/GeneralStateTests/VMTests/vmArithmeticTest/ \
42-
jsontests/res/ethtests/GeneralStateTests/VMTests/vmBitwiseLogicOperation/
42+
jsontests/res/ethtests/GeneralStateTests/VMTests/vmBitwiseLogicOperation/ \
43+
jsontests/res/ethtests/GeneralStateTests/VMTests/vmIOandFlowOperations/ \
44+
jsontests/res/ethtests/GeneralStateTests/VMTests/vmLogTest/
45+

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
target
1010
**/result
1111
tests.bin
12-
Cargo.lock
12+
Cargo.lock
13+
perf.data
14+
perf.data.old

interpreter/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<S> Machine<S> {
122122
}
123123
}
124124

125+
#[inline]
125126
/// Step the machine N times.
126127
pub fn stepn<H, Tr, F>(
127128
&mut self,
@@ -152,6 +153,10 @@ impl<S> Machine<S> {
152153
where
153154
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
154155
{
156+
if self.is_empty() {
157+
return Err(Capture::Exit(ExitSucceed::Stopped.into()));
158+
}
159+
155160
let position = self.position;
156161
if position >= self.code.len() {
157162
return Err(Capture::Exit(ExitFatal::AlreadyExited.into()));
@@ -189,6 +194,10 @@ impl<S> Machine<S> {
189194
self.code.get(self.position).map(|opcode| Opcode(*opcode))
190195
}
191196

197+
pub fn is_empty(&self) -> bool {
198+
self.code.is_empty()
199+
}
200+
192201
pub fn advance(&mut self) {
193202
if self.position == self.code.len() {
194203
return;

src/standard/gasometer/mod.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ mod utils;
44

55
use crate::standard::Config;
66
use crate::{
7-
ExitError, ExitException, Gasometer as GasometerT, Machine, MergeStrategy, Opcode,
7+
ExitError, ExitException, ExitFatal, Gasometer as GasometerT, Machine, MergeStrategy, Opcode,
88
RuntimeBackend, RuntimeState, Stack,
99
};
10-
use core::cmp::max;
10+
use core::cmp::{max, min};
1111
use primitive_types::{H160, H256, U256};
1212

1313
pub trait TransactGasometer<'config, S: AsRef<RuntimeState>>: Sized {
@@ -25,6 +25,8 @@ pub trait TransactGasometer<'config, S: AsRef<RuntimeState>>: Sized {
2525
access_list: &Vec<(H160, Vec<H256>)>,
2626
config: &'config Config,
2727
) -> Result<Self, ExitError>;
28+
29+
fn effective_gas(&self) -> U256;
2830
}
2931

3032
pub struct Gasometer<'config> {
@@ -36,6 +38,7 @@ pub struct Gasometer<'config> {
3638
}
3739

3840
impl<'config> Gasometer<'config> {
41+
#[inline]
3942
pub fn perform<R, F: FnOnce(&mut Self) -> Result<R, ExitError>>(
4043
&mut self,
4144
f: F,
@@ -125,6 +128,17 @@ impl<'config, S: AsRef<RuntimeState>> TransactGasometer<'config, S> for Gasomete
125128
s.record_cost(transaction_cost)?;
126129
Ok(s)
127130
}
131+
132+
fn effective_gas(&self) -> U256 {
133+
U256::from(
134+
self.gas_limit
135+
- (self.total_used_gas()
136+
- min(
137+
self.total_used_gas() / self.config.max_refund_quotient,
138+
self.refunded_gas,
139+
)),
140+
)
141+
}
128142
}
129143

130144
impl<'config, S: AsRef<RuntimeState>, H: RuntimeBackend> GasometerT<S, H> for Gasometer<'config> {
@@ -134,8 +148,12 @@ impl<'config, S: AsRef<RuntimeState>, H: RuntimeBackend> GasometerT<S, H> for Ga
134148
is_static: bool,
135149
handler: &H,
136150
) -> Result<(), ExitError> {
151+
if machine.is_empty() {
152+
return Ok(());
153+
}
154+
137155
self.perform(|gasometer| {
138-
let opcode = machine.peek_opcode().ok_or(ExitException::OutOfGas)?;
156+
let opcode = machine.peek_opcode().ok_or(ExitFatal::AlreadyExited)?;
139157

140158
if let Some(cost) = consts::STATIC_COST_TABLE[opcode.as_usize()] {
141159
gasometer.record_cost(cost)?;

src/standard/invoker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ where
281281
mut machine: GasedMachine<S, G>,
282282
handler: &mut H,
283283
) -> Result<Self::TransactValue, ExitError> {
284-
let left_gas = machine.gasometer.gas();
284+
let left_gas = machine.gasometer.effective_gas();
285285

286286
let work = || -> Result<Self::TransactValue, ExitError> {
287287
if result.is_ok() {

0 commit comments

Comments
 (0)