Skip to content

Commit

Permalink
fix caching when account from kvdb
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Nov 1, 2023
1 parent ac754bf commit a711f16
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
23 changes: 19 additions & 4 deletions accountresolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *KVDBAccountsResolver) CommitBlock(ctx context.Context, blockNum uint64)
return fmt.Errorf("flushing extended accounts for tableKey %q: %w", tableKey, err)
}

r.pushToCache(blockNum, tableKey.Base58(), extendedAccounts)
r.prependToCache(blockNum, tableKey.Base58(), extendedAccounts)
}
r.toCommit = make(map[string][]Account)
return nil
Expand Down Expand Up @@ -91,7 +91,7 @@ func (r *KVDBAccountsResolver) Resolve(ctx context.Context, atBlockNum uint64, k
_, keyBlockNum := Keys.UnpackTableLookup(item.Key)
accounts := DecodeAccounts(item.Value)

r.pushToCache(keyBlockNum, key.Base58(), accounts)
r.appendToCache(keyBlockNum, key.Base58(), accounts)

if keyBlockNum < atBlockNum && resolvedAccounts == nil {
resolvedAccounts = accounts
Expand Down Expand Up @@ -124,7 +124,7 @@ func (r *KVDBAccountsResolver) ResolveWithBlock(ctx context.Context, atBlockNum
_, keyBlockNum = Keys.UnpackTableLookup(item.Key)
accounts := DecodeAccounts(item.Value)

r.pushToCache(keyBlockNum, key.Base58(), accounts)
r.appendToCache(keyBlockNum, key.Base58(), accounts)

if keyBlockNum <= atBlockNum && resolvedAccounts == nil {
resolvedAccounts = accounts
Expand Down Expand Up @@ -168,7 +168,22 @@ func (r *KVDBAccountsResolver) GetCursor(ctx context.Context, readerName string)
return NewCursor(blockNum), nil
}

func (r *KVDBAccountsResolver) pushToCache(blockNum uint64, key string, accounts Accounts) {
func (r *KVDBAccountsResolver) appendToCache(blockNum uint64, key string, accounts Accounts) {
if cacheItems, found := r.cache[key]; found {
for _, ci := range cacheItems {
if ci.blockNum == blockNum {
return
}
}
}
r.cache[key] = append(r.cache[key], []*cacheItem{
{
blockNum: blockNum,
accounts: accounts,
},
}...)
}
func (r *KVDBAccountsResolver) prependToCache(blockNum uint64, key string, accounts Accounts) {
if cacheItems, found := r.cache[key]; found {
for _, ci := range cacheItems {
if ci.blockNum == blockNum {
Expand Down
17 changes: 17 additions & 0 deletions accountresolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/streamingfast/kvdb/store"
_ "github.com/streamingfast/kvdb/store/badger3"
_ "github.com/streamingfast/kvdb/store/bigkv"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,6 +19,22 @@ var a3 = "5J7HHVuLb1kUn9q4PZgGYsLm4DNRg1dcmB5FENuM7wQz"
var a4 = "9hT5nqawMAn4xgCcjCmiPDXzVqECQTap3c3wHk6dxyFx"
var a5 = "A8YFwAca6hSp9Xw1RcqUcdXuVgMvQbT2yYLmArCFKxfD"

func TestKVDBAccountsResolver_WTF(t *testing.T) {

db, err := store.New("bigkv://dfuseio-global.dfuse-saas/sol-account-lookup-v1.1")
require.NoError(t, err)

resolver := NewKVDBAccountsResolver(db, zap.NewNop())
accounts, _, err := resolver.Resolve(context.Background(), 154804305, MustFromBase58("2yEiJNpLnFPizeXzpFSGwBC6idLfNYCJ2h42EqqdJ19h"))
require.NoError(t, err)
require.Equal(t, 254, len(accounts))

accounts, _, err = resolver.Resolve(context.Background(), 154804305, MustFromBase58("2yEiJNpLnFPizeXzpFSGwBC6idLfNYCJ2h42EqqdJ19h"))
require.NoError(t, err)
require.Equal(t, 254, len(accounts))

}

func TestKVDBAccountsResolver_Extended(t *testing.T) {
err := os.RemoveAll("/tmp/my-badger.db")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/firesol/validate_resolved_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func processValidateAllResolvedAddressesE(chain *firecore.Chain[*pbsolv1.Block],
fmt.Println("Saved validation state file", keyCount, notFoundCount)
}

time.Sleep(time.Second / 10)
time.Sleep(time.Second / 5)

item := iter.Item()
tableAccount, atBlock := accountsresolver.Keys.UnpackTableLookup(item.Key)
Expand Down

0 comments on commit a711f16

Please sign in to comment.