Skip to content

Commit e096dc1

Browse files
committed
refactor: block export
1 parent 70148ed commit e096dc1

File tree

4 files changed

+67
-43
lines changed

4 files changed

+67
-43
lines changed

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ tasks:
107107
START_BLOCK: '{{.START_BLOCK}}'
108108
END_BLOCK: '{{.END_BLOCK}}'
109109
cmds:
110-
- cmd: go test -timeout=0 -run=TestExportBlockRange github.com/ava-labs/avalanchego/tests/reexecute/c --block-dir-src={{.BLOCK_DIR_SRC}} --block-dir-dst={{.BLOCK_DIR_DST}} --start-block={{.START_BLOCK}} --end-block={{.END_BLOCK}}
110+
- cmd: go run github.com/ava-labs/avalanchego/tests/reexecute/blockexport --block-dir-src={{.BLOCK_DIR_SRC}} --block-dir-dst={{.BLOCK_DIR_DST}} --start-block={{.START_BLOCK}} --end-block={{.END_BLOCK}}
111111

112112
export-dir-to-s3:
113113
desc: Copies a directory to s3
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
9+
"github.com/prometheus/client_golang/prometheus"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/ava-labs/avalanchego/database/leveldb"
13+
"github.com/ava-labs/avalanchego/tests"
14+
"github.com/ava-labs/avalanchego/tests/reexecute"
15+
"github.com/ava-labs/avalanchego/utils/logging"
16+
"github.com/ava-labs/avalanchego/utils/units"
17+
)
18+
19+
var (
20+
blockDirSrcArg string
21+
blockDirDstArg string
22+
startBlockArg uint64
23+
endBlockArg uint64
24+
chanSizeArg int
25+
)
26+
27+
func init() {
28+
flag.StringVar(&blockDirSrcArg, "block-dir-src", blockDirSrcArg, "Source block directory to copy from when running TestExportBlockRange.")
29+
flag.StringVar(&blockDirDstArg, "block-dir-dst", blockDirDstArg, "Destination block directory to write blocks into when executing TestExportBlockRange.")
30+
flag.Uint64Var(&startBlockArg, "start-block", 101, "Start block to begin execution (exclusive).")
31+
flag.Uint64Var(&endBlockArg, "end-block", 200, "End block to end execution (inclusive).")
32+
flag.IntVar(&chanSizeArg, "chan-size", 100, "Size of the channel to use for block processing.")
33+
34+
flag.Parse()
35+
}
36+
37+
func main() {
38+
tc := tests.NewTestContext(tests.NewDefaultLogger(""))
39+
defer tc.RecoverAndExit()
40+
41+
r := require.New(tc)
42+
blockChan, err := reexecute.CreateBlockChanFromLevelDB(blockDirSrcArg, startBlockArg, endBlockArg, chanSizeArg, tc.DeferCleanup)
43+
r.NoError(err)
44+
45+
db, err := leveldb.New(blockDirDstArg, nil, logging.NoLog{}, prometheus.NewRegistry())
46+
r.NoError(err)
47+
tc.DeferCleanup(func() {
48+
r.NoError(db.Close())
49+
})
50+
51+
batch := db.NewBatch()
52+
for blkResult := range blockChan {
53+
r.NoError(batch.Put(reexecute.BlockKey(blkResult.Height), blkResult.BlockBytes))
54+
55+
if batch.Size() > 10*units.MiB {
56+
r.NoError(batch.Write())
57+
batch = db.NewBatch()
58+
}
59+
}
60+
61+
r.NoError(batch.Write())
62+
}

