Skip to content

Commit

Permalink
Use hex_debug, serde_bytes and skip_if where appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
afck committed Nov 13, 2024
1 parent dbe3cad commit 7c20047
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 7 deletions.
8 changes: 8 additions & 0 deletions linera-base/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Vec<ApplicationId>>,
/// 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<ApplicationId>,
/// 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<ApplicationId>,
}

Expand Down
22 changes: 16 additions & 6 deletions linera-base/src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
//! 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,
};

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};

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
Expand All @@ -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<Owner>,
}

Expand Down Expand Up @@ -381,7 +383,11 @@ pub struct BytecodeId<Abi = (), Parameters = (), InstantiationArgument = ()> {
WitStore,
WitType,
)]
pub struct ChannelName(#[serde(with = "serde_bytes")] Vec<u8>);
pub struct ChannelName(
#[serde(with = "serde_bytes")]
#[debug(with = "hex_debug")]
Vec<u8>,
);

/// The name of an event stream.
#[derive(
Expand All @@ -398,7 +404,11 @@ pub struct ChannelName(#[serde(with = "serde_bytes")] Vec<u8>);
WitStore,
WitType,
)]
pub struct StreamName(#[serde(with = "serde_bytes")] pub Vec<u8>);
pub struct StreamName(
#[serde(with = "serde_bytes")]
#[debug(with = "hex_debug")]
pub Vec<u8>,
);

/// An event stream ID.
#[derive(
Expand Down Expand Up @@ -559,7 +569,7 @@ impl<Abi, Parameters, InstantiationArgument> Hash
}
}

impl<Abi, Parameters, InstantiationArgument> Debug
impl<Abi, Parameters, InstantiationArgument> fmt::Debug
for BytecodeId<Abi, Parameters, InstantiationArgument>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -731,7 +741,7 @@ impl<A> Hash for ApplicationId<A> {
}
}

impl<A> Debug for ApplicationId<A> {
impl<A> fmt::Debug for ApplicationId<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ApplicationId {
bytecode_id,
Expand Down
4 changes: 4 additions & 0 deletions linera-base/src/ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TimeDelta>,
/// The duration of the first single-leader and all multi-leader rounds.
pub base_timeout: TimeDelta,
Expand Down Expand Up @@ -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<Owner, PublicKey>,
/// The regular owners, with their weights that determine how often they are round leader.
#[debug(skip_if = BTreeMap::is_empty)]
pub owners: BTreeMap<Owner, (PublicKey, u64)>,
/// The number of initial rounds after 0 in which all owners are allowed to propose blocks.
pub multi_leader_rounds: u32,
Expand Down
12 changes: 12 additions & 0 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IncomingBundle>,
/// The operations to execute.
#[debug(skip_if = Vec::is_empty)]
pub operations: Vec<Operation>,
/// The block height.
pub height: BlockHeight,
Expand All @@ -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<Owner>,
/// Certified hash (see `Certificate` below) of the previous block in the
/// chain, if any.
Expand Down Expand Up @@ -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<Blob>,
#[debug(skip_if = Option::is_none)]
pub validated_block_certificate: Option<LiteCertificate<'static>>,
}

Expand All @@ -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<Owner>,
/// 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<Account>,
/// The kind of message being sent.
pub kind: MessageKind,
Expand All @@ -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<Owner>,
/// 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<Account>,
/// The kind of message being sent.
pub kind: MessageKind,
Expand Down Expand Up @@ -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<Vec<Vec<OracleResponse>>>,
}

