Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
icorderi committed Apr 4, 2023
1 parent 8cd9f82 commit a496f01
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 122 deletions.
4 changes: 2 additions & 2 deletions ledger/store/trackerdb/generickv/accounts_ext_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (r *accountsReader) TotalKVs(ctx context.Context) (total uint64, err error)
func (r *accountsReader) LookupOnlineAccountDataByAddress(addr basics.Address) (ref trackerdb.OnlineAccountRef, data []byte, err error) {
low := onlineAccountOnlyPartialKey(addr)
high := onlineAccountOnlyPartialKey(addr)
high[len(high)-1] += 1
high[len(high)-1]++
iter := r.kvr.NewIter(low, high, true)
defer iter.Close()

Expand Down Expand Up @@ -183,7 +183,7 @@ func (r *accountsReader) AccountsOnlineTop(rnd basics.Round, offset uint64, n ui
// prepare iter over online accounts (by balance)
low := onlineAccountBalanceOnlyPartialKey(rnd)
high := onlineAccountBalanceOnlyPartialKey(rnd)
high[len(high)-1] += 1
high[len(high)-1]++
// reverse order iterator to get high-to-low
iter := r.kvr.NewIter(low, high, true)
defer iter.Close()
Expand Down
9 changes: 6 additions & 3 deletions ledger/store/trackerdb/generickv/accounts_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
"github.com/algorand/go-algorand/protocol"
)

// KvRead is a low level KV db interface for reading.
type KvRead interface {
Get(key []byte) ([]byte, io.Closer, error)
NewIter(low, high []byte, reverse bool) KvIter
}

// KvIter is a low level KV iterator.
type KvIter interface {
Next() bool
Key() []byte
Expand All @@ -43,6 +45,7 @@ type KvIter interface {
Close()
}

// Slice is a low level slice used during the KV iterator.
type Slice interface {
Data() []byte
Free()
Expand Down Expand Up @@ -147,7 +150,7 @@ func (r *accountsReader) LookupResources(addr basics.Address, aidx basics.Creata
func (r *accountsReader) LookupAllResources(addr basics.Address) (data []trackerdb.PersistedResourcesData, rnd basics.Round, err error) {
low := resourceAddrOnlyPartialKey(addr)
high := resourceAddrOnlyPartialKey(addr)
high[len(high)-1] += 1
high[len(high)-1]++

iter := r.kvr.NewIter(low, high, false)
defer iter.Close()
Expand Down Expand Up @@ -267,7 +270,7 @@ func (r *accountsReader) LookupKeysByPrefix(prefix string, maxKeyNum uint64, res
results[key] = len(value) > 0

// inc results in range
resultCount += 1
resultCount++
}

return
Expand Down Expand Up @@ -342,7 +345,7 @@ func (r *accountsReader) ListCreatables(maxIdx basics.CreatableIndex, maxResults
results = append(results, cl)

// inc results in range
resultCount += 1
resultCount++
}

// read the current db round
Expand Down
59 changes: 1 addition & 58 deletions ledger/store/trackerdb/generickv/accounts_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,7 @@ import (
"github.com/algorand/go-algorand/protocol"
)

// schema:
// "account"-<addr>-latest +1 (for all accounts)
// "account"-<addr>-<round> historical (only turned on for online

// ..
// "account"-<addr>-<round> <--- latest less than `rnd` param
// ..

// "account"-<addr>-horizon-320 (320 ago)

// # Only for Online Accounts

// Note: think about hwo to round encode this data..

// true index, for a given round, the top N balance addresses
// "crazy thing"-<round>-<balance>-<addr>
// "crazy thing"-horizon-<size>-<addr> // value: {rnd, balance} // cutoff points
// 2187-$500-chris
// 2188-$1000-chris
// <------ cutoff happened here
// 2199-$5-nacho value: nil
// 2200-$35-chris <--
// 2200-$20-nacho <--
// 2202-$0-chris <-- // offline
// 2249-$10-chris <-- // online
//
// new write, just get added, new balance and the round
// we need to figure out the online/offline/online/offline thing

// # Going Online
// - Write "crazy thing"-<rnd>-<balance>-<addr>
// - you are not really online until the "online" event falls off the horizon
// - we need to write the horizon entry when this happens with the same balance as the online event

// GetRangeReverse(["crazy-horizon-*"])
// .Map(|data| decode(data))
// .Extend(/* this stuff below */
// GetRangeReverse(["crazy-1900-*", "crazy-2200-*"])
// .Map(|data| decode(data))
// ) // closing the extend
// .SortBy(|x| x.balance)
// .DedupBy(|x| x.addr)

// Horizon cleanup (online process)
// - Move the horizon..
// cost here is the # of online accounts that did not change in 319 accounts and where not already in this situation last round.
// - This can be done with a GetRange before the deleting to find the latest of each
// - DeleteRange("account"-<addr>-<round value of xxx - 320>)
// this requires moving the horizon for ppl that fell off
// ""

// # When/how do we cleanup things in the horizon?
// - we can delete it when the latest balance older than the horizon is $0

// Option B (radicall):
// "really crazy"-round -> value: "pick a tree" data
// "really crazy"-round -> value: "pick a tree" data

// KvWrite is a low level KV db interface for writing.
type KvWrite interface {
Set(key, value []byte) error
Delete(key []byte) error
Expand Down
8 changes: 6 additions & 2 deletions ledger/store/trackerdb/generickv/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func RunMigrations(ctx context.Context, db trackerdb.TrackerStore, params tracke
var bad trackerdb.BaseAccountData
bad.SetAccountData(&account)
// insert the account
aow.InsertAccount(addr, account.NormalizedOnlineBalance(proto), bad)
_, err = aow.InsertAccount(addr, account.NormalizedOnlineBalance(proto), bad)
if err != nil {
return err
}

// build a ledgercore.AccountData to track the totals
ad := ledgercore.ToAccountData(account)
// track the totals
Expand Down Expand Up @@ -100,7 +104,7 @@ func RunMigrations(ctx context.Context, db trackerdb.TrackerStore, params tracke

// insert online params
params := []ledgercore.OnlineRoundParamsData{
ledgercore.OnlineRoundParamsData{
{
OnlineSupply: totals.Online.Money.Raw,
RewardsLevel: totals.RewardsLevel,
CurrentProtocol: params.InitProto,
Expand Down
1 change: 1 addition & 0 deletions ledger/store/trackerdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/algorand/go-algorand/ledger/ledgercore"
)

// ErrNotFound is returned when a record is not found.
var ErrNotFound = errors.New("trackerdb: not found")

// AccountRef is an opaque ref to an account in the db.
Expand Down
6 changes: 2 additions & 4 deletions ledger/store/trackerdb/pebbledbdriver/store_pebbledb_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,15 +463,13 @@ func (i *pebbleIter) Next() bool {
i.firstCall = false
if i.reverse {
return i.iter.Last()
} else {
return i.iter.First()
}
return i.iter.First()
}
if i.reverse {
return i.iter.Prev()
} else {
return i.iter.Next()
}
return i.iter.Next()
}
func (i *pebbleIter) Valid() bool { return i.iter.Valid() }
func (i *pebbleIter) Close() { i.iter.Close() }
Expand Down
16 changes: 8 additions & 8 deletions ledger/store/trackerdb/testsuite/accounts_ext_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,31 @@ func CustomTestTotals(t *customT) {
require.NoError(t, err)

// read the totals
read_totals, err := ar.AccountsTotals(context.Background(), false)
readTotals, err := ar.AccountsTotals(context.Background(), false)
require.NoError(t, err)
require.Equal(t, totals, read_totals)
require.Equal(t, totals, readTotals)

// generate some staging values
staging_totals := ledgercore.AccountTotals{
stagingTotals := ledgercore.AccountTotals{
Online: ledgercore.AlgoCount{Money: basics.MicroAlgos{Raw: 1}},
Offline: ledgercore.AlgoCount{Money: basics.MicroAlgos{Raw: 2}},
NotParticipating: ledgercore.AlgoCount{Money: basics.MicroAlgos{Raw: 3}},
RewardsLevel: 4,
}

// update the (staging) totals
err = aw.AccountsPutTotals(staging_totals, true)
err = aw.AccountsPutTotals(stagingTotals, true)
require.NoError(t, err)

// read the totals
read_totals, err = ar.AccountsTotals(context.Background(), true)
readTotals, err = ar.AccountsTotals(context.Background(), true)
require.NoError(t, err)
require.Equal(t, staging_totals, read_totals)
require.Equal(t, stagingTotals, readTotals)

// double check the live data is still there
read_totals, err = ar.AccountsTotals(context.Background(), false)
readTotals, err = ar.AccountsTotals(context.Background(), false)
require.NoError(t, err)
require.Equal(t, totals, read_totals)
require.Equal(t, totals, readTotals)
}

func CustomTestTxTail(t *customT) {
Expand Down
12 changes: 6 additions & 6 deletions ledger/store/trackerdb/testsuite/onlineaccounts_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func CustomTestAccountsOnlineTop(t *customT) {
require.NoError(t, err)

// generate some test data
var test_data []basics.Address
var testData []basics.Address
updRound := uint64(0)
for i := 0; i < 10; i++ {
addr := RandomAddress()
Expand All @@ -135,21 +135,21 @@ func CustomTestAccountsOnlineTop(t *customT) {
_, err := oaw.InsertOnlineAccount(addr, normalizedBal, data, updRound, lastValid)
require.NoError(t, err)

test_data = append(test_data, addr)
testData = append(testData, addr)
}

// read (all)
poA, err := ar.AccountsOnlineTop(basics.Round(0), 0, 10, t.proto)
require.NoError(t, err)
require.Contains(t, poA, test_data[9]) // most money
require.Contains(t, poA, test_data[0]) // least money
require.Contains(t, poA, testData[9]) // most money
require.Contains(t, poA, testData[0]) // least money

// read (just a few)
poA, err = ar.AccountsOnlineTop(basics.Round(0), 1, 2, t.proto)
require.NoError(t, err)
require.Len(t, poA, 2)
require.Contains(t, poA, test_data[8]) // (second most money, we skipped 1)
require.Contains(t, poA, test_data[7]) // (third, we only have 2 items)
require.Contains(t, poA, testData[8]) // (second most money, we skipped 1)
require.Contains(t, poA, testData[7]) // (third, we only have 2 items)
}

func CustomTestLookupOnlineAccountDataByAddress(t *customT) {
Expand Down
Loading

0 comments on commit a496f01

Please sign in to comment.