Skip to content

Commit

Permalink
incentives: handle round 0 "top voters" lookups for easier testing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cce authored Nov 5, 2024
1 parent 28338ff commit 8de2829
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
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
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

0 comments on commit 8de2829

Please sign in to comment.