Skip to content

Commit

Permalink
Fix issue-222
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann committed Nov 20, 2023
1 parent d543f10 commit 168de4a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
8 changes: 8 additions & 0 deletions gasometer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ impl<'config> Gasometer<'config> {
Ok(())
}

#[inline]
/// Check gas limit before `CREATE` code deposit. (See EIP-2)
pub fn can_deposit(&self, len: usize) -> bool {
let cost = len as u64 * consts::G_CODEDEPOSIT;
let all_gas_cost = self.total_used_gas() + cost;
self.gas_limit >= all_gas_cost
}

#[inline]
/// Record `CREATE` code deposit.
pub fn record_deposit(&mut self, len: usize) -> Result<(), ExitError> {
Expand Down
22 changes: 15 additions & 7 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>

match reason {
ExitReason::Succeed(s) => {
let out = return_data;
let mut out = return_data;
let address = created_address;
// As of EIP-3541 code starting with 0xef cannot be deployed
if let Err(e) = check_first_byte(self.config, &out) {
Expand All @@ -1002,12 +1002,20 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
}
}

match self
.state
.metadata_mut()
.gasometer
.record_deposit(out.len())
{
// Before EIP-2 is possible to create empty contract
let result = if self.config.empty_considered_exists {
if self.state.metadata().gasometer.can_deposit(out.len()) {
out.clear();
}
Ok(())
} else {
self.state
.metadata_mut()
.gasometer
.record_deposit(out.len())
};

match result {
Ok(()) => {
let exit_result = self.exit_substate(StackExitKind::Succeeded);
if let Err(e) = self.record_external_operation(
Expand Down

0 comments on commit 168de4a

Please sign in to comment.