1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
// This file is part of the HotShot repository.

// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.

//! Abstraction over the contents of a block
//!
//! This module provides the [`Transaction`], [`BlockPayload`], and [`BlockHeader`] traits, which
//! describe the behaviors that a block is expected to have.

use std::{
    error::Error,
    fmt::{Debug, Display},
    future::Future,
    hash::Hash,
    sync::Arc,
};

use async_trait::async_trait;
use committable::{Commitment, Committable};
use jf_vid::{precomputable::Precomputable, VidScheme};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use vbs::version::Version;

use super::signature_key::BuilderSignatureKey;
use crate::{
    data::Leaf2,
    traits::{node_implementation::NodeType, states::InstanceState, ValidatedState},
    utils::BuilderCommitment,
    vid::{vid_scheme, VidCommitment, VidCommon, VidSchemeType},
};

/// Trait for structures that need to be unambiguously encoded as bytes.
pub trait EncodeBytes {
    /// Encode `&self`
    fn encode(&self) -> Arc<[u8]>;
}

/// Abstraction over any type of transaction. Used by [`BlockPayload`].
pub trait Transaction:
    Clone + Serialize + DeserializeOwned + Debug + PartialEq + Eq + Sync + Send + Committable + Hash
{
    /// The function to estimate the transaction size
    /// It takes in the transaction itself and a boolean indicating if the transaction adds a new namespace
    /// Since each new namespace adds overhead
    /// just ignore this parameter by default and use it when needed
    fn minimum_block_size(&self) -> u64;
}

/// Abstraction over the full contents of a block
///
/// This trait encapsulates the behaviors that the transactions of a block must have in order to be
/// used by consensus
///   * Must have a predefined error type ([`BlockPayload::Error`])
///   * Must have a transaction type that can be compared for equality, serialized and serialized,
///     sent between threads, and can have a hash produced of it
///   * Must be hashable
#[async_trait]
pub trait BlockPayload<TYPES: NodeType>:
    Serialize
    + Clone
    + Debug
    + Display
    + Hash
    + PartialEq
    + Eq
    + Send
    + Sync
    + DeserializeOwned
    + EncodeBytes
{
    /// The error type for this type of block
    type Error: Error + Debug + Send + Sync + Serialize + DeserializeOwned;

    /// The type of the instance-level state this state is associated with
    type Instance: InstanceState;
    /// The type of the transitions we are applying
    type Transaction: Transaction + Serialize + DeserializeOwned;
    /// Validated State
    type ValidatedState: ValidatedState<TYPES>;
    /// Data created during block building which feeds into the block header
    type Metadata: Clone
        + Debug
        + DeserializeOwned
        + Eq
        + Hash
        + Send
        + Sync
        + Serialize
        + EncodeBytes;

    /// Build a payload and associated metadata with the transactions.
    /// This function is asynchronous because it may need to request updated state from the peers via GET requests.
    /// # Errors
    /// If the transaction length conversion fails.
    async fn from_transactions(
        transactions: impl IntoIterator<Item = Self::Transaction> + Send,
        validated_state: &Self::ValidatedState,
        instance_state: &Self::Instance,
    ) -> Result<(Self, Self::Metadata), Self::Error>;

    /// Build a payload with the encoded transaction bytes, metadata,
    /// and the associated number of VID storage nodes
    fn from_bytes(encoded_transactions: &[u8], metadata: &Self::Metadata) -> Self;

    /// Build the payload and metadata for genesis/null block.
    fn empty() -> (Self, Self::Metadata);

    /// List of transaction commitments.
    fn transaction_commitments(
        &self,
        metadata: &Self::Metadata,
    ) -> Vec<Commitment<Self::Transaction>> {
        self.transactions(metadata).map(|tx| tx.commit()).collect()
    }

    /// Number of transactions in the block.
    fn num_transactions(&self, metadata: &Self::Metadata) -> usize {
        self.transactions(metadata).count()
    }

    /// Generate commitment that builders use to sign block options.
    fn builder_commitment(&self, metadata: &Self::Metadata) -> BuilderCommitment;

    /// Get the transactions in the payload.
    fn transactions<'a>(
        &'a self,
        metadata: &'a Self::Metadata,
    ) -> impl 'a + Iterator<Item = Self::Transaction>;
}

/// extra functions required on block to be usable by hotshot-testing
pub trait TestableBlock<TYPES: NodeType>: BlockPayload<TYPES> + Debug {
    /// generate a genesis block
    fn genesis() -> Self;

    /// the number of transactions in this block
    fn txn_count(&self) -> u64;
}

