Skip to content

Commit 7db59c2

Browse files
author
Kang
committed
cfx sync
1 parent bc9350b commit 7db59c2

File tree

10 files changed

+948
-89
lines changed

10 files changed

+948
-89
lines changed

cmd/startCfxSync.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"sync"
6+
7+
"github.com/Conflux-Chain/data-house/common"
8+
"github.com/Conflux-Chain/data-house/model"
9+
"github.com/Conflux-Chain/data-house/sync/cfx"
10+
"github.com/Conflux-Chain/go-conflux-util/cmd"
11+
"github.com/Conflux-Chain/go-conflux-util/store"
12+
"github.com/Conflux-Chain/go-conflux-util/viper"
13+
"github.com/sirupsen/logrus"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
// startCfxSyncCmd represents the startCfxSync command
18+
var startCfxSyncCmd = &cobra.Command{
19+
Use: "startCfxSync",
20+
Short: "start Cfx sync",
21+
Run: startCfx,
22+
}
23+
24+
func init() {
25+
rootCmd.AddCommand(startCfxSyncCmd)
26+
}
27+
28+
func startCfx(*cobra.Command, []string) {
29+
ctx, cancel := context.WithCancel(context.Background())
30+
var wg sync.WaitGroup
31+
32+
storeConfig := store.MustNewConfigFromViper()
33+
db := storeConfig.MustOpenOrCreate(model.CfxTables...)
34+
35+
var config common.CfxConfig
36+
viper.MustUnmarshalKey("cfx", &config)
37+
38+
logrus.WithField("batch", config.Batch).Info("Starting cfx sync ...")
39+
if config.Batch {
40+
if err := cfx.StartSyncBatch(ctx, &wg, &config, db); err != nil {
41+
logrus.Fatal(err)
42+
}
43+
} else {
44+
if err := cfx.StartSync(ctx, &wg, &config, db); err != nil {
45+
logrus.WithError(err).Fatal("failed to start cfx sync")
46+
}
47+
}
48+
logrus.Info("cfx sync started")
49+
50+
cmd.GracefulShutdown(&wg, cancel)
51+
}

cmd/startEvmSync.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ func init() {
2626
}
2727

