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

chore: a set of minor improvements for engine #916

Merged
merged 8 commits into from
Mar 21, 2024
Merged
1 change: 1 addition & 0 deletions engine-precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
clippy::missing_errors_doc,
clippy::unreadable_literal
)]
#![forbid(unsafe_code)]
pub mod account_ids;
pub mod alt_bn256;
pub mod blake2;
Expand Down
3 changes: 1 addition & 2 deletions engine-precompiles/src/xcc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Cross contract call precompile.
//!
//! Allow Aurora users interacting with NEAR smart contracts using cross contract call primitives.
//! TODO: How they work (low level explanation with examples)

use crate::{utils, HandleBasedPrecompile, PrecompileOutput};
use aurora_engine_sdk::io::IO;
Expand Down Expand Up @@ -80,7 +79,7 @@ pub mod cross_contract_call {
H256,
};

/// Exit to Ethereum precompile address
/// NEAR Cross Contract Call precompile address
///
/// Address: `0x516cded1d16af10cad47d6d49128e2eb7d27b372`
/// This address is computed as: `&keccak("nearCrossContractCall")[12..]`
Expand Down
2 changes: 1 addition & 1 deletion engine-standalone-storage/src/engine_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<'db, 'input: 'db, 'output: 'db> IO for EngineStateAccess<'db, 'input, 'outp
let value = iter.next().and_then(|maybe_elem| {
maybe_elem
.ok()
.map(|(_, value)| DiffValue::try_from_bytes(&value).unwrap())
.map(|(_, value)| DiffValue::try_from_bytes(&value).expect("value should be valid"))
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
})?;
value.take_value().map(EngineStorageValue::Vec)
}
Expand Down
14 changes: 8 additions & 6 deletions engine-standalone-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ impl Storage {
let storage_key = construct_storage_key(StoragePrefix::Diff, &tx_included.to_bytes());
self.db
.get_pinned(storage_key)?
.map(|slice| Diff::try_from_bytes(slice.as_ref()).unwrap())
.map(|slice| {
Diff::try_from_bytes(slice.as_ref()).expect("transaction_diff should be valid")
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
})
.ok_or(Error::TransactionNotFound(tx_included))
}

