2022-08-23 07:35:50 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-01-17 12:30:53 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-23 07:35:50 +00:00
|
|
|
"encoding/json"
|
2022-01-17 12:30:53 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-18 17:14:38 +00:00
|
|
|
"math/big"
|
2022-05-31 06:29:49 +00:00
|
|
|
"sync"
|
2022-01-17 12:30:53 +00:00
|
|
|
"time"
|
|
|
|
|
2024-04-23 10:25:52 +00:00
|
|
|
statediff "github.com/cerc-io/plugeth-statediff"
|
2023-09-21 08:01:49 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/database/sql/postgres"
|
2022-01-18 17:14:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/clique"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2022-01-17 12:30:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2024-04-23 10:25:52 +00:00
|
|
|
"github.com/holiman/uint256"
|
2022-01-17 12:30:53 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-01 05:59:53 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
ipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0"
|
|
|
|
ipldeth "github.com/cerc-io/ipld-eth-server/v5/pkg/eth"
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/shared"
|
|
|
|
ipldstate "github.com/cerc-io/ipld-eth-statedb/trie_by_cid/state"
|
2022-08-23 07:35:50 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-db-validator/v5/pkg/prom"
|
2022-01-17 12:30:53 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 17:14:38 +00:00
|
|
|
var (
|
2022-01-20 13:22:01 +00:00
|
|
|
big8 = big.NewInt(8)
|
|
|
|
big32 = big.NewInt(32)
|
2022-01-18 17:14:38 +00:00
|
|
|
)
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
type Service struct {
|
|
|
|
db *sqlx.DB
|
2022-08-23 07:35:50 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
chainConfig *params.ChainConfig
|
2022-08-23 07:35:50 +00:00
|
|
|
ethClient *rpc.Client
|
2023-06-21 23:25:27 +00:00
|
|
|
blockNum, trail uint64
|
|
|
|
retryInterval time.Duration
|
|
|
|
stateDiffMissingBlock bool
|
|
|
|
stateDiffTimeout time.Duration
|
2022-08-23 07:35:50 +00:00
|
|
|
|
|
|
|
quitChan chan bool
|
2023-06-21 23:25:27 +00:00
|
|
|
progressChan chan<- uint64
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
func NewService(cfg *Config, progressChan chan<- uint64) (*Service, error) {
|
|
|
|
db, err := postgres.ConnectSQLX(context.Background(), cfg.DBConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Enable DB stats
|
|
|
|
if cfg.DBStats {
|
|
|
|
prom.RegisterDBCollector(cfg.DBConfig.DatabaseName, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
db: db,
|
|
|
|
chainConfig: cfg.ChainConfig,
|
|
|
|
ethClient: cfg.Client,
|
|
|
|
blockNum: cfg.FromBlock,
|
2022-08-23 07:35:50 +00:00
|
|
|
trail: cfg.Trail,
|
2023-06-21 23:25:27 +00:00
|
|
|
retryInterval: cfg.RetryInterval,
|
2022-08-23 07:35:50 +00:00
|
|
|
stateDiffMissingBlock: cfg.StateDiffMissingBlock,
|
|
|
|
stateDiffTimeout: cfg.StateDiffTimeout,
|
|
|
|
quitChan: make(chan bool),
|
|
|
|
progressChan: progressChan,
|
2022-01-17 12:30:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start is used to begin the service
|
2023-06-21 23:25:27 +00:00
|
|
|
func (s *Service) Start(ctx context.Context, wg *sync.WaitGroup) {
|
2022-05-31 06:29:49 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
api, err := EthAPI(ctx, s.db, s.chainConfig)
|
2022-01-17 12:30:53 +00:00
|
|
|
if err != nil {
|
2022-08-23 07:35:50 +00:00
|
|
|
log.Fatal(err)
|
2022-05-31 06:29:49 +00:00
|
|
|
return
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
nextBlockNum := s.blockNum
|
|
|
|
var delay time.Duration
|
2022-05-31 06:29:49 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.quitChan:
|
2023-06-21 23:25:27 +00:00
|
|
|
log.Info("stopping ipld-eth-db-validator process")
|
2022-06-03 13:28:02 +00:00
|
|
|
if s.progressChan != nil {
|
|
|
|
close(s.progressChan)
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
if err := api.B.Close(); err != nil {
|
|
|
|
log.Errorf("error closing backend: %s", err)
|
|
|
|
}
|
2022-05-31 06:29:49 +00:00
|
|
|
return
|
2023-06-21 23:25:27 +00:00
|
|
|
case <-time.After(delay):
|
|
|
|
err := s.Validate(ctx, api, nextBlockNum)
|
|
|
|
// If chain is not synced, wait for trail to catch up before trying again
|
|
|
|
if notsynced, ok := err.(*ChainNotSyncedError); ok {
|
|
|
|
delay = s.retryInterval
|
|
|
|
log.Infof("waiting %v for chain to advance to block %d (head is at %d)",
|
|
|
|
delay, nextBlockNum+s.trail, notsynced.Head)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-31 06:29:49 +00:00
|
|
|
if err != nil {
|
2022-08-23 07:35:50 +00:00
|
|
|
log.Fatal(err)
|
2022-05-31 06:29:49 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
prom.SetLastValidatedBlock(float64(nextBlockNum))
|
|
|
|
nextBlockNum++
|
|
|
|
delay = 0
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
2022-05-31 06:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 12:30:53 +00:00
|
|
|
|
2022-05-31 06:29:49 +00:00
|
|
|
// Stop is used to gracefully stop the service
|
2023-06-21 23:25:27 +00:00
|
|
|
func (s *Service) Stop() {
|
2022-05-31 06:29:49 +00:00
|
|
|
close(s.quitChan)
|
|
|
|
}
|
2022-01-17 12:30:53 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
func (s *Service) Validate(ctx context.Context, api *ipldeth.PublicEthAPI, idxBlockNum uint64) error {
|
|
|
|
log.Debugf("validating block %d", idxBlockNum)
|
2022-05-31 06:29:49 +00:00
|
|
|
headBlockNum, err := fetchHeadBlockNumber(ctx, api)
|
|
|
|
if err != nil {
|
2023-06-21 23:25:27 +00:00
|
|
|
return err
|
2022-05-31 06:29:49 +00:00
|
|
|
}
|
2022-01-17 12:30:53 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
// Check if block at requested height can be validated
|
|
|
|
if idxBlockNum+s.trail > headBlockNum {
|
|
|
|
return &ChainNotSyncedError{headBlockNum}
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
blockToBeValidated, err := api.B.BlockByNumber(ctx, rpc.BlockNumber(idxBlockNum))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to fetch block at height %d", idxBlockNum)
|
|
|
|
return err
|
2022-08-23 07:35:50 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
// Make a writeStateDiffAt call if block not found in the db
|
|
|
|
if blockToBeValidated == nil {
|
|
|
|
return s.writeStateDiffAt(idxBlockNum)
|
2022-08-23 07:35:50 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
err = ValidateBlock(blockToBeValidated, api.B, idxBlockNum)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to verify state root at block %d", idxBlockNum)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("state root verified for block %d", idxBlockNum)
|
2022-08-23 07:35:50 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
tx := s.db.MustBegin()
|
|
|
|
defer tx.Rollback()
|
|
|
|
err = ValidateReferentialIntegrity(tx, idxBlockNum)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to verify referential integrity at block %d", idxBlockNum)
|
2022-05-31 06:29:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
log.Infof("referential integrity verified for block %d", idxBlockNum)
|
2022-05-31 06:29:49 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
if s.progressChan != nil {
|
|
|
|
s.progressChan <- idxBlockNum
|
|
|
|
}
|
2022-08-23 07:35:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateBlock validates block at the given height
|
2023-06-21 23:25:27 +00:00
|
|
|
func ValidateBlock(blockToBeValidated *types.Block, b *ipldeth.Backend, blockNumber uint64) error {
|
|
|
|
state, err := applyTransactions(blockToBeValidated, b)
|
2022-05-31 06:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
blockStateRoot := blockToBeValidated.Header().Root
|
|
|
|
dbStateRoot := state.IntermediateRoot(true)
|
2022-05-31 06:29:49 +00:00
|
|
|
if blockStateRoot != dbStateRoot {
|
|
|
|
return fmt.Errorf("state roots do not match at block %d", blockNumber)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
func EthAPI(ctx context.Context, db *sqlx.DB, chainCfg *params.ChainConfig) (*ipldeth.PublicEthAPI, error) {
|
2022-01-18 17:14:38 +00:00
|
|
|
// TODO: decide network for custom chainConfig.
|
2023-06-21 23:25:27 +00:00
|
|
|
backend, err := ethBackend(db, &ipldeth.Config{
|
2022-01-20 13:22:01 +00:00
|
|
|
ChainConfig: chainCfg,
|
2023-06-21 23:25:27 +00:00
|
|
|
GroupCacheConfig: &shared.GroupCacheConfig{
|
|
|
|
StateDB: shared.GroupConfig{
|
|
|
|
Name: "cerc_validator",
|
2022-01-17 12:30:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:14:38 +00:00
|
|
|
var genesisBlock *types.Block
|
|
|
|
if backend.Config.ChainConfig == nil {
|
|
|
|
genesisBlock, err = backend.BlockByNumber(ctx, rpc.BlockNumber(0))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
backend.Config.ChainConfig = setChainConfig(genesisBlock.Hash())
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
config := ipldeth.APIConfig{
|
2023-04-01 19:09:13 +00:00
|
|
|
SupportsStateDiff: false,
|
|
|
|
ForwardEthCalls: false,
|
|
|
|
ForwardGetStorageAt: false,
|
|
|
|
ProxyOnError: false,
|
|
|
|
StateDiffTimeout: 0,
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
return ipldeth.NewPublicEthAPI(backend, nil, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ethBackend(db *sqlx.DB, c *ipldeth.Config) (*ipldeth.Backend, error) {
|
|
|
|
gcc := c.GroupCacheConfig
|
|
|
|
|
|
|
|
groupName := gcc.StateDB.Name
|
|
|
|
if groupName == "" {
|
|
|
|
groupName = ipldeth.StateDBGroupCacheName
|
|
|
|
}
|
|
|
|
|
|
|
|
r := ipldeth.NewRetriever(db)
|
|
|
|
ethDB := ipfsethdb.NewDatabase(db, ipfsethdb.CacheConfig{
|
|
|
|
Name: groupName,
|
|
|
|
Size: gcc.StateDB.CacheSizeInMB * 1024 * 1024,
|
|
|
|
ExpiryDuration: time.Minute * time.Duration(gcc.StateDB.CacheExpiryInMins),
|
|
|
|
})
|
|
|
|
// Read only wrapper around ipfs-ethdb eth.Database implementation
|
2024-04-23 10:25:52 +00:00
|
|
|
ethDB = newDatabase(ethDB)
|
2023-06-21 23:25:27 +00:00
|
|
|
|
|
|
|
return &ipldeth.Backend{
|
|
|
|
DB: db,
|
|
|
|
Retriever: r,
|
2024-04-23 10:25:52 +00:00
|
|
|
EthDB: ethDB,
|
|
|
|
IpldTrieStateDatabase: ipldstate.NewDatabase(ethDB),
|
2023-06-21 23:25:27 +00:00
|
|
|
Config: c,
|
|
|
|
}, nil
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 06:29:49 +00:00
|
|
|
// fetchHeadBlockNumber gets the latest block number from the db
|
2023-06-21 23:25:27 +00:00
|
|
|
func fetchHeadBlockNumber(ctx context.Context, api *ipldeth.PublicEthAPI) (uint64, error) {
|
2022-05-31 06:29:49 +00:00
|
|
|
headBlock, err := api.B.BlockByNumber(ctx, rpc.LatestBlockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return headBlock.NumberU64(), nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
// writeStateDiffAt calls out to a statediffing geth client to fill in a gap in the index
|
|
|
|
func (s *Service) writeStateDiffAt(height uint64) error {
|
|
|
|
if !s.stateDiffMissingBlock {
|
|
|
|
return nil
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
var data json.RawMessage
|
|
|
|
params := statediff.Params{
|
|
|
|
IncludeBlock: true,
|
|
|
|
IncludeReceipts: true,
|
|
|
|
IncludeTD: true,
|
|
|
|
IncludeCode: true,
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), s.stateDiffTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
log.Warnf("calling writeStateDiffAt at block %d", height)
|
|
|
|
if err := s.ethClient.CallContext(ctx, &data, "statediff_writeStateDiffAt", height, params); err != nil {
|
|
|
|
log.Errorf("writeStateDiffAt %d failed with err %s", height, err)
|
|
|
|
return err
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-17 12:30:53 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
// applyTransaction attempts to apply block transactions to the given state database
|
|
|
|
// and uses the input parameters for its environment. It returns the stateDB of parent with applied txs.
|
2024-04-23 10:25:52 +00:00
|
|
|
//
|
|
|
|
// Note: this skips the DAO hard fork refund
|
2023-06-21 23:25:27 +00:00
|
|
|
func applyTransactions(block *types.Block, backend *ipldeth.Backend) (*ipldstate.StateDB, error) {
|
|
|
|
if block.NumberU64() == 0 {
|
|
|
|
return nil, errors.New("no transaction in genesis")
|
|
|
|
}
|
2024-04-23 10:25:52 +00:00
|
|
|
config := backend.Config.ChainConfig
|
2023-06-21 23:25:27 +00:00
|
|
|
|
|
|
|
// Create the parent state database
|
|
|
|
parentHash := block.ParentHash()
|
|
|
|
nrOrHash := rpc.BlockNumberOrHash{BlockHash: &parentHash}
|
|
|
|
statedb, _, err := backend.IPLDTrieStateDBAndHeaderByNumberOrHash(context.Background(), nrOrHash)
|
2022-01-17 12:30:53 +00:00
|
|
|
if err != nil {
|
2023-08-10 09:02:50 +00:00
|
|
|
return nil, fmt.Errorf("error accessing state DB: %w", err)
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
var gp core.GasPool
|
|
|
|
gp.AddGas(block.GasLimit())
|
|
|
|
|
|
|
|
blockContext := core.NewEVMBlockContext(block.Header(), backend, getAuthor(backend, block.Header()))
|
2024-04-23 10:25:52 +00:00
|
|
|
evm := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, vm.Config{})
|
|
|
|
signer := types.MakeSigner(config, block.Number(), block.Time())
|
|
|
|
|
|
|
|
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
|
|
|
|
ProcessBeaconBlockRoot(*beaconRoot, evm, statedb)
|
|
|
|
}
|
2023-06-21 23:25:27 +00:00
|
|
|
|
|
|
|
// Iterate over and process the individual transactions
|
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
msg, err := core.TransactionToMessage(tx, signer, block.BaseFee())
|
|
|
|
if err != nil {
|
2023-08-10 09:02:50 +00:00
|
|
|
return nil, fmt.Errorf("error converting transaction to message: %w", err)
|
2023-06-21 23:25:27 +00:00
|
|
|
}
|
|
|
|
statedb.SetTxContext(tx.Hash(), i)
|
2022-01-17 12:30:53 +00:00
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
// Create a new context to be used in the EVM environment.
|
|
|
|
evm.Reset(core.NewEVMTxContext(msg), statedb)
|
|
|
|
// Apply the transaction to the current state (included in the env).
|
|
|
|
if _, err := core.ApplyMessage(evm, msg, &gp); err != nil {
|
2024-07-24 10:50:46 +00:00
|
|
|
return nil, fmt.Errorf("error applying tx %#x: %w", tx.Hash(), err)
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
2024-04-23 10:25:52 +00:00
|
|
|
|
|
|
|
if config.IsByzantium(block.Number()) {
|
|
|
|
statedb.Finalise(true)
|
|
|
|
} else {
|
|
|
|
statedb.IntermediateRoot(config.IsEIP158(block.Number())).Bytes()
|
|
|
|
}
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
2022-01-18 17:14:38 +00:00
|
|
|
|
2024-07-24 10:50:46 +00:00
|
|
|
// Withdrawals processing.
|
|
|
|
for _, w := range block.Withdrawals() {
|
|
|
|
// Convert amount from gwei to wei.
|
|
|
|
amount := new(uint256.Int).SetUint64(w.Amount)
|
|
|
|
amount = amount.Mul(amount, uint256.NewInt(params.GWei))
|
|
|
|
statedb.AddBalance(w.Address, amount)
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:25:52 +00:00
|
|
|
if config.Ethash != nil {
|
|
|
|
accumulateRewards(config, statedb, block.Header(), block.Uncles())
|
2022-01-18 17:14:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
return statedb, nil
|
2022-01-17 12:30:53 +00:00
|
|
|
}
|
2022-01-18 17:14:38 +00:00
|
|
|
|
|
|
|
// accumulateRewards credits the coinbase of the given block with the mining
|
|
|
|
// reward. The total reward consists of the static block reward and rewards for
|
|
|
|
// included uncles. The coinbase of each uncle block is also rewarded.
|
2023-06-21 23:25:27 +00:00
|
|
|
func accumulateRewards(config *params.ChainConfig, state *ipldstate.StateDB, header *types.Header, uncles []*types.Header) {
|
2022-01-18 17:14:38 +00:00
|
|
|
// Select the correct block reward based on chain progression
|
|
|
|
blockReward := ethash.FrontierBlockReward
|
|
|
|
if config.IsByzantium(header.Number) {
|
|
|
|
blockReward = ethash.ByzantiumBlockReward
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.IsConstantinople(header.Number) {
|
|
|
|
blockReward = ethash.ConstantinopleBlockReward
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the rewards for the miner and any included uncles
|
2024-04-23 10:25:52 +00:00
|
|
|
reward := new(big.Int).Set(blockReward.ToBig())
|
2022-01-18 17:14:38 +00:00
|
|
|
r := new(big.Int)
|
|
|
|
for _, uncle := range uncles {
|
|
|
|
r.Add(uncle.Number, big8)
|
|
|
|
r.Sub(r, header.Number)
|
2024-04-23 10:25:52 +00:00
|
|
|
r.Mul(r, blockReward.ToBig())
|
2022-01-18 17:14:38 +00:00
|
|
|
r.Div(r, big8)
|
2024-04-23 10:25:52 +00:00
|
|
|
state.AddBalance(uncle.Coinbase, uint256.MustFromBig(r))
|
2022-01-18 17:14:38 +00:00
|
|
|
|
2024-04-23 10:25:52 +00:00
|
|
|
r.Div(blockReward.ToBig(), big32)
|
2022-01-18 17:14:38 +00:00
|
|
|
reward.Add(reward, r)
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:25:52 +00:00
|
|
|
state.AddBalance(header.Coinbase, uint256.MustFromBig(reward))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessBeaconBlockRoot applies the EIP-4788 system call to the beacon block root
|
|
|
|
// contract. This method is exported to be used in tests.
|
|
|
|
func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *ipldstate.StateDB) {
|
|
|
|
// If EIP-4788 is enabled, we need to invoke the beaconroot storage contract with
|
|
|
|
// the new root
|
|
|
|
msg := &core.Message{
|
|
|
|
From: params.SystemAddress,
|
|
|
|
GasLimit: 30_000_000,
|
|
|
|
GasPrice: common.Big0,
|
|
|
|
GasFeeCap: common.Big0,
|
|
|
|
GasTipCap: common.Big0,
|
|
|
|
To: ¶ms.BeaconRootsStorageAddress,
|
|
|
|
Data: beaconRoot[:],
|
|
|
|
}
|
|
|
|
vmenv.Reset(core.NewEVMTxContext(msg), statedb)
|
|
|
|
statedb.AddAddressToAccessList(params.BeaconRootsStorageAddress)
|
|
|
|
_, _, _ = vmenv.Call(vm.AccountRef(msg.From), *msg.To, msg.Data, 30_000_000, common.U2560)
|
|
|
|
statedb.Finalise(true)
|
2022-01-18 17:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setChainConfig(ghash common.Hash) *params.ChainConfig {
|
|
|
|
switch {
|
|
|
|
case ghash == params.MainnetGenesisHash:
|
|
|
|
return params.MainnetChainConfig
|
|
|
|
case ghash == params.SepoliaGenesisHash:
|
|
|
|
return params.SepoliaChainConfig
|
|
|
|
case ghash == params.GoerliGenesisHash:
|
|
|
|
return params.GoerliChainConfig
|
|
|
|
default:
|
|
|
|
return params.AllEthashProtocolChanges
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
func getAuthor(b *ipldeth.Backend, header *types.Header) *common.Address {
|
2022-01-18 17:14:38 +00:00
|
|
|
author, err := getEngine(b).Author(header)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &author
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:25:27 +00:00
|
|
|
func getEngine(b *ipldeth.Backend) consensus.Engine {
|
2022-01-18 17:14:38 +00:00
|
|
|
// TODO: add logic for other engines
|
|
|
|
if b.Config.ChainConfig.Clique != nil {
|
|
|
|
engine := clique.New(b.Config.ChainConfig.Clique, nil)
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethash.NewFaker()
|
|
|
|
}
|