block_validator_test.go 2.6 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2015 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// 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/>.
F
Felix Lange 已提交
16

17 18 19
package core

import (
O
obscuren 已提交
20
	"fmt"
21 22 23
	"math/big"
	"testing"

F
Felix Lange 已提交
24
	"github.com/ethereum/go-ethereum/common"
25 26
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/types"
27
	"github.com/ethereum/go-ethereum/core/vm"
28 29
	"github.com/ethereum/go-ethereum/ethdb"
	"github.com/ethereum/go-ethereum/event"
O
obscuren 已提交
30
	"github.com/ethereum/go-ethereum/pow/ezp"
31 32
)

33
func testChainConfig() *ChainConfig {
34
	return &ChainConfig{HomesteadBlock: big.NewInt(0)}
35 36
}

37
func proc() (Validator, *BlockChain) {
38 39 40
	db, _ := ethdb.NewMemDatabase()
	var mux event.TypeMux

41
	WriteTestNetGenesisBlock(db)
42
	blockchain, err := NewBlockChain(db, testChainConfig(), thePow(), &mux)
O
obscuren 已提交
43 44 45
	if err != nil {
		fmt.Println(err)
	}
46
	return blockchain.validator, blockchain
47 48 49
}

func TestNumber(t *testing.T) {
50
	pow := ezp.New()
51
	_, chain := proc()
52

53
	statedb, _ := state.New(chain.Genesis().Root(), chain.chainDb)
54
	header := makeHeader(chain.Genesis(), statedb)
F
Felix Lange 已提交
55
	header.Number = big.NewInt(3)
56 57
	cfg := testChainConfig()
	err := ValidateHeader(cfg, pow, header, chain.Genesis().Header(), false, false)
58
	if err != BlockNumberErr {
F
Felix Lange 已提交
59
		t.Errorf("expected block number error, got %q", err)
60 61
	}

62
	header = makeHeader(chain.Genesis(), statedb)
63
	err = ValidateHeader(cfg, pow, header, chain.Genesis().Header(), false, false)
64 65 66 67
	if err == BlockNumberErr {
		t.Errorf("didn't expect block number error")
	}
}
68 69 70 71 72 73 74 75 76 77

func TestPutReceipt(t *testing.T) {
	db, _ := ethdb.NewMemDatabase()

	var addr common.Address
	addr[0] = 1
	var hash common.Hash
	hash[0] = 2

	receipt := new(types.Receipt)
78
	receipt.Logs = vm.Logs{&vm.Log{
79 80 81 82 83 84 85 86
		Address:     addr,
		Topics:      []common.Hash{hash},
		Data:        []byte("hi"),
		BlockNumber: 42,
		TxHash:      hash,
		TxIndex:     0,
		BlockHash:   hash,
		Index:       0,
87
	}}
88

89
	WriteReceipts(db, types.Receipts{receipt})
90 91 92
	receipt = GetReceipt(db, common.Hash{})
	if receipt == nil {
		t.Error("expected to get 1 receipt, got none.")
93 94
	}
}