diff --git a/turbo/jsonrpc/bor_snapshot.go b/turbo/jsonrpc/bor_snapshot.go index 8cd855d9040..fe99ba18b87 100644 --- a/turbo/jsonrpc/bor_snapshot.go +++ b/turbo/jsonrpc/bor_snapshot.go @@ -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) } @@ -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) } @@ -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 @@ -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 @@ -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) } } } @@ -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 } @@ -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) @@ -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 {