block_processor_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "fmt"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/event"
  26. "github.com/ethereum/go-ethereum/pow/ezp"
  27. )
  28. func proc() (*BlockProcessor, *ChainManager) {
  29. db, _ := ethdb.NewMemDatabase()
  30. var mux event.TypeMux
  31. genesis := GenesisBlock(0, db)
  32. chainMan, err := NewChainManager(genesis, db, db, db, thePow(), &mux)
  33. if err != nil {
  34. fmt.Println(err)
  35. }
  36. return NewBlockProcessor(db, db, ezp.New(), chainMan, &mux), chainMan
  37. }
  38. func TestNumber(t *testing.T) {
  39. pow := ezp.New()
  40. _, chain := proc()
  41. statedb := state.New(chain.Genesis().Root(), chain.stateDb)
  42. header := makeHeader(chain.Genesis(), statedb)
  43. header.Number = big.NewInt(3)
  44. err := ValidateHeader(pow, header, chain.Genesis(), false)
  45. if err != BlockNumberErr {
  46. t.Errorf("expected block number error, got %q", err)
  47. }
  48. header = makeHeader(chain.Genesis(), statedb)
  49. err = ValidateHeader(pow, header, chain.Genesis(), false)
  50. if err == BlockNumberErr {
  51. t.Errorf("didn't expect block number error")
  52. }
  53. }
  54. func TestPutReceipt(t *testing.T) {
  55. db, _ := ethdb.NewMemDatabase()
  56. var addr common.Address
  57. addr[0] = 1
  58. var hash common.Hash
  59. hash[0] = 2
  60. receipt := new(types.Receipt)
  61. receipt.SetLogs(state.Logs{&state.Log{
  62. Address: addr,
  63. Topics: []common.Hash{hash},
  64. Data: []byte("hi"),
  65. Number: 42,
  66. TxHash: hash,
  67. TxIndex: 0,
  68. BlockHash: hash,
  69. Index: 0,
  70. }})
  71. PutReceipts(db, types.Receipts{receipt})
  72. receipt = GetReceipt(db, common.Hash{})
  73. if receipt == nil {
  74. t.Error("expected to get 1 receipt, got none.")
  75. }
  76. }