OSDN Git Service

add dpos consensus
[bytom/vapor.git] / config / genesis.go
1 package config
2
3 import (
4         "bytes"
5         "crypto/sha256"
6         "encoding/hex"
7
8         log "github.com/sirupsen/logrus"
9
10         "github.com/vapor/consensus"
11         "github.com/vapor/crypto/ed25519"
12         "github.com/vapor/crypto/ed25519/chainkd"
13         "github.com/vapor/protocol/bc"
14         "github.com/vapor/protocol/bc/types"
15         "github.com/vapor/protocol/vm/vmutil"
16 )
17
18 func commitToArguments() (res *[32]byte) {
19         var fedpegPubkeys []ed25519.PublicKey
20         for _, xpub := range consensus.ActiveNetParams.FedpegXPubs {
21                 fedpegPubkeys = append(fedpegPubkeys, xpub.PublicKey())
22         }
23         fedpegScript, _ := vmutil.P2SPMultiSigProgram(fedpegPubkeys, len(fedpegPubkeys))
24
25         var buffer bytes.Buffer
26         for _, address := range CommonConfig.Consensus.Dpos.Signers {
27                 redeemContract := address.ScriptAddress()
28                 buffer.Write(redeemContract)
29         }
30
31         hasher := sha256.New()
32         hasher.Write(fedpegScript)
33         hasher.Write(buffer.Bytes())
34         resSlice := hasher.Sum(nil)
35         res = new([32]byte)
36         copy(res[:], resSlice)
37         return
38 }
39
40 func genesisTx() *types.Tx {
41
42         contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
43         if err != nil {
44                 log.Panicf("fail on decode genesis tx output control program")
45         }
46
47         coinbaseInput := commitToArguments()
48         txData := types.TxData{
49                 Version: 1,
50                 Inputs: []*types.TxInput{
51                         // Any consensus-related values that are command-line set can be added here for anti-footgun
52                         types.NewCoinbaseInput(coinbaseInput[:]),
53                         //types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
54                 },
55                 Outputs: []*types.TxOutput{
56                         types.NewTxOutput(*consensus.BTMAssetID, consensus.InitialBlockSubsidy, contract),
57                 },
58         }
59
60         return types.NewTx(txData)
61 }
62
63 func mainNetGenesisBlock() *types.Block {
64         tx := genesisTx()
65         txStatus := bc.NewTransactionStatus()
66         if err := txStatus.SetStatus(0, false); err != nil {
67                 log.Panicf(err.Error())
68         }
69         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
70         if err != nil {
71                 log.Panicf("fail on calc genesis tx status merkle root")
72         }
73
74         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
75         if err != nil {
76                 log.Panicf("fail on calc genesis tx merkel root")
77         }
78
79         var xPrv chainkd.XPrv
80         if CommonConfig.Consensus.Dpos.XPrv == "" {
81                 log.Panicf("Signer is empty")
82         }
83         xPrv.UnmarshalText([]byte(CommonConfig.Consensus.Dpos.XPrv))
84         b, _ := xPrv.XPub().MarshalText()
85
86         block := &types.Block{
87                 BlockHeader: types.BlockHeader{
88                         Version:   1,
89                         Height:    0,
90                         Timestamp: 1524549600,
91                         BlockCommitment: types.BlockCommitment{
92                                 TransactionsMerkleRoot: merkleRoot,
93                                 TransactionStatusHash:  txStatusHash,
94                         },
95                         Coinbase: b,
96                 },
97                 Transactions: []*types.Tx{tx},
98         }
99         return block
100 }
101
102 func testNetGenesisBlock() *types.Block {
103         tx := genesisTx()
104         txStatus := bc.NewTransactionStatus()
105         if err := txStatus.SetStatus(0, false); err != nil {
106                 log.Panicf(err.Error())
107         }
108         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
109         if err != nil {
110                 log.Panicf("fail on calc genesis tx status merkle root")
111         }
112
113         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
114         if err != nil {
115                 log.Panicf("fail on calc genesis tx merkel root")
116         }
117
118         var xPrv chainkd.XPrv
119         if CommonConfig.Consensus.Dpos.XPrv == "" {
120                 log.Panicf("Signer is empty")
121         }
122         xPrv.UnmarshalText([]byte(CommonConfig.Consensus.Dpos.XPrv))
123         b, _ := xPrv.XPub().MarshalText()
124
125         block := &types.Block{
126                 BlockHeader: types.BlockHeader{
127                         Version:   1,
128                         Height:    0,
129                         Timestamp: 1528945000,
130                         BlockCommitment: types.BlockCommitment{
131                                 TransactionsMerkleRoot: merkleRoot,
132                                 TransactionStatusHash:  txStatusHash,
133                         },
134                         Coinbase: b,
135                 },
136                 Transactions: []*types.Tx{tx},
137         }
138         return block
139 }
140
141 func soloNetGenesisBlock() *types.Block {
142         tx := genesisTx()
143         txStatus := bc.NewTransactionStatus()
144         if err := txStatus.SetStatus(0, false); err != nil {
145                 log.Panicf(err.Error())
146         }
147         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
148         if err != nil {
149                 log.Panicf("fail on calc genesis tx status merkle root")
150         }
151
152         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
153         if err != nil {
154                 log.Panicf("fail on calc genesis tx merkel root")
155         }
156
157         var xPrv chainkd.XPrv
158         if CommonConfig.Consensus.Dpos.XPrv == "" {
159                 log.Panicf("Signer is empty")
160         }
161         xPrv.UnmarshalText([]byte(CommonConfig.Consensus.Dpos.XPrv))
162         b, _ := xPrv.XPub().MarshalText()
163
164         block := &types.Block{
165                 BlockHeader: types.BlockHeader{
166                         Version:   1,
167                         Height:    0,
168                         Timestamp: 1528945000,
169                         BlockCommitment: types.BlockCommitment{
170                                 TransactionsMerkleRoot: merkleRoot,
171                                 TransactionStatusHash:  txStatusHash,
172                         },
173                         Coinbase: b,
174                 },
175                 Transactions: []*types.Tx{tx},
176         }
177         return block
178 }
179
180 // GenesisBlock will return genesis block
181 func GenesisBlock() *types.Block {
182         return map[string]func() *types.Block{
183                 "main": mainNetGenesisBlock,
184                 "test": testNetGenesisBlock,
185                 "solo": soloNetGenesisBlock,
186         }[consensus.ActiveNetParams.Name]()
187 }