github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/core/block_processor_test.go (about)

     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  
    17  package core
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/state"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/ethdb"
    28  	"github.com/ethereum/go-ethereum/event"
    29  	"github.com/ethereum/go-ethereum/pow/ezp"
    30  )
    31  
    32  func proc() (*BlockProcessor, *ChainManager) {
    33  	db, _ := ethdb.NewMemDatabase()
    34  	var mux event.TypeMux
    35  
    36  	WriteTestNetGenesisBlock(db, 0)
    37  	chainMan, err := NewChainManager(db, thePow(), &mux)
    38  	if err != nil {
    39  		fmt.Println(err)
    40  	}
    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(), 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(), false, false)
    58  	if err == BlockNumberErr {
    59  		t.Errorf("didn't expect block number error")
    60  	}
    61  }
    62  
    63  func TestPutReceipt(t *testing.T) {
    64  	db, _ := ethdb.NewMemDatabase()
    65  
    66  	var addr common.Address
    67  	addr[0] = 1
    68  	var hash common.Hash
    69  	hash[0] = 2
    70  
    71  	receipt := new(types.Receipt)
    72  	receipt.SetLogs(state.Logs{&state.Log{
    73  		Address:   addr,
    74  		Topics:    []common.Hash{hash},
    75  		Data:      []byte("hi"),
    76  		Number:    42,
    77  		TxHash:    hash,
    78  		TxIndex:   0,
    79  		BlockHash: hash,
    80  		Index:     0,
    81  	}})
    82  
    83  	PutReceipts(db, types.Receipts{receipt})
    84  	receipt = GetReceipt(db, common.Hash{})
    85  	if receipt == nil {
    86  		t.Error("expected to get 1 receipt, got none.")
    87  	}
    88  }