block_processor_test.go 2.53 KB
Newer Older
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 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,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
16

17 18 19
package core

import (
20
	"fmt"
21 22 23
	"math/big"
	"testing"

Felix Lange's avatar
Felix Lange committed
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"
obscuren's avatar
obscuren committed
30
	"github.com/ethereum/go-ethereum/pow/ezp"
31 32
)

33
func proc() (*BlockProcessor, *BlockChain) {
34 35 36
	db, _ := ethdb.NewMemDatabase()
	var mux event.TypeMux

37
	WriteTestNetGenesisBlock(db, 0)
38
	blockchain, err := NewBlockChain(db, thePow(), &mux)
39 40 41
	if err != nil {
		fmt.Println(err)
	}
42
	return NewBlockProcessor(db, ezp.New(), blockchain, &mux), blockchain
43 44 45
}

func TestNumber(t *testing.T) {
46
	pow := ezp.New()
47
	_, chain := proc()
48

49
	statedb, _ := state.New(chain.Genesis().Root(), chain.chainDb)
50
	header := makeHeader(chain.Genesis(), statedb)
51
	header.Number = big.NewInt(3)
52
	err := ValidateHeader(pow, header, chain.Genesis().Header(), false, false)
53
	if err != BlockNumberErr {
54
		t.Errorf("expected block number error, got %q", err)
55 56
	}

57
	header = makeHeader(chain.Genesis(), statedb)
58
	err = ValidateHeader(pow, header, chain.Genesis().Header(), false, false)
59 60 61 62
	if err == BlockNumberErr {
		t.Errorf("didn't expect block number error")
	}
}
63 64 65 66 67 68 69 70 71 72

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)
73
	receipt.Logs = vm.Logs{&vm.Log{
74 75 76 77 78 79 80 81
		Address:     addr,
		Topics:      []common.Hash{hash},
		Data:        []byte("hi"),
		BlockNumber: 42,
		TxHash:      hash,
		TxIndex:     0,
		BlockHash:   hash,
		Index:       0,
82
	}}
83

84 85 86 87
	PutReceipts(db, types.Receipts{receipt})
	receipt = GetReceipt(db, common.Hash{})
	if receipt == nil {
		t.Error("expected to get 1 receipt, got none.")
88 89
	}
}