/// Compute the VID payload commitment.
/// TODO(Gus) delete this function?
/// # Panics
/// If the VID computation fails.
#[must_use]
#[allow(clippy::panic)]
pub fn vid_commitment(
    encoded_transactions: &[u8],
    num_storage_nodes: usize,
) -> <VidSchemeType as VidScheme>::Commit {
    let encoded_tx_len = encoded_transactions.len();
    vid_scheme(num_storage_nodes).commit_only(encoded_transactions).unwrap_or_else(|err| panic!("VidScheme::commit_only failure:(num_storage_nodes,payload_byte_len)=({num_storage_nodes},{encoded_tx_len}) error: {err}"))
}

/// Compute the VID payload commitment along with precompute data reducing time in VID Disperse
/// # Panics
/// If the VID computation fails.
#[must_use]
#[allow(clippy::panic)]
pub fn precompute_vid_commitment(
    encoded_transactions: &[u8],
    num_storage_nodes: usize,
) -> (
    <VidSchemeType as VidScheme>::Commit,
    <VidSchemeType as Precomputable>::PrecomputeData,
) {
    let encoded_tx_len = encoded_transactions.len();
    vid_scheme(num_storage_nodes).commit_only_precompute(encoded_transactions).unwrap_or_else(|err| panic!("VidScheme::commit_only failure:(num_storage_nodes,payload_byte_len)=({num_storage_nodes},{encoded_tx_len}) error: {err}"))
}

/// The number of storage nodes to use when computing the genesis VID commitment.
///
/// The number of storage nodes for the genesis VID commitment is arbitrary, since we don't actually
/// do dispersal for the genesis block. For simplicity and performance, we use 1.
pub const GENESIS_VID_NUM_STORAGE_NODES: usize = 1;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// Information about builder fee for proposed block
pub struct BuilderFee<TYPES: NodeType> {
    /// Proposed fee amount
    pub fee_amount: u64,
    /// Account authorizing the fee.
    pub fee_account: TYPES::BuilderSignatureKey,
    /// Signature over fee amount by `fee_account`.
    pub fee_signature: <TYPES::BuilderSignatureKey as BuilderSignatureKey>::BuilderSignature,
}

/// Header of a block, which commits to a [`BlockPayload`].
pub trait BlockHeader<TYPES: NodeType>:
    Serialize + Clone + Debug + Hash + PartialEq + Eq + Send + Sync + DeserializeOwned + Committable
{
    /// Error type for this type of block header
    type Error: Error + Debug + Send + Sync;

    /// Build a header with the parent validate state, instance-level state, parent leaf, payload
    /// and builder commitments, and metadata. This is only used in pre-marketplace versions
    #[allow(clippy::too_many_arguments)]
    fn new_legacy(
        parent_state: &TYPES::ValidatedState,
        instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
        parent_leaf: &Leaf2<TYPES>,
        payload_commitment: VidCommitment,
        builder_commitment: BuilderCommitment,
        metadata: <TYPES::BlockPayload as BlockPayload<TYPES>>::Metadata,
        builder_fee: BuilderFee<TYPES>,
        vid_common: VidCommon,
        version: Version,
    ) -> impl Future<Output = Result<Self, Self::Error>> + Send;

    /// Build a header with the parent validate state, instance-level state, parent leaf, payload
    /// and builder commitments, metadata, and auction results. This is only used in post-marketplace
    /// versions
    #[allow(clippy::too_many_arguments)]
    fn new_marketplace(
        parent_state: &TYPES::ValidatedState,
        instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
        parent_leaf: &Leaf2<TYPES>,
        payload_commitment: VidCommitment,
        builder_commitment: BuilderCommitment,
        metadata: <TYPES::BlockPayload as BlockPayload<TYPES>>::Metadata,
        builder_fee: Vec<BuilderFee<TYPES>>,
        view_number: u64,
        vid_common: VidCommon,
        auction_results: Option<TYPES::AuctionResult>,
        version: Version,
    ) -> impl Future<Output = Result<Self, Self::Error>> + Send;

    /// Build the genesis header, payload, and metadata.
    fn genesis(
        instance_state: &<TYPES::ValidatedState as ValidatedState<TYPES>>::Instance,
        payload_commitment: VidCommitment,
        builder_commitment: BuilderCommitment,
        metadata: <TYPES::BlockPayload as BlockPayload<TYPES>>::Metadata,
    ) -> Self;

    /// Get the block number.
    fn block_number(&self) -> u64;

    /// Get the payload commitment.
    fn payload_commitment(&self) -> VidCommitment;

    /// Get the metadata.
    fn metadata(&self) -> &<TYPES::BlockPayload as BlockPayload<TYPES>>::Metadata;

    /// Get the builder commitment
    fn builder_commitment(&self) -> BuilderCommitment;

    /// Get the results of the auction for this Header. Only used in post-marketplace versions
    fn get_auction_results(&self) -> Option<TYPES::AuctionResult>;
}