fmt and clippy

This commit is contained in:
Sergio Chouhy 2026-03-31 01:39:17 -03:00
parent d8ffa22b81
commit 67baefeaee
12 changed files with 81 additions and 71 deletions

View File

@ -46,7 +46,7 @@ impl BedrockClient {
info!("Creating Bedrock client with node URL {node_url}");
let client = Client::builder()
//Add more fields if needed
.timeout(std::time::Duration::from_secs(60))
.timeout(std::time::Duration::from_mins(1))
.build()
.context("Failed to build HTTP client")?;

View File

@ -5,10 +5,10 @@ use sha2::{Digest as _, Sha256, digest::FixedOutput as _};
use crate::{HashType, transaction::NSSATransaction};
pub use nssa_core::Timestamp;
pub type MantleMsgId = [u8; 32];
pub type BlockHash = HashType;
pub type BlockId = u64;
pub use nssa_core::Timestamp;
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
pub struct BlockMeta {

View File

@ -122,11 +122,10 @@ impl IndexerStore {
let expected_clock_tx = NSSATransaction::clock_invocation(block.header.timestamp);
// Validate block structure: the last transaction must be the sole clock invocation.
let last_tx = block
.body
.transactions
.last()
.ok_or_else(|| anyhow::anyhow!("Block must contain at least one transaction"))?;
let last_tx =
block.body.transactions.last().ok_or_else(|| {
anyhow::anyhow!("Block must contain at least one transaction")
})?;
anyhow::ensure!(
last_tx == &expected_clock_tx,
"Last transaction in block must be the canonical clock invocation"

View File

@ -210,7 +210,7 @@ pub fn sequencer_config(
max_block_size,
mempool_max_size,
block_create_timeout,
retry_pending_blocks_timeout: Duration::from_secs(120),
retry_pending_blocks_timeout: Duration::from_mins(2),
initial_accounts: initial_data.sequencer_initial_accounts(),
initial_commitments: initial_data.sequencer_initial_commitments(),
signing_key: [37; 32],

View File

@ -3,7 +3,6 @@
reason = "We prefer to group methods by functionality rather than by type for encoding"
)]
pub type Timestamp = u64;
pub use circuit_io::{PrivacyPreservingCircuitInput, PrivacyPreservingCircuitOutput};
pub use commitment::{
@ -23,3 +22,5 @@ pub mod program;
#[cfg(feature = "host")]
pub mod error;
pub type Timestamp = u64;

View File

@ -17,7 +17,7 @@ pub use program_methods::PRIVACY_PRESERVING_CIRCUIT_ID;
pub use public_transaction::PublicTransaction;
pub use signature::{PrivateKey, PublicKey, Signature};
pub use state::{
CLOCK_10_PROGRAM_ACCOUNT_ID, CLOCK_50_PROGRAM_ACCOUNT_ID, CLOCK_01_PROGRAM_ACCOUNT_ID, V03State,
CLOCK_01_PROGRAM_ACCOUNT_ID, CLOCK_10_PROGRAM_ACCOUNT_ID, CLOCK_50_PROGRAM_ACCOUNT_ID, V03State,
};
pub mod encoding;

View File

@ -8,7 +8,7 @@ use serde::Serialize;
use crate::{
error::NssaError,
program_methods::{AMM_ELF, AUTHENTICATED_TRANSFER_ELF, PINATA_ELF, TOKEN_ELF, CLOCK_ELF},
program_methods::{AMM_ELF, AUTHENTICATED_TRANSFER_ELF, CLOCK_ELF, PINATA_ELF, TOKEN_ELF},
};
/// Maximum number of cycles for a public execution.

View File

@ -164,7 +164,7 @@ impl V03State {
}
fn insert_clock_accounts(&mut self, genesis_timestamp: nssa_core::Timestamp) {
let mut data = [0u8; 16];
let mut data = [0_u8; 16];
data[8..].copy_from_slice(&genesis_timestamp.to_le_bytes());
let clock_program_id = Program::clock().id();
for account_id in [
@ -391,7 +391,7 @@ pub mod tests {
public_transaction,
signature::PrivateKey,
state::{
CLOCK_10_PROGRAM_ACCOUNT_ID, CLOCK_50_PROGRAM_ACCOUNT_ID, CLOCK_01_PROGRAM_ACCOUNT_ID,
CLOCK_01_PROGRAM_ACCOUNT_ID, CLOCK_10_PROGRAM_ACCOUNT_ID, CLOCK_50_PROGRAM_ACCOUNT_ID,
MAX_NUMBER_CHAINED_CALLS,
},
};
@ -540,10 +540,7 @@ pub mod tests {
account_id,
Account {
program_owner: clock_program.id(),
data: [0u8; 16]
.to_vec()
.try_into()
.unwrap(),
data: [0_u8; 16].to_vec().try_into().unwrap(),
..Account::default()
},
);
@ -721,10 +718,7 @@ pub mod tests {
)
}
fn clock_account_data(
state: &V03State,
account_id: AccountId,
) -> (u64, nssa_core::Timestamp) {
fn clock_account_data(state: &V03State, account_id: AccountId) -> (u64, nssa_core::Timestamp) {
let data = state.get_account_by_id(account_id).data.into_inner();
let block_id = u64::from_le_bytes(data[..8].try_into().unwrap());
let timestamp = u64::from_le_bytes(data[8..].try_into().unwrap());
@ -733,7 +727,7 @@ pub mod tests {
#[test]
fn clock_genesis_state_has_zero_block_id_and_genesis_timestamp() {
let genesis_timestamp = 1_000_000u64;
let genesis_timestamp = 1_000_000_u64;
let state = V03State::new_with_genesis_accounts(&[], &[], genesis_timestamp);
let (block_id, timestamp) = clock_account_data(&state, CLOCK_01_PROGRAM_ACCOUNT_ID);
@ -756,7 +750,7 @@ pub mod tests {
#[test]
fn clock_invocation_stores_timestamp_from_instruction() {
let mut state = V03State::new_with_genesis_accounts(&[], &[], 0);
let block_timestamp = 1_700_000_000_000u64;
let block_timestamp = 1_700_000_000_000_u64;
let tx = clock_transaction(block_timestamp);
state.transition_from_public_transaction(&tx).unwrap();
@ -769,7 +763,7 @@ pub mod tests {
fn clock_invocation_sequence_correctly_increments_block_id() {
let mut state = V03State::new_with_genesis_accounts(&[], &[], 0);
for expected_block_id in 1u64..=5 {
for expected_block_id in 1_u64..=5 {
let tx = clock_transaction(expected_block_id * 1000);
state.transition_from_public_transaction(&tx).unwrap();
@ -781,17 +775,16 @@ pub mod tests {
#[test]
fn clock_10_account_not_updated_when_block_id_not_multiple_of_10() {
let genesis_timestamp = 0u64;
let genesis_timestamp = 0_u64;
let mut state = V03State::new_with_genesis_accounts(&[], &[], genesis_timestamp);
// Run 9 clock ticks (block_ids 1..=9), none of which are multiples of 10.
for tick in 1u64..=9 {
for tick in 1_u64..=9 {
let tx = clock_transaction(tick * 1000);
state.transition_from_public_transaction(&tx).unwrap();
}
let (block_id_10, timestamp_10) =
clock_account_data(&state, CLOCK_10_PROGRAM_ACCOUNT_ID);
let (block_id_10, timestamp_10) = clock_account_data(&state, CLOCK_10_PROGRAM_ACCOUNT_ID);
// The 10-block account should still reflect genesis state.
assert_eq!(block_id_10, 0);
assert_eq!(timestamp_10, genesis_timestamp);
@ -802,14 +795,13 @@ pub mod tests {
let mut state = V03State::new_with_genesis_accounts(&[], &[], 0);
// Run 10 clock ticks so block_id reaches 10.
for tick in 1u64..=10 {
for tick in 1_u64..=10 {
let tx = clock_transaction(tick * 1000);
state.transition_from_public_transaction(&tx).unwrap();
}
let (block_id_1, timestamp_1) = clock_account_data(&state, CLOCK_01_PROGRAM_ACCOUNT_ID);
let (block_id_10, timestamp_10) =
clock_account_data(&state, CLOCK_10_PROGRAM_ACCOUNT_ID);
let (block_id_10, timestamp_10) = clock_account_data(&state, CLOCK_10_PROGRAM_ACCOUNT_ID);
assert_eq!(block_id_1, 10);
assert_eq!(block_id_10, 10);
assert_eq!(timestamp_10, timestamp_1);
@ -820,7 +812,7 @@ pub mod tests {
let mut state = V03State::new_with_genesis_accounts(&[], &[], 0);
// After 49 ticks the 50-block account should be unchanged.
for tick in 1u64..=49 {
for tick in 1_u64..=49 {
let tx = clock_transaction(tick * 1000);
state.transition_from_public_transaction(&tx).unwrap();
}
@ -830,8 +822,7 @@ pub mod tests {
// Tick 50 — now the 50-block account should update.
let tx = clock_transaction(50 * 1000);
state.transition_from_public_transaction(&tx).unwrap();
let (block_id_50, timestamp_50) =
clock_account_data(&state, CLOCK_50_PROGRAM_ACCOUNT_ID);
let (block_id_50, timestamp_50) = clock_account_data(&state, CLOCK_50_PROGRAM_ACCOUNT_ID);
assert_eq!(block_id_50, 50);
assert_eq!(timestamp_50, 50 * 1000);
}
@ -841,7 +832,7 @@ pub mod tests {
let mut state = V03State::new_with_genesis_accounts(&[], &[], 0);
// Advance to block 50 (a multiple of both 10 and 50).
for tick in 1u64..=50 {
for tick in 1_u64..=50 {
let tx = clock_transaction(tick * 1000);
state.transition_from_public_transaction(&tx).unwrap();
}

View File

@ -11,7 +11,7 @@ fn update_if_multiple(
current_block_id: u64,
updated_data: [u8; 16],
) -> (AccountWithMetadata, AccountPostState) {
if current_block_id % divisor == 0 {
if current_block_id.is_multiple_of(divisor) {
let mut post_account = pre.account.clone();
post_account.data = updated_data
.to_vec()
@ -47,7 +47,7 @@ fn main() {
.expect("Next block id should be within u64 boundaries");
let updated_data = {
let mut data = [0u8; 16];
let mut data = [0_u8; 16];
data[..8].copy_from_slice(&current_block_id.to_le_bytes());
data[8..].copy_from_slice(&timestamp.to_le_bytes());
data

View File

@ -210,7 +210,8 @@ impl<BC: BlockSettlementClientTrait, IC: IndexerClientTrait> SequencerCore<BC, I
let clock_accounts_pre = [
(
nssa::CLOCK_01_PROGRAM_ACCOUNT_ID,
self.state.get_account_by_id(nssa::CLOCK_01_PROGRAM_ACCOUNT_ID),
self.state
.get_account_by_id(nssa::CLOCK_01_PROGRAM_ACCOUNT_ID),
),
(
nssa::CLOCK_10_PROGRAM_ACCOUNT_ID,
@ -238,7 +239,9 @@ impl<BC: BlockSettlementClientTrait, IC: IndexerClientTrait> SequencerCore<BC, I
NSSATransaction::ProgramDeployment(_) => false,
};
if touches_system {
warn!("Dropping transaction from mempool: user transactions may not modify the system clock account");
warn!(
"Dropping transaction from mempool: user transactions may not modify the system clock account"
);
continue;
}
@ -287,7 +290,8 @@ impl<BC: BlockSettlementClientTrait, IC: IndexerClientTrait> SequencerCore<BC, I
}
// Append the Block Context Program invocation as the mandatory last transaction.
match self.execute_check_transaction_on_state(NSSATransaction::clock_invocation(curr_time)) {
match self.execute_check_transaction_on_state(NSSATransaction::clock_invocation(curr_time))
{
Ok(clock_nssa_tx) => {
valid_transactions.push(clock_nssa_tx);
}
@ -415,8 +419,7 @@ mod tests {
use base58::ToBase58 as _;
use bedrock_client::BackoffConfig;
use common::{
block::AccountInitialData,
test_utils::sequencer_sign_key_for_testing,
block::AccountInitialData, test_utils::sequencer_sign_key_for_testing,
transaction::NSSATransaction,
};
use logos_blockchain_core::mantle::ops::channel::ChannelId;
@ -454,7 +457,7 @@ mod tests {
node_url: "http://not-used-in-unit-tests".parse().unwrap(),
auth: None,
},
retry_pending_blocks_timeout: Duration::from_secs(60 * 4),
retry_pending_blocks_timeout: Duration::from_mins(4),
indexer_rpc_url: "ws://localhost:8779".parse().unwrap(),
}
}
@ -743,7 +746,13 @@ mod tests {
.unwrap();
// Only one user tx should be included; the clock tx is always appended last.
assert_eq!(block.body.transactions, vec![tx.clone(), NSSATransaction::clock_invocation(block.header.timestamp)]);
assert_eq!(
block.body.transactions,
vec![
tx.clone(),
NSSATransaction::clock_invocation(block.header.timestamp)
]
);
}
#[tokio::test]
@ -769,7 +778,13 @@ mod tests {
.get_block_at_id(sequencer.chain_height)
.unwrap()
.unwrap();
assert_eq!(block.body.transactions, vec![tx.clone(), NSSATransaction::clock_invocation(block.header.timestamp)]);
assert_eq!(
block.body.transactions,
vec![
tx.clone(),
NSSATransaction::clock_invocation(block.header.timestamp)
]
);
// Add same transaction should fail
mempool_handle.push(tx.clone()).await.unwrap();
@ -782,7 +797,10 @@ mod tests {
.unwrap()
.unwrap();
// The replay is rejected, so only the clock tx is in the block.
assert_eq!(block.body.transactions, vec![NSSATransaction::clock_invocation(block.header.timestamp)]);
assert_eq!(
block.body.transactions,
vec![NSSATransaction::clock_invocation(block.header.timestamp)]
);
}
#[tokio::test]
@ -817,7 +835,13 @@ mod tests {
.get_block_at_id(sequencer.chain_height)
.unwrap()
.unwrap();
assert_eq!(block.body.transactions, vec![tx.clone(), NSSATransaction::clock_invocation(block.header.timestamp)]);
assert_eq!(
block.body.transactions,
vec![
tx.clone(),
NSSATransaction::clock_invocation(block.header.timestamp)
]
);
}
// Instantiating a new sequencer from the same config. This should load the existing block
@ -947,7 +971,10 @@ mod tests {
);
assert_eq!(
new_block.body.transactions,
vec![tx, NSSATransaction::clock_invocation(new_block.header.timestamp)],
vec![
tx,
NSSATransaction::clock_invocation(new_block.header.timestamp)
],
"New block should contain the submitted transaction and the clock invocation"
);
}
@ -988,7 +1015,10 @@ mod tests {
.unwrap();
// Both transactions were dropped. Only the system-appended clock tx remains.
assert_eq!(block.body.transactions, vec![NSSATransaction::clock_invocation(block.header.timestamp)]);
assert_eq!(
block.body.transactions,
vec![NSSATransaction::clock_invocation(block.header.timestamp)]
);
}
#[tokio::test]

View File

@ -77,8 +77,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata(
match block_on(pinata.claim(pinata_id, winner_id, solution)) {
Ok(tx_hash) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -184,8 +183,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_already_initializ
) {
Ok((tx_hash, _shared_key)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -270,8 +268,7 @@ pub unsafe extern "C" fn wallet_ffi_claim_pinata_private_owned_not_initialized(
match block_on(pinata.claim_private_owned_account(pinata_id, winner_id, solution)) {
Ok((tx_hash, _shared_key)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;

View File

@ -75,8 +75,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_public(
match block_on(transfer.send_public_transfer(from_id, to_id, amount)) {
Ok(tx_hash) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -165,8 +164,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded(
) {
Ok((tx_hash, _shared_key)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -246,8 +244,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_deshielded(
match block_on(transfer.send_deshielded_transfer(from_id, to_id, amount)) {
Ok((tx_hash, _shared_key)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -335,8 +332,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private(
{
Ok((tx_hash, _shared_key)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -419,8 +415,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_shielded_owned(
match block_on(transfer.send_shielded_transfer(from_id, to_id, amount)) {
Ok((tx_hash, _shared_key)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -503,8 +498,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_private_owned(
match block_on(transfer.send_private_transfer_to_owned_account(from_id, to_id, amount)) {
Ok((tx_hash, _shared_keys)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -575,8 +569,7 @@ pub unsafe extern "C" fn wallet_ffi_register_public_account(
match block_on(transfer.register_account(account_id)) {
Ok(tx_hash) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;
@ -647,8 +640,7 @@ pub unsafe extern "C" fn wallet_ffi_register_private_account(
match block_on(transfer.register_account_private(account_id)) {
Ok((tx_hash, _secret)) => {
let tx_hash = CString::new(tx_hash.to_string())
.map(std::ffi::CString::into_raw)
.unwrap_or(ptr::null_mut());
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
unsafe {
(*out_result).tx_hash = tx_hash;