Skip to content

Commit

Permalink
fix epochConfirmed, use canVoteEpoch instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaiba committed Feb 19, 2025
1 parent 6bbe794 commit da36253
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
8 changes: 4 additions & 4 deletions node/exts/erc20-bridge/erc20/meta_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,13 +1071,13 @@ func init() {
return err
}

confirmed, err := epochConfirmed(ctx.TxContext.Ctx, app, epochID)
ok, err := canVoteEpoch(ctx.TxContext.Ctx, app, epochID)
if err != nil {
return fmt.Errorf("check epoch is confirmed: %w", err)
return fmt.Errorf("check epoch can vote: %w", err)
}

if confirmed {
return fmt.Errorf("epoch is already confirmed")
if !ok {
return fmt.Errorf("epoch cannot be voted")
}

return voteEpoch(ctx.TxContext.Ctx, app, epochID, from, amount, nonce, signature)
Expand Down
21 changes: 9 additions & 12 deletions node/exts/erc20-bridge/erc20/meta_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,8 @@ func getRewardsForEpoch(ctx context.Context, app *common.App, epochID *types.UUI
}

// previousEpochConfirmed return whether previous exists and confirmed.
func previousEpochConfirmed(ctx context.Context, app *common.App, instanceID *types.UUID, endBlock int64) (bool, bool, error) {
// if no previous epoch
exist := false
confirmed := false

err := app.Engine.ExecuteWithoutEngineCtx(ctx, app.DB, `
func previousEpochConfirmed(ctx context.Context, app *common.App, instanceID *types.UUID, endBlock int64) (exist bool, confirmed bool, err error) {
err = app.Engine.ExecuteWithoutEngineCtx(ctx, app.DB, `
{kwil_erc20_meta}SELECT confirmed from epochs
WHERE instance_id = $instance_id AND ended_at = $end_block
`, map[string]any{
Expand Down Expand Up @@ -580,27 +576,28 @@ func setVersionToCurrent(ctx context.Context, app *common.App) error {
}, nil)
}

func epochConfirmed(ctx context.Context, app *common.App, epochID *types.UUID) (bool, error) {
var confirmed bool
err := app.Engine.ExecuteWithoutEngineCtx(ctx, app.DB, `
// canVoteEpoch returns a bool indicate whether an epoch can be voted.
func canVoteEpoch(ctx context.Context, app *common.App, epochID *types.UUID) (ok bool, err error) {
// get epoch that is finalized, but not confirmed.
err = app.Engine.ExecuteWithoutEngineCtx(ctx, app.DB, `
{kwil_erc20_meta}SELECT confirmed
FROM epochs WHERE id = $id;
FROM epochs WHERE id = $id AND ended_at IS NOT NULL AND confirmed IS NOT true;
`, map[string]any{
"id": epochID,
}, func(row *common.Row) error {
if len(row.Values) != 1 {
return fmt.Errorf("expected 1 value, got %d", len(row.Values))
}

confirmed = row.Values[0].(bool)
ok = true
return nil
})

if err != nil {
return false, err
}

return confirmed, nil
return ok, nil
}

// voteEpoch vote an epoch by submitting signature.
Expand Down

0 comments on commit da36253

Please sign in to comment.