Skip to content

Commit

Permalink
BridgeTokenFactory: is_used_proof() method. (#62)
Browse files Browse the repository at this point in the history
* Add `is_used_proof()` method.

* Update build files.
  • Loading branch information
sept-en authored Jun 3, 2021
1 parent 23b0c3a commit 97006b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
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.

0 comments on commit 97006b8

Please sign in to comment.