Skip to content

Commit 26de12d

Browse files
committed
Changed nonce to a uint64
1 parent e9f1e86 commit 26de12d

10 files changed

Lines changed: 30 additions & 25 deletions

File tree

‎core/block_processor.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error {
277277

278278
// Verify the nonce of the block. Return an error if it's not valid
279279
if !sm.Pow.Verify(block) {
280-
return ValidationError("Block's nonce is invalid (= %v)", ethutil.Bytes2Hex(block.Header().Nonce))
280+
return ValidationError("Block's nonce is invalid (= %x)", block.Header().Nonce)
281281
}
282282

283283
return nil
@@ -305,7 +305,7 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
305305
}
306306

307307
if !sm.Pow.Verify(types.NewBlockWithHeader(uncle)) {
308-
return ValidationError("Uncle's nonce is invalid (= %v)", ethutil.Bytes2Hex(uncle.Nonce))
308+
return ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
309309
}
310310

311311
r := new(big.Int)

‎core/chain_makers.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
// So we can generate blocks easily
1515
type FakePow struct{}
1616

17-
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) {
18-
return nil, nil, nil
17+
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
18+
return 0, nil, nil
1919
}
2020
func (f FakePow) Verify(block pow.Block) bool { return true }
2121
func (f FakePow) GetHashrate() int64 { return 0 }
@@ -55,7 +55,7 @@ func NewCanonical(n int, db ethutil.Database) (*BlockProcessor, error) {
5555

5656
// block time is fixed at 10 seconds
5757
func newBlockFromParent(addr []byte, parent *types.Block) *types.Block {
58-
block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), nil, "")
58+
block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), 0, "")
5959
block.SetUncles(nil)
6060
block.SetTransactions(nil)
6161
block.SetReceipts(nil)

‎core/chain_manager.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block {
197197
coinbase,
198198
root,
199199
ethutil.BigPow(2, 32),
200-
nil,
200+
0,
201201
"")
202202
block.SetUncles(nil)
203203
block.SetTransactions(nil)

‎core/genesis.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{}))
2323
var EmptyListRoot = crypto.Sha3(ethutil.Encode(""))
2424

2525
func GenesisBlock(db ethutil.Database) *types.Block {
26-
genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(131072), crypto.Sha3(big.NewInt(42).Bytes()), "")
26+
genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(131072), 42, "")
2727
genesis.Header().Number = ethutil.Big0
2828
genesis.Header().GasLimit = big.NewInt(1000000)
2929
genesis.Header().GasUsed = ethutil.Big0

