diff --git a/cairo-libs/packages/cometbft/src/ibc.cairo b/cairo-libs/packages/cometbft/src/ibc.cairo index 32ba12a4..a572c493 100644 --- a/cairo-libs/packages/cometbft/src/ibc.cairo +++ b/cairo-libs/packages/cometbft/src/ibc.cairo @@ -18,11 +18,9 @@ impl HeightAsProtoMessage of ProtoMessage { context.encode_field(2, self.revision_height); } - fn decode_raw(ref self: Height, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Height, ref context: DecodeContext) { context.decode_field(1, ref self.revision_number); context.decode_field(2, ref self.revision_height); - context.end_branch(); } fn wire_type() -> WireType { @@ -46,10 +44,8 @@ impl MerkleRootAsProtoMessage of ProtoMessage { context.encode_field(1, self.hash); } - fn decode_raw(ref self: MerkleRoot, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: MerkleRoot, ref context: DecodeContext) { context.decode_field(1, ref self.hash); - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/cometbft/src/ics23.cairo b/cairo-libs/packages/cometbft/src/ics23.cairo index 8625842e..68c89a73 100644 --- a/cairo-libs/packages/cometbft/src/ics23.cairo +++ b/cairo-libs/packages/cometbft/src/ics23.cairo @@ -124,15 +124,13 @@ impl InnerSpecAsProtoMessage of ProtoMessage { context.encode_field(6, self.hash); } - fn decode_raw(ref self: InnerSpec, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: InnerSpec, ref context: DecodeContext) { context.decode_repeated_field(1, ref self.child_order); context.decode_field(2, ref self.child_size); context.decode_field(3, ref self.min_prefix_length); context.decode_field(4, ref self.max_prefix_length); context.decode_field(5, ref self.empty_child); context.decode_field(6, ref self.hash); - context.end_branch(); } fn wire_type() -> WireType { @@ -164,14 +162,12 @@ impl LeafOpAsProtoMessage of ProtoMessage { context.encode_field(5, self.prefix); } - fn decode_raw(ref self: LeafOp, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: LeafOp, ref context: DecodeContext) { context.decode_field(1, ref self.hash); context.decode_field(2, ref self.prehash_key); context.decode_field(3, ref self.prehash_value); context.decode_field(4, ref self.length); context.decode_field(5, ref self.prefix); - context.end_branch(); } fn wire_type() -> WireType { @@ -203,14 +199,12 @@ impl ProofSpecAsProtoMessage of ProtoMessage { context.encode_field(5, self.prehash_key_before_comparison); } - fn decode_raw(ref self: ProofSpec, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: ProofSpec, ref context: DecodeContext) { context.decode_field(1, ref self.leaf_spec); context.decode_field(2, ref self.inner_spec); context.decode_field(3, ref self.max_depth); context.decode_field(4, ref self.min_depth); context.decode_field(5, ref self.prehash_key_before_comparison); - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/cometbft/src/light_client.cairo b/cairo-libs/packages/cometbft/src/light_client.cairo index e993b0ef..cfa21969 100644 --- a/cairo-libs/packages/cometbft/src/light_client.cairo +++ b/cairo-libs/packages/cometbft/src/light_client.cairo @@ -42,8 +42,7 @@ impl ClientStateAsProtoMessage of ProtoMessage { context.encode_field(11, self.allow_update_after_misbehaviour); } - fn decode_raw(ref self: ClientState, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: ClientState, ref context: DecodeContext) { context.decode_field(1, ref self.chain_id); context.decode_field(2, ref self.trust_level); context.decode_field(3, ref self.trusting_period); @@ -55,7 +54,6 @@ impl ClientStateAsProtoMessage of ProtoMessage { context.decode_repeated_field(9, ref self.upgrade_path); context.decode_field(10, ref self.allow_update_after_expiry); context.decode_field(11, ref self.allow_update_after_misbehaviour); - context.end_branch(); } fn wire_type() -> WireType { @@ -85,12 +83,10 @@ impl ConsensusStateAsProtoMessage of ProtoMessage { context.encode_field(3, self.next_validators_hash); } - fn decode_raw(ref self: ConsensusState, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: ConsensusState, ref context: DecodeContext) { context.decode_field(1, ref self.timestamp); context.decode_field(2, ref self.root); context.decode_field(3, ref self.next_validators_hash); - context.end_branch(); } fn wire_type() -> WireType { @@ -118,12 +114,10 @@ impl MisbehaviourAsProtoMessage of ProtoMessage { context.encode_field(3, self.header_2); } - fn decode_raw(ref self: Misbehaviour, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Misbehaviour, ref context: DecodeContext) { context.decode_field(1, ref self.client_id); context.decode_field(2, ref self.header_1); context.decode_field(3, ref self.header_2); - context.end_branch(); } fn wire_type() -> WireType { @@ -153,13 +147,11 @@ impl HeaderAsProtoMessage of ProtoMessage
{ context.encode_field(4, self.trusted_validator_set); } - fn decode_raw(ref self: Header, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Header, ref context: DecodeContext) { context.decode_field(1, ref self.signed_header); context.decode_field(2, ref self.validator_set); context.decode_field(3, ref self.trusted_height); context.decode_field(4, ref self.trusted_validator_set); - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/cometbft/src/types.cairo b/cairo-libs/packages/cometbft/src/types.cairo index ab12e9aa..7fc480b1 100644 --- a/cairo-libs/packages/cometbft/src/types.cairo +++ b/cairo-libs/packages/cometbft/src/types.cairo @@ -21,11 +21,9 @@ impl ConsensusAsProtoMessage of ProtoMessage { context.encode_field(2, self.app); } - fn decode_raw(ref self: Consensus, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Consensus, ref context: DecodeContext) { context.decode_field(1, ref self.block); context.decode_field(2, ref self.app); - context.end_branch(); } fn wire_type() -> WireType { @@ -51,11 +49,9 @@ impl PartSetHeaderAsProtoMessage of ProtoMessage { context.encode_field(2, self.hash); } - fn decode_raw(ref self: PartSetHeader, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: PartSetHeader, ref context: DecodeContext) { context.decode_field(1, ref self.total); context.decode_field(2, ref self.hash); - context.end_branch(); } fn wire_type() -> WireType { @@ -81,11 +77,9 @@ impl BlockIdAsProtoMessage of ProtoMessage { context.encode_field(2, self.part_set_header); } - fn decode_raw(ref self: BlockId, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: BlockId, ref context: DecodeContext) { context.decode_field(1, ref self.hash); context.decode_field(2, ref self.part_set_header); - context.end_branch(); } fn wire_type() -> WireType { @@ -135,8 +129,7 @@ impl HeaderAsProtoMessage of ProtoMessage
{ context.encode_field(14, self.proposer_address); } - fn decode_raw(ref self: Header, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Header, ref context: DecodeContext) { context.decode_field(1, ref self.version); context.decode_field(2, ref self.chain_id); context.decode_field(3, ref self.height); @@ -151,7 +144,6 @@ impl HeaderAsProtoMessage of ProtoMessage
{ context.decode_field(12, ref self.last_results_hash); context.decode_field(13, ref self.evidence_hash); context.decode_field(14, ref self.proposer_address); - context.end_branch(); } fn wire_type() -> WireType { @@ -213,13 +205,11 @@ impl CommitSigAsProtoMessage of ProtoMessage { context.encode_field(4, self.signature); } - fn decode_raw(ref self: CommitSig, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: CommitSig, ref context: DecodeContext) { context.decode_field(1, ref self.block_id_flag); context.decode_field(2, ref self.validator_address); context.decode_field(3, ref self.timestamp); context.decode_field(4, ref self.signature); - context.end_branch(); } fn wire_type() -> WireType { @@ -249,13 +239,11 @@ impl CommitAsProtoMessage of ProtoMessage { context.encode_repeated_field(4, self.signatures); } - fn decode_raw(ref self: Commit, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Commit, ref context: DecodeContext) { context.decode_field(1, ref self.height); context.decode_field(2, ref self.round); context.decode_field(3, ref self.block_id); context.decode_repeated_field(4, ref self.signatures); - context.end_branch(); } fn wire_type() -> WireType { @@ -281,11 +269,9 @@ impl SignedHeaderAsProtoMessage of ProtoMessage { context.encode_field(2, self.commit); } - fn decode_raw(ref self: SignedHeader, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: SignedHeader, ref context: DecodeContext) { context.decode_field(1, ref self.header); context.decode_field(2, ref self.commit); - context.end_branch(); } fn wire_type() -> WireType { @@ -312,11 +298,9 @@ impl PublicKeyAsProtoMessage of ProtoMessage { context.encode_field(2, self.secp256k1); } - fn decode_raw(ref self: PublicKey, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: PublicKey, ref context: DecodeContext) { context.decode_field(1, ref self.ed25519); context.decode_field(2, ref self.secp256k1); - context.end_branch(); } fn wire_type() -> WireType { @@ -346,13 +330,11 @@ impl ValidatorAsProtoMessage of ProtoMessage { context.encode_field(4, self.proposer_priority); } - fn decode_raw(ref self: Validator, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Validator, ref context: DecodeContext) { context.decode_field(1, ref self.address); context.decode_field(2, ref self.pub_key); context.decode_field(3, ref self.voting_power); context.decode_field(4, ref self.proposer_priority); - context.end_branch(); } fn wire_type() -> WireType { @@ -380,12 +362,10 @@ impl ValidatorSetAsProtoMessage of ProtoMessage { context.encode_field(3, self.total_voting_power); } - fn decode_raw(ref self: ValidatorSet, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: ValidatorSet, ref context: DecodeContext) { context.decode_repeated_field(1, ref self.validators); context.decode_field(2, ref self.proposer); context.decode_field(3, ref self.total_voting_power); - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/cometbft/src/utils.cairo b/cairo-libs/packages/cometbft/src/utils.cairo index 6f499912..e36d440c 100644 --- a/cairo-libs/packages/cometbft/src/utils.cairo +++ b/cairo-libs/packages/cometbft/src/utils.cairo @@ -16,11 +16,9 @@ impl FractionAsProtoMessage of ProtoMessage { context.encode_field(2, self.denominator); } - fn decode_raw(ref self: Fraction, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Fraction, ref context: DecodeContext) { context.decode_field(1, ref self.numerator); context.decode_field(2, ref self.denominator); - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/protobuf/src/primitives/array.cairo b/cairo-libs/packages/protobuf/src/primitives/array.cairo index 25a175c7..8ca63e06 100644 --- a/cairo-libs/packages/protobuf/src/primitives/array.cairo +++ b/cairo-libs/packages/protobuf/src/primitives/array.cairo @@ -1,3 +1,4 @@ +use super::super::types::message::DecodeContextTrait; use protobuf::types::message::{ ProtoMessage, ProtoCodecImpl, EncodeContext, DecodeContext, EncodeContextImpl, DecodeContextImpl }; @@ -9,15 +10,11 @@ pub impl ByteArrayAsProtoMessage of ProtoMessage { context.buffer.append(self); } - fn decode_raw(ref self: ByteArray, ref context: DecodeContext, length: usize) { - context.init_branch(length); - + fn decode_raw(ref self: ByteArray, ref context: DecodeContext) { while context.can_read_branch() { self.append_byte(context.buffer[context.index]); context.index += 1; }; - - context.end_branch(); } fn wire_type() -> WireType { @@ -46,26 +43,24 @@ pub impl ArrayAsProtoMessage, +Drop, +Default> of Prot } } - fn decode_raw(ref self: Array, ref context: DecodeContext, length: usize) { - context.init_branch(length); - + fn decode_raw(ref self: Array, ref context: DecodeContext) { if ProtoMessage::::wire_type() == WireType::LengthDelimited { while context.can_read_branch() { let mut length = 0; - length.decode_raw(ref context, 0); + length.decode_raw(ref context); let mut item = Default::::default(); - item.decode_raw(ref context, length); + context.init_branch(length); + item.decode_raw(ref context); + context.end_branch(); self.append(item); } } else { while context.can_read_branch() { let mut item = Default::::default(); - item.decode_raw(ref context, 0); + item.decode_raw(ref context); self.append(item); } } - - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/protobuf/src/primitives/numeric.cairo b/cairo-libs/packages/protobuf/src/primitives/numeric.cairo index b866879a..1ad6bb90 100644 --- a/cairo-libs/packages/protobuf/src/primitives/numeric.cairo +++ b/cairo-libs/packages/protobuf/src/primitives/numeric.cairo @@ -17,8 +17,7 @@ pub impl UnsignedAsProtoMessage< context.buffer.append(@bytes); } - fn decode_raw(ref self: T, ref context: DecodeContext, length: usize) { - assert(length == 0, 'invalid length for u64'); + fn decode_raw(ref self: T, ref context: DecodeContext) { self = decode_varint_u64(context.buffer, ref context.index).try_into().unwrap() } @@ -33,9 +32,9 @@ pub impl I32AsProtoMessage of ProtoMessage { num.encode_raw(ref context); } - fn decode_raw(ref self: i32, ref context: DecodeContext, length: usize) { + fn decode_raw(ref self: i32, ref context: DecodeContext) { let mut num: u32 = 0; - num.decode_raw(ref context, length); + num.decode_raw(ref context); self = decode_2_complement_32(@num) } @@ -50,9 +49,9 @@ pub impl I64AsProtoMessage of ProtoMessage { num.encode_raw(ref context); } - fn decode_raw(ref self: i64, ref context: DecodeContext, length: usize) { + fn decode_raw(ref self: i64, ref context: DecodeContext) { let mut num: u64 = 0; - num.decode_raw(ref context, length); + num.decode_raw(ref context); self = decode_2_complement_64(@num) } @@ -71,10 +70,9 @@ pub impl BoolAsProtoMessage of ProtoMessage { num.encode_raw(ref context); } - fn decode_raw(ref self: bool, ref context: DecodeContext, length: usize) { - assert(length == 0, 'invalid length for bool'); + fn decode_raw(ref self: bool, ref context: DecodeContext) { let mut num: u64 = 0; - num.decode_raw(ref context, length); + num.decode_raw(ref context); if num != 0 && num != 1 { panic!("invalid boolean value"); } diff --git a/cairo-libs/packages/protobuf/src/tests.cairo b/cairo-libs/packages/protobuf/src/tests.cairo index 551ac158..4bbc3d1c 100644 --- a/cairo-libs/packages/protobuf/src/tests.cairo +++ b/cairo-libs/packages/protobuf/src/tests.cairo @@ -21,11 +21,9 @@ impl ProposerAsProtoMessage of ProtoMessage { context.encode_field(2, self.pub_key); } - fn decode_raw(ref self: Proposer, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Proposer, ref context: DecodeContext) { context.decode_field(1, ref self.address); context.decode_field(2, ref self.pub_key); - context.end_branch(); } fn wire_type() -> WireType { @@ -93,8 +91,7 @@ impl TmHeaderAsProtoMessage of ProtoMessage { context.encode_repeated_field(9, self.proposers); } - fn decode_raw(ref self: TmHeader, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: TmHeader, ref context: DecodeContext) { context.decode_field(1, ref self.height); context.decode_field(2, ref self.active); context.decode_field(3, ref self.chain_id); @@ -104,7 +101,6 @@ impl TmHeaderAsProtoMessage of ProtoMessage { context.decode_field(7, ref self.proposer); context.decode_field(8, ref self.validator_type); context.decode_repeated_field(9, ref self.proposers); - context.end_branch(); } fn wire_type() -> WireType { diff --git a/cairo-libs/packages/protobuf/src/types/message.cairo b/cairo-libs/packages/protobuf/src/types/message.cairo index 23557c62..c5cf0257 100644 --- a/cairo-libs/packages/protobuf/src/types/message.cairo +++ b/cairo-libs/packages/protobuf/src/types/message.cairo @@ -2,7 +2,7 @@ use protobuf::types::tag::{WireType, ProtobufTag, ProtobufTagImpl}; use protobuf::primitives::numeric::UnsignedAsProtoMessage; pub trait ProtoMessage { - fn decode_raw(ref self: T, ref context: DecodeContext, length: usize); + fn decode_raw(ref self: T, ref context: DecodeContext); fn encode_raw(self: @T, ref context: EncodeContext); fn wire_type() -> WireType; } @@ -101,13 +101,15 @@ pub impl DecodeContextImpl of DecodeContextTrait { assert(wire_type == tag.wire_type, 'unexpected wire type'); - let mut length = 0; - if wire_type == WireType::LengthDelimited { - length.decode_raw(ref self, 0); + let mut length = 0; + length.decode_raw(ref self); + self.init_branch(length); + value.decode_raw(ref self); + self.end_branch(); + } else { + value.decode_raw(ref self); } - - value.decode_raw(ref self, length); } else if tag.field_number < field_number { panic!( "unexpected field number order: at expected field {} but got older field {}", @@ -146,14 +148,15 @@ pub impl DecodeContextImpl of DecodeContextTrait { #[generate_trait] pub impl ProtoCodecImpl of ProtoCodecTrait { fn decode, +Drop, +Default>(serialized: @ByteArray) -> T { - let length = if ProtoMessage::::wire_type() == WireType::LengthDelimited { - serialized.len() - } else { - 0 - }; let mut value = Default::::default(); let mut context = DecodeContextImpl::new(serialized); - value.decode_raw(ref context, length); + if ProtoMessage::::wire_type() == WireType::LengthDelimited { + context.init_branch(serialized.len()); + value.decode_raw(ref context); + context.end_branch(); + } else { + value.decode_raw(ref context); + }; value } diff --git a/cairo-libs/packages/protobuf/src/types/wkt.cairo b/cairo-libs/packages/protobuf/src/types/wkt.cairo index f00827a9..85140854 100644 --- a/cairo-libs/packages/protobuf/src/types/wkt.cairo +++ b/cairo-libs/packages/protobuf/src/types/wkt.cairo @@ -18,11 +18,9 @@ impl DurationAsProtoMessage of ProtoMessage { context.encode_field(2, self.nanos); } - fn decode_raw(ref self: Duration, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Duration, ref context: DecodeContext) { context.decode_field(1, ref self.seconds); context.decode_field(2, ref self.nanos); - context.end_branch(); } fn wire_type() -> WireType { @@ -48,11 +46,9 @@ impl TimestampAsProtoMessage of ProtoMessage { context.encode_field(2, self.nanos); } - fn decode_raw(ref self: Timestamp, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Timestamp, ref context: DecodeContext) { context.decode_field(1, ref self.seconds); context.decode_field(2, ref self.nanos); - context.end_branch(); } fn wire_type() -> WireType { @@ -78,11 +74,9 @@ impl AnyAsProtoMessage of ProtoMessage { context.encode_field(2, self.value); } - fn decode_raw(ref self: Any, ref context: DecodeContext, length: usize) { - context.init_branch(length); + fn decode_raw(ref self: Any, ref context: DecodeContext) { context.decode_field(1, ref self.type_url); context.decode_field(2, ref self.value); - context.end_branch(); } fn wire_type() -> WireType {