Skip to content

Commit

Permalink
Remove sdk::current_account_id usage from engine-precompiles (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd committed Nov 11, 2021
1 parent 696f028 commit 25bbaad
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 111 deletions.
20 changes: 12 additions & 8 deletions engine-precompiles/src/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Precompile for Blake2F {
/// See: https://eips.ethereum.org/EIPS/eip-152
/// See: https://etherscan.io/address/0000000000000000000000000000000000000009
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand Down Expand Up @@ -120,12 +121,12 @@ mod tests {

fn test_blake2f_out_of_gas() -> EvmPrecompileResult {
let input = hex::decode(INPUT).unwrap();
Blake2F::run(&input, Some(11), &new_context(), false)
Blake2F.run(&input, Some(11), &new_context(), false)
}

fn test_blake2f_empty() -> EvmPrecompileResult {
let input = [0u8; 0];
Blake2F::run(&input, Some(0), &new_context(), false)
Blake2F.run(&input, Some(0), &new_context(), false)
}

fn test_blake2f_invalid_len_1() -> EvmPrecompileResult {
Expand All @@ -143,7 +144,7 @@ mod tests {
01",
)
.unwrap();
Blake2F::run(&input, Some(12), &new_context(), false)
Blake2F.run(&input, Some(12), &new_context(), false)
}

fn test_blake2f_invalid_len_2() -> EvmPrecompileResult {
Expand All @@ -161,7 +162,7 @@ mod tests {
01",
)
.unwrap();
Blake2F::run(&input, Some(12), &new_context(), false)
Blake2F.run(&input, Some(12), &new_context(), false)
}

fn test_blake2f_invalid_flag() -> EvmPrecompileResult {
Expand All @@ -179,7 +180,7 @@ mod tests {
02",
)
.unwrap();
Blake2F::run(&input, Some(12), &new_context(), false)
Blake2F.run(&input, Some(12), &new_context(), false)
}

fn test_blake2f_r_0() -> Vec<u8> {
Expand All @@ -197,14 +198,16 @@ mod tests {
01",
)
.unwrap();
Blake2F::run(&input, Some(12), &new_context(), false)
Blake2F
.run(&input, Some(12), &new_context(), false)
.unwrap()
.output
}

fn test_blake2f_r_12() -> Vec<u8> {
let input = hex::decode(INPUT).unwrap();
Blake2F::run(&input, Some(12), &new_context(), false)
Blake2F
.run(&input, Some(12), &new_context(), false)
.unwrap()
.output
}
Expand All @@ -224,7 +227,8 @@ mod tests {
00",
)
.unwrap();
Blake2F::run(&input, Some(12), &new_context(), false)
Blake2F
.run(&input, Some(12), &new_context(), false)
.unwrap()
.output
}
Expand Down
56 changes: 41 additions & 15 deletions engine-precompiles/src/bn128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub(super) struct Bn128Add<HF: HardFork>(PhantomData<HF>);

impl<HF: HardFork> Bn128Add<HF> {
pub(super) const ADDRESS: Address = super::make_address(0, 6);

pub fn new() -> Self {
Self(Default::default())
}
}

impl<HF: HardFork> Bn128Add<HF> {
Expand Down Expand Up @@ -103,6 +107,7 @@ impl Precompile for Bn128Add<Byzantium> {
/// See: https://eips.ethereum.org/EIPS/eip-196
/// See: https://etherscan.io/address/0000000000000000000000000000000000000006
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand Down Expand Up @@ -131,6 +136,7 @@ impl Precompile for Bn128Add<Istanbul> {
/// See: https://eips.ethereum.org/EIPS/eip-196
/// See: https://etherscan.io/address/0000000000000000000000000000000000000006
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand All @@ -151,6 +157,10 @@ pub(super) struct Bn128Mul<HF: HardFork>(PhantomData<HF>);

impl<HF: HardFork> Bn128Mul<HF> {
pub(super) const ADDRESS: Address = super::make_address(0, 7);

pub fn new() -> Self {
Self(Default::default())
}
}

impl<HF: HardFork> Bn128Mul<HF> {
Expand Down Expand Up @@ -188,6 +198,7 @@ impl Precompile for Bn128Mul<Byzantium> {
/// See: https://eips.ethereum.org/EIPS/eip-196
/// See: https://etherscan.io/address/0000000000000000000000000000000000000007
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand Down Expand Up @@ -215,6 +226,7 @@ impl Precompile for Bn128Mul<Istanbul> {
/// See: https://eips.ethereum.org/EIPS/eip-196
/// See: https://etherscan.io/address/0000000000000000000000000000000000000007
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand All @@ -236,6 +248,10 @@ pub(super) struct Bn128Pair<HF: HardFork>(PhantomData<HF>);

impl<HF: HardFork> Bn128Pair<HF> {
pub(super) const ADDRESS: Address = super::make_address(0, 8);

pub fn new() -> Self {
Self(Default::default())
}
}

impl<HF: HardFork> Bn128Pair<HF> {
Expand Down Expand Up @@ -345,6 +361,7 @@ impl Precompile for Bn128Pair<Byzantium> {
/// See: https://eips.ethereum.org/EIPS/eip-197
/// See: https://etherscan.io/address/0000000000000000000000000000000000000008
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand Down Expand Up @@ -375,6 +392,7 @@ impl Precompile for Bn128Pair<Istanbul> {
/// See: https://eips.ethereum.org/EIPS/eip-197
/// See: https://etherscan.io/address/0000000000000000000000000000000000000008
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
context: &Context,
Expand Down Expand Up @@ -415,7 +433,8 @@ mod tests {
)
.unwrap();

let res = Bn128Add::<Byzantium>::run(&input, Some(500), &new_context(), false)
let res = Bn128Add::<Byzantium>::new()
.run(&input, Some(500), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -436,7 +455,8 @@ mod tests {
)
.unwrap();

let res = Bn128Add::<Byzantium>::run(&input, Some(500), &new_context(), false)
let res = Bn128Add::<Byzantium>::new()
.run(&input, Some(500), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -450,7 +470,7 @@ mod tests {
0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap();
let res = Bn128Add::<Byzantium>::run(&input, Some(499), &new_context(), false);
let res = Bn128Add::<Byzantium>::new().run(&input, Some(499), &new_context(), false);
assert!(matches!(res, Err(ExitError::OutOfGas)));

// no input test
Expand All @@ -462,7 +482,8 @@ mod tests {
)
.unwrap();

let res = Bn128Add::<Byzantium>::run(&input, Some(500), &new_context(), false)
let res = Bn128Add::<Byzantium>::new()
.run(&input, Some(500), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -477,7 +498,7 @@ mod tests {
)
.unwrap();

let res = Bn128Add::<Byzantium>::run(&input, Some(500), &new_context(), false);
let res = Bn128Add::<Byzantium>::new().run(&input, Some(500), &new_context(), false);
assert!(matches!(
res,
Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_POINT")))
Expand All @@ -500,7 +521,8 @@ mod tests {
)
.unwrap();

let res = Bn128Mul::<Byzantium>::run(&input, Some(40_000), &new_context(), false)
let res = Bn128Mul::<Byzantium>::new()
.run(&input, Some(40_000), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -513,7 +535,7 @@ mod tests {
0200000000000000000000000000000000000000000000000000000000000000",
)
.unwrap();
let res = Bn128Mul::<Byzantium>::run(&input, Some(39_999), &new_context(), false);
let res = Bn128Mul::<Byzantium>::new().run(&input, Some(39_999), &new_context(), false);
assert!(matches!(res, Err(ExitError::OutOfGas)));

// zero multiplication test
Expand All @@ -531,7 +553,8 @@ mod tests {
)
.unwrap();

let res = Bn128Mul::<Byzantium>::run(&input, Some(40_000), &new_context(), false)
let res = Bn128Mul::<Byzantium>::new()
.run(&input, Some(40_000), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -545,7 +568,8 @@ mod tests {
)
.unwrap();

let res = Bn128Mul::<Byzantium>::run(&input, Some(40_000), &new_context(), false)
let res = Bn128Mul::<Byzantium>::new()
.run(&input, Some(40_000), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -559,7 +583,7 @@ mod tests {
)
.unwrap();

let res = Bn128Mul::<Byzantium>::run(&input, Some(40_000), &new_context(), false);
let res = Bn128Mul::<Byzantium>::new().run(&input, Some(40_000), &new_context(), false);
assert!(matches!(
res,
Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_POINT")))
Expand Down Expand Up @@ -588,7 +612,8 @@ mod tests {
hex::decode("0000000000000000000000000000000000000000000000000000000000000001")
.unwrap();

let res = Bn128Pair::<Byzantium>::run(&input, Some(260_000), &new_context(), false)
let res = Bn128Pair::<Byzantium>::new()
.run(&input, Some(260_000), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -610,7 +635,7 @@ mod tests {
12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
)
.unwrap();
let res = Bn128Pair::<Byzantium>::run(&input, Some(259_999), &new_context(), false);
let res = Bn128Pair::<Byzantium>::new().run(&input, Some(259_999), &new_context(), false);
assert!(matches!(res, Err(ExitError::OutOfGas)));

// no input test
Expand All @@ -619,7 +644,8 @@ mod tests {
hex::decode("0000000000000000000000000000000000000000000000000000000000000001")
.unwrap();

let res = Bn128Pair::<Byzantium>::run(&input, Some(260_000), &new_context(), false)
let res = Bn128Pair::<Byzantium>::new()
.run(&input, Some(260_000), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -636,7 +662,7 @@ mod tests {
)
.unwrap();

let res = Bn128Pair::<Byzantium>::run(&input, Some(260_000), &new_context(), false);
let res = Bn128Pair::<Byzantium>::new().run(&input, Some(260_000), &new_context(), false);
assert!(matches!(
res,
Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_A")))
Expand All @@ -652,7 +678,7 @@ mod tests {
)
.unwrap();

let res = Bn128Pair::<Byzantium>::run(&input, Some(260_000), &new_context(), false);
let res = Bn128Pair::<Byzantium>::new().run(&input, Some(260_000), &new_context(), false);
assert!(matches!(
res,
Err(ExitError::Other(Borrowed("ERR_BN128_INVALID_LEN",)))
Expand Down
9 changes: 7 additions & 2 deletions engine-precompiles/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Precompile for SHA256 {
/// See: https://etherscan.io/address/0000000000000000000000000000000000000002
#[cfg(not(feature = "contract"))]
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand All @@ -64,6 +65,7 @@ impl Precompile for SHA256 {
/// See: https://etherscan.io/address/0000000000000000000000000000000000000002
#[cfg(feature = "contract")]
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand Down Expand Up @@ -110,6 +112,7 @@ impl Precompile for RIPEMD160 {
/// See: https://docs.soliditylang.org/en/develop/units-and-global-variables.html#mathematical-and-cryptographic-functions
/// See: https://etherscan.io/address/0000000000000000000000000000000000000003
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand Down Expand Up @@ -147,7 +150,8 @@ mod tests {
hex::decode("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
.unwrap();

let res = SHA256::run(input, Some(60), &new_context(), false)
let res = SHA256
.run(input, Some(60), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand All @@ -160,7 +164,8 @@ mod tests {
hex::decode("0000000000000000000000009c1185a5c5e9fc54612808977ee8f548b2258d31")
.unwrap();

let res = RIPEMD160::run(input, Some(600), &new_context(), false)
let res = RIPEMD160
.run(input, Some(600), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
Expand Down
12 changes: 8 additions & 4 deletions engine-precompiles/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Precompile for Identity {
/// See: https://ethereum.github.io/yellowpaper/paper.pdf
/// See: https://etherscan.io/address/0000000000000000000000000000000000000004
fn run(
&self,
input: &[u8],
target_gas: Option<u64>,
_context: &Context,
Expand Down Expand Up @@ -65,19 +66,21 @@ mod tests {
let input = [0u8, 1, 2, 3];

let expected = input[0..2].to_vec();
let res = Identity::run(&input[0..2], Some(18), &new_context(), false)
let res = Identity
.run(&input[0..2], Some(18), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);

let expected = input.to_vec();
let res = Identity::run(&input, Some(18), &new_context(), false)
let res = Identity
.run(&input, Some(18), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);

// gas fail
let res = Identity::run(&input[0..2], Some(17), &new_context(), false);
let res = Identity.run(&input[0..2], Some(17), &new_context(), false);

assert!(matches!(res, Err(ExitError::OutOfGas)));

Expand All @@ -86,7 +89,8 @@ mod tests {
0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32,
];
let res = Identity::run(&input, Some(21), &new_context(), false)
let res = Identity
.run(&input, Some(21), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, input.to_vec());
Expand Down
Loading

0 comments on commit 25bbaad

Please sign in to comment.