Skip to content

Commit

Permalink
Dead code as far as the eye can see.
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti committed Jun 21, 2023
1 parent 49b4704 commit f067653
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 362 deletions.
95 changes: 0 additions & 95 deletions ledger/acctupdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

"github.com/algorand/go-deadlock"
"golang.org/x/exp/slices"

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
Expand Down Expand Up @@ -587,100 +586,6 @@ func (au *accountUpdates) LookupWithoutRewards(rnd basics.Round, addr basics.Add
return
}

// ListAssets lists the assets by their asset index, limiting to the first maxResults
func (au *accountUpdates) ListAssets(maxAssetIdx basics.AssetIndex, maxResults uint64) ([]basics.CreatableLocator, error) {
return au.listCreatables(basics.CreatableIndex(maxAssetIdx), maxResults, basics.AssetCreatable)
}

// ListApplications lists the application by their app index, limiting to the first maxResults
func (au *accountUpdates) ListApplications(maxAppIdx basics.AppIndex, maxResults uint64) ([]basics.CreatableLocator, error) {
return au.listCreatables(basics.CreatableIndex(maxAppIdx), maxResults, basics.AppCreatable)
}

// listCreatables lists the application/asset by their app/asset index, limiting to the first maxResults
func (au *accountUpdates) listCreatables(maxCreatableIdx basics.CreatableIndex, maxResults uint64, ctype basics.CreatableType) ([]basics.CreatableLocator, error) {
au.accountsMu.RLock()
for {
currentDbRound := au.cachedDBRound
currentDeltaLen := len(au.deltas)
// Sort indices for creatables that have been created/deleted. If this
// turns out to be too inefficient, we could keep around a heap of
// created/deleted asset indices in memory.
keys := make([]basics.CreatableIndex, 0, len(au.creatables))
for cidx, delta := range au.creatables {
if delta.Ctype != ctype {
continue
}
if cidx <= maxCreatableIdx {
keys = append(keys, cidx)
}
}
slices.Sort(keys)

// Check for creatables that haven't been synced to disk yet.
unsyncedCreatables := make([]basics.CreatableLocator, 0, len(keys))
deletedCreatables := make(map[basics.CreatableIndex]bool, len(keys))
for _, cidx := range keys {
delta := au.creatables[cidx]
if delta.Created {
// Created but only exists in memory
unsyncedCreatables = append(unsyncedCreatables, basics.CreatableLocator{
Type: delta.Ctype,
Index: cidx,
Creator: delta.Creator,
})
} else {
// Mark deleted creatables for exclusion from the results set
deletedCreatables[cidx] = true
}
}

au.accountsMu.RUnlock()

// Check in-memory created creatables, which will always be newer than anything
// in the database
if uint64(len(unsyncedCreatables)) >= maxResults {
return unsyncedCreatables[:maxResults], nil
}
res := unsyncedCreatables

// Fetch up to maxResults - len(res) + len(deletedCreatables) from the database,
// so we have enough extras in case creatables were deleted
numToFetch := maxResults - uint64(len(res)) + uint64(len(deletedCreatables))
dbResults, dbRound, err := au.accountsq.ListCreatables(maxCreatableIdx, numToFetch, ctype)
if err != nil {
return nil, err
}

if dbRound == currentDbRound {
// Now we merge the database results with the in-memory results
for _, loc := range dbResults {
// Check if we have enough results
if uint64(len(res)) == maxResults {
return res, nil
}

// Creatable was deleted
if _, ok := deletedCreatables[loc.Index]; ok {
continue
}

// We're OK to include this result
res = append(res, loc)
}
return res, nil
}
if dbRound < currentDbRound {
au.log.Errorf("listCreatables: database round %d is behind in-memory round %d", dbRound, currentDbRound)
return []basics.CreatableLocator{}, &StaleDatabaseRoundError{databaseRound: dbRound, memoryRound: currentDbRound}
}
au.accountsMu.RLock()
for currentDbRound >= au.cachedDBRound && currentDeltaLen == len(au.deltas) {
au.accountsReadCond.Wait()
}
}
}

// GetCreatorForRound returns the creator for a given asset/app index at a given round
func (au *accountUpdates) GetCreatorForRound(rnd basics.Round, cidx basics.CreatableIndex, ctype basics.CreatableType) (creator basics.Address, ok bool, err error) {
return au.getCreatorForRound(rnd, cidx, ctype, true /* take the lock */)
Expand Down
184 changes: 0 additions & 184 deletions ledger/acctupdates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,190 +913,6 @@ func testAcctUpdatesUpdatesCorrectness(t *testing.T, cfg config.Local) {
t.Run("DiskDB", testFunction)
}

// listAndCompareComb lists the assets/applications and then compares against the expected
// It repeats with different combinations of the limit parameters
func listAndCompareComb(t *testing.T, au *accountUpdates, expected map[basics.CreatableIndex]ledgercore.ModifiedCreatable) {

// test configuration parameters

// pick the second largest index for the app and asset
// This is to make sure exactly one element is left out
// as a result of max index
maxAss1 := basics.CreatableIndex(0)
maxAss2 := basics.CreatableIndex(0)
maxApp1 := basics.CreatableIndex(0)
maxApp2 := basics.CreatableIndex(0)
for a, b := range expected {
// A moving window of the last two largest indexes: [maxAss1, maxAss2]
if b.Ctype == basics.AssetCreatable {
if maxAss2 < a {
maxAss1 = maxAss2
maxAss2 = a
} else if maxAss1 < a {
maxAss1 = a
}
}
if b.Ctype == basics.AppCreatable {
if maxApp2 < a {
maxApp1 = maxApp2
maxApp2 = a
} else if maxApp1 < a {
maxApp1 = a
}
}
}

// No limits. max asset index, max app index and max results have no effect
// This is to make sure the deleted elements do not show up
maxAssetIdx := basics.AssetIndex(maxAss2)
maxAppIdx := basics.AppIndex(maxApp2)
maxResults := uint64(len(expected))
listAndCompare(t, maxAssetIdx, maxAppIdx, maxResults, au, expected)

// Limit with max asset index and max app index (max results has no effect)
maxAssetIdx = basics.AssetIndex(maxAss1)
maxAppIdx = basics.AppIndex(maxApp1)
maxResults = uint64(len(expected))
listAndCompare(t, maxAssetIdx, maxAppIdx, maxResults, au, expected)

// Limit with max results
maxResults = 1
listAndCompare(t, maxAssetIdx, maxAppIdx, maxResults, au, expected)
}

// listAndCompareComb lists the assets/applications and then compares against the expected
// It uses the provided limit parameters
func listAndCompare(t *testing.T,
maxAssetIdx basics.AssetIndex,
maxAppIdx basics.AppIndex,
maxResults uint64,
au *accountUpdates,
expected map[basics.CreatableIndex]ledgercore.ModifiedCreatable) {

// get the results with the given parameters
assetRes, err := au.ListAssets(maxAssetIdx, maxResults)
require.NoError(t, err)
appRes, err := au.ListApplications(maxAppIdx, maxResults)
require.NoError(t, err)

// count the expected number of results
expectedAssetCount := uint64(0)
expectedAppCount := uint64(0)
for a, b := range expected {
if b.Created {
if b.Ctype == basics.AssetCreatable &&
a <= basics.CreatableIndex(maxAssetIdx) &&
expectedAssetCount < maxResults {
expectedAssetCount++
}
if b.Ctype == basics.AppCreatable &&
a <= basics.CreatableIndex(maxAppIdx) &&
expectedAppCount < maxResults {
expectedAppCount++
}
}
}

// check the total counts are as expected
require.Equal(t, int(expectedAssetCount), len(assetRes))
require.Equal(t, int(expectedAppCount), len(appRes))

// verify the results are correct
for _, respCrtor := range assetRes {
crtor := expected[respCrtor.Index]
require.NotNil(t, crtor)
require.Equal(t, basics.AssetCreatable, crtor.Ctype)
require.Equal(t, true, crtor.Created)

require.Equal(t, basics.AssetCreatable, respCrtor.Type)
require.Equal(t, crtor.Creator, respCrtor.Creator)
}
for _, respCrtor := range appRes {
crtor := expected[respCrtor.Index]
require.NotNil(t, crtor)
require.Equal(t, basics.AppCreatable, crtor.Ctype)
require.Equal(t, true, crtor.Created)

require.Equal(t, basics.AppCreatable, respCrtor.Type)
require.Equal(t, crtor.Creator, respCrtor.Creator)
}
}

// TestListCreatables tests ListAssets and ListApplications
// It tests with all elements in cache, all synced to database, and combination of both
// It also tests the max results, max app index and max asset index
func TestListCreatables(t *testing.T) {
partitiontest.PartitionTest(t)

// test configuration parameters
numElementsPerSegement := 25

// set up the database
dbs, _ := sqlitedriver.OpenForTesting(t, true)
defer dbs.Close()

proto := config.Consensus[protocol.ConsensusCurrentVersion]

err := dbs.Transaction(func(ctx context.Context, tx trackerdb.TransactionScope) (err error) {
accts := make(map[basics.Address]basics.AccountData)
_ = tx.Testing().AccountsInitTest(t, accts, protocol.ConsensusCurrentVersion)
return
})
require.NoError(t, err)

au := &accountUpdates{}
au.accountsq, err = dbs.MakeAccountsOptimizedReader()
require.NoError(t, err)

// ******* All results are obtained from the cache. Empty database *******
// ******* No deletes *******
// get random data. Initial batch, no deletes
ctbsList, randomCtbs := randomCreatables(numElementsPerSegement)
expectedDbImage := make(map[basics.CreatableIndex]ledgercore.ModifiedCreatable)
ctbsWithDeletes := randomCreatableSampling(1, ctbsList, randomCtbs,
expectedDbImage, numElementsPerSegement)
// set the cache
au.creatables = ctbsWithDeletes
listAndCompareComb(t, au, expectedDbImage)

// ******* All results are obtained from the database. Empty cache *******
// ******* No deletes *******
// sync with the database
var updates compactAccountDeltas
var resUpdates compactResourcesDeltas
err = dbs.Transaction(func(ctx context.Context, tx trackerdb.TransactionScope) (err error) {
_, _, _, err = accountsNewRound(tx, updates, resUpdates, nil, ctbsWithDeletes, proto, basics.Round(1))
require.NoError(t, err)
return
})
require.NoError(t, err)

// nothing left in cache
au.creatables = make(map[basics.CreatableIndex]ledgercore.ModifiedCreatable)
listAndCompareComb(t, au, expectedDbImage)

// ******* Results are obtained from the database and from the cache *******
// ******* No deletes in the database. *******
// ******* Data in the database deleted in the cache *******
au.creatables = randomCreatableSampling(2, ctbsList, randomCtbs,
expectedDbImage, numElementsPerSegement)
listAndCompareComb(t, au, expectedDbImage)

// ******* Results are obtained from the database and from the cache *******
// ******* Deletes are in the database and in the cache *******
// sync with the database. This has deletes synced to the database.
err = dbs.Transaction(func(ctx context.Context, tx trackerdb.TransactionScope) (err error) {
_, _, _, err = accountsNewRound(tx, updates, resUpdates, nil, au.creatables, proto, basics.Round(1))
require.NoError(t, err)
return
})
require.NoError(t, err)
// get new creatables in the cache. There will be deleted in the cache from the previous batch.
au.creatables = randomCreatableSampling(3, ctbsList, randomCtbs,
expectedDbImage, numElementsPerSegement)
listAndCompareComb(t, au, expectedDbImage)
}

func TestBoxNamesByAppIDs(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()
Expand Down
18 changes: 0 additions & 18 deletions ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,24 +502,6 @@ func (l *Ledger) GetStateProofVerificationContext(stateProofLastAttestedRound ba
return l.spVerification.LookupVerificationContext(stateProofLastAttestedRound)
}

// ListAssets takes a maximum asset index and maximum result length, and
// returns up to that many CreatableLocators from the database where app idx is
// less than or equal to the maximum.
func (l *Ledger) ListAssets(maxAssetIdx basics.AssetIndex, maxResults uint64) (results []basics.CreatableLocator, err error) {
l.trackerMu.RLock()
defer l.trackerMu.RUnlock()
return l.accts.ListAssets(maxAssetIdx, maxResults)
}

// ListApplications takes a maximum app index and maximum result length, and
// returns up to that many CreatableLocators from the database where app idx is
// less than or equal to the maximum.
func (l *Ledger) ListApplications(maxAppIdx basics.AppIndex, maxResults uint64) (results []basics.CreatableLocator, err error) {
l.trackerMu.RLock()
defer l.trackerMu.RUnlock()
return l.accts.ListApplications(maxAppIdx, maxResults)
}

// LookupLatest uses the accounts tracker to return the account state (including
// resources) for a given address, for the latest round. The returned account values
// reflect the changes of all blocks up to and including the returned round number.
Expand Down
65 changes: 0 additions & 65 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1623,71 +1623,6 @@ func generateCreatables(numElementsPerSegement int) (
return
}

// TestListAssetsAndApplications tests the ledger.ListAssets and ledger.ListApplications
// interfaces. The detailed test on the correctness of these functions is given in:
// TestListCreatables (acctupdates_test.go)
func TestListAssetsAndApplications(t *testing.T) {
partitiontest.PartitionTest(t)

numElementsPerSegement := 10 // This is multiplied by 10. see randomCreatables

//initLedger
genesisInitState, _ := ledgertesting.GenerateInitState(t, protocol.ConsensusCurrentVersion, 100)
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()

// ******* All results are obtained from the cache. Empty database *******
// ******* No deletes *******
// get random data. Initial batch, no deletes
randomCtbs, maxAsset, maxApp, err := generateCreatables(numElementsPerSegement)
require.NoError(t, err)

// set the cache
ledger.accts.creatables = randomCtbs

// Test ListAssets
// Check the number of results limit
results, err := ledger.ListAssets(basics.AssetIndex(maxAsset), 2)
require.NoError(t, err)
require.Equal(t, 2, len(results))
// Check the max asset id limit
results, err = ledger.ListAssets(basics.AssetIndex(maxAsset), 100)
require.NoError(t, err)
assetCount := 0
for id, ctb := range randomCtbs {
if ctb.Ctype == basics.AssetCreatable &&
ctb.Created &&
id <= maxAsset {
assetCount++
}
}
require.Equal(t, assetCount, len(results))

// Test ListApplications
// Check the number of results limit
ledger.accts.creatables = randomCtbs
results, err = ledger.ListApplications(basics.AppIndex(maxApp), 2)
require.NoError(t, err)
require.Equal(t, 2, len(results))
// Check the max application id limit
results, err = ledger.ListApplications(basics.AppIndex(maxApp), 100)
require.NoError(t, err)
appCount := 0
for id, ctb := range randomCtbs {
if ctb.Ctype == basics.AppCreatable &&
ctb.Created &&
id <= maxApp {
appCount++
}
}
require.Equal(t, appCount, len(results))
}

// TestLedgerVerifiesOldStateProofs test that if stateproof chain is delayed for X intervals (pass StateProofMaxRecoveryIntervals),
// The ledger will still be able to verify the state proof - i.e the ledger has the necessary data to verify it.
func TestLedgerVerifiesOldStateProofs(t *testing.T) {
Expand Down

0 comments on commit f067653

Please sign in to comment.