Introduce extra functions for BorSpans (no-op) (#8648)

This commit is contained in:
ledgerwatch 2023-11-04 10:59:07 +00:00 committed by GitHub
parent 1ffa3fcf94
commit a77e33e7c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 114 additions and 0 deletions

View File

@ -271,6 +271,9 @@ func (back *RemoteBackend) EventLookup(ctx context.Context, tx kv.Getter, txnHas
func (back *RemoteBackend) EventsByBlock(ctx context.Context, tx kv.Tx, hash common.Hash, blockNum uint64) ([]rlp.RawValue, error) { func (back *RemoteBackend) EventsByBlock(ctx context.Context, tx kv.Tx, hash common.Hash, blockNum uint64) ([]rlp.RawValue, error) {
return back.blockReader.EventsByBlock(ctx, tx, hash, blockNum) return back.blockReader.EventsByBlock(ctx, tx, hash, blockNum)
} }
func (back *RemoteBackend) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]byte, error) {
return back.blockReader.Span(ctx, tx, spanId)
}
func (back *RemoteBackend) NodeInfo(ctx context.Context, limit uint32) ([]p2p.NodeInfo, error) { func (back *RemoteBackend) NodeInfo(ctx context.Context, limit uint32) ([]p2p.NodeInfo, error) {
nodes, err := back.remoteEthBackend.NodeInfo(ctx, &remote.NodesInfoRequest{Limit: limit}) nodes, err := back.remoteEthBackend.NodeInfo(ctx, &remote.NodesInfoRequest{Limit: limit})

View File

@ -286,6 +286,7 @@ func (cr ChainReader) HasBlock(hash libcommon.Hash, number uint64) bool {
func (cr ChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue { func (cr ChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue {
panic("") panic("")
} }
func (cr ChainReader) BorSpan(spanId uint64) []byte { panic("") }
func NewWorkersPool(lock sync.Locker, ctx context.Context, background bool, chainDb kv.RoDB, rs *state.StateV3, in *exec22.QueueWithRetry, blockReader services.FullBlockReader, chainConfig *chain.Config, genesis *types.Genesis, engine consensus.Engine, workerCount int) (reconWorkers []*Worker, applyWorker *Worker, rws *exec22.ResultsQueue, clear func(), wait func()) { func NewWorkersPool(lock sync.Locker, ctx context.Context, background bool, chainDb kv.RoDB, rs *state.StateV3, in *exec22.QueueWithRetry, blockReader services.FullBlockReader, chainConfig *chain.Config, genesis *types.Genesis, engine consensus.Engine, workerCount int) (reconWorkers []*Worker, applyWorker *Worker, rws *exec22.ResultsQueue, clear func(), wait func()) {
reconWorkers = make([]*Worker, workerCount) reconWorkers = make([]*Worker, workerCount)

View File

@ -2,6 +2,7 @@ package bor_test
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"math/big" "math/big"
"testing" "testing"
@ -173,6 +174,11 @@ func (r headerReader) GetTd(libcommon.Hash, uint64) *big.Int {
return nil return nil
} }
func (r headerReader) BorSpan(spanId uint64) []byte {
b, _ := json.Marshal(&r.validator.heimdall.currentSpan)
return b
}
type spanner struct { type spanner struct {
*span.ChainSpanner *span.ChainSpanner
currentSpan span.Span currentSpan span.Span

View File

@ -78,3 +78,11 @@ func (cr ChainReaderImpl) GetTd(hash libcommon.Hash, number uint64) *big.Int {
func (cr ChainReaderImpl) FrozenBlocks() uint64 { func (cr ChainReaderImpl) FrozenBlocks() uint64 {
return cr.BlockReader.FrozenBlocks() return cr.BlockReader.FrozenBlocks()
} }
func (cr ChainReaderImpl) BorSpan(spanId uint64) []byte {
spanBytes, err := cr.BlockReader.Span(context.Background(), cr.Db, spanId)
if err != nil {
log.Error("BorSpan failed", "err", err)
}
return spanBytes
}

View File

@ -55,6 +55,9 @@ type ChainHeaderReader interface {
// Number of blocks frozen in the block snapshots // Number of blocks frozen in the block snapshots
FrozenBlocks() uint64 FrozenBlocks() uint64
// Byte string representation of a bor span with given ID
BorSpan(spanId uint64) []byte
} }
// ChainReader defines a small collection of methods needed to access the local // ChainReader defines a small collection of methods needed to access the local

View File

@ -41,6 +41,10 @@ func (r readerMock) FrozenBlocks() uint64 {
return 0 return 0
} }
func (r readerMock) BorSpan(spanId uint64) []byte {
return nil
}
// The thing only that changes beetwen normal ethash checks other than POW, is difficulty // The thing only that changes beetwen normal ethash checks other than POW, is difficulty
// and nonce so we are gonna test those // and nonce so we are gonna test those
func TestVerifyHeaderDifficulty(t *testing.T) { func TestVerifyHeaderDifficulty(t *testing.T) {

View File

@ -653,3 +653,4 @@ func (cr *FakeChainReader) FrozenBlocks() uint64
func (cr *FakeChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue { func (cr *FakeChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue {
return nil return nil
} }
func (cr *FakeChainReader) BorSpan(spanId uint64) []byte { return nil }

View File

@ -84,3 +84,7 @@ func (cr ChainReader) FrozenBlocks() uint64 {
func (cr ChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue { func (cr ChainReader) BorEventsByBlock(hash libcommon.Hash, number uint64) []rlp.RawValue {
panic("") panic("")
} }
func (cr ChainReader) BorSpan(spanId uint64) []byte {
panic("")
}

View File

@ -568,3 +568,11 @@ func (cr ChainReaderImpl) BorEventsByBlock(hash libcommon.Hash, number uint64) [
} }
return events return events
} }
func (cr ChainReaderImpl) BorSpan(spanId uint64) []byte {
span, err := cr.blockReader.Span(context.Background(), cr.tx, spanId)
if err != nil {
cr.logger.Error("BorSpan failed", "err", err)
return nil
}
return span
}

View File

@ -38,6 +38,10 @@ type BorEventReader interface {
EventsByBlock(ctx context.Context, tx kv.Tx, hash common.Hash, blockNum uint64) ([]rlp.RawValue, error) EventsByBlock(ctx context.Context, tx kv.Tx, hash common.Hash, blockNum uint64) ([]rlp.RawValue, error)
} }
type BorSpanReader interface {
Span(ctx context.Context, tx kv.Getter, spanNum uint64) ([]byte, error)
}
type CanonicalReader interface { type CanonicalReader interface {
CanonicalHash(ctx context.Context, tx kv.Getter, blockNum uint64) (common.Hash, error) CanonicalHash(ctx context.Context, tx kv.Getter, blockNum uint64) (common.Hash, error)
BadHeaderNumber(ctx context.Context, tx kv.Getter, hash common.Hash) (blockHeight *uint64, err error) BadHeaderNumber(ctx context.Context, tx kv.Getter, hash common.Hash) (blockHeight *uint64, err error)
@ -71,6 +75,7 @@ type FullBlockReader interface {
BodyReader BodyReader
HeaderReader HeaderReader
BorEventReader BorEventReader
BorSpanReader
TxnReader TxnReader
CanonicalReader CanonicalReader

View File

@ -235,6 +235,10 @@ func (r *RemoteBlockReader) EventsByBlock(ctx context.Context, tx kv.Tx, hash co
return result, nil return result, nil
} }
func (r *RemoteBlockReader) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]byte, error) {
return nil, nil
}
// BlockReader can read blocks from db and snapshots // BlockReader can read blocks from db and snapshots
type BlockReader struct { type BlockReader struct {
sn *RoSnapshots sn *RoSnapshots
@ -1078,6 +1082,73 @@ func (r *BlockReader) LastFrozenEventID() uint64 {
return lastEventID return lastEventID
} }
func (r *BlockReader) LastFrozenSpanID() uint64 {
view := r.borSn.View()
defer view.Close()
segments := view.Spans()
if len(segments) == 0 {
return 0
}
lastSegment := segments[len(segments)-1]
var lastSpanID uint64
if lastSegment.ranges.to > zerothSpanEnd {
lastSpanID = (lastSegment.ranges.to - zerothSpanEnd - 1) / spanLength
}
return lastSpanID
}
func (r *BlockReader) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]byte, error) {
// Compute starting block of the span
var endBlock uint64
if spanId > 0 {
endBlock = (spanId)*spanLength + zerothSpanEnd
}
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], spanId)
if endBlock >= r.FrozenBorBlocks() {
v, err := tx.GetOne(kv.BorSpans, buf[:])
if err != nil {
return nil, err
}
if v == nil {
return nil, fmt.Errorf("span %d not found (db)", spanId)
}
return common.Copy(v), nil
}
view := r.borSn.View()
defer view.Close()
segments := view.Spans()
for i := len(segments) - 1; i >= 0; i-- {
sn := segments[i]
if sn.idx == nil {
continue
}
var spanFrom uint64
if sn.ranges.from > zerothSpanEnd {
spanFrom = 1 + (sn.ranges.from-zerothSpanEnd-1)/spanLength
}
if spanId < spanFrom {
continue
}
var spanTo uint64
if sn.ranges.to > zerothSpanEnd {
spanTo = 1 + (sn.ranges.to-zerothSpanEnd-1)/spanLength
}
if spanId >= spanTo {
continue
}
if sn.idx.KeyCount() == 0 {
continue
}
offset := sn.idx.OrdinalLookup(spanId - sn.idx.BaseDataID())
gg := sn.seg.MakeGetter()
gg.Reset(offset)
result, _ := gg.Next(nil)
return common.Copy(result), nil
}
return nil, fmt.Errorf("span %d not found (snapshots)", spanId)
}
// ---- Data Integrity part ---- // ---- Data Integrity part ----
func (r *BlockReader) ensureHeaderNumber(n uint64, seg *HeaderSegment) error { func (r *BlockReader) ensureHeaderNumber(n uint64, seg *HeaderSegment) error {