Skip to content

Commit 2f8a45c

Browse files
committed
Fixed chain test & added new chain
1 parent ce68ac6 commit 2f8a45c

5 files changed

Lines changed: 37 additions & 17 deletions

File tree

‎_data/chain1‎

-154 KB
Binary file not shown.

‎_data/chain2‎

-21.5 KB
Binary file not shown.

‎core/chain_manager_test.go‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package core
22

33
import (
44
"fmt"
5+
"os"
56
"path"
7+
"reflect"
68
"runtime"
79
"testing"
810

911
"github.com/ethereum/go-ethereum/core/types"
1012
"github.com/ethereum/go-ethereum/ethdb"
1113
"github.com/ethereum/go-ethereum/ethutil"
1214
"github.com/ethereum/go-ethereum/event"
15+
"github.com/ethereum/go-ethereum/rlp"
1316
//logpkg "github.com/ethereum/go-ethereum/logger"
1417
)
1518

@@ -30,20 +33,19 @@ func init() {
3033
ethutil.Config.Db = db
3134
}
3235

33-
func loadChain(fn string, t *testing.T) types.Blocks {
34-
c1, err := ethutil.ReadAllFile(path.Join("..", "_data", fn))
36+
func loadChain(fn string, t *testing.T) (types.Blocks, error) {
37+
fh, err := os.OpenFile(path.Join("..", "_data", fn), os.O_RDONLY, os.ModePerm)
3538
if err != nil {
36-
fmt.Println(err)
37-
t.FailNow()
39+
return nil, err
3840
}
39-
value := ethutil.NewValueFromBytes([]byte(c1))
40-
blocks := make(types.Blocks, value.Len())
41-
it := value.NewIterator()
42-
for it.Next() {
43-
blocks[it.Idx()] = types.NewBlockFromRlpValue(it.Value())
41+
defer fh.Close()
42+
43+
var chain types.Blocks
44+
if err := rlp.Decode(fh, &chain); err != nil {
45+
return nil, err
4446
}
4547

46-
return blocks
48+
return chain, nil
4749
}
4850

4951
func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *testing.T) {
@@ -56,11 +58,21 @@ func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *
5658
}
5759

5860
func TestChainInsertions(t *testing.T) {
59-
chain1 := loadChain("chain1", t)
60-
chain2 := loadChain("chain2", t)
61+
chain1, err := loadChain("chain1", t)
62+
if err != nil {
63+
fmt.Println(err)
64+
t.FailNow()
65+
}
66+
67+
chain2, err := loadChain("chain2", t)
68+
if err != nil {
69+
fmt.Println(err)
70+
t.FailNow()
71+
}
72+
6173
var eventMux event.TypeMux
6274
chainMan := NewChainManager(&eventMux)
63-
txPool := NewTxPool(chainMan, nil, &eventMux)
75+
txPool := NewTxPool(chainMan, &eventMux)
6476
blockMan := NewBlockManager(txPool, chainMan, &eventMux)
6577
chainMan.SetProcessor(blockMan)
6678

@@ -73,5 +85,12 @@ func TestChainInsertions(t *testing.T) {
7385
for i := 0; i < max; i++ {
7486
<-done
7587
}
76-
fmt.Println(chainMan.CurrentBlock())
88+
89+
if reflect.DeepEqual(chain2[len(chain2)-1], chainMan.CurrentBlock()) {
90+
t.Error("chain2 is canonical and shouldn't be")
91+
}
92+
93+
if !reflect.DeepEqual(chain1[len(chain1)-1], chainMan.CurrentBlock()) {
94+
t.Error("chain1 isn't canonical and should be")
95+
}
7796
}

‎core/helper_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/ethereum/go-ethereum/ethdb"
1010
"github.com/ethereum/go-ethereum/ethutil"
1111
"github.com/ethereum/go-ethereum/event"
12-
"github.com/ethereum/go-ethereum/wire"
12+
"github.com/ethereum/go-ethereum/p2p"
1313
)
1414

1515
// Implement our EthTest Manager
@@ -54,11 +54,11 @@ func (tm *TestManager) TxPool() *TxPool {
5454
func (tm *TestManager) EventMux() *event.TypeMux {
5555
return tm.eventMux
5656
}
57-
func (tm *TestManager) Broadcast(msgType wire.MsgType, data []interface{}) {
57+
func (tm *TestManager) Broadcast(msgType p2p.Msg, data []interface{}) {
5858
fmt.Println("Broadcast not implemented")
5959
}
6060

61-
func (tm *TestManager) ClientIdentity() wire.ClientIdentity {
61+
func (tm *TestManager) ClientIdentity() p2p.ClientIdentity {
6262
return nil
6363
}
6464
func (tm *TestManager) KeyManager() *crypto.KeyManager {

‎core/types/block.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ func (self *Block) Hash() []byte { return self.header.Hash() }
199199
func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) }
200200
func (self *Block) State() *state.StateDB { return state.New(self.Trie()) }
201201
func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) }
202+
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
202203

203204
// Implement block.Pow
204205
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }

0 commit comments

Comments
 (0)