From: Paladz Date: Mon, 25 Feb 2019 01:44:09 +0000 (+0800) Subject: Revert "Recommit (#1547)" (#1583) X-Git-Tag: 2.0.0-alpha~89^2~38 X-Git-Url: http://git.sourceforge.jp/view?a=commitdiff_plain;h=3ed56c8bac57343b61ec876c5683c47d936ca29b;p=bytom%2Fbytom.git Revert "Recommit (#1547)" (#1583) This reverts commit f9e8455ed02759378767d4dc3222a40e33a024c9. --- diff --git a/README.md b/README.md index 85280a50..98107b32 100644 --- 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. diff --git a/api/miner.go b/api/miner.go index 9d393653..4fd2ec6d 100644 --- a/api/miner.go +++ b/api/miner.go @@ -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, diff --git a/cmd/bytomd/commands/run_node.go b/cmd/bytomd/commands/run_node.go index f38407d8..3e370e4f 100644 --- a/cmd/bytomd/commands/run_node.go +++ b/cmd/bytomd/commands/run_node.go @@ -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") diff --git a/config/config.go b/config/config.go index 18b9a503..8308025c 100644 --- a/config/config.go +++ b/config/config.go @@ -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, diff --git a/mining/miningpool/miningpool.go b/mining/miningpool/miningpool.go index 0d140040..33520d56 100644 --- a/mining/miningpool/miningpool.go +++ b/mining/miningpool/miningpool.go @@ -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 } diff --git a/node/node.go b/node/node.go index fbc2c948..0b27d1cf 100644 --- a/node/node.go +++ b/node/node.go @@ -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) diff --git a/test/integration/run_test.go b/test/integration/run_test.go index 0078b99a..a8e8c215 100644 --- a/test/integration/run_test.go +++ b/test/integration/run_test.go @@ -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 }