Skip to content

Commit

Permalink
Merge pull request #3 from multiversx/client-impl
Browse files Browse the repository at this point in the history
Client impl
  • Loading branch information
dorin-iancu authored Oct 24, 2024
2 parents f96d963 + dad1dec commit 8e32b21
Show file tree
Hide file tree
Showing 62 changed files with 2,934 additions and 1,428 deletions.
95 changes: 93 additions & 2 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
resolver = "2"

members = [
"client",
"client/meta",
"client-impls/local-host",
"client-impls/local-host/meta",
"client-impls/mock",
"client-impls/mock/meta",
"client-impls/qbft",
"client-impls/qbft/meta",
"host",
"host/meta"
]
14 changes: 14 additions & 0 deletions client-impls/client-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "client-common"
version = "0.0.0"
authors = ["Dorin Iancu <[email protected]>"]
edition = "2021"

[lib]
path = "src/lib.rs"

[dependencies.common-types]
path = "../../common/common-types"

[dependencies.multiversx-sc]
version = "=0.53.0"
51 changes: 51 additions & 0 deletions client-impls/client-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![no_std]

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

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

pub struct ConsensusStateUpdate<M: ManagedTypeApi> {
pub consensus_state_commitment: Hash<M>,
pub height: height::Data,
}

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

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

#[multiversx_sc::module]
pub trait CommonClientLogicModule {
fn set_ibc_handler(&self, ibc_handler: &ManagedAddress) {
require!(
self.blockchain().is_smart_contract(ibc_handler) && !ibc_handler.is_zero(),
"Invalid SC address"
);

self.ibc_handler().set(ibc_handler);
}

fn require_ibc_handler_caller(&self) {
let caller = self.blockchain().get_caller();
let ibc_handler = self.ibc_handler().get();
require!(
caller == ibc_handler,
"Only the IBC handler may call this endpoint"
);
}

#[view(getIbcHandler)]
#[storage_mapper("ibcHandler")]
fn ibc_handler(&self) -> SingleValueMapper<ManagedAddress>;
}
30 changes: 30 additions & 0 deletions client-impls/local-host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "local-host"
version = "0.0.0"
publish = false
edition = "2021"
authors = ["you"]

[lib]
path = "src/lib.rs"

[dependencies.multiversx-sc]
version = "=0.53.0"

[dependencies.client-common]
path = "../client-common"

[dependencies.host]
path = "../../host"

[dependencies.common-types]
path = "../../common/common-types"

[dependencies.common-modules]
path = "../../common/common-modules"

[dev-dependencies]
num-bigint = "0.4"

[dev-dependencies.multiversx-sc-scenario]
version = "=0.53.0"
12 changes: 12 additions & 0 deletions client-impls/local-host/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "local-host-meta"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.local-host]
path = ".."

[dependencies.multiversx-sc-meta-lib]
version = "=0.53.0"
default-features = false
3 changes: 3 additions & 0 deletions client-impls/local-host/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta_lib::cli_main::<local_host::AbiProvider>();
}
3 changes: 3 additions & 0 deletions client-impls/local-host/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
84 changes: 84 additions & 0 deletions client-impls/local-host/src/client_logic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use common_types::{channel_types::height, ClientId};

use crate::local_host_types::{client_state, consensus_state};

multiversx_sc::imports!();

// static CLIENT_TYPE: &[u8] = b"09-localhost";
static CLIENT_ID: &[u8] = b"09-localhost-0";

mod client_proxy {
use common_types::ClientId;

multiversx_sc::imports!();

#[multiversx_sc::proxy]
pub trait ClientProxy {
#[endpoint(updateClientCommitments)]
fn update_client_commitments(
&self,
client_id: ClientId<Self::Api>,
encoded_heights: ManagedBuffer,
);
}
}

#[multiversx_sc::module]
pub trait ClientLogicModule: client_common::CommonClientLogicModule {
/// initializes a new localhost client with the given client identifier, client state, and consensus state.
///
/// `client_id`` the client identifier must be match with `CLIENT_ID`
///
/// `client_state` the client state's latest height must be match with the current block number
///
/// `consensus_state` the consensus state must be match with the sentinel consensus state (i.e. 0)
#[endpoint(initializeClient)]
fn initialize_client(
&self,
client_id: ClientId<Self::Api>,
client_state: client_state::Data,
consensus_state: consensus_state::Data,
) -> height::Data {
self.require_ibc_handler_caller();
self.require_valid_client_id(&client_id);
require!(consensus_state.timestamp == 0, "Invalid consensus state");
require!(
client_state.latest_height.revision_number == 0,
"Invalid revision number"
);

let current_block = self.blockchain().get_block_nonce();
require!(
client_state.latest_height.revision_height == current_block,
"Invalid revision height"
);

height::Data {
revision_number: 0,
revision_height: current_block,
}
}

/// updates the client state commitment with the current block number
///
/// `client_id`` the client identifier must be match with `CLIENT_ID`
#[endpoint(updateClient)]
fn update_client(&self, client_id: ClientId<Self::Api>) -> ManagedVec<height::Data> {
self.require_valid_client_id(&client_id);

let ibc_handler = self.ibc_handler().get();
let _: () = self
.client_proxy(ibc_handler)
.update_client_commitments(client_id, ManagedBuffer::new())
.execute_on_dest_context();

ManagedVec::new()
}

fn require_valid_client_id(&self, client_id: &ClientId<Self::Api>) {
require!(client_id == CLIENT_ID, "Invalid client ID");
}

#[proxy]
fn client_proxy(&self, sc_address: ManagedAddress) -> client_proxy::ClientProxy<Self::Api>;
}
23 changes: 23 additions & 0 deletions client-impls/local-host/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_std]

multiversx_sc::imports!();

pub mod client_logic;
pub mod local_host_types;
pub mod views;

#[multiversx_sc::contract]
pub trait LocalHost:
client_common::CommonClientLogicModule
+ client_logic::ClientLogicModule
+ views::ViewsModule
+ common_modules::utils::UtilsModule
{
#[init]
fn init(&self, ibc_handler: ManagedAddress) {
self.set_ibc_handler(&ibc_handler);
}

#[upgrade]
fn upgrade(&self) {}
}
Loading

0 comments on commit 8e32b21

Please sign in to comment.