-
Notifications
You must be signed in to change notification settings - Fork 473
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 25 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 |
---|---|---|
|
@@ -47,6 +47,7 @@ type roundCowParent interface { | |
// lookup retrieves agreement data about an address, querying the ledger if necessary. | ||
lookupAgreement(basics.Address) (basics.OnlineAccountData, error) | ||
onlineStake() (basics.MicroAlgos, error) | ||
knockOfflineCandidates() (map[basics.Address]basics.OnlineAccountData, error) | ||
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. 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 commentThe 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. |
||
|
||
// lookupAppParams, lookupAssetParams, lookupAppLocalState, and lookupAssetHolding retrieve data for a given address and ID. | ||
// If cacheOnly is set, the ledger DB will not be queried, and only the cache will be consulted. | ||
|
@@ -192,6 +193,10 @@ func (cb *roundCowState) lookupAgreement(addr basics.Address) (data basics.Onlin | |
return cb.lookupParent.lookupAgreement(addr) | ||
} | ||
|
||
func (cb *roundCowState) knockOfflineCandidates() (map[basics.Address]basics.OnlineAccountData, error) { | ||
return cb.lookupParent.knockOfflineCandidates() | ||
} | ||
|
||
func (cb *roundCowState) lookupAppParams(addr basics.Address, aidx basics.AppIndex, cacheOnly bool) (ledgercore.AppParamsDelta, bool, error) { | ||
params, ok := cb.mods.Accts.GetAppParams(addr, aidx) | ||
if ok { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
"github.com/algorand/go-algorand/ledger/ledgercore" | ||
"github.com/algorand/go-algorand/logging" | ||
"github.com/algorand/go-algorand/protocol" | ||
"github.com/algorand/go-algorand/util" | ||
"github.com/algorand/go-algorand/util/execpool" | ||
) | ||
|
||
|
@@ -48,6 +49,7 @@ | |
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 +239,10 @@ | |
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) { | ||
|
@@ -1339,7 +1345,13 @@ | |
} | ||
|
||
// Call "endOfBlock" after all the block's rewards and transactions are processed. | ||
func (eval *BlockEvaluator) endOfBlock() error { | ||
// When generating a block, participating addresses are passed to prevent a | ||
// proposer from suspending itself. | ||
func (eval *BlockEvaluator) endOfBlock(participating ...basics.Address) error { | ||
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 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. it's true, this is just me optimizing for a smaller diff, to not change other endOfBlock callers, but the idea is to pass a slice — can change |
||
if participating != nil && !eval.generate { | ||
panic("logic error: only pass partAddresses to endOfBlock when generating") | ||
} | ||
|
||
if eval.generate { | ||
var err error | ||
eval.block.TxnCommitments, err = eval.block.PaysetCommit() | ||
|
@@ -1364,7 +1376,7 @@ | |
} | ||
} | ||
|
||
eval.generateKnockOfflineAccountsList() | ||
eval.generateKnockOfflineAccountsList(participating) | ||
|
||
if eval.proto.StateProofInterval > 0 { | ||
var basicStateProof bookkeeping.StateProofTrackingData | ||
|
@@ -1607,25 +1619,94 @@ | |
// deltas and testing if any of them needs to be reset/suspended. Expiration | ||
// takes precedence - if an account is expired, it should be knocked offline and | ||
// key material deleted. If it is only suspended, the key material will remain. | ||
func (eval *BlockEvaluator) generateKnockOfflineAccountsList() { | ||
// | ||
// Different ndoes may propose different list of addresses based on node state. | ||
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. ndoes -> nodes |
||
// Block validators only check whether ExpiredParticipationAccounts or | ||
// AbsentParticipationAccounts meet the criteria for expiration or suspension, | ||
// not whether the lists are complete. | ||
// | ||
// This function is passed a list of participating addresses so a node will not | ||
// propose a block that suspends or expires itself. | ||
func (eval *BlockEvaluator) generateKnockOfflineAccountsList(participating []basics.Address) { | ||
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. participating is really "participating accounts excluding any I host" 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. here, the "participating" argument is the accounts that the node hosts. |
||
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 expired or 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 | ||
} | ||
candidates := make(map[basics.Address]candidateData) | ||
partAddrs := util.MakeSet(participating...) | ||
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. Do we do anything else with this slice? Maybe we should push the 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. It's used in GenerateBlock while making a map of end-of-block account state for participating addresses, to include in the |
||
|
||
// 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 | ||
candidates[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. | ||
candidates[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, | ||
} | ||
} | ||
|
||
// Now, check these candidate accounts to see if they are expired or absent. | ||
for accountAddr, acctData := range candidates { | ||
if acctData.MicroAlgosWithRewards.IsZero() { | ||
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. 100% of time, zero balance implies being closed? 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, that's correct, my understanding is currently the only way you can have a zero balance at the end of the round is if your account has been closed. |
||
continue // don't check accounts that are being closed | ||
} | ||
|
||
if _, ok := partAddrs[accountAddr]; ok { | ||
continue // don't check our own participation accounts | ||
} | ||
|
||
// Expired check: are this account's voting keys no longer valid? | ||
// Regardless of being online or suspended, if voting data exists, the | ||
// account can be expired to remove it. This means an offline account | ||
// can be expired (because it was already suspended). | ||
|
@@ -1641,13 +1722,15 @@ | |
} | ||
} | ||
|
||
// Absent check: has it been too long since the last heartbeat/proposal, or | ||
// has this online account failed a challenge? | ||
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 +1741,6 @@ | |
} | ||
} | ||
|
||
// 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 +1896,9 @@ | |
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()) { | ||
|
@@ -1890,7 +1968,16 @@ | |
// After a call to GenerateBlock, the BlockEvaluator can still be used to | ||
// accept transactions. However, to guard against reuse, subsequent calls | ||
// to GenerateBlock on the same BlockEvaluator will fail. | ||
func (eval *BlockEvaluator) GenerateBlock(addrs []basics.Address) (*ledgercore.UnfinishedBlock, error) { | ||
// | ||
// A list of participating addresses is passed to GenerateBlock. This lets | ||
// the BlockEvaluator know which of this node's participating addresses might | ||
// be proposing this block. This information is used when: | ||
// - generating lists of absent accounts (don't suspend yourself) | ||
// - preparing a ledgercore.UnfinishedBlock, which contains the end-of-block | ||
// state of each potential proposer. This allows for a final check in | ||
// UnfinishedBlock.FinishBlock to ensure the proposer hasn't closed its | ||
// account before setting the ProposerPayout header. | ||
func (eval *BlockEvaluator) GenerateBlock(participating []basics.Address) (*ledgercore.UnfinishedBlock, error) { | ||
if !eval.generate { | ||
logging.Base().Panicf("GenerateBlock() called but generate is false") | ||
} | ||
|
@@ -1899,19 +1986,19 @@ | |
return nil, fmt.Errorf("GenerateBlock already called on this BlockEvaluator") | ||
} | ||
|
||
err := eval.endOfBlock() | ||
err := eval.endOfBlock(participating...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// look up set of participation accounts passed to GenerateBlock (possible proposers) | ||
finalAccounts := make(map[basics.Address]ledgercore.AccountData, len(addrs)) | ||
for i := range addrs { | ||
acct, err := eval.state.lookup(addrs[i]) | ||
// look up end-of-block state of possible proposers passed to GenerateBlock | ||
finalAccounts := make(map[basics.Address]ledgercore.AccountData, len(participating)) | ||
for i := range participating { | ||
acct, err := eval.state.lookup(participating[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
finalAccounts[addrs[i]] = acct | ||
finalAccounts[participating[i]] = acct | ||
} | ||
|
||
vb := ledgercore.MakeUnfinishedBlock(eval.block, eval.state.deltas(), finalAccounts) | ||
|
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.
Why not keep this on future?
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 fails because heartbeats aren't implemented, but proposers aren't being set, so the big accounts are challenged and kicked offline, and all the stake numbers don't match the test expectations. I could have tried to fix this by ensuring all the test accounts show up as proposers as often as necessary to avoid suspension, but I thought maybe it would be better to see after heartbeats were implemented whether that would make the tests pass without as much modification.