block_processor_test.go 2.47 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 28
	"github.com/ethereum/go-ethereum/ethdb"
	"github.com/ethereum/go-ethereum/event"
obscuren's avatar
obscuren committed
29
	"github.com/ethereum/go-ethereum/pow/ezp"
30 31 32 33 34 35
)

func proc() (*BlockProcessor, *ChainManager) {
	db, _ := ethdb.NewMemDatabase()
	var mux event.TypeMux

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

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

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

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

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)
	receipt.SetLogs(state.Logs{&state.Log{
		Address:   addr,
		Topics:    []common.Hash{hash},
		Data:      []byte("hi"),
		Number:    42,
		TxHash:    hash,
		TxIndex:   0,
		BlockHash: hash,
		Index:     0,
	}})

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