-
Notifications
You must be signed in to change notification settings - Fork 471
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
incentives: cache top online accounts and use when building AbsentParticipationAccounts #6085
Changes from 13 commits
21db44d
e968515
8587b28
38d4b8d
9740ddc
b8b9673
0b7fbad
b2f1130
5242990
0baf81f
be464cf
97d0bcf
55d5068
01b150a
0f954d1
c24e809
c252d95
bb82a97
04c0a84
c41a49a
e476730
9115aae
23b9996
9bc39aa
c558d59
9d46fa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ type LedgerForCowBase interface { | |
CheckDup(config.ConsensusParams, basics.Round, basics.Round, basics.Round, transactions.Txid, ledgercore.Txlease) error | ||
LookupWithoutRewards(basics.Round, basics.Address) (ledgercore.AccountData, basics.Round, error) | ||
LookupAgreement(basics.Round, basics.Address) (basics.OnlineAccountData, error) | ||
GetKnockOfflineCandidates(basics.Round, config.ConsensusParams) (map[basics.Address]basics.OnlineAccountData, error) | ||
jannotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LookupAsset(basics.Round, basics.Address, basics.AssetIndex) (ledgercore.AssetResource, error) | ||
LookupApplication(basics.Round, basics.Address, basics.AppIndex) (ledgercore.AppResource, error) | ||
LookupKv(basics.Round, string) ([]byte, error) | ||
|
@@ -237,6 +238,10 @@ func (x *roundCowBase) lookupAgreement(addr basics.Address) (basics.OnlineAccoun | |
return ad, err | ||
} | ||
|
||
func (x *roundCowBase) knockOfflineCandidates() (map[basics.Address]basics.OnlineAccountData, error) { | ||
return x.l.GetKnockOfflineCandidates(x.rnd, x.proto) | ||
} | ||
|
||
// onlineStake returns the total online stake as of the start of the round. It | ||
// caches the result to prevent repeated calls to the ledger. | ||
func (x *roundCowBase) onlineStake() (basics.MicroAlgos, error) { | ||
|
@@ -1611,20 +1616,68 @@ func (eval *BlockEvaluator) generateKnockOfflineAccountsList() { | |
if !eval.generate { | ||
return | ||
} | ||
current := eval.Round() | ||
|
||
current := eval.Round() | ||
maxExpirations := eval.proto.MaxProposedExpiredOnlineAccounts | ||
maxSuspensions := eval.proto.Payouts.MaxMarkAbsent | ||
|
||
updates := &eval.block.ParticipationUpdates | ||
|
||
ch := activeChallenge(&eval.proto, uint64(eval.Round()), eval.state) | ||
ch := activeChallenge(&eval.proto, uint64(current), eval.state) | ||
|
||
// Make a set of candidate addresses to check for absentee status. | ||
type candidateData struct { | ||
VoteLastValid basics.Round | ||
VoteID crypto.OneTimeSignatureVerifier | ||
Status basics.Status | ||
LastProposed basics.Round | ||
LastHeartbeat basics.Round | ||
MicroAlgosWithRewards basics.MicroAlgos | ||
IncentiveEligible bool // currently unused below, but may be needed in the future | ||
} | ||
absentCandidates := make(map[basics.Address]candidateData) | ||
|
||
// First, ask the ledger for the top N online accounts, with their latest | ||
// online account data, current up to the previous round. | ||
if maxSuspensions > 0 { | ||
knockOfflineCandidates, err := eval.state.knockOfflineCandidates() | ||
cce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
// Log an error and keep going; generating lists of absent and expired | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this implies some nodes can "choose" not to search for absent/expired accounts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, when generating a block it is not required they put any accounts in the {Absent,Expired}ParticipationAccounts block headers, but if they are in the list, validation rules require that the accounts are actually absent or expired. |
||
// accounts is not required by block validation rules. | ||
logging.Base().Warnf("error fetching knockOfflineCandidates: %v", err) | ||
knockOfflineCandidates = nil | ||
} | ||
for accountAddr, acctData := range knockOfflineCandidates { | ||
// acctData is from previous block: doesn't include any updates in mods | ||
absentCandidates[accountAddr] = candidateData{ | ||
VoteLastValid: acctData.VoteLastValid, | ||
VoteID: acctData.VoteID, | ||
Status: basics.Online, // from lookupOnlineAccountData, which only returns online accounts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess need a test to enforce knockOfflineCandidates -> lookupOnlineAccountData control flow |
||
LastProposed: acctData.LastProposed, | ||
LastHeartbeat: acctData.LastHeartbeat, | ||
MicroAlgosWithRewards: acctData.MicroAlgosWithRewards, | ||
IncentiveEligible: acctData.IncentiveEligible, | ||
} | ||
} | ||
} | ||
|
||
// Then add any accounts modified in this block, with their state at the | ||
// end of the round. | ||
for _, accountAddr := range eval.state.modifiedAccounts() { | ||
acctData, found := eval.state.mods.Accts.GetData(accountAddr) | ||
if !found { | ||
continue | ||
} | ||
// This will overwrite data from the knockOfflineCandidates() list, if they were modified in the current block. | ||
absentCandidates[accountAddr] = candidateData{ | ||
VoteLastValid: acctData.VoteLastValid, | ||
VoteID: acctData.VoteID, | ||
Status: acctData.Status, | ||
jannotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LastProposed: acctData.LastProposed, | ||
LastHeartbeat: acctData.LastHeartbeat, | ||
MicroAlgosWithRewards: acctData.WithUpdatedRewards(eval.proto, eval.state.rewardsLevel()).MicroAlgos, | ||
IncentiveEligible: acctData.IncentiveEligible, | ||
} | ||
|
||
// Regardless of being online or suspended, if voting data exists, the | ||
// account can be expired to remove it. This means an offline account | ||
|
@@ -1637,17 +1690,24 @@ func (eval *BlockEvaluator) generateKnockOfflineAccountsList() { | |
updates.ExpiredParticipationAccounts, | ||
accountAddr, | ||
) | ||
continue // if marking expired, do not also suspend | ||
delete(absentCandidates, accountAddr) // if marking expired, do not also suspend | ||
} | ||
} | ||
} | ||
|
||
// Now, check these candidate accounts to see if they are expired or absent. | ||
for accountAddr, acctData := range absentCandidates { | ||
if acctData.MicroAlgosWithRewards.IsZero() { | ||
continue // should only happen in tests; prevents panic in isAbsent | ||
} | ||
|
||
if len(updates.AbsentParticipationAccounts) >= maxSuspensions { | ||
continue // no more room (don't break the loop, since we may have more expiries) | ||
} | ||
|
||
if acctData.Status == basics.Online { | ||
lastSeen := max(acctData.LastProposed, acctData.LastHeartbeat) | ||
if isAbsent(eval.state.prevTotals.Online.Money, acctData.MicroAlgos, lastSeen, current) || | ||
if isAbsent(eval.state.prevTotals.Online.Money, acctData.MicroAlgosWithRewards, lastSeen, current) || | ||
jannotti marked this conversation as resolved.
Show resolved
Hide resolved
gmalouf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
failsChallenge(ch, accountAddr, lastSeen) { | ||
updates.AbsentParticipationAccounts = append( | ||
updates.AbsentParticipationAccounts, | ||
|
@@ -1658,14 +1718,6 @@ func (eval *BlockEvaluator) generateKnockOfflineAccountsList() { | |
} | ||
} | ||
|
||
// delete me in Go 1.21 | ||
func max(a, b basics.Round) basics.Round { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
// bitsMatch checks if the first n bits of two byte slices match. Written to | ||
// work on arbitrary slices, but we expect that n is small. Only user today | ||
// calls with n=5. | ||
|
@@ -1821,6 +1873,9 @@ func (eval *BlockEvaluator) validateAbsentOnlineAccounts() error { | |
if acctData.Status != basics.Online { | ||
return fmt.Errorf("proposed absent account %v was %v, not Online", accountAddr, acctData.Status) | ||
} | ||
if acctData.MicroAlgos.IsZero() { | ||
return fmt.Errorf("proposed absent account %v with zero algos", accountAddr) | ||
} | ||
jannotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
lastSeen := max(acctData.LastProposed, acctData.LastHeartbeat) | ||
if isAbsent(eval.state.prevTotals.Online.Money, acctData.MicroAlgos, lastSeen, eval.Round()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -638,10 +638,39 @@ func (l *Ledger) LookupAgreement(rnd basics.Round, addr basics.Address) (basics. | |
defer l.trackerMu.RUnlock() | ||
|
||
// Intentionally apply (pending) rewards up to rnd. | ||
data, err := l.acctsOnline.LookupOnlineAccountData(rnd, addr) | ||
data, err := l.acctsOnline.lookupOnlineAccountData(rnd, addr) | ||
return data, err | ||
} | ||
|
||
// GetKnockOfflineCandidates retrieves a list of online accounts who will be | ||
// checked to a recent proposal or heartbeat. Large accounts are the ones worth checking. | ||
func (l *Ledger) GetKnockOfflineCandidates(rnd basics.Round, proto config.ConsensusParams) (map[basics.Address]basics.OnlineAccountData, error) { | ||
l.trackerMu.RLock() | ||
defer l.trackerMu.RUnlock() | ||
|
||
// get state proof worker's most recent list for top N addresses | ||
if proto.StateProofInterval == 0 { | ||
return nil, nil | ||
} | ||
// get latest state proof voters information, up to rnd, without calling cond.Wait() | ||
_, voters := l.acctsOnline.voters.LatestCompletedVotersUpTo(rnd) | ||
if voters == nil { // no cached voters found < rnd | ||
return nil, nil | ||
} | ||
|
||
// fetch fresh data up to this round from online account cache. These accounts should all | ||
// be in cache, as long as proto.StateProofTopVoters < onlineAccountsCacheMaxSize. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a condition to call out in the consensus file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added TestOnlineAccountsCacheSizeBiggerThanStateProofTopVoters |
||
ret := make(map[basics.Address]basics.OnlineAccountData) | ||
for addr := range voters.AddrToPos { | ||
data, err := l.acctsOnline.lookupOnlineAccountData(rnd, addr) | ||
if err != nil { | ||
continue // skip missing / not online accounts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why would voters ever return non-online account? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the voters are only calculating Top N every 256 rounds, so if a lookup for the current round (for the cached addr from the last state proof interval) being requested is that the account was closed/deleted, you could hit an error here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should add a comment and write a test exercising this case, realizing it is kind of complicated now after writing it out |
||
} | ||
ret[addr] = data | ||
} | ||
return ret, nil | ||
} | ||
|
||
// LookupWithoutRewards is like Lookup but does not apply pending rewards up | ||
// to the requested round rnd. | ||
func (l *Ledger) LookupWithoutRewards(rnd basics.Round, addr basics.Address) (ledgercore.AccountData, basics.Round, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a NIT: should we actually call this top online accounts or similar naming? It's very clear from comments that's what we are requesting, more a debate over if the name should be based on what it's sourced from vs the use-case we have for this atm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a potentially stale list of top online accounts, if new accounts appeared online in the last 256 rounds (since the last state proof) they wouldn't appear. So the word "candidates" was intended to make it seem a little less definitive that this was the complete list of top online accounts for the round... but happy to pick any other name, I wasn't particularly happy with this name.
This is already being used in a method JJ called "generateKnockOfflineAccountsList" in #5757 which is where the "knockOffline" part came from.