Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precompile used gas #175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 14 additions & 5 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>

let target_gas = target_gas.unwrap_or(after_gas);
let mut gas_limit = min(target_gas, after_gas);
// Record the gas used so far to propagate into the precompile.
let parent_gas_used = self.used_gas();

try_or_fail!(self.state.metadata_mut().gasometer.record_cost(gas_limit));

Expand Down Expand Up @@ -925,6 +927,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
gas_limit: Some(gas_limit),
context: &context,
is_static: precompile_is_static,
parent_gas_used,
}) {
return match result {
Ok(PrecompileOutput {
Expand Down Expand Up @@ -1366,6 +1369,7 @@ struct StackExecutorHandle<'inner, 'config, 'precompiles, S, P> {
gas_limit: Option<u64>,
context: &'inner Context,
is_static: bool,
parent_gas_used: u64,
}

impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> PrecompileHandle
Expand Down Expand Up @@ -1479,7 +1483,7 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
.refund_external_cost(ref_time, proof_size);
}

/// Retreive the remaining gas.
/// Retrieve the remaining gas.
fn remaining_gas(&self) -> u64 {
self.executor.state.metadata().gasometer.gas()
}
Expand All @@ -1489,17 +1493,17 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
Handler::log(self.executor, address, topics, data)
}

/// Retreive the code address (what is the address of the precompile being called).
/// Retrieve the code address (what is the address of the precompile being called).
fn code_address(&self) -> H160 {
self.code_address
}

/// Retreive the input data the precompile is called with.
/// Retrieve the input data the precompile is called with.
fn input(&self) -> &[u8] {
self.input
}

/// Retreive the context in which the precompile is executed.
/// Retrieve the context in which the precompile is executed.
fn context(&self) -> &Context {
self.context
}
Expand All @@ -1509,8 +1513,13 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr
self.is_static
}

/// Retreive the gas limit of this call.
/// Retrieve the gas limit of this call.
fn gas_limit(&self) -> Option<u64> {
self.gas_limit
}

// Retrieve the used gas.
fn used_gas(&self) -> u64 {
self.executor.used_gas() + self.parent_gas_used
}
}
13 changes: 8 additions & 5 deletions src/executor/stack/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,29 @@ pub trait PrecompileHandle {
/// Refund Substrate specific cost.
fn refund_external_cost(&mut self, ref_time: Option<u64>, proof_size: Option<u64>);

/// Retreive the remaining gas.
/// Retrieve the remaining gas.
fn remaining_gas(&self) -> u64;

/// Record a log.
fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError>;

/// Retreive the code address (what is the address of the precompile being called).
/// Retrieve the code address (what is the address of the precompile being called).
fn code_address(&self) -> H160;

/// Retreive the input data the precompile is called with.
/// Retrieve the input data the precompile is called with.
fn input(&self) -> &[u8];

/// Retreive the context in which the precompile is executed.
/// Retrieve the context in which the precompile is executed.
fn context(&self) -> &Context;

/// Is the precompile call is done statically.
fn is_static(&self) -> bool;

/// Retreive the gas limit of this call.
/// Retrieve the gas limit of this call.
fn gas_limit(&self) -> Option<u64>;

/// Retrieve the gas used.
fn used_gas(&self) -> u64;
}

/// A set of precompiles.
Expand Down
Loading