-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from blukat29/fix-reexec-stakinginfo
reward: Fix state regeneration with post-Kaia staking info
- Loading branch information
Showing
7 changed files
with
333 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package common | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// RefCntMap is a map with reference counting. | ||
type RefCountingMap struct { | ||
mu sync.RWMutex | ||
values map[interface{}]interface{} | ||
counts map[interface{}]int | ||
} | ||
|
||
func NewRefCountingMap() *RefCountingMap { | ||
return &RefCountingMap{ | ||
values: make(map[interface{}]interface{}), | ||
counts: make(map[interface{}]int), | ||
} | ||
} | ||
|
||
// Get returns the value associated with the given key. | ||
func (r *RefCountingMap) Get(key interface{}) (interface{}, bool) { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
value, ok := r.values[key] | ||
return value, ok | ||
} | ||
|
||
// Add adds an element to the map and increments its reference count. | ||
func (r *RefCountingMap) Add(key interface{}, value interface{}) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
r.values[key] = value | ||
r.counts[key]++ | ||
} | ||
|
||
// Remove decrements the reference count of the element with the given key. | ||
func (r *RefCountingMap) Remove(key interface{}) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
if r.counts[key] > 0 { | ||
r.counts[key]-- | ||
} | ||
if r.counts[key] == 0 { | ||
delete(r.values, key) | ||
delete(r.counts, key) | ||
} | ||
} | ||
|
||
// Len returns the number of elements in the map. | ||
func (r *RefCountingMap) Len() int { | ||
return len(r.values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.