Expand Down Expand Up @@ -249,12 +251,12 @@ impl Storage {
action(&mut batch, &storage_key, &msg_bytes);

let storage_key = construct_storage_key(StoragePrefix::Diff, &tx_included_bytes);
let diff_bytes = diff.try_to_bytes().unwrap();
let diff_bytes = diff.try_to_bytes().expect("diff should be valid");
action(&mut batch, &storage_key, &diff_bytes);

for (key, value) in diff {
let storage_key = construct_engine_key(key, block_height, tx_included.position);
let value_bytes = value.try_to_bytes().unwrap();
let value_bytes = value.try_to_bytes().expect("value should be valid");
action(&mut batch, &storage_key, &value_bytes);
}

Expand All @@ -275,7 +277,7 @@ impl Storage {
if k.len() < n || k[0..n] != db_key_prefix {
break;
}
let value = DiffValue::try_from_bytes(v.as_ref()).unwrap();
let value = DiffValue::try_from_bytes(v.as_ref()).expect("diff should be valid");
let block_height = {
let mut buf = [0u8; 8];
buf.copy_from_slice(&k[n..(n + 8)]);
Expand Down Expand Up @@ -314,7 +316,7 @@ impl Storage {

while iter.valid() {
// unwrap is safe because the iterator is valid
let db_key = iter.key().unwrap().to_vec();
let db_key = iter.key().expect("iterator should be valid").to_vec();
if db_key.get(0..engine_prefix_len) != Some(&engine_prefix) {
break;
}
Expand All @@ -335,7 +337,7 @@ impl Storage {
iter.seek_for_prev(&desired_db_key);

let value = if iter.valid() {
let bytes = iter.value().unwrap();
let bytes = iter.value().expect("iterator should be valid");
DiffValue::try_from_bytes(bytes).unwrap_or_else(|e| {
panic!(
"Could not deserialize key={} value={} error={:?}",
Expand Down
8 changes: 5 additions & 3 deletions engine-standalone-storage/src/relayer_db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use aurora_engine::parameters::SubmitArgs;
use aurora_engine::{engine, state};
use aurora_engine_sdk::env::{self, Env, DEFAULT_PREPAID_GAS};
Expand Down Expand Up @@ -69,9 +71,9 @@ pub fn initialize_transactions<I>(
where
I: FallibleIterator<Item = types::TransactionRow, Error = postgres::Error>,
{
let signer_account_id = "relayer.aurora".parse().unwrap();
let predecessor_account_id: AccountId = "relayer.aurora".parse().unwrap();
let current_account_id = "aurora".parse().unwrap();
let signer_account_id = AccountId::from_str("relayer.aurora").expect("valid account_id");
let predecessor_account_id = AccountId::from_str("relayer.aurora").expect("valid account_id");
let current_account_id = AccountId::from_str("aurora").expect("valid account_id");
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
let relayer_address =
aurora_engine_sdk::types::near_account_to_evm_address(predecessor_account_id.as_bytes());
let mut env = env::Fixed {
Expand Down
6 changes: 3 additions & 3 deletions engine-standalone-storage/src/relayer_db/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl TryFrom<postgres::Row> for TransactionRow {
near_hash,
near_receipt_hash,
from,
to: to.map(|arr| Address::try_from_slice(arr).unwrap()),
to: to.map(|arr| Address::try_from_slice(arr).expect("address should be valid")),
nonce,
gas_price,
gas_limit,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl From<TransactionRow> for EthTransactionKind {

fn get_numeric(row: &postgres::Row, field: &str) -> U256 {
let value: PostgresNumeric = row.get(field);
U256::try_from(value).unwrap()
U256::try_from(value).expect("postgres numeric should be valid")
}

fn get_hash(row: &postgres::Row, field: &str) -> H256 {
Expand All @@ -231,7 +231,7 @@ fn get_hash(row: &postgres::Row, field: &str) -> H256 {

fn get_address(row: &postgres::Row, field: &str) -> Address {
let value: &[u8] = row.get(field);
Address::try_from_slice(value).unwrap()
Address::try_from_slice(value).expect("address should be valid")
}

fn get_timestamp(row: &postgres::Row, field: &str) -> Option<u64> {
Expand Down
2 changes: 1 addition & 1 deletion engine-standalone-storage/src/sync/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl TransactionMessage {
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
let borshable: BorshableTransactionMessage = self.into();
borsh::to_vec(&borshable).unwrap()
borsh::to_vec(&borshable).expect("self to be valid")
}

pub fn try_from_slice(bytes: &[u8]) -> Result<Self, std::io::Error> {
Expand Down
1 change: 1 addition & 0 deletions engine-transactions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
clippy::missing_panics_doc,
clippy::missing_errors_doc
)]
#![forbid(unsafe_code)]

use aurora_engine_types::types::{Address, Wei};
use aurora_engine_types::{vec, Vec, H160, U256};
Expand Down
7 changes: 4 additions & 3 deletions engine/src/contract_methods/connector/deposit_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ impl FtTransferMessageData {
}
})?;

// Parse the fee from the message slice. It should contain 32 bytes,
// but after that, it will be parsed to u128.
// This logic is for compatibility.
// Parse the fee from the message slice.
// The fee is expected to be represented as a 32-byte value in the message.
// However, it will be parsed and converted to a u128 for further processing.
// This parsing logic is implemented to ensure compatibility
let fee_u128: u128 = U256::from_little_endian(&data[..32])
.try_into()
.map_err(|_| errors::ParseOnTransferMessageError::OverflowNumber)?;
Expand Down
11 changes: 7 additions & 4 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,14 @@ mod contract {
.sdk_unwrap();
}

/// Allow receiving NEP141 tokens to the EVM contract.
/// Allows receiving NEP141 tokens in the EVM contract.
///
/// This function returns the amount of tokens to return to the sender.
/// Either all tokens are transferred and tokens are returned
/// in case of an error, or no token is returned if the transaction was successful.
/// This function is called when NEP141 tokens are transferred to the contract.
/// It returns the amount of tokens that should be returned to the sender.
///
/// There are two possible outcomes:
/// 1. If an error occurs during the token transfer, all the transferred tokens are returned to the sender.
/// 2. If the token transfer is successful, no tokens are returned, and the contract keeps the transferred tokens.
#[no_mangle]
pub extern "C" fn ft_on_transfer() {
let io = Runtime;
Expand Down
Loading