OSDN Git Service

Revert "Recommit (#1547)" (#1583)
authorPaladz <yzhu101@uottawa.ca>
Mon, 25 Feb 2019 01:44:09 +0000 (09:44 +0800)
committerGitHub <noreply@github.com>
Mon, 25 Feb 2019 01:44:09 +0000 (09:44 +0800)
This reverts commit f9e8455ed02759378767d4dc3222a40e33a024c9.

README.md
api/miner.go
cmd/bytomd/commands/run_node.go
config/config.go
mining/miningpool/miningpool.go
node/node.go
test/integration/run_test.go

index 85280a5..98107b3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -95,8 +95,7 @@ available flags for `bytomd node`:
       --auth.disable                Disable rpc access authenticate
       --chain_id string             Select network type
   -h, --help                        help for node
-      --mining.enable               Enable mining
-      --mining.recommit_interval    Set mining pool recomit interval in seconds (default 15)
+      --mining                      Enable mining
       --p2p.dial_timeout int        Set dial timeout (default 3)
       --p2p.handshake_timeout int   Set handshake timeout (default 30)
       --p2p.laddr string            Node listen address.
index 9d39365..4fd2ec6 100644 (file)
@@ -11,8 +11,6 @@ import (
        "github.com/bytom/protocol/bc/types"
 )
 
-var ErrEmptyWorkSubmission = errors.New("empty work submission")
-
 // BlockHeaderJSON struct provides support for get work in json format, when it also follows
 // BlockHeader structure
 type BlockHeaderJSON struct {
@@ -101,10 +99,6 @@ type SubmitWorkReq struct {
 
 // submitWork submits work in compressed protobuf format
 func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response {
-       if req.BlockHeader == nil {
-               return NewErrorResponse(ErrEmptyWorkSubmission)
-       }
-
        if err := a.SubmitWork(req.BlockHeader); err != nil {
                return NewErrorResponse(err)
        }
@@ -118,10 +112,6 @@ type SubmitWorkJSONReq struct {
 
 // submitWorkJSON submits work in json format
 func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkJSONReq) Response {
-       if req.BlockHeader == nil {
-               return NewErrorResponse(ErrEmptyWorkSubmission)
-       }
-
        bh := &types.BlockHeader{
                Version:           req.BlockHeader.Version,
                Height:            req.BlockHeader.Height,
index f38407d..3e370e4 100644 (file)
@@ -18,9 +18,7 @@ var runNodeCmd = &cobra.Command{
 
 func init() {
        runNodeCmd.Flags().String("prof_laddr", config.ProfListenAddress, "Use http to profile bytomd programs")
-
-       runNodeCmd.Flags().Bool("mining.enable", config.Mining.Enable, "Enable mining")
-       runNodeCmd.Flags().Uint64("mining.recommit_interval", config.Mining.RecommitInterval, "Set mining pool recomit interval in seconds")
+       runNodeCmd.Flags().Bool("mining", config.Mining, "Enable mining")
 
        runNodeCmd.Flags().Bool("simd.enable", config.Simd.Enable, "Enable SIMD mechan for tensority")
 
index 18b9a50..8308025 100644 (file)
@@ -27,7 +27,6 @@ type Config struct {
        Auth      *RPCAuthConfig   `mapstructure:"auth"`
        Web       *WebConfig       `mapstructure:"web"`
        Simd      *SimdConfig      `mapstructure:"simd"`
-       Mining    *MiningConfig    `mapstructure:"mining"`
        Websocket *WebsocketConfig `mapstructure:"ws"`
 }
 
@@ -40,7 +39,6 @@ func DefaultConfig() *Config {
                Auth:       DefaultRPCAuthConfig(),
                Web:        DefaultWebConfig(),
                Simd:       DefaultSimdConfig(),
-               Mining:     DefaultMiningConfig(),
                Websocket:  DefaultWebsocketConfig(),
        }
 }
@@ -101,6 +99,8 @@ type BaseConfig struct {
        // TCP or UNIX socket address for the profiling server to listen on
        ProfListenAddress string `mapstructure:"prof_laddr"`
 
+       Mining bool `mapstructure:"mining"`
+
        // Database backend: leveldb | memdb
        DBBackend string `mapstructure:"db_backend"`
 
@@ -123,6 +123,7 @@ func DefaultBaseConfig() BaseConfig {
        return BaseConfig{
                Moniker:           "anonymous",
                ProfListenAddress: "",
+               Mining:            false,
                DBBackend:         "leveldb",
                DBPath:            "data",
                KeysPath:          "keystore",
@@ -186,11 +187,6 @@ type SimdConfig struct {
        Enable bool `mapstructure:"enable"`
 }
 
-type MiningConfig struct {
-       Enable           bool   `mapstructure:"enable"`
-       RecommitInterval uint64 `mapstructure:"recommit_interval"`
-}
-
 type WebsocketConfig struct {
        MaxNumWebsockets     int `mapstructure:"max_num_websockets"`
        MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"`
@@ -219,21 +215,13 @@ func DefaultWalletConfig() *WalletConfig {
        }
 }
 
-// Default configurable blockheader verification parameters.
+// Default configurable web parameters.
 func DefaultSimdConfig() *SimdConfig {
        return &SimdConfig{
                Enable: false,
        }
 }
 
-// Default configurable mining parameters.
-func DefaultMiningConfig() *MiningConfig {
-       return &MiningConfig{
-               Enable:           false,
-               RecommitInterval: 15,
-       }
-}
-
 func DefaultWebsocketConfig() *WebsocketConfig {
        return &WebsocketConfig{
                MaxNumWebsockets:     25,
index 0d14004..33520d5 100644 (file)
@@ -11,7 +11,6 @@ import (
        "github.com/bytom/event"
        "github.com/bytom/mining"
        "github.com/bytom/protocol"
-       "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
 )
 
@@ -26,11 +25,9 @@ type submitBlockMsg struct {
 
 // MiningPool is the support struct for p2p mine pool
 type MiningPool struct {
-       mutex            sync.RWMutex
-       blockHeader      *types.BlockHeader
-       submitCh         chan *submitBlockMsg
-       commitMap        map[bc.Hash]([]*types.Tx)
-       recommitInterval time.Duration
+       mutex    sync.RWMutex
+       block    *types.Block
+       submitCh chan *submitBlockMsg
 
        chain           *protocol.Chain
        accountManager  *account.Manager
@@ -39,36 +36,30 @@ type MiningPool struct {
 }
 
 // NewMiningPool will create a new MiningPool
-func NewMiningPool(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, dispatcher *event.Dispatcher, recommitInterval uint64) *MiningPool {
+func NewMiningPool(c *protocol.Chain, accountManager *account.Manager, txPool *protocol.TxPool, dispatcher *event.Dispatcher) *MiningPool {
        m := &MiningPool{
-               submitCh:         make(chan *submitBlockMsg, maxSubmitChSize),
-               commitMap:        make(map[bc.Hash]([]*types.Tx)),
-               recommitInterval: time.Duration(recommitInterval) * time.Second,
-               chain:            c,
-               accountManager:   accountManager,
-               txPool:           txPool,
-               eventDispatcher:  dispatcher,
+               submitCh:        make(chan *submitBlockMsg, maxSubmitChSize),
+               chain:           c,
+               accountManager:  accountManager,
+               txPool:          txPool,
+               eventDispatcher: dispatcher,
        }
-       m.generateBlock(true)
+       m.generateBlock()
        go m.blockUpdater()
        return m
 }
 
 // blockUpdater is the goroutine for keep update mining block
 func (m *MiningPool) blockUpdater() {
-       recommitTicker := time.NewTicker(m.recommitInterval)
        for {
                select {
-               case <-recommitTicker.C:
-                       m.generateBlock(false)
-
                case <-m.chain.BlockWaiter(m.chain.BestBlockHeight() + 1):
-                       m.generateBlock(true)
+                       m.generateBlock()
 
                case submitMsg := <-m.submitCh:
                        err := m.submitWork(submitMsg.blockHeader)
                        if err == nil {
-                               m.generateBlock(true)
+                               m.generateBlock()
                        }
                        submitMsg.reply <- err
                }
@@ -76,34 +67,27 @@ func (m *MiningPool) blockUpdater() {
 }
 
 // generateBlock generates a block template to mine
-func (m *MiningPool) generateBlock(isNextHeight bool) {
+func (m *MiningPool) generateBlock() {
        m.mutex.Lock()
        defer m.mutex.Unlock()
 
-       if isNextHeight {
-               // make a new commitMap, so that the expired map will be deleted(garbage-collected)
-               m.commitMap = make(map[bc.Hash]([]*types.Tx))
-       }
-
        block, err := mining.NewBlockTemplate(m.chain, m.txPool, m.accountManager)
        if err != nil {
                log.Errorf("miningpool: failed on create NewBlockTemplate: %v", err)
                return
        }
-
-       // The previous memory will be reclaimed by gc
-       m.blockHeader = &block.BlockHeader
-       m.commitMap[block.TransactionsMerkleRoot] = block.Transactions
+       m.block = block
 }
 
 // GetWork will return a block header for p2p mining
 func (m *MiningPool) GetWork() (*types.BlockHeader, error) {
-       if m.blockHeader != nil {
+       if m.block != nil {
                m.mutex.RLock()
                defer m.mutex.RUnlock()
 
-               m.blockHeader.Timestamp = uint64(time.Now().Unix())
-               return m.blockHeader, nil
+               m.block.BlockHeader.Timestamp = uint64(time.Now().Unix())
+               bh := m.block.BlockHeader
+               return &bh, nil
        }
        return nil, errors.New("no block is ready for mining")
 }
@@ -123,17 +107,13 @@ func (m *MiningPool) submitWork(bh *types.BlockHeader) error {
        m.mutex.Lock()
        defer m.mutex.Unlock()
 
-       if m.blockHeader == nil || bh.PreviousBlockHash != m.blockHeader.PreviousBlockHash {
+       if m.block == nil || bh.PreviousBlockHash != m.block.PreviousBlockHash {
                return errors.New("pending mining block has been changed")
        }
 
-       txs, ok := m.commitMap[bh.TransactionsMerkleRoot]
-       if !ok {
-               return errors.New("TransactionsMerkleRoot not found in history")
-       }
-
-       block := &types.Block{*bh, txs}
-       isOrphan, err := m.chain.ProcessBlock(block)
+       m.block.Nonce = bh.Nonce
+       m.block.Timestamp = bh.Timestamp
+       isOrphan, err := m.chain.ProcessBlock(m.block)
        if err != nil {
                return err
        }
@@ -141,5 +121,9 @@ func (m *MiningPool) submitWork(bh *types.BlockHeader) error {
                return errors.New("submit result is orphan")
        }
 
-       return m.eventDispatcher.Post(event.NewMinedBlockEvent{Block: block})
+       if err := m.eventDispatcher.Post(event.NewMinedBlockEvent{Block: m.block}); err != nil {
+               return err
+       }
+
+       return nil
 }
index fbc2c94..0b27d1c 100644 (file)
@@ -143,13 +143,13 @@ func NewNode(config *cfg.Config) *Node {
                wallet:          wallet,
                chain:           chain,
                txfeed:          txFeed,
-               miningEnable:    config.Mining.Enable,
+               miningEnable:    config.Mining,
 
                notificationMgr: notificationMgr,
        }
 
        node.cpuMiner = cpuminer.NewCPUMiner(chain, accounts, txPool, dispatcher)
-       node.miningPool = miningpool.NewMiningPool(chain, accounts, txPool, dispatcher, config.Mining.RecommitInterval)
+       node.miningPool = miningpool.NewMiningPool(chain, accounts, txPool, dispatcher)
 
        node.BaseService = *cmn.NewBaseService(nil, "Node", node)
 
index 0078b99..a8e8c21 100644 (file)
@@ -12,7 +12,7 @@ import (
 func mockConfig() *cfg.Config {
        var config = cfg.DefaultConfig()
        config.Wallet.Disable = false
-       config.Mining.Enable = true
+       config.Mining = true
        config.ApiAddress = "127.0.0.1:9888"
        return config
 }