Skip to content

Commit

Permalink
Test(engine): Increase unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHodulak committed Oct 3, 2022
1 parent bd0ae7e commit d7ba416
Show file tree
Hide file tree
Showing 11 changed files with 928 additions and 231 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions engine-precompiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ethabi = { version = "17.1", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }

[dev-dependencies]
aurora-engine-test-doubles = { path = "../engine-test-doubles" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rand = "0.7.3"
Expand Down
124 changes: 123 additions & 1 deletion engine-precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,101 @@ const fn make_h256(x: u128, y: u128) -> prelude::H256 {
#[cfg(test)]
mod tests {
use crate::prelude::H160;
use crate::{prelude, Byzantium, Istanbul};
use crate::{
prelude, AllPrecompiles, Byzantium, Context, EvmPrecompileResult, ExitError, Istanbul,
Precompile, PrecompileOutput, Precompiles,
};
use aurora_engine_sdk::env::Fixed;
use aurora_engine_sdk::promise::Noop;
use aurora_engine_test_doubles::io::StoragePointer;
use aurora_engine_types::types::EthGas;
use evm::executor::stack::{PrecompileFailure, PrecompileHandle, PrecompileSet};
use evm::{ExitFatal, ExitReason, Transfer};
use prelude::types::Address;
use rand::Rng;

struct MockPrecompile;

impl Precompile for MockPrecompile {
fn required_gas(input: &[u8]) -> Result<EthGas, ExitError>
where
Self: Sized,
{
Ok(EthGas::new(0))
}

fn run(
&self,
input: &[u8],
target_gas: Option<EthGas>,
context: &Context,
is_static: bool,
) -> EvmPrecompileResult {
Ok(PrecompileOutput::default())
}
}

struct MockPrecompileHandle {
code_address: H160,
}

impl MockPrecompileHandle {
pub fn new(code_address: H160) -> Self {
Self { code_address }
}
}

impl PrecompileHandle for MockPrecompileHandle {
fn call(
&mut self,
to: H160,
transfer: Option<Transfer>,
input: Vec<u8>,
gas_limit: Option<u64>,
is_static: bool,
context: &Context,
) -> (ExitReason, Vec<u8>) {
unimplemented!()
}

fn record_cost(&mut self, cost: u64) -> Result<(), ExitError> {
unimplemented!()
}

fn remaining_gas(&self) -> u64 {
unimplemented!()
}

fn log(
&mut self,
address: H160,
topics: Vec<aurora_engine_types::H256>,
data: Vec<u8>,
) -> Result<(), ExitError> {
unimplemented!()
}

fn code_address(&self) -> H160 {
self.code_address
}

fn input(&self) -> &[u8] {
unimplemented!()
}

fn context(&self) -> &Context {
unimplemented!()
}

fn is_static(&self) -> bool {
unimplemented!()
}

fn gas_limit(&self) -> Option<u64> {
unimplemented!()
}
}

#[test]
fn test_precompile_addresses() {
assert_eq!(super::secp256k1::ECRecover::ADDRESS, u8_to_address(1));
Expand Down Expand Up @@ -486,6 +577,37 @@ mod tests {
}
}

#[test]
fn test_paused_precompiles_throws_error() {
let precompile_address = Address::default();
let precompile: AllPrecompiles<StoragePointer, Fixed, Noop> =
AllPrecompiles::Generic(Box::new(MockPrecompile));

let precompiles: Precompiles<StoragePointer, Fixed, Noop> = Precompiles {
all_precompiles: {
let mut map = prelude::BTreeMap::new();
map.insert(precompile_address, precompile);
map
},
paused_precompiles: {
let mut set = prelude::BTreeSet::new();
set.insert(precompile_address);
set
},
};
let mut precompile_handle = MockPrecompileHandle::new(precompile_address.raw());

let result = precompiles
.execute(&mut precompile_handle)
.expect("result must contain error but is empty");
let actual_failure = result.expect_err("result must contain failure but is successful");
let expected_failure = PrecompileFailure::Fatal {
exit_status: ExitFatal::Other(prelude::Cow::Borrowed("ERR_PAUSED")),
};

assert_eq!(expected_failure, actual_failure);
}

fn u8_to_address(x: u8) -> Address {
let mut bytes = [0u8; 20];
bytes[19] = x;
Expand Down
1 change: 0 additions & 1 deletion engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ fn non_submit_execute<'db>(
} else {
engine.receive_erc20_tokens(
&env.predecessor_account_id,
&env.signer_account_id,
args,
&env.current_account_id,
&mut handler,
Expand Down
6 changes: 3 additions & 3 deletions engine-test-doubles/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl StorageIntermediate for Value {

#[derive(Debug, Default)]
pub struct Storage {
input: Vec<u8>,
output: Vec<u8>,
kv_store: HashMap<Vec<u8>, Vec<u8>>,
pub input: Vec<u8>,
pub output: Vec<u8>,
pub kv_store: HashMap<Vec<u8>, Vec<u8>>,
}

/// In-memory implementation of [IO].
Expand Down
1 change: 1 addition & 0 deletions engine-test-doubles/src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use aurora_engine_types::parameters::{PromiseBatchAction, PromiseCreateArgs};
use aurora_engine_types::types::PromiseResult;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum PromiseArgs {
Create(PromiseCreateArgs),
#[allow(dead_code)]
Expand Down
2 changes: 2 additions & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ aurora-engine-test-doubles = { path = "../engine-test-doubles" }
serde_json = "1"
rand = "0.7.3"
test-case = "2.1"
sha3 = "0.10"
digest = "0.10"

[features]
default = ["std"]
Expand Down
Loading

0 comments on commit d7ba416

Please sign in to comment.