use crate::{block::Block, header::Header, subnets::SUBNETWORK_ID_COINBASE, tx::Transaction};
use kaspa_hashes::{Hash, ZERO_HASH};
use kaspa_muhash::EMPTY_MUHASH;
#[derive(Clone, Debug)]
pub struct GenesisBlock {
pub hash: Hash,
pub version: u16,
pub hash_merkle_root: Hash,
pub utxo_commitment: Hash,
pub timestamp: u64,
pub bits: u32,
pub nonce: u64,
pub daa_score: u64,
pub coinbase_payload: &'static [u8],
}
impl GenesisBlock {
pub fn build_genesis_transactions(&self) -> Vec<Transaction> {
vec![Transaction::new(0, Vec::new(), Vec::new(), 0, SUBNETWORK_ID_COINBASE, 0, self.coinbase_payload.to_vec())]
}
}
impl From<&GenesisBlock> for Header {
fn from(genesis: &GenesisBlock) -> Self {
Header::new(
genesis.version,
Vec::new(),
genesis.hash_merkle_root,
ZERO_HASH,
genesis.utxo_commitment,
genesis.timestamp,
genesis.bits,
genesis.nonce,
genesis.daa_score,
0.into(),
0,
ZERO_HASH,
)
}
}
impl From<&GenesisBlock> for Block {
fn from(genesis: &GenesisBlock) -> Self {
Block::new(genesis.into(), genesis.build_genesis_transactions())
}
}
impl From<(&Header, &'static [u8])> for GenesisBlock {
fn from((header, payload): (&Header, &'static [u8])) -> Self {
Self {
hash: header.hash,
version: header.version,
hash_merkle_root: header.hash_merkle_root,
utxo_commitment: header.utxo_commitment,
timestamp: header.timestamp,
bits: header.bits,
nonce: header.nonce,
daa_score: header.daa_score,
coinbase_payload: payload,
}
}
}
pub const GENESIS: GenesisBlock = GenesisBlock {
hash: Hash::from_bytes([
0x58, 0xc2, 0xd4, 0x19, 0x9e, 0x21, 0xf9, 0x10, 0xd1, 0x57, 0x1d, 0x11, 0x49, 0x69, 0xce, 0xce, 0xf4, 0x8f, 0x9, 0xf9, 0x34,
0xd4, 0x2c, 0xcb, 0x6a, 0x28, 0x1a, 0x15, 0x86, 0x8f, 0x29, 0x99,
]),
version: 0,
hash_merkle_root: Hash::from_bytes([
0x8e, 0xc8, 0x98, 0x56, 0x8c, 0x68, 0x1, 0xd1, 0x3d, 0xf4, 0xee, 0x6e, 0x2a, 0x1b, 0x54, 0xb7, 0xe6, 0x23, 0x6f, 0x67, 0x1f,
0x20, 0x95, 0x4f, 0x5, 0x30, 0x64, 0x10, 0x51, 0x8e, 0xeb, 0x32,
]),
utxo_commitment: Hash::from_bytes([
0x71, 0x0f, 0x27, 0xdf, 0x42, 0x3e, 0x63, 0xaa, 0x6c, 0xdb, 0x72, 0xb8, 0x9e, 0xa5, 0xa0, 0x6c, 0xff, 0xa3, 0x99, 0xd6, 0x6f,
0x16, 0x77, 0x04, 0x45, 0x5b, 0x5a, 0xf5, 0x9d, 0xef, 0x8e, 0x20,
]),
timestamp: 1637609671037,
bits: 486722099,
nonce: 0x3392c,
daa_score: 1312860, #[rustfmt::skip]
coinbase_payload: &[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xd7, 0x95, 0xd7, 0x9e, 0xd7, 0x94, 0x20, 0xd7, 0x93, 0xd7, 0x99, 0x20, 0xd7, 0xa2, 0xd7, 0x9c,
0xd7, 0x99, 0xd7, 0x9a, 0x20, 0xd7, 0x95, 0xd7,
0xa2, 0xd7, 0x9c, 0x20, 0xd7, 0x90, 0xd7, 0x97,
0xd7, 0x99, 0xd7, 0x9a, 0x20, 0xd7, 0x99, 0xd7,
0x99, 0xd7, 0x98, 0xd7, 0x91, 0x20, 0xd7, 0x91,
0xd7, 0xa9, 0xd7, 0x90, 0xd7, 0xa8, 0x20, 0xd7,
0x9b, 0xd7, 0xa1, 0xd7, 0xa4, 0xd7, 0x90, 0x20,
0xd7, 0x95, 0xd7, 0x93, 0xd7, 0x94, 0xd7, 0x91,
0xd7, 0x94, 0x20, 0xd7, 0x9c, 0xd7, 0x9e, 0xd7,
0xa2, 0xd7, 0x91, 0xd7, 0x93, 0x20, 0xd7, 0x9b,
0xd7, 0xa8, 0xd7, 0xa2, 0xd7, 0x95, 0xd7, 0xaa,
0x20, 0xd7, 0x90, 0xd7, 0x9c, 0xd7, 0x94, 0xd7,
0x9b, 0xd7, 0x9d, 0x20, 0xd7, 0xaa, 0xd7, 0xa2,
0xd7, 0x91, 0xd7, 0x93, 0xd7, 0x95, 0xd7, 0x9f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x1f, 0x8e, 0x1c, 0x17, 0xb0, 0x13,
0x3d, 0x43, 0x91, 0x74, 0xe5, 0x2e, 0xfb, 0xb0,
0xc4, 0x1c, 0x35, 0x83, 0xa8, 0xaa, 0x66, 0xb0,
0x0f, 0xca, 0x37, 0xca, 0x66, 0x7c, 0x2d, 0x55, 0x0a, 0x6c, 0x44, 0x16, 0xda, 0xd9, 0x71, 0x7e,
0x50, 0x92, 0x71, 0x28, 0xc4, 0x24, 0xfa, 0x4e,
0xdb, 0xeb, 0xc4, 0x36, 0xab, 0x13, 0xae, 0xef,
],
};
pub const TESTNET_GENESIS: GenesisBlock = GenesisBlock {
hash: Hash::from_bytes([
0xf8, 0x96, 0xa3, 0x03, 0x48, 0x73, 0xbe, 0x17, 0x39, 0xfc, 0x43, 0x59, 0x23, 0x68, 0x99, 0xfd, 0x3d, 0x65, 0xd2, 0xbc, 0x94,
0xf9, 0x78, 0x0d, 0xf0, 0xd0, 0xda, 0x3e, 0xb1, 0xcc, 0x43, 0x70,
]),
version: 0,
hash_merkle_root: Hash::from_bytes([
0x17, 0x34, 0x14, 0x08, 0xa5, 0x72, 0x45, 0x56, 0x50, 0x4d, 0xf4, 0xd6, 0xcf, 0x51, 0x5c, 0xbf, 0xbb, 0x22, 0x04, 0x30, 0xdc,
0x45, 0x1c, 0x74, 0x3c, 0x22, 0xd5, 0xe9, 0x11, 0x72, 0x0c, 0x2a,
]),
utxo_commitment: EMPTY_MUHASH,
timestamp: 0x17c5f62fbb6,
bits: 0x1e7fffff,
nonce: 0x14582,
daa_score: 0,
#[rustfmt::skip]
coinbase_payload: &[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x6e, 0x65, 0x74, ],
};
pub const SIMNET_GENESIS: GenesisBlock = GenesisBlock {
hash: Hash::from_bytes([
0x41, 0x1f, 0x8c, 0xd2, 0x6f, 0x3d, 0x41, 0xae, 0xa3, 0x9e, 0x78, 0x57, 0x39, 0x27, 0xda, 0x24, 0xd2, 0x39, 0x95, 0x70, 0x5b,
0x57, 0x9f, 0x30, 0x95, 0x9b, 0x91, 0x27, 0xe9, 0x6b, 0x79, 0xe3,
]),
version: 0,
hash_merkle_root: Hash::from_bytes([
0x19, 0x46, 0xd6, 0x29, 0xf7, 0xe9, 0x22, 0xa7, 0xbc, 0xed, 0x59, 0x19, 0x05, 0x21, 0xc3, 0x77, 0x1f, 0x73, 0xd3, 0x52, 0xdd,
0xbb, 0xb6, 0x86, 0x56, 0x4a, 0xd7, 0xfd, 0x56, 0x85, 0x7c, 0x1b,
]),
utxo_commitment: EMPTY_MUHASH,
timestamp: 0x17c5f62fbb6,
bits: 0x207fffff,
nonce: 0x2,
daa_score: 0,
#[rustfmt::skip]
coinbase_payload: &[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x2d, 0x73, 0x69, 0x6d, 0x6e, 0x65, 0x74, ],
};
pub const DEVNET_GENESIS: GenesisBlock = GenesisBlock {
hash: Hash::from_bytes([
0x4c, 0xb4, 0x8d, 0x0b, 0x20, 0x73, 0xb8, 0x02, 0x36, 0x01, 0x45, 0xa1, 0x5a, 0xd1, 0xab, 0xdc, 0x01, 0xd8, 0x9b, 0x5c, 0x2f,
0xe4, 0x72, 0x26, 0x30, 0xab, 0x9b, 0x5f, 0xe9, 0xdf, 0xc4, 0xf2,
]),
version: 0,
hash_merkle_root: Hash::from_bytes([
0x58, 0xab, 0xf2, 0x03, 0x21, 0xd7, 0x07, 0x16, 0x16, 0x2b, 0x6b, 0xf8, 0xd9, 0xf5, 0x89, 0xca, 0x33, 0xae, 0x6e, 0x32, 0xb3,
0xb1, 0x9a, 0xbb, 0x7f, 0xa6, 0x5d, 0x11, 0x41, 0xa3, 0xf9, 0x4d,
]),
utxo_commitment: EMPTY_MUHASH,
timestamp: 0x11e9db49828,
bits: 0x1e21bc1c, nonce: 0x48e5e,
daa_score: 0,
#[rustfmt::skip]
coinbase_payload: &[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1, 0xF5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x6b, 0x61, 0x73, 0x70, 0x61, 0x2d, 0x64, 0x65, 0x76, 0x6e, 0x65, 0x74, ],
};
#[cfg(test)]
mod tests {
use super::*;
use crate::merkle::calc_hash_merkle_root;
#[test]
fn test_genesis_hashes() {
[GENESIS, TESTNET_GENESIS, SIMNET_GENESIS, DEVNET_GENESIS].into_iter().for_each(|genesis| {
let block: Block = (&genesis).into();
assert_eq!(calc_hash_merkle_root(block.transactions.iter()), block.header.hash_merkle_root);
assert_hashes_eq(block.hash(), genesis.hash);
});
}
fn assert_hashes_eq(got: Hash, expected: Hash) {
if got != expected {
panic!("Got hash {:#04x?} while expecting {:#04x?}", got.as_bytes(), expected.as_bytes());
}
}
}