-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Engine): Add custom precompile for NEAR prepaid_gas (#479)
- Loading branch information
Showing
7 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use super::{EvmPrecompileResult, Precompile}; | ||
use crate::prelude::types::{Address, EthGas}; | ||
use crate::PrecompileOutput; | ||
use aurora_engine_sdk::env::Env; | ||
use aurora_engine_types::{vec, U256}; | ||
use evm::{Context, ExitError}; | ||
|
||
/// predecessor_account_id precompile address | ||
/// | ||
/// Address: `0x536822d27de53629ef1f84c60555689e9488609f` | ||
/// This address is computed as: `&keccak("prepaidGas")[12..]` | ||
pub const ADDRESS: Address = crate::make_address(0x536822d2, 0x7de53629ef1f84c60555689e9488609f); | ||
|
||
mod costs { | ||
use crate::prelude::types::EthGas; | ||
|
||
// TODO(#483): Determine the correct amount of gas | ||
pub(super) const PREPAID_GAS_COST: EthGas = EthGas::new(0); | ||
} | ||
|
||
pub struct PrepaidGas<'a, E> { | ||
env: &'a E, | ||
} | ||
|
||
impl<'a, E> PrepaidGas<'a, E> { | ||
pub fn new(env: &'a E) -> Self { | ||
Self { env } | ||
} | ||
} | ||
|
||
impl<'a, E: Env> Precompile for PrepaidGas<'a, E> { | ||
fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError> { | ||
Ok(costs::PREPAID_GAS_COST) | ||
} | ||
|
||
fn run( | ||
&self, | ||
input: &[u8], | ||
target_gas: Option<EthGas>, | ||
_context: &Context, | ||
_is_static: bool, | ||
) -> EvmPrecompileResult { | ||
let cost = Self::required_gas(input)?; | ||
if let Some(target_gas) = target_gas { | ||
if cost > target_gas { | ||
return Err(ExitError::OutOfGas); | ||
} | ||
} | ||
|
||
let prepaid_gas = self.env.prepaid_gas(); | ||
let bytes = { | ||
let mut buf = vec![0; 32]; | ||
U256::from(prepaid_gas.as_u64()).to_big_endian(&mut buf); | ||
buf | ||
}; | ||
Ok(PrecompileOutput::without_logs(cost, bytes).into()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::prelude::sdk::types::near_account_to_evm_address; | ||
use crate::prepaid_gas; | ||
|
||
#[test] | ||
fn test_prepaid_gas_precompile_id() { | ||
assert_eq!( | ||
prepaid_gas::ADDRESS, | ||
near_account_to_evm_address("prepaidGas".as_bytes()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::test_utils::{self, standalone}; | ||
use aurora_engine_precompiles::prepaid_gas; | ||
use aurora_engine_transactions::legacy::TransactionLegacy; | ||
use aurora_engine_types::{types::Wei, U256}; | ||
|
||
#[test] | ||
fn test_prepaid_gas_precompile() { | ||
let mut signer = test_utils::Signer::random(); | ||
let mut runner = test_utils::deploy_evm(); | ||
let mut standalone = standalone::StandaloneRunner::default(); | ||
|
||
standalone.init_evm(); | ||
runner.standalone_runner = Some(standalone); | ||
|
||
let transaction = TransactionLegacy { | ||
nonce: signer.use_nonce().into(), | ||
gas_price: U256::zero(), | ||
gas_limit: u64::MAX.into(), | ||
to: Some(prepaid_gas::ADDRESS), | ||
value: Wei::zero(), | ||
data: Vec::new(), | ||
}; | ||
|
||
const EXPECTED_VALUE: u64 = 157_277_246_352_223; | ||
runner.context.prepaid_gas = EXPECTED_VALUE; | ||
let result = runner | ||
.submit_transaction(&signer.secret_key, transaction.clone()) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
U256::from(EXPECTED_VALUE), | ||
U256::from_big_endian(test_utils::unwrap_success_slice(&result)), | ||
); | ||
|
||
// confirm the precompile works in view calls too | ||
let sender = test_utils::address_from_secret_key(&signer.secret_key); | ||
let result = runner | ||
.view_call(test_utils::as_view_call(transaction, sender)) | ||
.unwrap(); | ||
assert!(result.is_ok()); | ||
} |