From 73f925db119d000360ad08318a502d4bdce77220 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Wed, 4 Mar 2015 23:58:09 -0500 Subject: [PATCH] Switching Nonce to Uint64 Everywhere - Fixed types.block to take MixDigest and SeedHash as part of its constructor - Added MixDigest and SeedHash to Genesis Block --- core/block_processor.go | 4 ++-- core/chain_makers.go | 6 +++--- core/chain_manager.go | 4 +++- core/genesis.go | 2 +- core/types/block.go | 10 ++++++---- miner/agent.go | 2 +- miner/worker.go | 6 +++--- pow/block.go | 2 +- pow/ezp/pow.go | 18 ++++++++++-------- pow/pow.go | 2 +- 10 files changed, 31 insertions(+), 25 deletions(-) diff --git a/core/block_processor.go b/core/block_processor.go index 3123511f9e..2f021bff42 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -274,7 +274,7 @@ func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error { // Verify the nonce of the block. Return an error if it's not valid 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 (= %v)", block.Header().Nonce) } return nil @@ -302,7 +302,7 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren } 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 (= %v)", uncle.Nonce) } r := new(big.Int) diff --git a/core/chain_makers.go b/core/chain_makers.go index 2c36b892e5..a6a3d93c1b 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -13,8 +13,8 @@ import ( // So we can generate blocks easily type FakePow struct{} -func (f FakePow) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) { - return nil, nil, nil +func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) { + return 0, nil, nil } func (f FakePow) Verify(block pow.Block) bool { return true } func (f FakePow) GetHashrate() int64 { return 0 } @@ -54,7 +54,7 @@ func NewCanonical(n int, db ethutil.Database) (*BlockProcessor, error) { // block time is fixed at 10 seconds 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, nil, nil) block.SetUncles(nil) block.SetTransactions(nil) block.SetReceipts(nil) diff --git a/core/chain_manager.go b/core/chain_manager.go index 959bfd3986..ee161d21a0 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -192,8 +192,10 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block { coinbase, root, ethutil.BigPow(2, 32), + "", + 0, nil, - "") + nil) block.SetUncles(nil) block.SetTransactions(nil) block.SetReceipts(nil) diff --git a/core/genesis.go b/core/genesis.go index decffc5418..e5ed21b8a7 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -23,7 +23,7 @@ var EmptyShaList = crypto.Sha3(ethutil.Encode([]interface{}{})) var EmptyListRoot = crypto.Sha3(ethutil.Encode("")) 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, []byte("nothing more depraved than a man"), []byte("in the depths of an ether binge~")) genesis.Header().Number = ethutil.Big0 genesis.Header().GasLimit = big.NewInt(1000000) genesis.Header().GasUsed = ethutil.Big0 diff --git a/core/types/block.go b/core/types/block.go index a37038f739..7bf5d83fdf 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -40,7 +40,7 @@ type Header struct { // Extra data Extra string // Block Nonce for verification - Nonce ethutil.Bytes + Nonce uint64 // Mix digest for quick checking to prevent DOS MixDigest ethutil.Bytes // SeedHash used for light client verification @@ -94,13 +94,15 @@ type Block struct { 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, extra string, nonce uint64, mixDigest []byte, seedHash []byte) *Block { header := &Header{ Root: root, ParentHash: parentHash, Coinbase: coinbase, Difficulty: difficulty, Nonce: nonce, + MixDigest: mixDigest, + SeedHash: seedHash, Time: uint64(time.Now().Unix()), Extra: extra, GasUsed: new(big.Int), @@ -195,7 +197,7 @@ func (self *Block) Number() *big.Int { return self.header.Number } func (self *Block) NumberU64() uint64 { return self.header.Number.Uint64() } func (self *Block) MixDigest() []byte { return self.header.MixDigest } 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) Coinbase() []byte { return self.header.Coinbase } func (self *Block) Time() int64 { return int64(self.header.Time) } @@ -267,7 +269,7 @@ func (self *Header) String() string { GasUsed: %v Time: %v Extra: %v - Nonce: %x + Nonce: %v `, self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.Nonce) } diff --git a/miner/agent.go b/miner/agent.go index 5b2bfdb06c..bdf211b37a 100644 --- a/miner/agent.go +++ b/miner/agent.go @@ -70,7 +70,7 @@ done: func (self *CpuMiner) mine(block *types.Block) { minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index) nonce, mixDigest, seedHash := self.pow.Search(block, self.quitCurrentOp) - if nonce != nil { + if mixDigest != nil { self.returnCh <- Work{block.Number().Uint64(), nonce, mixDigest, seedHash} } } diff --git a/miner/worker.go b/miner/worker.go index ce4a5b6bfa..765b0294a4 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -47,7 +47,7 @@ func env(block *types.Block, eth core.Backend) *environment { type Work struct { Number uint64 - Nonce []byte + Nonce uint64 MixDigest []byte SeedHash []byte } @@ -150,7 +150,7 @@ func (self *worker) wait() { for work := range self.recv { // Someone Successfully Mined! block := self.current.block - if block.Number().Uint64() == work.Number && block.Nonce() == nil { + if block.Number().Uint64() == work.Number && block.MixDigest() == nil { self.current.block.Header().Nonce = work.Nonce self.current.block.Header().MixDigest = work.MixDigest self.current.block.Header().SeedHash = work.SeedHash @@ -239,7 +239,7 @@ func (self *worker) commitUncle(uncle *types.Header) error { } if !self.pow.Verify(types.NewBlockWithHeader(uncle)) { - return core.ValidationError("Uncle's nonce is invalid (= %v)", ethutil.Bytes2Hex(uncle.Nonce)) + return core.ValidationError("Invalid Uncle: {\"nonce\" : %v, \"MixDigest\" : %v, \"SeedHash\" : %v}", uncle.Nonce, ethutil.Bytes2Hex(uncle.MixDigest), ethutil.Bytes2Hex(uncle.SeedHash)) } uncleAccount := self.current.state.GetAccount(uncle.Coinbase) diff --git a/pow/block.go b/pow/block.go index eb07bc86e7..54cae4f19e 100644 --- a/pow/block.go +++ b/pow/block.go @@ -8,7 +8,7 @@ import ( type Block interface { Difficulty() *big.Int HashNoNonce() []byte - Nonce() []byte + Nonce() uint64 MixDigest() []byte SeedHash() []byte NumberU64() uint64 diff --git a/pow/ezp/pow.go b/pow/ezp/pow.go index 49854c3d02..15d62ee938 100644 --- a/pow/ezp/pow.go +++ b/pow/ezp/pow.go @@ -1,11 +1,11 @@ package ezp import ( + "encoding/binary" "math/big" "math/rand" "time" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" @@ -32,7 +32,7 @@ func (pow *EasyPow) Turbo(on bool) { 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())) hash := block.HashNoNonce() diff := block.Difficulty() @@ -57,7 +57,7 @@ empty: for { select { case <-stop: - return nil, nil, nil + return 0, nil, nil default: i++ @@ -65,9 +65,9 @@ empty: hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000 pow.HashRate = int64(hashes) - sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes()) - if verify(hash, diff, sha) { - return sha, nil, nil + nonce := uint64(r.Int63()) + if verify(hash, diff, nonce) { + return nonce, nil, nil } } @@ -81,10 +81,12 @@ func (pow *EasyPow) Verify(block pow.Block) bool { 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() - d := append(hash, nonce...) + nonce_buf := make([]byte, 8) + binary.PutUvarint(nonce_buf, nonce) + d := append(hash, nonce_buf...) sha.Write(d) verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff) diff --git a/pow/pow.go b/pow/pow.go index 11aecbd6bc..3908e5f761 100644 --- a/pow/pow.go +++ b/pow/pow.go @@ -1,7 +1,7 @@ package pow 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 GetHashrate() int64 Turbo(bool)