Skip to content

Commit

Permalink
Fix: delegated balances (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Dec 14, 2024
1 parent 397fb73 commit 73831b6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
8 changes: 8 additions & 0 deletions internal/storage/postgres/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ func createIndices(ctx context.Context, conn *database.Bun) error {
Exec(ctx); err != nil {
return err
}
if _, err := tx.NewCreateIndex().
IfNotExists().
Model((*storage.StakingLog)(nil)).
Index("staking_log_address_id_idx").
Column("address_id").
Exec(ctx); err != nil {
return err
}
if _, err := tx.NewCreateIndex().
IfNotExists().
Model((*storage.StakingLog)(nil)).
Expand Down
50 changes: 50 additions & 0 deletions internal/storage/postgres/migrations/20241213_balances_fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package migrations

import (
"context"

"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/shopspring/decimal"
"github.com/uptrace/bun"
)

func init() {
Migrations.MustRegister(upBalancesFix, downBalancesFix)
}

func upBalancesFix(ctx context.Context, db *bun.DB) error {
var balances []*storage.Balance
if err := db.NewSelect().Model(&balances).Where("delegated < 0").Scan(ctx); err != nil {
return err
}

for i := range balances {
var logs []*storage.StakingLog
if err := db.NewSelect().Model(&logs).Where("address_id = ?", balances[i].Id).Scan(ctx); err != nil {
return err
}

total := decimal.Zero
for j := range logs {
total = total.Add(logs[j].Change)
}

_, err := db.NewUpdate().
Model(balances[i]).
WherePK().
Set("delegated = ?", total).
Exec(ctx)
if err != nil {
return err
}
}

return nil
}

func downBalancesFix(_ context.Context, _ *bun.DB) error {
return nil
}
1 change: 1 addition & 0 deletions pkg/indexer/decode/handle/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func MsgCreateValidator(ctx *context.Context, status storageTypes.Status, m *cos
if err := ctx.AddAddress(&address); err != nil {
return msgType, nil, err
}
addresses[0].Balance = address.Balance

ctx.AddDelegation(storage.Delegation{
Address: &address,
Expand Down
1 change: 1 addition & 0 deletions pkg/indexer/decode/handle/test/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func TestDecodeMsg_SuccessOnMsgCreateValidator(t *testing.T) {
},
},
}
addressesExpected[0].Balance.Delegated = decimal.RequireFromString("1")

expectedValidators := map[string]*storage.Validator{
"celestiavaloper1fg9l3xvfuu9wxremv2229966zawysg4r40gw5x": {
Expand Down
18 changes: 12 additions & 6 deletions pkg/indexer/genesis/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (module *Module) parse(genesis types.Genesis) (parsedData, error) {
block.Txs = append(block.Txs, tx)
}

for _, addr := range decodeCtx.GetAddresses() {
data.addresses[addr.String()] = addr
}

module.parseDenomMetadata(genesis.AppState.Bank.DenomMetadata, &data)
module.parseConstants(genesis.AppState, genesis.ConsensusParams, &data)

Expand Down Expand Up @@ -187,13 +191,15 @@ func (module *Module) parseAccounts(accounts []types.Accounts, block storage.Blo
return errors.Errorf("unknown account type: %s", accounts[i].Type)
}

_, hash, err := pkgTypes.Address(readableAddress).Decode()
if err != nil {
return err
if _, ok := data.addresses[readableAddress]; !ok {
_, hash, err := pkgTypes.Address(readableAddress).Decode()
if err != nil {
return err
}
address.Hash = hash
address.Address = readableAddress
data.addresses[address.String()] = &address
}
address.Hash = hash
address.Address = readableAddress
data.addresses[address.String()] = &address
}
return nil
}
Expand Down

0 comments on commit 73831b6

Please sign in to comment.