Skip to content

Commit

Permalink
Add types for transactions certified through consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtian committed Sep 27, 2024
1 parent 138aabf commit 7382a74
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
16 changes: 15 additions & 1 deletion crates/sui-types/src/executable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::messages_checkpoint::CheckpointSequenceNumber;
use crate::messages_consensus::{AuthorityIndex, Round, TransactionIndex};
use crate::{committee::EpochId, crypto::AuthorityStrongQuorumSignInfo};

use crate::message_envelope::{Envelope, TrustedEnvelope, VerifiedEnvelope};
Expand All @@ -23,6 +24,9 @@ pub enum CertificateProof {
QuorumExecuted(EpochId),
/// Transaction generated by the system, for example Clock update transaction
SystemTransaction(EpochId),
/// Validity was proven through consensus. Round, authority and transaction index indicate
/// the position of the transaction in the consensus DAG for debugging.
Consensus(EpochId, Round, AuthorityIndex, TransactionIndex),
}

impl CertificateProof {
Expand All @@ -38,11 +42,21 @@ impl CertificateProof {
Self::SystemTransaction(epoch)
}

pub fn new_from_consensus(
epoch: EpochId,
round: Round,
authority: AuthorityIndex,
transaction_index: TransactionIndex,
) -> Self {
Self::Consensus(epoch, round, authority, transaction_index)
}

pub fn epoch(&self) -> EpochId {
match self {
Self::Checkpoint(epoch, _)
| Self::QuorumExecuted(epoch)
| Self::SystemTransaction(epoch) => *epoch,
| Self::SystemTransaction(epoch)
| Self::Consensus(epoch, _, _, _) => *epoch,
Self::Certified(sig) => sig.epoch,
}
}
Expand Down
26 changes: 26 additions & 0 deletions crates/sui-types/src/message_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::crypto::{
use crate::error::SuiResult;
use crate::executable_transaction::CertificateProof;
use crate::messages_checkpoint::CheckpointSequenceNumber;
use crate::messages_consensus::{AuthorityIndex, Round, TransactionIndex};
use crate::transaction::SenderSignedData;
use fastcrypto::traits::KeyPair;
use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -452,6 +453,31 @@ impl<T: Message> VerifiedEnvelope<T, CertificateProof> {
})
}

pub fn new_from_consensus(
transaction: VerifiedEnvelope<T, EmptySignInfo>,
epoch: EpochId,
round: Round,
authority: AuthorityIndex,
transaction_index: TransactionIndex,
) -> Self {
let inner = transaction.into_inner();
let Envelope {
digest,
data,
auth_signature: _,
} = inner;
VerifiedEnvelope::new_unchecked(Envelope {
digest,
data,
auth_signature: CertificateProof::new_from_consensus(
epoch,
round,
authority,
transaction_index,
),
})
}

pub fn epoch(&self) -> EpochId {
self.auth_signature.epoch()
}
Expand Down
25 changes: 18 additions & 7 deletions crates/sui-types/src/messages_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
use crate::base_types::{AuthorityName, ObjectRef, TransactionDigest};
use crate::base_types::{ConciseableName, ObjectID, SequenceNumber};
use crate::digests::ConsensusCommitDigest;
use crate::messages_checkpoint::{
CheckpointSequenceNumber, CheckpointSignatureMessage, CheckpointTimestamp,
};
use crate::messages_checkpoint::{CheckpointSequenceNumber, CheckpointSignatureMessage};
use crate::supported_protocol_versions::{
Chain, SupportedProtocolVersions, SupportedProtocolVersionsWithHashes,
};
Expand All @@ -24,16 +22,29 @@ use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

/// The index of an authority in the consensus committee.
/// The value should be the same in Sui committee.
pub type AuthorityIndex = u32;

/// Consensus round number.
pub type Round = u32;

/// The index of a transaction in a consensus block.
pub type TransactionIndex = u16;

/// Consensus timestamp in ms.
pub type ConsensusTimestampMs = u64;

/// Only commit_timestamp_ms is passed to the move call currently.
/// However we include epoch and round to make sure each ConsensusCommitPrologue has a unique tx digest.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct ConsensusCommitPrologue {
/// Epoch of the commit prologue transaction
pub epoch: u64,
/// Consensus round of the commit
/// Consensus round of the commit. Using u64 for compatibility.
pub round: u64,
/// Unix timestamp from consensus
pub commit_timestamp_ms: CheckpointTimestamp,
pub commit_timestamp_ms: ConsensusTimestampMs,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
Expand All @@ -43,7 +54,7 @@ pub struct ConsensusCommitPrologueV2 {
/// Consensus round of the commit
pub round: u64,
/// Unix timestamp from consensus
pub commit_timestamp_ms: CheckpointTimestamp,
pub commit_timestamp_ms: ConsensusTimestampMs,
/// Digest of consensus output
pub consensus_commit_digest: ConsensusCommitDigest,
}
Expand All @@ -65,7 +76,7 @@ pub struct ConsensusCommitPrologueV3 {
/// are multiple consensus commits per round.
pub sub_dag_index: Option<u64>,
/// Unix timestamp from consensus
pub commit_timestamp_ms: CheckpointTimestamp,
pub commit_timestamp_ms: ConsensusTimestampMs,
/// Digest of consensus output
pub consensus_commit_digest: ConsensusCommitDigest,
/// Stores consensus handler determined shared object version assignments.
Expand Down

0 comments on commit 7382a74

Please sign in to comment.