Skip to content
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
46 changes: 0 additions & 46 deletions ledger/accountdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,52 +1185,6 @@ func accountsNewRound(tx *sql.Tx, updates compactAccountDeltas, creatables map[b
return
}

// totalsNewRounds updates the accountsTotals by applying series of round changes
func totalsNewRounds(tx *sql.Tx, updates []ledgercore.AccountDeltas, compactUpdates compactAccountDeltas, accountTotals []ledgercore.AccountTotals, proto config.ConsensusParams) (err error) {
var ot basics.OverflowTracker
totals, err := accountsTotals(tx, false)
if err != nil {
return
}

// copy the updates base account map, since we don't want to modify the input map.
accounts := make(map[basics.Address]basics.AccountData, compactUpdates.len())
for i := 0; i < compactUpdates.len(); i++ {
addr, acctData := compactUpdates.getByIdx(i)
accounts[addr] = acctData.old.accountData
}

for i := 0; i < len(updates); i++ {
totals.ApplyRewards(accountTotals[i].RewardsLevel, &ot)

for j := 0; j < updates[i].Len(); j++ {
addr, data := updates[i].GetByIdx(j)

if oldAccountData, has := accounts[addr]; has {
totals.DelAccount(proto, oldAccountData, &ot)
} else {
err = fmt.Errorf("missing old account data")
return
}

totals.AddAccount(proto, data, &ot)
accounts[addr] = data
}
}

if ot.Overflowed {
err = fmt.Errorf("overflow computing totals")
return
}

err = accountsPutTotals(tx, totals, false)
if err != nil {
return
}

return
}

// updates the round number associated with the current account data.
func updateAccountsRound(tx *sql.Tx, rnd basics.Round) (err error) {
res, err := tx.Exec("UPDATE acctrounds SET rnd=? WHERE id='acctbase' AND rnd<?", rnd, rnd)
Expand Down
20 changes: 17 additions & 3 deletions ledger/accountdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func checkAccounts(t *testing.T, tx *sql.Tx, rnd basics.Round, accts map[basics.

totals, err := accountsTotals(tx, false)
require.NoError(t, err)
require.Equal(t, totals.Online.Money.Raw, totalOnline)
require.Equal(t, totals.Online.Money.Raw, totalOnline, "mismatching total online money")
require.Equal(t, totals.Offline.Money.Raw, totalOffline)
require.Equal(t, totals.NotParticipating.Money.Raw, totalNotPart)
require.Equal(t, totals.Participating().Raw, totalOnline+totalOffline)
Expand Down Expand Up @@ -243,6 +243,8 @@ func TestAccountDBRound(t *testing.T) {
_, err = accountsInit(tx, accts, proto)
require.NoError(t, err)
checkAccounts(t, tx, 0, accts)
totals, err := accountsTotals(tx, false)
require.NoError(t, err)

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

updatesCnt := makeCompactAccountDeltas([]ledgercore.AccountDeltas{updates}, baseAccounts)
err = updatesCnt.accountsLoadOld(tx)
require.NoError(t, err)
err = totalsNewRounds(tx, []ledgercore.AccountDeltas{updates}, updatesCnt, []ledgercore.AccountTotals{{}}, proto)
err = accountsPutTotals(tx, totals, false)
require.NoError(t, err)
_, err = accountsNewRound(tx, updatesCnt, ctbsWithDeletes, proto, basics.Round(i))
require.NoError(t, err)
Expand All @@ -273,6 +276,17 @@ func TestAccountDBRound(t *testing.T) {
checkAccounts(t, tx, basics.Round(i), accts)
checkCreatables(t, tx, i, expectedDbImage)
}

// test the accounts totals
var updates ledgercore.AccountDeltas
for addr, acctData := range newaccts {
updates.Upsert(addr, acctData)
}

expectedTotals := ledgertesting.CalculateNewRoundAccountTotals(t, updates, 0, proto, nil, ledgercore.AccountTotals{})
actualTotals, err := accountsTotals(tx, false)
require.NoError(t, err)
require.Equal(t, expectedTotals, actualTotals)
}

// checkCreatables compares the expected database image to the actual databse content
Expand Down
8 changes: 3 additions & 5 deletions ledger/acctupdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -1739,10 +1739,9 @@ func (au *accountUpdates) prepareCommit(dcc *deferredCommitContext) error {
// create a copy of the deltas, round totals and protos for the range we're going to flush.
dcc.deltas = make([]ledgercore.AccountDeltas, offset)
creatableDeltas := make([]map[basics.CreatableIndex]ledgercore.ModifiedCreatable, offset)
dcc.roundTotals = make([]ledgercore.AccountTotals, offset+1)
dcc.roundTotals = au.roundTotals[offset]
copy(dcc.deltas, au.deltas[:offset])
copy(creatableDeltas, au.creatableDeltas[:offset])
copy(dcc.roundTotals, au.roundTotals[:offset+1])

// 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.
if au.versions[1] != au.versions[offset] {
Expand All @@ -1757,7 +1756,6 @@ func (au *accountUpdates) prepareCommit(dcc *deferredCommitContext) error {
}
return fmt.Errorf("attempted to commit series of rounds with non-uniform consensus versions")
}
dcc.roundConsensusVersion = au.versions[1]

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

err = totalsNewRounds(tx, dcc.deltas[:offset], dcc.compactAccountDeltas, dcc.roundTotals[1:offset+1], config.Consensus[dcc.roundConsensusVersion])
err = accountsPutTotals(tx, dcc.roundTotals, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -1893,7 +1891,7 @@ func (au *accountUpdates) postCommit(dcc deferredCommitContext) {
var catchpointLabel string
var err error
if dcc.isCatchpointRound {
catchpointLabel, err = au.accountsCreateCatchpointLabel(dbRound+basics.Round(offset)+lookback, dcc.roundTotals[offset], dcc.committedRoundDigest, dcc.trieBalancesHash)
catchpointLabel, err = au.accountsCreateCatchpointLabel(dbRound+basics.Round(offset)+lookback, dcc.roundTotals, dcc.committedRoundDigest, dcc.trieBalancesHash)
if err != nil {
au.log.Warnf("commitRound : unable to create a catchpoint label: %v", err)
}
Expand Down
23 changes: 23 additions & 0 deletions ledger/acctupdates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func TestAcctUpdates(t *testing.T) {
// lastCreatableID stores asset or app max used index to get rid of conflicts
lastCreatableID := crypto.RandUint64() % 512
knownCreatables := make(map[basics.CreatableIndex]bool)

for i := basics.Round(10); i < basics.Round(proto.MaxBalLookback+15); i++ {
rewardLevelDelta := crypto.RandUint64() % 5
rewardLevel += rewardLevelDelta
Expand Down Expand Up @@ -444,6 +445,28 @@ func TestAcctUpdates(t *testing.T) {
ml.waitAccountsWriting()
checkAcctUpdates(t, au, i, basics.Round(proto.MaxBalLookback+14), accts, rewardsLevels, proto)
}

// check the account totals.
var dbRound basics.Round
err = ml.dbs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) (err error) {
dbRound, err = accountsRound(tx)
return
})
require.NoError(t, err)

var updates ledgercore.AccountDeltas
for addr, acctData := range accts[dbRound] {
updates.Upsert(addr, acctData)
}

expectedTotals := ledgertesting.CalculateNewRoundAccountTotals(t, updates, rewardsLevels[dbRound], proto, nil, ledgercore.AccountTotals{})
var actualTotals ledgercore.AccountTotals
err = ml.dbs.Rdb.Atomic(func(ctx context.Context, tx *sql.Tx) (err error) {
actualTotals, err = accountsTotals(tx, false)
return
})
require.NoError(t, err)
require.Equal(t, expectedTotals, actualTotals)
}

func TestAcctUpdatesFastUpdates(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions ledger/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/logging/telemetryspec"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/util/db"
"github.com/algorand/go-deadlock"
)
Expand Down Expand Up @@ -156,11 +155,10 @@ type deferredCommitContext struct {
lookback basics.Round
flushTime time.Time

genesisProto config.ConsensusParams
roundConsensusVersion protocol.ConsensusVersion
genesisProto config.ConsensusParams

deltas []ledgercore.AccountDeltas
roundTotals []ledgercore.AccountTotals
roundTotals ledgercore.AccountTotals
compactAccountDeltas compactAccountDeltas
compactCreatableDeltas map[basics.CreatableIndex]ledgercore.ModifiedCreatable

Expand Down