aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cmd/geth/main.go4
-rw-r--r--common/README.md37
-rw-r--r--core/block_processor.go46
-rw-r--r--core/block_processor_test.go4
-rw-r--r--core/chain_makers.go11
-rw-r--r--eth/handler.go2
-rw-r--r--jsre/jsre.go4
-rw-r--r--miner/worker.go2
8 files changed, 62 insertions, 48 deletions
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index f546f89cc..b54d85c22 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -360,7 +360,7 @@ func main() {
}
}
-// MakeExtra resolves extradata for the miner from a flag or returns a default.
+// makeExtra resolves extradata for the miner from a flag or returns a default.
func makeExtra(ctx *cli.Context) []byte {
if ctx.GlobalIsSet(ExtraDataFlag.Name) {
return []byte(ctx.GlobalString(ExtraDataFlag.Name))
@@ -444,6 +444,8 @@ func console(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
+ cfg.ExtraData = makeExtra(ctx)
+
ethereum, err := eth.New(cfg)
if err != nil {
utils.Fatalf("%v", err)
diff --git a/common/README.md b/common/README.md
index 1ed56b71b..adea022b7 100644
--- a/common/README.md
+++ b/common/README.md
@@ -1,49 +1,50 @@
-# ethutil
+# common
[![Build
Status](https://travis-ci.org/ethereum/go-ethereum.png?branch=master)](https://travis-ci.org/ethereum/go-ethereum)
-The ethutil package contains the ethereum utility library.
+The common package contains the ethereum utility library.
# Installation
-`go get github.com/ethereum/ethutil-go`
+As a subdirectory the main go-ethereum repository, you get it with
+`go get github.com/ethereum/go-ethereum`.
# Usage
## RLP (Recursive Linear Prefix) Encoding
-RLP Encoding is an encoding scheme utilized by the Ethereum project. It
-encodes any native value or list to string.
+RLP Encoding is an encoding scheme used by the Ethereum project. It
+encodes any native value or list to a string.
-More in depth information about the Encoding scheme see the [Wiki](http://wiki.ethereum.org/index.php/RLP)
-article.
+More in depth information about the encoding scheme see the
+[Wiki](http://wiki.ethereum.org/index.php/RLP) article.
```go
-rlp := ethutil.Encode("doge")
+rlp := common.Encode("doge")
fmt.Printf("%q\n", rlp) // => "\0x83dog"
-rlp = ethutil.Encode([]interface{}{"dog", "cat"})
+rlp = common.Encode([]interface{}{"dog", "cat"})
fmt.Printf("%q\n", rlp) // => "\0xc8\0x83dog\0x83cat"
-decoded := ethutil.Decode(rlp)
+decoded := common.Decode(rlp)
fmt.Println(decoded) // => ["dog" "cat"]
```
## Patricia Trie
-Patricie Tree is a merkle trie utilized by the Ethereum project.
+Patricie Tree is a merkle trie used by the Ethereum project.
More in depth information about the (modified) Patricia Trie can be
found on the [Wiki](http://wiki.ethereum.org/index.php/Patricia_Tree).
The patricia trie uses a db as backend and could be anything as long as
-it satisfies the Database interface found in `ethutil/db.go`.
+it satisfies the Database interface found in `common/db.go`.
```go
db := NewDatabase()
// db, root
-trie := ethutil.NewTrie(db, "")
+trie := common.NewTrie(db, "")
trie.Put("puppy", "dog")
trie.Put("horse", "stallion")
@@ -65,7 +66,7 @@ all (key, value) bindings.
// ... Create db/trie
// Note that RLP uses interface slices as list
-value := ethutil.Encode([]interface{}{"one", 2, "three", []interface{}{42}})
+value := common.Encode([]interface{}{"one", 2, "three", []interface{}{42}})
// Store the RLP encoded value of the list
trie.Put("mykey", value)
```
@@ -89,7 +90,7 @@ type (e.g. `Slice()` returns []interface{}, `Uint()` return 0, etc).
`Append(v)` appends the value (v) to the current value/list.
```go
-val := ethutil.NewEmptyValue().Append(1).Append("2")
+val := common.NewEmptyValue().Append(1).Append("2")
val.AppendList().Append(3)
```
@@ -110,7 +111,7 @@ val.AppendList().Append(3)
`Byte()` returns the value as a single byte.
```go
-val := ethutil.NewValue([]interface{}{1,"2",[]interface{}{3}})
+val := common.NewValue([]interface{}{1,"2",[]interface{}{3}})
val.Get(0).Uint() // => 1
val.Get(1).Str() // => "2"
s := val.Get(2) // => Value([]interface{}{3})
@@ -122,7 +123,7 @@ s.Get(0).Uint() // => 3
Decoding streams of RLP data is simplified
```go
-val := ethutil.NewValueFromBytes(rlpData)
+val := common.NewValueFromBytes(rlpData)
val.Get(0).Uint()
```
@@ -132,7 +133,7 @@ Encoding from Value to RLP is done with the `Encode` method. The
underlying value can be anything RLP can encode (int, str, lists, bytes)
```go
-val := ethutil.NewValue([]interface{}{1,"2",[]interface{}{3}})
+val := common.NewValue([]interface{}{1,"2",[]interface{}{3}})
rlp := val.Encode()
// Store the rlp data
Store(rlp)
diff --git a/core/block_processor.go b/core/block_processor.go
index 1d3bc6656..238b2db95 100644
--- a/core/block_processor.go
+++ b/core/block_processor.go
@@ -214,7 +214,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
txs := block.Transactions()
// Block validation
- if err = ValidateHeader(sm.Pow, header, parent, false, false); err != nil {
+ if err = ValidateHeader(sm.Pow, header, parent.Header(), false, false); err != nil {
return
}
@@ -338,7 +338,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
return UncleError("uncle[%d](%x)'s parent is not ancestor (%x)", i, hash[:4], uncle.ParentHash[0:4])
}
- if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
+ if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash].Header(), true, true); err != nil {
return ValidationError(fmt.Sprintf("uncle[%d](%x) header invalid: %v", i, hash[:4], err))
}
}
@@ -368,52 +368,50 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
}
// See YP section 4.3.4. "Block Header Validity"
-// Validates a block. Returns an error if the block is invalid.
-func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow, uncle bool) error {
- if big.NewInt(int64(len(block.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
- return fmt.Errorf("Block extra data too long (%d)", len(block.Extra))
+// Validates a header. Returns an error if the header is invalid.
+func ValidateHeader(pow pow.PoW, header *types.Header, parent *types.Header, checkPow, uncle bool) error {
+ if big.NewInt(int64(len(header.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
+ return fmt.Errorf("Header extra data too long (%d)", len(header.Extra))
}
if uncle {
- if block.Time.Cmp(common.MaxBig) == 1 {
+ if header.Time.Cmp(common.MaxBig) == 1 {
return BlockTSTooBigErr
}
} else {
- if block.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
+ if header.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
return BlockFutureErr
}
}
- if block.Time.Cmp(parent.Time()) != 1 {
+ if header.Time.Cmp(parent.Time) != 1 {
return BlockEqualTSErr
}
- expd := CalcDifficulty(block.Time.Uint64(), parent.Time().Uint64(), parent.Number(), parent.Difficulty())
- if expd.Cmp(block.Difficulty) != 0 {
- return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
+ expd := CalcDifficulty(header.Time.Uint64(), parent.Time.Uint64(), parent.Number, parent.Difficulty)
+ if expd.Cmp(header.Difficulty) != 0 {
+ return fmt.Errorf("Difficulty check failed for header %v, %v", header.Difficulty, expd)
}
- var a, b *big.Int
- a = parent.GasLimit()
- a = a.Sub(a, block.GasLimit)
+ a := new(big.Int).Set(parent.GasLimit)
+ a = a.Sub(a, header.GasLimit)
a.Abs(a)
- b = parent.GasLimit()
+ b := new(big.Int).Set(parent.GasLimit)
b = b.Div(b, params.GasLimitBoundDivisor)
- if !(a.Cmp(b) < 0) || (block.GasLimit.Cmp(params.MinGasLimit) == -1) {
- return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b)
+ if !(a.Cmp(b) < 0) || (header.GasLimit.Cmp(params.MinGasLimit) == -1) {
+ return fmt.Errorf("GasLimit check failed for header %v (%v > %v)", header.GasLimit, a, b)
}
- num := parent.Number()
- num.Sub(block.Number, num)
+ num := new(big.Int).Set(parent.Number)
+ num.Sub(header.Number, num)
if num.Cmp(big.NewInt(1)) != 0 {
return BlockNumberErr
}
if checkPow {
- // Verify the nonce of the block. Return an error if it's not valid
- if !pow.Verify(types.NewBlockWithHeader(block)) {
- return ValidationError("Block's nonce is invalid (= %x)", block.Nonce)
+ // Verify the nonce of the header. Return an error if it's not valid
+ if !pow.Verify(types.NewBlockWithHeader(header)) {
+ return ValidationError("Header's nonce is invalid (= %x)", header.Nonce)
}
}
-
return nil
}
diff --git a/core/block_processor_test.go b/core/block_processor_test.go
index e0b2d3313..538cf4ee5 100644
--- a/core/block_processor_test.go
+++ b/core/block_processor_test.go
@@ -48,13 +48,13 @@ func TestNumber(t *testing.T) {
statedb := state.New(chain.Genesis().Root(), chain.chainDb)
header := makeHeader(chain.Genesis(), statedb)
header.Number = big.NewInt(3)
- err := ValidateHeader(pow, header, chain.Genesis(), false, false)
+ err := ValidateHeader(pow, header, chain.Genesis().Header(), false, false)
if err != BlockNumberErr {
t.Errorf("expected block number error, got %q", err)
}
header = makeHeader(chain.Genesis(), statedb)
- err = ValidateHeader(pow, header, chain.Genesis(), false, false)
+ err = ValidateHeader(pow, header, chain.Genesis().Header(), false, false)
if err == BlockNumberErr {
t.Errorf("didn't expect block number error")
}
diff --git a/core/chain_makers.go b/core/chain_makers.go
index d3b7c42b6..70233438d 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -131,6 +131,17 @@ func (b *BlockGen) PrevBlock(index int) *types.Block {
return b.chain[index]
}
+// OffsetTime modifies the time instance of a block, implicitly changing its
+// associated difficulty. It's useful to test scenarios where forking is not
+// tied to chain length directly.
+func (b *BlockGen) OffsetTime(seconds int64) {
+ b.header.Time.Add(b.header.Time, new(big.Int).SetInt64(seconds))
+ if b.header.Time.Cmp(b.parent.Header().Time) <= 0 {
+ panic("block time out of range")
+ }
+ b.header.Difficulty = CalcDifficulty(b.header.Time.Uint64(), b.parent.Time().Uint64(), b.parent.Number(), b.parent.Difficulty())
+}
+
// GenerateChain creates a chain of n blocks. The first block's
// parent will be the provided parent. db is used to store
// intermediate states and should contain the parent's state trie.
diff --git a/eth/handler.go b/eth/handler.go
index 0a06ea335..52c9c4151 100644
--- a/eth/handler.go
+++ b/eth/handler.go
@@ -119,7 +119,7 @@ func NewProtocolManager(networkId int, mux *event.TypeMux, txpool txPool, pow po
manager.downloader = downloader.New(manager.eventMux, manager.chainman.HasBlock, manager.chainman.GetBlock, manager.chainman.CurrentBlock, manager.chainman.GetTd, manager.chainman.InsertChain, manager.removePeer)
validator := func(block *types.Block, parent *types.Block) error {
- return core.ValidateHeader(pow, block.Header(), parent, true, false)
+ return core.ValidateHeader(pow, block.Header(), parent.Header(), true, false)
}
heighter := func() uint64 {
return manager.chainman.CurrentBlock().NumberU64()
diff --git a/jsre/jsre.go b/jsre/jsre.go
index 0db9e33fc..af7d507c6 100644
--- a/jsre/jsre.go
+++ b/jsre/jsre.go
@@ -154,7 +154,9 @@ loop:
if err != nil {
fmt.Println("js error:", err, arguments)
}
- if timer.interval {
+
+ _, inreg := registry[timer] // when clearInterval is called from within the callback don't reset it
+ if timer.interval && inreg {
timer.timer.Reset(timer.duration)
} else {
delete(registry, timer)
diff --git a/miner/worker.go b/miner/worker.go
index 96c42c292..22d0b9b6e 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -279,7 +279,7 @@ func (self *worker) wait() {
glog.V(logger.Error).Infoln("Invalid block found during mining")
continue
}
- if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true, false); err != nil && err != core.BlockFutureErr {
+ if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent.Header(), true, false); err != nil && err != core.BlockFutureErr {
glog.V(logger.Error).Infoln("Invalid header on mined block:", err)
continue
}