Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stagedsync: implement bor span for chain reader and fix loggers #9146

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"sort"
"testing"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
Expand All @@ -35,7 +37,7 @@ import (
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/stages/mock"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/turbo/testlog"
)

// testerAccountPool is a pool to maintain currently active tester accounts,
Expand Down Expand Up @@ -392,6 +394,7 @@ func TestClique(t *testing.T) {
tt := tt

t.Run(tt.name, func(t *testing.T) {
logger := testlog.Logger(t, log.LvlInfo)
// Create the account pool and generate the initial set of signers
accounts := newTesterAccountPool()

Expand Down Expand Up @@ -509,7 +512,13 @@ func TestClique(t *testing.T) {

var snap *clique.Snapshot
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
snap, err = engine.Snapshot(stagedsync.ChainReader{Cfg: config, Db: tx, BlockReader: m.BlockReader}, head.NumberU64(), head.Hash(), nil)
chainReader := stagedsync.ChainReader{
Cfg: config,
Db: tx,
BlockReader: m.BlockReader,
Logger: logger,
}
snap, err = engine.Snapshot(chainReader, head.NumberU64(), head.Hash(), nil)
if err != nil {
return err
}
Expand Down
34 changes: 26 additions & 8 deletions core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import (
"context"
"testing"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/stages/mock"
"github.com/ledgerwatch/erigon/turbo/testlog"
)

// Tests that simple header verification works, for both good and bad blocks.
Expand All @@ -37,6 +41,7 @@ func TestHeaderVerification(t *testing.T) {
gspec = &types.Genesis{Config: params.TestChainConfig}
engine = ethash.NewFaker()
)
logger := testlog.Logger(t, log.LvlInfo)
checkStateRoot := true
m := mock.MockWithGenesisEngine(t, gspec, engine, false, checkStateRoot)

Expand All @@ -48,13 +53,19 @@ func TestHeaderVerification(t *testing.T) {
for i := 0; i < chain.Length(); i++ {
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
for j, valid := range []bool{true, false} {
chainReader := stagedsync.ChainReader{
Cfg: *params.TestChainConfig,
Db: tx,
BlockReader: m.BlockReader,
Logger: logger,
}
var engine consensus.Engine
if valid {
engine := ethash.NewFaker()
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
engine = ethash.NewFaker()
} else {
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
engine = ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
}
err = engine.VerifyHeader(chainReader, chain.Headers[i], true)
if (err == nil) != valid {
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
}
Expand All @@ -79,6 +90,7 @@ func TestHeaderWithSealVerification(t *testing.T) {
gspec = &types.Genesis{Config: params.TestChainAuraConfig}
engine = ethash.NewFaker()
)
logger := testlog.Logger(t, log.LvlInfo)
checkStateRoot := true
m := mock.MockWithGenesisEngine(t, gspec, engine, false, checkStateRoot)

Expand All @@ -91,13 +103,19 @@ func TestHeaderWithSealVerification(t *testing.T) {
for i := 0; i < chain.Length(); i++ {
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
for j, valid := range []bool{true, false} {
chainReader := stagedsync.ChainReader{
Cfg: *params.TestChainAuraConfig,
Db: tx,
BlockReader: m.BlockReader,
Logger: logger,
}
var engine consensus.Engine
if valid {
engine := ethash.NewFaker()
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainAuraConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
engine = ethash.NewFaker()
} else {
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainAuraConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
engine = ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
}
err = engine.VerifyHeader(chainReader, chain.Headers[i], true)
if (err == nil) != valid {
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
}
Expand Down
23 changes: 15 additions & 8 deletions eth/stagedsync/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import (
"context"
"math/big"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/services"
)

// Implements consensus.ChainReader
// ChainReader implements consensus.ChainReader
type ChainReader struct {
Cfg chain.Config

Db kv.Getter
BlockReader services.FullBlockReader
Logger log.Logger
}

// Config retrieves the blockchain's chain configuration.
Expand Down Expand Up @@ -81,10 +82,16 @@ func (cr ChainReader) FrozenBlocks() uint64 {
return cr.BlockReader.FrozenBlocks()
}

func (cr ChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue {
panic("")
func (cr ChainReader) BorEventsByBlock(_ libcommon.Hash, _ uint64) []rlp.RawValue {
panic("bor events by block not implemented")
}

func (cr ChainReader) BorSpan(spanId uint64) []byte {
panic("")
span, err := cr.BlockReader.Span(context.Background(), cr.Db, spanId)
if err != nil {
cr.Logger.Error("BorSpan failed", "err", err)
return nil
}

return span
}
6 changes: 3 additions & 3 deletions eth/stagedsync/stage_bodies.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"runtime"
"time"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/rawdb/blockio"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/dataflow"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/turbo/adapter"
Expand Down Expand Up @@ -134,7 +134,7 @@ func BodiesForward(
prevProgress := bodyProgress
var noProgressCount uint = 0 // How many time the progress was printed without actual progress
var totalDelivered uint64 = 0
cr := ChainReader{Cfg: cfg.chanConfig, Db: tx, BlockReader: cfg.blockReader}
cr := ChainReader{Cfg: cfg.chanConfig, Db: tx, BlockReader: cfg.blockReader, Logger: logger}

loopBody := func() (bool, error) {
// loopCount is used here to ensure we don't get caught in a constant loop of making requests
Expand Down
17 changes: 11 additions & 6 deletions eth/stagedsync/stage_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
"time"

"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/rawdb/blockio"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/rawdb/blockio"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/engineapi/engine_helpers"
"github.com/ledgerwatch/erigon/turbo/services"
Expand Down Expand Up @@ -183,7 +183,12 @@ func HeadersPOW(
}
TEMP TESTING */
headerInserter := headerdownload.NewHeaderInserter(logPrefix, localTd, startProgress, cfg.blockReader)
cfg.hd.SetHeaderReader(&ChainReaderImpl{config: &cfg.chainConfig, tx: tx, blockReader: cfg.blockReader})
cfg.hd.SetHeaderReader(&ChainReaderImpl{
config: &cfg.chainConfig,
tx: tx,
blockReader: cfg.blockReader,
logger: logger,
})

stopped := false
var noProgressCounter uint = 0
Expand Down
8 changes: 4 additions & 4 deletions eth/stagedsync/stage_mining_create_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common/debug"
"github.com/ledgerwatch/erigon/consensus"
Expand All @@ -22,6 +21,7 @@ import (
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethutils"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/services"
)

type MiningBlock struct {
Expand Down Expand Up @@ -127,7 +127,7 @@ func SpawnMiningCreateBlockStage(s *StageState, tx kv.RwTx, cfg MiningCreateBloc
if err != nil {
return err
}
chain := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader}
chain := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader, Logger: logger}
var GetBlocksFromHash = func(hash libcommon.Hash, n int) (blocks []*types.Block) {
number := rawdb.ReadHeaderNumber(tx, hash)
if number == nil {
Expand Down
7 changes: 3 additions & 4 deletions eth/stagedsync/stage_mining_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (

mapset "github.com/deckarep/golang-set/v2"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/kv/membatch"
"github.com/ledgerwatch/log/v3"
"golang.org/x/net/context"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/membatch"
types2 "github.com/ledgerwatch/erigon-lib/types"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
Expand Down Expand Up @@ -89,7 +88,7 @@ func SpawnMiningExecStage(s *StageState, tx kv.RwTx, cfg MiningExecCfg, quit <-c
ibs := state.New(stateReader)
stateWriter := state.NewPlainStateWriter(tx, tx, current.Header.Number.Uint64())

chainReader := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader}
chainReader := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader, Logger: logger}
core.InitializeBlockExecution(cfg.engine, chainReader, current.Header, &cfg.chainConfig, ibs, logger)

// Create an empty block based on temporary copied state for
Expand Down Expand Up @@ -163,7 +162,7 @@ func SpawnMiningExecStage(s *StageState, tx kv.RwTx, cfg MiningExecCfg, quit <-c
}

var err error
_, current.Txs, current.Receipts, err = core.FinalizeBlockExecution(cfg.engine, stateReader, current.Header, current.Txs, current.Uncles, stateWriter, &cfg.chainConfig, ibs, current.Receipts, current.Withdrawals, ChainReaderImpl{config: &cfg.chainConfig, tx: tx, blockReader: cfg.blockReader}, true, logger)
_, current.Txs, current.Receipts, err = core.FinalizeBlockExecution(cfg.engine, stateReader, current.Header, current.Txs, current.Uncles, stateWriter, &cfg.chainConfig, ibs, current.Receipts, current.Withdrawals, ChainReaderImpl{config: &cfg.chainConfig, tx: tx, blockReader: cfg.blockReader, logger: logger}, true, logger)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions eth/stagedsync/stage_mining_finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package stagedsync
import (
"fmt"

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/turbo/builder"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/turbo/builder"
"github.com/ledgerwatch/erigon/turbo/services"
)

type MiningFinishCfg struct {
Expand Down Expand Up @@ -95,7 +95,7 @@ func SpawnMiningFinishStage(s *StageState, tx kv.RwTx, cfg MiningFinishCfg, quit
default:
logger.Trace("No in-flight sealing task.")
}
chain := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader}
chain := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader, Logger: logger}
if err := cfg.engine.Seal(chain, block, cfg.miningState.MiningResultCh, cfg.sealCancel); err != nil {
logger.Warn("Block sealing failed", "err", err)
}
Expand Down
Loading