Skip to content

Commit 055d08e

Browse files
committed
remove redundant uses of ensureHeightAvailable
1 parent 51a5d3e commit 055d08e

7 files changed

Lines changed: 36 additions & 133 deletions

File tree

evmrpc/block.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,16 @@ func (a *BlockAPI) GetBlockTransactionCountByNumber(ctx context.Context, number
127127
if err != nil {
128128
return nil, err
129129
}
130-
if err := a.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
131-
return nil, err
132-
}
133130
return a.getEvmTxCount(block.Block.Txs, block.Block.Height), nil
134131
}
135132

136133
func (a *BlockAPI) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) (result *hexutil.Uint, returnErr error) {
137134
startTime := time.Now()
138135
defer recordMetricsWithError(fmt.Sprintf("%s_getBlockTransactionCountByHash", a.namespace), a.connectionType, startTime, returnErr)
139-
block, err := blockByHashWithRetry(ctx, a.tmClient, blockHash[:], 1)
136+
block, err := blockByHashRespectingWatermarks(ctx, a.tmClient, a.watermarks, blockHash[:], 1)
140137
if err != nil {
141138
return nil, err
142139
}
143-
if err := a.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
144-
return nil, err
145-
}
146140
return a.getEvmTxCount(block.Block.Txs, block.Block.Height), nil
147141
}
148142

@@ -154,13 +148,10 @@ func (a *BlockAPI) GetBlockByHash(ctx context.Context, blockHash common.Hash, fu
154148
func (a *BlockAPI) getBlockByHash(ctx context.Context, blockHash common.Hash, fullTx bool, includeSyntheticTxs bool, isPanicTx func(ctx context.Context, hash common.Hash) (bool, error)) (result map[string]interface{}, returnErr error) {
155149
startTime := time.Now()
156150
defer recordMetricsWithError(fmt.Sprintf("%s_getBlockByHash", a.namespace), a.connectionType, startTime, returnErr)
157-
block, err := blockByHashWithRetry(ctx, a.tmClient, blockHash[:], 1)
151+
block, err := blockByHashRespectingWatermarks(ctx, a.tmClient, a.watermarks, blockHash[:], 1)
158152
if err != nil {
159153
return nil, err
160154
}
161-
if err := a.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
162-
return nil, err
163-
}
164155

165156
// Validate EVM block height for pacific-1 chain
166157
sdkCtx := a.ctxProvider(LatestCtxHeight)
@@ -230,23 +221,13 @@ func (a *BlockAPI) getBlockByNumber(
230221
if err != nil {
231222
return nil, err
232223
}
233-
if err := a.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
234-
return nil, err
235-
}
236224
blockRes, err := blockResultsWithRetry(ctx, a.tmClient, &block.Block.Height)
237225
if err != nil {
238226
return nil, err
239227
}
240228
return EncodeTmBlock(a.ctxProvider, a.txConfigProvider, block, blockRes, a.keeper, fullTx, a.includeBankTransfers, includeSyntheticTxs, isPanicTx)
241229
}
242230

243-
func (a *BlockAPI) ensureHeightAvailable(ctx context.Context, height int64) error {
244-
if a.watermarks == nil {
245-
return nil
246-
}
247-
return a.watermarks.EnsureHeightAvailable(ctx, height)
248-
}
249-
250231
func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (result []map[string]interface{}, returnErr error) {
251232
startTime := time.Now()
252233
defer recordMetricsWithError(fmt.Sprintf("%s_getBlockReceipts", a.namespace), a.connectionType, startTime, returnErr)
@@ -263,9 +244,6 @@ func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.Block
263244

264245
// Get all tx hashes for the block
265246
height := block.Block.Height
266-
if err := a.ensureHeightAvailable(ctx, height); err != nil {
267-
return nil, err
268-
}
269247
txHashes := getTxHashesFromBlock(a.ctxProvider, a.txConfigProvider, a.keeper, block, shouldIncludeSynthetic(a.namespace))
270248
// Get tx receipts for all hashes in parallel
271249
wg := sync.WaitGroup{}

evmrpc/filter.go

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -834,45 +834,12 @@ func (f *LogFetcher) mergeSortedLogs(batches [][]*ethtypes.Log) []*ethtypes.Log
834834
return res
835835
}
836836

