@@ -12,7 +12,6 @@ use core::default::Default;
1212
1313use hashes:: { Hash , sha256d} ;
1414use hex_lit:: hex;
15- use internals:: impl_array_newtype;
1615
1716use crate :: blockdata:: block:: { self , Block } ;
1817use crate :: blockdata:: locktime:: absolute;
@@ -23,7 +22,6 @@ use crate::blockdata::transaction::outpoint::OutPoint;
2322use crate :: blockdata:: transaction:: txin:: TxIn ;
2423use crate :: blockdata:: transaction:: txout:: TxOut ;
2524use crate :: blockdata:: witness:: Witness ;
26- use crate :: internal_macros:: impl_bytes_newtype;
2725use crate :: pow:: CompactTarget ;
2826use dash_network:: Network ;
2927
@@ -184,54 +182,6 @@ pub fn genesis_block(network: Network) -> Block {
184182 }
185183}
186184
187- /// The uniquely identifying hash of the target blockchain.
188- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
189- pub struct ChainHash ( [ u8 ; 32 ] ) ;
190- impl_array_newtype ! ( ChainHash , u8 , 32 ) ;
191- impl_bytes_newtype ! ( ChainHash , 32 ) ;
192-
193- impl ChainHash {
194- // Mainnet value can be verified at https://github.com/lightning/bolts/blob/master/00-introduction.md
195- /// `ChainHash` for mainnet dash.
196- pub const DASH : Self = Self ( [
197- 0x00 , 0x00 , 0x0f , 0xfd , 0x59 , 0x0b , 0x14 , 0x85 , 0xb3 , 0xca , 0xad , 0xc1 , 0x9b , 0x22 , 0xe6 ,
198- 0x37 , 0x9c , 0x73 , 0x33 , 0x55 , 0x10 , 0x8f , 0x10 , 0x7a , 0x43 , 0x04 , 0x58 , 0xcd , 0xf3 , 0x40 ,
199- 0x7a , 0xb6 ,
200- ] ) ;
201- /// `ChainHash` for testnet dash.
202- pub const TESTNET : Self = Self ( [
203- 0x00 , 0x00 , 0x0b , 0xaf , 0xbc , 0x94 , 0xad , 0xd7 , 0x6c , 0xb7 , 0x5e , 0x2e , 0xc9 , 0x28 , 0x94 ,
204- 0x83 , 0x72 , 0x88 , 0xa4 , 0x81 , 0xe5 , 0xc0 , 0x05 , 0xf6 , 0x56 , 0x3d , 0x91 , 0x62 , 0x3b , 0xf8 ,
205- 0xbc , 0x2c ,
206- ] ) ;
207- /// `ChainHash` for devnet dash.
208- pub const DEVNET : Self = Self ( [
209- 0x4e , 0x5f , 0x93 , 0x0c , 0x5d , 0x73 , 0xa8 , 0x79 , 0x2f , 0xa6 , 0x81 , 0xba , 0x8c , 0x5e , 0xaf ,
210- 0x74 , 0xaa , 0x63 , 0x97 , 0x4a , 0x5b , 0x1f , 0x59 , 0x8d , 0xd5 , 0x08 , 0x02 , 0x9a , 0xee , 0x70 ,
211- 0x16 , 0x7b ,
212- ] ) ;
213- /// `ChainHash` for regtest dash.
214- pub const REGTEST : Self = Self ( [
215- 0x53 , 0xb3 , 0xed , 0x30 , 0x30 , 0x78 , 0x1a , 0xc1 , 0x9e , 0x48 , 0x52 , 0xd9 , 0xc5 , 0x8f , 0x28 ,
216- 0x04 , 0x57 , 0x4f , 0x98 , 0x66 , 0xf3 , 0xf7 , 0xf6 , 0x91 , 0x31 , 0xee , 0xb1 , 0x3f , 0x44 , 0x9f ,
217- 0x80 , 0x07 ,
218- ] ) ;
219-
220- /// Returns the hash of the `network` genesis block for use as a chain hash.
221- ///
222- /// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash)
223- /// for specification.
224- pub const fn using_genesis_block ( network : Network ) -> Self {
225- let hashes = [ Self :: DASH , Self :: TESTNET , Self :: DEVNET , Self :: REGTEST ] ;
226- hashes[ network as usize ]
227- }
228-
229- /// Converts genesis block hash into `ChainHash`.
230- pub fn from_genesis_block_hash ( block_hash : crate :: BlockHash ) -> Self {
231- ChainHash ( block_hash. to_byte_array ( ) )
232- }
233- }
234-
235185#[ cfg( test) ]
236186mod test {
237187 use super :: * ;
@@ -326,54 +276,21 @@ mod test {
326276 ) ;
327277 }
328278
329- // The *_chain_hash tests are sanity/regression tests, they verify that the const byte array
330- // representing the genesis block is the same as that created by hashing the genesis block.
331- fn chain_hash_and_genesis_block ( network : Network ) {
332- // The genesis block hash is a double-sha256, and it is displayed backwards.
333- let genesis_hash = genesis_block ( network) . block_hash ( ) ;
334- let want = format ! ( "{:02x}" , genesis_hash) ;
335-
336- let chain_hash = ChainHash :: using_genesis_block ( network) ;
337- let got = format ! ( "{:02x}" , chain_hash) ;
338-
339- // Compare strings because the spec specifically states how the chain hash must encode to hex.
340- assert_eq ! ( got, want) ;
341-
342- #[ allow( unreachable_patterns) ] // This is specifically trying to catch later added variants.
343- match network {
344- Network :: Dash => { }
345- Network :: Testnet => { }
346- Network :: Devnet => { }
347- Network :: Regtest => { }
348- _ => panic ! (
349- "Update ChainHash::using_genesis_block and chain_hash_genesis_block with new variants"
350- ) ,
351- }
352- }
353-
354- macro_rules! chain_hash_genesis_block {
355- ( $( $test_name: ident, $network: expr) ;* $( ; ) * ) => {
356- $(
357- #[ test]
358- fn $test_name( ) {
359- chain_hash_and_genesis_block( $network) ;
360- }
361- ) *
362- }
363- }
364-
365- chain_hash_genesis_block ! {
366- mainnet_chain_hash_genesis_block, Network :: Dash ;
367- testnet_chain_hash_genesis_block, Network :: Testnet ;
368- devnet_chain_hash_genesis_block, Network :: Devnet ;
369- regtest_chain_hash_genesis_block, Network :: Regtest ;
370- }
371-
372- // Test vector taken from: https://github.com/lightning/bolts/blob/master/00-introduction.md
373279 #[ test]
374- fn mainnet_chain_hash_test_vector ( ) {
375- let got = ChainHash :: using_genesis_block ( Network :: Dash ) . to_string ( ) ;
376- let want = "00000ffd590b1485b3caadc19b22e6379c733355108f107a430458cdf3407ab6" ;
377- assert_eq ! ( got, want) ;
280+ fn regtest_genesis_full_block ( ) {
281+ let genesis_block = genesis_block ( Network :: Regtest ) ;
282+ assert_eq ! ( genesis_block. header. version, block:: Version :: ONE ) ;
283+ assert_eq ! ( genesis_block. header. prev_blockhash, Hash :: all_zeros( ) ) ;
284+ assert_eq ! (
285+ genesis_block. header. merkle_root. to_string( ) ,
286+ "babeaa0bf3af03c0f12d94da95c7f28168be22087a16fb207e7abda4ae654ee3"
287+ ) ;
288+ assert_eq ! ( genesis_block. header. time, 1296688602 ) ;
289+ assert_eq ! ( genesis_block. header. bits, CompactTarget :: from_consensus( 0x207fffff ) ) ;
290+ assert_eq ! ( genesis_block. header. nonce, 2 ) ;
291+ assert_eq ! (
292+ genesis_block. header. block_hash( ) . to_string( ) ,
293+ "53b3ed3030781ac19e4852d9c58f2804574f9866f3f7f69131eeb13f449f8007"
294+ ) ;
378295 }
379296}
0 commit comments