This commit is contained in:
gary rong 2017-12-22 12:39:30 +00:00 committed by GitHub
commit 02b93bc223
3 changed files with 21 additions and 16 deletions

View file

@ -31,7 +31,7 @@ import (
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner/remote"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
@ -68,12 +68,12 @@ func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 {
// It offers only methods that operate on data that pose no security risk when it is publicly accessible. // It offers only methods that operate on data that pose no security risk when it is publicly accessible.
type PublicMinerAPI struct { type PublicMinerAPI struct {
e *Ethereum e *Ethereum
agent *miner.RemoteAgent agent *remote.RemoteAgent
} }
// NewPublicMinerAPI create a new PublicMinerAPI instance. // NewPublicMinerAPI create a new PublicMinerAPI instance.
func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI { func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI {
agent := miner.NewRemoteAgent(e.BlockChain(), e.Engine()) agent := remote.NewRemoteAgent(e.BlockChain(), e.Engine())
e.Miner().Register(agent) e.Miner().Register(agent)
return &PublicMinerAPI{e, agent} return &PublicMinerAPI{e, agent}

View file

@ -1,4 +1,4 @@
// Copyright 2015 The go-ethereum Authors // Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library. // This file is part of the go-ethereum library.
// //
// The go-ethereum library is free software: you can redistribute it and/or modify // The go-ethereum library is free software: you can redistribute it and/or modify
@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package miner package remote
import ( import (
"errors" "errors"
@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner"
) )
type hashrate struct { type hashrate struct {
@ -39,13 +40,13 @@ type RemoteAgent struct {
mu sync.Mutex mu sync.Mutex
quitCh chan struct{} quitCh chan struct{}
workCh chan *Work workCh chan *miner.Work
returnCh chan<- *Result returnCh chan<- *miner.Result
chain consensus.ChainReader chain consensus.ChainReader
engine consensus.Engine engine consensus.Engine
currentWork *Work currentWork *miner.Work
work map[common.Hash]*Work work map[common.Hash]*miner.Work
hashrateMu sync.RWMutex hashrateMu sync.RWMutex
hashrate map[common.Hash]hashrate hashrate map[common.Hash]hashrate
@ -57,7 +58,7 @@ func NewRemoteAgent(chain consensus.ChainReader, engine consensus.Engine) *Remot
return &RemoteAgent{ return &RemoteAgent{
chain: chain, chain: chain,
engine: engine, engine: engine,
work: make(map[common.Hash]*Work), work: make(map[common.Hash]*miner.Work),
hashrate: make(map[common.Hash]hashrate), hashrate: make(map[common.Hash]hashrate),
} }
} }
@ -69,11 +70,11 @@ func (a *RemoteAgent) SubmitHashrate(id common.Hash, rate uint64) {
a.hashrate[id] = hashrate{time.Now(), rate} a.hashrate[id] = hashrate{time.Now(), rate}
} }
func (a *RemoteAgent) Work() chan<- *Work { func (a *RemoteAgent) Work() chan<- *miner.Work {
return a.workCh return a.workCh
} }
func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result) { func (a *RemoteAgent) SetReturnCh(returnCh chan<- *miner.Result) {
a.returnCh = returnCh a.returnCh = returnCh
} }
@ -82,7 +83,7 @@ func (a *RemoteAgent) Start() {
return return
} }
a.quitCh = make(chan struct{}) a.quitCh = make(chan struct{})
a.workCh = make(chan *Work, 1) a.workCh = make(chan *miner.Work, 1)
go a.loop(a.workCh, a.quitCh) go a.loop(a.workCh, a.quitCh)
} }
@ -156,7 +157,7 @@ func (a *RemoteAgent) SubmitWork(nonce types.BlockNonce, mixDigest, hash common.
block := work.Block.WithSeal(result) block := work.Block.WithSeal(result)
// Solutions seems to be valid, return to the miner and notify acceptance // Solutions seems to be valid, return to the miner and notify acceptance
a.returnCh <- &Result{work, block} a.returnCh <- &miner.Result{work, block}
delete(a.work, hash) delete(a.work, hash)
return true return true
@ -168,7 +169,7 @@ func (a *RemoteAgent) SubmitWork(nonce types.BlockNonce, mixDigest, hash common.
// Note, the reason the work and quit channels are passed as parameters is because // Note, the reason the work and quit channels are passed as parameters is because
// RemoteAgent.Start() constantly recreates these channels, so the loop code cannot // RemoteAgent.Start() constantly recreates these channels, so the loop code cannot
// assume data stability in these member fields. // assume data stability in these member fields.
func (a *RemoteAgent) loop(workCh chan *Work, quitCh chan struct{}) { func (a *RemoteAgent) loop(workCh chan *miner.Work, quitCh chan struct{}) {
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop() defer ticker.Stop()
@ -184,7 +185,7 @@ func (a *RemoteAgent) loop(workCh chan *Work, quitCh chan struct{}) {
// cleanup // cleanup
a.mu.Lock() a.mu.Lock()
for hash, work := range a.work { for hash, work := range a.work {
if time.Since(work.createdAt) > 7*(12*time.Second) { if time.Since(work.GetCreateTime()) > 7*(12*time.Second) {
delete(a.work, hash) delete(a.work, hash)
} }
} }

View file

@ -81,6 +81,10 @@ type Work struct {
createdAt time.Time createdAt time.Time
} }
func (work Work) GetCreateTime() time.Time {
return work.createdAt
}
type Result struct { type Result struct {
Work *Work Work *Work
Block *types.Block Block *types.Block