2828
func start(*cobra.Command, []string) {
29-
logrus.Info("Starting evm sync ...")
30-
3129
ctx, cancel := context.WithCancel(context.Background())
3230
var wg sync.WaitGroup
3331

@@ -37,8 +35,15 @@ func start(*cobra.Command, []string) {
3735
var config common.EvmConfig
3836
viper.MustUnmarshalKey("evm", &config)
3937

40-
if err := evm.StartSync(ctx, &wg, &config, db); err != nil {
41-
logrus.WithError(err).Fatal("failed to start evm sync")
38+
logrus.WithField("batch", config.Batch).Info("Starting evm sync ...")
39+
if config.Batch {
40+
if err := evm.StartSyncBatch(ctx, &wg, &config, db); err != nil {
41+
logrus.Fatal(err)
42+
}
43+
} else {
44+
if err := evm.StartSync(ctx, &wg, &config, db); err != nil {
45+
logrus.WithError(err).Fatal("failed to start evm sync")
46+
}
4247
}
4348
logrus.Info("Evm sync started")
4449

common/config.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
package common
22

3-
import "github.com/Conflux-Chain/go-conflux-util/blockchain/sync/evm"
3+
import (
4+
"github.com/Conflux-Chain/go-conflux-util/blockchain/sync"
5+
"github.com/Conflux-Chain/go-conflux-util/blockchain/sync/core"
6+
"github.com/Conflux-Chain/go-conflux-util/blockchain/sync/evm"
7+
)
48

5-
type EvmConfig struct {
9+
//blockchain/sync/sync_db.go
10+
11+
type Config[T evm.BlockData | core.EpochData] struct {
12+
sync.CatchupParamsDB[T]
13+
sync.ParamsDB[T]
614
FirstBlock uint64
7-
evm.Config
15+
Batch bool
16+
}
17+
18+
type EvmConfig struct {
19+
Config[evm.BlockData]
20+
evm.AdapterConfig
21+
}
22+
23+
type CfxConfig struct {
24+
Config[core.EpochData]
25+
core.AdapterConfig
826
}

common/recover.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package common
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Recover() {
10+
if r := recover(); r != nil {
11+
logrus.Error("recover panic, ", r)
12+
logrus.Info(string(debug.Stack()))
13+
os.Exit(-1)
14+
}
15+
}

common/stack.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package common
2+
3+
type Stack[T any] struct {
4+
items []T
5+
}
6+
7+
func NewStack[T any]() *Stack[T] {
8+
return &Stack[T]{items: make([]T, 0)}
9+
}
10+
11+
func (s *Stack[T]) Push(item T) {
12+
s.items = append(s.items, item)
13+
}
14+
15+
func (s *Stack[T]) Pop() (T, bool) {
16+
if len(s.items) == 0 {
17+
var zero T
18+
return zero, false
19+
}
20+
item := s.items[len(s.items)-1]
21+
s.items = s.items[:len(s.items)-1]
22+
return item, true
23+
}
24+
25+
func (s *Stack[T]) Peek() (T, bool) {
26+
if len(s.items) == 0 {
27+
var zero T
28+
return zero, false
29+
}
30+
return s.items[len(s.items)-1], true
31+
}
32+
33+
func (s *Stack[T]) IsEmpty() bool {
34+
return len(s.items) == 0
35+
}
36+
37+
func (s *Stack[T]) Size() int {
38+
return len(s.items)
39+
}

go.mod

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module github.com/Conflux-Chain/data-house
33
go 1.23.10
44

55
require (
6-
github.com/Conflux-Chain/go-conflux-util v0.6.9-0.20251226095241-429733610f4c
6+
github.com/Conflux-Chain/go-conflux-sdk v1.5.11
7+
github.com/Conflux-Chain/go-conflux-util v0.6.9-0.20260108031542-576cfb1db8a1
78
github.com/openweb3/web3go v0.3.0
9+
github.com/pkg/errors v0.9.1
810
github.com/shopspring/decimal v1.3.1
911
github.com/sirupsen/logrus v1.9.3
1012
github.com/spf13/cobra v1.10.2
@@ -19,33 +21,33 @@ require (
1921
github.com/StackExchange/wmi v1.2.1 // indirect
2022
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
2123
github.com/andybalholm/brotli v1.0.5 // indirect
22-
github.com/bits-and-blooms/bitset v1.20.0 // indirect
24+
github.com/bits-and-blooms/bitset v1.22.0 // indirect
2325
github.com/btcsuite/btcd v0.24.0 // indirect
2426
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
2527
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
2628
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
2729
github.com/cespare/xxhash/v2 v2.3.0 // indirect
28-
github.com/consensys/bavard v0.1.27 // indirect
29-
github.com/consensys/gnark-crypto v0.16.0 // indirect
30+
github.com/consensys/gnark-crypto v0.18.0 // indirect
3031
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
3132
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
3233
github.com/davecgh/go-spew v1.1.1 // indirect
3334
github.com/deckarep/golang-set v1.8.0 // indirect
3435
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
35-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
36-
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
36+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
37+
github.com/ethereum/c-kzg-4844/v2 v2.1.1 // indirect
3738
github.com/ethereum/go-ethereum v1.15.11 // indirect
3839
github.com/ethereum/go-verkle v0.2.2 // indirect
3940
github.com/fatih/color v1.16.0 // indirect
4041
github.com/fsnotify/fsnotify v1.6.0 // indirect
4142
github.com/go-ole/go-ole v1.3.0 // indirect
4243
github.com/go-sql-driver/mysql v1.8.1 // indirect
4344
github.com/go-telegram/bot v1.2.2 // indirect
45+
github.com/goccy/go-json v0.10.5 // indirect
4446
github.com/gofrs/flock v0.8.1 // indirect
4547
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
46-
github.com/google/go-cmp v0.6.0 // indirect
48+
github.com/google/go-cmp v0.7.0 // indirect
4749
github.com/google/go-querystring v1.1.0 // indirect
48-
github.com/google/uuid v1.5.0 // indirect
50+
github.com/google/uuid v1.6.0 // indirect
4951
github.com/gorilla/websocket v1.5.0 // indirect
5052
github.com/hashicorp/hcl v1.0.0 // indirect
5153
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
@@ -56,6 +58,7 @@ require (
5658
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
5759
github.com/jinzhu/inflection v1.0.0 // indirect
5860
github.com/jinzhu/now v1.1.5 // indirect
61+
github.com/kilic/bls12-381 v0.1.0 // indirect
5962
github.com/klauspost/compress v1.16.0 // indirect
6063
github.com/magiconair/properties v1.8.5 // indirect
6164
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -64,7 +67,6 @@ require (
6467
github.com/mattn/go-sqlite3 v1.14.22 // indirect
6568
github.com/mcuadros/go-defaults v1.2.0 // indirect
6669
github.com/mitchellh/mapstructure v1.4.3 // indirect
67-
github.com/mmcloughlin/addchain v0.4.0 // indirect
6870
github.com/olekukonko/tablewriter v0.0.5 // indirect
6971
github.com/openweb3/go-ethereum-hdwallet v0.1.0 // indirect
7072
github.com/openweb3/go-rpc-provider v0.3.5 // indirect
@@ -75,7 +77,7 @@ require (
7577
github.com/pion/stun/v2 v2.0.0 // indirect
7678
github.com/pion/transport/v2 v2.2.1 // indirect
7779
github.com/pion/transport/v3 v3.0.1 // indirect
78-
github.com/pkg/errors v0.9.1 // indirect
80+
github.com/pmezard/go-difflib v1.0.0 // indirect
7981
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect
8082
github.com/rivo/uniseg v0.2.0 // indirect
8183
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
@@ -84,24 +86,25 @@ require (
8486
github.com/spf13/jwalterweatherman v1.1.0 // indirect
8587
github.com/spf13/pflag v1.0.9 // indirect
8688
github.com/spf13/viper v1.10.0 // indirect
89+
github.com/stretchr/testify v1.10.0 // indirect
8790
github.com/subosito/gotenv v1.2.0 // indirect
88-
github.com/supranational/blst v0.3.14 // indirect
91+
github.com/supranational/blst v0.3.15 // indirect
8992
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
9093
github.com/tklauser/go-sysconf v0.3.12 // indirect
9194
github.com/tklauser/numcpus v0.6.1 // indirect
9295
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
9396
github.com/valyala/bytebufferpool v1.0.0 // indirect
9497
github.com/valyala/fasthttp v1.40.0 // indirect
95-
golang.org/x/crypto v0.35.0 // indirect
96-
golang.org/x/sync v0.11.0 // indirect
97-
golang.org/x/sys v0.30.0 // indirect
98-
golang.org/x/text v0.22.0 // indirect
98+
golang.org/x/crypto v0.39.0 // indirect
99+
golang.org/x/sync v0.15.0 // indirect
100+
golang.org/x/sys v0.33.0 // indirect
101+
golang.org/x/text v0.26.0 // indirect
99102
gopkg.in/ini.v1 v1.67.0 // indirect
100103
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
101104
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
102105
gopkg.in/yaml.v2 v2.4.0 // indirect
106+
gopkg.in/yaml.v3 v3.0.1 // indirect
103107
gorm.io/driver/mysql v1.6.0 // indirect
104108
gorm.io/driver/sqlite v1.6.0 // indirect
105109
gotest.tools v2.2.0+incompatible // indirect
106-
rsc.io/tmplfunc v0.0.3 // indirect
107110
)

0 commit comments

Comments
 (0)