Skip to main content

iota_types/
executable_transaction.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    committee::EpochId,
9    crypto::AuthorityStrongQuorumSignInfo,
10    message_envelope::{Envelope, TrustedEnvelope, VerifiedEnvelope},
11    messages_checkpoint::CheckpointSequenceNumber,
12    transaction::{SenderSignedData, TransactionDataAPI},
13};
14
15/// CertificateProof is a proof that a transaction certs existed at a given
16/// epoch and hence can be executed. There are two types of proofs: one that is
17/// proven by inclusion in a checkpoint and one that is proven by quorum
18/// signature.
19#[derive(Clone, Debug, Serialize, Deserialize)]
20pub enum CertificateProof {
21    /// Validity was proven by inclusion in the given checkpoint
22    Checkpoint(EpochId, CheckpointSequenceNumber),
23    /// Validity was proven by transaction certificate signature
24    Certified(AuthorityStrongQuorumSignInfo),
25    /// At least f+1 validators have executed this transaction.
26    /// In practice, we will always get 2f+1 (effects cert), but theoretically
27    /// f+1 is enough to prove that the transaction is valid.
28    QuorumExecuted(EpochId),
29    /// Transaction generated by the system, for example Clock update
30    /// transaction
31    SystemTransaction(EpochId),
32    /// P-COOL transaction ordered by consensus. Bypasses pre-consensus
33    /// certification. Validity proven by consensus ordering post owned-object
34    /// conflict resolution.
35    ConsensusOrdered(EpochId),
36}
37
38impl CertificateProof {
39    pub fn new_from_cert_sig(sig: AuthorityStrongQuorumSignInfo) -> Self {
40        Self::Certified(sig)
41    }
42
43    pub fn new_from_checkpoint(epoch: EpochId, checkpoint: CheckpointSequenceNumber) -> Self {
44        Self::Checkpoint(epoch, checkpoint)
45    }
46
47    pub fn new_system(epoch: EpochId) -> Self {
48        Self::SystemTransaction(epoch)
49    }
50
51    pub fn epoch(&self) -> EpochId {
52        match self {
53            Self::Checkpoint(epoch, _)
54            | Self::QuorumExecuted(epoch)
55            | Self::SystemTransaction(epoch)
56            | Self::ConsensusOrdered(epoch) => *epoch,
57            Self::Certified(sig) => sig.epoch,
58        }
59    }
60}
61
62/// An ExecutableTransaction is a wrapper of a transaction with a
63/// CertificateProof that indicates there existed a valid certificate for this
64/// transaction, and hence it can be executed locally. This is an abstraction
65/// data structure to cover both the case where the transaction is certified or
66/// checkpointed when we schedule it for execution.
67pub type ExecutableTransaction = Envelope<SenderSignedData, CertificateProof>;
68pub type VerifiedExecutableTransaction = VerifiedEnvelope<SenderSignedData, CertificateProof>;
69pub type TrustedExecutableTransaction = TrustedEnvelope<SenderSignedData, CertificateProof>;
70
71impl VerifiedExecutableTransaction {
72    pub fn certificate_sig(&self) -> Option<&AuthorityStrongQuorumSignInfo> {
73        match self.auth_sig() {
74            CertificateProof::Certified(sig) => Some(sig),
75            _ => None,
76        }
77    }
78
79    pub fn gas_budget(&self) -> u64 {
80        self.data().transaction_data().gas_budget()
81    }
82}