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

Improved ETH connector logic #36

Merged
merged 71 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
98b302a
Added prover & extended types
mrLSD Apr 2, 2021
97a3759
Mode Borsh args from types to parameters
mrLSD Apr 2, 2021
a889328
Merge branch 'master' of github.com:aurora-is-near/aurora-engine into…
mrLSD Apr 5, 2021
25154f0
Added fungible tokens
mrLSD Apr 5, 2021
a0942a2
Added eth-connector
mrLSD Apr 6, 2021
5876814
Modify assert for fee
mrLSD Apr 6, 2021
facc786
Fix formatting
mrLSD Apr 6, 2021
b055a54
Extend eth-conenctor with EVM token logic
mrLSD Apr 6, 2021
eb9e361
Changed eth-connector deposit logic
mrLSD Apr 6, 2021
fcc1332
Added changes for ETH deposit/withdraw and Engine changes
mrLSD Apr 6, 2021
978474b
Mint ETH-tokens
mrLSD Apr 7, 2021
041879e
Added: transfer_eth
mrLSD Apr 7, 2021
b5ffb23
ETH withdraw basic method
mrLSD Apr 7, 2021
a7e73f4
eth-withdraw validation structure & modified Deposit-eth fields
mrLSD Apr 8, 2021
45a5ca9
eth transfer and withdraw logic
mrLSD Apr 8, 2021
676a86c
eip712 message verifier - started encoding
mrLSD Apr 8, 2021
e345d71
added encode-packed
mrLSD Apr 9, 2021
de7f36d
virefy EIP712 message for withdraw
mrLSD Apr 9, 2021
f1f6479
Changed EIP712 message fields
mrLSD Apr 9, 2021
944b6e2
Modify logs for EIP712 messages
mrLSD Apr 9, 2021
1201a39
Test EIP712
mrLSD Apr 12, 2021
11dec10
Tests EIP712
mrLSD Apr 13, 2021
89f572c
Integration tests for encode_withdraw_eip712
mrLSD Apr 13, 2021
d6830ea
Integration tests for encode_withdraw_eip712
mrLSD Apr 13, 2021
dd1f8e1
EIP712-Withdraw: improvements and fixes.
sept-en Apr 14, 2021
96b3c0e
Extend tests for eth-connector
mrLSD Apr 14, 2021
348e5cc
Merge branch 'evm-deposit-withdraw' of github.com:aurora-is-near/auro…
mrLSD Apr 14, 2021
80f033b
eth-connector test deposit & balance & total_supply
mrLSD Apr 14, 2021
07251fd
Imporved tests
mrLSD Apr 15, 2021
2de5af8
FT tests
mrLSD Apr 15, 2021
e71d52a
Fixed verify_transfer_eip712
mrLSD Apr 16, 2021
09b79c1
Change test_withdraw_near
mrLSD Apr 16, 2021
f2e1f11
Tests for: ft_transfer, ft_transfer_call
mrLSD Apr 16, 2021
356ad42
test_eth_deposit_balance_total_supply
mrLSD Apr 16, 2021
6cae502
test and ifx: deposit_eth, withdraw_near
mrLSD Apr 17, 2021
d920662
References in fungible token (#29)
birchmd Apr 19, 2021
27f90ef
Fix: hide logging behind feature flag
birchmd Apr 19, 2021
3b8f0d5
Merge branch 'evm-deposit-withdraw' into improved-evm-token-logic
mrLSD Apr 21, 2021
1074daf
Remove eth-conenctor transfer methods and deposit for new design
mrLSD Apr 21, 2021
5d26090
Completed Deposit logic
mrLSD Apr 21, 2021
628dfd8
Merge branch 'eth-connector' of github.com:aurora-is-near/aurora-engi…
mrLSD Apr 22, 2021
8b8043c
Fix clippy; added comments; improved ft_transfer_call
mrLSD Apr 22, 2021
41011e9
Extend external functions for eth-connector
mrLSD Apr 23, 2021
a4c1cc1
Added deploy_evm_token
mrLSD Apr 23, 2021
afb5291
Added ft_on_transfer logic
mrLSD Apr 23, 2021
a7b5d45
Changed ft_on_transfer & remove json depends
mrLSD Apr 23, 2021
886c7f9
Changed deposit logic and fixed transfers
mrLSD Apr 26, 2021
402223c
Added register relayer
mrLSD Apr 26, 2021
6c100d0
Added message coder for ft_transfer_call
mrLSD Apr 26, 2021
4217a5a
ft_on_transfer - added logic for erc20
mrLSD Apr 27, 2021
919f704
Impoved ft_on_transfer
mrLSD Apr 27, 2021
a662f50
ft_on_transfer: call erc20 contract adn send fee to Relayer. Added logs
mrLSD Apr 27, 2021
909b295
eth-connector: Removed unsued methods
mrLSD Apr 28, 2021
a0a02b1
tests: deposit & fixed init test
mrLSD Apr 28, 2021
c056e59
tests: depoist, withdraw
mrLSD Apr 29, 2021
9eac293
tests: fix test_withdraw_near
mrLSD Apr 29, 2021
253fb11
Eth-connector: never skip bridge call.
sept-en Apr 29, 2021
5b46752
Tests: fix ft_transfer_call
mrLSD Apr 29, 2021
1148981
ft_transfer_call - changed gas amountr
mrLSD Apr 29, 2021
fd7f31c
Fixed: test_eth_deposit_balance_total_supply, test_ft_transfer
mrLSD Apr 29, 2021
bae83f0
Merge branch 'improved-evm-token-logic' of github.com:aurora-is-near/…
mrLSD Apr 29, 2021
e97e7a8
Added: test_ft_transfer_call_near_eth
mrLSD Apr 29, 2021
f3f8e22
Clippy fix
mrLSD Apr 29, 2021
6bc6d3e
Added test_ft_transfer_call_erc20
mrLSD Apr 30, 2021
e98cd2b
Added test_ft_transfer_call_erc20
mrLSD Apr 30, 2021
da067eb
tests: ft_transfer_call for ERC20 changes
mrLSD Apr 30, 2021
95c80ff
Fix finish_deposit - promise flow when failed for ft_transfer_call
mrLSD May 1, 2021
8fbb523
added: test_deposit_with_same_proof
mrLSD May 3, 2021
cdf2e84
Improved EVM token master branch update (#50)
joshuajbouw May 5, 2021
5a0e427
Update src/connector.rs misspel
mrLSD May 5, 2021
79602e5
Update src/connector.rs change constants error
mrLSD May 5, 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
1,492 changes: 1,191 additions & 301 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ homepage = "https://github.com/aurora-is-near/aurora-engine"
repository = "https://github.com/aurora-is-near/aurora-engine"
license = "CC0-1.0"
publish = false
autobenches = false

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down Expand Up @@ -37,9 +38,12 @@ codegen-units = 1
rpath = false

[dependencies]
blake2 = { git = "https://github.com/near/near-blake2.git", version = "0.9.1", default-features = false }
borsh = { version = "0.8.2", default-features = false }
bn = { package = "aurora-bn", git = "https://github.com/aurora-is-near/aurora-bn.git", default-features = false }
evm = { git = "https://github.com/aurora-is-near/sputnikvm", rev = "2a8a3e9", default-features = false }
libsecp256k1 = { version = "0.3.5", default-features = false }
num = { version = "0.4.0", default-features = false, features = ["alloc"] }
primitive-types = { version = "0.9.0", default-features = false, features = ["rlp"] }
ripemd160 = { version = "0.9.1", default-features = false }
rlp = { version = "0.5.0", default-features = false }
Expand All @@ -49,17 +53,30 @@ wee_alloc = { version = "0.4.5", default-features = false }
lunarity-lexer = { git = "https://github.com/ilblackdragon/lunarity", rev = "5201d9a76f7e491082b7f74af7e64049271e387f", default-features = false }
ethabi = { git = "https://github.com/darwinia-network/ethabi", branch = "xavier-no-std", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
rjson = { version = "0.3.1", default-features = false }
byte-slice-cast = { version = "1.0", default-features = false }

[dev-dependencies]
hex = { version = "0.4.3", default-features = false }
near-sdk = { git = "https://github.com/near/near-sdk-rs", rev = "9d99077c6acfde68c06845f2a1eb2b5ed7983401" }
near-sdk-sim = { git = "https://github.com/near/near-sdk-rs", rev = "9d99077c6acfde68c06845f2a1eb2b5ed7983401" }
near-crypto = "0.1.0"
near-vm-runner = { git = "https://github.com/near/nearcore", rev = "3744f07e13bf43a9522fb39fa8f6f128396d0e1f" }
near-primitives-core = { git = "https://github.com/near/nearcore", rev = "3744f07e13bf43a9522fb39fa8f6f128396d0e1f" }
near-vm-logic = { git = "https://github.com/near/nearcore", rev = "3744f07e13bf43a9522fb39fa8f6f128396d0e1f" }
libsecp256k1 = "0.3.5"
rand = "0.7.3"
criterion = "0.3.4"
git2 = "0.13"

[[bench]]
name = "benches"
path = "benches/main.rs"
harness = false

[features]
default = ["sha2", "std"]
std = ["borsh/std", "evm/std", "primitive-types/std", "rlp/std", "sha3/std", "ethabi/std", "lunarity-lexer/std"]
std = ["borsh/std", "evm/std", "primitive-types/std", "rlp/std", "sha3/std", "ethabi/std", "lunarity-lexer/std", "bn/std"]
testnet = []
contract = []
evm_bully = []
log = []
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
[![Builds](https://github.com/aurora-is-near/aurora-engine/actions/workflows/builds.yml/badge.svg)](https://github.com/aurora-is-near/aurora-engine/actions/workflows/builds.yml)

Aurora Engine implements an Ethereum Virtual Machine (EVM) on the NEAR Protocol.
See [NEAR docs](https://docs.near.org/docs/develop/evm/introduction)for
additional documentation.

## Deployments

Expand Down
70 changes: 70 additions & 0 deletions benches/eth_deploy_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use criterion::{criterion_group, BatchSize, BenchmarkId, Criterion, Throughput};
use secp256k1::SecretKey;

use super::{address_from_secret_key, create_eth_transaction, deploy_evm, SUBMIT};

const INITIAL_BALANCE: u64 = 1000;
const INITIAL_NONCE: u64 = 0;
const TRANSFER_AMOUNT: u64 = 0;

fn eth_deploy_code_benchmark(c: &mut Criterion) {
let mut runner = deploy_evm();
let mut rng = rand::thread_rng();
let source_account = SecretKey::random(&mut rng);
runner.create_address(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
INITIAL_NONCE.into(),
);
let inputs: Vec<_> = [1, 4, 8, 12, 16]
.iter()
.copied()
.map(|n| {
let code_size = 2usize.pow(n);
let code: Vec<u8> = vec![0; code_size];
let transaction = create_eth_transaction(
None,
TRANSFER_AMOUNT.into(),
code,
Some(runner.chain_id),
&source_account,
);
rlp::encode(&transaction).to_vec()
})
.collect();
let calling_account_id = "some-account.near".to_string();

// measure gas usage
for input in inputs.iter() {
let input_size = input.len();
let (output, maybe_err) =
runner
.one_shot()
.call(SUBMIT, calling_account_id.clone(), input.clone());
assert!(maybe_err.is_none());
let output = output.unwrap();
let gas = output.burnt_gas;
let eth_gas = super::parse_eth_gas(&output);
// TODO(#45): capture this in a file
println!("ETH_DEPLOY_CODE_{:?} NEAR GAS: {:?}", input_size, gas);
println!("ETH_DEPLOY_CODE_{:?} ETH GAS: {:?}", input_size, eth_gas);
}

// measure wall-clock time
let mut group = c.benchmark_group("deploy_code");
for input in inputs {
let input_size = input.len() as u64;
let id = BenchmarkId::from_parameter(input_size);
group.throughput(Throughput::Bytes(input_size));
group.bench_function(id, |b| {
b.iter_batched(
|| (runner.one_shot(), calling_account_id.clone(), input.clone()),
|(r, c, i)| r.call(SUBMIT, c, i),
BatchSize::SmallInput,
)
});
}
group.finish();
}

criterion_group!(benches, eth_deploy_code_benchmark);
257 changes: 257 additions & 0 deletions benches/eth_erc20.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
use aurora_engine::parameters::SubmitResult;
use aurora_engine::prelude::{Address, U256};
use aurora_engine::transaction::EthTransaction;
use borsh::BorshDeserialize;
use criterion::{criterion_group, BatchSize, BenchmarkId, Criterion};
use near_vm_logic::VMOutcome;
use secp256k1::SecretKey;
use std::path::{Path, PathBuf};

use super::{address_from_secret_key, deploy_evm, sign_transaction, AuroraRunner, SUBMIT};
use crate::solidity;

const INITIAL_BALANCE: u64 = 1000;
const INITIAL_NONCE: u64 = 0;
const TRANSFER_AMOUNT: u64 = 67;

fn eth_erc20_benchmark(c: &mut Criterion) {
let mut runner = deploy_evm();
let mut rng = rand::thread_rng();
let source_account = SecretKey::random(&mut rng);
runner.create_address(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
INITIAL_NONCE.into(),
);
let calling_account_id = "some-account.near".to_string();

// deploy the erc20 contract
let constructor = ERC20Constructor::load();
let output = exec_transaction(
&mut runner,
constructor.deploy("Benchmarker", "BENCH", INITIAL_NONCE.into()),
&source_account,
);
let submit_result =
SubmitResult::try_from_slice(&output.return_data.as_value().unwrap()).unwrap();
let erc20_address = Address::from_slice(&submit_result.result);
let contract = ERC20 {
abi: constructor.abi,
address: erc20_address,
};

// create the transaction for minting
let tx = contract.mint(
address_from_secret_key(&source_account),
INITIAL_BALANCE.into(),
U256::from(INITIAL_NONCE + 1),
);
let signed_tx = sign_transaction(tx, Some(runner.chain_id), &source_account);
let mint_tx_bytes = rlp::encode(&signed_tx).to_vec();

// create the transaction for transfer
let dest_address = address_from_secret_key(&SecretKey::random(&mut rng));
let tx = contract.transfer(
dest_address,
TRANSFER_AMOUNT.into(),
U256::from(INITIAL_NONCE + 2),
);
let signed_tx = sign_transaction(tx, Some(runner.chain_id), &source_account);
let transfer_tx_bytes = rlp::encode(&signed_tx).to_vec();

let mut group = c.benchmark_group("erc20");
let mint_id = BenchmarkId::from_parameter("mint");
let transfer_id = BenchmarkId::from_parameter("transfer");

// measure mint wall-clock time
group.bench_function(mint_id, |b| {
b.iter_batched(
|| {
(
runner.one_shot(),
calling_account_id.clone(),
mint_tx_bytes.clone(),
)
},
|(r, c, i)| r.call(SUBMIT, c, i),
BatchSize::SmallInput,
)
});

// Measure mint gas usage; don't use `one_shot` because we want to keep this state change for
// the next benchmark where we transfer some of the minted tokens.
let (output, maybe_error) =
runner.call(SUBMIT, calling_account_id.clone(), mint_tx_bytes.clone());
assert!(maybe_error.is_none());
let output = output.unwrap();
let gas = output.burnt_gas;
let eth_gas = super::parse_eth_gas(&output);
// TODO(#45): capture this in a file
println!("ETH_ERC20_MINT NEAR GAS: {:?}", gas);
println!("ETH_ERC20_MINT ETH GAS: {:?}", eth_gas);

// Measure transfer gas usage
let (output, maybe_err) = runner.one_shot().call(
SUBMIT,
calling_account_id.clone(),
transfer_tx_bytes.clone(),
);
assert!(maybe_err.is_none());
let output = output.unwrap();
let gas = output.burnt_gas;
let eth_gas = super::parse_eth_gas(&output);
// TODO(#45): capture this in a file
println!("ETH_ERC20_TRANSFER NEAR GAS: {:?}", gas);
println!("ETH_ERC20_TRANSFER ETH GAS: {:?}", eth_gas);

// measure transfer wall-clock time
group.bench_function(transfer_id, |b| {
b.iter_batched(
|| {
(
runner.one_shot(),
calling_account_id.clone(),
transfer_tx_bytes.clone(),
)
},
|(r, c, i)| r.call(SUBMIT, c, i),
BatchSize::SmallInput,
)
});

group.finish();
}

struct ERC20Constructor {
abi: ethabi::Contract,
code: Vec<u8>,
}

struct ERC20 {
abi: ethabi::Contract,
address: Address,
}

impl ERC20Constructor {
fn load() -> Self {
let artifacts_base_path = Self::solidity_artifacts_path();
let hex_path = artifacts_base_path.join("ERC20PresetMinterPauser.bin");
let hex_rep = match std::fs::read_to_string(&hex_path) {
Ok(hex) => hex,
Err(_) => {
// An error occurred opening the file, maybe the contract hasn't been compiled?
let sources_root = Self::download_solidity_sources();
solidity::compile(
sources_root,
"token/ERC20/presets/ERC20PresetMinterPauser.sol",
&artifacts_base_path,
);
// If another error occurs, then we can't handle it so we just unwrap.
std::fs::read_to_string(hex_path).unwrap()
}
};
let code = hex::decode(&hex_rep).unwrap();
let abi_path = artifacts_base_path.join("ERC20PresetMinterPauser.abi");
let reader = std::fs::File::open(abi_path).unwrap();
let abi = ethabi::Contract::load(reader).unwrap();

Self { abi, code }
}

fn deploy(&self, name: &str, symbol: &str, nonce: U256) -> EthTransaction {
let data = self
.abi
.constructor()
.unwrap()
.encode_input(
self.code.clone(),
&[
ethabi::Token::String(name.to_string()),
ethabi::Token::String(symbol.to_string()),
],
)
.unwrap();
EthTransaction {
nonce,
gas_price: Default::default(),
gas: Default::default(),
to: None,
value: Default::default(),
data,
}
}

fn download_solidity_sources() -> PathBuf {
let sources_dir = Path::new("target").join("openzeppelin-contracts");
let contracts_dir = sources_dir.join("contracts");
if contracts_dir.exists() {
contracts_dir
} else {
let url = "https://github.com/OpenZeppelin/openzeppelin-contracts";
let repo = git2::Repository::clone(url, sources_dir).unwrap();
// repo.path() gives the path of the .git directory, so we need to use the parent
repo.path().parent().unwrap().join("contracts")
}
}

fn solidity_artifacts_path() -> PathBuf {
Path::new("target").join("solidity_build")
}
}

impl ERC20 {
fn mint(&self, recipient: Address, amount: U256, nonce: U256) -> EthTransaction {
let data = self
.abi
.function("mint")
.unwrap()
.encode_input(&[
ethabi::Token::Address(recipient),
ethabi::Token::Uint(amount),
])
.unwrap();
EthTransaction {
nonce,
gas_price: Default::default(),
gas: Default::default(),
to: Some(self.address),
value: Default::default(),
data,
}
}

fn transfer(&self, recipient: Address, amount: U256, nonce: U256) -> EthTransaction {
let data = self
.abi
.function("transfer")
.unwrap()
.encode_input(&[
ethabi::Token::Address(recipient),
ethabi::Token::Uint(amount),
])
.unwrap();
EthTransaction {
nonce,
gas_price: Default::default(),
gas: Default::default(),
to: Some(self.address),
value: Default::default(),
data,
}
}
}

fn exec_transaction(
runner: &mut AuroraRunner,
tx: EthTransaction,
account: &SecretKey,
) -> VMOutcome {
let calling_account_id = "some-account.near".to_string();
let signed_tx = sign_transaction(tx, Some(runner.chain_id), &account);
let (output, maybe_err) =
runner.call(SUBMIT, calling_account_id, rlp::encode(&signed_tx).to_vec());
assert!(maybe_err.is_none());
output.unwrap()
}

criterion_group!(benches, eth_erc20_benchmark);
Loading