github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/config/genesis.go (about)

     1  package config
     2  
     3  import (
     4  	log "github.com/sirupsen/logrus"
     5  
     6  	"github.com/bytom/bytom/consensus"
     7  	"github.com/bytom/bytom/protocol/bc"
     8  	"github.com/bytom/bytom/protocol/bc/types"
     9  )
    10  
    11  func toBCTxs(txs []*types.Tx) []*bc.Tx {
    12  	var bcTxs []*bc.Tx
    13  	for _, tx := range txs {
    14  		bcTxs = append(bcTxs, tx.Tx)
    15  	}
    16  	return bcTxs
    17  }
    18  
    19  func mainNetGenesisBlock() *types.Block {
    20  	txs := GenesisTxs()
    21  	merkleRoot, err := types.TxMerkleRoot(toBCTxs(txs))
    22  	if err != nil {
    23  		log.Panicf("fail on calc genesis tx merkel root")
    24  	}
    25  
    26  	block := &types.Block{
    27  		BlockHeader: types.BlockHeader{
    28  			Version:   1,
    29  			Height:    0,
    30  			Timestamp: 1524549600000,
    31  			BlockCommitment: types.BlockCommitment{
    32  				TransactionsMerkleRoot: merkleRoot,
    33  			},
    34  		},
    35  		Transactions: txs,
    36  	}
    37  	return block
    38  }
    39  
    40  func testNetGenesisBlock() *types.Block {
    41  	txs := GenesisTxs()
    42  	merkleRoot, err := types.TxMerkleRoot(toBCTxs(txs))
    43  	if err != nil {
    44  		log.Panicf("fail on calc genesis tx merkel root")
    45  	}
    46  
    47  	block := &types.Block{
    48  		BlockHeader: types.BlockHeader{
    49  			Version:   1,
    50  			Height:    0,
    51  			Timestamp: 1528945000000,
    52  			BlockCommitment: types.BlockCommitment{
    53  				TransactionsMerkleRoot: merkleRoot,
    54  			},
    55  		},
    56  		Transactions: txs,
    57  	}
    58  	return block
    59  }
    60  
    61  func soloNetGenesisBlock() *types.Block {
    62  	txs := GenesisTxs()
    63  	merkleRoot, err := types.TxMerkleRoot(toBCTxs(txs))
    64  	if err != nil {
    65  		log.Panicf("fail on calc genesis tx merkel root")
    66  	}
    67  
    68  	block := &types.Block{
    69  		BlockHeader: types.BlockHeader{
    70  			Version:   1,
    71  			Height:    0,
    72  			Timestamp: 1528945000000,
    73  			BlockCommitment: types.BlockCommitment{
    74  				TransactionsMerkleRoot: merkleRoot,
    75  			},
    76  		},
    77  		Transactions: txs,
    78  	}
    79  	return block
    80  }
    81  
    82  // GenesisBlock will return genesis block
    83  func GenesisBlock() *types.Block {
    84  	return map[string]func() *types.Block{
    85  		"main": mainNetGenesisBlock,
    86  		"test": testNetGenesisBlock,
    87  		"solo": soloNetGenesisBlock,
    88  	}[consensus.ActiveNetParams.Name]()
    89  }