Skip to content

Commit

Permalink
rename Message to ProxyMessage and implement `submit_misbehaviour…
Browse files Browse the repository at this point in the history
…` for lcp-client

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Feb 2, 2024
1 parent 7c81810 commit 6219311
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use context::Context;
use crypto::{EnclavePublicKey, Signer, Verifier};
use ecall_commands::{AggregateMessagesInput, AggregateMessagesResponse, LightClientResponse};
use light_client::{
commitments::{self, prove_commitment, Message, UpdateClientMessage},
commitments::{self, prove_commitment, ProxyMessage, UpdateStateProxyMessage},
HostContext, LightClientResolver,
};
use store::KVStore;
Expand Down Expand Up @@ -32,31 +32,31 @@ pub fn aggregate_messages<R: LightClientResolver, S: KVStore, K: Signer>(
let messages = input
.messages
.into_iter()
.map(|c| Message::from_bytes(&c)?.try_into())
.map(|m| ProxyMessage::from_bytes(&m)?.try_into())
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.zip(input.signatures.iter())
.map(|(c, s)| -> Result<_, Error> {
verify_commitment(&pk, &c, s)?;
c.context.validate(ctx.host_timestamp())?;
Ok(c)
.map(|(m, s)| -> Result<_, Error> {
verify_message(&pk, &m, s)?;
m.context.validate(ctx.host_timestamp())?;
Ok(m)
})
.collect::<Result<Vec<_>, _>>()?;

let message = Message::from(commitments::aggregate_messages(messages)?);
let message = ProxyMessage::from(commitments::aggregate_messages(messages)?);
let proof = prove_commitment(ek, input.signer, message)?;

Ok(LightClientResponse::AggregateMessages(
AggregateMessagesResponse(proof),
))
}

fn verify_commitment(
fn verify_message(
verifier: &EnclavePublicKey,
commitment: &UpdateClientMessage,
message: &UpdateStateProxyMessage,
signature: &[u8],
) -> Result<(), Error> {
let message_bytes = Message::UpdateClient(commitment.clone()).to_bytes();
let message_bytes = ProxyMessage::UpdateState(message.clone()).to_bytes();
verifier
.verify(&message_bytes, signature)
.map_err(Error::crypto)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::prelude::*;
use context::Context;
use crypto::Signer;
use ecall_commands::{LightClientResponse, UpdateClientInput, UpdateClientResponse};
use light_client::commitments::{prove_commitment, CommitmentProof, EmittedState, Message};
use light_client::commitments::{prove_commitment, CommitmentProof, EmittedState, ProxyMessage};
use light_client::{ClientKeeper, LightClientResolver, UpdateClientResult};
use store::KVStore;

Expand All @@ -18,7 +18,7 @@ pub fn update_client<R: LightClientResolver, S: KVStore, K: Signer>(
let ek = ctx.get_enclave_key();
match lc.update_client(ctx, input.client_id.clone(), input.any_header.into())? {
UpdateClientResult::UpdateClient(mut data) => {
let message: Message = {
let message: ProxyMessage = {
if input.include_state && data.message.emitted_states.is_empty() {
data.message.emitted_states =
vec![EmittedState(data.height, data.new_any_client_state.clone())];
Expand Down
4 changes: 2 additions & 2 deletions modules/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub use context::{TrustingPeriodContext, ValidationContext};
pub use encoder::EthABIEncoder;
pub use errors::Error;
pub use message::{
aggregate_messages, CommitmentPrefix, EmittedState, Message, MisbehaviourMessage, PrevState,
UpdateClientMessage, VerifyMembershipMessage,
aggregate_messages, CommitmentPrefix, EmittedState, MisbehaviourProxyMessage, PrevState,
ProxyMessage, UpdateStateProxyMessage, VerifyMembershipProxyMessage,
};
pub use proof::CommitmentProof;
pub use prover::prove_commitment;
Expand Down
109 changes: 57 additions & 52 deletions modules/commitments/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use self::misbehaviour::{MisbehaviourMessage, PrevState};
pub use self::update_client::{aggregate_messages, EmittedState, UpdateClientMessage};
pub use self::verify_membership::{CommitmentPrefix, VerifyMembershipMessage};
pub use self::misbehaviour::{MisbehaviourProxyMessage, PrevState};
pub use self::update_client::{aggregate_messages, EmittedState, UpdateStateProxyMessage};
pub use self::verify_membership::{CommitmentPrefix, VerifyMembershipProxyMessage};
use crate::encoder::EthABIEncoder;
use crate::prelude::*;
use crate::Error;
Expand All @@ -14,18 +14,18 @@ mod verify_membership;
pub const MESSAGE_SCHEMA_VERSION: u16 = 1;
pub const MESSAGE_HEADER_SIZE: usize = 32;

pub const MESSAGE_TYPE_UPDATE_CLIENT: u16 = 1;
pub const MESSAGE_TYPE_UPDATE_STATE: u16 = 1;
pub const MESSAGE_TYPE_STATE: u16 = 2;
pub const MESSAGE_TYPE_MISBEHAVIOUR: u16 = 3;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Message {
UpdateClient(UpdateClientMessage),
VerifyMembership(VerifyMembershipMessage),
Misbehaviour(MisbehaviourMessage),
pub enum ProxyMessage {
UpdateState(UpdateStateProxyMessage),
VerifyMembership(VerifyMembershipProxyMessage),
Misbehaviour(MisbehaviourProxyMessage),
}

impl Message {
impl ProxyMessage {
pub fn to_bytes(self) -> Vec<u8> {
self.ethabi_encode()
}
Expand All @@ -47,49 +47,49 @@ impl Message {

pub fn message_type(&self) -> u16 {
match self {
Message::UpdateClient(_) => MESSAGE_TYPE_UPDATE_CLIENT,
Message::VerifyMembership(_) => MESSAGE_TYPE_STATE,
Message::Misbehaviour(_) => MESSAGE_TYPE_MISBEHAVIOUR,
Self::UpdateState(_) => MESSAGE_TYPE_UPDATE_STATE,
Self::VerifyMembership(_) => MESSAGE_TYPE_STATE,
Self::Misbehaviour(_) => MESSAGE_TYPE_MISBEHAVIOUR,
}
}

pub fn validate(&self) -> Result<(), Error> {
match self {
Message::UpdateClient(c) => c.validate(),
Message::VerifyMembership(c) => c.validate(),
Message::Misbehaviour(c) => c.validate(),
Self::UpdateState(c) => c.validate(),
Self::VerifyMembership(c) => c.validate(),
Self::Misbehaviour(c) => c.validate(),
}
}
}

impl Display for Message {
impl Display for ProxyMessage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Message::UpdateClient(c) => write!(f, "{}", c),
Message::VerifyMembership(c) => write!(f, "{}", c),
Message::Misbehaviour(c) => write!(f, "{}", c),
Self::UpdateState(c) => write!(f, "{}", c),
Self::VerifyMembership(c) => write!(f, "{}", c),
Self::Misbehaviour(c) => write!(f, "{}", c),
}
}
}

impl TryFrom<Message> for UpdateClientMessage {
impl TryFrom<ProxyMessage> for UpdateStateProxyMessage {
type Error = Error;
fn try_from(value: Message) -> Result<Self, Self::Error> {
fn try_from(value: ProxyMessage) -> Result<Self, Self::Error> {
match value {
Message::UpdateClient(m) => Ok(m),
ProxyMessage::UpdateState(m) => Ok(m),
_ => Err(Error::unexpected_message_type(
MESSAGE_TYPE_UPDATE_CLIENT,
MESSAGE_TYPE_UPDATE_STATE,
value.message_type(),
)),
}
}
}

impl TryFrom<Message> for VerifyMembershipMessage {
impl TryFrom<ProxyMessage> for VerifyMembershipProxyMessage {
type Error = Error;
fn try_from(value: Message) -> Result<Self, Self::Error> {
fn try_from(value: ProxyMessage) -> Result<Self, Self::Error> {
match value {
Message::VerifyMembership(m) => Ok(m),
ProxyMessage::VerifyMembership(m) => Ok(m),
_ => Err(Error::unexpected_message_type(
MESSAGE_TYPE_STATE,
value.message_type(),
Expand All @@ -98,11 +98,11 @@ impl TryFrom<Message> for VerifyMembershipMessage {
}
}

impl TryFrom<Message> for MisbehaviourMessage {
impl TryFrom<ProxyMessage> for MisbehaviourProxyMessage {
type Error = Error;
fn try_from(value: Message) -> Result<Self, Self::Error> {
fn try_from(value: ProxyMessage) -> Result<Self, Self::Error> {
match value {
Message::Misbehaviour(m) => Ok(m),
ProxyMessage::Misbehaviour(m) => Ok(m),
_ => Err(Error::unexpected_message_type(
MESSAGE_TYPE_MISBEHAVIOUR,
value.message_type(),
Expand All @@ -111,21 +111,21 @@ impl TryFrom<Message> for MisbehaviourMessage {
}
}

impl From<UpdateClientMessage> for Message {
fn from(value: UpdateClientMessage) -> Self {
Message::UpdateClient(value)
impl From<UpdateStateProxyMessage> for ProxyMessage {
fn from(value: UpdateStateProxyMessage) -> Self {
ProxyMessage::UpdateState(value)
}
}

impl From<VerifyMembershipMessage> for Message {
fn from(value: VerifyMembershipMessage) -> Self {
Message::VerifyMembership(value)
impl From<VerifyMembershipProxyMessage> for ProxyMessage {
fn from(value: VerifyMembershipProxyMessage) -> Self {
ProxyMessage::VerifyMembership(value)
}
}

impl From<MisbehaviourMessage> for Message {
fn from(value: MisbehaviourMessage) -> Self {
Message::Misbehaviour(value)
impl From<MisbehaviourProxyMessage> for ProxyMessage {
fn from(value: MisbehaviourProxyMessage) -> Self {
ProxyMessage::Misbehaviour(value)
}
}

Expand All @@ -136,14 +136,14 @@ sol! {
}
}

impl EthABIEncoder for Message {
impl EthABIEncoder for ProxyMessage {
fn ethabi_encode(self) -> Vec<u8> {
EthABIHeaderedMessage {
header: self.header().into(),
message: match self {
Message::UpdateClient(c) => c.ethabi_encode(),
Message::VerifyMembership(c) => c.ethabi_encode(),
Message::Misbehaviour(c) => c.ethabi_encode(),
Self::UpdateState(c) => c.ethabi_encode(),
Self::VerifyMembership(c) => c.ethabi_encode(),
Self::Misbehaviour(c) => c.ethabi_encode(),
},
}
.abi_encode()
Expand Down Expand Up @@ -177,8 +177,13 @@ impl EthABIEncoder for Message {
}
let message = eth_abi_message.message;
match message_type {
MESSAGE_TYPE_UPDATE_CLIENT => Ok(UpdateClientMessage::ethabi_decode(&message)?.into()),
MESSAGE_TYPE_STATE => Ok(VerifyMembershipMessage::ethabi_decode(&message)?.into()),
MESSAGE_TYPE_UPDATE_STATE => {
Ok(UpdateStateProxyMessage::ethabi_decode(&message)?.into())
}
MESSAGE_TYPE_STATE => Ok(VerifyMembershipProxyMessage::ethabi_decode(&message)?.into()),
MESSAGE_TYPE_MISBEHAVIOUR => {
Ok(MisbehaviourProxyMessage::ethabi_decode(&message)?.into())
}
_ => Err(Error::invalid_abi(format!(
"invalid message type: {}",
message_type
Expand All @@ -203,16 +208,16 @@ mod tests {
}

fn test_update_client_message(
c1: UpdateClientMessage,
c1: UpdateStateProxyMessage,
proof_signer: Address,
proof_signature: Vec<u8>,
) {
let v = c1.clone().ethabi_encode();
let c2 = UpdateClientMessage::ethabi_decode(&v).unwrap();
let c2 = UpdateStateProxyMessage::ethabi_decode(&v).unwrap();
assert_eq!(c1, c2);

let p1 = CommitmentProof {
message: Message::from(c1).to_bytes(),
message: ProxyMessage::from(c1).to_bytes(),
signer: proof_signer,
signature: proof_signature.to_vec(),
};
Expand All @@ -234,7 +239,7 @@ mod tests {
proof_signer in any::<[u8; 20]>(),
proof_signature in any::<[u8; 65]>()
) {
let c1 = UpdateClientMessage {
let c1 = UpdateStateProxyMessage {
prev_height,
prev_state_id,
post_height,
Expand Down Expand Up @@ -263,7 +268,7 @@ mod tests {
untrusted_header_timestamp in ..=MAX_UNIX_TIMESTAMP_NANOS,
trusted_state_timestamp in ..=MAX_UNIX_TIMESTAMP_NANOS
) {
let c1 = UpdateClientMessage {
let c1 = UpdateStateProxyMessage {
prev_height,
prev_state_id,
post_height,
Expand Down Expand Up @@ -292,19 +297,19 @@ mod tests {
proof_signer in any::<[u8; 20]>(),
proof_signature in any::<[u8; 65]>()
) {
let c1 = VerifyMembershipMessage {
let c1 = VerifyMembershipProxyMessage {
prefix,
path,
value,
height,
state_id,
};
let v = c1.clone().ethabi_encode();
let c2 = VerifyMembershipMessage::ethabi_decode(&v).unwrap();
let c2 = VerifyMembershipProxyMessage::ethabi_decode(&v).unwrap();
assert_eq!(c1, c2);

let p1 = CommitmentProof {
message: Message::from(c1).to_bytes(),
message: ProxyMessage::from(c1).to_bytes(),
signer: Address(proof_signer),
signature: proof_signature.to_vec(),
};
Expand Down
22 changes: 11 additions & 11 deletions modules/commitments/src/message/misbehaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use prost::Message;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MisbehaviourMessage {
pub struct MisbehaviourProxyMessage {
pub prev_states: Vec<PrevState>,
pub context: ValidationContext,
pub client_message: Any,
Expand All @@ -18,7 +18,7 @@ pub struct PrevState {
pub state_id: StateID,
}

impl MisbehaviourMessage {
impl MisbehaviourProxyMessage {
pub fn validate(&self) -> Result<(), Error> {
if self.prev_states.is_empty() {
return Err(Error::empty_prev_states());
Expand All @@ -27,7 +27,7 @@ impl MisbehaviourMessage {
}
}

impl Display for MisbehaviourMessage {
impl Display for MisbehaviourProxyMessage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
Expand All @@ -43,15 +43,15 @@ sol! {
bytes32 state_id;
}

struct EthABIMisbehaviourMessage {
struct EthABIMisbehaviourProxyMessage {
EthABIPrevState[] prev_states;
bytes context;
bytes client_message;
}
}

impl From<MisbehaviourMessage> for EthABIMisbehaviourMessage {
fn from(msg: MisbehaviourMessage) -> Self {
impl From<MisbehaviourProxyMessage> for EthABIMisbehaviourProxyMessage {
fn from(msg: MisbehaviourProxyMessage) -> Self {
Self {
prev_states: msg
.prev_states
Expand All @@ -67,10 +67,10 @@ impl From<MisbehaviourMessage> for EthABIMisbehaviourMessage {
}
}

impl TryFrom<EthABIMisbehaviourMessage> for MisbehaviourMessage {
impl TryFrom<EthABIMisbehaviourProxyMessage> for MisbehaviourProxyMessage {
type Error = Error;

fn try_from(msg: EthABIMisbehaviourMessage) -> Result<Self, Self::Error> {
fn try_from(msg: EthABIMisbehaviourProxyMessage) -> Result<Self, Self::Error> {
Ok(Self {
prev_states: msg
.prev_states
Expand All @@ -87,12 +87,12 @@ impl TryFrom<EthABIMisbehaviourMessage> for MisbehaviourMessage {
}
}

impl EthABIEncoder for MisbehaviourMessage {
impl EthABIEncoder for MisbehaviourProxyMessage {
fn ethabi_encode(self) -> Vec<u8> {
Into::<EthABIMisbehaviourMessage>::into(self).abi_encode()
Into::<EthABIMisbehaviourProxyMessage>::into(self).abi_encode()
}

fn ethabi_decode(bz: &[u8]) -> Result<Self, Error> {
EthABIMisbehaviourMessage::abi_decode(bz, true)?.try_into()
EthABIMisbehaviourProxyMessage::abi_decode(bz, true)?.try_into()
}
}
Loading

0 comments on commit 6219311

Please sign in to comment.