OSDN Git Service

init push for add newBlockCh for p2p (#443)
authorPaladz <yzhu101@uottawa.ca>
Wed, 21 Mar 2018 02:01:18 +0000 (10:01 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Mar 2018 02:01:18 +0000 (10:01 +0800)
blockchain/reactor.go
mining/cpuminer/cpuminer.go
mining/miningpool/minepool.go
protocol/txpool.go

index 34e5fc2..a6aefe9 100755 (executable)
@@ -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)
index fcd6a3b..9112980 100644 (file)
@@ -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,
        }
 }
index 7dabafb..0fe2e41 100644 (file)
@@ -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
 }
index 906d43e..ca39057 100644 (file)
@@ -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