‎core/types/block.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type Header struct {
3939
Time uint64
4040
// Extra data
4141
Extra string
42-
// Block Nonce for verification
43-
Nonce ethutil.Bytes
42+
// Nonce
43+
Nonce uint64
4444
// Mix digest for quick checking to prevent DOS
4545
MixDigest ethutil.Bytes
4646
// SeedHash used for light client verification
@@ -94,7 +94,7 @@ type Block struct {
9494
Reward *big.Int
9595
}
9696

97-
func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce []byte, extra string) *Block {
97+
func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce uint64, extra string) *Block {
9898
header := &Header{
9999
Root: root,
100100
ParentHash: parentHash,
@@ -195,7 +195,7 @@ func (self *Block) Number() *big.Int { return self.header.Number }
195195
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
196196
func (self *Block) MixDigest() []byte { return self.header.MixDigest }
197197
func (self *Block) SeedHash() []byte { return self.header.SeedHash }
198-
func (self *Block) Nonce() []byte { return self.header.Nonce }
198+
func (self *Block) Nonce() uint64 { return self.header.Nonce }
199199
func (self *Block) Bloom() []byte { return self.header.Bloom }
200200
func (self *Block) Coinbase() []byte { return self.header.Coinbase }
201201
func (self *Block) Time() int64 { return int64(self.header.Time) }
@@ -267,7 +267,7 @@ func (self *Header) String() string {
267267
GasUsed: %v
268268
Time: %v
269269
Extra: %v
270-
Nonce: %x
270+
Nonce: %d
271271
MixDigest: %x
272272
SeedHash: %x
273273

‎miner/agent.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ done:
7575
func (self *CpuMiner) mine(block *types.Block) {
7676
minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index)
7777
nonce, mixDigest, seedHash := self.pow.Search(block, self.quitCurrentOp)
78-
if nonce != nil {
78+
if nonce != 0 {
7979
self.returnCh <- Work{block.Number().Uint64(), nonce, mixDigest, seedHash}
8080
}
8181
}

‎miner/worker.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func env(block *types.Block, eth core.Backend) *environment {
4747

4848
type Work struct {
4949
Number uint64
50-
Nonce []byte
50+
Nonce uint64
5151
MixDigest []byte
5252
SeedHash []byte
5353
}
@@ -150,7 +150,7 @@ func (self *worker) wait() {
150150
for work := range self.recv {
151151
// Someone Successfully Mined!
152152
block := self.current.block
153-
if block.Number().Uint64() == work.Number && block.Nonce() == nil {
153+
if block.Number().Uint64() == work.Number && block.Nonce() == 0 {
154154
self.current.block.Header().Nonce = work.Nonce
155155
self.current.block.Header().MixDigest = work.MixDigest
156156
self.current.block.Header().SeedHash = work.SeedHash
@@ -242,7 +242,7 @@ func (self *worker) commitUncle(uncle *types.Header) error {
242242
}
243243

244244
if !self.pow.Verify(types.NewBlockWithHeader(uncle)) {
245-
return core.ValidationError("Uncle's nonce is invalid (= %v)", ethutil.Bytes2Hex(uncle.Nonce))
245+
return core.ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
246246
}
247247

248248
uncleAccount := self.current.state.GetAccount(uncle.Coinbase)

‎pow/block.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package pow
22

33
import (
4-
"github.com/ethereum/go-ethereum/core/types"
54
"math/big"
5+
6+
"github.com/ethereum/go-ethereum/core/types"
67
)
78

89
type Block interface {
910
Difficulty() *big.Int
1011
HashNoNonce() []byte
11-
Nonce() []byte
12+
Nonce() uint64
1213
MixDigest() []byte
1314
SeedHash() []byte
1415
NumberU64() uint64

‎pow/ezp/pow.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package ezp
22

33
import (
4+
"encoding/binary"
45
"math/big"
56
"math/rand"
67
"time"
78

8-
"github.com/ethereum/go-ethereum/crypto"
99
"github.com/ethereum/go-ethereum/crypto/sha3"
1010
"github.com/ethereum/go-ethereum/ethutil"
1111
"github.com/ethereum/go-ethereum/logger"
@@ -32,7 +32,7 @@ func (pow *EasyPow) Turbo(on bool) {
3232
pow.turbo = on
3333
}
3434

35-
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) {
35+
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
3636
r := rand.New(rand.NewSource(time.Now().UnixNano()))
3737
hash := block.HashNoNonce()
3838
diff := block.Difficulty()
@@ -57,15 +57,15 @@ empty:
5757
for {
5858
select {
5959
case <-stop:
60-
return nil, nil, nil
60+
return 0, nil, nil
6161
default:
6262
i++
6363

6464
elapsed := time.Now().UnixNano() - start
6565
hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000
6666
pow.HashRate = int64(hashes)
6767

68-
sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes())
68+
sha := uint64(r.Int63())
6969
if verify(hash, diff, sha) {
7070
return sha, nil, nil
7171
}
@@ -75,16 +75,20 @@ empty:
7575
time.Sleep(20 * time.Microsecond)
7676
}
7777
}
78+
79+
return 0, nil, nil
7880
}
7981

8082
func (pow *EasyPow) Verify(block pow.Block) bool {
8183
return Verify(block)
8284
}
8385

84-
func verify(hash []byte, diff *big.Int, nonce []byte) bool {
86+
func verify(hash []byte, diff *big.Int, nonce uint64) bool {
8587
sha := sha3.NewKeccak256()
8688

87-
d := append(hash, nonce...)
89+
n := make([]byte, 8)
90+
binary.PutUvarint(n, nonce)
91+
d := append(hash, n...)
8892
sha.Write(d)
8993

9094
verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff)

‎pow/pow.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package pow
22

33
type PoW interface {
4-
Search(block Block, stop <-chan struct{}) ([]byte, []byte, []byte)
4+
Search(block Block, stop <-chan struct{}) (uint64, []byte, []byte)
55
Verify(block Block) bool
66
GetHashrate() int64
77
Turbo(bool)

0 commit comments

Comments
 (0)