Skip to content

Commit

Permalink
fix: linter, parse correctly ProposalID
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Nov 22, 2024
1 parent c42d235 commit 6c8d38f
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use starknet_crypto::Felt;
use crate::client::env::Method;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::repr::Repr;
use crate::repr::ReprBytes;
use crate::repr::{Repr, ReprBytes};
use crate::types::{ContextId, ContextIdentity};

#[derive(Copy, Clone, Debug, Serialize)]
Expand Down Expand Up @@ -39,7 +38,7 @@ impl Method<Starknet> for HasMemberRequest {
// Encode context_id (2 felts)
let context_bytes = self.context_id.as_bytes();
let (context_high, context_low) = context_bytes.split_at(context_bytes.len() / 2);

// Convert to Felts and add to result
let context_high_felt = Felt::from_bytes_be_slice(context_high);
let context_low_felt = Felt::from_bytes_be_slice(context_low);
Expand All @@ -49,7 +48,7 @@ impl Method<Starknet> for HasMemberRequest {
// Encode member identity (2 felts)
let identity_bytes = self.identity.as_bytes();
let (identity_high, identity_low) = identity_bytes.split_at(identity_bytes.len() / 2);

// Convert to Felts and add to result
let identity_high_felt = Felt::from_bytes_be_slice(identity_high);
let identity_low_felt = Felt::from_bytes_be_slice(identity_low);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use starknet_crypto::Felt;
use crate::client::env::Method;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::repr::Repr;
use crate::repr::ReprBytes;
use crate::repr::{Repr, ReprBytes};
use crate::types::ContextId;

#[derive(Copy, Clone, Debug, Serialize)]
Expand Down Expand Up @@ -61,7 +60,7 @@ impl Method<Starknet> for ProxyContractRequest {

// Convert the Felt to a hex string representing the contract address
let hex_string = format!("0x{}", hex::encode(&response));

Ok(hex_string)
}
}
3 changes: 1 addition & 2 deletions crates/context/config/src/client/env/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use crate::client::{CallClient, Environment};
mod mutate;
mod query;
mod types;
pub use types::*;

use mutate::ContextProxyMutate;
use query::ContextProxyQuery;
pub use types::*;

#[derive(Copy, Clone, Debug)]
pub enum ContextProxy {}
Expand Down
32 changes: 14 additions & 18 deletions crates/context/config/src/client/env/proxy/mutate.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use ed25519_dalek::{Signer, SigningKey};
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet::signers::SigningKey as StarknetSigningKey;
use starknet::core::codec::Encode;
use crate::client::env::proxy::starknet::StarknetProposalWithApprovals;
use crate::Repr;
use crate::repr::ReprBytes;
use starknet::signers::SigningKey as StarknetSigningKey;
use starknet_crypto::{poseidon_hash_many, Felt};

use super::types::starknet::{StarknetProxyMutateRequest, StarknetSignedRequest};
use crate::client::env::{utils, Method};
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::client::transport::Transport;
use crate::client::{CallClient, ClientError, Operation};
use crate::repr::ReprBytes;
use crate::types::{ProposalId, Signed, SignerId};
use crate::{ProposalWithApprovals, ProxyMutateRequest};
use starknet::core::codec::Decode;

use super::types::starknet::{StarknetProxyMutateRequest, StarknetSignedRequest};
use crate::{ProposalWithApprovals, ProxyMutateRequest, Repr};

pub mod methods;

Expand Down Expand Up @@ -100,24 +96,24 @@ impl Method<Starknet> for Mutate {
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
println!("response: {:?}", response);
if response.is_empty() {
return Ok(None);
}

// Skip first 32 bytes (array length)
let response = &response[32..];

// Get proposal_id from the next 32 bytes (using only low part)
// Get proposal_id from the next 64 bytes (32 for high, 32 for low)
let proposal_id = Repr::new(ProposalId::from_bytes(|bytes| {
bytes.copy_from_slice(&response[..32]);
// Take 16 bytes from high and 16 bytes from low
bytes[..16].copy_from_slice(&response[16..32]); // Last 16 bytes of high
bytes[16..].copy_from_slice(&response[48..64]); // Last 16 bytes of low
Ok(32)
})?);

// Get num_approvals from the last 32 bytes
let num_approvals = u32::from_be_bytes(response[32..][28..32].try_into()?)
as usize;

let num_approvals = u32::from_be_bytes(response[64..][28..32].try_into()?) as usize;

Ok(Some(ProposalWithApprovals {
proposal_id,
num_approvals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Method<Starknet> for ActiveProposalRequest {
}

// Take the last byte which contains our value
let value = response[31] as u16; // Get the last byte (index 31)
let value = response[31] as u16; // Get the last byte (index 31)

Ok(value)
}
Expand Down
56 changes: 27 additions & 29 deletions crates/context/config/src/client/env/proxy/query/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::Serialize;
use starknet::core::codec::Decode;
use starknet_crypto::Felt;

use super::ProposalId;
Expand All @@ -8,7 +9,6 @@ use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::repr::Repr;
use crate::Proposal;
use starknet::core::codec::Decode;

#[derive(Clone, Debug, Serialize)]
pub(super) struct ProposalRequest {
Expand Down Expand Up @@ -37,41 +37,39 @@ impl Method<Starknet> for ProposalRequest {
fn encode(self) -> eyre::Result<Vec<u8>> {
// Convert ProposalId to StarknetProposalId
let starknet_id: StarknetProposalId = self.proposal_id.into();

// Encode both high and low parts
let mut encoded = Vec::new();
encoded.extend_from_slice(&starknet_id.high.to_bytes_be());
encoded.extend_from_slice(&starknet_id.low.to_bytes_be());

Ok(encoded)
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
// First check if we got a None response
if response.is_empty() || response.len() < 32 {
return Ok(None);
}

// Convert bytes to Felts
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().unwrap()));
}
}

// First felt should be 1 for Some, 0 for None
let is_some = felts[0].to_bytes_be()[31] == 1;
if !is_some {
return Ok(None);
}

// Decode the proposal starting from index 1
let proposal = StarknetProposal::decode(&felts[1..])
.map_err(|e| eyre::eyre!("Failed to decode proposal: {:?}", e))?;
println!("Decoded proposal: {:?}", proposal);


Ok(Some(proposal.into()))
// First check if we got a None response
if response.is_empty() || response.len() < 32 {
return Ok(None);
}

// Convert bytes to Felts
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().unwrap()));
}
}

