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

clear_payloads func #551

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Changes from 4 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
40 changes: 25 additions & 15 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl MpcContract {
s
);
if self.pending_requests.contains_key(&payload) {
self.pending_requests.insert(&payload, &Some((big_r, s)));
self.add_request(&payload, &Some((big_r, s)))
}
} else {
env::panic_str("only participants can respond");
Expand All @@ -378,20 +378,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 {
Expand Down Expand Up @@ -423,4 +409,28 @@ impl MpcContract {
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) {
Copy link
Contributor

@ppca ppca Apr 2, 2024

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

@ppca ppca Apr 2, 2024

Choose a reason for hiding this comment

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

I've used

#[private]
    pub fn clean_payloads(&mut self) {
        log!("clean_payloads");
        self.pending_requests = LookupMap::new(b"m");
        self.request_counter = 0;
    }

to clean the payloads on dev for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, that is fine. But the old payloads are still there.
LookupMap can not clean its key-value pairs. It is optimized for storage and access speed.

Copy link
Contributor

Choose a reason for hiding this comment

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

wait, why would the old payloads still be there? I thought self.pending_requests = LookupMap::new(b"m"); self.request_counter = 0; will reset it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is how insert and remove works in LookupMap

    /// 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;
}
}
Loading