837-
func (f *LogFetcher) ensureHeightAvailable(ctx context.Context, height int64) error {
838-
if f.watermarks == nil {
839-
return nil
840-
}
841-
return f.watermarks.EnsureHeightAvailable(ctx, height)
842-
}
843-
844837
func (f *LogFetcher) latestHeight(ctx context.Context) (int64, error) {
845-
if f.watermarks != nil {
846-
return f.watermarks.LatestHeight(ctx)
847-
}
848-
if f.ctxProvider == nil {
849-
return 0, fmt.Errorf("ctx provider not configured")
850-
}
851-
return f.ctxProvider(LatestCtxHeight).BlockHeight(), nil
838+
return f.watermarks.LatestHeight(ctx)
852839
}
853840

854841
func (f *LogFetcher) earliestHeight(ctx context.Context) (int64, error) {
855-
if f.watermarks != nil {
856-
return f.watermarks.EarliestHeight(ctx)
857-
}
858-
if f.ctxProvider == nil {
859-
return 0, fmt.Errorf("ctx provider not configured")
860-
}
861-
storeCtx := f.ctxProvider(LatestCtxHeight)
862-
ms := storeCtx.MultiStore()
863-
if ms == nil {
864-
return 0, nil
865-
}
866-
earliest := int64(0)
867-
func() {
868-
defer func() {
869-
if r := recover(); r != nil {
870-
earliest = 0
871-
}
872-
}()
873-
earliest = ms.GetEarliestVersion()
874-
}()
875-
return earliest, nil
842+
return f.watermarks.EarliestHeight(ctx)
876843
}
877844

878845
// Pooled version that reuses slice allocation
@@ -957,30 +924,25 @@ func (f *LogFetcher) fetchBlocksByCrit(ctx context.Context, crit filters.FilterC
957924
return res, 0, false, nil
958925
}
959926

960-
block, err := blockByHashWithRetry(ctx, f.tmClient, crit.BlockHash[:], 1)
927+
block, err := blockByHashRespectingWatermarks(ctx, f.tmClient, f.watermarks, crit.BlockHash[:], 1)
961928
if err != nil {
962929
// For non-existent blocks, return empty channel instead of error
963930
res := make(chan *coretypes.ResultBlock)
964931
close(res)
965932
return res, 0, false, nil
966933
}
967-
if err := f.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
968-
res := make(chan *coretypes.ResultBlock)
969-
close(res)
970-
return res, 0, false, nil
971-
}
972934
res := make(chan *coretypes.ResultBlock, 1)
973935
res <- block
974936
close(res)
975937
return res, 0, false, nil
976938
}
977939

