Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
f06ae5ca
Commit
f06ae5ca
authored
Nov 24, 2019
by
Guillaume Ballet
Committed by
Felix Lange
Nov 24, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
miner: fix staticcheck warnings (#20375)
parent
3a0480e0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
39 deletions
+43
-39
miner.go
miner/miner.go
+38
-38
worker_test.go
miner/worker_test.go
+5
-1
No files found.
miner/miner.go
View file @
f06ae5ca
...
@@ -84,8 +84,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
...
@@ -84,8 +84,8 @@ func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *even
// It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
// It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
// the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
// the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
// and halt your mining operation for as long as the DOS continues.
// and halt your mining operation for as long as the DOS continues.
func
(
self
*
Miner
)
update
()
{
func
(
miner
*
Miner
)
update
()
{
events
:=
self
.
mux
.
Subscribe
(
downloader
.
StartEvent
{},
downloader
.
DoneEvent
{},
downloader
.
FailedEvent
{})
events
:=
miner
.
mux
.
Subscribe
(
downloader
.
StartEvent
{},
downloader
.
DoneEvent
{},
downloader
.
FailedEvent
{})
defer
events
.
Unsubscribe
()
defer
events
.
Unsubscribe
()
for
{
for
{
...
@@ -96,77 +96,77 @@ func (self *Miner) update() {
...
@@ -96,77 +96,77 @@ func (self *Miner) update() {
}
}
switch
ev
.
Data
.
(
type
)
{
switch
ev
.
Data
.
(
type
)
{
case
downloader
.
StartEvent
:
case
downloader
.
StartEvent
:
atomic
.
StoreInt32
(
&
self
.
canStart
,
0
)
atomic
.
StoreInt32
(
&
miner
.
canStart
,
0
)
if
self
.
Mining
()
{
if
miner
.
Mining
()
{
self
.
Stop
()
miner
.
Stop
()
atomic
.
StoreInt32
(
&
self
.
shouldStart
,
1
)
atomic
.
StoreInt32
(
&
miner
.
shouldStart
,
1
)
log
.
Info
(
"Mining aborted due to sync"
)
log
.
Info
(
"Mining aborted due to sync"
)
}
}
case
downloader
.
DoneEvent
,
downloader
.
FailedEvent
:
case
downloader
.
DoneEvent
,
downloader
.
FailedEvent
:
shouldStart
:=
atomic
.
LoadInt32
(
&
self
.
shouldStart
)
==
1
shouldStart
:=
atomic
.
LoadInt32
(
&
miner
.
shouldStart
)
==
1
atomic
.
StoreInt32
(
&
self
.
canStart
,
1
)
atomic
.
StoreInt32
(
&
miner
.
canStart
,
1
)
atomic
.
StoreInt32
(
&
self
.
shouldStart
,
0
)
atomic
.
StoreInt32
(
&
miner
.
shouldStart
,
0
)
if
shouldStart
{
if
shouldStart
{
self
.
Start
(
self
.
coinbase
)
miner
.
Start
(
miner
.
coinbase
)
}
}
// stop immediately and ignore all further pending events
// stop immediately and ignore all further pending events
return
return
}
}
case
<-
self
.
exitCh
:
case
<-
miner
.
exitCh
:
return
return
}
}
}
}
}
}
func
(
self
*
Miner
)
Start
(
coinbase
common
.
Address
)
{
func
(
miner
*
Miner
)
Start
(
coinbase
common
.
Address
)
{
atomic
.
StoreInt32
(
&
self
.
shouldStart
,
1
)
atomic
.
StoreInt32
(
&
miner
.
shouldStart
,
1
)
self
.
SetEtherbase
(
coinbase
)
miner
.
SetEtherbase
(
coinbase
)
if
atomic
.
LoadInt32
(
&
self
.
canStart
)
==
0
{
if
atomic
.
LoadInt32
(
&
miner
.
canStart
)
==
0
{
log
.
Info
(
"Network syncing, will start miner afterwards"
)
log
.
Info
(
"Network syncing, will start miner afterwards"
)
return
return
}
}
self
.
worker
.
start
()
miner
.
worker
.
start
()
}
}
func
(
self
*
Miner
)
Stop
()
{
func
(
miner
*
Miner
)
Stop
()
{
self
.
worker
.
stop
()
miner
.
worker
.
stop
()
atomic
.
StoreInt32
(
&
self
.
shouldStart
,
0
)
atomic
.
StoreInt32
(
&
miner
.
shouldStart
,
0
)
}
}
func
(
self
*
Miner
)
Close
()
{
func
(
miner
*
Miner
)
Close
()
{
self
.
worker
.
close
()
miner
.
worker
.
close
()
close
(
self
.
exitCh
)
close
(
miner
.
exitCh
)
}
}
func
(
self
*
Miner
)
Mining
()
bool
{
func
(
miner
*
Miner
)
Mining
()
bool
{
return
self
.
worker
.
isRunning
()
return
miner
.
worker
.
isRunning
()
}
}
func
(
self
*
Miner
)
HashRate
()
uint64
{
func
(
miner
*
Miner
)
HashRate
()
uint64
{
if
pow
,
ok
:=
self
.
engine
.
(
consensus
.
PoW
);
ok
{
if
pow
,
ok
:=
miner
.
engine
.
(
consensus
.
PoW
);
ok
{
return
uint64
(
pow
.
Hashrate
())
return
uint64
(
pow
.
Hashrate
())
}
}
return
0
return
0
}
}
func
(
self
*
Miner
)
SetExtra
(
extra
[]
byte
)
error
{
func
(
miner
*
Miner
)
SetExtra
(
extra
[]
byte
)
error
{
if
uint64
(
len
(
extra
))
>
params
.
MaximumExtraDataSize
{
if
uint64
(
len
(
extra
))
>
params
.
MaximumExtraDataSize
{
return
fmt
.
Errorf
(
"
E
xtra exceeds max length. %d > %v"
,
len
(
extra
),
params
.
MaximumExtraDataSize
)
return
fmt
.
Errorf
(
"
e
xtra exceeds max length. %d > %v"
,
len
(
extra
),
params
.
MaximumExtraDataSize
)
}
}
self
.
worker
.
setExtra
(
extra
)
miner
.
worker
.
setExtra
(
extra
)
return
nil
return
nil
}
}
// SetRecommitInterval sets the interval for sealing work resubmitting.
// SetRecommitInterval sets the interval for sealing work resubmitting.
func
(
self
*
Miner
)
SetRecommitInterval
(
interval
time
.
Duration
)
{
func
(
miner
*
Miner
)
SetRecommitInterval
(
interval
time
.
Duration
)
{
self
.
worker
.
setRecommitInterval
(
interval
)
miner
.
worker
.
setRecommitInterval
(
interval
)
}
}
// Pending returns the currently pending block and associated state.
// Pending returns the currently pending block and associated state.
func
(
self
*
Miner
)
Pending
()
(
*
types
.
Block
,
*
state
.
StateDB
)
{
func
(
miner
*
Miner
)
Pending
()
(
*
types
.
Block
,
*
state
.
StateDB
)
{
return
self
.
worker
.
pending
()
return
miner
.
worker
.
pending
()
}
}
// PendingBlock returns the currently pending block.
// PendingBlock returns the currently pending block.
...
@@ -174,11 +174,11 @@ func (self *Miner) Pending() (*types.Block, *state.StateDB) {
...
@@ -174,11 +174,11 @@ func (self *Miner) Pending() (*types.Block, *state.StateDB) {
// Note, to access both the pending block and the pending state
// Note, to access both the pending block and the pending state
// simultaneously, please use Pending(), as the pending state can
// simultaneously, please use Pending(), as the pending state can
// change between multiple method calls
// change between multiple method calls
func
(
self
*
Miner
)
PendingBlock
()
*
types
.
Block
{
func
(
miner
*
Miner
)
PendingBlock
()
*
types
.
Block
{
return
self
.
worker
.
pendingBlock
()
return
miner
.
worker
.
pendingBlock
()
}
}
func
(
self
*
Miner
)
SetEtherbase
(
addr
common
.
Address
)
{
func
(
miner
*
Miner
)
SetEtherbase
(
addr
common
.
Address
)
{
self
.
coinbase
=
addr
miner
.
coinbase
=
addr
self
.
worker
.
setEtherbase
(
addr
)
miner
.
worker
.
setEtherbase
(
addr
)
}
}
miner/worker_test.go
View file @
f06ae5ca
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
miner
package
miner
import
(
import
(
"fmt"
"math/big"
"math/big"
"math/rand"
"math/rand"
"sync/atomic"
"sync/atomic"
...
@@ -217,6 +218,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
...
@@ -217,6 +218,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
chain
,
_
:=
core
.
NewBlockChain
(
db2
,
nil
,
b
.
chain
.
Config
(),
engine
,
vm
.
Config
{},
nil
)
chain
,
_
:=
core
.
NewBlockChain
(
db2
,
nil
,
b
.
chain
.
Config
(),
engine
,
vm
.
Config
{},
nil
)
defer
chain
.
Stop
()
defer
chain
.
Stop
()
loopErr
:=
make
(
chan
error
)
newBlock
:=
make
(
chan
struct
{})
newBlock
:=
make
(
chan
struct
{})
listenNewBlock
:=
func
()
{
listenNewBlock
:=
func
()
{
sub
:=
w
.
mux
.
Subscribe
(
core
.
NewMinedBlockEvent
{})
sub
:=
w
.
mux
.
Subscribe
(
core
.
NewMinedBlockEvent
{})
...
@@ -226,7 +228,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
...
@@ -226,7 +228,7 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
block
:=
item
.
Data
.
(
core
.
NewMinedBlockEvent
)
.
Block
block
:=
item
.
Data
.
(
core
.
NewMinedBlockEvent
)
.
Block
_
,
err
:=
chain
.
InsertChain
([]
*
types
.
Block
{
block
})
_
,
err
:=
chain
.
InsertChain
([]
*
types
.
Block
{
block
})
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"F
ailed to insert new mined block:%d, error:%v"
,
block
.
NumberU64
(),
err
)
loopErr
<-
fmt
.
Errorf
(
"f
ailed to insert new mined block:%d, error:%v"
,
block
.
NumberU64
(),
err
)
}
}
newBlock
<-
struct
{}{}
newBlock
<-
struct
{}{}
}
}
...
@@ -244,6 +246,8 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
...
@@ -244,6 +246,8 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
b
.
PostChainEvents
([]
interface
{}{
core
.
ChainSideEvent
{
Block
:
b
.
newRandomUncle
()}})
b
.
PostChainEvents
([]
interface
{}{
core
.
ChainSideEvent
{
Block
:
b
.
newRandomUncle
()}})
b
.
PostChainEvents
([]
interface
{}{
core
.
ChainSideEvent
{
Block
:
b
.
newRandomUncle
()}})
b
.
PostChainEvents
([]
interface
{}{
core
.
ChainSideEvent
{
Block
:
b
.
newRandomUncle
()}})
select
{
select
{
case
e
:=
<-
loopErr
:
t
.
Fatal
(
e
)
case
<-
newBlock
:
case
<-
newBlock
:
case
<-
time
.
NewTimer
(
3
*
time
.
Second
)
.
C
:
// Worker needs 1s to include new changes.
case
<-
time
.
NewTimer
(
3
*
time
.
Second
)
.
C
:
// Worker needs 1s to include new changes.
t
.
Fatalf
(
"timeout"
)
t
.
Fatalf
(
"timeout"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment