Skip to content

Commit

Permalink
fix: delegator job reset postgresql table sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
romever committed Jul 16, 2024
1 parent aa8507e commit 1c7e0b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
14 changes: 14 additions & 0 deletions job/model/delegatormodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type (
delegatorModel
SessionInsert(ctx context.Context, session sqlx.Session, data *Delegator) (sql.Result, error)
SessionDeleteAllByValidator(ctx context.Context, session sqlx.Session, validatorAddress string) error
SessionDeleteAll(ctx context.Context, session sqlx.Session) error
SessionResetSequence(ctx context.Context, session sqlx.Session) error
CountByValidator(ctx context.Context, validatorAddress string) (int64, error)
FindByValidator(ctx context.Context, validatorAddress string, pageable common.Pageable) ([]*Delegator, error)
}
Expand Down Expand Up @@ -46,6 +48,18 @@ func (m *customDelegatorModel) SessionDeleteAllByValidator(ctx context.Context,
return err
}

func (m *customDelegatorModel) SessionDeleteAll(ctx context.Context, session sqlx.Session) error {
query := fmt.Sprintf("delete from %s", m.table)
_, err := session.ExecCtx(ctx, query)
return err
}

func (m *customDelegatorModel) SessionResetSequence(ctx context.Context, session sqlx.Session) error {
query := fmt.Sprintf("alter sequence delegator_id_seq restart with 1")
_, err := session.ExecCtx(ctx, query)
return err
}

func (m *customDelegatorModel) CountByValidator(ctx context.Context, validatorAddress string) (int64, error) {
query := fmt.Sprintf("select count(*) from %s where validator=$1", m.table)
var resp int64
Expand Down
22 changes: 13 additions & 9 deletions job/task/validator_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ func DelegatorSync(ctx context.Context, svcCtx *svc.ServiceContext) {
return
}
err = svcCtx.PostgreDB.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
err = svcCtx.DelegatorModel.SessionDeleteAll(ctx, session)
if err != nil {
return fmt.Errorf("delegator SessionDeleteAll error, %v", err)
}
err = svcCtx.DelegatorModel.SessionResetSequence(ctx, session)
if err != nil {
return fmt.Errorf("delegator table reset sequence error, %v", err)
}
for _, validator := range validators {
var validatorAddress staking.Address
err := validatorAddress.UnmarshalText([]byte(validator.EntityAddress))
Expand All @@ -434,16 +442,12 @@ func DelegatorSync(ctx context.Context, svcCtx *svc.ServiceContext) {
return fmt.Errorf("staking api DelegationsTo error, %v", err)
}

err = svcCtx.DelegatorModel.SessionDeleteAllByValidator(ctx, session, validatorAddress.String())
if err != nil {
return fmt.Errorf("delegator SessionDeleteAllByValidator error, %v", err)
}
//err = svcCtx.DelegatorModel.SessionDeleteAllByValidator(ctx, session, validatorAddress.String())
//if err != nil {
// return fmt.Errorf("delegator SessionDeleteAllByValidator error, %v", err)
//}
for delegatorAddress, shares := range delegationsTo {
delegatorModel, err := svcCtx.DelegatorModel.FindOneByValidatorDelegator(ctx, validatorAddress.String(), delegatorAddress.String())
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
return fmt.Errorf("findOneByValidatorDelegator error, %v", err)
}
delegatorModel = &model.Delegator{
delegatorModel := &model.Delegator{
Validator: validatorAddress.String(),
Delegator: delegatorAddress.String(),
Shares: shares.Shares.ToBigInt().Int64(),
Expand Down

0 comments on commit 1c7e0b0

Please sign in to comment.