978940
applyOpenEndedLogLimit := f.filterConfig.maxLog > 0 && (crit.FromBlock == nil || crit.ToBlock == nil)
979-
latest, err := f.latestHeight(ctx)
941+
latest, err := f.watermarks.LatestHeight(ctx)
980942
if err != nil {
981943
return nil, 0, false, err
982944
}
983-
earliest, err := f.earliestHeight(ctx)
945+
earliest, err := f.watermarks.EarliestHeight(ctx)
984946
if err != nil {
985947
earliest = 0
986948
}
@@ -1059,7 +1021,7 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10591021
// check cache first, without holding the semaphore
10601022
if cachedEntry, found := f.globalBlockCache.Get(height); found {
10611023
if cachedEntry.Block != nil {
1062-
if err := f.ensureHeightAvailable(ctx, cachedEntry.Block.Block.Height); err != nil {
1024+
if err := f.watermarks.EnsureHeightAvailable(ctx, cachedEntry.Block.Block.Height); err != nil {
10631025
continue
10641026
}
10651027
}
@@ -1074,7 +1036,7 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
10741036
if cachedEntry, found := f.globalBlockCache.Get(height); found {
10751037
<-f.dbReadSemaphore
10761038
if cachedEntry.Block != nil {
1077-
if err := f.ensureHeightAvailable(ctx, cachedEntry.Block.Block.Height); err != nil {
1039+
if err := f.watermarks.EnsureHeightAvailable(ctx, cachedEntry.Block.Block.Height); err != nil {
10781040
continue
10791041
}
10801042
}
@@ -1109,10 +1071,6 @@ func (f *LogFetcher) processBatch(ctx context.Context, start, end int64, crit fi
11091071
<-f.dbReadSemaphore
11101072
continue
11111073
}
1112-
if err := f.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
1113-
<-f.dbReadSemaphore
1114-
continue
1115-
}
11161074

11171075
// Use LoadOrStore to create/get cache entry atomically
11181076
entry := loadOrStoreCacheEntry(&f.cacheCreationMutex, f.globalBlockCache, height, block)

evmrpc/state.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,11 @@ func (a *StateAPI) GetProof(ctx context.Context, address common.Address, storage
106106
}
107107
block, err = blockByNumberRespectingWatermarks(ctx, a.tmClient, a.watermarks, blockNumber, 1)
108108
} else {
109-
block, err = blockByHash(ctx, a.tmClient, blockNrOrHash.BlockHash[:])
109+
block, err = blockByHashRespectingWatermarks(ctx, a.tmClient, a.watermarks, blockNrOrHash.BlockHash[:], 1)
110110
}
111111
if err != nil {
112112
return nil, err
113113
}
114-
if err := a.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
115-
return nil, err
116-
}
117114
sdkCtx := a.ctxProvider(block.Block.Height)
118115
if err := CheckVersion(sdkCtx, a.keeper); err != nil {
119116
return nil, err
@@ -159,40 +156,7 @@ func (a *StateAPI) GetNonce(_ context.Context, address common.Address) uint64 {
159156
}
160157

161158
func (a *StateAPI) resolveHeight(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (int64, error) {
162-
if a.watermarks != nil {
163-
return a.watermarks.ResolveHeight(ctx, blockNrOrHash)
164-
}
165-
heightPtr, err := GetBlockNumberByNrOrHash(ctx, a.tmClient, blockNrOrHash)
166-
if err != nil {
167-
return 0, err
168-
}
169-
if heightPtr == nil {
170-
return a.ctxProvider(LatestCtxHeight).BlockHeight(), nil
171-
}
172-
if err := a.ensureHeightAvailable(ctx, *heightPtr); err != nil {
173-
return 0, err
174-
}
175-
return *heightPtr, nil
176-
}
177-
178-
func (a *StateAPI) ensureHeightAvailable(ctx context.Context, height int64) error {
179-
if a.watermarks != nil {
180-
return a.watermarks.EnsureHeightAvailable(ctx, height)
181-
}
182-
if a.tmClient == nil {
183-
return nil
184-
}
185-
status, err := a.tmClient.Status(ctx)
186-
if err != nil {
187-
return err
188-
}
189-
if height > status.SyncInfo.LatestBlockHeight {
190-
return fmt.Errorf("requested block height %d is not yet available; safe latest is %d", height, status.SyncInfo.LatestBlockHeight)
191-
}
192-
if height < status.SyncInfo.EarliestBlockHeight {
193-
return fmt.Errorf("requested block height %d has been pruned; earliest available is %d", height, status.SyncInfo.EarliestBlockHeight)
194-
}
195-
return nil
159+
return a.watermarks.ResolveHeight(ctx, blockNrOrHash)
196160
}
197161

198162
// decodeHash parses a hex-encoded 32-byte hash. The input may optionally

evmrpc/state_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ func TestGetProof(t *testing.T) {
216216
_, err := testApp.Commit(context.Background())
217217
require.Nil(t, err)
218218
}
219-
stateAPI := evmrpc.NewStateAPI(&MockClient{}, &testApp.EvmKeeper, func(int64) sdk.Context { return testApp.GetCheckCtx() }, evmrpc.ConnectionTypeHTTP, nil)
219+
client := &MockClient{}
220+
watermarks := evmrpc.NewWatermarkManager(client, func(int64) sdk.Context { return testApp.GetCheckCtx() }, nil, testApp.EvmKeeper.ReceiptStore())
221+
stateAPI := evmrpc.NewStateAPI(client, &testApp.EvmKeeper, func(int64) sdk.Context { return testApp.GetCheckCtx() }, evmrpc.ConnectionTypeHTTP, watermarks)
220222
require.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000616263", testApp.EvmKeeper.GetState(testApp.GetCheckCtx(), evmAddr, common.BytesToHash(key)).Hex())
221223
tests := []struct {
222224
key string

evmrpc/tx.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ func getTransactionReceipt(
116116
if err != nil {
117117
return nil, err
118118
}
119-
if err := t.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
120-
return nil, err
121-
}
122119

123120
// Find the transaction in the block
124121
for _, tx := range block.Block.Txs {
@@ -184,22 +181,16 @@ func (t *TransactionAPI) getTransactionByBlockNumberAndIndex(ctx context.Context
184181
if err != nil {
185182
return nil, err
186183
}
187-
if err := t.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
188-
return nil, err
189-
}
190184
return t.getTransactionWithBlock(block, txIndex, t.includeSynthetic)
191185
}
192186

193187
func (t *TransactionAPI) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint) (result *export.RPCTransaction, returnErr error) {
194188
startTime := time.Now()
195189
defer recordMetricsWithError("eth_getTransactionByBlockHashAndIndex", t.connectionType, startTime, returnErr)
196-
block, err := blockByHash(ctx, t.tmClient, blockHash[:])
190+
block, err := blockByHashRespectingWatermarks(ctx, t.tmClient, t.watermarks, blockHash[:], 1)
197191
if err != nil {
198192
return nil, err
199193
}
200-
if err := t.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
201-
return nil, err
202-
}
203194
var idx uint32
204195
idx, returnErr = txIndexToUint32(txIndex)
205196
if returnErr != nil {
@@ -259,9 +250,6 @@ func (t *TransactionAPI) GetTransactionByHash(ctx context.Context, hash common.H
259250
if err != nil {
260251
return nil, err
261252
}
262-
if err := t.ensureHeightAvailable(ctx, block.Block.Height); err != nil {
263-
return nil, err
264-
}
265253
filteredMsgs := t.getFilteredMsgs(block)
266254
txIndex, found, ethtx, _ := GetEvmTxIndex(t.ctxProvider(LatestCtxHeight), block, filteredMsgs, receipt.TransactionIndex, t.keeper)
267255
if !found {
@@ -273,13 +261,6 @@ func (t *TransactionAPI) GetTransactionByHash(ctx context.Context, hash common.H
273261
return t.encodeRPCTransaction(ethtx, block, uint32(txIndex)) //nolint:gosec
274262
}
275263

276-
func (t *TransactionAPI) ensureHeightAvailable(ctx context.Context, height int64) error {
277-
if t.watermarks == nil {
278-
return nil
279-
}
280-
return t.watermarks.EnsureHeightAvailable(ctx, height)
281-
}
282-
283264
func (t *TransactionAPI) GetTransactionErrorByHash(_ context.Context, hash common.Hash) (result string, returnErr error) {
284265
startTime := time.Now()
285266
defer recordMetricsWithError("eth_getTransactionErrorByHash", t.connectionType, startTime, returnErr)

evmrpc/tx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestGetTransactionError(t *testing.T) {
9191

9292
func TestSign(t *testing.T) {
9393
homeDir := t.TempDir()
94-
txApi := evmrpc.NewTransactionAPI(nil, nil, nil, nil, homeDir, evmrpc.ConnectionTypeHTTP, nil)
94+
txApi := evmrpc.NewTransactionAPI(nil, nil, nil, nil, homeDir, evmrpc.ConnectionTypeHTTP, &evmrpc.WatermarkManager{})
9595
infoApi := evmrpc.NewInfoAPI(nil, nil, nil, nil, homeDir, 1024, evmrpc.ConnectionTypeHTTP, nil, nil)
9696
clientCtx := client.Context{}.WithViper("").WithHomeDir(homeDir)
9797
clientCtx, err := config.ReadFromClientConfig(clientCtx)

evmrpc/watermark_manager.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ func blockByNumberRespectingWatermarks(
225225
return blockByNumberWithRetry(ctx, client, heightPtr, maxRetries)
226226
}
227227

228+
func blockByHashRespectingWatermarks(
229+
ctx context.Context,
230+
client rpcclient.Client,
231+
wm *WatermarkManager,
232+
hash []byte,
233+
maxRetries int,
234+
) (*coretypes.ResultBlock, error) {
235+
block, err := blockByHashWithRetry(ctx, client, hash, maxRetries)
236+
if err != nil {
237+
return nil, err
238+
}
239+
if wm == nil {
240+
return block, nil
241+
}
242+
if err := wm.EnsureHeightAvailable(ctx, block.Block.Height); err != nil {
243+
return nil, err
244+
}
245+
return block, nil
246+
}
247+
228248
func (m *WatermarkManager) fetchTendermintWatermarks(ctx context.Context) (int64, int64, error) {
229249
if m.tmClient == nil {
230250
return 0, 0, errNoHeightSource

0 commit comments

Comments
 (0)