diff --git a/.github/workflows/run-tests-on-push-to-main.yml b/.github/workflows/run-tests-on-push-to-main.yml index 3b1fc934..2883852b 100644 --- a/.github/workflows/run-tests-on-push-to-main.yml +++ b/.github/workflows/run-tests-on-push-to-main.yml @@ -27,7 +27,14 @@ jobs: run: | cargo clippy \ --package acropolis_common \ - --package acropolis_codec + --package acropolis_codec \ + --package acropolis_module_assets_state \ + --package acropolis_module_block_unpacker \ + --package acropolis_module_consensus \ + --package acropolis_module_drdd_state \ + --package acropolis_module_snapshot_bootstrapper \ + --package acropolis_module_spdd_state \ + --package acropolis_module_utxo_state - name: Run Build run: cargo build --verbose diff --git a/modules/assets_state/src/asset_registry.rs b/modules/assets_state/src/asset_registry.rs index 67782592..eac9d38e 100644 --- a/modules/assets_state/src/asset_registry.rs +++ b/modules/assets_state/src/asset_registry.rs @@ -26,6 +26,12 @@ pub struct AssetRegistry { id_to_key: Vec, } +impl Default for AssetRegistry { + fn default() -> Self { + Self::new() + } +} + impl AssetRegistry { pub fn new() -> Self { Self { @@ -52,8 +58,8 @@ impl AssetRegistry { pub fn lookup_id(&self, policy: &PolicyId, name: &AssetName) -> Option { let key = AssetKey { - policy: Arc::new(policy.clone()), - name: Arc::new(name.clone()), + policy: Arc::new(*policy), + name: Arc::new(*name), }; self.key_to_id.get(&key).copied() } diff --git a/modules/assets_state/src/assets_state.rs b/modules/assets_state/src/assets_state.rs index d58f85ec..240277cd 100644 --- a/modules/assets_state/src/assets_state.rs +++ b/modules/assets_state/src/assets_state.rs @@ -84,7 +84,7 @@ impl AssetsState { // Always handle the mint deltas (This is how assets get initialized) { let mut reg = registry.lock().await; - state = match state.handle_mint_deltas(&deltas_msg.deltas, &mut *reg) { + state = match state.handle_mint_deltas(&deltas_msg.deltas, &mut reg) { Ok(new_state) => new_state, Err(e) => { error!("Asset deltas handling error: {e:#}"); @@ -97,7 +97,7 @@ impl AssetsState { if storage_config.store_info { let mut reg = registry.lock().await; state = match state - .handle_cip25_metadata(&mut *reg, &deltas_msg.cip25_metadata_updates) + .handle_cip25_metadata(&mut reg, &deltas_msg.cip25_metadata_updates) { Ok(new_state) => new_state, Err(e) => { @@ -125,20 +125,19 @@ impl AssetsState { if storage_config.store_info { let reg = registry.lock().await; - state = - match state.handle_cip68_metadata(&utxo_deltas_msg.deltas, &*reg) { - Ok(new_state) => new_state, - Err(e) => { - error!("CIP-68 metadata handling error: {e:#}"); - state - } - }; + state = match state.handle_cip68_metadata(&utxo_deltas_msg.deltas, ®) + { + Ok(new_state) => new_state, + Err(e) => { + error!("CIP-68 metadata handling error: {e:#}"); + state + } + }; } if storage_config.store_transactions.is_enabled() { let reg = registry.lock().await; - state = match state.handle_transactions(&utxo_deltas_msg.deltas, &*reg) - { + state = match state.handle_transactions(&utxo_deltas_msg.deltas, ®) { Ok(new_state) => new_state, Err(e) => { error!("Transactions handling error: {e:#}"); @@ -161,7 +160,7 @@ impl AssetsState { Self::check_sync(¤t_block, block_info, "address"); let reg = registry.lock().await; - state = match state.handle_address_deltas(&address_deltas_msg.deltas, &*reg) + state = match state.handle_address_deltas(&address_deltas_msg.deltas, ®) { Ok(new_state) => new_state, Err(e) => { @@ -299,7 +298,7 @@ impl AssetsState { } AssetsStateQuery::GetAssetInfo { policy, name } => { let reg = registry.lock().await; - match reg.lookup_id(&policy, &name) { + match reg.lookup_id(policy, name) { Some(asset_id) => match state.get_asset_info(&asset_id, ®) { Ok(Some(info)) => AssetsStateQueryResponse::AssetInfo(info), Ok(None) => AssetsStateQueryResponse::NotFound, @@ -318,7 +317,7 @@ impl AssetsState { } AssetsStateQuery::GetAssetHistory { policy, name } => { let reg = registry.lock().await; - match reg.lookup_id(&policy, &name) { + match reg.lookup_id(policy, name) { Some(asset_id) => match state.get_asset_history(&asset_id) { Ok(Some(history)) => { AssetsStateQueryResponse::AssetHistory(history) @@ -339,7 +338,7 @@ impl AssetsState { } AssetsStateQuery::GetAssetAddresses { policy, name } => { let reg = registry.lock().await; - match reg.lookup_id(&policy, &name) { + match reg.lookup_id(policy, name) { Some(asset_id) => match state.get_asset_addresses(&asset_id) { Ok(Some(addresses)) => { AssetsStateQueryResponse::AssetAddresses(addresses) @@ -360,7 +359,7 @@ impl AssetsState { } AssetsStateQuery::GetAssetTransactions { policy, name } => { let reg = registry.lock().await; - match reg.lookup_id(&policy, &name) { + match reg.lookup_id(policy, name) { Some(asset_id) => match state.get_asset_transactions(&asset_id) { Ok(Some(txs)) => AssetsStateQueryResponse::AssetTransactions(txs), Ok(None) => AssetsStateQueryResponse::NotFound, @@ -379,7 +378,7 @@ impl AssetsState { } AssetsStateQuery::GetPolicyIdAssets { policy } => { let reg = registry.lock().await; - match state.get_policy_assets(&policy, ®) { + match state.get_policy_assets(policy, ®) { Ok(Some(assets)) => AssetsStateQueryResponse::PolicyIdAssets(assets), Ok(None) => AssetsStateQueryResponse::NotFound, Err(e) => AssetsStateQueryResponse::Error(e.to_string()), diff --git a/modules/assets_state/src/state.rs b/modules/assets_state/src/state.rs index 199f2f96..364837b3 100644 --- a/modules/assets_state/src/state.rs +++ b/modules/assets_state/src/state.rs @@ -5,7 +5,7 @@ use acropolis_common::{ math::update_value_with_delta, queries::assets::{AssetHistory, PolicyAssets}, Address, AddressDelta, AssetAddressEntry, AssetInfoRecord, AssetMetadataStandard, - AssetMintRecord, AssetName, Datum, Lovelace, NativeAssetDelta, PolicyAsset, PolicyId, + AssetMintRecord, AssetName, Datum, Lovelace, NativeAssetsDelta, PolicyAsset, PolicyId, ShelleyAddress, TxIdentifier, UTXODelta, }; use anyhow::Result; @@ -27,19 +27,14 @@ pub struct AssetsStorageConfig { pub index_by_policy: bool, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Default, Clone, Copy)] pub enum StoreTransactions { + #[default] None, All, Last(u64), } -impl Default for StoreTransactions { - fn default() -> Self { - StoreTransactions::None - } -} - impl StoreTransactions { pub fn is_enabled(&self) -> bool { !matches!(self, StoreTransactions::None) @@ -123,7 +118,7 @@ impl State { if let Some(key) = registry.lookup(*id) { out.push(PolicyAsset { policy: *key.policy, - name: key.name.as_ref().clone(), + name: *key.name.as_ref(), quantity: *amount, }); } @@ -235,7 +230,7 @@ impl State { let key = registry.lookup(*asset_id)?; Some(PolicyAsset { policy: *policy_id, - name: (*key.name).clone(), + name: *key.name, quantity: *supply, }) }) @@ -273,7 +268,7 @@ impl State { pub fn handle_mint_deltas( &self, - deltas: &[(TxIdentifier, Vec<(PolicyId, Vec)>)], + deltas: &[(TxIdentifier, NativeAssetsDelta)], registry: &mut AssetRegistry, ) -> Result { let mut new_supply = self.supply.clone(); @@ -286,7 +281,7 @@ impl State { for (tx_identifier, tx_deltas) in deltas { for (policy_id, asset_deltas) in tx_deltas { for delta in asset_deltas { - let asset_id = registry.get_or_insert(*policy_id, delta.name.clone()); + let asset_id = registry.get_or_insert(*policy_id, delta.name); if let Some(supply) = new_supply.as_mut() { let delta_amount = delta.amount; @@ -314,7 +309,7 @@ impl State { .entry(asset_id) .and_modify(|rec| rec.mint_or_burn_count += 1) .or_insert(AssetInfoRecord { - initial_mint_tx: tx_identifier.clone(), + initial_mint_tx: *tx_identifier, mint_or_burn_count: 1, onchain_metadata: None, metadata_standard: None, @@ -385,7 +380,7 @@ impl State { if let Some(asset_id) = registry.lookup_id(policy_id, &asset.name) { let entry = txs_map.entry(asset_id).or_default(); - let should_push = entry.back().map_or(true, |last| last != &tx_identifier); + let should_push = entry.back() != Some(&tx_identifier); if should_push { entry.push_back(tx_identifier); diff --git a/modules/consensus/src/consensus.rs b/modules/consensus/src/consensus.rs index 8b5f1445..b2c97c26 100644 --- a/modules/consensus/src/consensus.rs +++ b/modules/consensus/src/consensus.rs @@ -95,7 +95,7 @@ impl Consensus { block_info, CardanoMessage::BlockValidation(status), )) => match status { - ValidationStatus::Go => all_ok && true, + ValidationStatus::Go => all_ok, ValidationStatus::NoGo(err) => { error!( block = block_info.number, diff --git a/modules/spdd_state/src/spdd_state.rs b/modules/spdd_state/src/spdd_state.rs index 197c0234..3c8194ae 100644 --- a/modules/spdd_state/src/spdd_state.rs +++ b/modules/spdd_state/src/spdd_state.rs @@ -142,9 +142,9 @@ impl SPDDState { } }; - return Arc::new(Message::StateQueryResponse(StateQueryResponse::SPDD( + Arc::new(Message::StateQueryResponse(StateQueryResponse::SPDD( response, - ))); + ))) } }); diff --git a/modules/spdd_state/src/state.rs b/modules/spdd_state/src/state.rs index e1bdb17b..178e12b0 100644 --- a/modules/spdd_state/src/state.rs +++ b/modules/spdd_state/src/state.rs @@ -57,7 +57,7 @@ impl State { // we plus 2 to epoch number pub fn get_epoch_total_active_stakes(&self, epoch: u64) -> Option { if epoch <= 2 { - return None; + None } else { self.spdd_history .get_by_index(epoch - 2) diff --git a/modules/utxo_state/src/address_delta_publisher.rs b/modules/utxo_state/src/address_delta_publisher.rs index 29a2b222..b0ae78ce 100644 --- a/modules/utxo_state/src/address_delta_publisher.rs +++ b/modules/utxo_state/src/address_delta_publisher.rs @@ -61,7 +61,7 @@ impl AddressDeltaObserver for AddressDeltaPublisher { Message::Cardano((block.clone(), CardanoMessage::AddressDeltas(message))); self.context .message_bus - .publish(&topic, Arc::new(message_enum)) + .publish(topic, Arc::new(message_enum)) .await .unwrap_or_else(|e| error!("Failed to publish: {e}")); } diff --git a/modules/utxo_state/src/fjall_async_immutable_utxo_store.rs b/modules/utxo_state/src/fjall_async_immutable_utxo_store.rs index 20a6c081..38beadc5 100644 --- a/modules/utxo_state/src/fjall_async_immutable_utxo_store.rs +++ b/modules/utxo_state/src/fjall_async_immutable_utxo_store.rs @@ -59,7 +59,7 @@ impl FjallAsyncImmutableUTXOStore { fn should_flush(&self) -> bool { let count = self.write_counter.fetch_add(1, Ordering::Relaxed) + 1; let threshold = self.flush_every.load(Ordering::Relaxed); - threshold != 0 && count % threshold == 0 + threshold != 0 && count.is_multiple_of(threshold) } } diff --git a/modules/utxo_state/src/fjall_immutable_utxo_store.rs b/modules/utxo_state/src/fjall_immutable_utxo_store.rs index 32f0e2fd..6a5854c2 100644 --- a/modules/utxo_state/src/fjall_immutable_utxo_store.rs +++ b/modules/utxo_state/src/fjall_immutable_utxo_store.rs @@ -57,7 +57,7 @@ impl FjallImmutableUTXOStore { fn should_flush(&self) -> bool { let count = self.write_counter.fetch_add(1, Ordering::Relaxed) + 1; let threshold = self.flush_every.load(Ordering::Relaxed); - threshold != 0 && count % threshold == 0 + threshold != 0 && count.is_multiple_of(threshold) } } diff --git a/modules/utxo_state/src/state.rs b/modules/utxo_state/src/state.rs index 611d43cd..db945cb9 100644 --- a/modules/utxo_state/src/state.rs +++ b/modules/utxo_state/src/state.rs @@ -98,7 +98,7 @@ impl State { pub async fn get_utxos_sum(&self, utxo_identifiers: &Vec) -> Result { let mut balance = Value::new(0, Vec::new()); for identifier in utxo_identifiers { - match self.lookup_utxo(&identifier).await { + match self.lookup_utxo(identifier).await { Ok(Some(utxo)) => balance += &utxo.value, Ok(None) => return Err(anyhow::anyhow!("UTxO {} does not exist", identifier)), Err(e) => { @@ -135,53 +135,49 @@ impl State { /// Observe a block for statistics and handle rollbacks pub async fn observe_block(&mut self, block: &BlockInfo) -> Result<()> { - match block.status { - BlockStatus::RolledBack => { - info!( - slot = block.slot, - number = block.number, - "Rollback received" - ); + if block.status == BlockStatus::RolledBack { + info!( + slot = block.slot, + number = block.number, + "Rollback received" + ); - // Delete all UTXOs created in or after this block - let utxos = self.volatile_created.prune_on_or_after(block.number); - for key in utxos { - if let Some(utxo) = self.volatile_utxos.remove(&key) { - // Tell the observer to debit it - if let Some(observer) = self.address_delta_observer.as_ref() { - observer - .observe_delta(&AddressDelta { - address: utxo.address.clone(), - utxo: key.clone(), - value: -ValueDelta::from(&utxo.value), - }) - .await; - } + // Delete all UTXOs created in or after this block + let utxos = self.volatile_created.prune_on_or_after(block.number); + for key in utxos { + if let Some(utxo) = self.volatile_utxos.remove(&key) { + // Tell the observer to debit it + if let Some(observer) = self.address_delta_observer.as_ref() { + observer + .observe_delta(&AddressDelta { + address: utxo.address.clone(), + utxo: key, + value: -ValueDelta::from(&utxo.value), + }) + .await; } } + } - // Any remaining (which were necessarily created before this block) - // that were spent in or after this block can be reinstated - let utxos = self.volatile_spent.prune_on_or_after(block.number); - for key in utxos { - if let Some(utxo) = self.volatile_utxos.get(&key) { - // Tell the observer to recredit it - if let Some(observer) = self.address_delta_observer.as_ref() { - observer - .observe_delta(&AddressDelta { - address: utxo.address.clone(), - utxo: key.clone(), - value: ValueDelta::from(&utxo.value), - }) - .await; - } + // Any remaining (which were necessarily created before this block) + // that were spent in or after this block can be reinstated + let utxos = self.volatile_spent.prune_on_or_after(block.number); + for key in utxos { + if let Some(utxo) = self.volatile_utxos.get(&key) { + // Tell the observer to recredit it + if let Some(observer) = self.address_delta_observer.as_ref() { + observer + .observe_delta(&AddressDelta { + address: utxo.address.clone(), + utxo: key, + value: ValueDelta::from(&utxo.value), + }) + .await; } } - - // Let the pruner compress the map } - _ => {} + // Let the pruner compress the map } self.last_slot = block.slot; @@ -224,7 +220,7 @@ impl State { if let Some(obs) = &self.address_delta_observer { obs.observe_delta(&AddressDelta { address: utxo.address.clone(), - utxo: key.clone(), + utxo: key, value: -ValueDelta::from(&utxo.value), }) .await; @@ -279,7 +275,7 @@ impl State { BlockStatus::Volatile | BlockStatus::RolledBack => { self.volatile_created.add_utxo(&key); - if self.volatile_utxos.insert(key.clone(), value).is_some() { + if self.volatile_utxos.insert(key, value).is_some() { error!( "Saw UTXO {}:{}:{} before", output.utxo_identifier.block_number(), @@ -289,7 +285,7 @@ impl State { } } BlockStatus::Bootstrap | BlockStatus::Immutable => { - self.immutable_utxos.add_utxo(key.clone(), value).await?; + self.immutable_utxos.add_utxo(key, value).await?; // Note we don't check for duplicates in immutable - store // may double check this anyway } @@ -299,7 +295,7 @@ impl State { if let Some(obs) = &self.address_delta_observer { obs.observe_delta(&AddressDelta { address: output.address.clone(), - utxo: output.utxo_identifier.clone(), + utxo: output.utxo_identifier, value: ValueDelta::from(&output.value), }) .await; @@ -373,11 +369,11 @@ impl State { pub async fn handle(&mut self, block: &BlockInfo, deltas: &UTXODeltasMessage) -> Result<()> { // Start the block for observer if let Some(observer) = self.address_delta_observer.as_mut() { - observer.start_block(&block).await; + observer.start_block(block).await; } // Observe block for stats and rollbacks - self.observe_block(&block).await?; + self.observe_block(block).await?; // Process the deltas for delta in &deltas.deltas { @@ -385,11 +381,11 @@ impl State { match delta { UTXODelta::Input(tx_input) => { - self.observe_input(&tx_input, &block).await?; + self.observe_input(tx_input, block).await?; } UTXODelta::Output(tx_output) => { - self.observe_output(&tx_output, &block).await?; + self.observe_output(tx_output, block).await?; } _ => {} @@ -398,7 +394,7 @@ impl State { // End the block for observer if let Some(observer) = self.address_delta_observer.as_mut() { - observer.finalise_block(&block).await; + observer.finalise_block(block).await; } Ok(()) diff --git a/modules/utxo_state/src/volatile_index.rs b/modules/utxo_state/src/volatile_index.rs index 7479cfd5..ae5f3c5b 100644 --- a/modules/utxo_state/src/volatile_index.rs +++ b/modules/utxo_state/src/volatile_index.rs @@ -23,7 +23,7 @@ impl VolatileIndex { /// Get the number of entries in the index pub fn len(&self) -> usize { - return self.blocks.iter().map(|v| v.len()).sum(); + self.blocks.iter().map(|v| v.len()).sum() } /// Add a new block entry @@ -44,7 +44,7 @@ impl VolatileIndex { /// Add a UTXO to the current last block pub fn add_utxo(&mut self, utxo: &UTxOIdentifier) { if let Some(last) = self.blocks.back_mut() { - last.push(utxo.clone()); + last.push(*utxo); } } @@ -66,7 +66,7 @@ impl VolatileIndex { self.first_block += 1; } - return utxos; + utxos } /// Prune all blocks at or after the given boundary returning a vector of @@ -92,7 +92,7 @@ impl VolatileIndex { last_block -= 1; } - return utxos; + utxos } }