Skip to content

Commit 0ee7966

Browse files
karalabefjl
authored andcommitted
eth, miner: verify PoW in the remote agent to notify submitter (bnb-chain#3438)
1 parent 1fe67c1 commit 0ee7966

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

‎eth/api.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type PublicMinerAPI struct {
8282

8383
// NewPublicMinerAPI create a new PublicMinerAPI instance.
8484
func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI {
85-
agent := miner.NewRemoteAgent()
85+
agent := miner.NewRemoteAgent(e.Pow())
8686
e.Miner().Register(agent)
8787

8888
return &PublicMinerAPI{e, agent}

‎miner/remote_agent.go‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/ethereum/go-ethereum/common"
2828
"github.com/ethereum/go-ethereum/logger"
2929
"github.com/ethereum/go-ethereum/logger/glog"
30+
"github.com/ethereum/go-ethereum/pow"
3031
)
3132

3233
type hashrate struct {
@@ -41,6 +42,7 @@ type RemoteAgent struct {
4142
workCh chan *Work
4243
returnCh chan<- *Result
4344

45+
pow pow.PoW
4446
currentWork *Work
4547
work map[common.Hash]*Work
4648

@@ -50,8 +52,9 @@ type RemoteAgent struct {
5052
running int32 // running indicates whether the agent is active. Call atomically
5153
}
5254

53-
func NewRemoteAgent() *RemoteAgent {
55+
func NewRemoteAgent(pow pow.PoW) *RemoteAgent {
5456
return &RemoteAgent{
57+
pow: pow,
5558
work: make(map[common.Hash]*Work),
5659
hashrate: make(map[common.Hash]hashrate),
5760
}
@@ -126,24 +129,30 @@ func (a *RemoteAgent) GetWork() ([3]string, error) {
126129
return res, errors.New("No work available yet, don't panic.")
127130
}
128131

129-
// Returns true or false, but does not indicate if the PoW was correct
132+
// SubmitWork tries to inject a PoW solution tinto the remote agent, returning
133+
// whether the solution was acceted or not (not can be both a bad PoW as well as
134+
// any other error, like no work pending).
130135
func (a *RemoteAgent) SubmitWork(nonce uint64, mixDigest, hash common.Hash) bool {
131136
a.mu.Lock()
132137
defer a.mu.Unlock()
133138

134139
// Make sure the work submitted is present
135-
if a.work[hash] != nil {
136-
block := a.work[hash].Block.WithMiningResult(nonce, mixDigest)
137-
a.returnCh <- &Result{a.work[hash], block}
138-
139-
delete(a.work, hash)
140-
141-
return true
142-
} else {
143-
glog.V(logger.Info).Infof("Work was submitted for %x but no pending work found\n", hash)
140+
work := a.work[hash]
141+
if work == nil {
142+
glog.V(logger.Info).Infof("Work was submitted for %x but no pending work found", hash)
143+
return false
144+
}
145+
// Make sure the PoW solutions is indeed valid
146+
block := work.Block.WithMiningResult(nonce, mixDigest)
147+
if !a.pow.Verify(block) {
148+
glog.V(logger.Warn).Infof("Invalid PoW submitted for %x", hash)
149+
return false
144150
}
151+
// Solutions seems to be valid, return to the miner and notify acceptance
152+
a.returnCh <- &Result{work, block}
153+
delete(a.work, hash)
145154

146-
return false
155+
return true
147156
}
148157

149158
// loop monitors mining events on the work and quit channels, updating the internal

0 commit comments

Comments
 (0)