OSDN Git Service

Add the implementation for dppos
[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/protocol/bc"
13         "github.com/vapor/protocol/bc/types"
14         "github.com/vapor/protocol/vm/vmutil"
15 )
16
17 func commitToArguments() (res *[32]byte) {
18         var fedpegPubkeys []ed25519.PublicKey
19         for _, xpub := range consensus.ActiveNetParams.FedpegXPubs {
20                 fedpegPubkeys = append(fedpegPubkeys, xpub.PublicKey())
21         }
22         fedpegScript, _ := vmutil.P2SPMultiSigProgram(fedpegPubkeys, len(fedpegPubkeys))
23
24         var buffer bytes.Buffer
25         for _, address := range CommonConfig.Consensus.Signers {
26                 redeemContract := address.ScriptAddress()
27                 buffer.Write(redeemContract)
28         }
29
30         hasher := sha256.New()
31         hasher.Write(fedpegScript)
32         hasher.Write(buffer.Bytes())
33         resSlice := hasher.Sum(nil)
34         res = new([32]byte)
35         copy(res[:], resSlice)
36         return
37 }
38
39 func genesisTx() *types.Tx {
40
41         contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
42         if err != nil {
43                 log.Panicf("fail on decode genesis tx output control program")
44         }
45
46         coinbaseInput := commitToArguments()
47         txData := types.TxData{
48                 Version: 1,
49                 Inputs: []*types.TxInput{
50                         // Any consensus-related values that are command-line set can be added here for anti-footgun
51                         types.NewCoinbaseInput(coinbaseInput[:]),
52                         //types.NewCoinbaseInput([]byte("Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018.")),
53                 },
54                 Outputs: []*types.TxOutput{
55                         types.NewTxOutput(*consensus.BTMAssetID, consensus.InitialBlockSubsidy, contract),
56                 },
57         }
58
59         return types.NewTx(txData)
60 }
61
62 func mainNetGenesisBlock() *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: 1524549600,
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 testNetGenesisBlock() *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         block := &types.Block{
109                 BlockHeader: types.BlockHeader{
110                         Version:   1,
111                         Height:    0,
112                         Timestamp: 1528945000,
113                         BlockCommitment: types.BlockCommitment{
114                                 TransactionsMerkleRoot: merkleRoot,
115                                 TransactionStatusHash:  txStatusHash,
116                         },
117                 },
118                 Transactions: []*types.Tx{tx},
119         }
120         return block
121 }
122
123 func soloNetGenesisBlock() *types.Block {
124         tx := genesisTx()
125         txStatus := bc.NewTransactionStatus()
126         if err := txStatus.SetStatus(0, false); err != nil {
127                 log.Panicf(err.Error())
128         }
129         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
130         if err != nil {
131                 log.Panicf("fail on calc genesis tx status merkle root")
132         }
133
134         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
135         if err != nil {
136                 log.Panicf("fail on calc genesis tx merkel root")
137         }
138
139         block := &types.Block{
140                 BlockHeader: types.BlockHeader{
141                         Version:   1,
142                         Height:    0,
143                         Timestamp: CommonConfig.Consensus.GenesisTimestamp,
144                         BlockCommitment: types.BlockCommitment{
145                                 TransactionsMerkleRoot: merkleRoot,
146                                 TransactionStatusHash:  txStatusHash,
147                         },
148                 },
149                 Transactions: []*types.Tx{tx},
150         }
151         return block
152 }
153
154 // GenesisBlock will return genesis block
155 func GenesisBlock() *types.Block {
156         return map[string]func() *types.Block{
157                 "main": mainNetGenesisBlock,
158                 "test": testNetGenesisBlock,
159                 "solo": soloNetGenesisBlock,
160         }[consensus.ActiveNetParams.Name]()
161 }