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}
33
34impl CertificateProof {
35    pub fn new_from_cert_sig(sig: AuthorityStrongQuorumSignInfo) -> Self {
36        Self::Certified(sig)
37    }
38
39    pub fn new_from_checkpoint(epoch: EpochId, checkpoint: CheckpointSequenceNumber) -> Self {
40        Self::Checkpoint(epoch, checkpoint)
41    }
42
43    pub fn new_system(epoch: EpochId) -> Self {
44        Self::SystemTransaction(epoch)
45    }
46
47    pub fn epoch(&self) -> EpochId {
48        match self {
49            Self::Checkpoint(epoch, _)
50            | Self::QuorumExecuted(epoch)
51            | Self::SystemTransaction(epoch) => *epoch,
52            Self::Certified(sig) => sig.epoch,
53        }
54    }
55}
56
57/// An ExecutableTransaction is a wrapper of a transaction with a
58/// CertificateProof that indicates there existed a valid certificate for this
59/// transaction, and hence it can be executed locally. This is an abstraction
60/// data structure to cover both the case where the transaction is certified or
61/// checkpointed when we schedule it for execution.
62pub type ExecutableTransaction = Envelope<SenderSignedData, CertificateProof>;
63pub type VerifiedExecutableTransaction = VerifiedEnvelope<SenderSignedData, CertificateProof>;
64pub type TrustedExecutableTransaction = TrustedEnvelope<SenderSignedData, CertificateProof>;
65
66impl VerifiedExecutableTransaction {
67    pub fn certificate_sig(&self) -> Option<&AuthorityStrongQuorumSignInfo> {
68        match self.auth_sig() {
69            CertificateProof::Certified(sig) => Some(sig),
70            _ => None,
71        }
72    }
73
74    pub fn gas_budget(&self) -> u64 {
75        self.data().transaction_data().gas_budget()
76    }
77}