aboutsummaryrefslogtreecommitdiffstats
path: root/chain
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-11-19 19:25:52 +0800
committerobscuren <geffobscura@gmail.com>2014-11-19 19:25:52 +0800
commit14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6 (patch)
tree7d8b81beb6285eab7dfc8772b35f1d9d8ce1b186 /chain
parent437d79f09418b24693b4e3fc4d2f88e291b5340f (diff)
downloaddexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.tar
dexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.tar.gz
dexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.tar.bz2
dexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.tar.lz
dexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.tar.xz
dexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.tar.zst
dexon-14e2e488fdf0f4d6ed1a5a48ffbbe883faa7edb6.zip
Added `chain` tests & minor fixes
* Fork tests (equal and larger chains) * `chain.link` fields are now exported * moved debug function from state to dump.go
Diffstat (limited to 'chain')
-rw-r--r--chain/chain_manager.go26
-rw-r--r--chain/chain_manager_test.go115
2 files changed, 128 insertions, 13 deletions
diff --git a/chain/chain_manager.go b/chain/chain_manager.go
index 970fa5377..11e16fa7d 100644
--- a/chain/chain_manager.go
+++ b/chain/chain_manager.go
@@ -191,8 +191,8 @@ func (self *ChainManager) GetBlock(hash []byte) *types.Block {
if self.workingChain != nil {
// Check the temp chain
for e := self.workingChain.Front(); e != nil; e = e.Next() {
- if bytes.Compare(e.Value.(*link).block.Hash(), hash) == 0 {
- return e.Value.(*link).block
+ if bytes.Compare(e.Value.(*link).Block.Hash(), hash) == 0 {
+ return e.Value.(*link).Block
}
}
}
@@ -275,15 +275,15 @@ func (self *ChainManager) InsertChain(chain *BlockChain, call func(*types.Block,
for e := chain.Front(); e != nil; e = e.Next() {
link := e.Value.(*link)
- self.add(link.block)
- self.SetTotalDifficulty(link.td)
+ self.add(link.Block)
+ self.SetTotalDifficulty(link.Td)
- call(link.block, link.messages)
+ call(link.Block, link.Messages)
}
b, e := chain.Front(), chain.Back()
if b != nil && e != nil {
- front, back := b.Value.(*link).block, e.Value.(*link).block
+ front, back := b.Value.(*link).Block, e.Value.(*link).Block
chainlogger.Infof("Imported %d blocks. #%v (%x) / %#v (%x)", chain.Len(), front.Number, front.Hash()[0:4], back.Number, back.Hash()[0:4])
}
}
@@ -295,7 +295,7 @@ func (self *ChainManager) TestChain(chain *BlockChain) (td *big.Int, err error)
for e := chain.Front(); e != nil; e = e.Next() {
var (
l = e.Value.(*link)
- block = l.block
+ block = l.Block
parent = self.GetBlock(block.PrevHash)
)
@@ -314,8 +314,8 @@ func (self *ChainManager) TestChain(chain *BlockChain) (td *big.Int, err error)
err = fmt.Errorf("incoming chain failed %v\n", err)
return
}
- l.td = td
- l.messages = messages
+ l.Td = td
+ l.Messages = messages
}
if td.Cmp(self.TD) <= 0 {
@@ -329,9 +329,9 @@ func (self *ChainManager) TestChain(chain *BlockChain) (td *big.Int, err error)
}
type link struct {
- block *types.Block
- messages state.Messages
- td *big.Int
+ Block *types.Block
+ Messages state.Messages
+ Td *big.Int
}
type BlockChain struct {
@@ -351,7 +351,7 @@ func NewChain(blocks types.Blocks) *BlockChain {
func (self *BlockChain) RlpEncode() []byte {
dat := make([]interface{}, 0)
for e := self.Front(); e != nil; e = e.Next() {
- dat = append(dat, e.Value.(*link).block.RlpData())
+ dat = append(dat, e.Value.(*link).Block.RlpData())
}
return ethutil.Encode(dat)
diff --git a/chain/chain_manager_test.go b/chain/chain_manager_test.go
index fef1d2010..0314914a9 100644
--- a/chain/chain_manager_test.go
+++ b/chain/chain_manager_test.go
@@ -1 +1,116 @@
package chain
+
+import (
+ "fmt"
+ "math/big"
+ "testing"
+ "time"
+
+ "github.com/ethereum/go-ethereum/chain/types"
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/ethutil"
+ "github.com/ethereum/go-ethereum/state"
+)
+
+var TD *big.Int
+
+func init() {
+ ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
+ ethutil.Config.Db, _ = ethdb.NewMemDatabase()
+}
+
+type fakeproc struct {
+}
+
+func (self fakeproc) ProcessWithParent(a, b *types.Block) (*big.Int, state.Messages, error) {
+ TD = new(big.Int).Add(TD, big.NewInt(1))
+ return TD, nil, nil
+}
+
+func makechain(cman *ChainManager, max int) *BlockChain {
+ blocks := make(types.Blocks, max)
+ for i := 0; i < max; i++ {
+ addr := ethutil.LeftPadBytes([]byte{byte(i)}, 20)
+ block := cman.NewBlock(addr)
+ if i != 0 {
+ cman.CurrentBlock = blocks[i-1]
+ }
+ blocks[i] = block
+ }
+ return NewChain(blocks)
+}
+
+func TestLongerFork(t *testing.T) {
+ cman := NewChainManager()
+ cman.SetProcessor(fakeproc{})
+
+ TD = big.NewInt(1)
+ chainA := makechain(cman, 5)
+
+ TD = big.NewInt(1)
+ chainB := makechain(cman, 10)
+
+ td, err := cman.TestChain(chainA)
+ if err != nil {
+ t.Error("unable to create new TD from chainA:", err)
+ }
+ cman.TD = td
+
+ _, err = cman.TestChain(chainB)
+ if err != nil {
+ t.Error("expected chainB not to give errors:", err)
+ }
+}
+
+func TestEqualFork(t *testing.T) {
+ cman := NewChainManager()
+ cman.SetProcessor(fakeproc{})
+
+ TD = big.NewInt(1)
+ chainA := makechain(cman, 5)
+
+ TD = big.NewInt(2)
+ chainB := makechain(cman, 5)
+
+ td, err := cman.TestChain(chainA)
+ if err != nil {
+ t.Error("unable to create new TD from chainA:", err)
+ }
+ cman.TD = td
+
+ _, err = cman.TestChain(chainB)
+ if err != nil {
+ t.Error("expected chainB not to give errors:", err)
+ }
+}
+
+func TestBrokenChain(t *testing.T) {
+ cman := NewChainManager()
+ cman.SetProcessor(fakeproc{})
+
+ TD = big.NewInt(1)
+ chain := makechain(cman, 5)
+ chain.Remove(chain.Front())
+
+ _, err := cman.TestChain(chain)
+ if err == nil {
+ t.Error("expected broken chain to return error")
+ }
+}
+
+func BenchmarkChainTesting(b *testing.B) {
+ const chainlen = 1000
+
+ ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
+ ethutil.Config.Db, _ = ethdb.NewMemDatabase()
+
+ cman := NewChainManager()
+ cman.SetProcessor(fakeproc{})
+
+ TD = big.NewInt(1)
+ chain := makechain(cman, chainlen)
+
+ stime := time.Now()
+ cman.TestChain(chain)
+ fmt.Println(chainlen, "took", time.Since(stime))
+}