2022-11-02 17:32:20 +08: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 (
|
2022-11-07 15:30:54 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/binary"
|
2022-11-02 17:32:20 +08:00
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-02-06 16:37:58 +01:00
|
|
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
2022-11-02 17:32:20 +08:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2022-11-03 22:18:28 +08:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2023-02-07 19:16:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2022-11-02 17:32:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// BuildPayloadArgs contains the provided parameters for building payload.
|
|
|
|
// Check engine-api specification for more details.
|
2023-10-17 01:32:35 -05:00
|
|
|
// https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md#payloadattributesv3
|
2022-11-02 17:32:20 +08:00
|
|
|
type BuildPayloadArgs struct {
|
2024-01-24 00:39:12 -07:00
|
|
|
Parent common.Hash // The parent block to build payload on top
|
|
|
|
Timestamp uint64 // The provided timestamp of generated payload
|
|
|
|
FeeRecipient common.Address // The provided recipient address for collecting transaction fee
|
|
|
|
Random common.Hash // The provided randomness value
|
|
|
|
Withdrawals types.Withdrawals // The provided withdrawals
|
|
|
|
BeaconRoot *common.Hash // The provided beaconRoot (Cancun)
|
|
|
|
Version engine.PayloadVersion // Versioning byte for payload id calculation.
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 15:30:54 +01:00
|
|
|
// Id computes an 8-byte identifier by hashing the components of the payload arguments.
|
2023-02-06 16:37:58 +01:00
|
|
|
func (args *BuildPayloadArgs) Id() engine.PayloadID {
|
2022-11-07 15:30:54 +01:00
|
|
|
// Hash
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write(args.Parent[:])
|
|
|
|
binary.Write(hasher, binary.BigEndian, args.Timestamp)
|
|
|
|
hasher.Write(args.Random[:])
|
|
|
|
hasher.Write(args.FeeRecipient[:])
|
2023-02-07 19:16:53 +01:00
|
|
|
rlp.Encode(hasher, args.Withdrawals)
|
2023-08-26 04:52:12 +02:00
|
|
|
if args.BeaconRoot != nil {
|
|
|
|
hasher.Write(args.BeaconRoot[:])
|
|
|
|
}
|
2023-02-06 16:37:58 +01:00
|
|
|
var out engine.PayloadID
|
2022-11-07 15:30:54 +01:00
|
|
|
copy(out[:], hasher.Sum(nil)[:8])
|
2024-01-24 00:39:12 -07:00
|
|
|
out[0] = byte(args.Version)
|
2022-11-07 15:30:54 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:32:20 +08:00
|
|
|
// Payload wraps the built payload(block waiting for sealing). According to the
|
|
|
|
// engine-api specification, EL should build the initial version of the payload
|
|
|
|
// which has an empty transaction set and then keep update it in order to maximize
|
|
|
|
// the revenue. Therefore, the empty-block here is always available and full-block
|
|
|
|
// will be set/updated afterwards.
|
|
|
|
type Payload struct {
|
2023-02-06 16:37:58 +01:00
|
|
|
id engine.PayloadID
|
2022-11-02 17:32:20 +08:00
|
|
|
empty *types.Block
|
|
|
|
full *types.Block
|
2024-03-22 22:37:47 +08:00
|
|
|
sidecars types.BlobSidecars
|
2022-11-02 17:32:20 +08:00
|
|
|
fullFees *big.Int
|
|
|
|
stop chan struct{}
|
2022-11-03 22:18:28 +08:00
|
|
|
lock sync.Mutex
|
2022-11-02 17:32:20 +08:00
|
|
|
cond *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
// newPayload initializes the payload object.
|
2023-02-06 16:37:58 +01:00
|
|
|
func newPayload(empty *types.Block, id engine.PayloadID) *Payload {
|
2022-11-03 22:18:28 +08:00
|
|
|
payload := &Payload{
|
2022-11-07 15:30:54 +01:00
|
|
|
id: id,
|
2022-11-02 17:32:20 +08:00
|
|
|
empty: empty,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
2022-11-07 15:30:54 +01:00
|
|
|
log.Info("Starting work on payload", "id", payload.id)
|
2022-11-03 22:18:28 +08:00
|
|
|
payload.cond = sync.NewCond(&payload.lock)
|
|
|
|
return payload
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// update updates the full-block with latest built version.
|
2023-08-23 15:16:14 -06:00
|
|
|
func (payload *Payload) update(r *newPayloadResult, elapsed time.Duration) {
|
2022-11-02 17:32:20 +08:00
|
|
|
payload.lock.Lock()
|
|
|
|
defer payload.lock.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-payload.stop:
|
|
|
|
return // reject stale update
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
// Ensure the newly provided full block has a higher transaction fee.
|
|
|
|
// In post-merge stage, there is no uncle reward anymore and transaction
|
|
|
|
// fee(apart from the mev revenue) is the only indicator for comparison.
|
2023-08-23 15:16:14 -06:00
|
|
|
if payload.full == nil || r.fees.Cmp(payload.fullFees) > 0 {
|
|
|
|
payload.full = r.block
|
|
|
|
payload.fullFees = r.fees
|
|
|
|
payload.sidecars = r.sidecars
|
|
|
|
|
|
|
|
feesInEther := new(big.Float).Quo(new(big.Float).SetInt(r.fees), big.NewFloat(params.Ether))
|
|
|
|
log.Info("Updated payload",
|
|
|
|
"id", payload.id,
|
|
|
|
"number", r.block.NumberU64(),
|
|
|
|
"hash", r.block.Hash(),
|
|
|
|
"txs", len(r.block.Transactions()),
|
|
|
|
"withdrawals", len(r.block.Withdrawals()),
|
|
|
|
"gas", r.block.GasUsed(),
|
|
|
|
"fees", feesInEther,
|
|
|
|
"root", r.block.Root(),
|
|
|
|
"elapsed", common.PrettyDuration(elapsed),
|
|
|
|
)
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
payload.cond.Broadcast() // fire signal for notifying full block
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve returns the latest built payload and also terminates the background
|
|
|
|
// thread for updating payload. It's safe to be called multiple times.
|
2023-02-06 16:37:58 +01:00
|
|
|
func (payload *Payload) Resolve() *engine.ExecutionPayloadEnvelope {
|
2022-11-02 17:32:20 +08:00
|
|
|
payload.lock.Lock()
|
|
|
|
defer payload.lock.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-payload.stop:
|
|
|
|
default:
|
|
|
|
close(payload.stop)
|
|
|
|
}
|
|
|
|
if payload.full != nil {
|
2023-08-23 15:16:14 -06:00
|
|
|
return engine.BlockToExecutableData(payload.full, payload.fullFees, payload.sidecars)
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
2023-08-23 15:16:14 -06:00
|
|
|
return engine.BlockToExecutableData(payload.empty, big.NewInt(0), nil)
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveEmpty is basically identical to Resolve, but it expects empty block only.
|
|
|
|
// It's only used in tests.
|
2023-02-06 16:37:58 +01:00
|
|
|
func (payload *Payload) ResolveEmpty() *engine.ExecutionPayloadEnvelope {
|
2022-11-02 17:32:20 +08:00
|
|
|
payload.lock.Lock()
|
|
|
|
defer payload.lock.Unlock()
|
|
|
|
|
2023-08-23 15:16:14 -06:00
|
|
|
return engine.BlockToExecutableData(payload.empty, big.NewInt(0), nil)
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveFull is basically identical to Resolve, but it expects full block only.
|
2023-07-06 10:42:34 +02:00
|
|
|
// Don't call Resolve until ResolveFull returns, otherwise it might block forever.
|
2023-02-06 16:37:58 +01:00
|
|
|
func (payload *Payload) ResolveFull() *engine.ExecutionPayloadEnvelope {
|
2022-11-02 17:32:20 +08:00
|
|
|
payload.lock.Lock()
|
|
|
|
defer payload.lock.Unlock()
|
|
|
|
|
|
|
|
if payload.full == nil {
|
|
|
|
select {
|
|
|
|
case <-payload.stop:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2023-07-06 10:42:34 +02:00
|
|
|
// Wait the full payload construction. Note it might block
|
|
|
|
// forever if Resolve is called in the meantime which
|
|
|
|
// terminates the background construction process.
|
2022-11-02 17:32:20 +08:00
|
|
|
payload.cond.Wait()
|
|
|
|
}
|
2023-07-06 10:42:34 +02:00
|
|
|
// Terminate the background payload construction
|
|
|
|
select {
|
|
|
|
case <-payload.stop:
|
|
|
|
default:
|
|
|
|
close(payload.stop)
|
|
|
|
}
|
2023-08-23 15:16:14 -06:00
|
|
|
return engine.BlockToExecutableData(payload.full, payload.fullFees, payload.sidecars)
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildPayload builds the payload according to the provided parameters.
|
|
|
|
func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
|
|
|
|
// Build the initial version with no transaction included. It should be fast
|
|
|
|
// enough to run. The empty payload can at least make sure there is something
|
|
|
|
// to deliver for not missing slot.
|
2023-08-24 00:28:38 +02:00
|
|
|
emptyParams := &generateParams{
|
|
|
|
timestamp: args.Timestamp,
|
|
|
|
forceTime: true,
|
|
|
|
parentHash: args.Parent,
|
|
|
|
coinbase: args.FeeRecipient,
|
|
|
|
random: args.Random,
|
|
|
|
withdrawals: args.Withdrawals,
|
2023-08-26 04:52:12 +02:00
|
|
|
beaconRoot: args.BeaconRoot,
|
2023-08-24 00:28:38 +02:00
|
|
|
noTxs: true,
|
|
|
|
}
|
|
|
|
empty := w.getSealingBlock(emptyParams)
|
2023-08-23 15:16:14 -06:00
|
|
|
if empty.err != nil {
|
|
|
|
return nil, empty.err
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
2023-08-24 00:28:38 +02:00
|
|
|
|
2022-11-02 17:32:20 +08:00
|
|
|
// Construct a payload object for return.
|
2023-08-23 15:16:14 -06:00
|
|
|
payload := newPayload(empty.block, args.Id())
|
2022-11-02 17:32:20 +08:00
|
|
|
|
|
|
|
// Spin up a routine for updating the payload in background. This strategy
|
|
|
|
// can maximum the revenue for including transactions with highest fee.
|
|
|
|
go func() {
|
|
|
|
// Setup the timer for re-building the payload. The initial clock is kept
|
|
|
|
// for triggering process immediately.
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
// Setup the timer for terminating the process if SECONDS_PER_SLOT (12s in
|
|
|
|
// the Mainnet configuration) have passed since the point in time identified
|
|
|
|
// by the timestamp parameter.
|
|
|
|
endTimer := time.NewTimer(time.Second * 12)
|
|
|
|
|
2023-08-24 00:28:38 +02:00
|
|
|
fullParams := &generateParams{
|
|
|
|
timestamp: args.Timestamp,
|
|
|
|
forceTime: true,
|
|
|
|
parentHash: args.Parent,
|
|
|
|
coinbase: args.FeeRecipient,
|
|
|
|
random: args.Random,
|
|
|
|
withdrawals: args.Withdrawals,
|
2023-08-26 04:52:12 +02:00
|
|
|
beaconRoot: args.BeaconRoot,
|
2023-08-24 00:28:38 +02:00
|
|
|
noTxs: false,
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:32:20 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
2022-11-03 22:18:28 +08:00
|
|
|
start := time.Now()
|
2023-08-24 00:28:38 +02:00
|
|
|
r := w.getSealingBlock(fullParams)
|
2023-08-23 15:16:14 -06:00
|
|
|
if r.err == nil {
|
|
|
|
payload.update(r, time.Since(start))
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
timer.Reset(w.recommit)
|
|
|
|
case <-payload.stop:
|
2022-11-07 15:30:54 +01:00
|
|
|
log.Info("Stopping work on payload", "id", payload.id, "reason", "delivery")
|
2022-11-02 17:32:20 +08:00
|
|
|
return
|
|
|
|
case <-endTimer.C:
|
2022-11-07 15:30:54 +01:00
|
|
|
log.Info("Stopping work on payload", "id", payload.id, "reason", "timeout")
|
2022-11-02 17:32:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return payload, nil
|
|
|
|
}
|