Skip to content

Commit

Permalink
rpcdaemon: Remove spanProducersReader from snapshot (#12435)
Browse files Browse the repository at this point in the history
Create split between new and old code for easier removal later on. This
also handles divergent behaviour for `bor_getSnapshotProposerSequence`.
  • Loading branch information
shohamc1 authored Oct 24, 2024
1 parent e7949c6 commit 98527e9
Showing 1 changed file with 115 additions and 27 deletions.
142 changes: 115 additions & 27 deletions turbo/jsonrpc/bor_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ func (api *BorImpl) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
}
defer borTx.Rollback()

if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return nil, err
}

snap := &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}

return snap, nil
}

return snapshot(ctx, api, tx, borTx, header)
}

Expand Down Expand Up @@ -157,6 +172,22 @@ func (api *BorImpl) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
return nil, err
}
defer borTx.Rollback()

if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return nil, err
}

snap := &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}

return snap, nil
}

return snapshot(ctx, api, tx, borTx, header)
}

Expand Down Expand Up @@ -195,6 +226,21 @@ func (api *BorImpl) GetSigners(number *rpc.BlockNumber) ([]common.Address, error
}
defer borTx.Rollback()

if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return nil, err
}

snap := &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}

return snap.signers(), nil
}

snap, err := snapshot(ctx, api, tx, borTx, header)
if err != nil {
return nil, err
Expand Down Expand Up @@ -234,6 +280,21 @@ func (api *BorImpl) GetSignersAtHash(hash common.Hash) ([]common.Address, error)
}
defer borTx.Rollback()

if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return nil, err
}

snap := &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}

return snap.signers(), nil
}

snap, err := snapshot(ctx, api, tx, borTx, header)
if err != nil {
return nil, err
Expand Down Expand Up @@ -338,11 +399,11 @@ func (api *BorImpl) GetSnapshotProposer(blockNrOrHash *rpc.BlockNumberOrHash) (c
if blockNr == rpc.LatestBlockNumber {
header = rawdb.ReadCurrentHeader(tx)
} else {
header = rawdb.ReadHeaderByNumber(tx, uint64(blockNr))
header, err = getHeaderByNumber(ctx, blockNr, api, tx)
}
} else {
if blockHash, ok := blockNrOrHash.Hash(); ok {
header, err = rawdb.ReadHeaderByHash(tx, blockHash)
header, err = getHeaderByHash(ctx, api, tx, blockHash)
}
}
}
Expand All @@ -351,12 +412,40 @@ func (api *BorImpl) GetSnapshotProposer(blockNrOrHash *rpc.BlockNumberOrHash) (c
return common.Address{}, errUnknownBlock
}

snapNumber := rpc.BlockNumber(header.Number.Int64() - 1)
snap, err := api.GetSnapshot(&snapNumber)
borEngine, err := api.bor()
if err != nil {
return common.Address{}, err
}

borTx, err := borEngine.DB.BeginRo(ctx)
if err != nil {
return common.Address{}, err
}
defer borTx.Rollback()

var snap *Snapshot
if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return common.Address{}, err
}

snap = &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}
} else {
parent, err := getHeaderByNumber(ctx, rpc.BlockNumber(int64(header.Number.Uint64()-1)), api, tx)
if parent == nil || err != nil {
return common.Address{}, errUnknownBlock
}

snap, err = snapshot(ctx, api, tx, borTx, parent)
if err != nil {
return common.Address{}, err
}
}

return snap.ValidatorSet.GetProposer().Address, nil
}
Expand Down Expand Up @@ -406,18 +495,32 @@ func (api *BorImpl) GetSnapshotProposerSequence(blockNrOrHash *rpc.BlockNumberOr
}
defer borTx.Rollback()

parent, err := getHeaderByNumber(ctx, rpc.BlockNumber(int64(header.Number.Uint64()-1)), api, tx)
if parent == nil || err != nil {
return BlockSigners{}, errUnknownBlock
}
snap, err := snapshot(ctx, api, tx, borTx, parent)
var snap *Snapshot
if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return BlockSigners{}, err
}

var difficulties = make(map[common.Address]uint64)
snap = &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}
} else {
parent, err := getHeaderByNumber(ctx, rpc.BlockNumber(int64(header.Number.Uint64()-1)), api, tx)
if parent == nil || err != nil {
return BlockSigners{}, errUnknownBlock
}

if err != nil {
return BlockSigners{}, err
snap, err = snapshot(ctx, api, tx, borTx, parent)
if err != nil {
return BlockSigners{}, err
}
}

var difficulties = make(map[common.Address]uint64)

proposer := snap.ValidatorSet.GetProposer().Address
proposerIndex, _ := snap.ValidatorSet.GetByAddress(proposer)

Expand Down Expand Up @@ -553,21 +656,6 @@ func snapshot(ctx context.Context, api *BorImpl, db kv.Tx, borDb kv.Tx, header *
number := header.Number.Uint64()
hash := header.Hash()

if api.spanProducersReader != nil {
validatorSet, err := api.spanProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return nil, err
}

snap := &Snapshot{
Number: header.Number.Uint64(),
Hash: header.Hash(),
ValidatorSet: validatorSet,
}

return snap, nil
}

for snap == nil {
// If an on-disk checkpoint snapshot can be found, use that
if number%checkpointInterval == 0 {
Expand Down

0 comments on commit 98527e9

Please sign in to comment.