Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Nov 25, 2024
1 parent 28bd6d1 commit ae355e0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ impl Method<Starknet> for ApplicationRequest {
if response.is_empty() {
return Err(eyre::eyre!("No application found"));
}

// Convert bytes to Felts
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
let chunk_array: [u8; 32] = chunk.try_into()
let chunk_array: [u8; 32] = chunk
.try_into()
.map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?;
felts.push(Felt::from_bytes_be(&chunk_array));
}
}

// Skip version felt and decode the application
let application = StarknetApplication::decode(&felts[1..])
.map_err(|e| eyre::eyre!("Failed to decode application: {:?}", e))?;

Ok(application.into())
}
}
4 changes: 2 additions & 2 deletions crates/context/config/src/client/env/config/query/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ impl Method<Starknet> for MembersRequest {
let mut serialized_request = vec![];
req.encode(&mut serialized_request)
.map_err(|e| eyre::eyre!("Failed to encode request: {}", e))?;

let bytes: Vec<u8> = serialized_request
.iter()
.flat_map(|felt| felt.to_bytes_be())
.collect();

Ok(bytes)
}

Expand Down
14 changes: 11 additions & 3 deletions crates/context/config/src/client/env/config/query/privileges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ impl<'a> Method<Starknet> for PrivilegesRequest<'a> {
fn encode(self) -> eyre::Result<Vec<u8>> {
// Split context_id into high/low parts
let context_bytes = self.context_id.as_bytes();
let mid_point = context_bytes.len().checked_div(2).expect("Length should be even");
let mid_point = context_bytes
.len()
.checked_div(2)
.expect("Length should be even");
let (high_bytes, low_bytes) = context_bytes.split_at(mid_point);

// Convert to Felts and then to bytes
Expand All @@ -82,7 +85,10 @@ impl<'a> Method<Starknet> for PrivilegesRequest<'a> {
// Add each identity
for identity in self.identities {
let id_bytes = identity.as_bytes();
let mid_point = id_bytes.len().checked_div(2).expect("Length should be even");
let mid_point = id_bytes
.len()
.checked_div(2)
.expect("Length should be even");
let (id_high, id_low) = id_bytes.split_at(mid_point);

result.extend_from_slice(&Felt::from_bytes_be_slice(id_high).to_bytes_be());
Expand All @@ -101,7 +107,9 @@ impl<'a> Method<Starknet> for PrivilegesRequest<'a> {
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?));
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| {
eyre::eyre!("Failed to convert chunk to array: {}", e)
})?));
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/context/config/src/client/env/config/types/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ impl Encode for EncodableString {
fn encode<W: FeltWriter>(&self, writer: &mut W) -> Result<(), Error> {
const WORD_SIZE: usize = 31;
let bytes = self.0.as_bytes();

// Calculate full words and pending word
let full_words_count = bytes.len() / WORD_SIZE;
let pending_len = bytes.len() % WORD_SIZE;

// Write number of full words
writer.write(Felt::from(full_words_count));

// Write full words (31 chars each)
for i in 0..full_words_count {
let start = i * WORD_SIZE;
Expand All @@ -327,7 +327,7 @@ impl Encode for EncodableString {
.map_err(|e| Error::custom(&format!("Invalid word hex: {}", e)))?;
writer.write(felt);
}

// Write pending word if exists
if pending_len > 0 {
let pending_bytes = &bytes[full_words_count * WORD_SIZE..];
Expand All @@ -338,10 +338,10 @@ impl Encode for EncodableString {
} else {
writer.write(Felt::ZERO);
}

// Write pending word length
writer.write(Felt::from(pending_len));

Ok(())
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/context/config/src/client/env/proxy/query/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ impl Method<Starknet> for ProposalRequest {
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?));
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| {
eyre::eyre!("Failed to convert chunk to array: {}", e)
})?));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ impl Method<Starknet> for ProposalApprovalsRequest {
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?));
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| {
eyre::eyre!("Failed to convert chunk to array: {}", e)
})?));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl Method<Starknet> for ProposalApproversRequest {
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?));
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| {
eyre::eyre!("Failed to convert chunk to array: {}", e)
})?));
}
}

Expand Down
9 changes: 5 additions & 4 deletions crates/context/config/src/client/env/proxy/query/proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ impl Method<Starknet> for ProposalsRequest {
let mut felts = Vec::new();
for chunk in response.chunks(32) {
if chunk.len() == 32 {
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| eyre::eyre!("Failed to convert chunk to array: {}", e))?));
felts.push(Felt::from_bytes_be(chunk.try_into().map_err(|e| {
eyre::eyre!("Failed to convert chunk to array: {}", e)
})?));
}
}

// Skip version felt and decode the array
let proposals = StarknetProposals::decode(&felts).map_err(|e| {
eyre::eyre!("Failed to decode proposals: {:?}", e)
})?;
let proposals = StarknetProposals::decode(&felts)
.map_err(|e| eyre::eyre!("Failed to decode proposals: {:?}", e))?;

Ok(proposals.into())
}
Expand Down
8 changes: 4 additions & 4 deletions crates/context/config/src/client/protocol/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ impl Network {
}),
}
}
starknet::core::types::TransactionReceipt::L1Handler(_) |
starknet::core::types::TransactionReceipt::Declare(_) |
starknet::core::types::TransactionReceipt::Deploy(_) |
starknet::core::types::TransactionReceipt::DeployAccount(_) => Ok(vec![0]),
starknet::core::types::TransactionReceipt::L1Handler(_)
| starknet::core::types::TransactionReceipt::Declare(_)
| starknet::core::types::TransactionReceipt::Deploy(_)
| starknet::core::types::TransactionReceipt::DeployAccount(_) => Ok(vec![0]),
}
}
}

0 comments on commit ae355e0

Please sign in to comment.