Skip to content

Commit

Permalink
Remove duplicate instantiation of network params and update docs for …
Browse files Browse the repository at this point in the history
…migrationstatus
  • Loading branch information
charithabandi committed Sep 30, 2024
1 parent 896b62a commit 9290c89
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 66 deletions.
2 changes: 1 addition & 1 deletion cmd/kwil-admin/cmds/migration/network_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func networkStatusCmd() *cobra.Command {
Status: status.Status,
StartHeight: status.StartHeight,
EndHeight: status.EndHeight,
CurrentHeight: status.CurrentBlock,
CurrentHeight: status.CurrentHeight,
})
},
}
Expand Down
5 changes: 0 additions & 5 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ type NetworkParameters struct {
DisabledGasCosts bool

// MigrationStatus determines the status of the migration.
// It can be one of the following:
// - NoActiveMigration: No active migration is in progress.
// - MigrationNotStarted: A migration has been approved but not yet activated.
// - MigrationInProgress: A migration is in progress.
// - MigrationCompleted: A migration has been completed.
MigrationStatus types.MigrationStatus

// MaxVotesPerTx is the maximum number of votes that can be included in a
Expand Down
21 changes: 11 additions & 10 deletions core/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Migration struct {
Timestamp string `json:"timestamp"` // Timestamp when the migration was proposed
}

// MigrationStatus represents the status of the nodes in the zero downtime migration process.
type MigrationStatus string

const (
Expand All @@ -101,24 +102,24 @@ const (
// but before the migration begins. During this phase, validators prepare their nodes for migration.
ActivationPeriod MigrationStatus = "ActivationPeriod"

// MigrationInProgress is the phase where the migration is actively occurring. The old and new networks
// run concurrently, with state changes from the old network being replicated to the new network.
// MigrationInProgress indicates that the nodes on the old network are in migration mode and
// records the state changes to be replicated on the new network.
MigrationInProgress MigrationStatus = "MigrationInProgress"

// MigrationCompleted indicates the migration process has successfully finished,
// and the old network is ready to be decommissioned.
// MigrationCompleted indicates that the migration process has successfully finished on the old network,
// and the old network is ready to be decommissioned once the new network has caught up.
MigrationCompleted MigrationStatus = "MigrationCompleted"

// GenesisMigration refers to the phase where the node initializes with the genesis state,
// tries to replicate the state changes from the old network.
// GenesisMigration refers to the phase where the nodes on the new network during migration bootstraps
// with the genesis state and replicates the state changes from the old network.
GenesisMigration MigrationStatus = "GenesisMigration"
)

type MigrationState struct {
Status MigrationStatus `json:"status"` // Status is the current status of the migration
StartHeight int64 `json:"start_height"` // StartHeight is the block height at which the migration started
EndHeight int64 `json:"end_height"` // EndHeight is the block height at which the migration ends
CurrentBlock int64 `json:"chain_height"` // CurrentBlock is the current block height of the node
Status MigrationStatus `json:"status"` // Status is the current status of the migration
StartHeight int64 `json:"start_height"` // StartHeight is the block height at which the migration started
EndHeight int64 `json:"end_height"` // EndHeight is the block height at which the migration ends
CurrentHeight int64 `json:"chain_height"` // CurrentHeight is the current block height of the node
}

// MigrationMetadata holds metadata about a migration, informing
Expand Down
27 changes: 9 additions & 18 deletions internal/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ func NewAbciApp(ctx context.Context, cfg *AbciConfig, snapshotter SnapshotModule
endHeight := app.consensusParams.Migration.EndHeight

if startHeight != 0 && endHeight != 0 {
// set this only if "migrate_from" is configured
migrationParams = &common.MigrationContext{
StartHeight: startHeight,
EndHeight: endHeight,
Expand All @@ -270,27 +269,21 @@ func NewAbciApp(ctx context.Context, cfg *AbciConfig, snapshotter SnapshotModule
status = types.GenesisMigration
}

// we need to store the genesis network params
err = meta.StoreParams(ctx, tx, &common.NetworkParameters{
networkParams = &common.NetworkParameters{
MaxBlockSize: app.consensusParams.Block.MaxBytes,
JoinExpiry: app.consensusParams.Validator.JoinExpiry,
VoteExpiry: app.consensusParams.Votes.VoteExpiry,
DisabledGasCosts: app.consensusParams.WithoutGasCosts,
MaxVotesPerTx: app.consensusParams.Votes.MaxVotesPerTx,
MigrationStatus: status,
})
}

// we need to store the genesis network params
err = meta.StoreParams(ctx, tx, networkParams)
if err != nil {
return nil, fmt.Errorf("failed to store network params: %w", err)
}

networkParams = &common.NetworkParameters{
MaxBlockSize: app.consensusParams.Block.MaxBytes,
JoinExpiry: app.consensusParams.Validator.JoinExpiry,
VoteExpiry: app.consensusParams.Votes.VoteExpiry,
DisabledGasCosts: app.consensusParams.WithoutGasCosts,
MaxVotesPerTx: app.consensusParams.Votes.MaxVotesPerTx,
MigrationStatus: status,
}
} else if err != nil {
return nil, fmt.Errorf("failed to load network params: %w", err)
} else {
Expand Down Expand Up @@ -770,7 +763,6 @@ func (a *AbciApp) FinalizeBlock(ctx context.Context, req *abciTypes.RequestFinal
// Create a new changeset processor
csp := newChangesetProcessor()
// "migrator" module subscribes to the changeset processor to store changesets during the migration
csDoneChan := make(chan bool, 1)
csErrChan := make(chan error, 1)
if inMigration && !haltNetwork {
csChanMigrator, err := csp.Subscribe(ctx, "migrator")
Expand All @@ -779,7 +771,7 @@ func (a *AbciApp) FinalizeBlock(ctx context.Context, req *abciTypes.RequestFinal
}
// migrator go routine will receive changesets from the changeset processor
// give the new channel to the migrator to store changesets
go a.migrator.StoreChangesets(req.Height, csChanMigrator, csDoneChan, csErrChan)
go a.migrator.StoreChangesets(req.Height, csChanMigrator, csErrChan)
}

// statistics module can subscribe to the changeset processor to listen for changesets for updating statistics
Expand Down Expand Up @@ -887,10 +879,9 @@ func (a *AbciApp) FinalizeBlock(ctx context.Context, req *abciTypes.RequestFinal
}

if inMigration && !haltNetwork {
select {
case <-csDoneChan:
// migrator has finished storing changesets
case err := <-csErrChan:
// wait for the migrator to finish storing changesets
err = <-csErrChan
if err != nil {
return nil, fmt.Errorf("failed to store changesets: %w", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/abci/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type WhitelistPeersModule interface {

type MigratorModule interface {
NotifyHeight(ctx context.Context, block *common.BlockContext, db migrations.Database) error
StoreChangesets(height int64, changes <-chan any, doneChan chan<- bool, errChan chan<- error)
StoreChangesets(height int64, changes <-chan any, errChan chan<- error)
PersistLastChangesetHeight(ctx context.Context, tx sql.Executor) error
SetMigrationStatus(status types.MigrationStatus)
}
11 changes: 6 additions & 5 deletions internal/migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const (
// at the appropriate height, persisting changesets for the migration for each
// block as it occurs, and making that data available via RPC for the new node.
// Similarly, if the local process is the new node, it is responsible for reading
// changesets from the external node and applying them to the local database.
// changesets from the external node and applying them to the local database.
// The changesets are stored from the start height of the migration to the end height (both inclusive).
type Migrator struct {
initialized bool // set to true after the migrator is initialized

Expand Down Expand Up @@ -612,9 +613,9 @@ func (cw *chunkWriter) SaveMetadata() error {
}

// storeChangeset persists a changeset to the migrations/changesets directory.
// doneChan is a channel that is closed when all the block changes have been written to disk.
// errChan is a channel that receives errors from the changeset storage routine.
func (m *Migrator) StoreChangesets(height int64, changes <-chan any, doneChan chan<- bool, errChan chan<- error) {
// errChan is a channel that receives errors from the changeset storage routine and signals
// abci that the changeset storage has completed or failed.
func (m *Migrator) StoreChangesets(height int64, changes <-chan any, errChan chan<- error) {
if changes == nil {
// no changesets to store, not in a migration
return
Expand Down Expand Up @@ -667,7 +668,7 @@ func (m *Migrator) StoreChangesets(height int64, changes <-chan any, doneChan ch
}

// signals NotifyHeight that all changesets have been written to disk
doneChan <- true
errChan <- nil
}

// LoadChangesets loads changesets at a given height from the migration directory.
Expand Down
22 changes: 0 additions & 22 deletions internal/migrations/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,25 +371,3 @@ func getChangeset(ctx context.Context, db sql.Executor, height int64, index int6

return changeset, nil
}

// InGenesisMigration checks if the node is going through a genesis migration.
// It returns true if the node is still syncing with the old network.
func InGenesisMigration(ctx context.Context, db sql.ReadTxMaker, endHeight int64) (bool, error) {
if endHeight == 0 {
return false, nil
}

tx, err := db.BeginReadTx(ctx)
if err != nil {
return false, err
}
defer tx.Rollback(ctx)

// Check if the migration table exists
height, err := getLastChangeset(ctx, tx)
if err != nil {
return false, err
}

return height < endHeight, nil
}
8 changes: 4 additions & 4 deletions internal/services/jsonrpc/usersvc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,10 @@ func (svc *Service) MigrationStatus(ctx context.Context, req *userjson.Migration

return &userjson.MigrationStatusResponse{
Status: &types.MigrationState{
Status: metadata.MigrationState.Status,
StartHeight: metadata.MigrationState.StartHeight,
EndHeight: metadata.MigrationState.EndHeight,
CurrentBlock: chainStatus.Sync.BestBlockHeight,
Status: metadata.MigrationState.Status,
StartHeight: metadata.MigrationState.StartHeight,
EndHeight: metadata.MigrationState.EndHeight,
CurrentHeight: chainStatus.Sync.BestBlockHeight,
},
}, nil
}
Expand Down

0 comments on commit 9290c89

Please sign in to comment.