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

Connection impl #4

Draft
wants to merge 11 commits into
base: client-impl
Choose a base branch
from
Draft
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
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ members = [
"client-impls/mock/meta",
"client-impls/qbft",
"client-impls/qbft/meta",
"connection",
"connection/meta",
"host",
"host/meta"
]
29 changes: 26 additions & 3 deletions client-impls/client-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

use common_types::{channel_types::height, Hash, Timestamp};
use common_types::{channel_types::height, ClientId, Hash, Timestamp};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();
Expand All @@ -10,21 +10,44 @@ pub struct ConsensusStateUpdate<M: ManagedTypeApi> {
pub height: height::Data,
}

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, PartialEq)]
#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq)]
pub enum ClientStatus {
None,
Active,
Expired,
Frozen,
}

#[derive(TypeAbi, TopEncode)]
#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct GetLatestInfoResultType {
pub latest_height: height::Data,
pub latest_timestamp: Timestamp,
pub client_status: ClientStatus,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct VerifyMembershipArgs<M: ManagedTypeApi> {
pub client_id: ClientId<M>,
pub height: height::Data,
pub delay_time_period: Timestamp,
pub delay_block_period: u64,
pub proof: Hash<M>,
pub prefix: ManagedBuffer<M>,
pub path: ManagedBuffer<M>,
pub value: ManagedBuffer<M>,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct VerifyNonMembershipArgs<M: ManagedTypeApi> {
pub client_id: ClientId<M>,
pub height: height::Data,
pub delay_time_period: Timestamp,
pub delay_block_period: u64,
pub proof: Hash<M>,
pub prefix: ManagedBuffer<M>,
pub path: ManagedBuffer<M>,
}

#[multiversx_sc::module]
pub trait CommonClientLogicModule {
fn set_ibc_handler(&self, ibc_handler: &ManagedAddress) {
Expand Down
47 changes: 15 additions & 32 deletions client-impls/local-host/src/views.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use client_common::{ClientStatus, GetLatestInfoResultType};
use client_common::{
ClientStatus, GetLatestInfoResultType, VerifyMembershipArgs, VerifyNonMembershipArgs,
};
use common_types::{channel_types::height, ClientId, Hash, Timestamp};
use host::{host_views::ProxyTrait as _, storage::ProxyTrait as _};

Expand Down Expand Up @@ -74,60 +76,41 @@ pub trait ViewsModule:
///
/// Proof uses "DEFAULT_PROOF_BYTES"
#[view(verifyMembership)]
fn verify_membership(
&self,
client_id: ClientId<Self::Api>,
height: height::Data,
_delay_time_period: Timestamp,
_delay_block_period: u64,
proof: Hash<Self::Api>,
prefix: ManagedBuffer,
path: ManagedBuffer,
value: ManagedBuffer,
) -> bool {
self.require_valid_client_id(&client_id);
let _ = self.get_timestamp_at_height(&client_id, &height);
self.require_ibc_prefix(&prefix);
fn verify_membership(&self, args: VerifyMembershipArgs<Self::Api>) -> bool {
self.require_valid_client_id(&args.client_id);
let _ = self.get_timestamp_at_height(&args.client_id, &args.height);
self.require_ibc_prefix(&args.prefix);

let default_proof = self
.crypto()
.keccak256(ManagedBuffer::from(DEFAULT_PROOF_BYTES));
require!(proof == default_proof, "Invalid proof");
require!(args.proof == default_proof, "Invalid proof");

let ibc_handler = self.ibc_handler().get();
let hashed_path = self.crypto().keccak256(path);
let hashed_path = self.crypto().keccak256(args.path);
let hash: Hash<Self::Api> = self
.host_proxy(ibc_handler)
.get_commitment(&hashed_path)
.execute_on_dest_context();

hash == self.crypto().keccak256(value)
hash == self.crypto().keccak256(args.value)
}

/// A generic proof verification method which verifies the absence of a given CommitmentPath at a specified height
///
/// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24)
#[view(verifyNonMembership)]
fn verify_non_membership(
&self,
client_id: ClientId<Self::Api>,
height: height::Data,
_delay_time_period: Timestamp,
_delay_block_period: u64,
proof: Hash<Self::Api>,
prefix: ManagedBuffer,
path: ManagedBuffer,
) -> bool {
let _ = self.get_timestamp_at_height(&client_id, &height);
self.require_ibc_prefix(&prefix);
fn verify_non_membership(&self, args: VerifyNonMembershipArgs<Self::Api>) -> bool {
let _ = self.get_timestamp_at_height(&args.client_id, &args.height);
self.require_ibc_prefix(&args.prefix);

let default_proof = self
.crypto()
.keccak256(ManagedBuffer::from(DEFAULT_PROOF_BYTES));
require!(proof == default_proof, "Invalid proof");
require!(args.proof == default_proof, "Invalid proof");

let ibc_handler = self.ibc_handler().get();
let hashed_path = self.crypto().keccak256(path);
let hashed_path = self.crypto().keccak256(args.path);
let hash: Hash<Self::Api> = self
.host_proxy(ibc_handler)
.get_commitment(&hashed_path)
Expand Down
52 changes: 20 additions & 32 deletions client-impls/mock/src/views.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use client_common::{ClientStatus, GetLatestInfoResultType};
use client_common::{
ClientStatus, GetLatestInfoResultType, VerifyMembershipArgs, VerifyNonMembershipArgs,
};
use common_types::{channel_types::height, ClientId, Hash, Timestamp};
use host::host_views::ProxyTrait as _;

Expand Down Expand Up @@ -58,43 +60,29 @@ pub trait ViewsModule:
///
/// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24)
#[view(verifyMembership)]
fn verify_membership(
&self,
client_id: ClientId<Self::Api>,
height: height::Data,
_delay_time_period: Timestamp,
_delay_block_period: u64,
proof: Hash<Self::Api>,
prefix: ManagedBuffer,
path: ManagedBuffer,
value: ManagedBuffer,
) -> bool {
let _ = self.get_timestamp_at_height(&client_id, &height);
self.require_ibc_prefix(&prefix);

let local_proof = self.encode_and_hash(&height, &prefix, &path, &value);
local_proof == proof
fn verify_membership(&self, args: VerifyMembershipArgs<Self::Api>) -> bool {
let _ = self.get_timestamp_at_height(&args.client_id, &args.height);
self.require_ibc_prefix(&args.prefix);

let local_proof = self.encode_and_hash(&args.height, &args.prefix, &args.path, &args.value);
local_proof == args.proof
}

/// A generic proof verification method which verifies the absence of a given CommitmentPath at a specified height
///
/// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24)
#[view(verifyNonMembership)]
fn verify_non_membership(
&self,
client_id: ClientId<Self::Api>,
height: height::Data,
_delay_time_period: Timestamp,
_delay_block_period: u64,
proof: Hash<Self::Api>,
prefix: ManagedBuffer,
path: ManagedBuffer,
) -> bool {
let _ = self.get_timestamp_at_height(&client_id, &height);
self.require_ibc_prefix(&prefix);

let local_proof = self.encode_and_hash(&height, &prefix, &path, &ManagedBuffer::new());
local_proof == proof
fn verify_non_membership(&self, args: VerifyNonMembershipArgs<Self::Api>) -> bool {
let _ = self.get_timestamp_at_height(&args.client_id, &args.height);
self.require_ibc_prefix(&args.prefix);

let local_proof = self.encode_and_hash(
&args.height,
&args.prefix,
&args.path,
&ManagedBuffer::new(),
);
local_proof == args.proof
}

/// returns the clientState corresponding to `clientId`
Expand Down
9 changes: 1 addition & 8 deletions client/src/create_and_update_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ pub trait CreateAndUpdateClientsModule:
let client_impl_mapper = self.client_registry(&args.client_type);
require!(!client_impl_mapper.is_empty(), "Client not registered");

// TODO: Check register function

let client_impl = client_impl_mapper.get();
let client_id = self.generate_client_identifier(&args.client_type);
self.client_info(&client_id).set(ClientInfo {
Expand Down Expand Up @@ -123,12 +121,7 @@ pub trait CreateAndUpdateClientsModule:
&self,
client_type: &ClientType<Self::Api>,
) -> ClientId<Self::Api> {
let next_client_seq = self.host_info().update(|host_info| {
let returned_val = host_info.next_client_seq;
host_info.next_client_seq += 1;

returned_val
});
let next_client_seq = self.get_next_client_seq();

sc_format!("{}-{}", client_type, next_client_seq)
}
Expand Down
7 changes: 5 additions & 2 deletions client/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 7
// Endpoints: 10
// Async Callback (empty): 1
// Total number of exported functions: 10
// Total number of exported functions: 13

#![no_std]

Expand All @@ -23,6 +23,9 @@ multiversx_sc_wasm_adapter::endpoints! {
createClient => create_client
updateClient => update_client
updateClientCommitments => update_client_commitments
setExpectedTimePerBlock => set_expected_time_per_block
registerClient => register_client
bindPort => bind_port
getHostTimestamp => get_host_timestamp
getCommitmentPrefix => get_commitment_prefix
checkAndGetClient => check_and_get_client
Expand Down
7 changes: 7 additions & 0 deletions common/common-modules/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ pub trait UtilsModule {
None => sc_panic!("Overlow!!!"),
}
}

fn encode_to_buffer<T: TopEncode>(&self, value: &T) -> ManagedBuffer {
let mut encoded_value = ManagedBuffer::new();
let _ = value.top_encode(&mut encoded_value);

encoded_value
}
}
20 changes: 10 additions & 10 deletions common/common-types/src/channel_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ pub mod channel {

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)]
pub enum State {
StateUninitializedUnspecified,
StateInit,
StateTryOpen,
StateOpen,
StateClosed,
StateFlushing,
StateFlushComplete,
UninitializedUnspecified,
Init,
TryOpen,
Open,
Closed,
Flushing,
FlushComplete,
}

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)]
pub enum Order {
OrderNoneUnspecified,
OrderUnordered,
OrderOrdered,
NoneUnspecified,
Unordered,
Ordered,
}

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode)]
Expand Down
Loading
Loading