Skip to content

Commit

Permalink
chore: gas decreasing and other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Mar 24, 2024
1 parent 1e44a5e commit c846820
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 70 deletions.
2 changes: 1 addition & 1 deletion factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use near_sdk::{
};

const FORWARDER_WASM: &[u8] = include_bytes!("../../res/aurora-forwarder.wasm");
const INIT_BALANCE: NearToken = NearToken::from_millinear(450);
const INIT_BALANCE: NearToken = NearToken::from_millinear(445);
const STORAGE_BALANCE_BOUND: NearToken = NearToken::from_yoctonear(1_250_000_000_000_000_000_000);
const FORWARDER_NEW_GAS: Gas = Gas::from_tgas(5);
const MAX_NUM_CONTRACTS: usize = 8;
Expand Down
18 changes: 9 additions & 9 deletions forwarder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![cfg_attr(target_arch = "wasm32", no_std)]
#![allow(clippy::module_name_repetitions, clippy::as_conversions)]

use crate::error::ContractError;
use borsh::BorshDeserialize;
use core::str::FromStr;

use crate::error::ContractError;
use crate::params::{
ft_balance_args, ft_transfer_args, ft_transfer_call_args, FeesParams, FinishForwardParams,
ForwardParams, State,
Expand All @@ -24,17 +24,17 @@ mod types;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

const MINIMUM_BALANCE: u128 = 450_000_000_000_000_000_000_000;
const MINIMUM_BALANCE: u128 = 445_000_000_000_000_000_000_000;
const ZERO_YOCTO: u128 = 0;
const MAX_FEE_PERCENT: u128 = 10;

const CALCULATE_FEES_GAS: u64 = 5_000_000_000_000;
const NEAR_DEPOSIT_GAS: u64 = 5_000_000_000_000;
const FT_BALANCE_GAS: u64 = 5_000_000_000_000;
const FT_TRANSFER_GAS: u64 = 5_000_000_000_000;
const FT_TRANSFER_CALL_GAS: u64 = 50_000_000_000_000;
const CALCULATE_FEES_CALLBACK_GAS: u64 = 100_000_000_000_000;
const FINISH_FORWARD_GAS: u64 = 70_000_000_000_000;
const CALCULATE_FEES_GAS: u64 = 4_000_000_000_000;
const NEAR_DEPOSIT_GAS: u64 = 2_000_000_000_000;
const FT_BALANCE_GAS: u64 = 2_000_000_000_000;
const FT_TRANSFER_GAS: u64 = 3_000_000_000_000;
const FT_TRANSFER_CALL_GAS: u64 = 40_000_000_000_000;
const CALCULATE_FEES_CALLBACK_GAS: u64 = 80_000_000_000_000;
const FINISH_FORWARD_GAS: u64 = 60_000_000_000_000;

// Key is used for upgrading the smart contract.
const UPDATER_PK: &str = "ed25519:BaiF3VUJf5pxB9ezVtzH4SejpdYc7EA3SqrKczsj1wno";
Expand Down
12 changes: 6 additions & 6 deletions forwarder/src/runtime/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ impl PromiseHandler for Runtime {
PromiseId::new(id)
}

fn promise_return(&mut self, promise: PromiseId) {
unsafe {
exports::promise_return(promise.raw());
}
}

unsafe fn promise_create_batch<const S: usize>(
&mut self,
args: &PromiseBatchAction<S>,
Expand All @@ -154,4 +148,10 @@ impl PromiseHandler for Runtime {

PromiseId::new(id)
}

fn promise_return(&mut self, promise: PromiseId) {
unsafe {
exports::promise_return(promise.raw());
}
}
}
8 changes: 5 additions & 3 deletions forwarder/src/runtime/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ pub trait StorageIntermediate: Sized {

fn to_vec(&self) -> Vec<u8> {
let len = self.len();
let mut buff = [0; 256];
self.copy_to_slice(&mut buff[..len]);
let mut buf = Vec::new();
buf.try_extend_from_slice(&buff[..len]).unwrap();
assert!(len <= buf.capacity());
unsafe {
buf.set_len(len);
}
self.copy_to_slice(&mut buf[..len]);
buf
}

Expand Down
5 changes: 2 additions & 3 deletions forwarder/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::runtime::sys::exports;
use crate::types::{AccountId, PromiseAction, PromiseBatchAction, RawPublicKey};
use crate::types::{AccountId, PromiseAction, PromiseBatchAction};

pub use env::Env;
pub use handler::PromiseHandler;
Expand Down Expand Up @@ -62,8 +62,7 @@ impl Runtime {
for action in &args.actions {
match action {
PromiseAction::AddFullAccessKey { public_key, nonce } => {
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
let pk_bytes = public_key.as_bytes();
exports::promise_batch_action_add_key_with_full_access(
id,
pk_bytes.len() as _,
Expand Down
4 changes: 2 additions & 2 deletions forwarder/src/types/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use borsh::{io, BorshDeserialize, BorshSerialize};

use crate::error::ContractError;

pub const MIN_ACCOUNT_ID_LEN: usize = 2;
pub const MAX_ACCOUNT_ID_LEN: usize = 64;
const MIN_ACCOUNT_ID_LEN: usize = 2;
const MAX_ACCOUNT_ID_LEN: usize = 64;

#[derive(Default, Debug, Copy, Clone)]
pub struct AccountId(ArrayString<MAX_ACCOUNT_ID_LEN>);
Expand Down
2 changes: 1 addition & 1 deletion forwarder/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use address::Address;
pub use promise::{
PromiseAction, PromiseBatchAction, PromiseCreateArgs, PromiseResult, PromiseWithCallbackArgs,
};
pub use public_key::{PublicKey, RawPublicKey};
pub use public_key::PublicKey;

pub type Vec<T> = arrayvec::ArrayVec<T, 256>;

Expand Down
54 changes: 10 additions & 44 deletions forwarder/src/types/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use crate::error::ContractError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PublicKey {
/// ed25519 public keys are 32 bytes
Ed25519([u8; 32]),
Ed25519([u8; 33]),
/// secp256k1 keys are in the uncompressed 64 byte format
Secp256k1([u8; 64]),
Secp256k1([u8; 65]),
}

impl PublicKey {
#[must_use]
pub fn key_data(&self) -> &[u8] {
pub const fn as_bytes(&self) -> &[u8] {
match self {
Self::Ed25519(data) => &data[..],
Self::Secp256k1(data) => &data[..],
Self::Ed25519(bytes) => bytes,
Self::Secp256k1(bytes) => bytes,
}
}
}
Expand All @@ -27,16 +26,17 @@ impl FromStr for PublicKey {
let (key_type, key_data) = split_key_type_data(value)?;
Ok(match key_type {
KeyType::Ed25519 => {
let mut buf = [0; 32];
let mut buf = [0; 33];
bs58::decode(key_data)
.onto(&mut buf)
.onto(&mut buf[1..])
.expect("TODO: panic message");
Self::Ed25519(buf)
}
KeyType::Secp256k1 => {
let mut buf = [0; 64];
let mut buf = [0; 65];
buf[0] = 0x01;
bs58::decode(key_data)
.onto(&mut buf)
.onto(&mut buf[1..])
.expect("TODO: panic message");
Self::Secp256k1(buf)
}
Expand Down Expand Up @@ -71,37 +71,3 @@ impl FromStr for KeyType {
}
}
}

/// Similar to `NearPublicKey`, except the first byte includes
/// the curve identifier.
pub enum RawPublicKey {
Ed25519([u8; 33]),
Secp256k1([u8; 65]),
}

impl RawPublicKey {
pub const fn as_bytes(&self) -> &[u8] {
match self {
Self::Ed25519(bytes) => bytes,
Self::Secp256k1(bytes) => bytes,
}
}
}

impl<'a> From<&'a PublicKey> for RawPublicKey {
fn from(key: &'a PublicKey) -> Self {
match key {
PublicKey::Ed25519(_) => {
let mut buf = [0u8; 33];
buf[1..33].copy_from_slice(key.key_data());
Self::Ed25519(buf)
}
PublicKey::Secp256k1(_) => {
let mut buf = [0u8; 65];
buf[0] = 0x01;
buf[1..65].copy_from_slice(key.key_data());
Self::Secp256k1(buf)
}
}
}
}
2 changes: 1 addition & 1 deletion tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod wrap;
const RECEIVER: &str = "0x17ffdf6becbbc34d5c7d3bf4a0ed4a680395d057";
const TOTAL_SUPPLY: u128 = 1_000_000_000_000_000;
const MAX_NUM_CONTRACTS: usize = 8;
const MIN_FWD_BALANCE: NearToken = NearToken::from_millinear(450);
const MIN_FWD_BALANCE: NearToken = NearToken::from_millinear(445);

static WNEAR: Lazy<AccountId> = Lazy::new(|| AccountId::from_str("wrap.test.near").unwrap());

Expand Down

0 comments on commit c846820

Please sign in to comment.