block_validator_test.go 2.4 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 20 21 22
package core

import (
	"math/big"
	"testing"

F
Felix Lange 已提交
23
	"github.com/ethereum/go-ethereum/common"
24 25
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/types"
26
	"github.com/ethereum/go-ethereum/ethdb"
27
	"github.com/ethereum/go-ethereum/params"
28
	"github.com/ethereum/go-ethereum/pow"
29 30
)

F
Felix Lange 已提交
31 32 33 34
func testGenesis(account common.Address, balance *big.Int) *Genesis {
	return &Genesis{
		Config: params.TestChainConfig,
		Alloc:  GenesisAlloc{account: {Balance: balance}},
O
obscuren 已提交
35
	}
36 37 38
}

func TestNumber(t *testing.T) {
F
Felix Lange 已提交
39
	chain := newTestBlockChain()
40
	statedb, _ := state.New(chain.Genesis().Root(), chain.chainDb)
F
Felix Lange 已提交
41
	header := makeHeader(chain.config, chain.Genesis(), statedb)
42
	header.Number = big.NewInt(3)
F
Felix Lange 已提交
43
	err := ValidateHeader(chain.config, pow.FakePow{}, header, chain.Genesis().Header(), false, false)
44
	if err != BlockNumberErr {
F
Felix Lange 已提交
45
		t.Errorf("expected block number error, got %q", err)
46 47
	}

F
Felix Lange 已提交
48 49
	header = makeHeader(chain.config, chain.Genesis(), statedb)
	err = ValidateHeader(chain.config, pow.FakePow{}, header, chain.Genesis().Header(), false, false)
50 51 52 53
	if err == BlockNumberErr {
		t.Errorf("didn't expect block number error")
	}
}
54 55 56 57 58 59 60 61 62 63

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)
F
Felix Lange 已提交
64
	receipt.Logs = []*types.Log{{
65 66 67 68 69 70 71 72
		Address:     addr,
		Topics:      []common.Hash{hash},
		Data:        []byte("hi"),
		BlockNumber: 42,
		TxHash:      hash,
		TxIndex:     0,
		BlockHash:   hash,
		Index:       0,
73
	}}
74

75
	WriteReceipts(db, types.Receipts{receipt})
76 77 78
	receipt = GetReceipt(db, common.Hash{})
	if receipt == nil {
		t.Error("expected to get 1 receipt, got none.")
79 80
	}
}