OSDN Git Service

Utxo storage (#196)
[bytom/bytom.git] / config / genesis.go
1 package config
2
3 import (
4         log "github.com/sirupsen/logrus"
5
6         "github.com/bytom/consensus"
7         "github.com/bytom/crypto/sha3pool"
8         "github.com/bytom/protocol/bc"
9         "github.com/bytom/protocol/bc/legacy"
10 )
11
12 // Generate genesis transaction
13 func GenerateGenesisTx() *legacy.Tx {
14         txData := legacy.TxData{
15                 Version:        1,
16                 SerializedSize: 63,
17                 Inputs:         []*legacy.TxInput{},
18                 Outputs: []*legacy.TxOutput{
19                         &legacy.TxOutput{
20                                 AssetVersion: 1,
21                                 OutputCommitment: legacy.OutputCommitment{
22                                         AssetAmount: bc.AssetAmount{
23                                                 AssetId: consensus.BTMAssetID,
24                                                 Amount:  1470000000000000000,
25                                         },
26                                         VMVersion:      1,
27                                         ControlProgram: []byte{81},
28                                 },
29                         },
30                 },
31         }
32
33         return legacy.NewTx(txData)
34 }
35
36 // Generate genesis block
37 func GenerateGenesisBlock() *legacy.Block {
38         genesisCoinbaseTx := GenerateGenesisTx()
39         merkleRoot, err := bc.MerkleRoot([]*bc.Tx{genesisCoinbaseTx.Tx})
40         if err != nil {
41                 log.Panicf("Fatal create merkelRoot")
42         }
43
44         var seed [32]byte
45         sha3pool.Sum256(seed[:], make([]byte, 32))
46
47         genesisBlock := &legacy.Block{
48                 BlockHeader: legacy.BlockHeader{
49                         Version:     1,
50                         Height:      1,
51                         Seed:        bc.NewHash(seed),
52                         TimestampMS: 1511318565142,
53                         BlockCommitment: legacy.BlockCommitment{
54                                 TransactionsMerkleRoot: merkleRoot,
55                         },
56                         Bits: 2161727821138738707,
57                 },
58                 Transactions: []*legacy.Tx{genesisCoinbaseTx},
59         }
60
61         for i := uint64(0); i <= 10000000000000; i++ {
62                 genesisBlock.Nonce = i
63                 hash := genesisBlock.Hash()
64
65                 if consensus.CheckProofOfWork(&hash, genesisBlock.Bits) {
66                         break
67                 }
68         }
69
70         log.Infof("genesisBlock:%v", genesisBlock)
71         return genesisBlock
72 }