// First felt should be 1 for Some, 0 for None
let is_some = felts[0].to_bytes_be()[31] == 1;
if !is_some {
return Ok(None);
}

// Decode the proposal starting from index 1
let proposal = StarknetProposal::decode(&felts[1..])
.map_err(|e| eyre::eyre!("Failed to decode proposal: {:?}", e))?;

Ok(Some(proposal.into()))
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use serde::Serialize;
use starknet::core::codec::Decode;
use starknet::core::types::Felt;
use crate::client::env::proxy::types::starknet::{StarknetProposalId, StarknetProposalWithApprovals};

use super::ProposalId;
use crate::client::env::proxy::types::starknet::{
StarknetProposalId, StarknetProposalWithApprovals,
};
use crate::client::env::Method;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::repr::Repr;
use crate::ProposalWithApprovals;
use starknet::core::codec::Decode;

#[derive(Clone, Debug, Serialize)]
pub(super) struct ProposalApprovalsRequest {
Expand Down Expand Up @@ -37,12 +39,12 @@ impl Method<Starknet> for ProposalApprovalsRequest {
fn encode(self) -> eyre::Result<Vec<u8>> {
// Convert ProposalId to StarknetProposalId
let starknet_id: StarknetProposalId = self.proposal_id.into();

// Encode both high and low parts
let mut encoded = Vec::new();
encoded.extend_from_slice(&starknet_id.high.to_bytes_be());
encoded.extend_from_slice(&starknet_id.low.to_bytes_be());

Ok(encoded)
}

Expand All @@ -54,12 +56,9 @@ impl Method<Starknet> for ProposalApprovalsRequest {
felts.push(Felt::from_bytes_be(chunk.try_into().unwrap()));
}
}
println!("Felts: {:?}", felts);

// Decode and convert in one go
let approvals = StarknetProposalWithApprovals::decode(&felts)
.map_err(|e| eyre::eyre!("Failed to decode approvals: {:?}", e))?;
println!("Decoded approvals: {:?}", approvals);

Ok(approvals.into())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::mem;

use serde::Serialize;
use starknet::core::codec::Decode;
use starknet::core::types::Felt;

use super::ProposalId;
use crate::client::env::proxy::types::starknet::{StarknetProposalId, StarknetApprovers};
use crate::client::env::proxy::types::starknet::{StarknetApprovers, StarknetProposalId};
use crate::client::env::Method;
use crate::client::protocol::near::Near;
use crate::client::protocol::starknet::Starknet;
use crate::repr::Repr;
use crate::repr::ReprBytes;
use crate::types::ContextIdentity;
use starknet::core::types::Felt;
use starknet::core::codec::Decode;

#[derive(Clone, Debug, Serialize)]
pub(super) struct ProposalApproversRequest {
Expand Down Expand Up @@ -47,14 +48,26 @@ impl Method<Starknet> for ProposalApproversRequest {
type Returns = Vec<ContextIdentity>;

fn encode(self) -> eyre::Result<Vec<u8>> {
// Convert ProposalId to StarknetProposalId
let starknet_id: StarknetProposalId = self.proposal_id.into();
// Get the full 32 bytes
let bytes = self.proposal_id.as_bytes();

// Split into high and low parts (16 bytes each)
let (high_bytes, low_bytes) = bytes.split_at(16);

// Create Felts with proper padding
let mut high = [0u8; 32];
let mut low = [0u8; 32];
high[16..].copy_from_slice(high_bytes); // Put in last 16 bytes
low[16..].copy_from_slice(low_bytes); // Put in last 16 bytes

// Encode both high and low parts
let starknet_id = StarknetProposalId {
high: Felt::from_bytes_be(&high),
low: Felt::from_bytes_be(&low),
};
// Encode exactly as in mutate response
let mut encoded = Vec::new();
encoded.extend_from_slice(&starknet_id.high.to_bytes_be());
encoded.extend_from_slice(&starknet_id.low.to_bytes_be());

Ok(encoded)
}

Expand All @@ -66,19 +79,10 @@ impl Method<Starknet> for ProposalApproversRequest {
felts.push(Felt::from_bytes_be(chunk.try_into().unwrap()));
}
}
println!("Felts for decoding: {:?}", felts);

// First felt should be array length
if !felts.is_empty() {
let array_len = u32::from_be_bytes(felts[0].to_bytes_be()[28..32].try_into().unwrap());
println!("Array length from felt: {}", array_len);
}

// Decode the array of approvers
let approvers = StarknetApprovers::decode(&felts)
.map_err(|e| eyre::eyre!("Failed to decode approvers: {:?}", e))?;
println!("Decoded approvers: {:?}", approvers);


Ok(approvers.into())
}
}
Loading

0 comments on commit 6c8d38f

Please sign in to comment.