Skip to content

Commit

Permalink
Merge pull request #13 from octopus-network/upgrade-near-sdk
Browse files Browse the repository at this point in the history
Integrate `octopus-lpos` and `octopus-appchain-anchor-ibc`.
  • Loading branch information
riversyang authored Dec 14, 2023
2 parents c69875d + 48ff642 commit 41ac142
Show file tree
Hide file tree
Showing 36 changed files with 2,763 additions and 1,145 deletions.
2,175 changes: 1,624 additions & 551 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"utils",
"wrapped-token",
]
resolver = "2"

[profile.release]
codegen-units = 1
Expand All @@ -18,6 +19,5 @@ panic = "abort"
overflow-checks = true

[patch.crates-io]
ibc = { git = "https://github.com/octopus-network/ibc-rs.git", branch = "octopus-relayer-v0.42.0" }
ibc-proto = { git = "https://github.com/octopus-network/ibc-proto-rs.git", branch = "octopus-relayer-v0.32.0" }
ics23 = { git = "https://github.com/octopus-network/ics23.git", branch = "for-near-ibc-pre2" }
ibc = { git = "https://github.com/octopus-network/ibc-rs.git", branch = "v0.45.0-generic-tendermint-verifier" }
ibc-proto = { git = "https://github.com/octopus-network/ibc-proto-rs.git", branch = "upgrade-borsh-version" }
8 changes: 5 additions & 3 deletions channel-escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "4.1.0"
near-contract-standards = "4.1.0"
ibc = { version = "0.42", default-features = false, features = ["borsh", "serde"]}
# near-sdk = "4.1.0"
near-sdk = { git = "https://github.com/near/near-sdk-rs", branch = "master" }
# near-contract-standards = "4.1.0"
near-contract-standards = { git = "https://github.com/near/near-sdk-rs", branch = "master" }
ibc = { version = "0.45.0", default-features = false, features = ["borsh", "serde"]}
hex = "0.4"
utils = { path = "../utils" }
15 changes: 9 additions & 6 deletions channel-escrow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![no_std]
#![deny(
warnings,
trivial_casts,
Expand All @@ -12,15 +11,14 @@ extern crate alloc;

use alloc::{
string::{String, ToString},
vec,
vec::Vec,
};
use ibc::applications::transfer::PORT_ID_STR;
use near_contract_standards::fungible_token::core::ext_ft_core;
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
borsh::{BorshDeserialize, BorshSerialize},
env,
json_types::U128,
json_types::{U128, U64},
near_bindgen,
serde::{Deserialize, Serialize},
serde_json,
Expand All @@ -36,6 +34,7 @@ use utils::{
};

