log "github.com/sirupsen/logrus"
"github.com/vapor/account"
+ "github.com/vapor/event"
+ "github.com/vapor/mining"
"github.com/vapor/protocol"
- "github.com/vapor/protocol/bc"
"github.com/vapor/protocol/bc/types"
)
block *types.Block
submitCh chan *submitBlockMsg
- chain *protocol.Chain
- accountManager *account.Manager
- txPool *protocol.TxPool
- newBlockCh chan *bc.Hash
+ chain *protocol.Chain
+ accountManager *account.Manager
+ txPool *protocol.TxPool
+ eventDispatcher *event.Dispatcher
}
// NewMiningPool will create a new MiningPool
-func NewMiningPool(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, newBlockCh chan *bc.Hash) *MiningPool {
+func NewMiningPool(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, dispatcher *event.Dispatcher) *MiningPool {
m := &MiningPool{
- submitCh: make(chan *submitBlockMsg, maxSubmitChSize),
- chain: c,
- accountManager: accountManager,
- txPool: txPool,
- newBlockCh: newBlockCh,
+ submitCh: make(chan *submitBlockMsg, maxSubmitChSize),
+ chain: c,
+ accountManager: accountManager,
+ txPool: txPool,
+ eventDispatcher: dispatcher,
}
m.generateBlock()
go m.blockUpdater()
// generateBlock generates a block template to mine
func (m *MiningPool) generateBlock() {
+ m.mutex.Lock()
+ defer m.mutex.Unlock()
+ block, err := mining.NewBlockTemplate(m.chain, m.txPool, m.accountManager)
+ if err != nil {
+ log.Errorf("miningpool: failed on create NewBlockTemplate: %v", err)
+ return
+ }
+ m.block = block
}
// GetWork will return a block header for p2p mining
return errors.New("pending mining block has been changed")
}
- //m.block.Nonce = bh.Nonce
+ m.block.Nonce = bh.Nonce
m.block.Timestamp = bh.Timestamp
isOrphan, err := m.chain.ProcessBlock(m.block)
if err != nil {
return errors.New("submit result is orphan")
}
- blockHash := bh.Hash()
- m.newBlockCh <- &blockHash
+ if err := m.eventDispatcher.Post(event.NewMinedBlockEvent{Block: *m.block}); err != nil {
+ return err
+ }
+
return nil
}