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

Add ChainId to Starknet and Comet client state #173

Merged
merged 18 commits into from
Jan 15, 2025

Conversation

ljoss17
Copy link
Contributor

@ljoss17 ljoss17 commented Jan 8, 2025

Closes: #147

Status

  • Added chain_id field to CometClientState in relayer code
  • Added chain_id field to CometClientState in Cairo code
  • Added chain_id field to StarknetClientState in relayer code

@rnbguy rnbguy force-pushed the luca_joss/add-chain-id-field-to-client-state branch from b8dcf58 to ae71a7b Compare January 10, 2025 14:14
@rnbguy rnbguy force-pushed the luca_joss/add-chain-id-field-to-client-state branch from ae71a7b to 546bd98 Compare January 10, 2025 14:34
@rnbguy rnbguy force-pushed the luca_joss/add-chain-id-field-to-client-state branch from 31af839 to 9904ef4 Compare January 10, 2025 14:45
@@ -190,6 +190,7 @@ pub trait CanUseStarknetLightClientEncoding:
+ CanConvertBothWays<Any, StarknetHeader>
+ CanEncodeAndDecodeMut<ViaProtobuf, Timestamp>
+ CanEncodeAndDecodeMut<ViaProtobuf, CommitmentRoot>
// + CanEncodeAndDecodeMut<ViaProtobuf, ChainId>
Copy link
Member

@rnbguy rnbguy Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncommenting this line fails our build.

Specifically, CanEncodeAndDecodeMut<ViaProtobuf, ChainId> doesn't satisfy.

Comment on lines +185 to +213
pub struct EncodeChainId;

impl<Encoding, Strategy> MutEncoder<Encoding, Strategy, ChainId> for EncodeChainId
where
Encoding: CanEncodeMut<Strategy, String>,
{
fn encode_mut(
encoding: &Encoding,
chain_id: &ChainId,
buffer: &mut Encoding::EncodeBuffer,
) -> Result<(), Encoding::Error> {
let chain_id_str = chain_id.as_str().to_string();
encoding.encode_mut(&chain_id_str, buffer)?;
Ok(())
}
}

impl<Encoding, Strategy> MutDecoder<Encoding, Strategy, ChainId> for EncodeChainId
where
Encoding: CanDecodeMut<Strategy, String> + CanRaiseError<&'static str>,
{
fn decode_mut<'a>(
encoding: &Encoding,
buffer: &mut Encoding::DecodeBuffer<'a>,
) -> Result<ChainId, Encoding::Error> {
let chain_id_str = encoding.decode_mut(buffer)?;
ChainId::new(&chain_id_str).map_err(|_| Encoding::raise_error("invalid chain id"))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct to satisfy CanEncodeAndDecodeMut<ViaProtobuf, ChainId>?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have explained this to @ljoss17. This encoding strategy works for Cairo, but not for protobuf. This is because protobuf requires special field tag to be applied to field values, and "primitive" values like strings can only be field values, meaning they always require a tag.

The solution is to define a EncodeChainIdField<const TAG: u32> encoder, which makes use of EncodeStringField<TAG> to encode the given protobuf field.

Comment on lines 25 to 28
// EncodeField<
// symbol!("chain_id"),
// EncodeLengthDelimitedProtoField<2, UseContext>,
// >,
Copy link
Member

@rnbguy rnbguy Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented out parts in this file are done the same way as EncodeStarknetConsensusState and EncodeStarknetHeader. Not sure why this doesn't work.

@soareschen
Copy link
Collaborator

@ljoss17 will move the EncodeChainIdField encoder to Hermes SDK, and then we will use the construct here once the corresponding PR is merged.

@ljoss17
Copy link
Contributor Author

ljoss17 commented Jan 15, 2025

@ljoss17 will move the EncodeChainIdField encoder to Hermes SDK, and then we will use the construct here once the corresponding PR is merged.

Opened informalsystems/hermes-sdk#525

@ljoss17 ljoss17 marked this pull request as ready for review January 15, 2025 16:22
@@ -61,6 +61,7 @@ where

let new_client_state = ClientStateType {
latest_height: header.height,
chain_id: "dummy".parse()?, // TODO: fill with correct info
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to query the current client state here, and then use the chain ID from the original client state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 279deb4

@soareschen soareschen merged commit 7e16860 into main Jan 15, 2025
6 checks passed
@soareschen soareschen deleted the luca_joss/add-chain-id-field-to-client-state branch January 15, 2025 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Store chain ID in both Starknet and Comet client state
3 participants