-
Notifications
You must be signed in to change notification settings - Fork 17
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
Contract call limits #520
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
use near_sdk::log; | ||
use near_sdk::serde::{Deserialize, Serialize}; | ||
use near_sdk::{env, near_bindgen, AccountId, PanicOnDefault, Promise, PromiseOrValue, PublicKey}; | ||
|
@@ -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] | ||
|
@@ -68,7 +68,7 @@ impl MpcContract { | |
threshold, | ||
pk_votes: PkVotes::new(), | ||
}), | ||
pending_requests: LookupMap::new(b"m"), | ||
pending_requests: TreeMap::new(b"m"), | ||
} | ||
} | ||
|
||
|
@@ -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, | ||
|
@@ -337,6 +340,10 @@ impl MpcContract { | |
PromiseOrValue::Value(signature) | ||
} | ||
None => { | ||
if depth > 30 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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] | ||
|
@@ -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 { | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
overLookupMap
? Doesn't seem clear to me what the benefit here isThere was a problem hiding this comment.
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
.