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