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

Add is_used_proof() method. #62

Merged
merged 2 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions bridge-token-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,24 @@ impl BridgeTokenFactory {
format!("{}.{}", address, env::current_account_id())
}

/// Checks whether the provided proof is already used
pub fn is_used_proof(&self, #[serializer(borsh)] proof: Proof) -> bool {
self.used_events.contains(&proof.get_key())
}

/// Record proof to make sure it is not re-used later for anther deposit.
fn record_proof(&mut self, proof: &Proof) -> Balance {
// TODO: Instead of sending the full proof (clone only relevant parts of the Proof)
// log_index / receipt_index / header_data
assert_self();
let initial_storage = env::storage_usage();
let mut data = proof.log_index.try_to_vec().unwrap();
data.extend(proof.receipt_index.try_to_vec().unwrap());
data.extend(proof.header_data.clone());
let key = env::sha256(&data);

let proof_key = proof.get_key();
assert!(
!self.used_events.contains(&key),
!self.used_events.contains(&proof_key),
"Event cannot be reused for depositing."
);
self.used_events.insert(&key);
self.used_events.insert(&proof_key);
let current_storage = env::storage_usage();
let required_deposit =
Balance::from(current_storage - initial_storage) * STORAGE_PRICE_PER_BYTE;
Expand Down
11 changes: 10 additions & 1 deletion bridge-token-factory/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eth_types::*;
use ethabi::{Event, EventParam, Hash, Log, ParamType, RawLog, Token};
use ethabi::param_type::Writer;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::ext_contract;
use near_sdk::{env, ext_contract};
use near_sdk::serde::{Serialize, Deserialize};
use tiny_keccak::Keccak;

Expand Down Expand Up @@ -43,6 +43,15 @@ pub struct Proof {
pub proof: Vec<Vec<u8>>,
}

impl Proof {
pub fn get_key(&self) -> Vec<u8> {
let mut data = self.log_index.try_to_vec().unwrap();
data.extend(self.receipt_index.try_to_vec().unwrap());
data.extend(self.header_data.clone());
env::sha256(&data[..])
}
}

pub type EthEventParams = Vec<(String, ParamType, bool)>;

pub struct EthEvent {
Expand Down
Binary file modified res/bridge_token_factory.wasm
Binary file not shown.