OSDN Git Service

Merge pull request #16 from Bytom/dev
[bytom/vapor.git] / config / genesis.go
1 package config
2
3 import (
4         "crypto/sha256"
5         "encoding/hex"
6
7         log "github.com/sirupsen/logrus"
8
9         "github.com/vapor/consensus"
10         "github.com/vapor/crypto"
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         var signBlockPubkeys []ed25519.PublicKey
21         for _, xpub := range consensus.ActiveNetParams.FedpegXPubs {
22                 fedpegPubkeys = append(fedpegPubkeys, xpub.PublicKey())
23         }
24         fedpegScript, _ := vmutil.P2SPMultiSigProgram(fedpegPubkeys, len(fedpegPubkeys))
25
26         for _, xpub := range consensus.ActiveNetParams.SignBlockXPubs {
27                 signBlockPubkeys = append(signBlockPubkeys, xpub.PublicKey())
28         }
29         signBlockScript, _ := vmutil.P2SPMultiSigProgram(signBlockPubkeys, len(signBlockPubkeys))
30
31         hasher := sha256.New()
32         hasher.Write(fedpegScript)
33         hasher.Write(signBlockScript)
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         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
91         var xPrv chainkd.XPrv
92         if consensus.ActiveNetParams.Signer == "" {
93                 return block
94         }
95         copy(xPrv[:], []byte(consensus.ActiveNetParams.Signer))
96         msg, _ := block.MarshalText()
97         sign := xPrv.Sign(msg)
98         pubHash := crypto.Ripemd160(xPrv.XPub().PublicKey())
99         control, err := vmutil.P2WPKHProgram([]byte(pubHash))
100         if err != nil {
101                 log.Panicf(err.Error())
102         }
103         block.Proof.Sign = sign
104         block.Proof.ControlProgram = control
105         return block
106 }
107
108 func testNetGenesisBlock() *types.Block {
109         tx := genesisTx()
110         txStatus := bc.NewTransactionStatus()
111         if err := txStatus.SetStatus(0, false); err != nil {
112                 log.Panicf(err.Error())
113         }
114         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
115         if err != nil {
116                 log.Panicf("fail on calc genesis tx status merkle root")
117         }
118
119         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
120         if err != nil {
121                 log.Panicf("fail on calc genesis tx merkel root")
122         }
123
124         block := &types.Block{
125                 BlockHeader: types.BlockHeader{
126                         Version:   1,
127                         Height:    0,
128                         Timestamp: 1528945000,
129                         BlockCommitment: types.BlockCommitment{
130                                 TransactionsMerkleRoot: merkleRoot,
131                                 TransactionStatusHash:  txStatusHash,
132                         },
133                 },
134                 Transactions: []*types.Tx{tx},
135         }
136         return block
137 }
138
139 func soloNetGenesisBlock() *types.Block {
140         tx := genesisTx()
141         txStatus := bc.NewTransactionStatus()
142         if err := txStatus.SetStatus(0, false); err != nil {
143                 log.Panicf(err.Error())
144         }
145         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
146         if err != nil {
147                 log.Panicf("fail on calc genesis tx status merkle root")
148         }
149
150         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
151         if err != nil {
152                 log.Panicf("fail on calc genesis tx merkel root")
153         }
154
155         block := &types.Block{
156                 BlockHeader: types.BlockHeader{
157                         Version:   1,
158                         Height:    0,
159                         Timestamp: 1528945000,
160                         BlockCommitment: types.BlockCommitment{
161                                 TransactionsMerkleRoot: merkleRoot,
162                                 TransactionStatusHash:  txStatusHash,
163                         },
164                 },
165                 Transactions: []*types.Tx{tx},
166         }
167         return block
168 }
169
170 // GenesisBlock will return genesis block
171 func GenesisBlock() *types.Block {
172         return map[string]func() *types.Block{
173                 "main": mainNetGenesisBlock,
174                 "test": testNetGenesisBlock,
175                 "solo": soloNetGenesisBlock,
176         }[consensus.ActiveNetParams.Name]()
177 }