2023-07-06 08:42:34 +00:00
|
|
|
// Copyright 2023 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 catalyst
|
|
|
|
|
|
|
|
import (
|
2023-08-31 18:37:16 +00:00
|
|
|
"crypto/rand"
|
2023-07-06 08:42:34 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2023-08-21 17:33:37 +00:00
|
|
|
const devEpochLength = 32
|
|
|
|
|
2023-07-06 08:42:34 +00:00
|
|
|
// withdrawalQueue implements a FIFO queue which holds withdrawals that are
|
|
|
|
// pending inclusion.
|
|
|
|
type withdrawalQueue struct {
|
|
|
|
pending chan *types.Withdrawal
|
|
|
|
}
|
|
|
|
|
|
|
|
// add queues a withdrawal for future inclusion.
|
|
|
|
func (w *withdrawalQueue) add(withdrawal *types.Withdrawal) error {
|
|
|
|
select {
|
|
|
|
case w.pending <- withdrawal:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return errors.New("withdrawal queue full")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// gatherPending returns a number of queued withdrawals up to a maximum count.
|
|
|
|
func (w *withdrawalQueue) gatherPending(maxCount int) []*types.Withdrawal {
|
|
|
|
withdrawals := []*types.Withdrawal{}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case withdrawal := <-w.pending:
|
|
|
|
withdrawals = append(withdrawals, withdrawal)
|
|
|
|
if len(withdrawals) == maxCount {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return withdrawals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SimulatedBeacon struct {
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
eth *eth.Ethereum
|
|
|
|
period uint64
|
|
|
|
withdrawals withdrawalQueue
|
|
|
|
|
|
|
|
feeRecipient common.Address
|
|
|
|
feeRecipientLock sync.Mutex // lock gates concurrent access to the feeRecipient
|
|
|
|
|
|
|
|
engineAPI *ConsensusAPI
|
|
|
|
curForkchoiceState engine.ForkchoiceStateV1
|
|
|
|
lastBlockTime uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) {
|
|
|
|
block := eth.BlockChain().CurrentBlock()
|
|
|
|
current := engine.ForkchoiceStateV1{
|
|
|
|
HeadBlockHash: block.Hash(),
|
|
|
|
SafeBlockHash: block.Hash(),
|
|
|
|
FinalizedBlockHash: block.Hash(),
|
|
|
|
}
|
2023-08-24 08:48:09 +00:00
|
|
|
engineAPI := newConsensusAPIWithoutHeartbeat(eth)
|
2023-07-06 08:42:34 +00:00
|
|
|
|
|
|
|
// if genesis block, send forkchoiceUpdated to trigger transition to PoS
|
|
|
|
if block.Number.Sign() == 0 {
|
|
|
|
if _, err := engineAPI.ForkchoiceUpdatedV2(current, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &SimulatedBeacon{
|
|
|
|
eth: eth,
|
|
|
|
period: period,
|
|
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
engineAPI: engineAPI,
|
|
|
|
lastBlockTime: block.Time,
|
|
|
|
curForkchoiceState: current,
|
|
|
|
withdrawals: withdrawalQueue{make(chan *types.Withdrawal, 20)},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SimulatedBeacon) setFeeRecipient(feeRecipient common.Address) {
|
|
|
|
c.feeRecipientLock.Lock()
|
|
|
|
c.feeRecipient = feeRecipient
|
|
|
|
c.feeRecipientLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start invokes the SimulatedBeacon life-cycle function in a goroutine.
|
|
|
|
func (c *SimulatedBeacon) Start() error {
|
|
|
|
if c.period == 0 {
|
|
|
|
go c.loopOnDemand()
|
|
|
|
} else {
|
|
|
|
go c.loop()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop halts the SimulatedBeacon service.
|
|
|
|
func (c *SimulatedBeacon) Stop() error {
|
|
|
|
close(c.shutdownCh)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sealBlock initiates payload building for a new block and creates a new block
|
|
|
|
// with the completed payload.
|
|
|
|
func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal) error {
|
|
|
|
tstamp := uint64(time.Now().Unix())
|
|
|
|
if tstamp <= c.lastBlockTime {
|
|
|
|
tstamp = c.lastBlockTime + 1
|
|
|
|
}
|
|
|
|
c.feeRecipientLock.Lock()
|
|
|
|
feeRecipient := c.feeRecipient
|
|
|
|
c.feeRecipientLock.Unlock()
|
|
|
|
|
2023-08-25 19:38:27 +00:00
|
|
|
// Reset to CurrentBlock in case of the chain was rewound
|
|
|
|
if header := c.eth.BlockChain().CurrentBlock(); c.curForkchoiceState.HeadBlockHash != header.Hash() {
|
|
|
|
finalizedHash := c.finalizedBlockHash(header.Number.Uint64())
|
|
|
|
c.setCurrentState(header.Hash(), *finalizedHash)
|
|
|
|
}
|
|
|
|
|
2023-08-31 18:37:16 +00:00
|
|
|
var random [32]byte
|
|
|
|
rand.Read(random[:])
|
2023-07-06 08:42:34 +00:00
|
|
|
fcResponse, err := c.engineAPI.ForkchoiceUpdatedV2(c.curForkchoiceState, &engine.PayloadAttributes{
|
|
|
|
Timestamp: tstamp,
|
|
|
|
SuggestedFeeRecipient: feeRecipient,
|
|
|
|
Withdrawals: withdrawals,
|
2023-08-31 18:37:16 +00:00
|
|
|
Random: random,
|
2023-07-06 08:42:34 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fcResponse == engine.STATUS_SYNCING {
|
|
|
|
return errors.New("chain rewind prevented invocation of payload creation")
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 12:14:04 +00:00
|
|
|
envelope, err := c.engineAPI.getPayload(*fcResponse.PayloadID, true)
|
2023-07-06 08:42:34 +00:00
|
|
|
if err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
return err
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
|
|
|
payload := envelope.ExecutionPayload
|
|
|
|
|
2023-08-21 17:33:37 +00:00
|
|
|
var finalizedHash common.Hash
|
|
|
|
if payload.Number%devEpochLength == 0 {
|
|
|
|
finalizedHash = payload.BlockHash
|
|
|
|
} else {
|
2023-08-25 19:38:27 +00:00
|
|
|
if fh := c.finalizedBlockHash(payload.Number); fh == nil {
|
|
|
|
return errors.New("chain rewind interrupted calculation of finalized block hash")
|
|
|
|
} else {
|
|
|
|
finalizedHash = *fh
|
|
|
|
}
|
2023-08-21 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 19:38:27 +00:00
|
|
|
// Mark the payload as canon
|
2023-07-06 08:42:34 +00:00
|
|
|
if _, err = c.engineAPI.NewPayloadV2(*payload); err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
return err
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
2023-08-25 19:38:27 +00:00
|
|
|
c.setCurrentState(payload.BlockHash, finalizedHash)
|
|
|
|
// Mark the block containing the payload as canonical
|
2023-07-06 08:42:34 +00:00
|
|
|
if _, err = c.engineAPI.ForkchoiceUpdatedV2(c.curForkchoiceState, nil); err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
return err
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
|
|
|
c.lastBlockTime = payload.Timestamp
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loopOnDemand runs the block production loop for "on-demand" configuration (period = 0)
|
|
|
|
func (c *SimulatedBeacon) loopOnDemand() {
|
|
|
|
var (
|
|
|
|
newTxs = make(chan core.NewTxsEvent)
|
2023-10-04 09:36:36 +00:00
|
|
|
sub = c.eth.TxPool().SubscribeTransactions(newTxs, true)
|
2023-07-06 08:42:34 +00:00
|
|
|
)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.shutdownCh:
|
|
|
|
return
|
|
|
|
case w := <-c.withdrawals.pending:
|
|
|
|
withdrawals := append(c.withdrawals.gatherPending(9), w)
|
|
|
|
if err := c.sealBlock(withdrawals); err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
log.Warn("Error performing sealing work", "err", err)
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
|
|
|
case <-newTxs:
|
|
|
|
withdrawals := c.withdrawals.gatherPending(10)
|
|
|
|
if err := c.sealBlock(withdrawals); err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
log.Warn("Error performing sealing work", "err", err)
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 19:38:27 +00:00
|
|
|
// loop runs the block production loop for non-zero period configuration
|
2023-07-06 08:42:34 +00:00
|
|
|
func (c *SimulatedBeacon) loop() {
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.shutdownCh:
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
withdrawals := c.withdrawals.gatherPending(10)
|
|
|
|
if err := c.sealBlock(withdrawals); err != nil {
|
2023-08-25 19:38:27 +00:00
|
|
|
log.Warn("Error performing sealing work", "err", err)
|
|
|
|
} else {
|
|
|
|
timer.Reset(time.Second * time.Duration(c.period))
|
2023-07-06 08:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 19:38:27 +00:00
|
|
|
// finalizedBlockHash returns the block hash of the finalized block corresponding to the given number
|
|
|
|
// or nil if doesn't exist in the chain.
|
|
|
|
func (c *SimulatedBeacon) finalizedBlockHash(number uint64) *common.Hash {
|
|
|
|
var finalizedNumber uint64
|
|
|
|
if number%devEpochLength == 0 {
|
|
|
|
finalizedNumber = number
|
|
|
|
} else {
|
|
|
|
finalizedNumber = (number - 1) / devEpochLength * devEpochLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if finalizedBlock := c.eth.BlockChain().GetBlockByNumber(finalizedNumber); finalizedBlock != nil {
|
|
|
|
fh := finalizedBlock.Hash()
|
|
|
|
return &fh
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCurrentState sets the current forkchoice state
|
|
|
|
func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) {
|
|
|
|
c.curForkchoiceState = engine.ForkchoiceStateV1{
|
|
|
|
HeadBlockHash: headHash,
|
|
|
|
SafeBlockHash: headHash,
|
|
|
|
FinalizedBlockHash: finalizedHash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 08:42:34 +00:00
|
|
|
func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) {
|
|
|
|
stack.RegisterAPIs([]rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "dev",
|
|
|
|
Service: &api{sim},
|
|
|
|
Version: "1.0",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|