Expand Down
22 changes: 22 additions & 0 deletions linera-chain/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<WeightedAliasIndex<u64>>,
/// The probability distribution for choosing a fallback round leader.
#[debug(skip_if = Option::is_none)]
pub fallback_distribution: Option<WeightedAliasIndex<u64>>,
/// 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<BlockProposal>,
/// 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<ValidatedBlockCertificate>,
/// Latest leader timeout certificate we have received.
#[debug(skip_if = Option::is_none)]
pub timeout: Option<TimeoutCertificate>,
/// Latest vote we have cast, to validate or confirm.
#[debug(skip_if = Option::is_none)]
pub pending: Option<Vote>,
/// Latest timeout vote we cast.
#[debug(skip_if = Option::is_none)]
pub timeout_vote: Option<Vote>,
/// Fallback vote we cast.
#[debug(skip_if = Option::is_none)]
pub fallback_vote: Option<Vote>,
/// 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<Timestamp>,
/// The lowest round where we can still vote to validate or confirm a block. This is
/// the round to which the timeout applies.
Expand All @@ -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<Owner, (PublicKey, u64)>,
/// 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<BlobId, Blob>,
}

Expand Down Expand Up @@ -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<Box<BlockProposal>>,
/// 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<Box<ValidatedBlockCertificate>>,
/// Latest timeout certificate we have seen.
#[debug(skip_if = Option::is_none)]
pub timeout: Option<Box<TimeoutCertificate>>,
/// Latest vote we cast (either to validate or to confirm a block).
#[debug(skip_if = Option::is_none)]
pub pending: Option<LiteVote>,
/// Latest timeout vote we cast.
#[debug(skip_if = Option::is_none)]
pub timeout_vote: Option<LiteVote>,
/// Fallback vote we cast.
#[debug(skip_if = Option::is_none)]
pub fallback_vote: Option<LiteVote>,
/// The value we voted for, if requested.
#[debug(skip_if = Option::is_none)]
pub requested_pending_value: Option<Box<HashedCertificateValue>>,
/// 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<Owner>,
/// The timestamp when the current round times out.
#[debug(skip_if = Option::is_none)]
pub round_timeout: Option<Timestamp>,
/// 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<BlobId, Blob>,
}

Expand Down
1 change: 1 addition & 0 deletions linera-core/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
}

Expand Down
6 changes: 6 additions & 0 deletions linera-execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Owner>,
/// `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<UserApplicationId>,
/// The current block height.
pub height: BlockHeight,
/// The current index of the operation.
#[debug(skip_if = Option::is_none)]
pub index: Option<u32>,
}

Expand All @@ -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<Owner>,
/// 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<Account>,
/// The current block height.
pub height: BlockHeight,
Expand All @@ -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<Owner>,
/// The current block height.
pub height: BlockHeight,
Expand Down
3 changes: 3 additions & 0 deletions linera-execution/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ pub struct SyncRuntimeInternal<UserInstance> {
/// 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<Owner>,
/// The current message being executed, if there is one.
#[debug(skip_if = Option::is_none)]
executing_message: Option<ExecutingMessage>,

/// How to interact with the storage view of the execution state.
Expand All @@ -94,6 +96,7 @@ pub struct SyncRuntimeInternal<UserInstance> {
view_user_states: BTreeMap<UserApplicationId, ViewUserState>,

/// 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<Account>,
/// Controller to track fuel and storage consumption.
resource_controller: ResourceController,
Expand Down
8 changes: 7 additions & 1 deletion linera-execution/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Owner>,
recipient: Recipient,
amount: Amount,
Expand All @@ -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<PublicKey>,
/// 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,
Expand Down Expand Up @@ -165,8 +168,9 @@ pub enum SystemOperation {
#[debug(with = "hex_debug")]
parameters: Vec<u8>,
#[serde(with = "serde_bytes")]
#[debug(with = "hex_debug")]
#[debug(with = "hex_debug", skip_if = Vec::is_empty)]
instantiation_argument: Vec<u8>,
#[debug(skip_if = Vec::is_empty)]
required_application_ids: Vec<UserApplicationId>,
},
/// Requests a message from another chain to register a user application on this chain.
Expand Down Expand Up @@ -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<Owner>,
amount: Amount,
#[debug(skip_if = Option::is_none)]
source: Option<Owner>,
},
/// Withdraws `amount` units of value from the account and starts a transfer to credit
Expand Down
Loading

0 comments on commit 7c20047

Please sign in to comment.