Skip to content

Commit

Permalink
Bugfix when updating input addresses (pointer to range variable)
Browse files Browse the repository at this point in the history
  • Loading branch information
mboben committed Feb 13, 2024
1 parent 0d2aba6 commit 8df941a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions indexer/pchain/in_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (iu *pChainInputUpdater) updateFromDB(
return nil, err
}
baseOuts := shared.NewOutputMap()
for _, out := range outs {
baseOuts.Add(shared.NewIdIndexKey(out.TxID, out.Index()), &out.TxOutput)
for i, out := range outs {
baseOuts.Add(shared.NewIdIndexKey(out.TxID, out.Index()), &outs[i].TxOutput)
}
return inputs.UpdateWithOutputs(baseOuts), nil
}
Expand Down
17 changes: 11 additions & 6 deletions utils/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type Cache[K comparable, V any] interface {

// Map object cache
type cache[K comparable, V any] struct {
sync.RWMutex

cacheMap map[K]V
accessed []K

sync.RWMutex
}

func NewCache[K comparable, V any]() Cache[K, V] {
Expand All @@ -31,24 +31,29 @@ func NewCache[K comparable, V any]() Cache[K, V] {
}

func (c *cache[K, V]) Add(k K, v V) {
c.Lock()
defer c.Unlock()

c.cacheMap[k] = v
}

func (c *cache[K, V]) Get(k K) (V, bool) {
c.RWMutex.Lock()
c.Lock()
defer c.Unlock()

v, ok := c.cacheMap[k]
if ok {
c.accessed = append(c.accessed, k)
}
c.RWMutex.Unlock()
return v, ok
}

func (c *cache[K, V]) RemoveAccessed() {
c.RWMutex.Lock()
c.Lock()
defer c.Unlock()

for _, k := range c.accessed {
delete(c.cacheMap, k)
}
c.accessed = nil
c.RWMutex.Unlock()
}

0 comments on commit 8df941a

Please sign in to comment.