hotshot_types/traits/
node_implementation.rs1use std::{
13 fmt::{Debug, Display},
14 hash::Hash,
15 ops::{self, Deref, Sub},
16 sync::Arc,
17 time::Duration,
18};
19
20use async_trait::async_trait;
21use committable::Committable;
22use serde::{Deserialize, Serialize};
23use vbs::version::StaticVersionType;
24
25use super::{
26 block_contents::{BlockHeader, TestableBlock, Transaction},
27 network::{
28 AsyncGenerator, ConnectedNetwork, NetworkReliability, TestableNetworkingImplementation,
29 },
30 signature_key::{
31 BuilderSignatureKey, LCV1StateSignatureKey, LCV2StateSignatureKey, LCV3StateSignatureKey,
32 StateSignatureKey,
33 },
34 states::TestableState,
35 storage::Storage,
36 ValidatedState,
37};
38use crate::{
39 constants::DEFAULT_UPGRADE_CONSTANTS,
40 data::{Leaf2, TestableLeaf},
41 traits::{
42 election::Membership, signature_key::SignatureKey, states::InstanceState, BlockPayload,
43 },
44 upgrade_config::UpgradeConstants,
45};
46
47pub trait NodeImplementation<TYPES: NodeType>:
55 Send + Sync + Clone + Eq + Hash + 'static + Serialize + for<'de> Deserialize<'de>
56{
57 type Network: ConnectedNetwork<TYPES::SignatureKey>;
59
60 type Storage: Storage<TYPES>;
62}
63
64#[allow(clippy::type_complexity)]
66#[async_trait]
67pub trait TestableNodeImplementation<TYPES: NodeType>: NodeImplementation<TYPES> {
68 fn state_create_random_transaction(
72 state: Option<&TYPES::ValidatedState>,
73 rng: &mut dyn rand::RngCore,
74 padding: u64,
75 ) -> <TYPES::BlockPayload as BlockPayload<TYPES>>::Transaction;
76
77 fn leaf_create_random_transaction(
81 leaf: &Leaf2<TYPES>,
82 rng: &mut dyn rand::RngCore,
83 padding: u64,
84 ) -> <TYPES::BlockPayload as BlockPayload<TYPES>>::Transaction;
85
86 fn block_genesis() -> TYPES::BlockPayload;
88
89 fn txn_count(block: &TYPES::BlockPayload) -> u64;
91
92 fn gen_networks(
94 expected_node_count: usize,
95 num_bootstrap: usize,
96 da_committee_size: usize,
97 reliability_config: Option<Box<dyn NetworkReliability>>,
98 secondary_network_delay: Duration,
99 ) -> AsyncGenerator<Arc<Self::Network>>;
100}
101
102#[async_trait]
103impl<TYPES: NodeType, I: NodeImplementation<TYPES>> TestableNodeImplementation<TYPES> for I
104where
105 TYPES::ValidatedState: TestableState<TYPES>,
106 TYPES::BlockPayload: TestableBlock<TYPES>,
107 I::Network: TestableNetworkingImplementation<TYPES>,
108{
109 fn state_create_random_transaction(
110 state: Option<&TYPES::ValidatedState>,
111 rng: &mut dyn rand::RngCore,
112 padding: u64,
113 ) -> <TYPES::BlockPayload as BlockPayload<TYPES>>::Transaction {
114 <TYPES::ValidatedState as TestableState<TYPES>>::create_random_transaction(
115 state, rng, padding,
116 )
117 }
118
119 fn leaf_create_random_transaction(
120 leaf: &Leaf2<TYPES>,
121 rng: &mut dyn rand::RngCore,
122 padding: u64,
123 ) -> <TYPES::BlockPayload as BlockPayload<TYPES>>::Transaction {
124 Leaf2::create_random_transaction(leaf, rng, padding)
125 }
126
127 fn block_genesis() -> TYPES::BlockPayload {
128 <TYPES::BlockPayload as TestableBlock<TYPES>>::genesis()
129 }
130
131 fn txn_count(block: &TYPES::BlockPayload) -> u64 {
132 <TYPES::BlockPayload as TestableBlock<TYPES>>::txn_count(block)
133 }
134
135 fn gen_networks(
136 expected_node_count: usize,
137 num_bootstrap: usize,
138 da_committee_size: usize,
139 reliability_config: Option<Box<dyn NetworkReliability>>,
140 secondary_network_delay: Duration,
141 ) -> AsyncGenerator<Arc<Self::Network>> {
142 <I::Network as TestableNetworkingImplementation<TYPES>>::generator(
143 expected_node_count,
144 num_bootstrap,
145 0,
146 da_committee_size,
147 reliability_config.clone(),
148 secondary_network_delay,
149 )
150 }
151}
152
153pub trait ConsensusTime:
155 PartialOrd
156 + Ord
157 + Send
158 + Sync
159 + Debug
160 + Clone
161 + Copy
162 + Hash
163 + Deref<Target = u64>
164 + serde::Serialize
165 + for<'de> serde::Deserialize<'de>
166 + ops::AddAssign<u64>
167 + ops::Add<u64, Output = Self>
168 + Sub<u64, Output = Self>
169 + 'static
170 + Committable
171{
172 #[must_use]
174 fn genesis() -> Self {
175 Self::new(0)
176 }
177
178 fn new(val: u64) -> Self;
180
181 fn u64(&self) -> u64;
183}
184
185pub trait NodeType:
187 Clone
188 + Copy
189 + Debug
190 + Hash
191 + PartialEq
192 + Eq
193 + PartialOrd
194 + Ord
195 + Default
196 + serde::Serialize
197 + for<'de> Deserialize<'de>
198 + Send
199 + Sync
200 + 'static
201{
202 const UPGRADE_CONSTANTS: UpgradeConstants = DEFAULT_UPGRADE_CONSTANTS;
204 type View: ConsensusTime + Display;
208 type Epoch: ConsensusTime + Display;
210 type BlockHeader: BlockHeader<Self>;
212 type BlockPayload: BlockPayload<
216 Self,
217 Instance = Self::InstanceState,
218 Transaction = Self::Transaction,
219 ValidatedState = Self::ValidatedState,
220 >;
221 type SignatureKey: SignatureKey;
223 type Transaction: Transaction;
227
228 type InstanceState: InstanceState;
230
231 type ValidatedState: ValidatedState<Self, Instance = Self::InstanceState, Time = Self::View>;
233
234 type Membership: Membership<Self>;
236
237 type BuilderSignatureKey: BuilderSignatureKey;
239
240 type StateSignatureKey: StateSignatureKey
242 + LCV1StateSignatureKey
243 + LCV2StateSignatureKey
244 + LCV3StateSignatureKey;
245}
246
247pub trait Versions: Clone + Copy + Debug + Send + Sync + 'static {
249 type Base: StaticVersionType;
251
252 type Upgrade: StaticVersionType;
254
255 const UPGRADE_HASH: [u8; 32];
257
258 type Epochs: StaticVersionType;
260
261 type DrbAndHeaderUpgrade: StaticVersionType;
263}