81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
// Copyright 2022 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// 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.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
package miner
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
"github.com/ethereum/go-ethereum/core/beacon"
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
)
|
|
|
|
func TestBuildPayload(t *testing.T) {
|
|
var (
|
|
db = rawdb.NewMemoryDatabase()
|
|
recipient = common.HexToAddress("0xdeadbeef")
|
|
)
|
|
w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
|
|
defer w.close()
|
|
|
|
timestamp := uint64(time.Now().Unix())
|
|
args := &BuildPayloadArgs{
|
|
Parent: b.chain.CurrentBlock().Hash(),
|
|
Timestamp: timestamp,
|
|
Random: common.Hash{},
|
|
FeeRecipient: recipient,
|
|
}
|
|
payload, err := w.buildPayload(args)
|
|
if err != nil {
|
|
t.Fatalf("Failed to build payload %v", err)
|
|
}
|
|
verify := func(data *beacon.ExecutableDataV1, txs int) {
|
|
if data.ParentHash != b.chain.CurrentBlock().Hash() {
|
|
t.Fatal("Unexpect parent hash")
|
|
}
|
|
if data.Random != (common.Hash{}) {
|
|
t.Fatal("Unexpect random value")
|
|
}
|
|
if data.Timestamp != timestamp {
|
|
t.Fatal("Unexpect timestamp")
|
|
}
|
|
if data.FeeRecipient != recipient {
|
|
t.Fatal("Unexpect fee recipient")
|
|
}
|
|
if len(data.Transactions) != txs {
|
|
t.Fatal("Unexpect transaction set")
|
|
}
|
|
}
|
|
empty := payload.ResolveEmpty()
|
|
verify(empty, 0)
|
|
|
|
full := payload.ResolveFull()
|
|
verify(full, len(pendingTxs))
|
|
|
|
// Ensure resolve can be called multiple times and the
|
|
// result should be unchanged
|
|
dataOne := payload.Resolve()
|
|
dataTwo := payload.Resolve()
|
|
if !reflect.DeepEqual(dataOne, dataTwo) {
|
|
t.Fatal("Unexpected payload data")
|
|
}
|
|
}
|