Changed nonce to a uint64

This commit is contained in:
obscuren 2015-03-03 21:04:31 +01:00
parent e9f1e868e2
commit 26de12d9bf
10 changed files with 30 additions and 25 deletions

View File

@ -277,7 +277,7 @@ func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error {
// Verify the nonce of the block. Return an error if it's not valid // Verify the nonce of the block. Return an error if it's not valid
if !sm.Pow.Verify(block) { if !sm.Pow.Verify(block) {
return ValidationError("Block's nonce is invalid (= %v)", ethutil.Bytes2Hex(block.Header().Nonce)) return ValidationError("Block's nonce is invalid (= %x)", block.Header().Nonce)
} }
return nil return nil
@ -305,7 +305,7 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
} }
if !sm.Pow.Verify(types.NewBlockWithHeader(uncle)) { if !sm.Pow.Verify(types.NewBlockWithHeader(uncle)) {
return ValidationError("Uncle's nonce is invalid (= %v)", ethutil.Bytes2Hex(uncle.Nonce)) return ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
} }
r := new(big.Int) r := new(big.Int)

View File

@ -14,8 +14,8 @@ import (
// So we can generate blocks easily // So we can generate blocks easily
type FakePow struct{} type FakePow struct{}
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) { func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
return nil, nil, nil return 0, nil, nil
} }
func (f FakePow) Verify(block pow.Block) bool { return true } func (f FakePow) Verify(block pow.Block) bool { return true }
func (f FakePow) GetHashrate() int64 { return 0 } func (f FakePow) GetHashrate() int64 { return 0 }
@ -55,7 +55,7 @@ func NewCanonical(n int, db ethutil.Database) (*BlockProcessor, error) {
// block time is fixed at 10 seconds // block time is fixed at 10 seconds
func newBlockFromParent(addr []byte, parent *types.Block) *types.Block { func newBlockFromParent(addr []byte, parent *types.Block) *types.Block {
block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), nil, "") block := types.NewBlock(parent.Hash(), addr, parent.Root(), ethutil.BigPow(2, 32), 0, "")
block.SetUncles(nil) block.SetUncles(nil)
block.SetTransactions(nil) block.SetTransactions(nil)
block.SetReceipts(nil) block.SetReceipts(nil)

View File

@ -197,7 +197,7 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block {
coinbase, coinbase,
root, root,
ethutil.BigPow(2, 32), ethutil.BigPow(2, 32),
nil, 0,
"") "")
block.SetUncles(nil) block.SetUncles(nil)
block.SetTransactions(nil) block.SetTransactions(nil)

View File

@ -23,7 +23,7 @@ var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{}))
var EmptyListRoot = crypto.Sha3(ethutil.Encode("")) var EmptyListRoot = crypto.Sha3(ethutil.Encode(""))
func GenesisBlock(db ethutil.Database) *types.Block { func GenesisBlock(db ethutil.Database) *types.Block {
genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(131072), crypto.Sha3(big.NewInt(42).Bytes()), "") genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, big.NewInt(131072), 42, "")
genesis.Header().Number = ethutil.Big0 genesis.Header().Number = ethutil.Big0
genesis.Header().GasLimit = big.NewInt(1000000) genesis.Header().GasLimit = big.NewInt(1000000)
genesis.Header().GasUsed = ethutil.Big0 genesis.Header().GasUsed = ethutil.Big0

View File

