block_validator_test.go 2.5 KB

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