Skip to content

Commit

Permalink
Validator issues (#54)
Browse files Browse the repository at this point in the history
* delegations sort by amount

* Show validators that haven't started yet.

* node uptime grace period. TODO: period length?
  • Loading branch information
dqnk committed Jul 18, 2024
1 parent d24c0e0 commit 8b7f5a2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
26 changes: 22 additions & 4 deletions database/pchain_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func FetchPChainStakingData(
txType PChainTxType,
offset int,
limit int,
alreadyStarted bool,
) ([]PChainTxData, error) {
var validatorTxs []PChainTxData

Expand All @@ -373,8 +374,13 @@ func FetchPChainStakingData(

query := db.
Table("p_chain_txes").
Joins("left join p_chain_tx_inputs as inputs on inputs.tx_id = p_chain_txes.tx_id").
Where("start_time <= ?", time).Where("? <= end_time", time).
Joins("left join p_chain_tx_inputs as inputs on inputs.tx_id = p_chain_txes.tx_id")

if alreadyStarted {
query = query.Where("start_time <= ?", time)
}

query = query.Where("? <= end_time", time).
Where("p_chain_txes.type = ?", txType).
Group("p_chain_txes.id").
Order("p_chain_txes.id").Offset(offset).Limit(limit).
Expand Down Expand Up @@ -446,6 +452,7 @@ func FetchPChainStakingDataPerNode(
offset int,
limit int,
sortDirection string,
amountSortDirection string,
) ([]PChainTxData, error) {
var validatorTxs []PChainTxData
if limit <= 0 || limit > 100 {
Expand All @@ -459,12 +466,23 @@ func FetchPChainStakingDataPerNode(
Table("p_chain_txes").
Where("p_chain_txes.type = ? AND p_chain_txes.node_id = ? AND start_time <= ? AND ? <= end_time", txType, nodeID, time, time).Offset(offset).Limit(limit).Select("p_chain_txes.*")

// Applied only if set
switch amountSortDirection {
case "desc":
query = query.Order("p_chain_txes.weight desc")
case "asc":
query = query.Order("p_chain_txes.weight")
}

// This is the default sort
if sortDirection == "desc" {
query = query.Order("end_time desc")
query = query.Order("p_chain_txes.end_time desc")
} else {
query = query.Order("end_time")
query = query.Order("p_chain_txes.end_time")
}

query.Scan(&validatorTxs)

return validatorTxs, query.Error
}

Expand Down
4 changes: 2 additions & 2 deletions indexer/cronjob/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (c *uptimeCronjob) Call() error {
// find matching start time
for _, e := range uptimes {
if e.StakingStart.Equal(v.StakingStart) {
// update the entity
if v.Connected {
// update the entity, allow a grace period of 1 minute
if now.Sub(v.StakingStart) >= 0 && now.Sub(v.StakingStart) < time.Minute || v.Connected {
e.Uptime += int64(now.Sub(e.UpdateTime))
e.Online = true
} else {
Expand Down
24 changes: 17 additions & 7 deletions services/routes/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ type GetStakerResponse struct {
}

type ListValidatorRequest struct {
AlreadyStarted bool `json:"alreadyStarted" jsonschema:"Only show validators that have already started (startTime <= now), excluding those that will start at some point in the future."`
PaginatedRequest
}

type SortingFields struct {
AmountSortDirection string `json:"amountSortDirection" validate:"omitempty,oneof=asc desc" jsonschema:"Sorting direction, 'asc' or 'desc'. If set, has priority over EndTimeSortDirection, but is not used by default."`
EndTimeSortDirection string `json:"endTimeSortDirection" validate:"omitempty,oneof=asc desc" jsonschema:"Sorting direction, 'asc' or 'desc'. If not specified or invalid, 'asc' is used"`
}
type ListDelegationsRequest struct {
PaginatedRequest
SortingRequest
SortingFields
NodeID string `json:"nodeID"`
}

Expand Down Expand Up @@ -90,6 +95,7 @@ type stakingDB interface {
txType database.PChainTxType,
offset int,
limit int,
alreadyStarted bool, // to return events that might not have started yet such as staking
) ([]database.PChainTxData, error)
GetPChainDelegators(
time time.Time,
Expand All @@ -106,6 +112,7 @@ type stakingDB interface {
offset int,
limit int,
sortDirection string,
amountSortDirection string,
) ([]database.PChainTxData, error)
GetPChainDelegatorsPerNode(
time time.Time,
Expand Down Expand Up @@ -153,7 +160,7 @@ func (rh *stakerRouteHandlers) listStakingTransactions(txType database.PChainTxT

func (rh *stakerRouteHandlers) listStakers(txType database.PChainTxType) utils.RouteHandler {
handler := func(request GetStakerRequest) ([]GetStakerResponse, *utils.ErrorHandler) {
stakerTxData, err := rh.db.GetPChainStakingData(request.Time, txType, request.Offset, request.Limit)
stakerTxData, err := rh.db.GetPChainStakingData(request.Time, txType, request.Offset, request.Limit, true)
if err != nil {
return nil, utils.InternalServerErrorHandler(err)
}
Expand Down Expand Up @@ -181,7 +188,7 @@ func (rh *stakerRouteHandlers) listValidators() utils.RouteHandler {
pagination := request.ToQueryPaginatedFilter()
offset := pagination.GetOffset()
limit := pagination.GetLimit()
validatorTxData, err := rh.db.GetPChainStakingData(now, txType, offset, limit)
validatorTxData, err := rh.db.GetPChainStakingData(now, txType, offset, limit, request.AlreadyStarted)
if err != nil {
return nil, utils.InternalServerErrorHandler(err)
}
Expand Down Expand Up @@ -285,7 +292,8 @@ func (rh *stakerRouteHandlers) getValidatorData() utils.RouteHandler {

now := rh.db.GetCurrentTime()

validatorTxData, err := rh.db.GetPChainStakingDataPerNode(now, node_id, database.PChainAddValidatorTx, 0, 100, "asc")
// NOTE: Do not sort by amount for now by passing empty string
validatorTxData, err := rh.db.GetPChainStakingDataPerNode(now, node_id, database.PChainAddValidatorTx, 0, 100, "asc", "")
if err != nil {
return nil, utils.InternalServerErrorHandler(err)
}
Expand Down Expand Up @@ -350,7 +358,7 @@ func (rh *stakerRouteHandlers) listValidatorDelegations() utils.RouteHandler {

now := rh.db.GetCurrentTime()

delegationsData, err := rh.db.GetPChainStakingDataPerNode(now, node_id, database.PChainAddDelegatorTx, offset, limit, request.Direction)
delegationsData, err := rh.db.GetPChainStakingDataPerNode(now, node_id, database.PChainAddDelegatorTx, offset, limit, request.SortingFields.EndTimeSortDirection, request.SortingFields.AmountSortDirection)
if err != nil {
return nil, utils.InternalServerErrorHandler(err)
}
Expand Down Expand Up @@ -408,8 +416,9 @@ func (rh stakerDBGorm) GetPChainStakingData(
txType database.PChainTxType,
offset int,
limit int,
alreadyStarted bool,
) ([]database.PChainTxData, error) {
return database.FetchPChainStakingData(rh.db, time, txType, offset, limit)
return database.FetchPChainStakingData(rh.db, time, txType, offset, limit, alreadyStarted)
}

func (rh stakerDBGorm) GetPChainValidatorData(
Expand Down Expand Up @@ -447,8 +456,9 @@ func (rh stakerDBGorm) GetPChainStakingDataPerNode(
offset int,
limit int,
sortDirection string,
amountSortDirection string,
) ([]database.PChainTxData, error) {
return database.FetchPChainStakingDataPerNode(rh.db, time, nodeID, txType, offset, limit, sortDirection)
return database.FetchPChainStakingDataPerNode(rh.db, time, nodeID, txType, offset, limit, sortDirection, amountSortDirection)
}

func (rh stakerDBGorm) GetPChainDelegatorsPerNode(
Expand Down
2 changes: 2 additions & 0 deletions services/routes/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (rh stakerDBTest) GetPChainStakingData(
txType database.PChainTxType,
offset int,
limit int,
alreadyStarted bool,
) ([]database.PChainTxData, error) {
fileData, err := os.ReadFile("../../resources/test/staking/validators_tx_data.json")
if err != nil {
Expand Down Expand Up @@ -189,6 +190,7 @@ func (rh stakerDBTest) GetPChainStakingDataPerNode(
offset int,
limit int,
sortDirection string,
amountSortDirection string,
) ([]database.PChainTxData, error) {
switch txType {
case database.PChainAddValidatorTx:
Expand Down

0 comments on commit 8b7f5a2

Please sign in to comment.