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

feat: add tombstoned information inside validator status #275

Merged
merged 11 commits into from
Dec 1, 2021
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
20 changes: 20 additions & 0 deletions modules/slashing/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,23 @@ func (s Source) GetParams(height int64) (slashingtypes.Params, error) {

return res.Params, nil
}

func (s Source) GetSigningInfo(height int64, consAddr sdk.ConsAddress) (slashingtypes.ValidatorSigningInfo, error) {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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
}
16 changes: 16 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,17 @@ func (s Source) GetParams(height int64) (slashingtypes.Params, error) {

return res.Params, nil
}

func (s Source) GetSigningInfo(height int64, consAddr sdk.ConsAddress) (slashingtypes.ValidatorSigningInfo, error) {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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)
}
21 changes: 21 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,22 @@ func (m *Module) getSigningInfos(height int64) ([]types.ValidatorSigningInfo, er

return infos, nil
}

func (m *Module) GetSigningInfo(height int64, consAddr sdk.ConsAddress) (types.ValidatorSigningInfo, error) {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
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