Skip to content

Commit

Permalink
Fix: validators
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 4, 2024
1 parent 5298afe commit 958ea5e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/indexer/decode/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func parseValidatorUpdateAction(body *astria.Action_ValidatorUpdateAction, heigh
Height: action.Height,
ActionType: action.Type,
})
ctx.Validators.Set(pubKey, power)
ctx.Validators.Set(pubKey, power, address, height)
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/indexer/decode/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func TestAddressFromPubKey(t *testing.T) {
name: "test 3",
pk: "96F43A8448928F1E580864D69FE44E093C5A82A1D4A80C59086D7E67976CDA45",
want: "astria1z90efkxf3l7h8ln9rqnpz9q0pmw8c0y5dvfdhe",
}, {
name: "test 4",
pk: "352b09264c7ca6e2b40845f589973eeeb1c1068fc336ba571714ec018760be06",
want: "astria13cyhkel6fkkzskxtjntulcqtpwpvjvslfclvtg",
},
}
for _, tt := range tests {
Expand Down
10 changes: 7 additions & 3 deletions pkg/indexer/decode/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"

"github.com/celenium-io/astria-indexer/internal/storage"
"github.com/celenium-io/astria-indexer/pkg/types"
"github.com/shopspring/decimal"
)

Expand All @@ -16,7 +17,7 @@ func NewValidators() Validators {
return make(map[string]*storage.Validator)
}

func (v Validators) Set(pubKey []byte, power int64) *storage.Validator {
func (v Validators) Set(pubKey []byte, power int64, address string, height types.Level) *storage.Validator {
sPubKey := hex.EncodeToString(pubKey)

pow := decimal.NewFromInt(power)
Expand All @@ -26,8 +27,11 @@ func (v Validators) Set(pubKey []byte, power int64) *storage.Validator {
}

validator := &storage.Validator{
PubKey: pubKey,
Power: pow,
PubKey: pubKey,
Power: pow,
Address: address,
Height: height,
PubkeyType: "tendermint/PubKeyEd25519",
}
v[sPubKey] = validator
return validator
Expand Down
2 changes: 1 addition & 1 deletion pkg/indexer/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (module *Module) processBlockInTransaction(ctx context.Context, tx storage.
return state, err
}

if err := saveValidators(ctx, tx, block.Validators); err != nil {
if err := module.saveValidators(ctx, tx, block.Validators); err != nil {
return state, err
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/indexer/storage/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/celenium-io/astria-indexer/internal/storage"
)

func saveValidators(
func (module *Module) saveValidators(
ctx context.Context,
tx storage.Transaction,
validators map[string]*storage.Validator,
Expand All @@ -23,5 +23,15 @@ func saveValidators(
vals = append(vals, val)
}

return tx.UpdateValidators(ctx, vals...)
if err := tx.UpdateValidators(ctx, vals...); err != nil {
return err
}

for i := range vals {
if _, ok := module.validators[vals[i].Address]; !ok {
module.validators[vals[i].Address] = vals[i].Id
}
}

return nil
}

0 comments on commit 958ea5e

Please sign in to comment.