OSDN Git Service

Bvm (#138)
[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         "github.com/bytom/protocol/state"
11 )
12
13 // Generate genesis transaction
14 func GenerateGenesisTx() *legacy.Tx {
15         txData := legacy.TxData{
16                 Version:        1,
17                 SerializedSize: 63,
18                 Inputs:         []*legacy.TxInput{},
19                 Outputs: []*legacy.TxOutput{
20                         &legacy.TxOutput{
21                                 AssetVersion: 1,
22                                 OutputCommitment: legacy.OutputCommitment{
23                                         AssetAmount: bc.AssetAmount{
24                                                 AssetId: consensus.BTMAssetID,
25                                                 Amount:  1470000000000000000,
26                                         },
27                                         VMVersion:      1,
28                                         ControlProgram: []byte{81},
29                                 },
30                         },
31                 },
32         }
33
34         return legacy.NewTx(txData)
35 }
36
37 // Generate genesis block
38 func GenerateGenesisBlock() *legacy.Block {
39         genesisCoinbaseTx := GenerateGenesisTx()
40         merkleRoot, err := bc.MerkleRoot([]*bc.Tx{genesisCoinbaseTx.Tx})
41         if err != nil {
42                 log.Panicf("Fatal create merkelRoot")
43         }
44
45         snap := state.Empty()
46         if err := snap.ApplyTx(genesisCoinbaseTx.Tx); err != nil {
47                 log.Panicf("Fatal ApplyTx")
48         }
49
50         var seed [32]byte
51         sha3pool.Sum256(seed[:], make([]byte, 32))
52
53         genesisBlock := &legacy.Block{
54                 BlockHeader: legacy.BlockHeader{
55                         Version:     1,
56                         Height:      1,
57                         Seed:        bc.NewHash(seed),
58                         TimestampMS: 1511318565142,
59                         BlockCommitment: legacy.BlockCommitment{
60                                 TransactionsMerkleRoot: merkleRoot,
61                                 AssetsMerkleRoot:       snap.Tree.RootHash(),
62                         },
63                         Bits: 2161727821138738707,
64                 },
65                 Transactions: []*legacy.Tx{genesisCoinbaseTx},
66         }
67
68         for i := uint64(0); i <= 10000000000000; i++ {
69                 genesisBlock.Nonce = i
70                 hash := genesisBlock.Hash()
71
72                 if consensus.CheckProofOfWork(&hash, genesisBlock.Bits) {
73                         break
74                 }
75         }
76
77         log.Infof("genesisBlock:%v", genesisBlock)
78         return genesisBlock
79 }