Skip to content

Commit

Permalink
Use LookupAgreement to hit caches
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti committed Dec 19, 2024
1 parent ec3007f commit 923a514
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions heartbeat/abstractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type ledger interface {

// LookupAccount allows the Service to observe accounts for suspension
LookupAccount(round basics.Round, addr basics.Address) (data ledgercore.AccountData, validThrough basics.Round, withoutRewards basics.MicroAlgos, err error)

LookupAgreement(rnd basics.Round, addr basics.Address) (basics.OnlineAccountData, error)
}

// participants captures the aspects of the AccountManager that are used by this
Expand Down
14 changes: 9 additions & 5 deletions heartbeat/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *Service) findChallenged(rules config.ProposerPayoutRules, current basic

var found []account.ParticipationRecordForRound
for _, pr := range s.accts.Keys(current) { // only look at accounts we have part keys for
acct, _, _, err := s.ledger.LookupAccount(current, pr.Account)
acct, err := s.ledger.LookupAgreement(current, pr.Account)
if err != nil {
s.log.Errorf("error looking up %v: %v", pr.Account, err)
continue
Expand All @@ -97,14 +97,18 @@ func (s *Service) findChallenged(rules config.ProposerPayoutRules, current basic
if acct.VoteID != pr.Voting.OneTimeSignatureVerifier {
continue
}
if acct.Status == basics.Online && acct.IncentiveEligible {
if ch.Failed(pr.Account, acct.LastSeen()) {
// We want to match the logic in generateKnockOfflineAccountsList, but
// don't need to check Online status because we obtained records from
// LookupAgreement, which only returns Online accounts (or empty, which
// will not be IncentiveEligible) If we ever decide to knockoff accounts
// that are not IncentiveEligoible, this code should remember to check
// acct.MicroAlgosWithRewards > 0 to ensure we need a heartbeat.
if acct.IncentiveEligible {
if ch.Failed(pr.Account, max(acct.LastHeartbeat, acct.LastProposed)) {
s.log.Infof(" %v needs a heartbeat\n", pr.Account)
found = append(found, pr)
}
}
/* If we add a grace period to suspension for absenteeism, then we could
also make it free to heartbeat during that period. */
}
return found
}
Expand Down
23 changes: 23 additions & 0 deletions heartbeat/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ func (l *mockedLedger) LookupAccount(round basics.Round, addr basics.Address) (l
return ledgercore.AccountData{}, round, basics.MicroAlgos{}, nil
}

func (l *mockedLedger) LookupAgreement(round basics.Round, addr basics.Address) (basics.OnlineAccountData, error) {
l.mu.Lock()
defer l.mu.Unlock()

if round > l.lastRound() {
panic("mockedLedger.LookupAgreement: future round")
}

for r := round; r <= round; r-- {
if acct, ok := l.history[r][addr]; ok {
oad := basics.OnlineAccountData{
MicroAlgosWithRewards: acct.MicroAlgos,
VotingData: acct.VotingData,
IncentiveEligible: acct.IncentiveEligible,
LastProposed: acct.LastProposed,
LastHeartbeat: acct.LastHeartbeat,
}
return oad, nil
}
}
return basics.OnlineAccountData{}, nil
}

// waitFor confirms that the Service made it through the last block in the
// ledger and is waiting for the next. The Service is written such that it
// operates properly without this sort of wait, but for testing, we often want
Expand Down

0 comments on commit 923a514

Please sign in to comment.