#[derive(BorshSerialize, BorshStorageKey)]
#[borsh(crate = "near_sdk::borsh")]
pub enum StorageKey {
TokenContracts,
PendingTransferRequests,
Expand All @@ -45,6 +44,8 @@ pub enum StorageKey {
#[serde(crate = "near_sdk::serde")]
pub struct FtOnTransferMsg {
pub receiver: String,
#[serde(default)]
pub timeout_seconds: Option<U64>,
}

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -56,6 +57,7 @@ pub struct RegisteredAsset {

#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
#[borsh(crate = "near_sdk::borsh")]
pub struct Contract {
/// The account id of IBC/TAO implementation.
near_ibc_account: AccountId,
Expand Down Expand Up @@ -112,6 +114,7 @@ impl Contract {
amount,
sender: sender_id.to_string(),
receiver: msg.receiver,
timeout_seconds: msg.timeout_seconds,
};
ext_transfer_request_handler::ext(self.near_ibc_account())
.with_attached_deposit(0)
Expand Down Expand Up @@ -192,7 +195,7 @@ impl ChannelEscrow for Contract {
let token_contract = maybe_existed_token_contract.unwrap();
ext_ft_core::ext(token_contract.clone())
.with_attached_deposit(1)
.with_static_gas(utils::GAS_FOR_SIMPLE_FUNCTION_CALL * 2)
.with_static_gas(utils::GAS_FOR_SIMPLE_FUNCTION_CALL.saturating_mul(2))
.with_unused_gas_weight(0)
.ft_transfer(receiver_id, amount.into(), None);
}
Expand Down Expand Up @@ -251,7 +254,7 @@ impl ProcessTransferRequestCallback for Contract {
let token_contract = maybe_existed_token_contract.unwrap();
ext_ft_core::ext(token_contract.clone())
.with_attached_deposit(1)
.with_static_gas(utils::GAS_FOR_SIMPLE_FUNCTION_CALL * 2)
.with_static_gas(utils::GAS_FOR_SIMPLE_FUNCTION_CALL.saturating_mul(2))
.with_unused_gas_weight(0)
.ft_transfer(sender_id, amount.into(), None);
}
Expand Down
4 changes: 2 additions & 2 deletions contract-builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM rust:1.65.0
FROM rust:1.67.1

LABEL description="Container for builds"

RUN rustup default 1.65.0
RUN rustup default 1.67.1
RUN rustup target add wasm32-unknown-unknown
RUN rustup component add rustfmt

Expand Down
5 changes: 3 additions & 2 deletions escrow-factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "4.1.0"
ibc = { version = "0.42", default-features = false, features = ["borsh", "serde"]}
# near-sdk = "4.1.0"
near-sdk = { git = "https://github.com/near/near-sdk-rs", branch = "master" }
ibc = { version = "0.45.0", default-features = false, features = ["borsh", "serde"] }
hex = "0.4"
utils = { path = "../utils" }
10 changes: 5 additions & 5 deletions escrow-factory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![no_std]
#![deny(
warnings,
trivial_casts,
Expand All @@ -13,7 +12,6 @@ extern crate alloc;
use alloc::{
format,
string::{String, ToString},
vec,
vec::Vec,
};
use ibc::core::ics24_host::identifier::ChannelId;
Expand All @@ -29,13 +27,15 @@ use near_sdk::{
use utils::{interfaces::EscrowFactory, ExtraDepositCost};

#[derive(BorshSerialize, BorshStorageKey)]
#[borsh(crate = "near_sdk::borsh")]
pub enum StorageKey {
ChannelIdSet,
EscrowContractWasm,
}

#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
#[borsh(crate = "near_sdk::borsh")]
pub struct Contract {
channel_id_set: UnorderedSet<ChannelId>,
}
Expand Down Expand Up @@ -82,7 +82,7 @@ impl EscrowFactory for Contract {
.create_account()
.transfer(utils::INIT_BALANCE_FOR_CHANNEL_ESCROW_CONTRACT)
.deploy_contract(
env::storage_read(&StorageKey::EscrowContractWasm.try_to_vec().unwrap())
env::storage_read(&borsh::to_vec(&StorageKey::EscrowContractWasm).unwrap())
.unwrap(),
)
.function_call(
Expand Down Expand Up @@ -125,7 +125,7 @@ pub extern "C" fn store_wasm_of_channel_escrow() {
let input = env::input().expect("ERR_NO_INPUT");
let sha256_hash = env::sha256(&input);

let current_len = env::storage_read(&StorageKey::EscrowContractWasm.try_to_vec().unwrap())
let current_len = env::storage_read(&borsh::to_vec(&StorageKey::EscrowContractWasm).unwrap())
.map_or_else(|| 0, |bytes| bytes.len());
let blob_len = input.len();
if blob_len > current_len {
Expand All @@ -139,7 +139,7 @@ pub extern "C" fn store_wasm_of_channel_escrow() {
}

env::storage_write(
&StorageKey::EscrowContractWasm.try_to_vec().unwrap(),
&borsh::to_vec(&StorageKey::EscrowContractWasm).unwrap(),
&input,
);

Expand Down
14 changes: 9 additions & 5 deletions near-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "4.1.0"
near-contract-standards = "4.1.0"
ibc = { version = "0.42.0", default-features = false, features = ["borsh", "serde"]}
ibc-proto = { version = "0.32.0", default-features = false }
# near-sdk = "4.1.0"
near-sdk = { git = "https://github.com/near/near-sdk-rs", branch = "master" }
# near-contract-standards = "4.1.0"
near-contract-standards = { git = "https://github.com/near/near-sdk-rs", branch = "master" }
ibc = { version = "0.45.0", default-features = false, features = ["borsh", "serde"]}
ibc-proto = { version = "0.35.0", default-features = false, features = ["serde"] }
sha2 = { version = "0.10.6", default-features = false }
itertools = "0.10.5"
serde = "1.0"
getrandom = { version = "0.2", features = ["custom"] }
token-factory = { path = "../token-factory" }
utils = { path = "../utils" }
ics06-solomachine = { git = "https://github.com/octopus-network/ics06-solomachine.git", branch = "octopus-relayer-ibcv0.42.0" }
tendermint = { version = "0.33" }
hex = "0.4"
octopus-lpos = { git = "https://github.com/octopus-network/octopus-lpos-ibc.git", branch = "main" }
46 changes: 30 additions & 16 deletions near-ibc/src/collections/indexed_ascending_lookup_queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{types::ProcessingResult, *};

use super::IndexedAscendingQueueViewer;
use crate::{types::ProcessingResult, *};
use core::fmt::Debug;

/// A indexed ascending lookup queue.
///
Expand All @@ -13,6 +13,7 @@ use super::IndexedAscendingQueueViewer;
/// when remove them from the queue, the extra storage usage will not be released.
///
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
pub struct IndexedAscendingLookupQueue<K, V>
where
K: BorshDeserialize + BorshSerialize + Clone + Ord,
Expand All @@ -37,8 +38,8 @@ where
/// Implement change functions for `IndexedLookupQueue`.
impl<K, V> IndexedAscendingLookupQueue<K, V>
where
K: BorshDeserialize + BorshSerialize + Clone + Ord,
V: BorshDeserialize + BorshSerialize + Clone,
K: BorshDeserialize + BorshSerialize + Clone + Ord + Debug,
V: BorshDeserialize + BorshSerialize + Clone + Debug,
{
///
pub fn new(
Expand Down Expand Up @@ -88,10 +89,18 @@ where
/// Add a new element to the queue.
/// If the queue reaches max length, the oldest (first) element will be removed.
pub fn push_back(&mut self, element: (K, V)) {
assert!(
self.end_index == 0 || &element.0 > self.index_map.get(&self.end_index).unwrap(),
"The key to be added should be larger than the latest key in the queue."
);
if !(self.end_index == 0 || &element.0 > self.index_map.get(&self.end_index).unwrap()) {
log!(
"Maybe invalid element to add, key: {:?}, value: {:?}. \
The key to be added should be larger than the latest key in the queue. \
Current index range of queue: {} - {}, latest key in queue: {:?}",
element.0,
element.1,
self.start_index,
self.end_index,
self.index_map.get(&self.end_index).unwrap()
);
};
self.index_map.insert(self.end_index + 1, element.0.clone());
self.value_map.insert(element.0, element.1);
if self.start_index == 0 && self.end_index == 0 {
Expand All @@ -113,7 +122,7 @@ where
/// Set max length of the queue.
pub fn set_max_length(&mut self, max_length: u64) -> ProcessingResult {
self.max_length = max_length;
let max_gas = env::prepaid_gas() * 4 / 5;
let max_gas = env::prepaid_gas().saturating_mul(4).saturating_div(5);
while self.end_index - self.start_index + 1 > self.max_length {
self.pop_front();
self.flush();
Expand All @@ -129,32 +138,37 @@ where
ProcessingResult::Ok
}
/// Clear the queue.
pub fn clear(&mut self) -> ProcessingResult {
let max_gas = env::prepaid_gas() * 4 / 5;
pub fn clear(&mut self, lt_key: Option<&K>) -> ProcessingResult {
let max_gas = env::prepaid_gas().saturating_mul(4).saturating_div(5);
for index in self.start_index..self.end_index + 1 {
if let Some(key) = self.index_map.get(&index) {
if lt_key.is_some() && key.ge(lt_key.unwrap()) {
break;
}
env::storage_remove(
migration::get_storage_key_of_lookup_map(
&self.index_map_storage_key_prefix,
&key,
&index,
)
.as_slice(),
);
env::storage_remove(
migration::get_storage_key_of_lookup_map(
&self.value_map_storage_key_prefix,
&index,
&key,
)
.as_slice(),
);
}
self.start_index = index + 1;
if env::used_gas() >= max_gas {
self.start_index = index + 1;
return ProcessingResult::NeedMoreGas;
}
}
self.start_index = 0;
self.end_index = 0;
if self.start_index > self.end_index {
self.start_index = 0;
self.end_index = 0;
}
ProcessingResult::Ok
}
/// Flush lookup map to storage.
Expand Down
28 changes: 17 additions & 11 deletions near-ibc/src/collections/indexed_ascending_simple_queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{types::ProcessingResult, *};

use super::IndexedAscendingQueueViewer;
use crate::{types::ProcessingResult, *};
use core::fmt::Debug;

/// A simple implementation of `indexed ordered queue`.
///
Expand All @@ -15,9 +15,10 @@ use super::IndexedAscendingQueueViewer;
/// when remove them from the queue, the extra storage usage will not be released.
///
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
pub struct IndexedAscendingSimpleQueue<K>
where
K: BorshDeserialize + BorshSerialize + Clone + Ord,
K: BorshDeserialize + BorshSerialize + Clone + Ord + Debug,
{
/// The map of index to K.
index_map: LookupMap<u64, K>,
Expand All @@ -34,7 +35,7 @@ where
/// Implement change functions for `IndexedLookupQueue`.
impl<K> IndexedAscendingSimpleQueue<K>
where
K: BorshDeserialize + BorshSerialize + Clone + Ord,
K: BorshDeserialize + BorshSerialize + Clone + Ord + Debug,
{
///
pub fn new(index_map_storage_key: StorageKey, max_length: u64) -> Self {
Expand Down Expand Up @@ -63,10 +64,15 @@ where
}
///
pub fn push_back(&mut self, key: K) {
assert!(
self.end_index == 0 || &key > self.get_key_by_index(&self.end_index).unwrap(),
"The key to be added should be larger than the latest key in the queue."
);
if !(self.end_index == 0 || &key > self.get_key_by_index(&self.end_index).unwrap()) {
log!(
"The key to be added should be larger than the latest key in the queue. \
Key: {:?}, Latest key: {:?}",
key,
self.get_key_by_index(&self.end_index).unwrap()
);
}

self.index_map.insert(self.end_index + 1, key.clone());
if self.start_index == 0 && self.end_index == 0 {
self.start_index = 1;
Expand All @@ -89,7 +95,7 @@ where
///
pub fn set_max_length(&mut self, max_length: u64) -> ProcessingResult {
self.max_length = max_length;
let max_gas = env::prepaid_gas() * 4 / 5;
let max_gas = env::prepaid_gas().saturating_mul(4).saturating_div(5);
while self.end_index - self.start_index + 1 > self.max_length {
self.pop_front();
self.flush();
Expand All @@ -106,7 +112,7 @@ where
}
/// Clear the queue.
pub fn clear(&mut self) -> ProcessingResult {
let max_gas = env::prepaid_gas() * 4 / 5;
let max_gas = env::prepaid_gas().saturating_mul(4).saturating_div(5);
for index in self.start_index..self.end_index + 1 {
if let Some(key) = self.index_map.get(&index) {
env::storage_remove(
Expand Down Expand Up @@ -134,7 +140,7 @@ where

impl<K> IndexedAscendingQueueViewer<K> for IndexedAscendingSimpleQueue<K>
where
K: BorshDeserialize + BorshSerialize + Clone + Ord,
K: BorshDeserialize + BorshSerialize + Clone + Ord + Debug,
{
///
fn start_index(&self) -> u64 {
Expand Down
Loading

0 comments on commit 41ac142

Please sign in to comment.