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

solana-ibc: use ConsensusState derive #175

Merged
merged 1 commit into from
Jan 3, 2024
Merged
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
40 changes: 13 additions & 27 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ derive_more = "0.99.17"
hex-literal = "0.4.1"
ibc = { version = "0.48.2", default-features = false, features = ["borsh", "serde"] }
ibc-core-channel-types = { version = "0.48.2", default-features = false }
ibc-core-client-context = { version = "0.48.2", default-features = false }
ibc-core-client-types = { version = "0.48.2", default-features = false }
ibc-core-commitment-types = { version = "0.48.2", default-features = false }
ibc-core-connection-types = { version = "0.48.2", default-features = false }
ibc-core-host-types = { version = "0.48.2", default-features = false }
ibc-proto = { version = "0.39.1", default-features = false }
ibc-primitives = { version = "0.48.2", default-features = false }
ibc-proto = { version = "0.39.2", default-features = false }
ibc-testkit = { version = "0.48.2", default-features = false }
insta = { version = "1.34.0" }
pretty_assertions = "1.4.0"
Expand Down
3 changes: 2 additions & 1 deletion common/blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ edition = "2021"
borsh.workspace = true
bytemuck.workspace = true
derive_more.workspace = true
ibc-core-client-context.workspace = true
ibc-core-commitment-types.workspace = true
ibc-proto.workspace = true
ibc-primitives.workspace = true
prost = { workspace = true, features = ["prost-derive"] }
strum.workspace = true

Expand Down
32 changes: 24 additions & 8 deletions common/blockchain/src/ibc_state/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::num::NonZeroU64;

use ibc_proto::google::protobuf::Any;
use ibc_primitives::proto::Any;
use lib::hash::CryptoHash;
use prost::Message as _;

Expand All @@ -17,9 +17,9 @@ pub struct ConsensusState {
}

impl ConsensusState {
/// Encodes the state into a vector as protocol buffer message.
pub fn encode_to_vec(&self) -> alloc::vec::Vec<u8> {
proto::ConsensusState::from(self).encode_to_vec()
pub fn new(block_hash: &CryptoHash, timestamp_ns: NonZeroU64) -> Self {
let block_hash = block_hash.as_array().to_vec().into();
Self { block_hash, timestamp_ns }
}

/// Decodes the state from a protocol buffer message.
Expand All @@ -28,13 +28,24 @@ impl ConsensusState {
}
}

impl ConsensusState {
pub fn new(block_hash: &CryptoHash, timestamp_ns: NonZeroU64) -> Self {
let block_hash = block_hash.as_array().to_vec().into();
Self { block_hash, timestamp_ns }
impl ibc_core_client_context::consensus_state::ConsensusState
for ConsensusState
{
fn root(&self) -> &ibc_core_commitment_types::commitment::CommitmentRoot {
&self.block_hash
}

fn timestamp(&self) -> ibc_primitives::Timestamp {
ibc_primitives::Timestamp::from_nanoseconds(self.timestamp_ns.get())
.unwrap()
}

fn encode_vec(self) -> alloc::vec::Vec<u8> {
proto::ConsensusState::from(self).encode_to_vec()
}
}


impl From<ConsensusState> for proto::ConsensusState {
fn from(state: ConsensusState) -> Self {
Self {
Expand Down Expand Up @@ -105,6 +116,11 @@ impl TryFrom<&Any> for ConsensusState {
}
}

impl ibc_primitives::proto::Protobuf<crate::proto::ConsensusState>
for ConsensusState
{
}


#[test]
fn test_consensus_state() {
Expand Down
2 changes: 1 addition & 1 deletion common/blockchain/src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ibc_proto::google::protobuf::Any;
use ibc_primitives::proto::Any;
use prost::Message as _;

mod pb {
Expand Down
Loading
Loading