"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"
)
const (
// BlockchainChannel is a channel for blocks and status updates
BlockchainChannel = byte(0x40)
+ maxNewBlockChSize = int(1024)
statusUpdateIntervalSeconds = 10
maxBlockchainResponseSize = 22020096 + 2
sw *p2p.Switch
handler http.Handler
evsw types.EventSwitch
+ newBlockCh chan *bc.Hash
miningEnable bool
}
// 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,
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
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)
"github.com/bytom/consensus/difficulty"
"github.com/bytom/mining"
"github.com/bytom/protocol"
+ "github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
)
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
"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)
}
// 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,
updateNumWorkers: make(chan struct{}),
queryHashesPerSec: make(chan float64),
updateHashes: make(chan uint64),
+ newBlockCh: newBlockCh,
}
}
"github.com/bytom/blockchain/account"
"github.com/bytom/mining"
"github.com/bytom/protocol"
+ "github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
)
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
} else if isOrphan {
log.Warning("SubmitWork is orphan")
}
+
+ blockHash := bh.Hash()
+ m.newBlockCh <- &blockHash
return err == nil
}
// 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