Skip to content

Commit

Permalink
chore: a set of minor improvements for engine (#916)
Browse files Browse the repository at this point in the history
## Description

A set of minor changes and clean-ups for overall codebase.

## Performance / NEAR gas cost considerations

No changes should affect gas cost. 

## Testing

Automated tests

## How should this be reviewed

Patches are minor and do not introduce any behaviour changes

---------

Co-authored-by: Oleksandr Anyshchenko <[email protected]>
  • Loading branch information
raventid and aleksuss authored Mar 21, 2024
1 parent 54468a9 commit fbe7f33
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 20 deletions.
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("diff value is invalid"))
})?;
value.take_value().map(EngineStorageValue::Vec)
}
Expand Down
12 changes: 6 additions & 6 deletions engine-standalone-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ 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 is invalid"))
.ok_or(Error::TransactionNotFound(tx_included))
}

Expand Down Expand Up @@ -249,12 +249,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 is invalid");
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 is invalid");
action(&mut batch, &storage_key, &value_bytes);
}

Expand All @@ -275,7 +275,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 is invalid");
let block_height = {
let mut buf = [0u8; 8];
buf.copy_from_slice(&k[n..(n + 8)]);
Expand Down Expand Up @@ -314,7 +314,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 is invalid").to_vec();
if db_key.get(0..engine_prefix_len) != Some(&engine_prefix) {
break;
}
Expand All @@ -335,7 +335,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 is invalid");
DiffValue::try_from_bytes(bytes).unwrap_or_else(|e| {
panic!(
"Could not deserialize key={} value={} error={:?}",
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 is invalid")),
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 is invalid")
}

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 is invalid")
}

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

0 comments on commit fbe7f33

Please sign in to comment.