OSDN Git Service

5f643a02f102944b69095346bd4be207100e2769
[bytom/vapor.git] / config / genesis.go
1 package config
2
3 import (
4         "encoding/hex"
5
6         log "github.com/sirupsen/logrus"
7
8         "github.com/vapor/consensus"
9         "github.com/vapor/protocol/bc"
10         "github.com/vapor/protocol/bc/types"
11 )
12
13 func GenesisTx() *types.Tx {
14         contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
15         if err != nil {
16                 log.Panicf("fail on decode genesis tx output control program")
17         }
18
19         txData := types.TxData{
20                 Version: 1,
21                 Inputs: []*types.TxInput{
22                         types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
23                 },
24                 Outputs: []*types.TxOutput{
25                         types.NewIntraChainOutput(*consensus.BTMAssetID, consensus.InitialBlockSubsidy, contract),
26                 },
27         }
28         return types.NewTx(txData)
29 }
30
31 func mainNetGenesisBlock() *types.Block {
32         tx := GenesisTx()
33         txStatus := bc.NewTransactionStatus()
34         if err := txStatus.SetStatus(0, false); err != nil {
35                 log.Panicf(err.Error())
36         }
37         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
38         if err != nil {
39                 log.Panicf("fail on calc genesis tx status merkle root")
40         }
41
42         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
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: 1524549600000,
52                         BlockCommitment: types.BlockCommitment{
53                                 TransactionsMerkleRoot: merkleRoot,
54                                 TransactionStatusHash:  txStatusHash,
55                         },
56                 },
57                 Transactions: []*types.Tx{tx},
58         }
59         return block
60 }
61
62 func testNetGenesisBlock() *types.Block {
63         tx := GenesisTx()
64         txStatus := bc.NewTransactionStatus()
65         if err := txStatus.SetStatus(0, false); err != nil {
66                 log.Panicf(err.Error())
67         }
68         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
69         if err != nil {
70                 log.Panicf("fail on calc genesis tx status merkle root")
71         }
72
73         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
74         if err != nil {
75                 log.Panicf("fail on calc genesis tx merkel root")
76         }
77
78         block := &types.Block{
79                 BlockHeader: types.BlockHeader{
80                         Version:   1,
81                         Height:    0,
82                         Timestamp: 1528945000000,
83                         BlockCommitment: types.BlockCommitment{
84                                 TransactionsMerkleRoot: merkleRoot,
85                                 TransactionStatusHash:  txStatusHash,
86                         },
87                 },
88                 Transactions: []*types.Tx{tx},
89         }
90         return block
91 }
92
93 func soloNetGenesisBlock() *types.Block {
94         tx := GenesisTx()
95         txStatus := bc.NewTransactionStatus()
96         if err := txStatus.SetStatus(0, false); err != nil {
97                 log.Panicf(err.Error())
98         }
99         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
100         if err != nil {
101                 log.Panicf("fail on calc genesis tx status merkle root")
102         }
103
104         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
105         if err != nil {
106                 log.Panicf("fail on calc genesis tx merkel root")
107         }
108
109         block := &types.Block{
110                 BlockHeader: types.BlockHeader{
111                         Version:   1,
112                         Height:    0,
113                         Timestamp: 1528945000000,
114                         BlockCommitment: types.BlockCommitment{
115                                 TransactionsMerkleRoot: merkleRoot,
116                                 TransactionStatusHash:  txStatusHash,
117                         },
118                 },
119                 Transactions: []*types.Tx{tx},
120         }
121         return block
122 }
123
124 // GenesisBlock will return genesis block
125 func GenesisBlock() *types.Block {
126         return map[string]func() *types.Block{
127                 "main":  mainNetGenesisBlock,
128                 "test":  testNetGenesisBlock,
129                 "solo":  soloNetGenesisBlock,
130                 "vapor": soloNetGenesisBlock,
131         }[consensus.ActiveNetParams.Name]()
132 }