block_validator_test.go 2.6 KB

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