@ -39,8 +39,8 @@ type Header struct {
Time uint64 Time uint64
// Extra data // Extra data
Extra string Extra string
// Block Nonce for verification // Nonce
Nonce ethutil.Bytes Nonce uint64
// Mix digest for quick checking to prevent DOS // Mix digest for quick checking to prevent DOS
MixDigest ethutil.Bytes MixDigest ethutil.Bytes
// SeedHash used for light client verification // SeedHash used for light client verification
@ -94,7 +94,7 @@ type Block struct {
Reward *big.Int Reward *big.Int
} }
func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce []byte, extra string) *Block { func NewBlock(parentHash []byte, coinbase []byte, root []byte, difficulty *big.Int, nonce uint64, extra string) *Block {
header := &Header{ header := &Header{
Root: root, Root: root,
ParentHash: parentHash, ParentHash: parentHash,
@ -195,7 +195,7 @@ func (self *Block) Number() *big.Int { return self.header.Number }
func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() } func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() }
func (self *Block) MixDigest() []byte { return self.header.MixDigest } func (self *Block) MixDigest() []byte { return self.header.MixDigest }
func (self *Block) SeedHash() []byte { return self.header.SeedHash } func (self *Block) SeedHash() []byte { return self.header.SeedHash }
func (self *Block) Nonce() []byte { return self.header.Nonce } func (self *Block) Nonce() uint64 { return self.header.Nonce }
func (self *Block) Bloom() []byte { return self.header.Bloom } func (self *Block) Bloom() []byte { return self.header.Bloom }
func (self *Block) Coinbase() []byte { return self.header.Coinbase } func (self *Block) Coinbase() []byte { return self.header.Coinbase }
func (self *Block) Time() int64 { return int64(self.header.Time) } func (self *Block) Time() int64 { return int64(self.header.Time) }
@ -267,7 +267,7 @@ func (self *Header) String() string {
GasUsed: %v GasUsed: %v
Time: %v Time: %v
Extra: %v Extra: %v
Nonce: %x Nonce: %d
MixDigest: %x MixDigest: %x
SeedHash: %x SeedHash: %x

View File

@ -75,7 +75,7 @@ done:
func (self *CpuMiner) mine(block *types.Block) { func (self *CpuMiner) mine(block *types.Block) {
minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index) minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index)
nonce, mixDigest, seedHash := self.pow.Search(block, self.quitCurrentOp) nonce, mixDigest, seedHash := self.pow.Search(block, self.quitCurrentOp)
if nonce != nil { if nonce != 0 {
self.returnCh <- Work{block.Number().Uint64(), nonce, mixDigest, seedHash} self.returnCh <- Work{block.Number().Uint64(), nonce, mixDigest, seedHash}
} }
} }

View File

@ -47,7 +47,7 @@ func env(block *types.Block, eth core.Backend) *environment {
type Work struct { type Work struct {
Number uint64 Number uint64
Nonce []byte Nonce uint64
MixDigest []byte MixDigest []byte
SeedHash []byte SeedHash []byte
} }
@ -150,7 +150,7 @@ func (self *worker) wait() {
for work := range self.recv { for work := range self.recv {
// Someone Successfully Mined! // Someone Successfully Mined!
block := self.current.block block := self.current.block
if block.Number().Uint64() == work.Number && block.Nonce() == nil { if block.Number().Uint64() == work.Number && block.Nonce() == 0 {
self.current.block.Header().Nonce = work.Nonce self.current.block.Header().Nonce = work.Nonce
self.current.block.Header().MixDigest = work.MixDigest self.current.block.Header().MixDigest = work.MixDigest
self.current.block.Header().SeedHash = work.SeedHash self.current.block.Header().SeedHash = work.SeedHash
@ -242,7 +242,7 @@ func (self *worker) commitUncle(uncle *types.Header) error {
} }
if !self.pow.Verify(types.NewBlockWithHeader(uncle)) { if !self.pow.Verify(types.NewBlockWithHeader(uncle)) {
return core.ValidationError("Uncle's nonce is invalid (= %v)", ethutil.Bytes2Hex(uncle.Nonce)) return core.ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
} }
uncleAccount := self.current.state.GetAccount(uncle.Coinbase) uncleAccount := self.current.state.GetAccount(uncle.Coinbase)

View File

@ -1,14 +1,15 @@
package pow package pow
import ( import (
"github.com/ethereum/go-ethereum/core/types"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/core/types"
) )
type Block interface { type Block interface {
Difficulty() *big.Int Difficulty() *big.Int
HashNoNonce() []byte HashNoNonce() []byte
Nonce() []byte Nonce() uint64
MixDigest() []byte MixDigest() []byte
SeedHash() []byte SeedHash() []byte
NumberU64() uint64 NumberU64() uint64

View File

@ -1,11 +1,11 @@
package ezp package ezp
import ( import (
"encoding/binary"
"math/big" "math/big"
"math/rand" "math/rand"
"time" "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
@ -32,7 +32,7 @@ func (pow *EasyPow) Turbo(on bool) {
pow.turbo = on pow.turbo = on
} }
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) { func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
hash := block.HashNoNonce() hash := block.HashNoNonce()
diff := block.Difficulty() diff := block.Difficulty()
@ -57,7 +57,7 @@ empty:
for { for {
select { select {
case <-stop: case <-stop:
return nil, nil, nil return 0, nil, nil
default: default:
i++ i++
@ -65,7 +65,7 @@ empty:
hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000 hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000
pow.HashRate = int64(hashes) pow.HashRate = int64(hashes)
sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes()) sha := uint64(r.Int63())
if verify(hash, diff, sha) { if verify(hash, diff, sha) {
return sha, nil, nil return sha, nil, nil
} }
@ -75,16 +75,20 @@ empty:
time.Sleep(20 * time.Microsecond) time.Sleep(20 * time.Microsecond)
} }
} }
return 0, nil, nil
} }
func (pow *EasyPow) Verify(block pow.Block) bool { func (pow *EasyPow) Verify(block pow.Block) bool {
return Verify(block) return Verify(block)
} }
func verify(hash []byte, diff *big.Int, nonce []byte) bool { func verify(hash []byte, diff *big.Int, nonce uint64) bool {
sha := sha3.NewKeccak256() sha := sha3.NewKeccak256()
d := append(hash, nonce...) n := make([]byte, 8)
binary.PutUvarint(n, nonce)
d := append(hash, n...)
sha.Write(d) sha.Write(d)
verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff) verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff)

View File

@ -1,7 +1,7 @@
package pow package pow
type PoW interface { type PoW interface {
Search(block Block, stop <-chan struct{}) ([]byte, []byte, []byte) Search(block Block, stop <-chan struct{}) (uint64, []byte, []byte)
Verify(block Block) bool Verify(block Block) bool
GetHashrate() int64 GetHashrate() int64
Turbo(bool) Turbo(bool)