Skip to content

Commit 3792420

Browse files
tsachihermancce
authored andcommitted
ledger: remove duplicate testing of accounts totals (algorand#3053)
## Summary This PR eliminate the `totalsNewRounds`, which perform the exact same calculation previously performed by the block evaluator. ## Test Plan - [x] Unit tests updates. - [x] Catchpoint label testing : starting a node catching up to 16,800,000 and the correct catchpoint label was observed on round 16,810,000.
1 parent a97bf43 commit 3792420

File tree

5 files changed

+45
-58
lines changed

5 files changed

+45
-58
lines changed

ledger/accountdb.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,52 +1185,6 @@ func accountsNewRound(tx *sql.Tx, updates compactAccountDeltas, creatables map[b
11851185
return
11861186
}
11871187

1188-
// totalsNewRounds updates the accountsTotals by applying series of round changes
1189-
func totalsNewRounds(tx *sql.Tx, updates []ledgercore.AccountDeltas, compactUpdates compactAccountDeltas, accountTotals []ledgercore.AccountTotals, proto config.ConsensusParams) (err error) {
1190-
var ot basics.OverflowTracker
1191-
totals, err := accountsTotals(tx, false)
1192-
if err != nil {
1193-
return
1194-
}
1195-
1196-
// copy the updates base account map, since we don't want to modify the input map.
1197-
accounts := make(map[basics.Address]basics.AccountData, compactUpdates.len())
1198-
for i := 0; i < compactUpdates.len(); i++ {
1199-
addr, acctData := compactUpdates.getByIdx(i)
1200-
accounts[addr] = acctData.old.accountData
1201-
}
1202-
1203-
for i := 0; i < len(updates); i++ {
1204-
totals.ApplyRewards(accountTotals[i].RewardsLevel, &ot)
1205-
1206-
for j := 0; j < updates[i].Len(); j++ {
1207-
addr, data := updates[i].GetByIdx(j)
1208-
1209-
if oldAccountData, has := accounts[addr]; has {
1210-
totals.DelAccount(proto, oldAccountData, &ot)
1211-
} else {
1212-
err = fmt.Errorf("missing old account data")
1213-
return
1214-
}
1215-
1216-
totals.AddAccount(proto, data, &ot)
1217-
accounts[addr] = data
1218-
}
1219-
}
1220-
1221-
if ot.Overflowed {
1222-
err = fmt.Errorf("overflow computing totals")
1223-
return
1224-
}
1225-
1226-
err = accountsPutTotals(tx, totals, false)
1227-
if err != nil {
1228-
return
1229-
}
1230-
1231-
return
1232-
}
1233-
12341188
// updates the round number associated with the current account data.
12351189
func updateAccountsRound(tx *sql.Tx, rnd basics.Round) (err error) {
12361190
res, err := tx.Exec("UPDATE acctrounds SET rnd=? WHERE id='acctbase' AND rnd<?", rnd, rnd)

ledger/accountdb_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func checkAccounts(t *testing.T, tx *sql.Tx, rnd basics.Round, accts map[basics.
8181

8282
totals, err := accountsTotals(tx, false)
8383
require.NoError(t, err)
84-
require.Equal(t, totals.Online.Money.Raw, totalOnline)
84+
require.Equal(t, totals.Online.Money.Raw, totalOnline, "mismatching total online money")
8585
require.Equal(t, totals.Offline.Money.Raw, totalOffline)
8686
require.Equal(t, totals.NotParticipating.Money.Raw, totalNotPart)
8787
require.Equal(t, totals.Participating().Raw, totalOnline+totalOffline)
@@ -243,6 +243,8 @@ func TestAccountDBRound(t *testing.T) {
243243
_, err = accountsInit(tx, accts, proto)
244244
require.NoError(t, err)
245245
checkAccounts(t, tx, 0, accts)
246+
totals, err := accountsTotals(tx, false)
247+
require.NoError(t, err)
246248

247249
// used to determine how many creatables element will be in the test per iteration
248250
numElementsPerSegement := 10
@@ -252,19 +254,20 @@ func TestAccountDBRound(t *testing.T) {
252254
ctbsList, randomCtbs := randomCreatables(numElementsPerSegement)
253255
expectedDbImage := make(map[basics.CreatableIndex]ledgercore.ModifiedCreatable)
254256
var baseAccounts lruAccounts
257+
var newaccts map[basics.Address]basics.AccountData
255258
baseAccounts.init(nil, 100, 80)
256259
for i := 1; i < 10; i++ {
257260
var updates ledgercore.AccountDeltas
258-
var newaccts map[basics.Address]basics.AccountData
259261
updates, newaccts, _, lastCreatableID = ledgertesting.RandomDeltasFull(20, accts, 0, lastCreatableID)
262+
totals = ledgertesting.CalculateNewRoundAccountTotals(t, updates, 0, proto, accts, totals)
260263
accts = newaccts
261264
ctbsWithDeletes := randomCreatableSampling(i, ctbsList, randomCtbs,
262265
expectedDbImage, numElementsPerSegement)
263266

264267
updatesCnt := makeCompactAccountDeltas([]ledgercore.AccountDeltas{updates}, baseAccounts)
265268
err = updatesCnt.accountsLoadOld(tx)
266269
require.NoError(t, err)
267-
err = totalsNewRounds(tx, []ledgercore.AccountDeltas{updates}, updatesCnt, []ledgercore.AccountTotals{{}}, proto)
270+
err = accountsPutTotals(tx, totals, false)
268271
require.NoError(t, err)
269272
_, err = accountsNewRound(tx, updatesCnt, ctbsWithDeletes, proto, basics.Round(i))
270273
require.NoError(t, err)
@@ -273,6 +276,17 @@ func TestAccountDBRound(t *testing.T) {
273276
checkAccounts(t, tx, basics.Round(i), accts)
274277
checkCreatables(t, tx, i, expectedDbImage)
275278
}
279+
280+
// test the accounts totals
281+
var updates ledgercore.AccountDeltas
282+
for addr, acctData := range newaccts {
283+
updates.Upsert(addr, acctData)
284+
}
285+
286+
expectedTotals := ledgertesting.CalculateNewRoundAccountTotals(t, updates, 0, proto, nil, ledgercore.AccountTotals{})
287+
actualTotals, err := accountsTotals(tx, false)
288+
require.NoError(t, err)
289+
require.Equal(t, expectedTotals, actualTotals)
276290
}
277291

278292
// checkCreatables compares the expected database image to the actual databse content

ledger/acctupdates.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,10 +1739,9 @@ func (au *accountUpdates) prepareCommit(dcc *deferredCommitContext) error {
17391739
// create a copy of the deltas, round totals and protos for the range we're going to flush.
17401740
dcc.deltas = make([]ledgercore.AccountDeltas, offset)
17411741
creatableDeltas := make([]map[basics.CreatableIndex]ledgercore.ModifiedCreatable, offset)
1742-
dcc.roundTotals = make([]ledgercore.AccountTotals, offset+1)
1742+
dcc.roundTotals = au.roundTotals[offset]
17431743
copy(dcc.deltas, au.deltas[:offset])
17441744
copy(creatableDeltas, au.creatableDeltas[:offset])
1745-
copy(dcc.roundTotals, au.roundTotals[:offset+1])
17461745

17471746
// verify version correctness : all the entries in the au.versions[1:offset+1] should have the *same* version, and the committedUpTo should be enforcing that.
17481747
if au.versions[1] != au.versions[offset] {
@@ -1757,7 +1756,6 @@ func (au *accountUpdates) prepareCommit(dcc *deferredCommitContext) error {
17571756
}
17581757
return fmt.Errorf("attempted to commit series of rounds with non-uniform consensus versions")
17591758
}
1760-
dcc.roundConsensusVersion = au.versions[1]
17611759

17621760
if dcc.isCatchpointRound {
17631761
dcc.committedRoundDigest = au.roundDigest[offset+uint64(lookback)-1]
@@ -1833,7 +1831,7 @@ func (au *accountUpdates) commitRound(ctx context.Context, tx *sql.Tx, dcc *defe
18331831
dcc.stats.OldAccountPreloadDuration = time.Duration(time.Now().UnixNano()) - dcc.stats.OldAccountPreloadDuration
18341832
}
18351833

1836-
err = totalsNewRounds(tx, dcc.deltas[:offset], dcc.compactAccountDeltas, dcc.roundTotals[1:offset+1], config.Consensus[dcc.roundConsensusVersion])
1834+
err = accountsPutTotals(tx, dcc.roundTotals, false)
18371835
if err != nil {
18381836
return err
18391837
}
@@ -1893,7 +1891,7 @@ func (au *accountUpdates) postCommit(dcc deferredCommitContext) {
18931891
var catchpointLabel string
18941892
var err error
18951893
if dcc.isCatchpointRound {
1896-
catchpointLabel, err = au.accountsCreateCatchpointLabel(dbRound+basics.Round(offset)+lookback, dcc.roundTotals[offset], dcc.committedRoundDigest, dcc.trieBalancesHash)
1894+
catchpointLabel, err = au.accountsCreateCatchpointLabel(dbRound+basics.Round(offset)+lookback, dcc.roundTotals, dcc.committedRoundDigest, dcc.trieBalancesHash)
18971895
if err != nil {
18981896
au.log.Warnf("commitRound : unable to create a catchpoint label: %v", err)
18991897
}

ledger/acctupdates_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ func TestAcctUpdates(t *testing.T) {
402402
// lastCreatableID stores asset or app max used index to get rid of conflicts
403403
lastCreatableID := crypto.RandUint64() % 512
404404
knownCreatables := make(map[basics.CreatableIndex]bool)
405+
405406
for i := basics.Round(10); i < basics.Round(proto.MaxBalLookback+15); i++ {
406407
rewardLevelDelta := crypto.RandUint64() % 5
407408
rewardLevel += rewardLevelDelta
@@ -444,6 +445,28 @@ func TestAcctUpdates(t *testing.T) {
444445
ml.waitAccountsWriting()
445446
checkAcctUpdates(t, au, i, basics.Round(proto.MaxBalLookback+14), accts, rewardsLevels, proto)
446447
}
448+
449+
// check the account totals.
450+
var dbRound basics.Round
451+
err = ml.dbs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) (err error) {
452+
dbRound, err = accountsRound(tx)
453+
return
454+
})
455+
require.NoError(t, err)
456+
457+
var updates ledgercore.AccountDeltas
458+
for addr, acctData := range accts[dbRound] {
459+
updates.Upsert(addr, acctData)
460+
}
461+
462+
expectedTotals := ledgertesting.CalculateNewRoundAccountTotals(t, updates, rewardsLevels[dbRound], proto, nil, ledgercore.AccountTotals{})
463+
var actualTotals ledgercore.AccountTotals
464+
err = ml.dbs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) (err error) {
465+
actualTotals, err = accountsTotals(tx, false)
466+
return
467+
})
468+
require.NoError(t, err)
469+
require.Equal(t, expectedTotals, actualTotals)
447470
}
448471

449472
func TestAcctUpdatesFastUpdates(t *testing.T) {

ledger/tracker.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/algorand/go-algorand/ledger/ledgercore"
3333
"github.com/algorand/go-algorand/logging"
3434
"github.com/algorand/go-algorand/logging/telemetryspec"
35-
"github.com/algorand/go-algorand/protocol"
3635
"github.com/algorand/go-algorand/util/db"
3736
"github.com/algorand/go-deadlock"
3837
)
@@ -156,11 +155,10 @@ type deferredCommitContext struct {
156155
lookback basics.Round
157156
flushTime time.Time
158157

159-
genesisProto config.ConsensusParams
160-
roundConsensusVersion protocol.ConsensusVersion
158+
genesisProto config.ConsensusParams
161159

162160
deltas []ledgercore.AccountDeltas
163-
roundTotals []ledgercore.AccountTotals
161+
roundTotals ledgercore.AccountTotals
164162
compactAccountDeltas compactAccountDeltas
165163
compactCreatableDeltas map[basics.CreatableIndex]ledgercore.ModifiedCreatable
166164

0 commit comments

Comments
 (0)