OSDN Git Service

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