Skip to content

Commit

Permalink
feat: non-resident-keys and expired updates
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 4, 2024
1 parent 382d97a commit 0e2a4cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
29 changes: 16 additions & 13 deletions internal/accounts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -26,7 +27,7 @@ type Account struct {
// A count of how many participation Keys exist on this node for this Account
Keys int
// Expires is the date the participation key will expire
Expires time.Time
Expires *time.Time
}

// Get Online Status of Account
Expand All @@ -51,15 +52,21 @@ func GetAccount(client api.ClientWithResponsesInterface, address string) (api.Ac
return *r.JSON200, nil
}

func GetExpiresTime(t Time, key api.ParticipationKey, state *StateModel) time.Time {
// GetExpiresTime calculates and returns the expiration time for a participation key based on the current account state.
func GetExpiresTime(t Time, key api.ParticipationKey, state *StateModel) *time.Time {
now := t.Now()
var expires = now.Add(-(time.Hour * 24 * 365 * 100))
if key.LastBlockProposal != nil && state.Status.LastRound != 0 && state.Metrics.RoundTime != 0 {
roundDiff := max(0, *key.EffectiveLastValid-int(state.Status.LastRound))
var expires time.Time
if state.Accounts[key.Address].Status == "Online" &&
state.Accounts[key.Address].Participation != nil &&
bytes.Equal(*state.Accounts[key.Address].Participation.StateProofKey, *key.Key.StateProofKey) &&
state.Status.LastRound != 0 &&
state.Metrics.RoundTime != 0 {
roundDiff := max(0, key.Key.VoteLastValid-int(state.Status.LastRound))
distance := int(state.Metrics.RoundTime) * roundDiff
expires = now.Add(time.Duration(distance))
return &expires
}
return expires
return nil
}

// AccountsFromParticipationKeys maps an array of api.ParticipationKey to a keyed map of Account
Expand Down Expand Up @@ -94,7 +101,6 @@ func AccountsFromState(state *StateModel, t Time, client api.ClientWithResponses
} else {
incentiveEligible = *account.IncentiveEligible
}

values[key.Address] = Account{
Participation: account.Participation,
Address: key.Address,
Expand All @@ -106,12 +112,9 @@ func AccountsFromState(state *StateModel, t Time, client api.ClientWithResponses
}
} else {
val.Keys++
if val.Expires.Before(t.Now()) {
now := t.Now()
var expires = GetExpiresTime(t, key, state)
if !expires.Before(now) {
val.Expires = expires
}
if val.Participation != nil &&
bytes.Equal(*val.Participation.StateProofKey, *key.Key.StateProofKey) {
val.Expires = GetExpiresTime(t, key, state)
}
values[key.Address] = val
}
Expand Down
24 changes: 21 additions & 3 deletions ui/pages/accounts/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,31 @@ func (m ViewModel) makeRows() *[]table.Row {
rows := make([]table.Row, 0)

for key := range m.Data.Accounts {
expires := m.Data.Accounts[key].Expires.Format(time.RFC822)
var expires = "N/A"
if m.Data.Accounts[key].Expires != nil {
// This condition will only exist for a split second
// until algod deletes the key
if m.Data.Accounts[key].Expires.Before(time.Now()) {
expires = "EXPIRED"
} else {
expires = m.Data.Accounts[key].Expires.Format(time.RFC822)
}

// Expires within the week
if m.Data.Accounts[key].Expires.Before(time.Now().Add(time.Hour * 24 * 7)) {
expires = "⚠ " + expires
}
}

// Override the state while syncing
if m.Data.Status.State != internal.StableState {
expires = "SYNCING"
}
if !m.Data.Accounts[key].Expires.After(time.Now().Add(-(time.Hour * 24 * 365 * 50))) {
expires = "N/A"

if m.Data.Accounts[key].Status == "Online" && expires == "N/A" && m.Data.Accounts[key].Expires != nil {
expires = "⚠ NON-RESIDENT-KEY"
}

rows = append(rows, table.Row{
m.Data.Accounts[key].Address,
strconv.Itoa(m.Data.Accounts[key].Keys),
Expand Down

0 comments on commit 0e2a4cf

Please sign in to comment.