From 1c7e0b08db7411aa2f0fa44e7b2b9848b8ae44c3 Mon Sep 17 00:00:00 2001 From: romever <360876221@qq.com> Date: Tue, 16 Jul 2024 19:10:39 +0800 Subject: [PATCH] fix: delegator job reset postgresql table sequence. --- job/model/delegatormodel.go | 14 ++++++++++++++ job/task/validator_task.go | 22 +++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/job/model/delegatormodel.go b/job/model/delegatormodel.go index fca50d1..fd49e7a 100644 --- a/job/model/delegatormodel.go +++ b/job/model/delegatormodel.go @@ -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) } @@ -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 diff --git a/job/task/validator_task.go b/job/task/validator_task.go index 06c21f8..c09fe31 100644 --- a/job/task/validator_task.go +++ b/job/task/validator_task.go @@ -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)) @@ -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(),