Fixed chain test & added new chain
This commit is contained in:
parent
ce68ac6959
commit
2f8a45cd8b
BIN
_data/chain1
BIN
_data/chain1
Binary file not shown.
BIN
_data/chain2
BIN
_data/chain2
Binary file not shown.
@ -2,7 +2,9 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -10,6 +12,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/ethutil"
|
"github.com/ethereum/go-ethereum/ethutil"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
//logpkg "github.com/ethereum/go-ethereum/logger"
|
//logpkg "github.com/ethereum/go-ethereum/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,20 +33,19 @@ func init() {
|
|||||||
ethutil.Config.Db = db
|
ethutil.Config.Db = db
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadChain(fn string, t *testing.T) types.Blocks {
|
func loadChain(fn string, t *testing.T) (types.Blocks, error) {
|
||||||
c1, err := ethutil.ReadAllFile(path.Join("..", "_data", fn))
|
fh, err := os.OpenFile(path.Join("..", "_data", fn), os.O_RDONLY, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return nil, err
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
value := ethutil.NewValueFromBytes([]byte(c1))
|
defer fh.Close()
|
||||||
blocks := make(types.Blocks, value.Len())
|
|
||||||
it := value.NewIterator()
|
var chain types.Blocks
|
||||||
for it.Next() {
|
if err := rlp.Decode(fh, &chain); err != nil {
|
||||||
blocks[it.Idx()] = types.NewBlockFromRlpValue(it.Value())
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return blocks
|
return chain, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *testing.T) {
|
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 *
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestChainInsertions(t *testing.T) {
|
func TestChainInsertions(t *testing.T) {
|
||||||
chain1 := loadChain("chain1", t)
|
chain1, err := loadChain("chain1", t)
|
||||||
chain2 := loadChain("chain2", t)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
chain2, err := loadChain("chain2", t)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
var eventMux event.TypeMux
|
var eventMux event.TypeMux
|
||||||
chainMan := NewChainManager(&eventMux)
|
chainMan := NewChainManager(&eventMux)
|
||||||
txPool := NewTxPool(chainMan, nil, &eventMux)
|
txPool := NewTxPool(chainMan, &eventMux)
|
||||||
blockMan := NewBlockManager(txPool, chainMan, &eventMux)
|
blockMan := NewBlockManager(txPool, chainMan, &eventMux)
|
||||||
chainMan.SetProcessor(blockMan)
|
chainMan.SetProcessor(blockMan)
|
||||||
|
|
||||||
@ -73,5 +85,12 @@ func TestChainInsertions(t *testing.T) {
|
|||||||
for i := 0; i < max; i++ {
|
for i := 0; i < max; i++ {
|
||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
fmt.Println(chainMan.CurrentBlock())
|
|
||||||
|
if reflect.DeepEqual(chain2[len(chain2)-1], chainMan.CurrentBlock()) {
|
||||||
|
t.Error("chain2 is canonical and shouldn't be")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(chain1[len(chain1)-1], chainMan.CurrentBlock()) {
|
||||||
|
t.Error("chain1 isn't canonical and should be")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/ethutil"
|
"github.com/ethereum/go-ethereum/ethutil"
|
||||||
"github.com/ethereum/go-ethereum/event"
|
"github.com/ethereum/go-ethereum/event"
|
||||||
"github.com/ethereum/go-ethereum/wire"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Implement our EthTest Manager
|
// Implement our EthTest Manager
|
||||||
@ -54,11 +54,11 @@ func (tm *TestManager) TxPool() *TxPool {
|
|||||||
func (tm *TestManager) EventMux() *event.TypeMux {
|
func (tm *TestManager) EventMux() *event.TypeMux {
|
||||||
return tm.eventMux
|
return tm.eventMux
|
||||||
}
|
}
|
||||||
func (tm *TestManager) Broadcast(msgType wire.MsgType, data []interface{}) {
|
func (tm *TestManager) Broadcast(msgType p2p.Msg, data []interface{}) {
|
||||||
fmt.Println("Broadcast not implemented")
|
fmt.Println("Broadcast not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *TestManager) ClientIdentity() wire.ClientIdentity {
|
func (tm *TestManager) ClientIdentity() p2p.ClientIdentity {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (tm *TestManager) KeyManager() *crypto.KeyManager {
|
func (tm *TestManager) KeyManager() *crypto.KeyManager {
|
||||||
|
@ -199,6 +199,7 @@ func (self *Block) Hash() []byte { return self.header.Hash() }
|
|||||||
func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) }
|
func (self *Block) Trie() *ptrie.Trie { return ptrie.New(self.header.Root, ethutil.Config.Db) }
|
||||||
func (self *Block) State() *state.StateDB { return state.New(self.Trie()) }
|
func (self *Block) State() *state.StateDB { return state.New(self.Trie()) }
|
||||||
func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) }
|
func (self *Block) Size() ethutil.StorageSize { return ethutil.StorageSize(len(ethutil.Encode(self))) }
|
||||||
|
func (self *Block) SetRoot(root []byte) { self.header.Root = root }
|
||||||
|
|
||||||
// Implement block.Pow
|
// Implement block.Pow
|
||||||
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
|
func (self *Block) Difficulty() *big.Int { return self.header.Difficulty }
|
||||||
|
Loading…
Reference in New Issue
Block a user