• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版dc43c45b0f6f0e0eda023465ff98d90e745abc58 (tree)
时间2020-03-03 15:15:34
作者shenao78 <shenao.78@163....>
Commitershenao78

Log Message

fixed reward address

更改概述

差异

--- a/application/mov/match/match.go
+++ b/application/mov/match/match.go
@@ -18,14 +18,14 @@ import (
1818
1919 // Engine is used to generate math transactions
2020 type Engine struct {
21- orderBook *OrderBook
22- maxFeeRate float64
23- nodeProgram []byte
21+ orderBook *OrderBook
22+ maxFeeRate float64
23+ rewardProgram []byte
2424 }
2525
2626 // NewEngine return a new Engine
27-func NewEngine(orderBook *OrderBook, maxFeeRate float64, nodeProgram []byte) *Engine {
28- return &Engine{orderBook: orderBook, maxFeeRate: maxFeeRate, nodeProgram: nodeProgram}
27+func NewEngine(orderBook *OrderBook, maxFeeRate float64, rewardProgram []byte) *Engine {
28+ return &Engine{orderBook: orderBook, maxFeeRate: maxFeeRate, rewardProgram: rewardProgram}
2929 }
3030
3131 // HasMatchedTx check does the input trade pair can generate a match deal
@@ -77,7 +77,7 @@ func (e *Engine) addMatchTxFeeOutput(txData *types.TxData) error {
7777 feeAmount = matchTxFee.MaxFeeAmount
7878 reminder = matchTxFee.FeeAmount - matchTxFee.MaxFeeAmount
7979 }
80- txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(assetID, uint64(feeAmount), e.nodeProgram))
80+ txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(assetID, uint64(feeAmount), e.rewardProgram))
8181
8282 // There is the remaining amount after paying the handling fee, assign it evenly to participants in the transaction
8383 averageAmount := reminder / int64(len(txData.Inputs))
--- a/application/mov/match/match_test.go
+++ b/application/mov/match/match_test.go
@@ -80,7 +80,7 @@ func TestGenerateMatchedTxs(t *testing.T) {
8080
8181 for i, c := range cases {
8282 movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
83- matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), 0.05, mock.NodeProgram)
83+ matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), 0.05, mock.RewardProgram)
8484 var gotMatchedTxs []*types.Tx
8585 for matchEngine.HasMatchedTx(c.tradePairs...) {
8686 matchedTx, err := matchEngine.NextMatchedTx(c.tradePairs...)
--- a/application/mov/mock/mock.go
+++ b/application/mov/mock/mock.go
@@ -10,11 +10,11 @@ import (
1010 )
1111
1212 var (
13- BTC = bc.NewAssetID([32]byte{1})
14- ETH = bc.NewAssetID([32]byte{2})
15- EOS = bc.NewAssetID([32]byte{3})
16- ETC = bc.NewAssetID([32]byte{4})
17- NodeProgram = []byte{0x58}
13+ BTC = bc.NewAssetID([32]byte{1})
14+ ETH = bc.NewAssetID([32]byte{2})
15+ EOS = bc.NewAssetID([32]byte{3})
16+ ETC = bc.NewAssetID([32]byte{4})
17+ RewardProgram = []byte{0x58}
1818
1919 Btc2EthOrders = []*common.Order{
2020 {
@@ -269,7 +269,7 @@ var (
269269 Outputs: []*types.TxOutput{
270270 types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 500, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19251")),
271271 types.NewIntraChainOutput(*Eth2BtcOrders[0].ToAssetID, 10, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19253")),
272- types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, NodeProgram),
272+ types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, RewardProgram),
273273 },
274274 }),
275275
@@ -285,7 +285,7 @@ var (
285285 // re-order
286286 types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 270, Eth2BtcOrders[2].Utxo.ControlProgram),
287287 // fee
288- types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, NodeProgram),
288+ types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, RewardProgram),
289289 // refund
290290 types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 6, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19251")),
291291 types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 7, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19255")),
@@ -370,7 +370,7 @@ var (
370370 types.NewIntraChainOutput(*Btc2EthOrders[3].FromAssetID, 1, Btc2EthOrders[3].Utxo.ControlProgram),
371371 types.NewIntraChainOutput(*Eth2BtcOrders[2].ToAssetID, 15, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19255")),
372372 // fee
373- types.NewIntraChainOutput(*Btc2EthOrders[3].FromAssetID, 1, NodeProgram),
373+ types.NewIntraChainOutput(*Btc2EthOrders[3].FromAssetID, 1, RewardProgram),
374374 },
375375 }),
376376
@@ -395,7 +395,7 @@ var (
395395 Outputs: []*types.TxOutput{
396396 types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 500, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19251")),
397397 types.NewIntraChainOutput(*Eth2BtcOrders[0].ToAssetID, 10, testutil.MustDecodeHexString("0014f928b723999312df4ed51cb275a2644336c19253")),
398- types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, NodeProgram),
398+ types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, RewardProgram),
399399 },
400400 }),
401401 }
--- a/application/mov/mov_core.go
+++ b/application/mov/mov_core.go
@@ -1,10 +1,13 @@
11 package mov
22
33 import (
4+ "encoding/hex"
5+
46 "github.com/bytom/vapor/application/mov/common"
57 "github.com/bytom/vapor/application/mov/contract"
68 "github.com/bytom/vapor/application/mov/database"
79 "github.com/bytom/vapor/application/mov/match"
10+ "github.com/bytom/vapor/consensus"
811 "github.com/bytom/vapor/consensus/segwit"
912 dbm "github.com/bytom/vapor/database/leveldb"
1013 "github.com/bytom/vapor/errors"
@@ -71,7 +74,7 @@ func (m *MovCore) ApplyBlock(block *types.Block) error {
7174 }
7275
7376 // BeforeProposalBlock return all transactions than can be matched, and the number of transactions cannot exceed the given capacity.
74-func (m *MovCore) BeforeProposalBlock(txs []*types.Tx, nodeProgram []byte, blockHeight uint64, gasLeft int64, isTimeout func() bool) ([]*types.Tx, error) {
77+func (m *MovCore) BeforeProposalBlock(txs []*types.Tx, blockHeight uint64, gasLeft int64, isTimeout func() bool) ([]*types.Tx, error) {
7578 if blockHeight <= m.startBlockHeight {
7679 return nil, nil
7780 }
@@ -81,7 +84,12 @@ func (m *MovCore) BeforeProposalBlock(txs []*types.Tx, nodeProgram []byte, block
8184 return nil, err
8285 }
8386
84- matchEngine := match.NewEngine(orderBook, maxFeeRate, nodeProgram)
87+ rewardProgram, err := hex.DecodeString(consensus.ActiveNetParams.MovRewardProgram)
88+ if err != nil {
89+ return nil, err
90+ }
91+
92+ matchEngine := match.NewEngine(orderBook, maxFeeRate, rewardProgram)
8593 tradePairIterator := database.NewTradePairIterator(m.movStore)
8694 matchCollector := newMatchTxCollector(matchEngine, tradePairIterator, gasLeft, isTimeout)
8795 return matchCollector.result()
--- a/application/mov/mov_core_test.go
+++ b/application/mov/mov_core_test.go
@@ -1,6 +1,7 @@
11 package mov
22
33 import (
4+ "encoding/hex"
45 "math"
56 "os"
67 "testing"
@@ -508,6 +509,8 @@ func TestValidateBlock(t *testing.T) {
508509 }
509510
510511 func TestBeforeProposalBlock(t *testing.T) {
512+ consensus.ActiveNetParams.MovRewardProgram = hex.EncodeToString(mock.RewardProgram)
513+
511514 cases := []struct {
512515 desc string
513516 initOrders []*common.Order
@@ -572,7 +575,7 @@ func TestBeforeProposalBlock(t *testing.T) {
572575 }
573576
574577 movCore := &MovCore{movStore: store}
575- gotMatchedTxs, err := movCore.BeforeProposalBlock(nil, []byte{0x51}, 2, c.gasLeft, func() bool { return false })
578+ gotMatchedTxs, err := movCore.BeforeProposalBlock(nil, 2, c.gasLeft, func() bool { return false })
576579 if err != nil {
577580 t.Fatal(err)
578581 }
--- a/consensus/general.go
+++ b/consensus/general.go
@@ -103,7 +103,12 @@ type Params struct {
103103 ProducerSubsidys []ProducerSubsidy
104104
105105 SoftForkPoint map[uint64]uint64
106+
107+ // Mov will only start when the block height is greater than this value
106108 MovStartHeight uint64
109+
110+ // Used to receive rewards for matching transactions
111+ MovRewardProgram string
107112 }
108113
109114 // ActiveNetParams is the active NetParams
--- a/proposal/proposal.go
+++ b/proposal/proposal.go
@@ -139,11 +139,6 @@ func (b *blockBuilder) applyTransactionFromPool() error {
139139 }
140140
141141 func (b *blockBuilder) applyTransactionFromSubProtocol() error {
142- cp, err := b.accountManager.GetCoinbaseControlProgram()
143- if err != nil {
144- return err
145- }
146-
147142 isTimeout := func() bool {
148143 return b.getTimeoutStatus() > timeoutOk
149144 }
@@ -153,7 +148,7 @@ func (b *blockBuilder) applyTransactionFromSubProtocol() error {
153148 break
154149 }
155150
156- subTxs, err := p.BeforeProposalBlock(b.block.Transactions, cp, b.block.Height, b.gasLeft, isTimeout)
151+ subTxs, err := p.BeforeProposalBlock(b.block.Transactions, b.block.Height, b.gasLeft, isTimeout)
157152 if err != nil {
158153 log.WithFields(log.Fields{"module": logModule, "index": i, "error": err}).Error("failed on sub protocol txs package")
159154 continue
--- a/protocol/bbft.go
+++ b/protocol/bbft.go
@@ -227,16 +227,16 @@ func (c *Chain) signBlockHeader(blockHeader *types.BlockHeader) ([]byte, error)
227227 xprv := config.CommonConfig.PrivateKey()
228228 xpub := xprv.XPub()
229229 node, err := c.getConsensusNode(&blockHeader.PreviousBlockHash, xpub.String())
230- blockHash := blockHeader.Hash().String()
230+ blockHash := blockHeader.Hash()
231231 if err == errNotFoundConsensusNode {
232- log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash}).Warn("can't find consensus node of current node")
232+ log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String()}).Warn("can't find consensus node of current node")
233233 return nil, nil
234234 } else if err != nil {
235235 return nil, err
236236 }
237237
238238 if err := c.checkDoubleSign(blockHeader, node.XPub.String()); err == errDoubleSignBlock {
239- log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash}).Warn("current node has double sign the block")
239+ log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String()}).Warn("current node has double sign the block")
240240 return nil, nil
241241 } else if err != nil {
242242 return nil, err
--- a/protocol/protocol.go
+++ b/protocol/protocol.go
@@ -22,7 +22,7 @@ const (
2222 type Protocoler interface {
2323 Name() string
2424 StartHeight() uint64
25- BeforeProposalBlock(txs []*types.Tx, nodeProgram []byte, blockHeight uint64, gasLeft int64, isTimeout func() bool) ([]*types.Tx, error)
25+ BeforeProposalBlock(txs []*types.Tx, blockHeight uint64, gasLeft int64, isTimeout func() bool) ([]*types.Tx, error)
2626 ChainStatus() (uint64, *bc.Hash, error)
2727 ValidateBlock(block *types.Block, verifyResults []*bc.TxVerifyResult) error
2828 ValidateTxs(txs []*types.Tx, verifyResults []*bc.TxVerifyResult) error
Show on old repository browser