-
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
clear_payloads func #551
clear_payloads func #551
Changes from all commits
ec85056
971fae8
e3d43ea
89c72b3
d0ef151
1c44ac5
81188fd
6e029f4
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 |
---|---|---|
|
@@ -2,13 +2,15 @@ pub mod primitives; | |
|
||
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
use near_sdk::collections::LookupMap; | ||
use near_sdk::log; | ||
use near_sdk::serde::{Deserialize, Serialize}; | ||
use near_sdk::{env, near_bindgen, AccountId, PanicOnDefault, Promise, PromiseOrValue, PublicKey}; | ||
use near_sdk::{log, Gas}; | ||
use primitives::ParticipantInfo; | ||
use primitives::{CandidateInfo, Candidates, Participants, PkVotes, Votes}; | ||
use std::collections::{BTreeMap, HashSet}; | ||
|
||
const GAS_FOR_SIGN_CALL: Gas = Gas::from_gas(3 * 100_000_000_000_000); | ||
|
||
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Debug)] | ||
pub struct InitializingContractState { | ||
pub candidates: Candidates, | ||
|
@@ -336,16 +338,24 @@ impl MpcContract { | |
"This version of the signer contract doesn't support versions greater than {}", | ||
latest_key_version, | ||
); | ||
// Make sure sign call will not run out of gas doing recursive calls because the payload will never be removed | ||
assert!( | ||
env::prepaid_gas() >= GAS_FOR_SIGN_CALL, | ||
"Insufficient gas provided. Provided: {} Required: {}", | ||
env::prepaid_gas(), | ||
GAS_FOR_SIGN_CALL | ||
); | ||
log!( | ||
"sign: signer={}, payload={:?}, path={:?}, key_version={}", | ||
"sign: signer={}, predecessor={}, payload={:?}, path={:?}, key_version={}", | ||
env::signer_account_id(), | ||
env::predecessor_account_id(), | ||
payload, | ||
path, | ||
key_version | ||
); | ||
match self.pending_requests.get(&payload) { | ||
None => { | ||
self.add_request(&payload, &None); | ||
self.add_request(&payload, None); | ||
log!(&serde_json::to_string(&near_sdk::env::random_seed_array()).unwrap()); | ||
Self::ext(env::current_account_id()).sign_helper(payload, 0) | ||
} | ||
|
@@ -399,9 +409,7 @@ impl MpcContract { | |
big_r, | ||
s | ||
); | ||
if self.pending_requests.contains_key(&payload) { | ||
self.pending_requests.insert(&payload, &Some((big_r, s))); | ||
} | ||
self.add_signature(&payload, (big_r, s)); | ||
} else { | ||
env::panic_str("only participants can respond"); | ||
} | ||
|
@@ -410,20 +418,6 @@ impl MpcContract { | |
} | ||
} | ||
|
||
#[private] | ||
#[init(ignore_state)] | ||
pub fn clean(keys: Vec<near_sdk::json_types::Base64VecU8>) -> Self { | ||
log!("clean: keys={:?}", keys); | ||
for key in keys.iter() { | ||
env::storage_remove(&key.0); | ||
} | ||
Self { | ||
protocol_state: ProtocolContractState::NotInitialized, | ||
pending_requests: LookupMap::new(b"m"), | ||
request_counter: 0, | ||
} | ||
} | ||
|
||
/// 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 { | ||
|
@@ -441,18 +435,48 @@ impl MpcContract { | |
0 | ||
} | ||
|
||
fn add_request(&mut self, payload: &[u8; 32], signature: &Option<(String, String)>) { | ||
fn add_signature(&mut self, payload: &[u8; 32], signature: (String, String)) { | ||
if self.pending_requests.contains_key(payload) { | ||
self.pending_requests.insert(payload, &Some(signature)); | ||
} | ||
} | ||
|
||
fn add_request(&mut self, payload: &[u8; 32], signature: Option<(String, String)>) { | ||
if self.request_counter > 8 { | ||
env::panic_str("Too many pending requests. Please, try again later."); | ||
} | ||
if !self.pending_requests.contains_key(payload) { | ||
self.request_counter += 1; | ||
} | ||
self.pending_requests.insert(payload, signature); | ||
self.pending_requests.insert(payload, &signature); | ||
} | ||
|
||
fn remove_request(&mut self, payload: &[u8; 32]) { | ||
self.pending_requests.remove(payload); | ||
self.request_counter -= 1; | ||
} | ||
|
||
// Helper functions | ||
#[private] | ||
#[init(ignore_state)] | ||
pub fn clean(keys: Vec<near_sdk::json_types::Base64VecU8>) -> Self { | ||
log!("clean: keys={:?}", keys); | ||
for key in keys.iter() { | ||
env::storage_remove(&key.0); | ||
} | ||
Self { | ||
protocol_state: ProtocolContractState::NotInitialized, | ||
pending_requests: LookupMap::new(b"m"), | ||
request_counter: 0, | ||
} | ||
} | ||
|
||
#[private] | ||
pub fn clean_payloads(&mut self, payloads: Vec<[u8; 32]>, counter: u32) { | ||
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 specify the payloads instead of clearing all the requests in the pending_requests? is it because of LookupMap? 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 used
to clean the payloads on dev for now. 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. Ok, that is fine. But the old payloads are still there. 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. wait, why would the old payloads still be there? I thought 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. This is how insert and remove works in /// Inserts a serialized key-value pair into the map.
/// If the map did not have this key present, `None` is returned. Otherwise returns
/// a serialized value. Note, the keys that have the same hash value are undistinguished by
/// the implementation.
pub fn insert_raw(&mut self, key_raw: &[u8], value_raw: &[u8]) -> Option<Vec<u8>> {
let storage_key = self.raw_key_to_storage_key(key_raw);
if env::storage_write(&storage_key, value_raw) {
Some(env::storage_get_evicted().unwrap())
} else {
None
}
}
/// Removes a serialized key from the map, returning the serialized value at the key if the key
/// was previously in the map.
pub fn remove_raw(&mut self, key_raw: &[u8]) -> Option<Vec<u8>> {
let storage_key = self.raw_key_to_storage_key(key_raw);
if env::storage_remove(&storage_key) {
Some(env::storage_get_evicted().unwrap())
} else {
None
}
} LookupMap is not aware of its key-value pairs. They are stored directly on the contract memory which is also a map. The key for storing values is "LopkupMap prefix + provided key". It is not possible to clear the Lookup map without passing the keys. |
||
log!("clean_payloads"); | ||
for payload in payloads.iter() { | ||
self.pending_requests.remove(payload); | ||
} | ||
self.request_counter = counter; | ||
} | ||
} |
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.
could also just do
Gas::from_tgas(300)