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

fix precompile array encode & other improvements #2196

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions runtime/common/src/precompile/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128_tuple(balance_a, balance_b),
output: Output::encode_uint_tuple(vec![balance_a, balance_b]),
logs: Default::default(),
})
}
Expand All @@ -123,7 +123,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_address(&address),
output: Output::encode_address(address),
logs: Default::default(),
})
}
Expand All @@ -149,7 +149,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(target),
output: Output::encode_uint(target),
logs: Default::default(),
})
}
Expand All @@ -175,7 +175,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(supply),
output: Output::encode_uint(supply),
logs: Default::default(),
})
}
Expand Down Expand Up @@ -207,7 +207,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(value),
output: Output::encode_uint(value),
logs: Default::default(),
})
}
Expand Down Expand Up @@ -239,7 +239,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(value),
output: Output::encode_uint(value),
logs: Default::default(),
})
}
Expand Down
12 changes: 6 additions & 6 deletions runtime/common/src/precompile/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u32(output),
output: Output::encode_uint(output),
logs: Default::default(),
})
}
Expand All @@ -100,7 +100,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(deposit),
output: Output::encode_uint(deposit),
logs: Default::default(),
})
}
Expand All @@ -118,7 +118,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_address(&maintainer),
output: Output::encode_address(maintainer),
logs: Default::default(),
})
}
Expand All @@ -127,7 +127,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(deposit),
output: Output::encode_uint(deposit),
logs: Default::default(),
})
}
Expand All @@ -136,7 +136,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(fee),
output: Output::encode_uint(fee),
logs: Default::default(),
})
}
Expand Down Expand Up @@ -227,7 +227,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_bool(developer_status),
output: Output::encode_bool(developer_status),
logs: Default::default(),
})
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/common/src/precompile/evm_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_fixed_bytes(output.into().as_ref()),
output: Output::encode_fixed_bytes(output.into().as_ref()),
logs: Default::default(),
})
}
Expand All @@ -101,7 +101,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_address(&address),
output: Output::encode_address(address),
logs: Default::default(),
})
}
Expand All @@ -125,7 +125,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_address(&address),
output: Output::encode_address(address),
logs: Default::default(),
})
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/common/src/precompile/homa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(rate.into_inner()),
output: Output::encode_uint(rate.into_inner()),
logs: Default::default(),
})
}
Expand All @@ -154,7 +154,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(rate.into_inner()),
output: Output::encode_uint(rate.into_inner()),
logs: Default::default(),
})
}
Expand All @@ -164,7 +164,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(rate.into_inner()),
output: Output::encode_uint(rate.into_inner()),
logs: Default::default(),
})
}
Expand All @@ -174,7 +174,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(rate.into_inner()),
output: Output::encode_uint(rate.into_inner()),
logs: Default::default(),
})
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/common/src/precompile/honzon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128_tuple(collateral, debit),
output: Output::encode_uint_tuple(vec![collateral, debit]),
logs: Default::default(),
})
}
Expand All @@ -177,7 +177,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(ratio.into_inner()),
output: Output::encode_uint(ratio.into_inner()),
logs: Default::default(),
})
}
Expand All @@ -195,7 +195,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(ratio.into_inner()),
output: Output::encode_uint(ratio.into_inner()),
logs: Default::default(),
})
}
Expand All @@ -211,7 +211,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(exchange_rate.into_inner()),
output: Output::encode_uint(exchange_rate.into_inner()),
logs: Default::default(),
})
}
Expand Down
101 changes: 61 additions & 40 deletions runtime/common/src/precompile/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use frame_support::ensure;
use sp_std::{marker::PhantomData, result::Result, vec, vec::Vec};
use sp_std::{marker::PhantomData, result::Result, vec::Vec};

use crate::WeightToGas;
use ethabi::Token;
Expand Down Expand Up @@ -201,68 +201,68 @@ where
}

fn bool_at(&self, index: usize) -> Result<bool, Self::Error> {
const ONE: U256 = U256([1u64, 0, 0, 0]);
let param = self.u256_at(index)?;
Ok(!param.is_zero())
if param == ONE {
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
Ok(true)
} else if param.is_zero() {
Ok(false)
} else {
Err(PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "failed to decode bool".into(),
cost: self.target_gas.unwrap_or_default(),
})
}
}
}

#[derive(Default, Clone, PartialEq, Debug)]
pub struct Output;

