block_validator_test.go 2.7 KB

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