-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconstants.rs
More file actions
379 lines (346 loc) · 15.2 KB
/
constants.rs
File metadata and controls
379 lines (346 loc) · 15.2 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
// SPDX-License-Identifier: CC0-1.0
//! Blockdata constants.
//!
//! This module provides various constants relating to the blockchain and
//! consensus code. In particular, it defines the genesis block and its
//! single transaction.
//!
use core::default::Default;
use hashes::{Hash, sha256d};
use hex_lit::hex;
use internals::impl_array_newtype;
use crate::blockdata::block::{self, Block};
use crate::blockdata::locktime::absolute;
use crate::blockdata::opcodes::all::*;
use crate::blockdata::script;
use crate::blockdata::transaction::Transaction;
use crate::blockdata::transaction::outpoint::OutPoint;
use crate::blockdata::transaction::txin::TxIn;
use crate::blockdata::transaction::txout::TxOut;
use crate::blockdata::witness::Witness;
use crate::internal_macros::impl_bytes_newtype;
use crate::pow::CompactTarget;
use dash_network::Network;
/// How many satoshis are in "one dash".
pub const COIN_VALUE: u64 = 100_000_000;
/// How many seconds between blocks we expect on average.
pub const TARGET_BLOCK_SPACING: u32 = 600;
/// How many blocks between diffchanges.
pub const DIFFCHANGE_INTERVAL: u32 = 2016;
/// How much time on average should occur between diffchanges.
pub const DIFFCHANGE_TIMESPAN: u32 = 14 * 24 * 3600;
/// The maximum allowed weight for a block, see BIP 141 (network rule).
pub const MAX_BLOCK_WEIGHT: u32 = 4_000_000;
/// The minimum transaction weight for a valid serialized transaction.
pub const MIN_TRANSACTION_WEIGHT: u32 = 4 * 60;
/// The factor that non-witness serialization data is multiplied by during weight calculation.
pub const WITNESS_SCALE_FACTOR: usize = 4;
/// The maximum allowed number of signature check operations in a block.
pub const MAX_BLOCK_SIGOPS_COST: i64 = 80_000;
/// Mainnet (dash) pubkey address prefix.
pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 76;
// 0x4C
/// Mainnet (dash) script address prefix.
pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 16;
// 0x10
/// Test (testnet, devnet, regtest) pubkey address prefix.
pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 140;
// 0x8C
/// Test (testnet, devnet, regtest) script address prefix.
pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 19;
// 0x13
/// The maximum allowed script size.
pub const MAX_SCRIPT_ELEMENT_SIZE: usize = 520;
/// How may blocks between halvings.
pub const SUBSIDY_HALVING_INTERVAL: u32 = 210_000;
/// Maximum allowed value for an integer in Script.
pub const MAX_SCRIPTNUM_VALUE: u32 = 0x80000000;
// 2^31
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
pub const COINBASE_MATURITY: u32 = 100;
/// The maximum value allowed in an output (useful for sanity checking,
/// since keeping everything below this value should prevent overflows
/// if you are doing anything remotely sane with monetary values).
pub const MAX_MONEY: u64 = 21_000_000 * COIN_VALUE;
/// Constructs and returns the coinbase (and only) transaction of the Dash genesis block.
fn dash_genesis_tx() -> Transaction {
// Base
let mut ret = Transaction {
version: 1,
lock_time: absolute::LockTime::ZERO.to_consensus_u32(),
input: vec![],
output: vec![],
special_transaction_payload: None,
};
// Inputs
// Using raw script bytes to avoid push_slice issues
let in_script = script::ScriptBuf::from(hex!(
"04ffff001d01044c5957697265642030392f4a616e2f32303134205468652047726e64204578706572696d656e7420476f6573204c6976653a204f76657273746f636b2e636f6d204973204e6f7720416363657074696e6720426974636f696e73"
).to_vec());
ret.input.push(TxIn {
previous_output: OutPoint::null(),
script_sig: in_script,
sequence: 0xFFFFFFFF,
witness: Witness::default(),
});
// Outputs
let script_bytes = hex!(
"040184710fa689ad5023690c80f3a49c8f13f8d45b8c857fbcbc8bc4a8e4d3eb4b10f4d4604fa08dce601aaf0f470216fe1b51850b4acf21b179c45070ac7b03a9"
);
let out_script =
script::Builder::new().push_slice(script_bytes).push_opcode(OP_CHECKSIG).into_script();
ret.output.push(TxOut {
value: 50 * COIN_VALUE,
script_pubkey: out_script,
});
// end
ret
}
/// Constructs and returns the genesis block.
pub fn genesis_block(network: Network) -> Block {
let txdata = vec![dash_genesis_tx()];
match network {
Network::Dash => {
// Mainnet merkle root - Note: bytes are reversed for internal representation
let merkle_bytes =
hex!("c762a6567f3cc092f0684bb62b7e00a84890b990f07cc71a6bb58d64b98e02e0");
let merkle_root = sha256d::Hash::from_slice(&merkle_bytes).unwrap().into();
Block {
header: block::Header {
version: block::Version::ONE,
prev_blockhash: Hash::all_zeros(),
merkle_root,
time: 1390095618,
bits: CompactTarget::from_consensus(0x1e0ffff0),
nonce: 28917698,
},
txdata,
}
}
Network::Testnet => {
// Testnet merkle root (same as mainnet for Dash) - Note: bytes are reversed for internal representation
let merkle_bytes =
hex!("c762a6567f3cc092f0684bb62b7e00a84890b990f07cc71a6bb58d64b98e02e0");
let merkle_root = sha256d::Hash::from_slice(&merkle_bytes).unwrap().into();
Block {
header: block::Header {
version: block::Version::ONE,
prev_blockhash: Hash::all_zeros(),
merkle_root,
time: 1390666206,
bits: CompactTarget::from_consensus(0x1e0ffff0),
nonce: 3861367235,
},
txdata,
}
}
Network::Devnet => {
// Devnet merkle root (same as mainnet/testnet - all use Dash genesis tx) - Note: bytes are reversed for internal representation
let merkle_bytes =
hex!("c762a6567f3cc092f0684bb62b7e00a84890b990f07cc71a6bb58d64b98e02e0");
let merkle_root = sha256d::Hash::from_slice(&merkle_bytes).unwrap().into();
Block {
header: block::Header {
version: block::Version::ONE,
prev_blockhash: Hash::all_zeros(),
merkle_root,
time: 1598918400,
bits: CompactTarget::from_consensus(0x1e0377ae),
nonce: 52613770,
},
txdata,
}
}
Network::Regtest => {
let hash: sha256d::Hash = txdata[0].txid().into();
let merkle_root = hash.into();
Block {
header: block::Header {
version: block::Version::ONE,
prev_blockhash: Hash::all_zeros(),
merkle_root,
time: 1296688602,
bits: CompactTarget::from_consensus(0x207fffff),
nonce: 2,
},
txdata,
}
}
// Any new network variant must be handled explicitly.
other => unreachable!("genesis_block(): unsupported network variant {other:?}"),
}
}
/// The uniquely identifying hash of the target blockchain.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChainHash([u8; 32]);
impl_array_newtype!(ChainHash, u8, 32);
impl_bytes_newtype!(ChainHash, 32);
impl ChainHash {
// Mainnet value can be verified at https://github.com/lightning/bolts/blob/master/00-introduction.md
/// `ChainHash` for mainnet dash.
pub const DASH: Self = Self([
0x00, 0x00, 0x0f, 0xfd, 0x59, 0x0b, 0x14, 0x85, 0xb3, 0xca, 0xad, 0xc1, 0x9b, 0x22, 0xe6,
0x37, 0x9c, 0x73, 0x33, 0x55, 0x10, 0x8f, 0x10, 0x7a, 0x43, 0x04, 0x58, 0xcd, 0xf3, 0x40,
0x7a, 0xb6,
]);
/// `ChainHash` for testnet dash.
pub const TESTNET: Self = Self([
0x00, 0x00, 0x0b, 0xaf, 0xbc, 0x94, 0xad, 0xd7, 0x6c, 0xb7, 0x5e, 0x2e, 0xc9, 0x28, 0x94,
0x83, 0x72, 0x88, 0xa4, 0x81, 0xe5, 0xc0, 0x05, 0xf6, 0x56, 0x3d, 0x91, 0x62, 0x3b, 0xf8,
0xbc, 0x2c,
]);
/// `ChainHash` for devnet dash.
pub const DEVNET: Self = Self([
0x4e, 0x5f, 0x93, 0x0c, 0x5d, 0x73, 0xa8, 0x79, 0x2f, 0xa6, 0x81, 0xba, 0x8c, 0x5e, 0xaf,
0x74, 0xaa, 0x63, 0x97, 0x4a, 0x5b, 0x1f, 0x59, 0x8d, 0xd5, 0x08, 0x02, 0x9a, 0xee, 0x70,
0x16, 0x7b,
]);
/// `ChainHash` for regtest dash.
pub const REGTEST: Self = Self([
0x53, 0xb3, 0xed, 0x30, 0x30, 0x78, 0x1a, 0xc1, 0x9e, 0x48, 0x52, 0xd9, 0xc5, 0x8f, 0x28,
0x04, 0x57, 0x4f, 0x98, 0x66, 0xf3, 0xf7, 0xf6, 0x91, 0x31, 0xee, 0xb1, 0x3f, 0x44, 0x9f,
0x80, 0x07,
]);
/// Returns the hash of the `network` genesis block for use as a chain hash.
///
/// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash)
/// for specification.
pub const fn using_genesis_block(network: Network) -> Self {
let hashes = [Self::DASH, Self::TESTNET, Self::DEVNET, Self::REGTEST];
hashes[network as usize]
}
/// Converts genesis block hash into `ChainHash`.
pub fn from_genesis_block_hash(block_hash: crate::BlockHash) -> Self {
ChainHash(block_hash.to_byte_array())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::consensus::encode::serialize;
use crate::internal_macros::hex;
use dash_network::Network;
#[test]
fn dash_genesis_first_transaction() {
let genesis_tx = dash_genesis_tx();
assert_eq!(genesis_tx.version, 1);
assert_eq!(genesis_tx.input.len(), 1);
assert_eq!(genesis_tx.input[0].previous_output.txid, Hash::all_zeros());
assert_eq!(genesis_tx.input[0].previous_output.vout, 0xFFFFFFFF);
assert_eq!(
serialize(&genesis_tx.input[0].script_sig),
hex!(
"6104ffff001d01044c5957697265642030392f4a616e2f32303134205468652047726e64204578706572696d656e7420476f6573204c6976653a204f76657273746f636b2e636f6d204973204e6f7720416363657074696e6720426974636f696e73"
)
);
assert_eq!(genesis_tx.input[0].sequence, u32::MAX);
assert_eq!(genesis_tx.output.len(), 1);
assert_eq!(
serialize(&genesis_tx.output[0].script_pubkey),
hex!(
"4341040184710fa689ad5023690c80f3a49c8f13f8d45b8c857fbcbc8bc4a8e4d3eb4b10f4d4604fa08dce601aaf0f470216fe1b51850b4acf21b179c45070ac7b03a9ac"
)
);
assert_eq!(genesis_tx.output[0].value, 50 * COIN_VALUE);
assert_eq!(genesis_tx.lock_time, 0);
// The wtxid should be deterministic for the coinbase transaction
let wtxid_str = genesis_tx.wtxid().to_string();
assert_eq!(wtxid_str, "babeaa0bf3af03c0f12d94da95c7f28168be22087a16fb207e7abda4ae654ee3");
}
#[test]
fn dash_genesis_full_block() {
let genesis_block = genesis_block(Network::Dash);
assert_eq!(genesis_block.header.version, block::Version::ONE);
assert_eq!(genesis_block.header.prev_blockhash, Hash::all_zeros());
assert_eq!(
genesis_block.header.merkle_root.to_string(),
"e0028eb9648db56b1ac77cf090b99048a8007e2bb64b68f092c03c7f56a662c7"
);
assert_eq!(genesis_block.header.time, 1390095618);
assert_eq!(genesis_block.header.bits, CompactTarget::from_consensus(0x1e0ffff0));
assert_eq!(genesis_block.header.nonce, 28917698);
assert_eq!(
genesis_block.header.block_hash().to_string(),
"00000ffd590b1485b3caadc19b22e6379c733355108f107a430458cdf3407ab6"
);
}
#[test]
fn testnet_genesis_full_block() {
let genesis_block = genesis_block(Network::Testnet);
assert_eq!(genesis_block.header.version, block::Version::ONE);
assert_eq!(genesis_block.header.prev_blockhash, Hash::all_zeros());
assert_eq!(
genesis_block.header.merkle_root.to_string(),
"e0028eb9648db56b1ac77cf090b99048a8007e2bb64b68f092c03c7f56a662c7"
);
assert_eq!(genesis_block.header.time, 1390666206);
assert_eq!(genesis_block.header.bits, CompactTarget::from_consensus(0x1e0ffff0));
assert_eq!(genesis_block.header.nonce, 3861367235);
assert_eq!(
genesis_block.header.block_hash().to_string(),
"00000bafbc94add76cb75e2ec92894837288a481e5c005f6563d91623bf8bc2c"
);
}
#[test]
fn devnet_genesis_full_block() {
let genesis_block = genesis_block(Network::Devnet);
assert_eq!(genesis_block.header.version, block::Version::ONE);
assert_eq!(genesis_block.header.prev_blockhash, Hash::all_zeros());
assert_eq!(
genesis_block.header.merkle_root.to_string(),
"e0028eb9648db56b1ac77cf090b99048a8007e2bb64b68f092c03c7f56a662c7"
);
assert_eq!(genesis_block.header.time, 1598918400);
assert_eq!(genesis_block.header.bits, CompactTarget::from_consensus(0x1e0377ae));
assert_eq!(genesis_block.header.nonce, 52613770);
assert_eq!(
genesis_block.header.block_hash().to_string(),
"4e5f930c5d73a8792fa681ba8c5eaf74aa63974a5b1f598dd508029aee70167b"
);
}
// The *_chain_hash tests are sanity/regression tests, they verify that the const byte array
// representing the genesis block is the same as that created by hashing the genesis block.
fn chain_hash_and_genesis_block(network: Network) {
// The genesis block hash is a double-sha256 and it is displayed backwards.
let genesis_hash = genesis_block(network).block_hash();
let want = format!("{:02x}", genesis_hash);
let chain_hash = ChainHash::using_genesis_block(network);
let got = format!("{:02x}", chain_hash);
// Compare strings because the spec specifically states how the chain hash must encode to hex.
assert_eq!(got, want);
#[allow(unreachable_patterns)] // This is specifically trying to catch later added variants.
match network {
Network::Dash => {}
Network::Testnet => {}
Network::Devnet => {}
Network::Regtest => {}
_ => panic!(
"Update ChainHash::using_genesis_block and chain_hash_genesis_block with new variants"
),
}
}
macro_rules! chain_hash_genesis_block {
($($test_name:ident, $network:expr);* $(;)*) => {
$(
#[test]
fn $test_name() {
chain_hash_and_genesis_block($network);
}
)*
}
}
chain_hash_genesis_block! {
mainnet_chain_hash_genesis_block, Network::Dash;
testnet_chain_hash_genesis_block, Network::Testnet;
devnet_chain_hash_genesis_block, Network::Devnet;
regtest_chain_hash_genesis_block, Network::Regtest;
}
// Test vector taken from: https://github.com/lightning/bolts/blob/master/00-introduction.md
#[test]
fn mainnet_chain_hash_test_vector() {
let got = ChainHash::using_genesis_block(Network::Dash).to_string();
let want = "00000ffd590b1485b3caadc19b22e6379c733355108f107a430458cdf3407ab6";
assert_eq!(got, want);
}
}