OSDN Git Service

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