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

Eth Address cleanup #387

Merged
merged 28 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b2cb328
Added address file
mrLSD Dec 14, 2021
94d7c7f
Changed basic Address dependency
mrLSD Dec 14, 2021
f493404
Changed Addess dependecies
mrLSD Dec 14, 2021
c1204c8
Added AddressConst
mrLSD Dec 14, 2021
edc99ad
Fix address types for engine
mrLSD Dec 14, 2021
bdbcb45
Removed EthAddress type
mrLSD Dec 15, 2021
99194ec
Changed Address const methods
mrLSD Dec 15, 2021
05f7986
Remove old Address type invokes
mrLSD Dec 15, 2021
b98046d
Engine - Address changes
mrLSD Dec 16, 2021
88bba7d
Fix Connector, Engine types for Address
mrLSD Dec 16, 2021
3186c30
Changed tests
mrLSD Dec 16, 2021
fa23f14
Fix tests
mrLSD Dec 16, 2021
175f406
Fix tests
mrLSD Dec 16, 2021
f2187af
Fix test
mrLSD Dec 16, 2021
2d9434e
Fix Address BorshDeserialize
mrLSD Dec 16, 2021
d3261b1
Fix borsh tests
mrLSD Dec 16, 2021
6729ea7
Fix bully
mrLSD Dec 16, 2021
d70dc41
Merge branch 'develop' of github.com:aurora-is-near/aurora-engine int…
mrLSD Dec 17, 2021
21b1a9c
Changed type for SignedTransaction1559. Added test_address_deocde
mrLSD Dec 20, 2021
2c50149
Update engine/src/engine.rs
mrLSD Dec 20, 2021
617bfbe
Changed method from_array
mrLSD Dec 20, 2021
29b5ed0
Changed Address from_array
mrLSD Dec 20, 2021
193b9c7
Merge branch 'feat/eth-addrass-cleanup' of github.com:aurora-is-near/…
mrLSD Dec 20, 2021
8fab0e6
Remove and Rename ADDRESS to Address::new
mrLSD Dec 21, 2021
2dfde80
Update engine-types/src/types/address.rs
mrLSD Dec 22, 2021
282d527
Update engine-types/src/types/address.rs
mrLSD Dec 22, 2021
7cf3a3c
Update engine/src/engine.rs
mrLSD Dec 22, 2021
28f0bda
Added decode reference
mrLSD Dec 22, 2021
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
2 changes: 1 addition & 1 deletion engine-precompiles/src/blake2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use evm::{Context, ExitError};

use crate::prelude::types::EthGas;
use crate::prelude::{mem, Address, Borrowed, TryInto};
use crate::prelude::{mem, types_new::Address, Borrowed, TryInto};
use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};

/// Blake2 costs.
Expand Down
2 changes: 1 addition & 1 deletion engine-precompiles/src/bn128.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::types::EthGas;
use crate::prelude::{Address, Borrowed, PhantomData, Vec};
use crate::prelude::{types_new::Address, Borrowed, PhantomData, Vec};
use crate::{Byzantium, EvmPrecompileResult, HardFork, Istanbul, Precompile, PrecompileOutput};
use evm::{Context, ExitError};

Expand Down
3 changes: 2 additions & 1 deletion engine-precompiles/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[cfg(feature = "contract")]
use crate::prelude::sdk;
use crate::prelude::types::EthGas;
use crate::prelude::{vec, Address};
use crate::prelude::types_new::Address;
use crate::prelude::vec;
use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};
use evm::{Context, ExitError};

Expand Down
2 changes: 1 addition & 1 deletion engine-precompiles/src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::types::EthGas;
use crate::prelude::Address;
use crate::prelude::types_new::Address;
use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};
use evm::{Context, ExitError};

