github.com/jimmyx0x/go-ethereum@v1.10.28/miner/payload_building_test.go (about)

     1  // Copyright 2022 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 miner
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/consensus/ethash"
    26  	"github.com/ethereum/go-ethereum/core/beacon"
    27  	"github.com/ethereum/go-ethereum/core/rawdb"
    28  	"github.com/ethereum/go-ethereum/params"
    29  )
    30  
    31  func TestBuildPayload(t *testing.T) {
    32  	var (
    33  		db        = rawdb.NewMemoryDatabase()
    34  		recipient = common.HexToAddress("0xdeadbeef")
    35  	)
    36  	w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
    37  	defer w.close()
    38  
    39  	timestamp := uint64(time.Now().Unix())
    40  	args := &BuildPayloadArgs{
    41  		Parent:       b.chain.CurrentBlock().Hash(),
    42  		Timestamp:    timestamp,
    43  		Random:       common.Hash{},
    44  		FeeRecipient: recipient,
    45  	}
    46  	payload, err := w.buildPayload(args)
    47  	if err != nil {
    48  		t.Fatalf("Failed to build payload %v", err)
    49  	}
    50  	verify := func(data *beacon.ExecutableDataV1, txs int) {
    51  		if data.ParentHash != b.chain.CurrentBlock().Hash() {
    52  			t.Fatal("Unexpect parent hash")
    53  		}
    54  		if data.Random != (common.Hash{}) {
    55  			t.Fatal("Unexpect random value")
    56  		}
    57  		if data.Timestamp != timestamp {
    58  			t.Fatal("Unexpect timestamp")
    59  		}
    60  		if data.FeeRecipient != recipient {
    61  			t.Fatal("Unexpect fee recipient")
    62  		}
    63  		if len(data.Transactions) != txs {
    64  			t.Fatal("Unexpect transaction set")
    65  		}
    66  	}
    67  	empty := payload.ResolveEmpty()
    68  	verify(empty, 0)
    69  
    70  	full := payload.ResolveFull()
    71  	verify(full, len(pendingTxs))
    72  
    73  	// Ensure resolve can be called multiple times and the
    74  	// result should be unchanged
    75  	dataOne := payload.Resolve()
    76  	dataTwo := payload.Resolve()
    77  	if !reflect.DeepEqual(dataOne, dataTwo) {
    78  		t.Fatal("Unexpected payload data")
    79  	}
    80  }