chain_manager_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package core
  2. import (
  3. "fmt"
  4. "path"
  5. "runtime"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/ethdb"
  9. "github.com/ethereum/go-ethereum/ethutil"
  10. "github.com/ethereum/go-ethereum/event"
  11. //logpkg "github.com/ethereum/go-ethereum/logger"
  12. )
  13. //var Logger logpkg.LogSystem
  14. //var Log = logpkg.NewLogger("TEST")
  15. func init() {
  16. runtime.GOMAXPROCS(runtime.NumCPU())
  17. //Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.InfoLevel)
  18. //logpkg.AddLogSystem(Logger)
  19. ethutil.ReadConfig("/tmp/ethtest", "/tmp/ethtest", "ETH")
  20. db, err := ethdb.NewMemDatabase()
  21. if err != nil {
  22. panic("Could not create mem-db, failing")
  23. }
  24. ethutil.Config.Db = db
  25. }
  26. func loadChain(fn string, t *testing.T) types.Blocks {
  27. c1, err := ethutil.ReadAllFile(path.Join("..", "_data", fn))
  28. if err != nil {
  29. fmt.Println(err)
  30. t.FailNow()
  31. }
  32. value := ethutil.NewValueFromBytes([]byte(c1))
  33. blocks := make(types.Blocks, value.Len())
  34. it := value.NewIterator()
  35. for it.Next() {
  36. blocks[it.Idx()] = types.NewBlockFromRlpValue(it.Value())
  37. }
  38. return blocks
  39. }
  40. func insertChain(done chan bool, chainMan *ChainManager, chain types.Blocks, t *testing.T) {
  41. err := chainMan.InsertChain(chain)
  42. if err != nil {
  43. fmt.Println(err)
  44. t.FailNow()
  45. }
  46. done <- true
  47. }
  48. func TestChainInsertions(t *testing.T) {
  49. chain1 := loadChain("chain1", t)
  50. chain2 := loadChain("chain2", t)
  51. var eventMux event.TypeMux
  52. chainMan := NewChainManager(&eventMux)
  53. txPool := NewTxPool(chainMan, nil, &eventMux)
  54. blockMan := NewBlockManager(txPool, chainMan, &eventMux)
  55. chainMan.SetProcessor(blockMan)
  56. const max = 2
  57. done := make(chan bool, max)
  58. go insertChain(done, chainMan, chain1, t)
  59. go insertChain(done, chainMan, chain2, t)
  60. for i := 0; i < max; i++ {
  61. <-done
  62. }
  63. fmt.Println(chainMan.CurrentBlock())
  64. }