tests/reexecute/c/vm_reexecute_test.go

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
"github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
4343
"github.com/ava-labs/avalanchego/utils/logging"
4444
"github.com/ava-labs/avalanchego/utils/timer"
45-
"github.com/ava-labs/avalanchego/utils/units"
4645
"github.com/ava-labs/avalanchego/vms/metervm"
4746
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
4847
)
@@ -55,8 +54,6 @@ var (
5554

5655
var (
5756
blockDirArg string
58-
blockDirSrcArg string
59-
blockDirDstArg string
6057
currentStateDirArg string
6158
startBlockArg uint64
6259
endBlockArg uint64
@@ -122,10 +119,6 @@ func TestMain(m *testing.M) {
122119
flag.StringVar(&configNameArg, configKey, defaultConfigKey, fmt.Sprintf("Specifies the predefined config to use for the VM. Options include %s.", predefinedConfigOptionsStr))
123120
flag.StringVar(&runnerNameArg, "runner", "dev", "Name of the runner executing this test. Added as a metric label and to the sub-benchmark's name to differentiate results on the runner key.")
124121

125-
// Flags specific to TestExportBlockRange.
126-
flag.StringVar(&blockDirSrcArg, "block-dir-src", blockDirSrcArg, "Source block directory to copy from when running TestExportBlockRange.")
127-
flag.StringVar(&blockDirDstArg, "block-dir-dst", blockDirDstArg, "Destination block directory to write blocks into when executing TestExportBlockRange.")
128-
129122
flag.Parse()
130123

131124
if metricsCollectorEnabledArg {
@@ -232,7 +225,7 @@ func benchmarkReexecuteRange(
232225
zap.Int("chan-size", chanSize),
233226
)
234227

235-
blockChan, err := reexecute.CreateBlockChanFromLevelDB(b, blockDir, startBlock, endBlock, chanSize)
228+
blockChan, err := reexecute.CreateBlockChanFromLevelDB(blockDir, startBlock, endBlock, chanSize, b.Cleanup)
236229
r.NoError(err)
237230

238231
dbLogger := tests.NewDefaultLogger("db")
@@ -477,34 +470,6 @@ func (e *vmExecutor) executeSequence(ctx context.Context, blkChan <-chan reexecu
477470
return nil
478471
}
479472

480-
func TestExportBlockRange(t *testing.T) {
481-
exportBlockRange(t, blockDirSrcArg, blockDirDstArg, startBlockArg, endBlockArg, chanSizeArg)
482-
}
483-
484-
func exportBlockRange(tb testing.TB, blockDirSrc string, blockDirDst string, startBlock, endBlock uint64, chanSize int) {
485-
r := require.New(tb)
486-
blockChan, err := reexecute.CreateBlockChanFromLevelDB(tb, blockDirSrc, startBlock, endBlock, chanSize)
487-
r.NoError(err)
488-
489-
db, err := leveldb.New(blockDirDst, nil, logging.NoLog{}, prometheus.NewRegistry())
490-
r.NoError(err)
491-
tb.Cleanup(func() {
492-
r.NoError(db.Close())
493-
})
494-
495-
batch := db.NewBatch()
496-
for blkResult := range blockChan {
497-
r.NoError(batch.Put(reexecute.BlockKey(blkResult.Height), blkResult.BlockBytes))
498-
499-
if batch.Size() > 10*units.MiB {
500-
r.NoError(batch.Write())
501-
batch = db.NewBatch()
502-
}
503-
}
504-
505-
r.NoError(batch.Write())
506-
}
507-
508473
type consensusMetrics struct {
509474
lastAcceptedHeight prometheus.Gauge
510475
}

tests/reexecute/db.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ package reexecute
66
import (
77
"encoding/binary"
88
"fmt"
9-
"testing"
109

1110
"github.com/prometheus/client_golang/prometheus"
12-
"github.com/stretchr/testify/require"
1311

1412
"github.com/ava-labs/avalanchego/database"
1513
"github.com/ava-labs/avalanchego/database/leveldb"
@@ -29,16 +27,15 @@ type BlockResult struct {
2927
// Blocks are read sequentially and sent to the returned channel as BlockResult values.
3028
//
3129
// Any validation errors or iteration errors are sent as BlockResult with Err set, then the channel is closed.
32-
func CreateBlockChanFromLevelDB(tb testing.TB, sourceDir string, startBlock, endBlock uint64, chanSize int) (<-chan BlockResult, error) {
33-
r := require.New(tb)
30+
func CreateBlockChanFromLevelDB(sourceDir string, startBlock, endBlock uint64, chanSize int, cleanup func(func())) (<-chan BlockResult, error) {
3431
ch := make(chan BlockResult, chanSize)
3532

3633
db, err := leveldb.New(sourceDir, nil, logging.NoLog{}, prometheus.NewRegistry())
3734
if err != nil {
3835
return nil, fmt.Errorf("failed to create leveldb database from %q: %w", sourceDir, err)
3936
}
40-
tb.Cleanup(func() {
41-
r.NoError(db.Close())
37+
cleanup(func() {
38+
db.Close()
4239
})
4340

4441
go func() {

0 commit comments

Comments
 (0)