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/types"
"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/rlp"
"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.
type PublicMinerAPI struct {
e *Ethereum
agent *miner.RemoteAgent
agent *remote.RemoteAgent
}
// NewPublicMinerAPI create a new PublicMinerAPI instance.
func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI {
agent := miner.NewRemoteAgent(e.BlockChain(), e.Engine())
agent := remote.NewRemoteAgent(e.BlockChain(), e.Engine())
e.Miner().Register(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.
//
// 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
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package miner
package remote
import (
"errors"
@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/miner"
)
type hashrate struct {
@ -39,13 +40,13 @@ type RemoteAgent struct {
mu sync.Mutex
quitCh chan struct{}
workCh chan *Work
returnCh chan<- *Result
workCh chan *miner.Work
returnCh chan<- *miner.Result
chain consensus.ChainReader
engine consensus.Engine
currentWork *Work
work map[common.Hash]*Work
currentWork *miner.Work
work map[common.Hash]*miner.Work
hashrateMu sync.RWMutex
hashrate map[common.Hash]hashrate
@ -57,7 +58,7 @@ func NewRemoteAgent(chain consensus.ChainReader, engine consensus.Engine) *Remot
return &RemoteAgent{
chain: chain,
engine: engine,
work: make(map[common.Hash]*Work),
work: make(map[common.Hash]*miner.Work),
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}
}
func (a *RemoteAgent) Work() chan<- *Work {
func (a *RemoteAgent) Work() chan<- *miner.Work {
return a.workCh
}
func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result) {
func (a *RemoteAgent) SetReturnCh(returnCh chan<- *miner.Result) {
a.returnCh = returnCh
}
@ -82,7 +83,7 @@ func (a *RemoteAgent) Start() {
return
}
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)
}
@ -156,7 +157,7 @@ func (a *RemoteAgent) SubmitWork(nonce types.BlockNonce, mixDigest, hash common.
block := work.Block.WithSeal(result)
// 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)
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
// RemoteAgent.Start() constantly recreates these channels, so the loop code cannot
// 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)
defer ticker.Stop()
@ -184,7 +185,7 @@ func (a *RemoteAgent) loop(workCh chan *Work, quitCh chan struct{}) {
// cleanup
a.mu.Lock()
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)
}
}

View file

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