OSDN Git Service

versoin1.1.9 (#594)
[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/bytom/vapor/consensus"
9         "github.com/bytom/vapor/crypto"
10         "github.com/bytom/vapor/crypto/ed25519/chainkd"
11         "github.com/bytom/vapor/protocol/bc"
12         "github.com/bytom/vapor/protocol/bc/types"
13         "github.com/bytom/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, height uint64) []byte {
37         script := FederationPMultiSigScript(c)
38         if height > consensus.CrossInForkHeight {
39                 script = FederationPMultiSigScript(&Config{Federation: SingleFederationConfig()})
40         }
41
42         scriptHash := crypto.Sha256(script)
43         wscript, err := vmutil.P2WSHProgram(scriptHash)
44         if err != nil {
45                 log.Panicf("Fail converts scriptHash to witness: %v", err)
46         }
47
48         return wscript
49 }
50
51 func GenesisTx() *types.Tx {
52         contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
53         if err != nil {
54                 log.Panicf("fail on decode genesis tx output control program")
55         }
56
57         coinbaseInput := FederationWScript(CommonConfig, 0)
58
59         txData := types.TxData{
60                 Version: 1,
61                 Inputs: []*types.TxInput{
62                         types.NewCoinbaseInput(coinbaseInput[:]),
63                 },
64                 Outputs: []*types.TxOutput{
65                         types.NewIntraChainOutput(*consensus.BTMAssetID, consensus.BlockSubsidy(0), contract),
66                 },
67         }
68         return types.NewTx(txData)
69 }
70
71 func mainNetGenesisBlock() *types.Block {
72         tx := GenesisTx()
73         txStatus := bc.NewTransactionStatus()
74         if err := txStatus.SetStatus(0, false); err != nil {
75                 log.Panicf(err.Error())
76         }
77         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
78         if err != nil {
79                 log.Panicf("fail on calc genesis tx status merkle root")
80         }
81
82         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
83         if err != nil {
84                 log.Panicf("fail on calc genesis tx merkel root")
85         }
86
87         block := &types.Block{
88                 BlockHeader: types.BlockHeader{
89                         Version:   1,
90                         Height:    0,
91                         Timestamp: 1563344560002,
92                         BlockCommitment: types.BlockCommitment{
93                                 TransactionsMerkleRoot: merkleRoot,
94                                 TransactionStatusHash:  txStatusHash,
95                         },
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         block := &types.Block{
119                 BlockHeader: types.BlockHeader{
120                         Version:   1,
121                         Height:    0,
122                         Timestamp: 1563344560001,
123                         BlockCommitment: types.BlockCommitment{
124                                 TransactionsMerkleRoot: merkleRoot,
125                                 TransactionStatusHash:  txStatusHash,
126                         },
127                 },
128                 Transactions: []*types.Tx{tx},
129         }
130         return block
131 }
132
133 func soloNetGenesisBlock() *types.Block {
134         tx := GenesisTx()
135         txStatus := bc.NewTransactionStatus()
136         if err := txStatus.SetStatus(0, false); err != nil {
137                 log.Panicf(err.Error())
138         }
139         txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
140         if err != nil {
141                 log.Panicf("fail on calc genesis tx status merkle root")
142         }
143
144         merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
145         if err != nil {
146                 log.Panicf("fail on calc genesis tx merkel root")
147         }
148
149         block := &types.Block{
150                 BlockHeader: types.BlockHeader{
151                         Version:   1,
152                         Height:    0,
153                         Timestamp: 1563344560000,
154                         BlockCommitment: types.BlockCommitment{
155                                 TransactionsMerkleRoot: merkleRoot,
156                                 TransactionStatusHash:  txStatusHash,
157                         },
158                 },
159                 Transactions: []*types.Tx{tx},
160         }
161         return block
162 }
163
164 // GenesisBlock will return genesis block
165 func GenesisBlock() *types.Block {
166         return map[string]func() *types.Block{
167                 "main": mainNetGenesisBlock,
168                 "test": testNetGenesisBlock,
169                 "solo": soloNetGenesisBlock,
170         }[consensus.ActiveNetParams.Name]()
171 }