block_validator_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. 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/core/vm"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/event"
  27. "github.com/ethereum/go-ethereum/params"
  28. "github.com/ethereum/go-ethereum/pow/ezp"
  29. )
  30. func testChainConfig() *params.ChainConfig {
  31. return params.TestChainConfig
  32. //return &params.ChainConfig{HomesteadBlock: big.NewInt(0)}
  33. }
  34. func proc() (Validator, *BlockChain) {
  35. db, _ := ethdb.NewMemDatabase()
  36. var mux event.TypeMux
  37. WriteTestNetGenesisBlock(db)
  38. blockchain, err := NewBlockChain(db, testChainConfig(), thePow(), &mux)
  39. if err != nil {
  40. fmt.Println(err)
  41. }
  42. return blockchain.validator, blockchain
  43. }
  44. func TestNumber(t *testing.T) {
  45. pow := ezp.New()
  46. _, chain := proc()
  47. statedb, _ := state.New(chain.Genesis().Root(), chain.chainDb)
  48. cfg := testChainConfig()
  49. header := makeHeader(cfg, chain.Genesis(), statedb)
  50. header.Number = big.NewInt(3)
  51. err := ValidateHeader(cfg, pow, header, chain.Genesis().Header(), false, false)
  52. if err != BlockNumberErr {
  53. t.Errorf("expected block number error, got %q", err)
  54. }
  55. header = makeHeader(cfg, chain.Genesis(), statedb)
  56. err = ValidateHeader(cfg, pow, header, chain.Genesis().Header(), false, false)
  57. if err == BlockNumberErr {
  58. t.Errorf("didn't expect block number error")
  59. }
  60. }
  61. func TestPutReceipt(t *testing.T) {
  62. db, _ := ethdb.NewMemDatabase()
  63. var addr common.Address
  64. addr[0] = 1
  65. var hash common.Hash
  66. hash[0] = 2
  67. receipt := new(types.Receipt)
  68. receipt.Logs = vm.Logs{&vm.Log{
  69. Address: addr,
  70. Topics: []common.Hash{hash},
  71. Data: []byte("hi"),
  72. BlockNumber: 42,
  73. TxHash: hash,
  74. TxIndex: 0,
  75. BlockHash: hash,
  76. Index: 0,
  77. }}
  78. WriteReceipts(db, types.Receipts{receipt})
  79. receipt = GetReceipt(db, common.Hash{})
  80. if receipt == nil {
  81. t.Error("expected to get 1 receipt, got none.")
  82. }
  83. }