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

Contract call limits #520

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod primitives;

use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::LookupMap;
use near_sdk::collections::TreeMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between the TreeMap and LookupMap?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lookup map does not know what is inside of it. It just stores elements directly using the formula:
map_tag + <element key>. TreeMap builds a tree of elements. In our case, LookupMap is more efficient, but probably it's not that important, since the max size of the map is 8.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what specifically do we get out of using TreeMap over LookupMap? Doesn't seem clear to me what the benefit here is

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible to know the size of the LookupMap.

use near_sdk::log;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, near_bindgen, AccountId, PanicOnDefault, Promise, PromiseOrValue, PublicKey};
Expand Down Expand Up @@ -49,7 +49,7 @@ pub enum ProtocolContractState {
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct MpcContract {
protocol_state: ProtocolContractState,
pending_requests: LookupMap<[u8; 32], Option<(String, String)>>,
pending_requests: TreeMap<[u8; 32], Option<(String, String)>>,
}

#[near_bindgen]
Expand All @@ -68,7 +68,7 @@ impl MpcContract {
threshold,
pk_votes: PkVotes::new(),
}),
pending_requests: LookupMap::new(b"m"),
pending_requests: TreeMap::new(b"m"),
}
}

Expand Down Expand Up @@ -296,6 +296,9 @@ impl MpcContract {
#[allow(unused_variables)]
/// `key_version` must be less than or equal to the value at `latest_key_version`
pub fn sign(&mut self, payload: [u8; 32], path: String, key_version: u32) -> Promise {
if self.pending_requests.len() > 8 {
env::panic_str("Too many pending requests. Please, try again later.");
}
let latest_key_version: u32 = self.latest_key_version();
assert!(
key_version <= latest_key_version,
Expand Down Expand Up @@ -337,6 +340,10 @@ impl MpcContract {
PromiseOrValue::Value(signature)
}
None => {
if depth > 30 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why specifically 30? The max theoretical depth is ~60 and the max seen has been around late 50s. So why not 50?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked several transactions, and most of them ranged from 35 to 45. So I picked a reliable number. We do not want to have "out of gas" errors anymore. In good conditions we are returning a signature on step 8.

self.pending_requests.remove(&payload);
env::panic_str("Signature was not provided in time. Please, try again.");
}
log!(&format!(
"sign_helper: signature not ready yet (depth={})",
depth
Expand All @@ -358,7 +365,9 @@ impl MpcContract {
big_r,
s
);
self.pending_requests.insert(&payload, &Some((big_r, s)));
if self.pending_requests.contains_key(&payload) {
self.pending_requests.insert(&payload, &Some((big_r, s)));
}
}

#[private]
Expand All @@ -370,10 +379,16 @@ impl MpcContract {
}
Self {
protocol_state: ProtocolContractState::NotInitialized,
pending_requests: LookupMap::new(b"m"),
pending_requests: TreeMap::new(b"m"),
}
}

#[private]
pub fn clean_requests(&mut self) {
log!("clean requests");
self.pending_requests.clear();
}

/// This is the root public key combined from all the public keys of the participants.
pub fn public_key(&self) -> PublicKey {
match &self.protocol_state {
Expand Down
Loading