2022-11-02 09:32:20 +00:00
|
|
|
// 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"
|
|
|
|
|
2023-02-06 15:37:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
2022-11-02 09:32:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-02-07 18:16:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-11-02 09:32:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildPayload(t *testing.T) {
|
2023-11-15 12:46:32 +00:00
|
|
|
t.Parallel()
|
2022-11-02 09:32:20 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-02-06 15:37:58 +00:00
|
|
|
verify := func(outer *engine.ExecutionPayloadEnvelope, txs int) {
|
2023-01-25 14:32:25 +00:00
|
|
|
payload := outer.ExecutionPayload
|
|
|
|
if payload.ParentHash != b.chain.CurrentBlock().Hash() {
|
2024-01-17 10:44:01 +00:00
|
|
|
t.Fatal("Unexpected parent hash")
|
2022-11-02 09:32:20 +00:00
|
|
|
}
|
2023-01-25 14:32:25 +00:00
|
|
|
if payload.Random != (common.Hash{}) {
|
2024-01-17 10:44:01 +00:00
|
|
|
t.Fatal("Unexpected random value")
|
2022-11-02 09:32:20 +00:00
|
|
|
}
|
2023-01-25 14:32:25 +00:00
|
|
|
if payload.Timestamp != timestamp {
|
2024-01-17 10:44:01 +00:00
|
|
|
t.Fatal("Unexpected timestamp")
|
2022-11-02 09:32:20 +00:00
|
|
|
}
|
2023-01-25 14:32:25 +00:00
|
|
|
if payload.FeeRecipient != recipient {
|
2024-01-17 10:44:01 +00:00
|
|
|
t.Fatal("Unexpected fee recipient")
|
2022-11-02 09:32:20 +00:00
|
|
|
}
|
2023-01-25 14:32:25 +00:00
|
|
|
if len(payload.Transactions) != txs {
|
2024-01-17 10:44:01 +00:00
|
|
|
t.Fatal("Unexpected transaction set")
|
2022-11-02 09:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 18:16:53 +00:00
|
|
|
|
|
|
|
func TestPayloadId(t *testing.T) {
|
2023-11-15 12:46:32 +00:00
|
|
|
t.Parallel()
|
2023-02-07 18:16:53 +00:00
|
|
|
ids := make(map[string]int)
|
|
|
|
for i, tt := range []*BuildPayloadArgs{
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{1},
|
|
|
|
Timestamp: 1,
|
|
|
|
Random: common.Hash{0x1},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different parent
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 1,
|
|
|
|
Random: common.Hash{0x1},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different timestamp
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x1},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different Random
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different fee-recipient
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x2},
|
|
|
|
},
|
|
|
|
// Different withdrawals (non-empty)
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x2},
|
|
|
|
Withdrawals: []*types.Withdrawal{
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Index: 0,
|
|
|
|
Validator: 0,
|
|
|
|
Address: common.Address{},
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Different withdrawals (non-empty)
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x2},
|
|
|
|
Withdrawals: []*types.Withdrawal{
|
2023-07-13 06:55:31 +00:00
|
|
|
{
|
2023-02-07 18:16:53 +00:00
|
|
|
Index: 2,
|
|
|
|
Validator: 0,
|
|
|
|
Address: common.Address{},
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
id := tt.Id().String()
|
|
|
|
if prev, exists := ids[id]; exists {
|
|
|
|
t.Errorf("ID collision, case %d and case %d: id %v", prev, i, id)
|
|
|
|
}
|
|
|
|
ids[id] = i
|
|
|
|
}
|
|
|
|
}
|