From 7c20047f62f2896d8bea3822951d6b379e12f77e Mon Sep 17 00:00:00 2001 From: Andreas Fackler Date: Wed, 13 Nov 2024 16:31:36 +0100 Subject: [PATCH] Use hex_debug, serde_bytes and skip_if where appropriate. --- linera-base/src/data_types.rs | 8 +++++++ linera-base/src/identifiers.rs | 22 ++++++++++++++----- linera-base/src/ownership.rs | 4 ++++ linera-chain/src/data_types.rs | 12 ++++++++++ linera-chain/src/manager.rs | 22 +++++++++++++++++++ linera-core/src/data_types.rs | 1 + linera-execution/src/lib.rs | 6 +++++ linera-execution/src/runtime.rs | 3 +++ linera-execution/src/system.rs | 8 ++++++- .../src/test_utils/system_execution_state.rs | 4 ++++ linera-execution/src/transaction_tracker.rs | 4 ++++ 11 files changed, 87 insertions(+), 7 deletions(-) diff --git a/linera-base/src/data_types.rs b/linera-base/src/data_types.rs index 7a5bce59f70..704c7c0cac7 100644 --- a/linera-base/src/data_types.rs +++ b/linera-base/src/data_types.rs @@ -675,6 +675,11 @@ impl Amount { pub fn saturating_div(self, other: Amount) -> u128 { self.0.checked_div(other.0).unwrap_or(u128::MAX) } + + /// Returns whether this amount is 0. + pub fn is_zero(&self) -> bool { + *self == Amount::ZERO + } } /// Permissions for applications on a chain. @@ -696,13 +701,16 @@ pub struct ApplicationPermissions { /// If this is `None`, all system operations and application operations are allowed. /// If it is `Some`, only operations from the specified applications are allowed, and /// no system operations. + #[debug(skip_if = Option::is_none)] pub execute_operations: Option>, /// At least one operation or incoming message from each of these applications must occur in /// every block. #[graphql(default)] + #[debug(skip_if = Vec::is_empty)] pub mandatory_applications: Vec, /// These applications are allowed to close the current chain using the system API. #[graphql(default)] + #[debug(skip_if = Vec::is_empty)] pub close_chain: Vec, } diff --git a/linera-base/src/identifiers.rs b/linera-base/src/identifiers.rs index d599518f47d..9f79e7c2cf9 100644 --- a/linera-base/src/identifiers.rs +++ b/linera-base/src/identifiers.rs @@ -4,7 +4,7 @@ //! Core identifiers used by the Linera protocol. use std::{ - fmt::{self, Debug, Display, Formatter}, + fmt::{self, Display, Formatter}, hash::{Hash, Hasher}, marker::PhantomData, str::FromStr, @@ -12,6 +12,7 @@ use std::{ use anyhow::{anyhow, Context}; use async_graphql::SimpleObject; +use custom_debug_derive::Debug; use linera_witty::{WitLoad, WitStore, WitType}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -19,7 +20,7 @@ use crate::{ bcs_scalar, crypto::{BcsHashable, CryptoError, CryptoHash, PublicKey}, data_types::{BlobContent, BlockHeight}, - doc_scalar, + doc_scalar, hex_debug, }; /// The owner of a chain. This is currently the hash of the owner's public key used to @@ -45,6 +46,7 @@ pub struct Account { /// The chain of the account. pub chain_id: ChainId, /// The owner of the account, or `None` for the chain balance. + #[debug(skip_if = Option::is_none)] pub owner: Option, } @@ -381,7 +383,11 @@ pub struct BytecodeId { WitStore, WitType, )] -pub struct ChannelName(#[serde(with = "serde_bytes")] Vec); +pub struct ChannelName( + #[serde(with = "serde_bytes")] + #[debug(with = "hex_debug")] + Vec, +); /// The name of an event stream. #[derive( @@ -398,7 +404,11 @@ pub struct ChannelName(#[serde(with = "serde_bytes")] Vec); WitStore, WitType, )] -pub struct StreamName(#[serde(with = "serde_bytes")] pub Vec); +pub struct StreamName( + #[serde(with = "serde_bytes")] + #[debug(with = "hex_debug")] + pub Vec, +); /// An event stream ID. #[derive( @@ -559,7 +569,7 @@ impl Hash } } -impl Debug +impl fmt::Debug for BytecodeId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -731,7 +741,7 @@ impl Hash for ApplicationId { } } -impl Debug for ApplicationId { +impl fmt::Debug for ApplicationId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let ApplicationId { bytecode_id, diff --git a/linera-base/src/ownership.rs b/linera-base/src/ownership.rs index 69a899afbe6..dfe3780188d 100644 --- a/linera-base/src/ownership.rs +++ b/linera-base/src/ownership.rs @@ -6,6 +6,7 @@ use std::{collections::BTreeMap, iter}; +use custom_debug_derive::Debug; use linera_witty::{WitLoad, WitStore, WitType}; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -21,6 +22,7 @@ use crate::{ #[derive(PartialEq, Eq, Clone, Hash, Debug, Serialize, Deserialize, WitLoad, WitStore, WitType)] pub struct TimeoutConfig { /// The duration of the fast round. + #[debug(skip_if = Option::is_none)] pub fast_round_duration: Option, /// The duration of the first single-leader and all multi-leader rounds. pub base_timeout: TimeDelta, @@ -48,8 +50,10 @@ impl Default for TimeoutConfig { )] pub struct ChainOwnership { /// Super owners can propose fast blocks in the first round, and regular blocks in any round. + #[debug(skip_if = BTreeMap::is_empty)] pub super_owners: BTreeMap, /// The regular owners, with their weights that determine how often they are round leader. + #[debug(skip_if = BTreeMap::is_empty)] pub owners: BTreeMap, /// The number of initial rounds after 0 in which all owners are allowed to propose blocks. pub multi_leader_rounds: u32, diff --git a/linera-chain/src/data_types.rs b/linera-chain/src/data_types.rs index 32d296b8f9b..93dbeb6362b 100644 --- a/linera-chain/src/data_types.rs +++ b/linera-chain/src/data_types.rs @@ -47,8 +47,10 @@ pub struct Block { pub epoch: Epoch, /// A selection of incoming messages to be executed first. Successive messages of same /// sender and height are grouped together for conciseness. + #[debug(skip_if = Vec::is_empty)] pub incoming_bundles: Vec, /// The operations to execute. + #[debug(skip_if = Vec::is_empty)] pub operations: Vec, /// The block height. pub height: BlockHeight, @@ -59,6 +61,7 @@ pub struct Block { /// fees. If set, this must be the `owner` in the block proposal. `None` means that /// the default account of the chain is used. This value is also used as recipient of /// potential refunds for the message grants created by the operations. + #[debug(skip_if = Option::is_none)] pub authenticated_signer: Option, /// Certified hash (see `Certificate` below) of the previous block in the /// chain, if any. @@ -285,7 +288,9 @@ pub struct BlockProposal { pub content: ProposalContent, pub owner: Owner, pub signature: Signature, + #[debug(skip_if = Vec::is_empty)] pub blobs: Vec, + #[debug(skip_if = Option::is_none)] pub validated_block_certificate: Option>, } @@ -295,10 +300,13 @@ pub struct OutgoingMessage { /// The destination of the message. pub destination: Destination, /// The user authentication carried by the message, if any. + #[debug(skip_if = Option::is_none)] pub authenticated_signer: Option, /// A grant to pay for the message execution. + #[debug(skip_if = Amount::is_zero)] pub grant: Amount, /// Where to send a refund for the unused part of the grant after execution, if any. + #[debug(skip_if = Option::is_none)] pub refund_grant_to: Option, /// The kind of message being sent. pub kind: MessageKind, @@ -310,10 +318,13 @@ pub struct OutgoingMessage { #[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, SimpleObject)] pub struct PostedMessage { /// The user authentication carried by the message, if any. + #[debug(skip_if = Option::is_none)] pub authenticated_signer: Option, /// A grant to pay for the message execution. + #[debug(skip_if = Amount::is_zero)] pub grant: Amount, /// Where to send a refund for the unused part of the grant after execution, if any. + #[debug(skip_if = Option::is_none)] pub refund_grant_to: Option, /// The kind of message being sent. pub kind: MessageKind, @@ -1016,6 +1027,7 @@ pub struct ProposalContent { pub round: Round, /// If this is a retry from an earlier round, the oracle responses from when the block was /// first validated. These are reused so the execution outcome remains the same. + #[debug(skip_if = Option::is_none)] pub forced_oracle_responses: Option>>, } diff --git a/linera-chain/src/manager.rs b/linera-chain/src/manager.rs index 2d5bbe677d3..d9492d235a8 100644 --- a/linera-chain/src/manager.rs +++ b/linera-chain/src/manager.rs @@ -70,6 +70,7 @@ use std::collections::BTreeMap; +use custom_debug_derive::Debug; use linera_base::{ crypto::{KeyPair, PublicKey}, data_types::{ArithmeticError, Blob, BlockHeight, Round, Timestamp}, @@ -106,24 +107,33 @@ pub struct ChainManager { /// The seed for the pseudo-random number generator that determines the round leaders. pub seed: u64, /// The probability distribution for choosing a round leader. + #[debug(skip_if = Option::is_none)] pub distribution: Option>, /// The probability distribution for choosing a fallback round leader. + #[debug(skip_if = Option::is_none)] pub fallback_distribution: Option>, /// Highest-round authenticated block that we have received and checked. If there are multiple /// proposals in the same round, this contains only the first one. + #[debug(skip_if = Option::is_none)] pub proposed: Option, /// Latest validated proposal that we have voted to confirm (or would have, if we are not a /// validator). + #[debug(skip_if = Option::is_none)] pub locked: Option, /// Latest leader timeout certificate we have received. + #[debug(skip_if = Option::is_none)] pub timeout: Option, /// Latest vote we have cast, to validate or confirm. + #[debug(skip_if = Option::is_none)] pub pending: Option, /// Latest timeout vote we cast. + #[debug(skip_if = Option::is_none)] pub timeout_vote: Option, /// Fallback vote we cast. + #[debug(skip_if = Option::is_none)] pub fallback_vote: Option, /// The time after which we are ready to sign a timeout certificate for the current round. + #[debug(skip_if = Option::is_none)] pub round_timeout: Option, /// The lowest round where we can still vote to validate or confirm a block. This is /// the round to which the timeout applies. @@ -133,8 +143,10 @@ pub struct ChainManager { /// round to become current, unless a higher one already is. pub current_round: Round, /// The owners that take over in fallback mode. + #[debug(skip_if = BTreeMap::is_empty)] pub fallback_owners: BTreeMap, /// These are blobs belonging to proposed or validated blocks that have not been confirmed yet. + #[debug(skip_if = BTreeMap::is_empty)] pub pending_blobs: BTreeMap, } @@ -558,28 +570,38 @@ pub struct ChainManagerInfo { /// The configuration of the chain's owners. pub ownership: ChainOwnership, /// Latest authenticated block that we have received, if requested. + #[debug(skip_if = Option::is_none)] pub requested_proposed: Option>, /// Latest validated proposal that we have voted to confirm (or would have, if we are not a /// validator). + #[debug(skip_if = Option::is_none)] pub requested_locked: Option>, /// Latest timeout certificate we have seen. + #[debug(skip_if = Option::is_none)] pub timeout: Option>, /// Latest vote we cast (either to validate or to confirm a block). + #[debug(skip_if = Option::is_none)] pub pending: Option, /// Latest timeout vote we cast. + #[debug(skip_if = Option::is_none)] pub timeout_vote: Option, /// Fallback vote we cast. + #[debug(skip_if = Option::is_none)] pub fallback_vote: Option, /// The value we voted for, if requested. + #[debug(skip_if = Option::is_none)] pub requested_pending_value: Option>, /// The current round, i.e. the lowest round where we can still vote to validate a block. pub current_round: Round, /// The current leader, who is allowed to propose the next block. /// `None` if everyone is allowed to propose. + #[debug(skip_if = Option::is_none)] pub leader: Option, /// The timestamp when the current round times out. + #[debug(skip_if = Option::is_none)] pub round_timeout: Option, /// These are blobs belonging to proposed or validated blocks that have not been confirmed yet. + #[debug(skip_if = BTreeMap::is_empty)] pub pending_blobs: BTreeMap, } diff --git a/linera-core/src/data_types.rs b/linera-core/src/data_types.rs index aa30cdb25f7..8fc7b6ca767 100644 --- a/linera-core/src/data_types.rs +++ b/linera-core/src/data_types.rs @@ -32,6 +32,7 @@ pub struct BlockHeightRange { /// Starting point pub start: BlockHeight, /// Optional limit on the number of elements. + #[debug(skip_if = Option::is_none)] pub limit: Option, } diff --git a/linera-execution/src/lib.rs b/linera-execution/src/lib.rs index a1c419c1b0d..c1b3b515555 100644 --- a/linera-execution/src/lib.rs +++ b/linera-execution/src/lib.rs @@ -356,13 +356,16 @@ pub struct OperationContext { /// The current chain ID. pub chain_id: ChainId, /// The authenticated signer of the operation, if any. + #[debug(skip_if = Option::is_none)] pub authenticated_signer: Option, /// `None` if this is the transaction entrypoint or the caller doesn't want this particular /// call to be authenticated (e.g. for safety reasons). + #[debug(skip_if = Option::is_none)] pub authenticated_caller_id: Option, /// The current block height. pub height: BlockHeight, /// The current index of the operation. + #[debug(skip_if = Option::is_none)] pub index: Option, } @@ -373,8 +376,10 @@ pub struct MessageContext { /// Whether the message was rejected by the original receiver and is now bouncing back. pub is_bouncing: bool, /// The authenticated signer of the operation that created the message, if any. + #[debug(skip_if = Option::is_none)] pub authenticated_signer: Option, /// Where to send a refund for the unused part of each grant after execution, if any. + #[debug(skip_if = Option::is_none)] pub refund_grant_to: Option, /// The current block height. pub height: BlockHeight, @@ -390,6 +395,7 @@ pub struct FinalizeContext { /// The current chain ID. pub chain_id: ChainId, /// The authenticated signer of the operation, if any. + #[debug(skip_if = Option::is_none)] pub authenticated_signer: Option, /// The current block height. pub height: BlockHeight, diff --git a/linera-execution/src/runtime.rs b/linera-execution/src/runtime.rs index ace33f9ce1c..132a4c913be 100644 --- a/linera-execution/src/runtime.rs +++ b/linera-execution/src/runtime.rs @@ -67,8 +67,10 @@ pub struct SyncRuntimeInternal { /// The current local time. local_time: Timestamp, /// The authenticated signer of the operation or message, if any. + #[debug(skip_if = Option::is_none)] authenticated_signer: Option, /// The current message being executed, if there is one. + #[debug(skip_if = Option::is_none)] executing_message: Option, /// How to interact with the storage view of the execution state. @@ -94,6 +96,7 @@ pub struct SyncRuntimeInternal { view_user_states: BTreeMap, /// Where to send a refund for the unused part of the grant after execution, if any. + #[debug(skip_if = Option::is_none)] refund_grant_to: Option, /// Controller to track fuel and storage consumption. resource_controller: ResourceController, diff --git a/linera-execution/src/system.rs b/linera-execution/src/system.rs index 2e1339d4997..2cfeaa30c6d 100644 --- a/linera-execution/src/system.rs +++ b/linera-execution/src/system.rs @@ -110,6 +110,7 @@ pub enum SystemOperation { /// Transfers `amount` units of value from the given owner's account to the recipient. /// If no owner is given, try to take the units out of the unattributed account. Transfer { + #[debug(skip_if = Option::is_none)] owner: Option, recipient: Recipient, amount: Amount, @@ -131,8 +132,10 @@ pub enum SystemOperation { /// Changes the ownership of the chain. ChangeOwnership { /// Super owners can propose fast blocks in the first round, and regular blocks in any round. + #[debug(skip_if = Vec::is_empty)] super_owners: Vec, /// The regular owners, with their weights that determine how often they are round leader. + #[debug(skip_if = Vec::is_empty)] owners: Vec<(PublicKey, u64)>, /// The number of initial rounds after 0 in which all owners are allowed to propose blocks. multi_leader_rounds: u32, @@ -165,8 +168,9 @@ pub enum SystemOperation { #[debug(with = "hex_debug")] parameters: Vec, #[serde(with = "serde_bytes")] - #[debug(with = "hex_debug")] + #[debug(with = "hex_debug", skip_if = Vec::is_empty)] instantiation_argument: Vec, + #[debug(skip_if = Vec::is_empty)] required_application_ids: Vec, }, /// Requests a message from another chain to register a user application on this chain. @@ -197,8 +201,10 @@ pub enum SystemMessage { /// Credits `amount` units of value to the account `target` -- unless the message is /// bouncing, in which case `source` is credited instead. Credit { + #[debug(skip_if = Option::is_none)] target: Option, amount: Amount, + #[debug(skip_if = Option::is_none)] source: Option, }, /// Withdraws `amount` units of value from the account and starts a transfer to credit diff --git a/linera-execution/src/test_utils/system_execution_state.rs b/linera-execution/src/test_utils/system_execution_state.rs index 3bb5c885515..33fe4a3cdf5 100644 --- a/linera-execution/src/test_utils/system_execution_state.rs +++ b/linera-execution/src/test_utils/system_execution_state.rs @@ -3,9 +3,11 @@ use std::{ collections::{BTreeMap, BTreeSet}, + ops::Not, sync::Arc, }; +use custom_debug_derive::Debug; use linera_base::{ crypto::CryptoHash, data_types::{Amount, ApplicationPermissions, Timestamp}, @@ -39,9 +41,11 @@ pub struct SystemExecutionState { pub committees: BTreeMap, pub ownership: ChainOwnership, pub balance: Amount, + #[debug(skip_if = BTreeMap::is_empty)] pub balances: BTreeMap, pub timestamp: Timestamp, pub registry: ApplicationRegistry, + #[debug(skip_if = Not::not)] pub closed: bool, pub application_permissions: ApplicationPermissions, } diff --git a/linera-execution/src/transaction_tracker.rs b/linera-execution/src/transaction_tracker.rs index ad505c277bb..00a15dee229 100644 --- a/linera-execution/src/transaction_tracker.rs +++ b/linera-execution/src/transaction_tracker.rs @@ -3,6 +3,7 @@ use std::vec; +use custom_debug_derive::Debug; use linera_base::{ data_types::{Amount, ArithmeticError, OracleResponse}, ensure, @@ -17,8 +18,11 @@ use crate::{ /// as replayed oracle responses. #[derive(Debug, Default)] pub struct TransactionTracker { + #[debug(skip_if = Option::is_none)] replaying_oracle_responses: Option>, + #[debug(skip_if = Vec::is_empty)] oracle_responses: Vec, + #[debug(skip_if = Vec::is_empty)] outcomes: Vec, next_message_index: u32, }