github.com/theQRL/go-zond@v0.1.1/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/theQRL/go-zond/beacon/engine"
    25  	"github.com/theQRL/go-zond/common"
    26  	"github.com/theQRL/go-zond/consensus/ethash"
    27  	"github.com/theQRL/go-zond/core/rawdb"
    28  	"github.com/theQRL/go-zond/core/types"
    29  	"github.com/theQRL/go-zond/params"
    30  )
    31  
    32  func TestBuildPayload(t *testing.T) {
    33  	var (
    34  		db        = rawdb.NewMemoryDatabase()
    35  		recipient = common.HexToAddress("0xdeadbeef")
    36  	)
    37  	w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
    38  	defer w.close()
    39  
    40  	timestamp := uint64(time.Now().Unix())
    41  	args := &BuildPayloadArgs{
    42  		Parent:       b.chain.CurrentBlock().Hash(),
    43  		Timestamp:    timestamp,
    44  		Random:       common.Hash{},
    45  		FeeRecipient: recipient,
    46  	}
    47  	payload, err := w.buildPayload(args)
    48  	if err != nil {
    49  		t.Fatalf("Failed to build payload %v", err)
    50  	}
    51  	verify := func(outer *engine.ExecutionPayloadEnvelope, txs int) {
    52  		payload := outer.ExecutionPayload
    53  		if payload.ParentHash != b.chain.CurrentBlock().Hash() {
    54  			t.Fatal("Unexpect parent hash")
    55  		}
    56  		if payload.Random != (common.Hash{}) {
    57  			t.Fatal("Unexpect random value")
    58  		}
    59  		if payload.Timestamp != timestamp {
    60  			t.Fatal("Unexpect timestamp")
    61  		}
    62  		if payload.FeeRecipient != recipient {
    63  			t.Fatal("Unexpect fee recipient")
    64  		}
    65  		if len(payload.Transactions) != txs {
    66  			t.Fatal("Unexpect transaction set")
    67  		}
    68  	}
    69  	empty := payload.ResolveEmpty()
    70  	verify(empty, 0)
    71  
    72  	full := payload.ResolveFull()
    73  	verify(full, len(pendingTxs))
    74  
    75  	// Ensure resolve can be called multiple times and the
    76  	// result should be unchanged
    77  	dataOne := payload.Resolve()
    78  	dataTwo := payload.Resolve()
    79  	if !reflect.DeepEqual(dataOne, dataTwo) {
    80  		t.Fatal("Unexpected payload data")
    81  	}
    82  }
    83  
    84  func TestPayloadId(t *testing.T) {
    85  	ids := make(map[string]int)
    86  	for i, tt := range []*BuildPayloadArgs{
    87  		{
    88  			Parent:       common.Hash{1},
    89  			Timestamp:    1,
    90  			Random:       common.Hash{0x1},
    91  			FeeRecipient: common.Address{0x1},
    92  		},
    93  		// Different parent
    94  		{
    95  			Parent:       common.Hash{2},
    96  			Timestamp:    1,
    97  			Random:       common.Hash{0x1},
    98  			FeeRecipient: common.Address{0x1},
    99  		},
   100  		// Different timestamp
   101  		{
   102  			Parent:       common.Hash{2},
   103  			Timestamp:    2,
   104  			Random:       common.Hash{0x1},
   105  			FeeRecipient: common.Address{0x1},
   106  		},
   107  		// Different Random
   108  		{
   109  			Parent:       common.Hash{2},
   110  			Timestamp:    2,
   111  			Random:       common.Hash{0x2},
   112  			FeeRecipient: common.Address{0x1},
   113  		},
   114  		// Different fee-recipient
   115  		{
   116  			Parent:       common.Hash{2},
   117  			Timestamp:    2,
   118  			Random:       common.Hash{0x2},
   119  			FeeRecipient: common.Address{0x2},
   120  		},
   121  		// Different withdrawals (non-empty)
   122  		{
   123  			Parent:       common.Hash{2},
   124  			Timestamp:    2,
   125  			Random:       common.Hash{0x2},
   126  			FeeRecipient: common.Address{0x2},
   127  			Withdrawals: []*types.Withdrawal{
   128  				{
   129  					Index:     0,
   130  					Validator: 0,
   131  					Address:   common.Address{},
   132  					Amount:    0,
   133  				},
   134  			},
   135  		},
   136  		// Different withdrawals (non-empty)
   137  		{
   138  			Parent:       common.Hash{2},
   139  			Timestamp:    2,
   140  			Random:       common.Hash{0x2},
   141  			FeeRecipient: common.Address{0x2},
   142  			Withdrawals: []*types.Withdrawal{
   143  				{
   144  					Index:     2,
   145  					Validator: 0,
   146  					Address:   common.Address{},
   147  					Amount:    0,
   148  				},
   149  			},
   150  		},
   151  	} {
   152  		id := tt.Id().String()
   153  		if prev, exists := ids[id]; exists {
   154  			t.Errorf("ID collision, case %d and case %d: id %v", prev, i, id)
   155  		}
   156  		ids[id] = i
   157  	}
   158  }