Skip to content

Commit

Permalink
feat: add tombstoned information inside validator status (forbole#275)
Browse files Browse the repository at this point in the history
## Description

Closes: forbole#274

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
huichiaotsou authored Dec 1, 2021
1 parent 436ff0f commit cc38030
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/fix/staking/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func validatorsCmd(parseConfig *parse.Config) *cobra.Command {
db := database.Cast(parseCtx.Database)

// Build the staking module
stakingModule := staking.NewModule(sources.StakingSource, nil, nil, nil, parseCtx.EncodingConfig.Marshaler, db)
stakingModule := staking.NewModule(sources.StakingSource, nil, nil, nil, nil, parseCtx.EncodingConfig.Marshaler, db)

// Get latest height
height, err := parseCtx.Node.LatestHeight()
Expand Down
1 change: 1 addition & 0 deletions database/schema/03-staking.sql
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ CREATE TABLE validator_status
validator_address TEXT NOT NULL REFERENCES validator (consensus_address) PRIMARY KEY,
status INT NOT NULL,
jailed BOOLEAN NOT NULL,
tombstoned BOOLEAN NOT NULL DEFAULT FALSE,
height BIGINT NOT NULL
);
CREATE INDEX validator_status_height_index ON validator_status (height);
Expand Down
11 changes: 6 additions & 5 deletions database/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,17 @@ func (db *Db) SaveValidatorsStatuses(statuses []types.ValidatorStatus) error {
validatorStmt := `INSERT INTO validator (consensus_address, consensus_pubkey) VALUES`
var valParams []interface{}

statusStmt := `INSERT INTO validator_status (validator_address, status, jailed, height) VALUES `
statusStmt := `INSERT INTO validator_status (validator_address, status, jailed, tombstoned, height) VALUES `
var statusParams []interface{}

for i, status := range statuses {
vi := i * 2
validatorStmt += fmt.Sprintf("($%d, $%d),", vi+1, vi+2)
valParams = append(valParams, status.ConsensusAddress, status.ConsensusPubKey)

si := i * 4
statusStmt += fmt.Sprintf("($%d,$%d,$%d,$%d),", si+1, si+2, si+3, si+4)
statusParams = append(statusParams, status.ConsensusAddress, status.Status, status.Jailed, status.Height)
si := i * 5
statusStmt += fmt.Sprintf("($%d,$%d,$%d,$%d,$%d),", si+1, si+2, si+3, si+4, si+5)
statusParams = append(statusParams, status.ConsensusAddress, status.Status, status.Jailed, status.Tombstoned, status.Height)
}

validatorStmt = validatorStmt[:len(validatorStmt)-1]
Expand All @@ -430,8 +430,9 @@ func (db *Db) SaveValidatorsStatuses(statuses []types.ValidatorStatus) error {
statusStmt = statusStmt[:len(statusStmt)-1]
statusStmt += `
ON CONFLICT (validator_address) DO UPDATE
SET status = excluded.status,
SET status = excluded.status,
jailed = excluded.jailed,
tombstoned = excluded.tombstoned,
height = excluded.height
WHERE validator_status.height <= excluded.height`
_, err = db.Sql.Exec(statusStmt, statusParams...)
Expand Down
8 changes: 8 additions & 0 deletions database/staking_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,15 @@ func (suite *DbTestSuite) TestSaveValidatorStatus() {
validator1.GetConsPubKey(),
1,
false,
false,
10,
),
types.NewValidatorStatus(
validator2.GetConsAddr(),
validator2.GetConsPubKey(),
2,
true,
true,
10,
),
})
Expand All @@ -654,12 +656,14 @@ func (suite *DbTestSuite) TestSaveValidatorStatus() {
dbtypes.NewValidatorStatusRow(
1,
false,
false,
validator1.GetConsAddr(),
10,
),
dbtypes.NewValidatorStatusRow(
2,
true,
true,
validator2.GetConsAddr(),
10,
),
Expand All @@ -676,13 +680,15 @@ func (suite *DbTestSuite) TestSaveValidatorStatus() {
validator1.GetConsPubKey(),
3,
true,
true,
9,
),
types.NewValidatorStatus(
validator2.GetConsAddr(),
validator2.GetConsPubKey(),
3,
true,
true,
11,
),
})
Expand All @@ -697,12 +703,14 @@ func (suite *DbTestSuite) TestSaveValidatorStatus() {
dbtypes.NewValidatorStatusRow(
1,
false,
false,
validator1.GetConsAddr(),
10,
),
dbtypes.NewValidatorStatusRow(
3,
true,
true,
validator2.GetConsAddr(),
11,
),
Expand Down
5 changes: 4 additions & 1 deletion database/types/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,17 @@ func (v ValidatorVotingPowerRow) Equal(w ValidatorVotingPowerRow) bool {
type ValidatorStatusRow struct {
Status int `db:"status"`
Jailed bool `db:"jailed"`
Tombstoned bool `db:"tombstoned"`
ConsAddress string `db:"validator_address"`
Height int64 `db:"height"`
}

// NewValidatorStatusRow builds a new ValidatorStatusRow
func NewValidatorStatusRow(status int, jailed bool, consAddess string, height int64) ValidatorStatusRow {
func NewValidatorStatusRow(status int, jailed bool, tombstoned bool, consAddess string, height int64) ValidatorStatusRow {
return ValidatorStatusRow{
Status: status,
Jailed: jailed,
Tombstoned: tombstoned,
ConsAddress: consAddess,
Height: height,
}
Expand All @@ -283,6 +285,7 @@ func NewValidatorStatusRow(status int, jailed bool, consAddess string, height in
func (v ValidatorStatusRow) Equal(w ValidatorStatusRow) bool {
return v.Status == w.Status &&
v.Jailed == w.Jailed &&
v.Tombstoned == w.Tombstoned &&
v.ConsAddress == w.ConsAddress &&
v.Height == w.Height
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ select_permissions:
- validator_address
- status
- jailed
- tombstoned
- height
filter: {}
role: anonymous
4 changes: 2 additions & 2 deletions modules/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules {
distrModule := distribution.NewModule(ctx.JunoConfig, sources.DistrSource, bankModule, db)
historyModule := history.NewModule(ctx.JunoConfig.Chain, r.parser, cdc, db)
mintModule := mint.NewModule(sources.MintSource, db)
stakingModule := staking.NewModule(sources.StakingSource, bankModule, distrModule, historyModule, cdc, db)
slashingModule := slashing.NewModule(sources.SlashingSource, stakingModule, db)
slashingModule := slashing.NewModule(sources.SlashingSource, nil, db)
stakingModule := staking.NewModule(sources.StakingSource, bankModule, distrModule, historyModule, slashingModule, cdc, db)

return []jmodules.Module{
messages.NewModule(r.parser, cdc, ctx.Database),
Expand Down
21 changes: 21 additions & 0 deletions modules/slashing/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,24 @@ func (s Source) GetParams(height int64) (slashingtypes.Params, error) {

return res.Params, nil
}

// GetSigningInfo implements slashingsource.GetSigningInfo
func (s Source) GetSigningInfo(height int64, consAddr sdk.ConsAddress) (slashingtypes.ValidatorSigningInfo, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
return slashingtypes.ValidatorSigningInfo{}, fmt.Errorf("error while loading height: %s", err)
}

res, err := s.querier.SigningInfo(
sdk.WrapSDKContext(ctx),
&slashingtypes.QuerySigningInfoRequest{
ConsAddress: consAddr.String(),
},
)

if err != nil {
return slashingtypes.ValidatorSigningInfo{}, err
}

return res.ValSigningInfo, nil
}
17 changes: 17 additions & 0 deletions modules/slashing/source/remote/source.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package remote

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/types/query"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/forbole/juno/v2/node/remote"
Expand Down Expand Up @@ -65,3 +67,18 @@ func (s Source) GetParams(height int64) (slashingtypes.Params, error) {

return res.Params, nil
}

// GetSigningInfo implements slashingsource.GetSigningInfo
func (s Source) GetSigningInfo(height int64, consAddr sdk.ConsAddress) (slashingtypes.ValidatorSigningInfo, error) {
res, err := s.querier.SigningInfo(
s.Ctx,
&slashingtypes.QuerySigningInfoRequest{
ConsAddress: consAddr.String(),
},
)

if err != nil {
return slashingtypes.ValidatorSigningInfo{}, err
}
return res.ValSigningInfo, nil
}
2 changes: 2 additions & 0 deletions modules/slashing/source/source.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package source

import (
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
)

type Source interface {
GetSigningInfo(height int64, consAddr sdk.ConsAddress) (slashingtypes.ValidatorSigningInfo, error)
GetSigningInfos(height int64) ([]slashingtypes.ValidatorSigningInfo, error)
GetParams(height int64) (slashingtypes.Params, error)
}
22 changes: 22 additions & 0 deletions modules/slashing/utils_signing_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package slashing

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/forbole/bdjuno/v2/types"
)

Expand All @@ -25,3 +27,23 @@ func (m *Module) getSigningInfos(height int64) ([]types.ValidatorSigningInfo, er

return infos, nil
}

// GetSigningInfo returns the signing info for the validator having the given consensus address at the specified height
func (m *Module) GetSigningInfo(height int64, consAddr sdk.ConsAddress) (types.ValidatorSigningInfo, error) {
info, err := m.source.GetSigningInfo(height, consAddr)
if err != nil {
return types.ValidatorSigningInfo{}, err
}

signingInfo := types.NewValidatorSigningInfo(
info.Address,
info.StartHeight,
info.IndexOffset,
info.JailedUntil,
info.Tombstoned,
info.MissedBlocksCounter,
height,
)

return signingInfo, nil
}
11 changes: 10 additions & 1 deletion modules/staking/expected_modules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package staking

import "time"
import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/bdjuno/v2/types"
)

type BankModule interface {
RefreshBalances(height int64, addresses []string) error
Expand All @@ -13,3 +18,7 @@ type DistrModule interface {
type HistoryModule interface {
UpdateAccountBalanceHistoryWithTime(address string, time time.Time) error
}

type SlashingModule interface {
GetSigningInfo(height int64, consAddr sdk.ConsAddress) (types.ValidatorSigningInfo, error)
}
28 changes: 15 additions & 13 deletions modules/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ var (

// Module represents the x/staking module
type Module struct {
cdc codec.Marshaler
db *database.Db
source stakingsource.Source
bankModule BankModule
distrModule DistrModule
historyModule HistoryModule
cdc codec.Marshaler
db *database.Db
source stakingsource.Source
bankModule BankModule
distrModule DistrModule
historyModule HistoryModule
slashingModule SlashingModule
}

// NewModule returns a new Module instance
func NewModule(
source stakingsource.Source,
bankModule BankModule, distrModule DistrModule, historyModule HistoryModule,
bankModule BankModule, distrModule DistrModule, historyModule HistoryModule, slashingModule SlashingModule,
cdc codec.Marshaler, db *database.Db,
) *Module {
return &Module{
cdc: cdc,
db: db,
source: source,
bankModule: bankModule,
distrModule: distrModule,
historyModule: historyModule,
cdc: cdc,
db: db,
source: source,
bankModule: bankModule,
distrModule: distrModule,
historyModule: historyModule,
slashingModule: slashingModule,
}
}

Expand Down
6 changes: 6 additions & 0 deletions modules/staking/utils_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,17 @@ func (m *Module) GetValidatorsStatuses(height int64, validators []stakingtypes.V
return nil, fmt.Errorf("error while getting validator consensus public key: %s", err)
}

valSigningInfo, err := m.slashingModule.GetSigningInfo(height, consAddr)
if err != nil {
return nil, fmt.Errorf("error while getting validator signing info: %s", err)
}

statuses[index] = types.NewValidatorStatus(
consAddr.String(),
consPubKey.String(),
int(validator.GetStatus()),
validator.IsJailed(),
valSigningInfo.Tombstoned,
height,
)
}
Expand Down
4 changes: 3 additions & 1 deletion types/staking_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ type ValidatorStatus struct {
ConsensusPubKey string
Status int
Jailed bool
Tombstoned bool
Height int64
}

// NewValidatorStatus creates a new ValidatorVotingPower
func NewValidatorStatus(valConsAddr, pubKey string, status int, jailed bool, height int64) ValidatorStatus {
func NewValidatorStatus(valConsAddr, pubKey string, status int, jailed bool, tombstoned bool, height int64) ValidatorStatus {
return ValidatorStatus{
ConsensusAddress: valConsAddr,
ConsensusPubKey: pubKey,
Status: status,
Jailed: jailed,
Tombstoned: tombstoned,
Height: height,
}
}
Expand Down

0 comments on commit cc38030

Please sign in to comment.