Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

calculate gas_used of the transaction trace correctly, #9194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
address: new_address,
sender: sender.clone(),
origin: sender.clone(),
entire_gas: t.gas,
gas: init_gas,
gas_price: t.gas_price,
value: ActionValue::Transfer(t.value),
Expand All @@ -316,6 +317,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
address: address.clone(),
sender: sender.clone(),
origin: sender.clone(),
entire_gas: t.gas,
gas: init_gas,
gas_price: t.gas_price,
value: ActionValue::Transfer(t.value),
Expand Down Expand Up @@ -469,7 +471,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
let mut trace_output = tracer.prepare_trace_output();
let mut subtracer = tracer.subtracer();

let gas = params.gas;
let entire_gas = params.entire_gas;

if params.code.is_some() {
// part of substate that may be reverted
Expand All @@ -490,7 +492,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
match res {
Ok(ref res) if res.apply_state => tracer.trace_call(
trace_info,
gas - res.gas_left,
entire_gas - res.gas_left,
trace_output,
traces
),
Expand Down Expand Up @@ -565,7 +567,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
let trace_info = tracer.prepare_trace_create(&params);
let mut trace_output = tracer.prepare_trace_output();
let mut subtracer = tracer.subtracer();
let gas = params.gas;
let entire_gas = params.entire_gas;
let created = params.address.clone();

let mut subvmtracer = vm_tracer.prepare_subtrace(params.code.as_ref().expect("two ways into create (Externalities::create and Executive::transact_with_tracer); both place `Some(...)` `code` in `params`; qed"));
Expand All @@ -584,7 +586,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
match res {
Ok(ref res) if res.apply_state => tracer.trace_create(
trace_info,
gas - res.gas_left,
entire_gas - res.gas_left,
trace_output.map(|data| output.as_ref().map(|out| out.to_vec()).unwrap_or(data)),
created,
subtracer.drain()
Expand Down Expand Up @@ -750,6 +752,7 @@ mod tests {
params.address = address.clone();
params.sender = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new("3331600055".from_hex().unwrap()));
params.value = ActionValue::Transfer(U256::from(0x7));
let mut state = get_temp_state_with_factory(factory);
Expand Down Expand Up @@ -807,6 +810,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100));
let mut state = get_temp_state_with_factory(factory);
Expand Down Expand Up @@ -848,6 +852,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100));
params.call_type = CallType::Call;
Expand Down Expand Up @@ -932,6 +937,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100));
params.call_type = CallType::Call;
Expand Down Expand Up @@ -1048,6 +1054,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100));
params.call_type = CallType::Call;
Expand Down Expand Up @@ -1121,6 +1128,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(100.into());
let mut state = get_temp_state();
Expand Down Expand Up @@ -1208,6 +1216,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100));
let mut state = get_temp_state_with_factory(factory);
Expand Down Expand Up @@ -1259,6 +1268,7 @@ mod tests {
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(100_000);
params.entire_gas = U256::from(100_000);
params.code = Some(Arc::new(code));
params.value = ActionValue::Transfer(U256::from(100));
let mut state = get_temp_state_with_factory(factory);
Expand Down Expand Up @@ -1606,6 +1616,7 @@ mod tests {
params.sender = sender.clone();
params.address = contract_address.clone();
params.gas = U256::from(20025);
params.entire_gas = U256::from(20025);
params.code = Some(wasm_sample_code());

let mut info = EnvInfo::default();
Expand Down
3 changes: 3 additions & 0 deletions ethcore/src/externalities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
value: ActionValue::Apparent(self.origin_info.value),
code_address: blockhash_contract_address.clone(),
origin: self.origin_info.origin.clone(),
entire_gas: self.machine.params().eip210_contract_gas,
gas: self.machine.params().eip210_contract_gas,
gas_price: 0.into(),
code: code,
Expand Down Expand Up @@ -208,6 +209,7 @@ impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
address: address.clone(),
sender: self.origin_info.address.clone(),
origin: self.origin_info.origin.clone(),
entire_gas: *gas,
gas: *gas,
gas_price: self.origin_info.gas_price,
value: ActionValue::Transfer(*value),
Expand Down Expand Up @@ -267,6 +269,7 @@ impl<'a, T: 'a, V: 'a, B: 'a> Ext for Externalities<'a, T, V, B>
value: ActionValue::Apparent(self.origin_info.value),
code_address: code_address.clone(),
origin: self.origin_info.origin.clone(),
entire_gas: *gas,
gas: *gas,
gas_price: self.origin_info.gas_price,
code: code,
Expand Down
1 change: 1 addition & 0 deletions ethcore/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl EthereumMachine {
address: contract_address.clone(),
sender: SYSTEM_ADDRESS.clone(),
origin: SYSTEM_ADDRESS.clone(),
entire_gas: gas,
gas: gas,
gas_price: 0.into(),
value: ActionValue::Transfer(0.into()),
Expand Down
1 change: 1 addition & 0 deletions ethcore/src/spec/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ impl Spec {
address: address.clone(),
sender: from.clone(),
origin: from.clone(),
entire_gas: U256::max_value(),
gas: U256::max_value(),
gas_price: Default::default(),
value: ActionValue::Transfer(Default::default()),
Expand Down
56 changes: 28 additions & 28 deletions ethcore/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,11 @@ mod tests {
action: trace::Action::Create(trace::Create {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
value: 100.into(),
gas: 77412.into(),
gas: 100_000.into(),
init: vec![96, 16, 128, 96, 12, 96, 0, 57, 96, 0, 243, 0, 96, 0, 53, 84, 21, 96, 9, 87, 0, 91, 96, 32, 53, 96, 0, 53, 85],
}),
result: trace::Res::Create(trace::CreateResult {
gas_used: U256::from(3224),
gas_used: U256::from(25_812),
address: Address::from_str("8988167e088c87cd314df6d3c2b83da5acb93ace").unwrap(),
code: vec![96, 0, 53, 84, 21, 96, 9, 87, 0, 91, 96, 32, 53, 96, 0, 53]
}),
Expand Down Expand Up @@ -1232,7 +1232,7 @@ mod tests {
action: trace::Action::Create(trace::Create {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
value: 100.into(),
gas: 78792.into(),
gas: 100_000.into(),
init: vec![91, 96, 0, 86],
}),
result: trace::Res::FailedCreate(TraceError::OutOfGas),
Expand Down Expand Up @@ -1270,12 +1270,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(3),
gas_used: 21_003.into(),
output: vec![]
}),
subtraces: 0,
Expand Down Expand Up @@ -1311,7 +1311,7 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
Expand Down Expand Up @@ -1352,7 +1352,7 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: "0000000000000000000000000000000000000001".into(),
value: 0.into(),
gas: 79_000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
Expand Down Expand Up @@ -1394,12 +1394,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 0.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(3_721), // in post-eip150
gas_used: 24_721.into(), // in post-eip150
output: vec![]
}),
subtraces: 0,
Expand Down Expand Up @@ -1438,12 +1438,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 0.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: 724.into(), // in post-eip150
gas_used: 21_724.into(), // in post-eip150
output: vec![]
}),
}, FlatTrace {
Expand Down Expand Up @@ -1497,12 +1497,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 0.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(736), // in post-eip150
gas_used: 21_736.into(), // in post-eip150
output: vec![]
}),
}, FlatTrace {
Expand Down Expand Up @@ -1553,7 +1553,7 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
Expand Down Expand Up @@ -1595,12 +1595,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(69),
gas_used: 21_069.into(),
output: vec![]
}),
}, FlatTrace {
Expand Down Expand Up @@ -1652,12 +1652,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(31761),
gas_used: 52_761.into(),
output: vec![]
}),
}, FlatTrace {
Expand Down Expand Up @@ -1706,12 +1706,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(31761),
gas_used: U256::from(52_761),
output: vec![]
}),
}];
Expand Down Expand Up @@ -1749,12 +1749,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(79_000),
gas_used: 100_000.into(),
output: vec![]
}),
}, FlatTrace {
Expand Down Expand Up @@ -1805,12 +1805,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(135),
gas_used: 21_135.into(),
output: vec![]
}),
}, FlatTrace {
Expand Down Expand Up @@ -1880,12 +1880,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: U256::from(79_000),
gas_used: 100_000.into(),
output: vec![]
})
}, FlatTrace {
Expand Down Expand Up @@ -1950,12 +1950,12 @@ mod tests {
from: "9cce34f7ab185c7aba1b7c8140d620b4bda941d6".into(),
to: 0xa.into(),
value: 100.into(),
gas: 79000.into(),
gas: 100_000.into(),
input: vec![],
call_type: CallType::Call,
}),
result: trace::Res::Call(trace::CallResult {
gas_used: 3.into(),
gas_used: 21_003.into(),
output: vec![]
}),
}, FlatTrace {
Expand Down
2 changes: 2 additions & 0 deletions ethcore/src/tests/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn test_blockhash_eip210(factory: Factory) {
address: contract_address,
sender: SYSTEM_ADDRESS.clone(),
origin: SYSTEM_ADDRESS.clone(),
entire_gas: 100000.into(),
gas: 100000.into(),
gas_price: 0.into(),
value: ActionValue::Transfer(0.into()),
Expand All @@ -76,6 +77,7 @@ fn test_blockhash_eip210(factory: Factory) {
address: Address::new(),
sender: Address::new(),
origin: Address::new(),
entire_gas: 100000.into(),
gas: 100000.into(),
gas_price: 0.into(),
value: ActionValue::Transfer(0.into()),
Expand Down
Loading