Skip to content

Commit

Permalink
chore: more refactorings (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler authored Dec 20, 2024
1 parent ea8e32e commit 9e9dde8
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 162 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ func (app *CosmosApp) LoadHomePath() error {
cmd.Env = append(os.Environ(), "COSMOVISOR_DISABLE_LOGS=true")
}

cmd.Args = append(cmd.Args, "start", "--help")

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to get output of binary: %w", err)
Expand Down
17 changes: 3 additions & 14 deletions engines/celestia-core-v34/celestiacore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
abciTypes "github.com/KYVENetwork/celestia-core/abci/types"
cfg "github.com/KYVENetwork/celestia-core/config"
cs "github.com/KYVENetwork/celestia-core/consensus"
"github.com/KYVENetwork/celestia-core/crypto"
"github.com/KYVENetwork/celestia-core/crypto/ed25519"
"github.com/KYVENetwork/celestia-core/evidence"
"github.com/KYVENetwork/celestia-core/libs/json"
Expand Down Expand Up @@ -44,9 +43,8 @@ type Engine struct {

evidenceDB db.DB

genDoc *GenesisDoc
privValidatorKey crypto.PubKey
nodeKey *tmP2P.NodeKey
genDoc *GenesisDoc
nodeKey *tmP2P.NodeKey

state tmState.State
proxyApp proxy.AppConns
Expand Down Expand Up @@ -94,15 +92,6 @@ func (engine *Engine) LoadConfig() error {

engine.genDoc = genDoc

privValidatorKey, err := privval.LoadFilePVEmptyState(
engine.config.PrivValidatorKeyFile(),
engine.config.PrivValidatorStateFile(),
).GetPubKey()
if err != nil {
return fmt.Errorf("failed to load validator key file: %w", err)
}
engine.privValidatorKey = privValidatorKey

nodeKey, err := tmP2P.LoadNodeKey(engine.config.NodeKeyFile())
if err != nil {
return fmt.Errorf("loading node key file failed: %w", err)
Expand Down Expand Up @@ -490,7 +479,7 @@ func (engine *Engine) StartRPCServer(port int64) {
ConsensusState: nil,
P2PPeers: nil,
P2PTransport: &Transport{nodeInfo: nodeInfo},
PubKey: engine.privValidatorKey,
PubKey: ed25519.GenPrivKey().PubKey(),
GenDoc: nil,
TxIndexer: nil,
BlockIndexer: nil,
Expand Down
17 changes: 3 additions & 14 deletions engines/cometbft-v37/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
abciTypes "github.com/KYVENetwork/cometbft/v37/abci/types"
cfg "github.com/KYVENetwork/cometbft/v37/config"
cs "github.com/KYVENetwork/cometbft/v37/consensus"
"github.com/KYVENetwork/cometbft/v37/crypto"
"github.com/KYVENetwork/cometbft/v37/crypto/ed25519"
"github.com/KYVENetwork/cometbft/v37/evidence"
"github.com/KYVENetwork/cometbft/v37/libs/json"
Expand Down Expand Up @@ -44,9 +43,8 @@ type Engine struct {

evidenceDB db.DB

genDoc *GenesisDoc
privValidatorKey crypto.PubKey
nodeKey *cometP2P.NodeKey
genDoc *GenesisDoc
nodeKey *cometP2P.NodeKey

state tmState.State
proxyApp proxy.AppConns
Expand Down Expand Up @@ -94,15 +92,6 @@ func (engine *Engine) LoadConfig() error {

engine.genDoc = genDoc

privValidatorKey, err := privval.LoadFilePVEmptyState(
engine.config.PrivValidatorKeyFile(),
engine.config.PrivValidatorStateFile(),
).GetPubKey()
if err != nil {
return fmt.Errorf("failed to load validator key file: %w", err)
}
engine.privValidatorKey = privValidatorKey

nodeKey, err := cometP2P.LoadNodeKey(engine.config.NodeKeyFile())
if err != nil {
return fmt.Errorf("loading node key file failed: %w", err)
Expand Down Expand Up @@ -486,7 +475,7 @@ func (engine *Engine) StartRPCServer(port int64) {
ConsensusState: nil,
P2PPeers: nil,
P2PTransport: &Transport{nodeInfo: nodeInfo},
PubKey: engine.privValidatorKey,
PubKey: ed25519.GenPrivKey().PubKey(),
GenDoc: nil,
TxIndexer: nil,
BlockIndexer: nil,
Expand Down
17 changes: 3 additions & 14 deletions engines/cometbft-v38/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
abciTypes "github.com/KYVENetwork/cometbft/v38/abci/types"
cfg "github.com/KYVENetwork/cometbft/v38/config"
cs "github.com/KYVENetwork/cometbft/v38/consensus"
"github.com/KYVENetwork/cometbft/v38/crypto"
"github.com/KYVENetwork/cometbft/v38/crypto/ed25519"
"github.com/KYVENetwork/cometbft/v38/evidence"
"github.com/KYVENetwork/cometbft/v38/libs/json"
Expand Down Expand Up @@ -45,9 +44,8 @@ type Engine struct {

evidenceDB db.DB

genDoc *GenesisDoc
privValidatorKey crypto.PubKey
nodeKey *cometP2P.NodeKey
genDoc *GenesisDoc
nodeKey *cometP2P.NodeKey

state tmState.State
proxyApp proxy.AppConns
Expand Down Expand Up @@ -95,15 +93,6 @@ func (engine *Engine) LoadConfig() error {

engine.genDoc = genDoc

privValidatorKey, err := privval.LoadFilePVEmptyState(
engine.config.PrivValidatorKeyFile(),
engine.config.PrivValidatorStateFile(),
).GetPubKey()
if err != nil {
return fmt.Errorf("failed to load validator key file: %w", err)
}
engine.privValidatorKey = privValidatorKey

nodeKey, err := cometP2P.LoadNodeKey(engine.config.NodeKeyFile())
if err != nil {
return fmt.Errorf("loading node key file failed: %w", err)
Expand Down Expand Up @@ -498,7 +487,7 @@ func (engine *Engine) StartRPCServer(port int64) {
ConsensusState: nil,
P2PPeers: nil,
P2PTransport: &Transport{nodeInfo: nodeInfo},
PubKey: engine.privValidatorKey,
PubKey: ed25519.GenPrivKey().PubKey(),
GenDoc: nil,
TxIndexer: nil,
BlockIndexer: nil,
Expand Down
17 changes: 3 additions & 14 deletions engines/tendermint-v34/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
abciTypes "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
cs "github.com/tendermint/tendermint/consensus"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/evidence"
"github.com/tendermint/tendermint/libs/json"
Expand Down Expand Up @@ -44,9 +43,8 @@ type Engine struct {

evidenceDB db.DB

genDoc *GenesisDoc
privValidatorKey crypto.PubKey
nodeKey *tmP2P.NodeKey
genDoc *GenesisDoc
nodeKey *tmP2P.NodeKey

state tmState.State
proxyApp proxy.AppConns
Expand Down Expand Up @@ -94,15 +92,6 @@ func (engine *Engine) LoadConfig() error {

engine.genDoc = genDoc

privValidatorKey, err := privval.LoadFilePVEmptyState(
engine.config.PrivValidatorKeyFile(),
engine.config.PrivValidatorStateFile(),
).GetPubKey()
if err != nil {
return fmt.Errorf("failed to load validator key file: %w", err)
}
engine.privValidatorKey = privValidatorKey

nodeKey, err := tmP2P.LoadNodeKey(engine.config.NodeKeyFile())
if err != nil {
return fmt.Errorf("loading node key file failed: %w", err)
Expand Down Expand Up @@ -482,7 +471,7 @@ func (engine *Engine) StartRPCServer(port int64) {
ConsensusState: nil,
P2PPeers: nil,
P2PTransport: &Transport{nodeInfo: nodeInfo},
PubKey: engine.privValidatorKey,
PubKey: ed25519.GenPrivKey().PubKey(),
GenDoc: nil,
TxIndexer: nil,
BlockIndexer: nil,
Expand Down
100 changes: 50 additions & 50 deletions sync/blocksync/blocksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,56 @@ import (
"github.com/KYVENetwork/ksync/utils"
)

func Start() error {
logger.Logger.Info().Msg("starting block-sync")

app, err := app.NewCosmosApp()
if err != nil {
return fmt.Errorf("failed to init cosmos app: %w", err)
}

if flags.Reset {
if err := app.ConsensusEngine.ResetAll(true); err != nil {
return fmt.Errorf("failed to reset cosmos app: %w", err)
}
}

continuationHeight := app.GetContinuationHeight()
metrics.SetContinuationHeight(continuationHeight)

blockCollector, err := getBlockCollector(app)
if err != nil {
return err
}

if err := PerformBlockSyncValidationChecks(blockCollector, continuationHeight, flags.TargetHeight); err != nil {
return fmt.Errorf("block-sync validation checks failed: %w", err)
}

if confirmation, err := getUserConfirmation(flags.Y, continuationHeight, flags.TargetHeight); !confirmation {
return err
}

if err := app.AutoSelectBinaryVersion(continuationHeight); err != nil {
return fmt.Errorf("failed to auto select binary version: %w", err)
}

if err := app.StartAll(0); err != nil {
return fmt.Errorf("failed to start app: %w", err)
}

defer app.StopAll()

// we only pass the snapshot collector to the block executor if we are creating
// state-sync snapshots with serve-snapshots
if err := StartBlockSyncExecutor(app, blockCollector, nil); err != nil {
return fmt.Errorf("failed to start block-sync executor: %w", err)
}

logger.Logger.Info().Str("duration", metrics.GetSyncDuration().String()).Msgf("successfully finished block-sync by reaching target height %d", flags.TargetHeight)
return nil
}

// PerformBlockSyncValidationChecks makes boundary checks if app can be block-synced from the given
// continuation height to the given target height
func PerformBlockSyncValidationChecks(blockCollector types.BlockCollector, continuationHeight, targetHeight int64) error {
Expand Down Expand Up @@ -80,53 +130,3 @@ func getUserConfirmation(y bool, continuationHeight, targetHeight int64) (bool,

return utils.GetUserConfirmationInput()
}

func Start() error {
logger.Logger.Info().Msg("starting block-sync")

app, err := app.NewCosmosApp()
if err != nil {
return fmt.Errorf("failed to init cosmos app: %w", err)
}

if flags.Reset {
if err := app.ConsensusEngine.ResetAll(true); err != nil {
return fmt.Errorf("failed to reset cosmos app: %w", err)
}
}

continuationHeight := app.GetContinuationHeight()
metrics.SetContinuationHeight(continuationHeight)

blockCollector, err := getBlockCollector(app)
if err != nil {
return err
}

if err := PerformBlockSyncValidationChecks(blockCollector, continuationHeight, flags.TargetHeight); err != nil {
return fmt.Errorf("block-sync validation checks failed: %w", err)
}

if confirmation, err := getUserConfirmation(flags.Y, continuationHeight, flags.TargetHeight); !confirmation {
return err
}

if err := app.AutoSelectBinaryVersion(continuationHeight); err != nil {
return fmt.Errorf("failed to auto select binary version: %w", err)
}

if err := app.StartAll(0); err != nil {
return fmt.Errorf("failed to start app: %w", err)
}

defer app.StopAll()

// we only pass the snapshot collector to the block executor if we are creating
// state-sync snapshots with serve-snapshots
if err := StartBlockSyncExecutor(app, blockCollector, nil); err != nil {
return fmt.Errorf("failed to start block-sync executor: %w", err)
}

logger.Logger.Info().Str("duration", metrics.GetSyncDuration().String()).Msgf("successfully finished block-sync by reaching target height %d", flags.TargetHeight)
return nil
}
40 changes: 20 additions & 20 deletions sync/heightsync/heightsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,6 @@ import (
"github.com/KYVENetwork/ksync/utils"
)

func getUserConfirmation(y, canApplySnapshot bool, snapshotHeight, continuationHeight, targetHeight int64) (bool, error) {
if y {
return true, nil
}

if canApplySnapshot {
if targetHeight == 0 {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m no target height specified, state-sync to height %d and sync indefinitely from there [y/N]: ", snapshotHeight)
} else if snapshotHeight == targetHeight {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m should target height %d be reached by applying a snapshot at height %d [y/N]: ", targetHeight, snapshotHeight)
} else {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m should target height %d be reached by applying a snapshot at height %d and syncing the remaining %d blocks [y/N]: ", targetHeight, snapshotHeight, targetHeight-(continuationHeight-1))
}
} else {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m should target height %d be reached by syncing from height %d [y/N]: ", targetHeight, continuationHeight-1)
}

return utils.GetUserConfirmationInput()
}

func Start() error {
logger.Logger.Info().Msg("starting height-sync")

Expand Down Expand Up @@ -130,3 +110,23 @@ func Start() error {
logger.Logger.Info().Str("duration", metrics.GetSyncDuration().String()).Msgf("successfully finished height-sync by reaching target height %d", flags.TargetHeight)
return nil
}

func getUserConfirmation(y, canApplySnapshot bool, snapshotHeight, continuationHeight, targetHeight int64) (bool, error) {
if y {
return true, nil
}

if canApplySnapshot {
if targetHeight == 0 {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m no target height specified, state-sync to height %d and sync indefinitely from there [y/N]: ", snapshotHeight)
} else if snapshotHeight == targetHeight {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m should target height %d be reached by applying a snapshot at height %d [y/N]: ", targetHeight, snapshotHeight)
} else {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m should target height %d be reached by applying a snapshot at height %d and syncing the remaining %d blocks [y/N]: ", targetHeight, snapshotHeight, targetHeight-(continuationHeight-1))
}
} else {
fmt.Printf("\u001B[36m[KSYNC]\u001B[0m should target height %d be reached by syncing from height %d [y/N]: ", targetHeight, continuationHeight-1)
}

return utils.GetUserConfirmationInput()
}
Loading

0 comments on commit 9e9dde8

Please sign in to comment.