From 1a89f6d9443d0cec7079713fbfc9c792d15c4f50 Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 22 Sep 2024 15:30:54 +0300 Subject: [PATCH] Fix: save validators --- internal/storage/generic.go | 1 - internal/storage/mock/generic.go | 43 ------------------- internal/storage/postgres/transaction.go | 18 -------- internal/storage/postgres/transaction_test.go | 12 +++--- internal/storage/validator.go | 3 +- pkg/indexer/storage/validators.go | 2 +- 6 files changed, 9 insertions(+), 70 deletions(-) diff --git a/internal/storage/generic.go b/internal/storage/generic.go index 3e363f0..185c03f 100644 --- a/internal/storage/generic.go +++ b/internal/storage/generic.go @@ -76,7 +76,6 @@ type Transaction interface { UpdateAddresses(ctx context.Context, address ...*Address) error UpdateConstants(ctx context.Context, constants ...*Constant) error UpdateRollups(ctx context.Context, rollups ...*Rollup) error - UpdateValidators(ctx context.Context, validators ...*Validator) error LastBlock(ctx context.Context) (block Block, err error) State(ctx context.Context, name string) (state State, err error) diff --git a/internal/storage/mock/generic.go b/internal/storage/mock/generic.go index ee336b2..9761330 100644 --- a/internal/storage/mock/generic.go +++ b/internal/storage/mock/generic.go @@ -1977,49 +1977,6 @@ func (c *MockTransactionUpdateRollupsCall) DoAndReturn(f func(context.Context, . return c } -// UpdateValidators mocks base method. -func (m *MockTransaction) UpdateValidators(ctx context.Context, validators ...*storage.Validator) error { - m.ctrl.T.Helper() - varargs := []any{ctx} - for _, a := range validators { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "UpdateValidators", varargs...) - ret0, _ := ret[0].(error) - return ret0 -} - -// UpdateValidators indicates an expected call of UpdateValidators. -func (mr *MockTransactionMockRecorder) UpdateValidators(ctx any, validators ...any) *MockTransactionUpdateValidatorsCall { - mr.mock.ctrl.T.Helper() - varargs := append([]any{ctx}, validators...) - call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateValidators", reflect.TypeOf((*MockTransaction)(nil).UpdateValidators), varargs...) - return &MockTransactionUpdateValidatorsCall{Call: call} -} - -// MockTransactionUpdateValidatorsCall wrap *gomock.Call -type MockTransactionUpdateValidatorsCall struct { - *gomock.Call -} - -// Return rewrite *gomock.Call.Return -func (c *MockTransactionUpdateValidatorsCall) Return(arg0 error) *MockTransactionUpdateValidatorsCall { - c.Call = c.Call.Return(arg0) - return c -} - -// Do rewrite *gomock.Call.Do -func (c *MockTransactionUpdateValidatorsCall) Do(f func(context.Context, ...*storage.Validator) error) *MockTransactionUpdateValidatorsCall { - c.Call = c.Call.Do(f) - return c -} - -// DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockTransactionUpdateValidatorsCall) DoAndReturn(f func(context.Context, ...*storage.Validator) error) *MockTransactionUpdateValidatorsCall { - c.Call = c.Call.DoAndReturn(f) - return c -} - // Validators mocks base method. func (m *MockTransaction) Validators(ctx context.Context) ([]storage.Validator, error) { m.ctrl.T.Helper() diff --git a/internal/storage/postgres/transaction.go b/internal/storage/postgres/transaction.go index 15bdffb..d64a5c2 100644 --- a/internal/storage/postgres/transaction.go +++ b/internal/storage/postgres/transaction.go @@ -415,24 +415,6 @@ func (tx Transaction) RetentionBlockSignatures(ctx context.Context, height types return err } -func (tx Transaction) UpdateValidators(ctx context.Context, validators ...*models.Validator) error { - if len(validators) == 0 { - return nil - } - - for _, val := range validators { - _, err := tx.Tx().NewUpdate(). - Model(val). - Where("pubkey = ?", val.PubKey). - Set("power = ?", val.Power). - Exec(ctx) - if err != nil { - return err - } - } - return nil -} - func (tx Transaction) UpdateConstants(ctx context.Context, constants ...*models.Constant) error { if len(constants) == 0 { return nil diff --git a/internal/storage/postgres/transaction_test.go b/internal/storage/postgres/transaction_test.go index 9d69d42..4095e5d 100644 --- a/internal/storage/postgres/transaction_test.go +++ b/internal/storage/postgres/transaction_test.go @@ -754,26 +754,28 @@ func (s *TransactionTestSuite) TestRetentionBlockSignatures() { s.Require().Len(signs, 3) } -func (s *TransactionTestSuite) TestUpdateValidators() { +func (s *TransactionTestSuite) TestCreateValidator() { ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second) defer ctxCancel() tx, err := BeginTransaction(ctx, s.storage.Transactable) s.Require().NoError(err) - pk, err := hex.DecodeString("32415f09dbee4297cc9a841c2c2312bf903fc53c48860d788ae66097355a585f") + pk, err := hex.DecodeString("52415f09dbee4297cc9a841c2c2312bf903fc53c48860d788ae66097355a5851") s.Require().NoError(err) - err = tx.UpdateValidators(ctx, &storage.Validator{ + val := &storage.Validator{ PubKey: pk, Power: decimal.NewFromInt(10000), - }) + } + err = tx.SaveValidators(ctx, val) s.Require().NoError(err) + s.Require().Greater(val.Id, uint64(0)) s.Require().NoError(tx.Flush(ctx)) s.Require().NoError(tx.Close(ctx)) - validator, err := s.storage.Validator.GetByID(ctx, 1) + validator, err := s.storage.Validator.GetByID(ctx, val.Id) s.Require().NoError(err) s.Require().EqualValues("10000", validator.Power.String()) } diff --git a/internal/storage/validator.go b/internal/storage/validator.go index e7f5edd..55fa1ac 100644 --- a/internal/storage/validator.go +++ b/internal/storage/validator.go @@ -8,7 +8,6 @@ import ( pkgTypes "github.com/celenium-io/astria-indexer/pkg/types" - "github.com/dipdup-net/indexer-sdk/pkg/storage" sdk "github.com/dipdup-net/indexer-sdk/pkg/storage" "github.com/shopspring/decimal" "github.com/uptrace/bun" @@ -16,7 +15,7 @@ import ( //go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed type IValidator interface { - storage.Table[*Validator] + sdk.Table[*Validator] ListByPower(ctx context.Context, limit, offset int, order sdk.SortOrder) ([]Validator, error) } diff --git a/pkg/indexer/storage/validators.go b/pkg/indexer/storage/validators.go index ae1c69b..5affbd5 100644 --- a/pkg/indexer/storage/validators.go +++ b/pkg/indexer/storage/validators.go @@ -23,7 +23,7 @@ func (module *Module) saveValidators( vals = append(vals, val) } - if err := tx.UpdateValidators(ctx, vals...); err != nil { + if err := tx.SaveValidators(ctx, vals...); err != nil { return err }