OSDN Git Service

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