Skip to content

Commit

Permalink
rm init_branch and end_branch from impls
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Sep 24, 2024
1 parent 89a8fad commit 9a226c8
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 109 deletions.
8 changes: 2 additions & 6 deletions cairo-libs/packages/cometbft/src/ibc.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ impl HeightAsProtoMessage of ProtoMessage<Height> {
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 {
Expand All @@ -46,10 +44,8 @@ impl MerkleRootAsProtoMessage of ProtoMessage<MerkleRoot> {
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 {
Expand Down
12 changes: 3 additions & 9 deletions cairo-libs/packages/cometbft/src/ics23.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,13 @@ impl InnerSpecAsProtoMessage of ProtoMessage<InnerSpec> {
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 {
Expand Down Expand Up @@ -164,14 +162,12 @@ impl LeafOpAsProtoMessage of ProtoMessage<LeafOp> {
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 {
Expand Down Expand Up @@ -203,14 +199,12 @@ impl ProofSpecAsProtoMessage of ProtoMessage<ProofSpec> {
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 {
Expand Down
16 changes: 4 additions & 12 deletions cairo-libs/packages/cometbft/src/light_client.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ impl ClientStateAsProtoMessage of ProtoMessage<ClientState> {
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);
Expand All @@ -55,7 +54,6 @@ impl ClientStateAsProtoMessage of ProtoMessage<ClientState> {
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 {
Expand Down Expand Up @@ -85,12 +83,10 @@ impl ConsensusStateAsProtoMessage of ProtoMessage<ConsensusState> {
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 {
Expand Down Expand Up @@ -118,12 +114,10 @@ impl MisbehaviourAsProtoMessage of ProtoMessage<Misbehaviour> {
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 {
Expand Down Expand Up @@ -153,13 +147,11 @@ impl HeaderAsProtoMessage of ProtoMessage<Header> {
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 {
Expand Down
40 changes: 10 additions & 30 deletions cairo-libs/packages/cometbft/src/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ impl ConsensusAsProtoMessage of ProtoMessage<Consensus> {
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 {
Expand All @@ -51,11 +49,9 @@ impl PartSetHeaderAsProtoMessage of ProtoMessage<PartSetHeader> {
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 {
Expand All @@ -81,11 +77,9 @@ impl BlockIdAsProtoMessage of ProtoMessage<BlockId> {
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 {
Expand Down Expand Up @@ -135,8 +129,7 @@ impl HeaderAsProtoMessage of ProtoMessage<Header> {
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);
Expand All @@ -151,7 +144,6 @@ impl HeaderAsProtoMessage of ProtoMessage<Header> {
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 {
Expand Down Expand Up @@ -213,13 +205,11 @@ impl CommitSigAsProtoMessage of ProtoMessage<CommitSig> {
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 {
Expand Down Expand Up @@ -249,13 +239,11 @@ impl CommitAsProtoMessage of ProtoMessage<Commit> {
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 {
Expand All @@ -281,11 +269,9 @@ impl SignedHeaderAsProtoMessage of ProtoMessage<SignedHeader> {
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 {
Expand All @@ -312,11 +298,9 @@ impl PublicKeyAsProtoMessage of ProtoMessage<PublicKey> {
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 {
Expand Down Expand Up @@ -346,13 +330,11 @@ impl ValidatorAsProtoMessage of ProtoMessage<Validator> {
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 {
Expand Down Expand Up @@ -380,12 +362,10 @@ impl ValidatorSetAsProtoMessage of ProtoMessage<ValidatorSet> {
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 {
Expand Down
4 changes: 1 addition & 3 deletions cairo-libs/packages/cometbft/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ impl FractionAsProtoMessage of ProtoMessage<Fraction> {
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 {
Expand Down
21 changes: 8 additions & 13 deletions cairo-libs/packages/protobuf/src/primitives/array.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::super::types::message::DecodeContextTrait;
use protobuf::types::message::{
ProtoMessage, ProtoCodecImpl, EncodeContext, DecodeContext, EncodeContextImpl, DecodeContextImpl
};
Expand All @@ -9,15 +10,11 @@ pub impl ByteArrayAsProtoMessage of ProtoMessage<ByteArray> {
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 {
Expand Down Expand Up @@ -46,26 +43,24 @@ pub impl ArrayAsProtoMessage<T, +ProtoMessage<T>, +Drop<T>, +Default<T>> of Prot
}
}

fn decode_raw(ref self: Array<T>, ref context: DecodeContext, length: usize) {
context.init_branch(length);

fn decode_raw(ref self: Array<T>, ref context: DecodeContext) {
if ProtoMessage::<T>::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::<T>::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::<T>::default();
item.decode_raw(ref context, 0);
item.decode_raw(ref context);
self.append(item);
}
}

context.end_branch();
}

fn wire_type() -> WireType {
Expand Down
16 changes: 7 additions & 9 deletions cairo-libs/packages/protobuf/src/primitives/numeric.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -33,9 +32,9 @@ pub impl I32AsProtoMessage of ProtoMessage<i32> {
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)
}

Expand All @@ -50,9 +49,9 @@ pub impl I64AsProtoMessage of ProtoMessage<i64> {
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)
}

Expand All @@ -71,10 +70,9 @@ pub impl BoolAsProtoMessage of ProtoMessage<bool> {
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");
}
Expand Down
Loading

0 comments on commit 9a226c8

Please sign in to comment.