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

feat: add ProveResponse to unionlabs #811

Merged
merged 1 commit into from
Oct 16, 2023
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
6 changes: 0 additions & 6 deletions generated/rust/src/union.galois.api.v1.rs

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

13 changes: 9 additions & 4 deletions lib/serde-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ impl core::fmt::Display for FromHexStringError {
}

pub fn to_hex<T: AsRef<[u8]>>(data: T) -> String {
format!(
"{HEX_ENCODING_PREFIX}{encoding}",
encoding = hex::encode(data.as_ref())
)
let data = data.as_ref();

let encoded = if data.is_empty() {
"0".to_string()
} else {
hex::encode(data)
};

format!("{HEX_ENCODING_PREFIX}{encoded}")
}

pub fn parse_hex<T>(string: impl AsRef<[u8]>) -> Result<T, FromHexStringError>
Expand Down
2 changes: 2 additions & 0 deletions lib/unionlabs/src/union/galois.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod prove_request;
pub mod prove_response;
pub mod validator_set_commit;
pub mod zero_knowledge_proof;
58 changes: 58 additions & 0 deletions lib/unionlabs/src/union/galois/prove_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::{Deserialize, Serialize};

use crate::{
errors::{required, InvalidLength, MissingField},
ethereum::H256,
union::galois::zero_knowledge_proof::ZeroKnowledgeProof,
Proto, TypeUrl,
};

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct ProveResponse {
pub proof: ZeroKnowledgeProof,
pub trusted_validator_set_root: H256,
pub untrusted_validator_set_root: H256,
}

impl Proto for ProveResponse {
type Proto = protos::union::galois::api::v1::ProveResponse;
}

impl TypeUrl for protos::union::galois::api::v1::ProveResponse {
const TYPE_URL: &'static str = "/union.galois.api.v1.ProveResponse";
}

impl From<ProveResponse> for protos::union::galois::api::v1::ProveResponse {
fn from(value: ProveResponse) -> Self {
Self {
proof: Some(value.proof.into()),
trusted_validator_set_root: value.trusted_validator_set_root.into(),
untrusted_validator_set_root: value.untrusted_validator_set_root.into(),
}
}
}

#[derive(Debug)]
pub enum TryFromProveResponseError {
MissingField(MissingField),
TrustedValidatorSetRoot(InvalidLength),
UntrustedValidatorSetRoot(InvalidLength),
}

impl TryFrom<protos::union::galois::api::v1::ProveResponse> for ProveResponse {
type Error = TryFromProveResponseError;

fn try_from(value: protos::union::galois::api::v1::ProveResponse) -> Result<Self, Self::Error> {
Ok(Self {
proof: required!(value.proof)?.into(),
trusted_validator_set_root: value
.trusted_validator_set_root
.try_into()
.map_err(TryFromProveResponseError::TrustedValidatorSetRoot)?,
untrusted_validator_set_root: value
.untrusted_validator_set_root
.try_into()
.map_err(TryFromProveResponseError::UntrustedValidatorSetRoot)?,
})
}
}
79 changes: 79 additions & 0 deletions lib/unionlabs/src/union/galois/zero_knowledge_proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::fmt::Debug;

use serde::{Deserialize, Serialize};

use crate::{Proto, TypeUrl};

#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct ZeroKnowledgeProof {
#[serde(with = "::serde_utils::hex_string")]
pub content: Vec<u8>,
#[serde(with = "::serde_utils::hex_string")]
pub compressed_content: Vec<u8>,
#[serde(with = "::serde_utils::hex_string")]
pub evm_proof: Vec<u8>,
#[serde(with = "::serde_utils::hex_string")]
pub public_inputs: Vec<u8>,
}

impl Debug for ZeroKnowledgeProof {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ZeroKnowledgeProof")
.field("content", &serde_utils::to_hex(&self.content))
.field(
"compressed_content",
&serde_utils::to_hex(&self.compressed_content),
)
.field("evm_proof", &serde_utils::to_hex(&self.evm_proof))
.field("public_inputs", &serde_utils::to_hex(&self.public_inputs))
.finish()
}
}

impl Proto for ZeroKnowledgeProof {
type Proto = protos::union::galois::api::v1::ZeroKnowledgeProof;
}

impl TypeUrl for protos::union::galois::api::v1::ZeroKnowledgeProof {
const TYPE_URL: &'static str = "/union.galois.api.v1.ZeroKnowledgeProof";
}

impl From<ZeroKnowledgeProof> for protos::union::galois::api::v1::ZeroKnowledgeProof {
fn from(value: ZeroKnowledgeProof) -> Self {
Self {
content: value.content,
compressed_content: value.compressed_content,
evm_proof: value.evm_proof,
public_inputs: value.public_inputs,
}
}
}

