Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: save validators #41

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/storage/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 0 additions & 43 deletions internal/storage/mock/generic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions internal/storage/postgres/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions internal/storage/postgres/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
3 changes: 1 addition & 2 deletions internal/storage/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ 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"
)

//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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/indexer/storage/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading