expose error prune history

This commit is contained in:
Sina Mahmoodi 2025-03-24 19:48:41 +01:00 committed by Felix Lange
parent 74de21cdba
commit 959bc4e401
4 changed files with 15 additions and 16 deletions

View file

@ -42,6 +42,12 @@ import (
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
// ErrorPrunedHistory is returned when the requested history is pruned.
type ErrorPrunedHistory struct{}
func (e *ErrorPrunedHistory) Error() string { return "Pruned history unavailable" }
func (e *ErrorPrunedHistory) ErrorCode() int { return 4444 }
// EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes // EthAPIBackend implements ethapi.Backend and tracers.Backend for full nodes
type EthAPIBackend struct { type EthAPIBackend struct {
extRPCEnabled bool extRPCEnabled bool
@ -154,7 +160,7 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
bn = b.eth.blockchain.HistoryPruningCutoff() bn = b.eth.blockchain.HistoryPruningCutoff()
} }
if bn < b.eth.blockchain.HistoryPruningCutoff() { if bn < b.eth.blockchain.HistoryPruningCutoff() {
return nil, &prunedHistoryError{} return nil, &ErrorPrunedHistory{}
} }
return b.eth.blockchain.GetBlockByNumber(bn), nil return b.eth.blockchain.GetBlockByNumber(bn), nil
} }
@ -165,7 +171,7 @@ func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*typ
return nil, nil return nil, nil
} }
if *number < b.eth.blockchain.HistoryPruningCutoff() { if *number < b.eth.blockchain.HistoryPruningCutoff() {
return nil, &prunedHistoryError{} return nil, &ErrorPrunedHistory{}
} }
return b.eth.blockchain.GetBlock(hash, *number), nil return b.eth.blockchain.GetBlock(hash, *number), nil
} }
@ -194,7 +200,7 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r
return nil, errors.New("hash is not currently canonical") return nil, errors.New("hash is not currently canonical")
} }
if header.Number.Uint64() < b.eth.blockchain.HistoryPruningCutoff() { if header.Number.Uint64() < b.eth.blockchain.HistoryPruningCutoff() {
return nil, &prunedHistoryError{} return nil, &ErrorPrunedHistory{}
} }
block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64()) block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
if block == nil { if block == nil {
@ -448,8 +454,3 @@ func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, re
func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) { func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
return b.eth.stateAtTransaction(ctx, block, txIndex, reexec) return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
} }
type prunedHistoryError struct{}
func (e *prunedHistoryError) Error() string { return "Pruned history unavailable" }
func (e *prunedHistoryError) ErrorCode() int { return 4444 }

View file

@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -359,7 +360,7 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type
return nil, errInvalidBlockRange return nil, errInvalidBlockRange
} }
if begin > 0 && begin < int64(api.events.backend.HistoryPruningCutoff()) { if begin > 0 && begin < int64(api.events.backend.HistoryPruningCutoff()) {
return nil, &prunedHistoryError{} return nil, &eth.ErrorPrunedHistory{}
} }
// Construct the range filter // Construct the range filter
filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics) filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics)

View file

@ -27,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/filtermaps" "github.com/ethereum/go-ethereum/core/filtermaps"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -87,7 +88,7 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
return nil, errors.New("unknown block") return nil, errors.New("unknown block")
} }
if header.Number.Uint64() < f.sys.backend.HistoryPruningCutoff() { if header.Number.Uint64() < f.sys.backend.HistoryPruningCutoff() {
return nil, &prunedHistoryError{} return nil, &eth.ErrorPrunedHistory{}
} }
return f.blockLogs(ctx, header) return f.blockLogs(ctx, header)
} }

View file

@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/filtermaps" "github.com/ethereum/go-ethereum/core/filtermaps"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -140,11 +141,6 @@ func (sys *FilterSystem) cachedGetBody(ctx context.Context, elem *logCacheElem,
return body, nil return body, nil
} }
type prunedHistoryError struct{}
func (e *prunedHistoryError) Error() string { return "Pruned history unavailable" }
func (e *prunedHistoryError) ErrorCode() int { return 4444 }
// Type determines the kind of filter and is used to put the filter in to // Type determines the kind of filter and is used to put the filter in to
// the correct bucket when added. // the correct bucket when added.
type Type byte type Type byte
@ -315,7 +311,7 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
} }
// Queries beyond the pruning cutoff are not supported. // Queries beyond the pruning cutoff are not supported.
if uint64(from) < es.backend.HistoryPruningCutoff() { if uint64(from) < es.backend.HistoryPruningCutoff() {
return nil, &prunedHistoryError{} return nil, &eth.ErrorPrunedHistory{}
} }
// only interested in new mined logs // only interested in new mined logs