Skip to content
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: handle round 0 "top voters" lookups for easier testing #6159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions ledger/eval_simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,53 @@ func TestAbsenteeChallenges(t *testing.T) {
})
}

func TestDoubleLedgerGetKnockoffCandidates(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

const onlineCount = 5
genBalances, _, _ := ledgertesting.NewTestGenesis(func(cfg *ledgertesting.GenesisCfg) {
cfg.OnlineCount = onlineCount
})
payoutsBegin := 40

checkAccts := func(l *Ledger, rnd basics.Round, cv protocol.ConsensusVersion) {
accts, err := l.GetKnockOfflineCandidates(rnd, config.Consensus[cv])
require.NoError(t, err)
require.NotEmpty(t, accts)
// get online genesis accounts
onlineCnt := 0
onlineAddrs := make(map[basics.Address]basics.OnlineAccountData)
for addr, ad := range genBalances.Balances {
if ad.Status == basics.Online {
onlineCnt++
onlineAddrs[addr] = ad.OnlineAccountData()
}
}
require.Equal(t, onlineCount, onlineCnt)
require.Len(t, accts, onlineCnt)
require.Equal(t, onlineAddrs, accts)

}

ledgertesting.TestConsensusRange(t, payoutsBegin-1, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
dl := NewDoubleLedger(t, genBalances, cv, cfg)
defer dl.Close()

checkAccts(dl.generator, basics.Round(0), cv)
checkAccts(dl.validator, basics.Round(0), cv)

// run up to round 240
proto := config.Consensus[cv]
upToRound := basics.Round(proto.StateProofInterval - proto.StateProofVotersLookback)
require.Equal(t, basics.Round(240), upToRound)
for rnd := dl.fullBlock().Block().Round(); rnd < upToRound; rnd = dl.fullBlock().Block().Round() {
checkAccts(dl.generator, rnd, cv)
checkAccts(dl.validator, rnd, cv)
}
})
}

// TestVoterAccess ensures that the `voter` opcode works properly when hooked up
// to a real ledger.
func TestVoterAccess(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ func (l *Ledger) GetKnockOfflineCandidates(rnd basics.Round, proto config.Consen
if proto.StateProofInterval == 0 {
return nil, nil
}

// special handling for rounds 0-240: return participating genesis accounts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if rnd < basics.Round(proto.StateProofInterval).SubSaturate(basics.Round(proto.StateProofVotersLookback)) {
ret := make(map[basics.Address]basics.OnlineAccountData)
for addr, data := range l.genesisAccounts {
if data.Status == basics.Online {
ret[addr] = data.OnlineAccountData()
}
}
return ret, 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
Expand Down
29 changes: 29 additions & 0 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,35 @@ func TestLookupAgreement(t *testing.T) {
require.Equal(t, oad, ad.OnlineAccountData())
}

func TestGetKnockOfflineCandidates(t *testing.T) {
partitiontest.PartitionTest(t)

ver := protocol.ConsensusFuture
genesisInitState, _ := ledgertesting.GenerateInitState(t, ver, 1_000_000)
const inMem = true
log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()
cfg.Archival = true
ledger, err := OpenLedger(log, t.Name(), inMem, genesisInitState, cfg)
require.NoError(t, err, "could not open ledger")
defer ledger.Close()

accts, err := ledger.GetKnockOfflineCandidates(0, config.Consensus[ver])
require.NoError(t, err)
require.NotEmpty(t, accts)
// get online genesis accounts
onlineCnt := 0
onlineAddrs := make(map[basics.Address]basics.OnlineAccountData)
for addr, ad := range genesisInitState.Accounts {
if ad.Status == basics.Online {
onlineCnt++
onlineAddrs[addr] = ad.OnlineAccountData()
}
}
require.Len(t, accts, onlineCnt)
require.Equal(t, onlineAddrs, accts)
}

func BenchmarkLedgerStartup(b *testing.B) {
log := logging.TestingLog(b)
tmpDir := b.TempDir()
Expand Down
Loading