Skip to content

Commit 51a5d3e

Browse files
committed
fix suggestions
1 parent 684779e commit 51a5d3e

8 files changed

Lines changed: 83 additions & 97 deletions

File tree

evmrpc/config.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ type Config struct {
7575
// controls whether to synchronously flush receipts before block finalze or not
7676
FlushReceiptSync bool `mapstructure:"flush_receipt_sync"`
7777

78-
// DisableWatermark disables cross-store watermark gating (intended for tests)
79-
DisableWatermark bool `mapstructure:"disable_watermark"`
80-
8178
// Deny list defines list of methods that EVM RPC should fail fast
8279
DenyList []string `mapstructure:"deny_list"`
8380

@@ -129,7 +126,6 @@ var DefaultConfig = Config{
129126
MaxTxPoolTxs: 1000,
130127
Slow: false,
131128
FlushReceiptSync: false,
132-
DisableWatermark: false,
133129
DenyList: make([]string, 0),
134130
MaxLogNoBlock: 10000,
135131
MaxBlocksForLog: 2000,
@@ -160,7 +156,6 @@ const (
160156
flagCheckTxTimeout = "evm.checktx_timeout"
161157
flagSlow = "evm.slow"
162158
FlagFlushReceiptSync = "evm.flush_receipt_sync"
163-
flagDisableWatermark = "evm.disable_watermark"
164159
flagDenyList = "evm.deny_list"
165160
flagMaxLogNoBlock = "evm.max_log_no_block"
166161
flagMaxBlocksForLog = "evm.max_blocks_for_log"
@@ -261,11 +256,6 @@ func ReadConfig(opts servertypes.AppOptions) (Config, error) {
261256
return cfg, err
262257
}
263258
}
264-
if v := opts.Get(flagDisableWatermark); v != nil {
265-
if cfg.DisableWatermark, err = cast.ToBoolE(v); err != nil {
266-
return cfg, err
267-
}
268-
}
269259
if v := opts.Get(flagDenyList); v != nil {
270260
if cfg.DenyList, err = cast.ToStringSliceE(v); err != nil {
271261
return cfg, err

evmrpc/info.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,19 +394,45 @@ func (i *InfoAPI) CalculateGasUsedRatio(ctx context.Context, blockHeight int64)
394394
}
395395

396396
func (i *InfoAPI) latestHeight(ctx context.Context) (int64, error) {
397-
if i.watermarks != nil {
398-
return i.watermarks.LatestHeight(ctx)
397+
return i.heightWithWatermarks(ctx, func(c context.Context) (int64, error) {
398+
return i.watermarks.LatestHeight(c)
399+
}, i.latestHeightFromContext)
400+
}
401+
402+
func (i *InfoAPI) earliestHeight(ctx context.Context) (int64, error) {
403+
return i.heightWithWatermarks(ctx, func(c context.Context) (int64, error) {
404+
return i.watermarks.EarliestHeight(c)
405+
}, i.earliestHeightFromContext)
406+
}
407+
408+
func (i *InfoAPI) heightWithWatermarks(
409+
ctx context.Context,
410+
wmFunc func(context.Context) (int64, error),
411+
fallback func() (int64, error),
412+
) (int64, error) {
413+
if i.watermarks != nil && wmFunc != nil {
414+
height, err := wmFunc(ctx)
415+
if err == nil {
416+
return height, nil
417+
}
418+
if !errors.Is(err, errNoHeightSource) {
419+
return 0, err
420+
}
421+
}
422+
if fallback == nil {
423+
return 0, fmt.Errorf("ctx provider not configured")
399424
}
425+
return fallback()
426+
}
427+
428+
func (i *InfoAPI) latestHeightFromContext() (int64, error) {
400429
if i.ctxProvider == nil {
401430
return 0, fmt.Errorf("ctx provider not configured")
402431
}
403432
return i.ctxProvider(LatestCtxHeight).BlockHeight(), nil
404433
}
405434

406-
func (i *InfoAPI) earliestHeight(ctx context.Context) (int64, error) {
407-
if i.watermarks != nil {
408-
return i.watermarks.EarliestHeight(ctx)
409-
}
435+
func (i *InfoAPI) earliestHeightFromContext() (int64, error) {
410436
if i.ctxProvider == nil {
411437
return 0, fmt.Errorf("ctx provider not configured")
412438
}

evmrpc/server.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ func NewEVMHTTPServer(
6464

6565
ctx := ctxProvider(LatestCtxHeight)
6666

67-
var watermarks *WatermarkManager
68-
if !config.DisableWatermark {
69-
watermarks = NewWatermarkManager(tmClient, ctxProvider, nil, k.ReceiptStore())
70-
}
67+
watermarks := NewWatermarkManager(tmClient, ctxProvider, nil, k.ReceiptStore())
7168
txAPI := NewTransactionAPI(tmClient, k, ctxProvider, txConfigProvider, homeDir, ConnectionTypeHTTP, watermarks)
7269
debugAPI := NewDebugAPI(tmClient, k, ctxProvider, txConfigProvider, simulateConfig, app, antehandler, ConnectionTypeHTTP, config)
7370
if isPanicOrSyntheticTxFunc == nil {
@@ -231,10 +228,7 @@ func NewEVMWebSocketServer(
231228
EVMTimeout: config.SimulationEVMTimeout,
232229
MaxConcurrentSimulationCalls: config.MaxConcurrentSimulationCalls,
233230
}
234-
var watermarks *WatermarkManager
235-
if !config.DisableWatermark {
236-
watermarks = NewWatermarkManager(tmClient, ctxProvider, nil, k.ReceiptStore())
237-
}
231+
watermarks := NewWatermarkManager(tmClient, ctxProvider, nil, k.ReceiptStore())
238232
dbReadSemaphore := make(chan struct{}, MaxDBReadConcurrency)
239233
globalBlockCache := NewBlockCache(3000)
240234
globalLogSlicePool := NewLogSlicePool()

evmrpc/setup_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ func init() {
573573
goodConfig.HTTPPort = TestPort
574574
goodConfig.WSPort = TestWSPort
575575
goodConfig.FilterTimeout = 500 * time.Millisecond
576-
goodConfig.DisableWatermark = true
577576
goodConfig.MaxLogNoBlock = 10
578577
infoLog, err := log.NewDefaultLogger("text", "info")
579578
if err != nil {
@@ -591,7 +590,6 @@ func init() {
591590
// Start bad http server
592591
badConfig := evmrpc.DefaultConfig
593592
badConfig.HTTPPort = TestBadPort
594-
badConfig.DisableWatermark = true
595593
badConfig.FilterTimeout = 500 * time.Millisecond
596594
badHTTPServer, err := evmrpc.NewEVMHTTPServer(infoLog, badConfig, &MockBadClient{}, EVMKeeper, testApp.BaseApp, testApp.TracerAnteHandler, ctxProvider, txConfigProvider, "", nil)
597595
if err != nil {
@@ -1074,6 +1072,13 @@ func setupLogs() {
10741072
EVMKeeper.SetBlockBloom(Ctx, []ethtypes.Bloom{bloomSynth, bloom4, bloomTx1})
10751073
EVMKeeper.SetEvmOnlyBlockBloom(Ctx, []ethtypes.Bloom{bloom4, bloomTx1})
10761074

1075+
if store := EVMKeeper.ReceiptStore(); store != nil {
1076+
if err := store.SetLatestVersion(MockHeight103); err != nil {
1077+
panic(err)
1078+
}
1079+
_ = store.SetEarliestVersion(1, true)
1080+
}
1081+
10771082
}
10781083

10791084
//nolint:deadcode

evmrpc/tests/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func setupTestServer(
137137
cfg := evmrpc.DefaultConfig
138138
cfg.HTTPEnabled = true
139139
cfg.HTTPPort = port
140-
cfg.DisableWatermark = true
141140
s, err := evmrpc.NewEVMHTTPServer(
142141
log.NewNopLogger(),
143142
cfg,

evmrpc/tx_test.go

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

8282
func TestGetTransactionError(t *testing.T) {
8383
h := common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
84-
EVMKeeper.MockReceipt(Ctx, h, &types.Receipt{VmError: "test error"})
84+
EVMKeeper.MockReceipt(Ctx, h, &types.Receipt{VmError: "test error", BlockNumber: 1})
8585
resObj := sendRequestGood(t, "getTransactionErrorByHash", "0x1111111111111111111111111111111111111111111111111111111111111111")
8686
require.Equal(t, "test error", resObj["result"])
8787

evmrpc/watermark_manager.go

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77

8-
storetypes "github.com/cosmos/cosmos-sdk/store/types"
98
sdk "github.com/cosmos/cosmos-sdk/types"
109
"github.com/ethereum/go-ethereum/rpc"
1110
sstypes "github.com/sei-protocol/sei-db/ss/types"
@@ -48,44 +47,64 @@ func (m *WatermarkManager) Watermarks(ctx context.Context) (int64, int64, error)
4847
}
4948

5049
var (
51-
earliestCandidates []int64
52-
latestCandidates []int64
50+
latest int64
51+
latestSet bool
52+
earliest int64
53+
earliestSet bool
5354
)
5455

56+
setLatest := func(candidate int64) {
57+
if candidate < 0 {
58+
return
59+
}
60+
if !latestSet || candidate < latest {
61+
latest = candidate
62+
latestSet = true
63+
}
64+
}
65+
66+
setEarliest := func(candidate int64) {
67+
if candidate < 0 {
68+
return
69+
}
70+
if !earliestSet || candidate > earliest {
71+
earliest = candidate
72+
earliestSet = true
73+
}
74+
}
75+
5576
// Tendermint heights
56-
if latest, earliest, err := m.fetchTendermintWatermarks(ctx); err == nil {
57-
latestCandidates = append(latestCandidates, latest)
58-
earliestCandidates = append(earliestCandidates, earliest)
77+
if tmLatest, tmEarliest, err := m.fetchTendermintWatermarks(ctx); err == nil {
78+
setLatest(tmLatest)
79+
setEarliest(tmEarliest)
5980
} else if !errors.Is(err, errNoHeightSource) {
6081
return 0, 0, err
6182
}
6283

84+
if m.ctxProvider != nil {
85+
if ctxHeight := m.ctxProvider(LatestCtxHeight).BlockHeight(); ctxHeight > 0 {
86+
setLatest(ctxHeight)
87+
}
88+
}
89+
6390
// State store heights (historical state DB)
6491
if ssLatest, ssEarliest, ok := m.fetchStateStoreWatermarks(); ok {
65-
latestCandidates = append(latestCandidates, ssLatest)
66-
earliestCandidates = append(earliestCandidates, ssEarliest)
67-
} else if msLatest, msEarliest, ok := m.fetchMultiStoreWatermarks(); ok {
68-
// Fall back to application multistore heights if state store unavailable
69-
latestCandidates = append(latestCandidates, msLatest)
70-
earliestCandidates = append(earliestCandidates, msEarliest)
92+
setLatest(ssLatest)
93+
setEarliest(ssEarliest)
7194
}
7295

7396
// Receipt store height participates only in the latest watermark, since
7497
// pruning guarantees the earliest watermark is bounded by TM+SS.
7598
if m.receiptStore != nil {
76-
if latest := m.receiptStore.GetLatestVersion(); latest > 0 {
77-
latestCandidates = append(latestCandidates, latest)
78-
}
99+
setLatest(m.receiptStore.GetLatestVersion())
79100
}
80101

81-
if len(latestCandidates) == 0 {
102+
if !latestSet {
82103
return 0, 0, errNoHeightSource
83104
}
84105

85-
latest := minInt64(latestCandidates)
86-
earliest := int64(0)
87-
if len(earliestCandidates) > 0 {
88-
earliest = maxInt64(earliestCandidates)
106+
if !earliestSet {
107+
earliest = 0
89108
}
90109

91110
if latest < earliest {
@@ -224,50 +243,3 @@ func (m *WatermarkManager) fetchStateStoreWatermarks() (int64, int64, bool) {
224243
}
225244
return m.stateStore.GetLatestVersion(), m.stateStore.GetEarliestVersion(), true
226245
}
227-
228-
func (m *WatermarkManager) fetchMultiStoreWatermarks() (latest int64, earliest int64, ok bool) {
229-
if m.ctxProvider == nil {
230-
return 0, 0, false
231-
}
232-
ctx := m.ctxProvider(LatestCtxHeight)
233-
ms := ctx.MultiStore()
234-
if ms == nil {
235-
return 0, 0, false
236-
}
237-
defer func() {
238-
if r := recover(); r != nil {
239-
latest, earliest, ok = 0, 0, false
240-
}
241-
}()
242-
earliest = ms.GetEarliestVersion()
243-
if earliest == 0 {
244-
earliest = ctx.BlockHeight()
245-
}
246-
if commitStore, implements := ms.(interface{ LastCommitID() storetypes.CommitID }); implements {
247-
latest = commitStore.LastCommitID().Version
248-
}
249-
if latest == 0 {
250-
latest = ctx.BlockHeight()
251-
}
252-
return latest, earliest, true
253-
}
254-
255-
func minInt64(values []int64) int64 {
256-
min := values[0]
257-
for _, v := range values[1:] {
258-
if v < min {
259-
min = v
260-
}
261-
}
262-
return min
263-
}
264-
265-
func maxInt64(values []int64) int64 {
266-
max := values[0]
267-
for _, v := range values[1:] {
268-
if v > max {
269-
max = v
270-
}
271-
}
272-
return max
273-
}

evmrpc/watermark_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestWatermarksAggregatesSources(t *testing.T) {
3737
require.Equal(t, int64(8), latest)
3838
}
3939

40-
func TestWatermarksFallbackToMultiStore(t *testing.T) {
40+
func TestWatermarksIncludesCtxProviderHeight(t *testing.T) {
4141
multi := &fakeMultiStore{earliest: 0, latest: 0}
4242
ctx := sdk.Context{}.
4343
WithBlockHeight(12).
@@ -49,7 +49,7 @@ func TestWatermarksFallbackToMultiStore(t *testing.T) {
4949

5050
earliest, latest, err := wm.Watermarks(context.Background())
5151
require.NoError(t, err)
52-
require.Equal(t, int64(12), earliest)
52+
require.Equal(t, int64(5), earliest)
5353
require.Equal(t, int64(12), latest)
5454
}
5555

0 commit comments

Comments
 (0)