Skip to content

Commit 17ecc6d

Browse files
committed
fixing tests
1 parent a21350c commit 17ecc6d

14 files changed

Lines changed: 107 additions & 26 deletions

File tree

abci/example/kvstore/kvstore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type Application struct {
5454

5555
// Generate blobs
5656
generateBlobs bool
57+
58+
Test bool // is test application
5759
}
5860

5961
// NewApplication creates an instance of the kvstore from the provided database

internal/db/db_utils.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package db
22

33
import (
4+
"bytes"
45
"encoding/binary"
56
"encoding/hex"
67
"fmt"
78
"log"
9+
"strconv"
810
"time"
911
"unicode/utf8"
1012

@@ -236,3 +238,58 @@ func isASCIIPrintable(b []byte) bool {
236238
}
237239
return true
238240
}
241+
242+
// findSmallestValueWithBrokenKeys attempts to find the smallest numeric value
243+
// among keys that share a given prefix.
244+
//
245+
// Note: The key format is **not properly designed** — numeric values are
246+
// concatenated as plain strings (e.g. "SC:2", "SC:10"), which causes lexicographic
247+
// rather than numeric ordering. This function compensates for that by iterating
248+
// through possible first digits and parsing keys manually.
249+
//
250+
// Example key set: "SC:2", "SC:10", "SC:3" → returns 2
251+
//
252+
// This is a workaround and should be replaced when the key schema is improved.
253+
func FindSmallestValueWithBrokenKeys(db dbm.DB, prefix []byte) (int, error) {
254+
var smallest *int
255+
256+
// We assume numeric suffixes can start with digits 0–9
257+
for d := byte('0'); d <= byte('9'); d++ {
258+
start := append(append([]byte{}, prefix...), d)
259+
it, err := db.Iterator(start, nil)
260+
if err != nil {
261+
return 0, fmt.Errorf("failed to iterate prefix %q: %w", start, err)
262+
}
263+
264+
for ; it.Valid(); it.Next() {
265+
key := it.Key()
266+
if !bytes.HasPrefix(key, prefix) {
267+
break // passed beyond the prefix
268+
}
269+
270+
// Extract numeric part after prefix
271+
suffix := bytes.TrimPrefix(key, prefix)
272+
if len(suffix) == 0 {
273+
continue
274+
}
275+
276+
n, err := strconv.Atoi(string(suffix))
277+
if err != nil {
278+
// Skip non-numeric keys gracefully
279+
continue
280+
}
281+
282+
if smallest == nil || n < *smallest {
283+
smallest = &n
284+
}
285+
}
286+
287+
it.Close()
288+
}
289+
290+
if smallest == nil {
291+
return 0, fmt.Errorf("no valid numeric keys found for prefix %q", prefix)
292+
}
293+
294+
return *smallest, nil
295+
}

