Skip to content

Commit

Permalink
WIP - Added mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiblt1304 committed Feb 12, 2024
1 parent fda791b commit de7b38b
Showing 1 changed file with 31 additions and 45 deletions.
76 changes: 31 additions & 45 deletions multisigverifier/src/bridge_operations.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
use bls_signature::{self, BlsSignature};
use esdt_safe::from_sovereign::{self, transfer_tokens};
use transaction::TransferData;

pub struct PendingOperations<M: ManagedTypeApi> {
hash_of_hashes: ManagedBuffer<M>,
bridge_operations_hash: MultiValueEncoded<M, ManagedBuffer<M>>,
bls_signature: BlsSignature<M>
}

pub struct Operation<M: ManagedTypeApi> {
address: ManagedAddress<M>,
token_info: MultiValue3<TokenIdentifier<M>, ManagedBuffer<M>, ManagedBuffer<M>>,
function_to_call: ManagedBuffer<M>,
args: ManagedBuffer<M>,
gas_limit: BigUint<M>
}
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[multiversx_sc::module]
pub trait BridgeOperationsModule
pub trait BridgeOperationsModule:
bls_signature::BlsSignatureModule
// + from_sovereign::transfer_tokens::TransferTokensModule
// + multiversx_sc_modules::pause::EndpointWrappers
// + multiversx_sc_modules::pause::PauseModule
// + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
{
fn register_bridge_operations(
&self,
operations: PendingOperations<Self::Api>
hash_of_hashes: ManagedBuffer,
hash_of_bridge_ops: ManagedVec<ManagedBuffer>,
signature: BlsSignature<Self::Api>,
signature_data: &ManagedBuffer,
//use Transaction nonce, token_data
) {
let caller_address = self.blockchain().get_caller();

let current_operations = self.operations_mapper().get();

require!(
!current_operations.contains(&operations.hash_of_hashes),
"Hash of operations already exists"
);

current_operations.push(operations);
let caller = self.blockchain().get_caller();
let is_batch_valid = self.check_validity(signature_data, signature, caller);
self.is_operations_batch_valid().insert(hash_of_hashes, is_batch_valid);
self.operations_mapper().insert(hash_of_hashes, hash_of_bridge_ops);
}

fn add_board_member(
Expand All @@ -41,17 +34,17 @@ pub trait BridgeOperationsModule
let user_id = self.board_members().get_or_create_user(user_address);
}

#[endpoint(sign)]
fn sign(
&self,
operations: PendingOperations<Self::Api>, //OutGointTxData
signature_data: &ManagedBuffer,
signature: &BlsSignature<Self::Api>
) {
let caller = self.blockchain().get_caller();
let signers = self.all_signers().get();

assert!(
!signers.contains(caller),
!signers.get(caller),
"Caller already signed the operations!"
);

Expand All @@ -63,41 +56,32 @@ pub trait BridgeOperationsModule
signature_data: &ManagedBuffer,
signature: &BlsSignature<Self::Api>,
user: ManagedAddress
) {
) -> bool {
let is_bls_valid = self.crypto().verify_bls(user.as_managed_buffer(), signature_data, signature);
let signature_count = self.signatures().len();

if is_bls_valid && signature_count > 2/3 * self.board_members() {
self.is_valid().set(true);
true
}

self.is_valid().set(false);
false
}

#[endpoint(execBridgeOp)]
fn execute_bridge_operation_endpoint(
&self,
operations: MultiValueEncoded<Self::Api, Operation<Self::Api>>,
operation_hash: ManagedBuffer,
) {
// require action not executed
let caller = self.blockchain().get_caller();
// require validator

self.execute_operation(operation_hash);

// transfer_tokens::batch_transfer_esdt_token();
}

fn execute_operation(&self, operation_hash: ManagedBuffer) {
// remove operation from mapper
let is_valid = self.is_valid().get();
let operation = self.pending_operations_mapper().get(operation_hash);

require!(
is_valid,
"The requirements for executing have not been met"
);

// exec operation.function to call with operation.args
fn execute_operation(&self, operation_hash: ManagedBuffer, data: TransferData<Self::Api>) {
}

#[view(signatures)]
Expand All @@ -108,10 +92,12 @@ pub trait BridgeOperationsModule
fn is_valid(&self) -> SingleValueMapper<bool>;

// maybe use a dictionary?
#[view(getOperations)]
#[storage_mapper("operations")]
fn pending_operations_mapper(&self) -> MultiValueEncoded<Self::Api, PendingOperations<Self::Api>>;
#[storage_mapper("valid_batches")]
fn is_operations_batch_valid(&self) -> MapMapper<ManagedBuffer, bool>;

#[storage_mapper("boardMembers")]
fn board_members(&self) -> UserMapper;

#[storage_mapper("operations_mapper")]
fn operations_mapper(&self) -> MapMapper<ManagedBuffer, ManagedBuffer>;
}

0 comments on commit de7b38b

Please sign in to comment.