block_processor_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/state"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/ethereum/go-ethereum/ethdb"
  10. "github.com/ethereum/go-ethereum/event"
  11. "github.com/ethereum/go-ethereum/pow/ezp"
  12. )
  13. func proc() (*BlockProcessor, *ChainManager) {
  14. db, _ := ethdb.NewMemDatabase()
  15. var mux event.TypeMux
  16. genesis := GenesisBlock(0, db)
  17. chainMan, err := NewChainManager(genesis, db, db, thePow(), &mux)
  18. if err != nil {
  19. fmt.Println(err)
  20. }
  21. return NewBlockProcessor(db, db, ezp.New(), chainMan, &mux), chainMan
  22. }
  23. func TestNumber(t *testing.T) {
  24. pow := ezp.New()
  25. _, chain := proc()
  26. statedb := state.New(chain.Genesis().Root(), chain.stateDb)
  27. header := makeHeader(chain.Genesis(), statedb)
  28. header.Number = big.NewInt(3)
  29. err := ValidateHeader(pow, header, chain.Genesis(), false)
  30. if err != BlockNumberErr {
  31. t.Errorf("expected block number error, got %q", err)
  32. }
  33. header = makeHeader(chain.Genesis(), statedb)
  34. err = ValidateHeader(pow, header, chain.Genesis(), false)
  35. if err == BlockNumberErr {
  36. t.Errorf("didn't expect block number error")
  37. }
  38. }
  39. func TestPutReceipt(t *testing.T) {
  40. db, _ := ethdb.NewMemDatabase()
  41. var addr common.Address
  42. addr[0] = 1
  43. var hash common.Hash
  44. hash[0] = 2
  45. receipt := new(types.Receipt)
  46. receipt.SetLogs(state.Logs{&state.Log{
  47. Address: addr,
  48. Topics: []common.Hash{hash},
  49. Data: []byte("hi"),
  50. Number: 42,
  51. TxHash: hash,
  52. TxIndex: 0,
  53. BlockHash: hash,
  54. Index: 0,
  55. }})
  56. putReceipts(db, hash, types.Receipts{receipt})
  57. receipts, err := getBlockReceipts(db, hash)
  58. if err != nil {
  59. t.Error("got err:", err)
  60. }
  61. if len(receipts) != 1 {
  62. t.Error("expected to get 1 receipt, got", len(receipts))
  63. }
  64. }