Skip to content

Commit

Permalink
bring back the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Sep 21, 2023
1 parent 78d202c commit 29e8c01
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions accountresolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,46 @@ func (r *KVDBAccountsResolver) Extend(ctx context.Context, blockNum uint64, trxH
return fmt.Errorf("flushing extended accounts for key %q: %w", key, err)
}

//r.cache[key.base58()] = append([]*cacheItem{{
// blockNum: blockNum,
// accounts: extendedAccount,
//}}, r.cache[key.base58()]...)
r.cache[key.base58()] = append([]*cacheItem{{
blockNum: blockNum,
accounts: extendedAccount,
}}, r.cache[key.base58()]...)

return nil
}

func (r *KVDBAccountsResolver) Resolve(ctx context.Context, atBlockNum uint64, key Account) (Accounts, bool, error) {
//if cacheItems, ok := r.cache[key.base58()]; ok {
// for _, cacheItem := range cacheItems {
// if cacheItem.blockNum <= atBlockNum {
// return cacheItem.accounts, true, nil
// }
// }
//}
if cacheItems, ok := r.cache[key.base58()]; ok {
for _, cacheItem := range cacheItems {
if cacheItem.blockNum <= atBlockNum {
return cacheItem.accounts, true, nil
}
}
}

keyBytes := Keys.tableLookupPrefix(key)
iter := r.store.Prefix(ctx, keyBytes, store.Unlimited)
if iter.Err() != nil {
return nil, false, fmt.Errorf("querying accounts for key %q: %w", key, iter.Err())
}

var resolvedAccounts Accounts
for iter.Next() {
item := iter.Item()
_, keyBlockNum := Keys.unpackTableLookup(item.Key)
if keyBlockNum <= atBlockNum {
return decodeAccounts(item.Value), false, nil
accounts := decodeAccounts(item.Value)

r.cache[key.base58()] = append([]*cacheItem{{
blockNum: keyBlockNum,
accounts: accounts,
}})

if keyBlockNum <= atBlockNum && resolvedAccounts == nil {
resolvedAccounts = accounts
}
}

return nil, false, nil
return resolvedAccounts, false, nil
}

func (r *KVDBAccountsResolver) isKnownTransaction(ctx context.Context, transactionHash []byte) bool {
Expand Down

0 comments on commit 29e8c01

Please sign in to comment.