OSDN Git Service

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