OSDN Git Service

test (#52)
[bytom/vapor.git] / mining / miningpool / miningpool.go
index eecec59..066fcb5 100644 (file)
@@ -8,8 +8,9 @@ import (
        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"
 )
 
@@ -28,20 +29,20 @@ type MiningPool struct {
        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()
@@ -67,7 +68,15 @@ func (m *MiningPool) 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
@@ -102,7 +111,7 @@ func (m *MiningPool) submitWork(bh *types.BlockHeader) error {
                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 {
@@ -112,7 +121,9 @@ func (m *MiningPool) submitWork(bh *types.BlockHeader) error {
                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
 }