impl From<protos::union::galois::api::v1::ZeroKnowledgeProof> for ZeroKnowledgeProof {
fn from(value: protos::union::galois::api::v1::ZeroKnowledgeProof) -> Self {
Self {
content: value.content,
compressed_content: value.compressed_content,
evm_proof: value.evm_proof,
public_inputs: value.public_inputs,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn serde() {
let json = serde_json::to_string_pretty(&ZeroKnowledgeProof {
content: [].into(),
compressed_content: [].into(),
evm_proof: [].into(),
public_inputs: [].into(),
})
.unwrap();

println!("{json}");
}
}
14 changes: 8 additions & 6 deletions voyager/src/chain/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use num_bigint::BigUint;
use prost::Message;
use protos::{
cosmos::base::tendermint::v1beta1::AbciQueryRequest,
union::galois::api::v1::{union_prover_api_client, ProveResponse as RawProveResponse},
union::galois::api::v1::union_prover_api_client,
};
use serde::{Deserialize, Serialize};
use tendermint_rpc::Client;
Expand Down Expand Up @@ -44,7 +44,9 @@ use unionlabs::{
signed_msg_type::SignedMsgType, simple_validator::SimpleValidator,
},
},
union::galois::{prove_request::ProveRequest, validator_set_commit::ValidatorSetCommit},
union::galois::{
prove_request::ProveRequest, prove_response, validator_set_commit::ValidatorSetCommit,
},
IntoProto, MsgIntoProto, Proto, TryFromProto, TryFromProtoErrorOf,
};

Expand Down Expand Up @@ -332,7 +334,7 @@ where
.into_inner();

EthereumDataMsg::ProveResponse(ProveResponse {
response,
response: response.try_into().unwrap(),
__marker: PhantomData,
})
}
Expand Down Expand Up @@ -709,14 +711,15 @@ pub struct UntrustedCommit<C: ChainSpec> {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Validators<C: ChainSpec> {
pub height: Height,
// TODO: Use non-`tendermint-rs` type here
pub validators: Vec<tendermint::validator::Info>,
#[serde(skip)]
pub __marker: PhantomData<fn() -> C>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProveResponse<C: ChainSpec> {
pub response: RawProveResponse,
pub response: prove_response::ProveResponse,
#[serde(skip)]
pub __marker: PhantomData<fn() -> C>,
}
Expand Down Expand Up @@ -1241,7 +1244,7 @@ where
client_message: cometbls::header::Header {
signed_header,
trusted_height: req.update_from,
zero_knowledge_proof: response.proof.unwrap().evm_proof,
zero_knowledge_proof: response.proof.evm_proof,
},
},
update_from: req.update_from,
Expand All @@ -1263,7 +1266,6 @@ where
data: Fetch::TrustedClientState(FetchTrustedClientState {
// NOTE: We can pass update_to directly here since cosmos -> evm always updates to the exact height requested.
at: QueryHeight::Specific(req.update_to),
// at: QueryHeight::Latest,
client_id: req.client_id,
}),
}))),
Expand Down
33 changes: 31 additions & 2 deletions voyager/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,16 @@ pub mod aggregate {
Aggregate::ChannelHandshakeUpdateClient(_) => {
write!(f, "ChannelHandshakeUpdateClient")
}
Aggregate::PacketUpdateClient(_) => write!(f, "PacketUpdateClient"),
Aggregate::PacketUpdateClient(msg) => {
write!(
f,
"PacketUpdateClient::{}",
match msg.packet_event {
PacketEvent::Send(_) => "Send",
PacketEvent::Recv(_) => "Recv",
}
)
}
Aggregate::WaitForTrustedHeight(_) => write!(f, "WaitForTrustedHeight"),
Aggregate::FetchCounterpartyStateproof(_) => {
write!(f, "FetchCounterpartyStateproof")
Expand All @@ -427,7 +436,27 @@ pub mod aggregate {
Aggregate::ConsensusStateProofAtLatestHeight(_) => {
write!(f, "ConsensusStateProofAtLatestHeight")
}
Aggregate::AggregateMsgAfterUpdate(_) => write!(f, "AggregateMsgAfterUpdate"),
Aggregate::AggregateMsgAfterUpdate(msg) => {
write!(f, "AggregateMsgAfterUpdate::")?;
match msg {
AggregateMsgAfterUpdate::ConnectionOpenTry(_) => {
write!(f, "ConnectionOpenTry")
}
AggregateMsgAfterUpdate::ConnectionOpenAck(_) => {
write!(f, "ConnectionOpenAck")
}
AggregateMsgAfterUpdate::ConnectionOpenConfirm(_) => {
write!(f, "ConnectionOpenConfirm")
}
AggregateMsgAfterUpdate::ChannelOpenTry(_) => write!(f, "ChannelOpenTry"),
AggregateMsgAfterUpdate::ChannelOpenAck(_) => write!(f, "ChannelOpenAck"),
AggregateMsgAfterUpdate::ChannelOpenConfirm(_) => {
write!(f, "ChannelOpenConfirm")
}
AggregateMsgAfterUpdate::RecvPacket(_) => write!(f, "RecvPacket"),
AggregateMsgAfterUpdate::AckPacket(_) => write!(f, "AckPacket"),
}
}
Aggregate::LightClientSpecific(agg) => write!(f, "LightClientSpecific({})", agg.0),
}
}
Expand Down