Skip to content

Commit

Permalink
Fix caching again!
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Nov 1, 2023
1 parent da56755 commit 82cfaad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
7 changes: 4 additions & 3 deletions accountresolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *KVDBAccountsResolver) Resolve(ctx context.Context, atBlockNum uint64, k
_, keyBlockNum := Keys.UnpackTableLookup(item.Key)
accounts := DecodeAccounts(item.Value)

r.pushToCache(atBlockNum, key.Base58(), resolvedAccounts)
r.pushToCache(keyBlockNum, key.Base58(), accounts)

if keyBlockNum < atBlockNum && resolvedAccounts == nil {
resolvedAccounts = accounts
Expand Down Expand Up @@ -177,8 +177,9 @@ func (r *KVDBAccountsResolver) pushToCache(blockNum uint64, key string, accounts
if cacheItems, found := r.cache[key]; found {
for _, ci := range cacheItems {
if ci.blockNum == blockNum {
ci.accounts = append(ci.accounts, accounts...)
return
continue
//ci.accounts = append(ci.accounts, accounts...)
//return
}
}
}
Expand Down
50 changes: 36 additions & 14 deletions accountresolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,6 @@ func Test_Extend_Multiple_Accounts_Same_Block(t *testing.T) {
)
require.NoError(t, err)

err = resolver.CommitBlock(context.Background(), 1)
require.NoError(t, err)

accounts, _, err := resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, 0, len(accounts))

accounts, _, err = resolver.Resolve(context.Background(), 2, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, 2, len(accounts))
require.Equal(t, accountFromBase58(t, a2), accounts[0])
require.Equal(t, accountFromBase58(t, a3), accounts[1])

err = resolver.Extend(
accountFromBase58(t, a1),
[]Account{
Expand All @@ -141,7 +128,7 @@ func Test_Extend_Multiple_Accounts_Same_Block(t *testing.T) {
err = resolver.CommitBlock(context.Background(), 1)
require.NoError(t, err)

accounts, _, err = resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
accounts, _, err := resolver.Resolve(context.Background(), 1, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, 0, len(accounts))

Expand Down Expand Up @@ -265,3 +252,38 @@ func Test_Create_Extend_TableLookupAccount_SameTransaction_Delete_Other_Block(t
require.NoError(t, err)
require.Equal(t, Accounts(nil), accounts)
}

func Test_Multiple_Cache_Call(t *testing.T) {
err := os.RemoveAll("/tmp/my-badger.db")
require.NoError(t, err)

db, err := store.New("badger3:///tmp/my-badger.db")
require.NoError(t, err)

resolver := NewKVDBAccountsResolver(db, zap.NewNop())
err = resolver.Extend(
accountFromBase58(t, a1),
[]Account{
accountFromBase58(t, a2),
})
require.NoError(t, err)

err = resolver.CommitBlock(context.Background(), 1)
require.NoError(t, err)

//flush the cache
resolver.cache = make(map[string][]*cacheItem)

accounts, cached, err := resolver.Resolve(context.Background(), 2, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, false, cached)
require.Equal(t, 1, len(accounts))
require.Equal(t, accountFromBase58(t, a2), accounts[0])

accounts, cached, err = resolver.Resolve(context.Background(), 2, accountFromBase58(t, a1))
require.NoError(t, err)
require.Equal(t, true, cached)
require.Equal(t, 1, len(accounts))
require.Equal(t, accountFromBase58(t, a2), accounts[0])

}

0 comments on commit 82cfaad

Please sign in to comment.