casper_types/chainspec/
transaction_config.rs1mod deploy_config;
2mod runtime_config;
3mod transaction_v1_config;
4
5#[cfg(feature = "datasize")]
6use datasize::DataSize;
7#[cfg(any(feature = "testing", test))]
8use rand::Rng;
9use runtime_config::RuntimeConfig;
10use serde::{Deserialize, Serialize};
11
12#[cfg(any(feature = "testing", test))]
13use crate::testing::TestRng;
14use crate::{
15 bytesrepr::{self, FromBytes, ToBytes},
16 TimeDiff,
17};
18
19pub use deploy_config::DeployConfig;
20#[cfg(any(feature = "testing", test))]
21pub use deploy_config::DEFAULT_MAX_PAYMENT_MOTES;
22#[cfg(any(feature = "testing", test))]
23pub use transaction_v1_config::DEFAULT_LARGE_TRANSACTION_GAS_LIMIT;
24pub use transaction_v1_config::{TransactionLaneDefinition, TransactionV1Config};
25
26pub const DEFAULT_MIN_TRANSFER_MOTES: u64 = 2_500_000_000;
28
29#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
31#[cfg_attr(feature = "datasize", derive(DataSize))]
32#[serde(deny_unknown_fields)]
34pub struct TransactionConfig {
35 pub max_ttl: TimeDiff,
37 pub block_max_approval_count: u32,
39 pub max_block_size: u32,
41 pub block_gas_limit: u64,
43 pub native_transfer_minimum_motes: u64,
46 pub max_timestamp_leeway: TimeDiff,
49 #[serde(rename = "deploy")]
51 pub deploy_config: DeployConfig,
52 #[serde(rename = "v1")]
55 pub transaction_v1_config: TransactionV1Config,
56 #[serde(rename = "enabled_runtime")]
60 pub runtime_config: RuntimeConfig,
61}
62
63#[cfg(any(all(feature = "std", feature = "testing"), test))]
64impl TransactionConfig {
65 pub fn random(rng: &mut TestRng) -> Self {
67 let max_ttl = TimeDiff::from_seconds(rng.gen_range(60..3_600));
68 let block_max_approval_count = rng.gen();
69 let max_block_size = rng.gen_range(1_000_000..1_000_000_000);
70 let block_gas_limit = rng.gen_range(100_000_000_000..1_000_000_000_000_000);
71 let native_transfer_minimum_motes =
72 rng.gen_range(DEFAULT_MIN_TRANSFER_MOTES..1_000_000_000_000_000);
73 let max_timestamp_leeway = TimeDiff::from_seconds(rng.gen_range(0..6));
74 let deploy_config = DeployConfig::random(rng);
75 let transaction_v1_config: TransactionV1Config = TransactionV1Config::random(rng);
76 let runtime_config = RuntimeConfig::random(rng);
77
78 TransactionConfig {
79 max_ttl,
80 block_max_approval_count,
81 max_block_size,
82 block_gas_limit,
83 native_transfer_minimum_motes,
84 max_timestamp_leeway,
85 deploy_config,
86 transaction_v1_config,
87 runtime_config,
88 }
89 }
90}
91
92impl Default for TransactionConfig {
93 fn default() -> Self {
94 let two_hours = TimeDiff::from_seconds(2 * 60 * 60);
95 TransactionConfig {
96 max_ttl: two_hours,
97 block_max_approval_count: 2600,
98 max_block_size: 10_485_760,
99 block_gas_limit: 10_000_000_000_000,
100 native_transfer_minimum_motes: DEFAULT_MIN_TRANSFER_MOTES,
101 max_timestamp_leeway: TimeDiff::from_seconds(5),
102 deploy_config: DeployConfig::default(),
103 runtime_config: RuntimeConfig {
104 vm_casper_v1: true,
105 vm_casper_v2: false,
106 },
107 transaction_v1_config: TransactionV1Config::default(),
108 }
109 }
110}
111
112impl ToBytes for TransactionConfig {
113 fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
114 self.max_ttl.write_bytes(writer)?;
115 self.block_max_approval_count.write_bytes(writer)?;
116 self.max_block_size.write_bytes(writer)?;
117 self.block_gas_limit.write_bytes(writer)?;
118 self.native_transfer_minimum_motes.write_bytes(writer)?;
119 self.max_timestamp_leeway.write_bytes(writer)?;
120 self.deploy_config.write_bytes(writer)?;
121 self.runtime_config.write_bytes(writer)?;
122 self.transaction_v1_config.write_bytes(writer)
123 }
124
125 fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
126 let mut buffer = bytesrepr::allocate_buffer(self)?;
127 self.write_bytes(&mut buffer)?;
128 Ok(buffer)
129 }
130
131 fn serialized_length(&self) -> usize {
132 self.max_ttl.serialized_length()
133 + self.block_max_approval_count.serialized_length()
134 + self.max_block_size.serialized_length()
135 + self.block_gas_limit.serialized_length()
136 + self.native_transfer_minimum_motes.serialized_length()
137 + self.max_timestamp_leeway.serialized_length()
138 + self.deploy_config.serialized_length()
139 + self.runtime_config.serialized_length()
140 + self.transaction_v1_config.serialized_length()
141 }
142}
143
144impl FromBytes for TransactionConfig {
145 fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
146 let (max_ttl, remainder) = TimeDiff::from_bytes(bytes)?;
147 let (block_max_approval_count, remainder) = u32::from_bytes(remainder)?;
148 let (max_block_size, remainder) = u32::from_bytes(remainder)?;
149 let (block_gas_limit, remainder) = u64::from_bytes(remainder)?;
150 let (native_transfer_minimum_motes, remainder) = u64::from_bytes(remainder)?;
151 let (max_timestamp_leeway, remainder) = TimeDiff::from_bytes(remainder)?;
152 let (deploy_config, remainder) = DeployConfig::from_bytes(remainder)?;
153 let (runtime_config, remainder) = RuntimeConfig::from_bytes(remainder)?;
154 let (transaction_v1_config, remainder) = TransactionV1Config::from_bytes(remainder)?;
155
156 let config = TransactionConfig {
157 max_ttl,
158 block_max_approval_count,
159 max_block_size,
160 block_gas_limit,
161 native_transfer_minimum_motes,
162 max_timestamp_leeway,
163 deploy_config,
164 runtime_config,
165 transaction_v1_config,
166 };
167 Ok((config, remainder))
168 }
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174
175 #[test]
176 fn bytesrepr_roundtrip() {
177 let mut rng = TestRng::new();
178 let config = TransactionConfig::random(&mut rng);
179 bytesrepr::test_serialization_roundtrip(&config);
180 }
181}