From: Paladz Date: Wed, 21 Mar 2018 02:01:18 +0000 (+0800) Subject: init push for add newBlockCh for p2p (#443) X-Git-Tag: v1.0.5~266 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=a2360ecfe3d613c693404b8f8fd1ba460876860c;p=bytom%2Fbytom.git init push for add newBlockCh for p2p (#443) --- diff --git a/blockchain/reactor.go b/blockchain/reactor.go index 34e5fc21..a6aefe9b 100755 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -20,6 +20,7 @@ import ( "github.com/bytom/p2p" "github.com/bytom/p2p/trust" "github.com/bytom/protocol" + "github.com/bytom/protocol/bc" protocolTypes "github.com/bytom/protocol/bc/types" "github.com/bytom/types" ) @@ -27,6 +28,7 @@ import ( const ( // BlockchainChannel is a channel for blocks and status updates BlockchainChannel = byte(0x40) + maxNewBlockChSize = int(1024) statusUpdateIntervalSeconds = 10 maxBlockchainResponseSize = 22020096 + 2 @@ -76,6 +78,7 @@ type BlockchainReactor struct { sw *p2p.Switch handler http.Handler evsw types.EventSwitch + newBlockCh chan *bc.Hash miningEnable bool } @@ -103,6 +106,7 @@ func maxBytes(h http.Handler) http.Handler { // NewBlockchainReactor returns the reactor of whole blockchain. func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, accounts *account.Manager, assets *asset.Registry, sw *p2p.Switch, hsm *pseudohsm.HSM, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, accessTokens *accesstoken.CredentialStore, miningEnable bool) *BlockchainReactor { + newBlockCh := make(chan *bc.Hash, maxNewBlockChSize) bcr := &BlockchainReactor{ chain: chain, wallet: wallet, @@ -110,14 +114,15 @@ func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, accoun assets: assets, blockKeeper: newBlockKeeper(chain, sw), txPool: txPool, - mining: cpuminer.NewCPUMiner(chain, accounts, txPool), - miningPool: miningpool.NewMiningPool(chain, accounts, txPool), + mining: cpuminer.NewCPUMiner(chain, accounts, txPool, newBlockCh), + miningPool: miningpool.NewMiningPool(chain, accounts, txPool, newBlockCh), mux: http.NewServeMux(), sw: sw, hsm: hsm, txFeedTracker: txfeeds, accessTokens: accessTokens, miningEnable: miningEnable, + newBlockCh: newBlockCh, } bcr.BaseReactor = *p2p.NewBaseReactor("BlockchainReactor", bcr) return bcr @@ -231,6 +236,12 @@ func (bcr *BlockchainReactor) syncRoutine() { for { select { + case blockHash := <-bcr.newBlockCh: + block, err := bcr.chain.GetBlockByHash(blockHash) + if err != nil { + log.Errorf("Error get block from newBlockCh %v", err) + } + log.WithFields(log.Fields{"Hash": blockHash, "height": block.Height}).Info("Boardcast my new block") case newTx := <-newTxCh: bcr.txFeedTracker.TxFilter(newTx) go bcr.BroadcastTransaction(newTx) diff --git a/mining/cpuminer/cpuminer.go b/mining/cpuminer/cpuminer.go index fcd6a3b2..91129807 100644 --- a/mining/cpuminer/cpuminer.go +++ b/mining/cpuminer/cpuminer.go @@ -14,6 +14,7 @@ import ( "github.com/bytom/consensus/difficulty" "github.com/bytom/mining" "github.com/bytom/protocol" + "github.com/bytom/protocol/bc" "github.com/bytom/protocol/bc/types" ) @@ -40,6 +41,7 @@ type CPUMiner struct { updateHashes chan uint64 speedMonitorQuit chan struct{} quit chan struct{} + newBlockCh chan *bc.Hash } // solveBlock attempts to find some combination of a nonce, extra nonce, and @@ -104,6 +106,9 @@ out: "isOrphan": isOrphan, "tx": len(block.Transactions), }).Info("Miner processed block") + + blockHash := block.Hash() + m.newBlockCh <- &blockHash } else { log.WithField("height", block.BlockHeader.Height).Errorf("Miner fail on ProcessBlock %v", err) } @@ -274,7 +279,7 @@ func (m *CPUMiner) NumWorkers() int32 { // NewCPUMiner returns a new instance of a CPU miner for the provided configuration. // Use Start to begin the mining process. See the documentation for CPUMiner // type for more details. -func NewCPUMiner(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool) *CPUMiner { +func NewCPUMiner(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, newBlockCh chan *bc.Hash) *CPUMiner { return &CPUMiner{ chain: c, accountManager: accountManager, @@ -283,5 +288,6 @@ func NewCPUMiner(c *protocol.Chain, accountManager *account.Manager, txPool *pro updateNumWorkers: make(chan struct{}), queryHashesPerSec: make(chan float64), updateHashes: make(chan uint64), + newBlockCh: newBlockCh, } } diff --git a/mining/miningpool/minepool.go b/mining/miningpool/minepool.go index 7dabafb5..0fe2e415 100644 --- a/mining/miningpool/minepool.go +++ b/mining/miningpool/minepool.go @@ -10,6 +10,7 @@ import ( "github.com/bytom/blockchain/account" "github.com/bytom/mining" "github.com/bytom/protocol" + "github.com/bytom/protocol/bc" "github.com/bytom/protocol/bc/types" ) @@ -23,14 +24,16 @@ type MiningPool struct { chain *protocol.Chain accountManager *account.Manager txPool *protocol.TxPool + newBlockCh chan *bc.Hash } // NewMiningPool will create a new MiningPool -func NewMiningPool(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool) *MiningPool { +func NewMiningPool(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, newBlockCh chan *bc.Hash) *MiningPool { m := &MiningPool{ chain: c, accountManager: accountManager, txPool: txPool, + newBlockCh: newBlockCh, } go m.blockUpdater() return m @@ -90,5 +93,8 @@ func (m *MiningPool) SubmitWork(bh *types.BlockHeader) bool { } else if isOrphan { log.Warning("SubmitWork is orphan") } + + blockHash := bh.Hash() + m.newBlockCh <- &blockHash return err == nil } diff --git a/protocol/txpool.go b/protocol/txpool.go index 906d43e7..ca390577 100644 --- a/protocol/txpool.go +++ b/protocol/txpool.go @@ -24,7 +24,7 @@ var ( // ErrTransactionNotExist is the pre-defined error message ErrTransactionNotExist = errors.New("transaction are not existed in the mempool") // ErrPoolIsFull indicates the pool is full - ErrPoolIsFull = errors.New("transaction pool reach the max number") + ErrPoolIsFull = errors.New("transaction pool reach the max number") ) // TxDesc store tx and related info for mining strategy