Expand Down
34 changes: 18 additions & 16 deletions engine-precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use crate::identity::Identity;
use crate::modexp::ModExp;
use crate::native::{ExitToEthereum, ExitToNear};
use crate::prelude::types::EthGas;
use crate::prelude::{Vec, H256};
use crate::prelude::{Vec, H160, H256};
use crate::random::RandomSeed;
use crate::secp256k1::ECRecover;
use aurora_engine_types::{account_id::AccountId, vec, Address, BTreeMap, Box};
use aurora_engine_types::{account_id::AccountId, types_new::Address, vec, BTreeMap, Box};
use evm::backend::Log;
use evm::executor;
use evm::{Context, ExitError, ExitSucceed};
Expand Down Expand Up @@ -106,20 +106,20 @@ pub struct Precompiles(pub prelude::BTreeMap<Address, Box<dyn Precompile>>);
impl executor::PrecompileSet for Precompiles {
fn execute(
&self,
address: prelude::Address,
address: prelude::H160,
input: &[u8],
gas_limit: Option<u64>,
context: &Context,
is_static: bool,
) -> Option<Result<executor::PrecompileOutput, executor::PrecompileFailure>> {
self.0.get(&address).map(|p| {
self.0.get(&Address::new(address)).map(|p| {
p.run(input, gas_limit.map(EthGas::new), context, is_static)
.map_err(|exit_status| executor::PrecompileFailure::Error { exit_status })
})
}

fn is_precompile(&self, address: prelude::Address) -> bool {
self.0.contains_key(&address)
fn is_precompile(&self, address: prelude::H160) -> bool {
self.0.contains_key(&Address::new(address))
}
}

Expand Down Expand Up @@ -259,13 +259,13 @@ impl Precompiles {
}
}

/// const fn for making an address by concatenating the bytes from two given numbers,
/// fn for making an address by concatenating the bytes from two given numbers,
/// Note that 32 + 128 = 160 = 20 bytes (the length of an address). This function is used
/// as a convenience for specifying the addresses of the various precompiles.
pub const fn make_address(x: u32, y: u128) -> prelude::Address {
pub const fn make_address(x: u32, y: u128) -> prelude::types_new::Address {
let x_bytes = x.to_be_bytes();
let y_bytes = y.to_be_bytes();
prelude::Address([
prelude::types_new::ADDRESS(H160([
x_bytes[0],
x_bytes[1],
x_bytes[2],
Expand All @@ -286,7 +286,7 @@ pub const fn make_address(x: u32, y: u128) -> prelude::Address {
y_bytes[13],
y_bytes[14],
y_bytes[15],
])
]))
}

const fn make_h256(x: u128, y: u128) -> prelude::H256 {
Expand Down Expand Up @@ -330,7 +330,9 @@ const fn make_h256(x: u128, y: u128) -> prelude::H256 {

#[cfg(test)]
mod tests {
use crate::prelude::H160;
use crate::{prelude, Byzantium, Istanbul};
use prelude::types_new::{Address, ADDRESS};
use rand::Rng;

#[test]
Expand All @@ -354,25 +356,25 @@ mod tests {

let mut rng = rand::thread_rng();
for _ in 0..u8::MAX {
let address: prelude::Address = prelude::Address(rng.gen());
let address = ADDRESS(H160(rng.gen()));
let (x, y) = split_address(address);
assert_eq!(address, super::make_address(x, y))
}
}

fn u8_to_address(x: u8) -> prelude::Address {
fn u8_to_address(x: u8) -> Address {
let mut bytes = [0u8; 20];
bytes[19] = x;
prelude::Address(bytes)
ADDRESS(H160(bytes))
}

// Inverse function of `super::make_address`.
fn split_address(a: prelude::Address) -> (u32, u128) {
fn split_address(a: Address) -> (u32, u128) {
let mut x_bytes = [0u8; 4];
let mut y_bytes = [0u8; 16];

x_bytes.copy_from_slice(&a[0..4]);
y_bytes.copy_from_slice(&a[4..20]);
x_bytes.copy_from_slice(&a.raw()[0..4]);
y_bytes.copy_from_slice(&a.raw()[4..20]);

(u32::from_be_bytes(x_bytes), u128::from_be_bytes(y_bytes))
}
Expand Down
2 changes: 1 addition & 1 deletion engine-precompiles/src/modexp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::{Address, PhantomData, Vec, U256};
use crate::prelude::{types_new::Address, PhantomData, Vec, U256};
use crate::{Berlin, Byzantium, EvmPrecompileResult, HardFork, Precompile, PrecompileOutput};

use crate::prelude::types::EthGas;
Expand Down
43 changes: 22 additions & 21 deletions engine-precompiles/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::prelude::{
parameters::{PromiseArgs, PromiseCreateArgs, WithdrawCallArgs},
sdk,
storage::{bytes_to_key, KeyPrefix},
vec, BorshSerialize, Cow, String, ToString, TryFrom, TryInto, Vec, H160, U256,
vec, BorshSerialize, Cow, String, ToString, TryFrom, TryInto, Vec, U256,
};
#[cfg(all(feature = "error_refund", feature = "contract"))]
use crate::prelude::{
Expand All @@ -14,7 +14,7 @@ use crate::prelude::{
};

use crate::prelude::types::EthGas;
use crate::prelude::Address;
use crate::prelude::types_new::Address;
use crate::PrecompileOutput;
use aurora_engine_types::account_id::AccountId;
#[cfg(feature = "contract")]
Expand Down Expand Up @@ -44,7 +44,8 @@ mod costs {
}

pub mod events {
use crate::prelude::{vec, Address, String, ToString, H256, U256};
use crate::prelude::{types_new::Address, vec, String, ToString, H160, H256, U256};
use aurora_engine_types::types_new::ADDRESS;

/// Derived from event signature (see tests::test_exit_signatures)
pub const EXIT_TO_NEAR_SIGNATURE: H256 = crate::make_h256(
Expand All @@ -61,7 +62,7 @@ pub mod events {
/// which ERC-20 token is being withdrawn. However, ETH is not an ERC-20 token
/// So we need to have some other address to fill this field. This constant is
/// used for this purpose.
pub const ETH_ADDRESS: Address = Address([0; 20]);
pub const ETH_ADDRESS: Address = ADDRESS(H160([0; 20]));

/// ExitToNear(
/// Address indexed sender,
Expand Down Expand Up @@ -127,7 +128,7 @@ pub mod events {

fn encode_address(a: Address) -> H256 {
let mut result = [0u8; 32];
result[12..].copy_from_slice(a.as_ref());
result[12..].copy_from_slice(a.as_bytes());
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
H256(result)
}

Expand Down Expand Up @@ -301,7 +302,7 @@ impl Precompile for ExitToNear {
context.apparent_value.as_u128()
),
events::ExitToNear {
sender: context.caller,
sender: Address::new(context.caller),
erc20_address: events::ETH_ADDRESS,
dest: dest_account.to_string(),
amount: context.apparent_value,
Expand Down Expand Up @@ -345,8 +346,8 @@ impl Precompile for ExitToNear {
amount.as_u128()
),
events::ExitToNear {
sender: erc20_address,
erc20_address,
sender: Address::new(erc20_address),
erc20_address: Address::new(erc20_address),
dest: receiver_account_id.to_string(),
amount,
},
Expand All @@ -364,11 +365,11 @@ impl Precompile for ExitToNear {
let erc20_address = if flag == 0 {
None
} else {
Some(exit_event.erc20_address.0)
Some(exit_event.erc20_address)
};
#[cfg(feature = "error_refund")]
let refund_args = RefundCallArgs {
recipient_address: refund_address.0,
recipient_address: refund_address,
erc20_address,
amount: types::u256_to_arr(&exit_event.amount),
};
Expand Down Expand Up @@ -397,13 +398,13 @@ impl Precompile for ExitToNear {
let promise = PromiseArgs::Create(transfer_promise);

let promise_log = Log {
address: Self::ADDRESS,
address: Self::ADDRESS.raw(),
topics: Vec::new(),
data: promise.try_to_vec().unwrap(),
};
let exit_event_log = exit_event.encode();
let exit_event_log = Log {
address: Self::ADDRESS,
address: Self::ADDRESS.raw(),
topics: exit_event_log.topics,
data: exit_event_log.data,
};
Expand Down Expand Up @@ -487,7 +488,7 @@ impl Precompile for ExitToEthereum {
//
// Input slice format:
// eth_recipient (20 bytes) - the address of recipient which will receive ETH on Ethereum
let recipient_address = input
let recipient_address: Address = input
.try_into()
.map_err(|_| ExitError::Other(Cow::from("ERR_INVALID_RECIPIENT_ADDRESS")))?;
(
Expand All @@ -501,9 +502,9 @@ impl Precompile for ExitToEthereum {
.try_to_vec()
.map_err(|_| ExitError::Other(Cow::from("ERR_INVALID_AMOUNT")))?,
events::ExitToEth {
sender: context.caller,
sender: Address::new(context.caller),
erc20_address: events::ETH_ADDRESS,
dest: H160(recipient_address),
dest: recipient_address,
amount: context.apparent_value,
},
)
Expand Down Expand Up @@ -534,7 +535,7 @@ impl Precompile for ExitToEthereum {
// Parse ethereum address in hex
let eth_recipient: String = hex::encode(input.to_vec());
// unwrap cannot fail since we checked the length already
let recipient_address = input.try_into().unwrap();
let recipient_address = Address::from_slice(input);

(
nep141_address,
Expand All @@ -548,9 +549,9 @@ impl Precompile for ExitToEthereum {
.as_bytes()
.to_vec(),
events::ExitToEth {
sender: erc20_address,
erc20_address,
dest: H160(recipient_address),
sender: Address::new(erc20_address),
erc20_address: Address::new(erc20_address),
dest: recipient_address,
amount,
},
)
Expand All @@ -575,13 +576,13 @@ impl Precompile for ExitToEthereum {

let promise = PromiseArgs::Create(withdraw_promise).try_to_vec().unwrap();
let promise_log = Log {
address: Self::ADDRESS,
address: Self::ADDRESS.raw(),
topics: Vec::new(),
data: promise,
};
let exit_event_log = exit_event.encode();
let exit_event_log = Log {
address: Self::ADDRESS,
address: Self::ADDRESS.raw(),
topics: exit_event_log.topics,
data: exit_event_log.data,
};
Expand Down
3 changes: 2 additions & 1 deletion engine-precompiles/src/random.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{EvmPrecompileResult, Precompile};
use crate::prelude::types::EthGas;
use crate::prelude::{Address, H256};
use crate::prelude::types_new::Address;
use crate::prelude::H256;
use crate::PrecompileOutput;
use evm::{Context, ExitError};

Expand Down
2 changes: 1 addition & 1 deletion engine-precompiles/src/secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::types::EthGas;
use crate::prelude::types_new::Address;
use crate::prelude::{sdk, vec, Borrowed, H256};
use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};
use ethabi::Address;
use evm::{Context, ExitError};

mod costs {
Expand Down
2 changes: 1 addition & 1 deletion engine-sdk/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub use aurora_engine_types::types::{NearGas, PromiseResult, STORAGE_PRICE_PER_BYTE};
pub use aurora_engine_types::{vec, Address, Vec, H256};
pub use aurora_engine_types::{types_new::Address, vec, Vec, H256};
pub use borsh::{BorshDeserialize, BorshSerialize};
2 changes: 1 addition & 1 deletion engine-standalone-storage/src/relayer_db/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use aurora_engine::transaction::{
legacy::{LegacyEthSignedTransaction, TransactionLegacy},
EthTransactionKind,
};
use aurora_engine_types::{types::Wei, Address, H256, U256};
use aurora_engine_types::{types::Wei, types_new::Address, H256, U256};
use std::convert::TryFrom;
use std::io::{Cursor, Read};
use std::time::SystemTime;
Expand Down
4 changes: 2 additions & 2 deletions engine-tests/src/benches/nft_pagination.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::types::Wei;
use crate::prelude::{Address, U256};
use crate::prelude::{types_new::Address, U256};
use crate::test_utils::{self, solidity};
use aurora_engine::transaction::legacy::TransactionLegacy;
use secp256k1::SecretKey;
Expand Down Expand Up @@ -134,7 +134,7 @@ impl MarketPlace {
self.0.call_method_with_args(
"minar",
&[
ethabi::Token::Address(recipient),
ethabi::Token::Address(recipient.raw()),
ethabi::Token::String(data),
ethabi::Token::Uint(price.raw()),
],
Expand Down
13 changes: 8 additions & 5 deletions engine-tests/src/test_utils/erc20.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::{transaction::legacy::TransactionLegacy, Address, U256};
use crate::prelude::{transaction::legacy::TransactionLegacy, types_new::Address, U256};
use crate::test_utils::solidity;
use std::path::{Path, PathBuf};
use std::sync::Once;
Expand Down Expand Up @@ -78,7 +78,7 @@ impl ERC20 {
.function("mint")
.unwrap()
.encode_input(&[
ethabi::Token::Address(recipient),
ethabi::Token::Address(recipient.raw()),
ethabi::Token::Uint(amount),
])
.unwrap();
Expand All @@ -100,7 +100,7 @@ impl ERC20 {
.function("transfer")
.unwrap()
.encode_input(&[
ethabi::Token::Address(recipient),
ethabi::Token::Address(recipient.raw()),
ethabi::Token::Uint(amount),
])
.unwrap();
Expand All @@ -120,7 +120,10 @@ impl ERC20 {
.abi
.function("approve")
.unwrap()
.encode_input(&[ethabi::Token::Address(spender), ethabi::Token::Uint(amount)])
.encode_input(&[
ethabi::Token::Address(spender.raw()),
ethabi::Token::Uint(amount),
])
.unwrap();
TransactionLegacy {
nonce,
Expand All @@ -138,7 +141,7 @@ impl ERC20 {
.abi
.function("balanceOf")
.unwrap()
.encode_input(&[ethabi::Token::Address(address)])
.encode_input(&[ethabi::Token::Address(address.raw())])
.unwrap();
TransactionLegacy {
nonce,
Expand Down
Loading