Skip to content

Commit 1a369ec

Browse files
committed
eth: update apis with pruning cutoff
1 parent ffba5f8 commit 1a369ec

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

eth/api_backend.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
3030
"github.com/ethereum/go-ethereum/core"
3131
"github.com/ethereum/go-ethereum/core/filtermaps"
32+
"github.com/ethereum/go-ethereum/core/history"
3233
"github.com/ethereum/go-ethereum/core/rawdb"
3334
"github.com/ethereum/go-ethereum/core/state"
3435
"github.com/ethereum/go-ethereum/core/systemcontracts"
@@ -94,7 +95,13 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
9495
}
9596
return block, nil
9697
}
97-
return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
98+
var bn uint64
99+
if number == rpc.EarliestBlockNumber {
100+
bn = b.HistoryPruningCutoff()
101+
} else {
102+
bn = uint64(number)
103+
}
104+
return b.eth.blockchain.GetHeaderByNumber(bn), nil
98105
}
99106

100107
func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
@@ -146,22 +153,42 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
146153
}
147154
return b.eth.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
148155
}
149-
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
156+
bn := uint64(number) // the resolved number
157+
if number == rpc.EarliestBlockNumber {
158+
bn = b.HistoryPruningCutoff()
159+
}
160+
block := b.eth.blockchain.GetBlockByNumber(bn)
161+
if block == nil && bn < b.HistoryPruningCutoff() {
162+
return nil, &history.PrunedHistoryError{}
163+
}
164+
return block, nil
150165
}
151166

152167
func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
153-
return b.eth.blockchain.GetBlockByHash(hash), nil
168+
number := b.eth.blockchain.GetBlockNumber(hash)
169+
if number == nil {
170+
return nil, nil
171+
}
172+
block := b.eth.blockchain.GetBlock(hash, *number)
173+
if block == nil && *number < b.HistoryPruningCutoff() {
174+
return nil, &history.PrunedHistoryError{}
175+
}
176+
return block, nil
154177
}
155178

156179
// GetBody returns body of a block. It does not resolve special block numbers.
157180
func (b *EthAPIBackend) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
158181
if number < 0 || hash == (common.Hash{}) {
159182
return nil, errors.New("invalid arguments; expect hash and no special block numbers")
160183
}
161-
if body := b.eth.blockchain.GetBody(hash); body != nil {
162-
return body, nil
184+
body := b.eth.blockchain.GetBody(hash)
185+
if body == nil {
186+
if uint64(number) < b.HistoryPruningCutoff() {
187+
return nil, &history.PrunedHistoryError{}
188+
}
189+
return nil, errors.New("block body not found")
163190
}
164-
return nil, errors.New("block body not found")
191+
return body, nil
165192
}
166193

167194
func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
@@ -178,6 +205,9 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r
178205
}
179206
block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
180207
if block == nil {
208+
if header.Number.Uint64() < b.HistoryPruningCutoff() {
209+
return nil, &history.PrunedHistoryError{}
210+
}
181211
return nil, errors.New("header found, but block body is missing")
182212
}
183213
return block, nil
@@ -237,6 +267,11 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN
237267
return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
238268
}
239269

270+
func (b *EthAPIBackend) HistoryPruningCutoff() uint64 {
271+
bn, _ := b.eth.blockchain.HistoryPruningCutoff()
272+
return bn
273+
}
274+
240275
func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
241276
return b.eth.blockchain.GetReceiptsByHash(hash), nil
242277
}
@@ -418,10 +453,6 @@ func (b *EthAPIBackend) ChainDb() ethdb.Database {
418453
return b.eth.ChainDb()
419454
}
420455

421-
func (b *EthAPIBackend) EventMux() *event.TypeMux {
422-
return b.eth.EventMux()
423-
}
424-
425456
func (b *EthAPIBackend) AccountManager() *accounts.Manager {
426457
return b.eth.AccountManager()
427458
}

0 commit comments

Comments
 (0)