github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/eth/catalyst/simulated_beacon_api.go (about)

     1  // Copyright 2023 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 catalyst
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/log"
    27  )
    28  
    29  type api struct {
    30  	sim *SimulatedBeacon
    31  }
    32  
    33  func (a *api) loop() {
    34  	var (
    35  		newTxs = make(chan core.NewTxsEvent)
    36  		sub    = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true)
    37  	)
    38  	defer sub.Unsubscribe()
    39  
    40  	for {
    41  		select {
    42  		case <-a.sim.shutdownCh:
    43  			return
    44  		case w := <-a.sim.withdrawals.pending:
    45  			withdrawals := append(a.sim.withdrawals.gatherPending(9), w)
    46  			if err := a.sim.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil {
    47  				log.Warn("Error performing sealing work", "err", err)
    48  			}
    49  		case <-newTxs:
    50  			a.sim.Commit()
    51  		}
    52  	}
    53  }
    54  
    55  func (a *api) AddWithdrawal(ctx context.Context, withdrawal *types.Withdrawal) error {
    56  	return a.sim.withdrawals.add(withdrawal)
    57  }
    58  
    59  func (a *api) SetFeeRecipient(ctx context.Context, feeRecipient common.Address) {
    60  	a.sim.setFeeRecipient(feeRecipient)
    61  }