Skip to content

Commit

Permalink
fix: return error if amount is greater than 2^128 (#681)
Browse files Browse the repository at this point in the history
* fix: return error if amount is greater than `2^128`

* Update engine-precompiles/src/native.rs

Co-authored-by: Joshua J. Bouw <joshua@aurora.dev>

* Update engine-precompiles/src/native.rs

* chore: remove unused constant

* chore: should panic with error msg if amount > 2^128

* fix: remove `validate_amount` from tests

---------

Co-authored-by: Joshua J. Bouw <joshua@aurora.dev>
  • Loading branch information
2 people authored and birchmd committed Apr 5, 2023
1 parent 98e6e97 commit 6352e5e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion engine-precompiles/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ fn get_nep141_from_erc20<I: IO>(erc20_token: &[u8], io: &I) -> Result<AccountId,
.map_err(|_| ExitError::Other(Cow::Borrowed("ERR_INVALID_NEP141_ACCOUNT")))
}

fn validate_amount(amount: U256) -> Result<(), ExitError> {
if amount > U256::from(u128::MAX) {
return Err(ExitError::Other(Cow::from("ERR_INVALID_AMOUNT")));
}
Ok(())
}

impl<I: IO> Precompile for ExitToNear<I> {
fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError> {
Ok(costs::EXIT_TO_NEAR_GAS)
Expand Down Expand Up @@ -331,6 +338,8 @@ impl<I: IO> Precompile for ExitToNear<I> {
let amount = U256::from_big_endian(&input[..32]);
input = &input[32..];

validate_amount(amount)?;

if let Ok(receiver_account_id) = AccountId::try_from(input) {
(
nep141_address,
Expand Down Expand Up @@ -519,6 +528,8 @@ impl<I: IO> Precompile for ExitToEthereum<I> {
let amount = U256::from_big_endian(&input[..32]);
input = &input[32..];

validate_amount(amount)?;

if input.len() == 20 {
// Parse ethereum address in hex
let eth_recipient: String = hex::encode(input);
Expand Down Expand Up @@ -585,8 +596,9 @@ impl<I: IO> Precompile for ExitToEthereum<I> {

#[cfg(test)]
mod tests {
use super::{exit_to_ethereum, exit_to_near};
use super::{exit_to_ethereum, exit_to_near, validate_amount};
use crate::prelude::sdk::types::near_account_to_evm_address;
use aurora_engine_types::U256;

#[test]
fn test_precompile_id() {
Expand Down Expand Up @@ -614,4 +626,15 @@ mod tests {
super::events::EXIT_TO_ETH_SIGNATURE
);
}

#[test]
#[should_panic(expected = "ERR_INVALID_AMOUNT")]
fn test_exit_with_invalid_amount() {
validate_amount(U256::MAX).unwrap();
}

#[test]
fn test_exit_with_valid_amount() {
validate_amount(U256::from(u128::MAX)).unwrap();
}
}

0 comments on commit 6352e5e

Please sign in to comment.