Skip to content

Commit caf18c6

Browse files
authored
chore: no panics + fix ci pt 2 (#54)
* chore: get rid of panics * format changes * state * aggregation * don't use deprecated type
1 parent 0258163 commit caf18c6

File tree

10 files changed

+77
-56
lines changed

10 files changed

+77
-56
lines changed

.github/workflows/pr.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ jobs:
6262
- name: Setup CI
6363
uses: ./.github/actions/setup
6464

65+
- name: Build RISC-V ELFs
66+
run: |
67+
./x.sh
68+
6569
- name: Run cargo test
6670
uses: actions-rs/cargo@v1
6771
with:
@@ -86,6 +90,10 @@ jobs:
8690
- name: Setup CI
8791
uses: ./.github/actions/setup
8892

93+
- name: Build RISC-V ELFs
94+
run: |
95+
./x.sh
96+
8997
- name: Run cargo fmt
9098
uses: actions-rs/cargo@v1
9199
with:

crates/rpc/src/retry.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ where
5555
// Check for tonic status errors.
5656
if let Some(status) = e.downcast_ref::<tonic::Status>() {
5757
match status.code() {
58-
Code::Unavailable |
59-
Code::DeadlineExceeded |
60-
Code::Internal |
61-
Code::Aborted => {
58+
Code::Unavailable
59+
| Code::DeadlineExceeded
60+
| Code::Internal
61+
| Code::Aborted => {
6262
warn!(
6363
"Network temporarily unavailable when {} due to {}, retrying...",
6464
operation_name,
@@ -83,14 +83,14 @@ where
8383
} else {
8484
// Check for common transport errors.
8585
let error_msg = e.to_string().to_lowercase();
86-
let is_transient = error_msg.contains("tls handshake") ||
87-
error_msg.contains("dns error") ||
88-
error_msg.contains("connection reset") ||
89-
error_msg.contains("broken pipe") ||
90-
error_msg.contains("transport error") ||
91-
error_msg.contains("failed to lookup") ||
92-
error_msg.contains("timeout") ||
93-
error_msg.contains("deadline exceeded");
86+
let is_transient = error_msg.contains("tls handshake")
87+
|| error_msg.contains("dns error")
88+
|| error_msg.contains("connection reset")
89+
|| error_msg.contains("broken pipe")
90+
|| error_msg.contains("transport error")
91+
|| error_msg.contains("failed to lookup")
92+
|| error_msg.contains("timeout")
93+
|| error_msg.contains("deadline exceeded");
9494

9595
if is_transient {
9696
warn!(

crates/vapp/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ pub enum VAppPanic {
133133

134134
#[error("Hashing body failed")]
135135
HashingBodyFailed,
136+
137+
#[error("Failed to parse hash")]
138+
FailedToParseBytes,
139+
140+
#[error("Missing public values hash")]
141+
MissingPublicValuesHash,
136142
}
137143

138144
impl From<VAppRevert> for VAppError {

crates/vapp/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
#![allow(clippy::needless_range_loop)]
1414
#![allow(clippy::cast_lossless)]
1515
#![allow(clippy::bool_to_int_with_if)]
16-
#![allow(clippy::should_panic_without_expect)]
1716
#![allow(clippy::field_reassign_with_default)]
1817
#![allow(clippy::manual_assert)]
1918
#![allow(clippy::unreadable_literal)]
2019
#![allow(clippy::match_wildcard_for_single_variants)]
21-
#![allow(clippy::missing_panics_doc)]
2220
#![allow(clippy::missing_errors_doc)]
2321
#![allow(clippy::explicit_iter_loop)]
2422
#![allow(clippy::struct_excessive_bools)]

crates/vapp/src/signing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! This module contains the functions for signing and verifying messages.
44
5-
use alloy_primitives::{Address, PrimitiveSignature};
5+
use alloy_primitives::{Address, Signature};
66
use eyre::Result;
77
use prost::Message;
88
use sha2::{Digest, Sha256};
@@ -26,7 +26,7 @@ pub fn proto_hash<T: Message>(message: &T, sender: &Address) -> Vec<u8> {
2626

2727
/// Verifies an Ethereum signature using the `personal_sign` format.
2828
pub fn eth_sign_verify(message: &[u8], signature: &[u8]) -> Result<Address> {
29-
let signature = PrimitiveSignature::from_raw(signature)?;
29+
let signature = Signature::from_raw(signature)?;
3030
let address = signature.recover_address_from_msg(message)?;
3131
Ok(address)
3232
}

crates/vapp/src/state.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
307307
let domain = B256::try_from(body.domain.as_slice())
308308
.map_err(|_| VAppPanic::DomainDeserializationFailed)?;
309309
if domain != self.domain {
310-
return Err(
311-
VAppPanic::DomainMismatch { expected: self.domain, actual: domain }.into()
312-
);
310+
return Err(VAppPanic::DomainMismatch {
311+
expected: self.domain,
312+
actual: domain,
313+
}
314+
.into());
313315
}
314316

315317
// Verify the proto signature.
@@ -364,9 +366,11 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
364366
let domain = B256::try_from(body.domain.as_slice())
365367
.map_err(|_| VAppPanic::DomainDeserializationFailed)?;
366368
if domain != self.domain {
367-
return Err(
368-
VAppPanic::DomainMismatch { expected: self.domain, actual: domain }.into()
369-
);
369+
return Err(VAppPanic::DomainMismatch {
370+
expected: self.domain,
371+
actual: domain,
372+
}
373+
.into());
370374
}
371375

372376
// Transfer the amount from the requester to the recipient.
@@ -446,10 +450,10 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
446450

447451
// Validate that the request ID is the same for all proto bodies.
448452
debug!("validate that request ID is the same for all proto bodies");
449-
if request_id.as_slice() != bid.request_id.as_slice() ||
450-
request_id.as_slice() != settle.request_id.as_slice() ||
451-
request_id.as_slice() != execute.request_id.as_slice() ||
452-
request_id.as_slice() != fulfill.request_id.as_slice()
453+
if request_id.as_slice() != bid.request_id.as_slice()
454+
|| request_id.as_slice() != settle.request_id.as_slice()
455+
|| request_id.as_slice() != execute.request_id.as_slice()
456+
|| request_id.as_slice() != fulfill.request_id.as_slice()
453457
{
454458
return Err(VAppPanic::RequestIdMismatch {
455459
request_id: address(&request_id)?,
@@ -476,8 +480,8 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
476480

477481
// Validate that the prover is in the request whitelist, if a whitelist is provided.
478482
debug!("validate prover is in whitelist");
479-
if !request.whitelist.is_empty() &&
480-
!request.whitelist.contains(&prover_address.to_vec())
483+
if !request.whitelist.is_empty()
484+
&& !request.whitelist.contains(&prover_address.to_vec())
481485
{
482486
return Err(VAppPanic::ProverNotInWhitelist { prover: prover_address }.into());
483487
}
@@ -518,7 +522,13 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
518522
debug!("verify proof");
519523
let mode = ProofMode::try_from(request.mode)
520524
.map_err(|_| VAppPanic::UnsupportedProofMode { mode: request.mode })?;
521-
let vk = bytes_to_words_be(&request.vk_hash.clone().try_into().unwrap());
525+
let vk = bytes_to_words_be(
526+
&request
527+
.vk_hash
528+
.clone()
529+
.try_into()
530+
.map_err(|_| VAppPanic::FailedToParseBytes)?,
531+
)?;
522532
match mode {
523533
ProofMode::Compressed => {
524534
let verifier = V::default();
@@ -527,7 +537,12 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
527537
vk,
528538
// TODO(jtguibas): this should be either execute.public_values_hash
529539
// or request.public_values_hash
530-
execute.public_values_hash.clone().unwrap().try_into().unwrap(),
540+
execute
541+
.public_values_hash
542+
.clone()
543+
.ok_or(VAppPanic::MissingPublicValuesHash)?
544+
.try_into()
545+
.map_err(|_| VAppPanic::FailedToParseBytes)?,
531546
)
532547
.map_err(|_| VAppPanic::InvalidProof)?;
533548
}
@@ -589,8 +604,15 @@ impl<A: Storage<Address, Account>, R: Storage<RequestId, bool>> VAppState<A, R>
589604

590605
// Log the clear event.
591606
debug!("log clear event");
592-
let request_id: [u8; 32] =
593-
clear.fulfill.body.as_ref().unwrap().request_id.clone().try_into().unwrap();
607+
let request_id: [u8; 32] = clear
608+
.fulfill
609+
.body
610+
.as_ref()
611+
.ok_or(VAppPanic::MissingProtoBody)?
612+
.request_id
613+
.clone()
614+
.try_into()
615+
.map_err(|_| VAppPanic::FailedToParseBytes)?;
594616
info!(
595617
"STEP {}: CLEAR(request_id={}, requester={}, prover={}, cost={})",
596618
self.tx_id,

crates/vapp/src/utils.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use alloy_primitives::Address;
77
use crate::errors::{VAppError, VAppPanic};
88

99
/// Converts a 32-byte array to a 8-word array in big-endian order.
10-
#[must_use]
11-
pub fn bytes_to_words_be(bytes: &[u8; 32]) -> [u32; 8] {
10+
pub fn bytes_to_words_be(bytes: &[u8; 32]) -> Result<[u32; 8], VAppError> {
1211
let mut words = [0u32; 8];
1312
for i in 0..8 {
14-
let chunk: [u8; 4] = bytes[i * 4..(i + 1) * 4].try_into().unwrap();
13+
let chunk: [u8; 4] =
14+
bytes[i * 4..(i + 1) * 4].try_into().map_err(|_| VAppPanic::FailedToParseBytes)?;
1515
words[i] = u32::from_be_bytes(chunk);
1616
}
17-
words
17+
Ok(words)
1818
}
1919

2020
/// Converts a byte array to an address.
@@ -24,6 +24,7 @@ pub fn address(bytes: &[u8]) -> Result<Address, VAppError> {
2424

2525
/// Test utilities for creating and verifying signatures.
2626
#[cfg(test)]
27+
#[allow(clippy::missing_panics_doc)]
2728
pub mod tests {
2829
/// Signing utilities for creating and verifying signatures.
2930
///
@@ -32,7 +33,7 @@ pub mod tests {
3233
#[cfg(any(test, feature = "network"))]
3334
pub mod signers {
3435
use alloy::signers::{local::PrivateKeySigner, SignerSync};
35-
use alloy_primitives::{keccak256, PrimitiveSignature, U256};
36+
use alloy_primitives::{keccak256, Signature, U256};
3637
use prost::Message;
3738
use spn_network_types::{
3839
BidRequest, BidRequestBody, ExecuteProofRequest, ExecuteProofRequestBody,
@@ -59,7 +60,7 @@ pub mod tests {
5960
pub fn proto_sign<T: Message>(
6061
signer: &PrivateKeySigner,
6162
message: &T,
62-
) -> PrimitiveSignature {
63+
) -> Signature {
6364
let mut buf = Vec::new();
6465
message.encode(&mut buf).unwrap();
6566
signer.sign_message_sync(&buf).unwrap()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub const STF_VKEY: [u32; 8] = [1792726344, 421991264, 1066483455, 1706997859, 1529590964, 965212562, 1130506634, 1658718291];
1+
pub const STF_VKEY: [u32; 8] = [1585405315, 815119958, 945268820, 963338962, 1420702568, 1573962290, 723711314, 266230300];

programs/vapp/aggregation/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub fn main() {
2727
// Decode all StepPublicValues and validate state transitions.
2828
let mut decoded_steps: Vec<StepPublicValues> = Vec::new();
2929
for public_value in &public_values {
30-
let step = StepPublicValues::abi_decode(public_value, false)
31-
.expect("failed to decode StepPublicValues");
30+
let step =
31+
StepPublicValues::abi_decode(public_value).expect("failed to decode StepPublicValues");
3232
decoded_steps.push(step);
3333
}
3434

x.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,4 @@ cd programs/vapp/aggregation
3434
cargo prove build --elf-name spn-vapp-aggregation $USE_DOCKER --tag v4.0.0 --output-directory ../../../elf
3535
cd ../../..
3636
echo "Done!"
37-
echo ""
38-
39-
# Build the CLI.
40-
cargo build --bin spn-cli --release
41-
42-
# Compute the verification key of the STF program.
43-
echo "Computing verification key of STF program..."
44-
cargo run --bin spn-cli --release -- vkey --elf-path elf/spn-vapp-stf
45-
echo "Done!"
46-
echo ""
47-
48-
# Compute the verification key of the aggregation program.
49-
echo "Computing verification key of aggregation program..."
50-
cargo run --bin spn-cli --release -- vkey --elf-path elf/spn-vapp-aggregation
51-
echo "Done!"
37+
echo ""

0 commit comments

Comments
 (0)