light/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func ExampleClient_VerifyLightBlockAtHeight() {
151151
func TestMain(m *testing.M) {
152152
// start a CometBFT node (and kvstore) in the background to test against
153153
app := kvstore.NewInMemoryApplication()
154-
node := rpctest.StartTendermint(app, rpctest.SuppressStdout)
154+
node := rpctest.StartTendermint(app, true, rpctest.SuppressStdout)
155155

156156
code := m.Run()
157157

light/provider/http/http_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func TestNewProvider(t *testing.T) {
3636
func TestProvider(t *testing.T) {
3737
app := kvstore.NewInMemoryApplication()
3838
app.RetainBlocks = 10
39-
node := rpctest.StartTendermint(app)
39+
app.Test = true
40+
node := rpctest.StartTendermint(app, true)
4041

4142
cfg := rpctest.GetConfig()
4243
defer os.RemoveAll(cfg.RootDir)

node/node.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,12 @@ func NewNode(config *cfg.Config,
276276
dbProvider cfg.DBProvider,
277277
metricsProvider MetricsProvider,
278278
logger log.Logger,
279+
isTest bool,
279280
options ...Option,
280281
) (*Node, error) {
281282
return NewNodeWithContext(context.TODO(), config, privValidator,
282283
nodeKey, clientCreator, genesisDocProvider, dbProvider,
283-
metricsProvider, logger, options...)
284+
metricsProvider, logger, isTest, options...)
284285
}
285286

286287
// NewNodeWithContext is cancellable version of NewNode.
@@ -293,6 +294,7 @@ func NewNodeWithContext(ctx context.Context,
293294
dbProvider cfg.DBProvider,
294295
metricsProvider MetricsProvider,
295296
logger log.Logger,
297+
isTest bool,
296298
options ...Option,
297299
) (*Node, error) {
298300
blockStore, stateDB, err := initDBs(config, dbProvider)
@@ -393,6 +395,7 @@ func NewNodeWithContext(ctx context.Context,
393395
blockStore,
394396
smMetrics,
395397
logger.With("module", "state"),
398+
isTest,
396399
)
397400
if err != nil {
398401
return nil, fmt.Errorf("failed to create pruner: %w", err)
@@ -1006,6 +1009,7 @@ func createPruner(
10061009
blockStore *store.BlockStore,
10071010
metrics *sm.Metrics,
10081011
logger log.Logger,
1012+
isTest bool,
10091013
) (*sm.Pruner, error) {
10101014
if err := initApplicationRetainHeight(stateStore); err != nil {
10111015
return nil, err
@@ -1037,6 +1041,10 @@ func createPruner(
10371041
err = initIndexerRetentionHeights(pruner)
10381042
}
10391043

1044+
if isTest {
1045+
pruner.DisableWaitAtStart()
1046+
}
1047+
10401048
return pruner, err
10411049
}
10421050

node/setup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func DefaultNewNode(config *cfg.Config, logger log.Logger) (*Node, error) {
7575
cfg.DefaultDBProvider,
7676
DefaultMetricsProvider(config.Instrumentation),
7777
logger,
78+
false,
7879
)
7980
}
8081

rpc/client/examples_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func ExampleHTTP_simple() {
1818
// Start a CometBFT node (and kvstore) in the background to test against
1919
app := kvstore.NewInMemoryApplication()
20-
node := rpctest.StartTendermint(app, rpctest.SuppressStdout, rpctest.RecreateConfig)
20+
node := rpctest.StartTendermint(app, true, rpctest.SuppressStdout, rpctest.RecreateConfig)
2121
defer rpctest.StopTendermint(node)
2222

2323
// Create our RPC client
@@ -70,7 +70,7 @@ func ExampleHTTP_simple() {
7070
func ExampleHTTP_batching() {
7171
// Start a CometBFT node (and kvstore) in the background to test against
7272
app := kvstore.NewInMemoryApplication()
73-
node := rpctest.StartTendermint(app, rpctest.SuppressStdout, rpctest.RecreateConfig)
73+
node := rpctest.StartTendermint(app, true, rpctest.SuppressStdout, rpctest.RecreateConfig)
7474

7575
// Create our RPC client
7676
rpcAddr := rpctest.GetConfig().RPC.ListenAddress
@@ -142,7 +142,7 @@ func ExampleHTTP_batching() {
142142
func ExampleHTTP_maxBatchSize() {
143143
// Start a CometBFT node (and kvstore) in the background to test against
144144
app := kvstore.NewInMemoryApplication()
145-
node := rpctest.StartTendermint(app, rpctest.RecreateConfig, rpctest.SuppressStdout, rpctest.MaxReqBatchSize)
145+
node := rpctest.StartTendermint(app, true, rpctest.RecreateConfig, rpctest.SuppressStdout, rpctest.MaxReqBatchSize)
146146

147147
// Change the max_request_batch_size
148148
node.Config().RPC.MaxRequestBatchSize = 2

rpc/client/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestMain(m *testing.M) {
2121
app := kvstore.NewPersistentApplication(dir)
2222
// If testing block event generation
2323
// app.SetGenBlockEvents() // needs to be called here (see TestBlockSearch in rpc_test.go)
24-
node = rpctest.StartTendermint(app)
24+
node = rpctest.StartTendermint(app, true)
2525

2626
code := m.Run()
2727

rpc/grpc/grpc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestMain(m *testing.M) {
1616
// start a CometBFT node in the background to test against
1717
app := kvstore.NewInMemoryApplication()
18-
node := rpctest.StartTendermint(app)
18+
node := rpctest.StartTendermint(app, true)
1919

2020
code := m.Run()
2121

rpc/test/helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ func GetGRPCClient() core_grpc.BroadcastAPIClient {
125125
}
126126

127127
// StartTendermint starts a test CometBFT server in a go routine and returns when it is initialized
128-
func StartTendermint(app abci.Application, opts ...func(*Options)) *nm.Node {
128+
func StartTendermint(app abci.Application, isTest bool, opts ...func(*Options)) *nm.Node {
129129
nodeOpts := defaultOptions
130130
for _, opt := range opts {
131131
opt(&nodeOpts)
132132
}
133-
node := NewTendermint(app, &nodeOpts)
133+
node := NewTendermint(app, isTest, &nodeOpts)
134134
err := node.Start()
135135
if err != nil {
136136
panic(err)
@@ -158,7 +158,7 @@ func StopTendermint(node *nm.Node) {
158158
}
159159

160160
// NewTendermint creates a new CometBFT server and sleeps forever
161-
func NewTendermint(app abci.Application, opts *Options) *nm.Node {
161+
func NewTendermint(app abci.Application, isTest bool, opts *Options) *nm.Node {
162162
// Create & start node
163163
config := GetConfig(opts.recreateConfig)
164164
var logger log.Logger
@@ -183,7 +183,7 @@ func NewTendermint(app abci.Application, opts *Options) *nm.Node {
183183
nm.DefaultGenesisDocProviderFunc(config),
184184
cfg.DefaultDBProvider,
185185
nm.DefaultMetricsProvider(config.Instrumentation),
186-
logger)
186+
logger, isTest)
187187
if err != nil {
188188
panic(err)
189189
}

0 commit comments

Comments
 (0)