Skip to content

Commit

Permalink
fix tests and cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Oct 30, 2023
1 parent a4dd415 commit 2ebaa04
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions accountresolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ func (r *KVDBAccountsResolver) CreateOrDelete(ctx context.Context, blockNum uint
return fmt.Errorf("flushing extended accounts for key %q: %w", key, err)
}

r.cache[key.Base58()] = append(r.cache[key.Base58()],
[]*cacheItem{{
blockNum: blockNum,
accounts: nil,
}}...)
r.pushToCache(blockNum, key.Base58(), nil)

return nil
}
Expand All @@ -70,27 +66,14 @@ func (r *KVDBAccountsResolver) Extend(ctx context.Context, blockNum uint64, trxH
return fmt.Errorf("flushing extended accounts for key %q: %w", key, err)
}

if cacheItems, ok := r.cache[key.Base58()]; ok {
for _, cacheItem := range cacheItems {
if cacheItem.blockNum == blockNum {
cacheItem.accounts = append(cacheItem.accounts, extendedAccounts...)
return nil
}
}
}

r.cache[key.Base58()] = append(r.cache[key.Base58()], []*cacheItem{{
blockNum: blockNum,
accounts: extendedAccounts,
}}...)
r.pushToCache(blockNum, key.Base58(), extendedAccounts)

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 i := len(cacheItems) - 1; i >= 0; i-- {
cacheItem := cacheItems[i]
for _, cacheItem := range cacheItems {
if cacheItem.blockNum < atBlockNum {
return cacheItem.accounts, true, nil
}
Expand All @@ -106,11 +89,7 @@ func (r *KVDBAccountsResolver) Resolve(ctx context.Context, atBlockNum uint64, k
_, keyBlockNum := Keys.UnpackTableLookup(item.Key)
accounts := DecodeAccounts(item.Value)

// why are we touching the cache here?
//r.cache[key.Base58()] = append(r.cache[key.Base58()], &cacheItem{
// blockNum: keyBlockNum,
// accounts: accounts,
//})
r.pushToCache(atBlockNum, key.Base58(), resolvedAccounts)

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

r.cache[key.Base58()] = append(r.cache[key.Base58()], &cacheItem{
blockNum: keyBlockNum,
accounts: accounts,
})
r.pushToCache(keyBlockNum, key.Base58(), accounts)

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

func (r *KVDBAccountsResolver) pushToCache(blockNum uint64, key string, accounts Accounts) {
if cacheItems, found := r.cache[key]; found {
for _, ci := range cacheItems {
if ci.blockNum == blockNum {
ci.accounts = append(ci.accounts, accounts...)
return
}
}
}
r.cache[key] = append([]*cacheItem{
{
blockNum: blockNum,
accounts: accounts,
},
}, r.cache[key]...)
}

func DecodeAccounts(payload []byte) Accounts {
var accounts Accounts
for i := 0; i < len(payload); i += 32 {
Expand Down

0 comments on commit 2ebaa04

Please sign in to comment.