impl Output {
pub fn encode_bool(&self, b: bool) -> Vec<u8> {
let out = Token::Bool(b);
ethabi::encode(&[out])
pub fn encode_bool(b: bool) -> Vec<u8> {
ethabi::encode(&[Token::Bool(b)])
}

pub fn encode_u8(&self, b: u8) -> Vec<u8> {
let out = Token::Uint(U256::from(b));
ethabi::encode(&[out])
pub fn encode_uint<T>(b: T) -> Vec<u8>
where
U256: From<T>,
{
ethabi::encode(&[Token::Uint(U256::from(b))])
}

pub fn encode_u32(&self, b: u32) -> Vec<u8> {
let out = Token::Uint(U256::from(b));
ethabi::encode(&[out])
pub fn encode_uint_tuple<T>(b: Vec<T>) -> Vec<u8>
where
U256: From<T>,
{
ethabi::encode(&[Token::Tuple(b.into_iter().map(U256::from).map(Token::Uint).collect())])
}

pub fn encode_u128(&self, b: u128) -> Vec<u8> {
let out = Token::Uint(U256::from(b));
ethabi::encode(&[out])
pub fn encode_uint_array<T>(b: Vec<T>) -> Vec<u8>
where
U256: From<T>,
{
ethabi::encode(&[Token::Array(b.into_iter().map(U256::from).map(Token::Uint).collect())])
}

pub fn encode_u128_tuple(&self, b: u128, c: u128) -> Vec<u8> {
let out = Token::Tuple(vec![Token::Uint(U256::from(b)), Token::Uint(U256::from(c))]);
ethabi::encode(&[out])
pub fn encode_bytes(b: &[u8]) -> Vec<u8> {
ethabi::encode(&[Token::Bytes(b.to_vec())])
}

pub fn encode_u128_array(&self, b: Vec<u128>) -> Vec<u8> {
let result: Vec<Token> = b.iter().map(|x| Token::Uint(U256::from(*x))).collect();
let out = Token::FixedArray(result);
ethabi::encode(&[out])
pub fn encode_fixed_bytes(b: &[u8]) -> Vec<u8> {
ethabi::encode(&[Token::FixedBytes(b.to_vec())])
}

pub fn encode_address_array(&self, b: Vec<H160>) -> Vec<u8> {
let result: Vec<Token> = b
.iter()
.map(|x| Token::Address(H160::from_slice(x.as_bytes())))
.collect();
let out = Token::FixedArray(result);
ethabi::encode(&[out])
pub fn encode_address(b: H160) -> Vec<u8> {
ethabi::encode(&[Token::Address(b)])
}

pub fn encode_bytes(&self, b: &[u8]) -> Vec<u8> {
let out = Token::Bytes(b.to_vec());
ethabi::encode(&[out])
pub fn encode_address_tuple(b: Vec<H160>) -> Vec<u8> {
ethabi::encode(&[Token::Tuple(b.into_iter().map(Token::Address).collect())])
}

pub fn encode_fixed_bytes(&self, b: &[u8]) -> Vec<u8> {
let out = Token::FixedBytes(b.to_vec());
ethabi::encode(&[out])
}

pub fn encode_address(&self, b: &H160) -> Vec<u8> {
let out = Token::Address(H160::from_slice(b.as_bytes()));
ethabi::encode(&[out])
pub fn encode_address_array(b: Vec<H160>) -> Vec<u8> {
ethabi::encode(&[Token::Array(b.into_iter().map(Token::Address).collect())])
}
}

Expand Down Expand Up @@ -568,6 +568,27 @@ mod tests {
assert_ok!(input.i128_at(6), 0);
}

#[test]
fn bool_works() {
let data = hex_literal::hex! {"
00000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000002
"};
let input = TestInput::new(&data[..], Some(10));
assert_ok!(input.bool_at(1), false);
assert_ok!(input.bool_at(2), true);
assert_eq!(
input.bool_at(3),
Err(PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "failed to decode bool".into(),
cost: 10,
})
);
}

#[test]
fn decode_int128() {
let items = [
Expand Down
10 changes: 5 additions & 5 deletions runtime/common/src/precompile/multicurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_bytes(&name),
output: Output::encode_bytes(&name),
logs: Default::default(),
})
}
Expand All @@ -124,7 +124,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_bytes(&symbol),
output: Output::encode_bytes(&symbol),
logs: Default::default(),
})
}
Expand All @@ -140,7 +140,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u8(decimals),
output: Output::encode_uint(decimals),
logs: Default::default(),
})
}
Expand All @@ -152,7 +152,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(total_issuance),
output: Output::encode_uint(total_issuance),
logs: Default::default(),
})
}
Expand All @@ -169,7 +169,7 @@ where
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: gas_cost,
output: Output::default().encode_u128(balance),
output: Output::encode_uint(balance),
logs: Default::default(),
})
}
Expand Down
Loading