6 log "github.com/sirupsen/logrus"
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"
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},
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)
30 log.Panicf("fail to generate federation scirpt for federation: %v", err)
36 func FederationWScript(c *Config, height uint64) []byte {
37 script := FederationPMultiSigScript(c)
38 if height > consensus.CrossInForkHeight {
39 script = FederationPMultiSigScript(&Config{Federation: SingleFederationConfig()})
42 scriptHash := crypto.Sha256(script)
43 wscript, err := vmutil.P2WSHProgram(scriptHash)
45 log.Panicf("Fail converts scriptHash to witness: %v", err)
51 func GenesisTx() *types.Tx {
52 contract, err := hex.DecodeString("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4")
54 log.Panicf("fail on decode genesis tx output control program")
57 coinbaseInput := FederationWScript(CommonConfig, 0)
59 txData := types.TxData{
61 Inputs: []*types.TxInput{
62 types.NewCoinbaseInput(coinbaseInput[:]),
64 Outputs: []*types.TxOutput{
65 types.NewIntraChainOutput(*consensus.BTMAssetID, consensus.BlockSubsidy(0), contract),
68 return types.NewTx(txData)
71 func mainNetGenesisBlock() *types.Block {
73 txStatus := bc.NewTransactionStatus()
74 if err := txStatus.SetStatus(0, false); err != nil {
75 log.Panicf(err.Error())
77 txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
79 log.Panicf("fail on calc genesis tx status merkle root")
82 merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
84 log.Panicf("fail on calc genesis tx merkel root")
87 block := &types.Block{
88 BlockHeader: types.BlockHeader{
91 Timestamp: 1563344560002,
92 BlockCommitment: types.BlockCommitment{
93 TransactionsMerkleRoot: merkleRoot,
94 TransactionStatusHash: txStatusHash,
97 Transactions: []*types.Tx{tx},
102 func testNetGenesisBlock() *types.Block {
104 txStatus := bc.NewTransactionStatus()
105 if err := txStatus.SetStatus(0, false); err != nil {
106 log.Panicf(err.Error())
108 txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
110 log.Panicf("fail on calc genesis tx status merkle root")
113 merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
115 log.Panicf("fail on calc genesis tx merkel root")
118 block := &types.Block{
119 BlockHeader: types.BlockHeader{
122 Timestamp: 1563344560001,
123 BlockCommitment: types.BlockCommitment{
124 TransactionsMerkleRoot: merkleRoot,
125 TransactionStatusHash: txStatusHash,
128 Transactions: []*types.Tx{tx},
133 func soloNetGenesisBlock() *types.Block {
135 txStatus := bc.NewTransactionStatus()
136 if err := txStatus.SetStatus(0, false); err != nil {
137 log.Panicf(err.Error())
139 txStatusHash, err := types.TxStatusMerkleRoot(txStatus.VerifyStatus)
141 log.Panicf("fail on calc genesis tx status merkle root")
144 merkleRoot, err := types.TxMerkleRoot([]*bc.Tx{tx.Tx})
146 log.Panicf("fail on calc genesis tx merkel root")
149 block := &types.Block{
150 BlockHeader: types.BlockHeader{
153 Timestamp: 1563344560000,
154 BlockCommitment: types.BlockCommitment{
155 TransactionsMerkleRoot: merkleRoot,
156 TransactionStatusHash: txStatusHash,
159 Transactions: []*types.Tx{tx},
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]()