Skip to content

Commit

Permalink
Implement AuRa remoteConsensusEngine (#13158)
Browse files Browse the repository at this point in the history
Cherry pick PR #13141 into `release/2.61`
  • Loading branch information
yperbasis authored Dec 18, 2024
1 parent f4e2b63 commit b74df90
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 27 deletions.
23 changes: 18 additions & 5 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/paths"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/aura"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/core/rawdb"
Expand Down Expand Up @@ -904,6 +905,8 @@ type remoteConsensusEngine struct {
engine consensus.Engine
}

var _ consensus.Engine = (*remoteConsensusEngine)(nil)

func (e *remoteConsensusEngine) HasEngine() bool {
return e.engine != nil
}
Expand Down Expand Up @@ -944,10 +947,18 @@ func (e *remoteConsensusEngine) init(db kv.RoDB, blockReader services.FullBlockR
// TODO(yperbasis): try to unify with CreateConsensusEngine
var eng consensus.Engine
if cc.Aura != nil {
// TODO(yperbasis): support Aura remoteConsensusEngine
return errors.New("aura remoteConsensusEngine is not supported yet")
} else if cc.Clique != nil {
return errors.New("clique remoteConsensusEngine is not supported")
auraKv, err := remotedb.NewRemote(gointerfaces.VersionFromProto(remotedbserver.KvServiceAPIVersion), logger, remoteKV).
WithBucketsConfig(kv.AuRaTablesCfg).
Open()

if err != nil {
return err
}

eng, err = aura.NewRo(cc.Aura, auraKv)
if err != nil {
return err
}
} else if cc.Bor != nil {
borKv, err := remotedb.NewRemote(gointerfaces.VersionFromProto(remotedbserver.KvServiceAPIVersion), logger, remoteKV).
WithBucketsConfig(kv.BorTablesCfg).
Expand All @@ -962,6 +973,8 @@ func (e *remoteConsensusEngine) init(db kv.RoDB, blockReader services.FullBlockR
eng = bor.NewRo(cc, borKv, blockReader,
bor.NewChainSpanner(bor.GenesisContractValidatorSetABI(), cc, true, logger),
bor.NewGenesisContractsClient(cc, borConfig.ValidatorContract, borConfig.StateReceiverContract, logger), logger)
} else if cc.Clique != nil {
return errors.New("clique remoteConsensusEngine is not supported")
} else {
eng = ethash.NewFaker()
}
Expand Down Expand Up @@ -1054,7 +1067,7 @@ func (e *remoteConsensusEngine) Finalize(_ *chain.Config, _ *types.Header, _ *st
panic("remoteConsensusEngine.Finalize not supported")
}

func (e *remoteConsensusEngine) FinalizeAndAssemble(_ *chain.Config, _ *types.Header, _ *state.IntraBlockState, _ types.Transactions, _ []*types.Header, _ types.Receipts, _ []*types.Withdrawal, _ consensus.ChainReader, _ consensus.SystemCall, _ consensus.Call, _ log.Logger) (*types.Block, types.Transactions, types.Receipts, error) {
func (e *remoteConsensusEngine) FinalizeAndAssemble(_ *chain.Config, _ *types.Header, _ *state.IntraBlockState, _ types.Transactions, _ []*types.Header, _ types.Receipts, _ []*types.Withdrawal, _ consensus.ChainReader, _ consensus.SystemCall, _ consensus.Call, _ log.Logger) (*types.Block, types.Transactions, types.Receipts, types.FlatRequests, error) {
panic("remoteConsensusEngine.FinalizeAndAssemble not supported")
}

Expand Down
10 changes: 10 additions & 0 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ func NewAuRa(spec *chain.AuRaConfig, db kv.RwDB) (*AuRa, error) {
return c, nil
}

// NewRo is used by the RPC daemon
func NewRo(spec *chain.AuRaConfig, db kv.RoDB) (*AuRa, error) {
c, err := NewAuRa(spec, kv.RwWrapper{RoDB: db})
if err != nil {
return nil, err
}
c.e.readonly = true
return c, nil
}

// A helper accumulator function mapping a step duration and a step duration transition timestamp
// to the corresponding step number and the correct starting second of the step.
func nextStepTimeDuration(info StepDurationInfo, time uint64) (uint64, uint64, bool) {
Expand Down
9 changes: 8 additions & 1 deletion consensus/aura/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

type NonTransactionalEpochReader struct {
db kv.RwDB
db kv.RwDB
readonly bool
}

func newEpochReader(db kv.RwDB) *NonTransactionalEpochReader {
Expand All @@ -24,6 +25,9 @@ func (cr *NonTransactionalEpochReader) GetEpoch(hash libcommon.Hash, number uint
})
}
func (cr *NonTransactionalEpochReader) PutEpoch(hash libcommon.Hash, number uint64, proof []byte) error {
if cr.readonly {
return nil
}
return cr.db.UpdateNosync(context.Background(), func(tx kv.RwTx) error {
return rawdb.WriteEpoch(tx, number, hash, proof)
})
Expand All @@ -35,6 +39,9 @@ func (cr *NonTransactionalEpochReader) GetPendingEpoch(hash libcommon.Hash, numb
})
}
func (cr *NonTransactionalEpochReader) PutPendingEpoch(hash libcommon.Hash, number uint64, proof []byte) error {
if cr.readonly {
return nil
}
return cr.db.UpdateNosync(context.Background(), func(tx kv.RwTx) error {
return rawdb.WritePendingEpoch(tx, number, hash, proof)
})
Expand Down
19 changes: 19 additions & 0 deletions erigon-lib/kv/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package kv

import (
"context"
"errors"
"fmt"
"os"
"sync"
Expand All @@ -28,6 +29,24 @@ import (
"github.com/ledgerwatch/erigon-lib/common"
)

// Adapts an RoDB to the RwDB interface (invoking write operations results in error)
type RwWrapper struct {
RoDB
}

func (w RwWrapper) Update(ctx context.Context, f func(tx RwTx) error) error {
return errors.New("Update not implemented")
}
func (w RwWrapper) UpdateNosync(ctx context.Context, f func(tx RwTx) error) error {
return errors.New("UpdateNosync not implemented")
}
func (w RwWrapper) BeginRw(ctx context.Context) (RwTx, error) {
return nil, errors.New("BeginRw not implemented")
}
func (w RwWrapper) BeginRwNosync(ctx context.Context) (RwTx, error) {
return nil, errors.New("BeginRwNosync not implemented")
}

func DefaultPageSize() uint64 {
osPageSize := os.Getpagesize()
if osPageSize < 4096 { // reduce further may lead to errors (because some data is just big)
Expand Down
5 changes: 5 additions & 0 deletions erigon-lib/kv/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,11 @@ var ChaindataTablesCfg = TableCfg{
RCodeIdx: {Flags: DupSort},
}

var AuRaTablesCfg = TableCfg{
Epoch: {},
PendingEpoch: {},
}

var BorTablesCfg = TableCfg{
BorReceipts: {Flags: DupSort},
BorFinality: {Flags: DupSort},
Expand Down
22 changes: 1 addition & 21 deletions polygon/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,26 +371,6 @@ func New(
return c
}

type rwWrapper struct {
kv.RoDB
}

func (w rwWrapper) Update(ctx context.Context, f func(tx kv.RwTx) error) error {
return fmt.Errorf("Update not implemented")
}

func (w rwWrapper) UpdateNosync(ctx context.Context, f func(tx kv.RwTx) error) error {
return fmt.Errorf("UpdateNosync not implemented")
}

func (w rwWrapper) BeginRw(ctx context.Context) (kv.RwTx, error) {
return nil, fmt.Errorf("BeginRw not implemented")
}

func (w rwWrapper) BeginRwNosync(ctx context.Context) (kv.RwTx, error) {
return nil, fmt.Errorf("BeginRwNosync not implemented")
}

// This is used by the rpcdaemon and tests which need read only access to the provided data services
func NewRo(chainConfig *chain.Config, db kv.RoDB, blockReader services.FullBlockReader, spanner Spanner,
genesisContracts GenesisContracts, logger log.Logger) *Bor {
Expand All @@ -408,7 +388,7 @@ func NewRo(chainConfig *chain.Config, db kv.RoDB, blockReader services.FullBlock
return &Bor{
chainConfig: chainConfig,
config: borConfig,
DB: rwWrapper{db},
DB: kv.RwWrapper{RoDB: db},
blockReader: blockReader,
logger: logger,
Recents: recents,
Expand Down

0 comments on commit b74df90

Please sign in to comment.