-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from multiversx/client-impl
Client impl
- Loading branch information
Showing
62 changed files
with
2,934 additions
and
1,428 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"language": "rust" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
Oops, something went wrong.