Skip to content

Commit

Permalink
Merge pull request #10 from octopus-network/v1.2.0
Browse files Browse the repository at this point in the history
Upgrade to v1.2.0
  • Loading branch information
riversyang authored Apr 18, 2022
2 parents f9d01f3 + c043131 commit 0fb820c
Show file tree
Hide file tree
Showing 38 changed files with 1,359 additions and 902 deletions.
96 changes: 88 additions & 8 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion appchain-anchor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "appchain-anchor"
version = "1.1.2"
version = "1.2.0"
authors = ["Octopus Network"]
edition = "2018"

Expand Down
43 changes: 29 additions & 14 deletions appchain-anchor/src/anchor_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::{interfaces::AnchorViewer, validator_set::ValidatorSetViewer, *};

#[near_bindgen]
impl AnchorViewer for AppchainAnchor {
//
fn get_anchor_version(&self) -> String {
ANCHOR_VERSION.to_string()
}
//
fn get_anchor_settings(&self) -> AnchorSettings {
self.anchor_settings.get().unwrap()
Expand Down Expand Up @@ -33,6 +37,7 @@ impl AnchorViewer for AppchainAnchor {
//
fn get_anchor_status(&self) -> AnchorStatus {
let next_validator_set = self.next_validator_set.get().unwrap();
let appchain_messages = self.appchain_messages.get().unwrap();
AnchorStatus {
total_stake_in_next_era: next_validator_set.total_stake().into(),
validator_count_in_next_era: next_validator_set.validator_count().into(),
Expand All @@ -53,11 +58,10 @@ impl AnchorViewer for AppchainAnchor {
.unwrap()
.index_range(),
index_range_of_staking_history: self.staking_histories.get().unwrap().index_range(),
index_range_of_appchain_message_processing_results: self
.appchain_message_processing_results
.get()
.unwrap()
.index_range(),
nonce_range_of_appchain_messages: IndexRange {
start_index: U64::from(u64::from(appchain_messages.min_nonce())),
end_index: U64::from(u64::from(appchain_messages.max_nonce())),
},
permissionless_actions_status: self.permissionless_actions_status.get().unwrap(),
asset_transfer_is_paused: self.asset_transfer_is_paused,
rewards_withdrawal_is_paused: self.rewards_withdrawal_is_paused,
Expand Down Expand Up @@ -556,23 +560,34 @@ impl AnchorViewer for AppchainAnchor {
results
}
//
fn get_appchain_message_of(&self, nonce: u32) -> Option<AppchainMessage> {
let appchain_messages = self.appchain_messages.get().unwrap();
appchain_messages.get_message(nonce)
}
//
fn get_appchain_messages(
&self,
start_nonce: u32,
quantity: Option<u32>,
) -> Vec<AppchainMessage> {
let appchain_messages = self.appchain_messages.get().unwrap();
appchain_messages.get_messages(&start_nonce, quantity)
}
//
fn get_appchain_message_processing_result_of(
&self,
nonce: u32,
) -> Option<AppchainMessageProcessingResult> {
let appchain_message_processing_results =
self.appchain_message_processing_results.get().unwrap();
appchain_message_processing_results.get_processing_result(nonce)
let appchain_messages = self.appchain_messages.get().unwrap();
appchain_messages.get_processing_result(&nonce)
}
//
fn get_appchain_message_processing_results(
&self,
start_index: U64,
quantity: Option<U64>,
start_nonce: u32,
quantity: Option<u32>,
) -> Vec<AppchainMessageProcessingResult> {
let appchain_message_processing_results =
self.appchain_message_processing_results.get().unwrap();
appchain_message_processing_results
.get_processing_results(&start_index.0, quantity.map(|q| q.0))
let appchain_messages = self.appchain_messages.get().unwrap();
appchain_messages.get_processing_results(&start_nonce, quantity)
}
}
88 changes: 0 additions & 88 deletions appchain-anchor/src/appchain_message_processing_results.rs

This file was deleted.

155 changes: 155 additions & 0 deletions appchain-anchor/src/appchain_messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use crate::*;

impl IndexedAndClearable for u32 {
//
fn set_index(&mut self, _index: &u64) {
()
}
//
fn clear_extra_storage(&mut self) {
()
}
}

#[derive(BorshDeserialize, BorshSerialize)]
pub struct AppchainMessages {
///
message_nonces: LookupArray<u32>,
///
message_map: LookupMap<u32, AppchainMessage>,
///
processing_result_map: LookupMap<u32, AppchainMessageProcessingResult>,
///
min_nonce: u32,
///
max_nonce: u32,
}

impl AppchainMessages {
///
pub fn new() -> Self {
Self {
message_nonces: LookupArray::new(StorageKey::AppchainMessageNonces),
message_map: LookupMap::new(StorageKey::AppchainMessageMap.into_bytes()),
processing_result_map: LookupMap::new(
StorageKey::AppchainMessageProcessingResultMap.into_bytes(),
),
min_nonce: 0,
max_nonce: 0,
}
}
///
pub fn min_nonce(&self) -> u32 {
self.min_nonce
}
///
pub fn max_nonce(&self) -> u32 {
self.max_nonce
}
///
pub fn insert_message(&mut self, appchain_message: &AppchainMessage) {
let nonce = appchain_message.nonce;
if !self.message_map.contains_key(&nonce) {
self.message_map.insert(&nonce, appchain_message);
if self.min_nonce == 0 && self.max_nonce == 0 {
self.min_nonce = nonce;
self.max_nonce = nonce;
} else {
if nonce < self.min_nonce {
self.min_nonce = nonce;
}
if nonce > self.max_nonce {
self.max_nonce = nonce;
}
}
}
}
///
pub fn insert_processing_result(
&mut self,
appchain_message_nonce: u32,
processing_result: &AppchainMessageProcessingResult,
) {
if self.message_map.contains_key(&appchain_message_nonce) {
self.processing_result_map
.insert(&appchain_message_nonce, &processing_result);
}
}
///
pub fn get_message(&self, appchain_message_nonce: u32) -> Option<AppchainMessage> {
self.message_map.get(&appchain_message_nonce)
}
///
pub fn get_messages(&self, start_nonce: &u32, quantity: Option<u32>) -> Vec<AppchainMessage> {
let mut results = Vec::<AppchainMessage>::new();
let end_nonce = start_nonce
+ match quantity {
Some(quantity) => quantity,
None => 50,
};
for nonce in *start_nonce..end_nonce {
if let Some(message) = self.message_map.get(&nonce) {
results.push(message);
}
}
results
}
///
pub fn get_processing_result(&self, nonce: &u32) -> Option<AppchainMessageProcessingResult> {
self.processing_result_map.get(nonce)
}
///
pub fn get_processing_results(
&self,
start_nonce: &u32,
quantity: Option<u32>,
) -> Vec<AppchainMessageProcessingResult> {
let mut results = Vec::<AppchainMessageProcessingResult>::new();
let end_nonce = start_nonce
+ match quantity {
Some(quantity) => quantity,
None => 50,
};
for nonce in *start_nonce..end_nonce {
if let Some(processing_result) = self.processing_result_map.get(&nonce) {
results.push(processing_result);
}
}
results
}
///
pub fn clear(&mut self) -> MultiTxsOperationProcessingResult {
log!(
"Nonce range of appchain messsages: {} - {}",
self.min_nonce,
self.max_nonce
);
let mut nonce = self.min_nonce + 1;
while nonce <= self.max_nonce + 1 && env::used_gas() < GAS_CAP_FOR_MULTI_TXS_PROCESSING {
self.remove_messages_before(&nonce);
nonce += 1;
}
if nonce <= self.max_nonce + 1 {
self.min_nonce = nonce - 1;
log!(
"Nonce range of appchain messsages after clear: {} - {}",
self.min_nonce,
self.max_nonce
);
MultiTxsOperationProcessingResult::NeedMoreGas
} else {
self.min_nonce = 0;
self.max_nonce = 0;
MultiTxsOperationProcessingResult::Ok
}
}
///
pub fn remove_messages_before(&mut self, nonce: &u32) {
for nonce in self.min_nonce..*nonce {
self.message_map.remove_raw(&nonce.try_to_vec().unwrap());
self.processing_result_map
.remove_raw(&nonce.try_to_vec().unwrap());
}
self.min_nonce = *nonce;
}
}
28 changes: 14 additions & 14 deletions appchain-anchor/src/assets/near_fungible_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata;
use near_sdk::serde_json;

use crate::{interfaces::NearFungibleTokenManager, *};
use crate::{
interfaces::NearFungibleTokenManager,
permissionless_actions::AppchainMessagesProcessingContext, *,
};

pub trait FungibleTokenContractResolver {
/// Resolver for transfer NEAR fungible token
Expand Down Expand Up @@ -285,7 +288,8 @@ impl AppchainAnchor {
receiver_id_in_near: AccountId,
amount: U128,
appchain_message_nonce: u32,
) -> AppchainMessageProcessingResult {
processing_context: &mut AppchainMessagesProcessingContext,
) -> MultiTxsOperationProcessingResult {
let near_fungible_tokens = self.near_fungible_tokens.get().unwrap();
if let Some(near_fungible_token) =
near_fungible_tokens.get_by_contract_account(&contract_account)
Expand All @@ -308,23 +312,19 @@ impl AppchainAnchor {
0,
GAS_FOR_RESOLVER_FUNCTION,
));
AppchainMessageProcessingResult::Ok {
nonce: appchain_message_nonce,
message: Some(format!(
"Need to confirm result of 'ft_transfer' on account '{}'.",
&near_fungible_token.contract_account
)),
}
processing_context.add_prepaid_gas(GAS_FOR_FT_TRANSFER + GAS_FOR_RESOLVER_FUNCTION);
MultiTxsOperationProcessingResult::Ok
} else {
let message = format!(
"Invalid contract account of NEAR fungible token: {}",
contract_account
);
let result = AppchainMessageProcessingResult::Error {
nonce: appchain_message_nonce,
message: format!(
"Invalid contract account of NEAR fungible token: {}",
&contract_account
),
message: message.clone(),
};
self.record_appchain_message_processing_result(&result);
result
MultiTxsOperationProcessingResult::Error(message)
}
}
}
Expand Down
Loading

0 comments on commit 0fb820c

Please sign in to comment.