OSDN Git Service

431cf8eb924598a7cbc99cdf22be02b002db02ab
[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/crypto/ed25519/chainkd"
10         "github.com/vapor/protocol/bc"
11         "github.com/vapor/protocol/bc/types"
12         "github.com/vapor/protocol/vm/vmutil"
13 )
14
15 func FederationProgrom(c *Config) []byte {
16         xpubs := c.Federation.Xpubs
17         fedpegScript, err := vmutil.P2SPMultiSigProgram(chainkd.XPubKeys(xpubs), c.Federation.Quorum)
18         if err != nil {
19                 log.Panicf("fail to generate federation scirpt for federation: %v", err)
20         }
21
22         return fedpegScript
23 }
24
25 func GenesisTx() *types.Tx {
26         contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
27         if err != nil {
28                 log.Panicf("fail on decode genesis tx output control program")
29         }
30
31         coinbaseInput := FederationProgrom(CommonConfig)
32
33         txData := types.TxData{
34                 Version: 1,
35                 Inputs: []*types.TxInput{
36                         types.NewCoinbaseInput(coinbaseInput[:]),
37                 },
38                 Outputs: []*types.TxOutput{
39                         types.NewIntraChainOutput(*consensus.BTMAssetID, consensus.BlockSubsidy(0), contract),
40                 },
41         }
42         return types.NewTx(txData)
43 }
44
45 func mainNetGenesisBlock() *types.Block {
46         tx := GenesisTx()
47         txStatus := bc.NewTransactionStatus()
48         if err := txStatus.SetStatus(0, false); err != nil {
49                 log.Panicf(err.Error())
50         }
51         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
52         if err != nil {
53                 log.Panicf("fail on calc genesis tx status merkle root")
54         }
55
56         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
57         if err != nil {
58                 log.Panicf("fail on calc genesis tx merkel root")
59         }
60
61         block := &types.Block{
62                 BlockHeader: types.BlockHeader{
63                         Version:   1,
64                         Height:    0,
65                         Timestamp: 1561600800002,
66                         BlockCommitment: types.BlockCommitment{
67                                 TransactionsMerkleRoot: merkleRoot,
68                                 TransactionStatusHash:  txStatusHash,
69                         },
70                 },
71                 Transactions: []*types.Tx{tx},
72         }
73         return block
74 }
75
76 func testNetGenesisBlock() *types.Block {
77         tx := GenesisTx()
78         txStatus := bc.NewTransactionStatus()
79         if err := txStatus.SetStatus(0, false); err != nil {
80                 log.Panicf(err.Error())
81         }
82         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
83         if err != nil {
84                 log.Panicf("fail on calc genesis tx status merkle root")
85         }
86
87         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
88         if err != nil {
89                 log.Panicf("fail on calc genesis tx merkel root")
90         }
91
92         block := &types.Block{
93                 BlockHeader: types.BlockHeader{
94                         Version:   1,
95                         Height:    0,
96                         Timestamp: 1561600800001,
97                         BlockCommitment: types.BlockCommitment{
98                                 TransactionsMerkleRoot: merkleRoot,
99                                 TransactionStatusHash:  txStatusHash,
100                         },
101                 },
102                 Transactions: []*types.Tx{tx},
103         }
104         return block
105 }
106
107 func soloNetGenesisBlock() *types.Block {
108         tx := GenesisTx()
109         txStatus := bc.NewTransactionStatus()
110         if err := txStatus.SetStatus(0, false); err != nil {
111                 log.Panicf(err.Error())
112         }
113         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
114         if err != nil {
115                 log.Panicf("fail on calc genesis tx status merkle root")
116         }
117
118         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
119         if err != nil {
120                 log.Panicf("fail on calc genesis tx merkel root")
121         }
122
123         block := &types.Block{
124                 BlockHeader: types.BlockHeader{
125                         Version:   1,
126                         Height:    0,
127                         Timestamp: 1561600800000,
128                         BlockCommitment: types.BlockCommitment{
129                                 TransactionsMerkleRoot: merkleRoot,
130                                 TransactionStatusHash:  txStatusHash,
131                         },
132                 },
133                 Transactions: []*types.Tx{tx},
134         }
135         return block
136 }
137
138 // GenesisBlock will return genesis block
139 func GenesisBlock() *types.Block {
140         return map[string]func() *types.Block{
141                 "main":  mainNetGenesisBlock,
142                 "test":  testNetGenesisBlock,
143                 "solo":  soloNetGenesisBlock,
144                 "vapor": soloNetGenesisBlock,
145         }[consensus.ActiveNetParams.Name]()
146 }