-
Notifications
You must be signed in to change notification settings - Fork 345
cow cache for staking view #4655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c497ff8
cow cache for staking view
envestcc dbe5e70
cow v1 staking view
envestcc abf89ac
add mutex for v1 staking view
envestcc eca5796
add mutex for v2 staking view
envestcc 34ba472
address comment
envestcc 167b528
Merge branch 'master' into cowcache
CoderZhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,104 @@ | ||
package stakingindex | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/iotexproject/iotex-address/address" | ||
|
||
"github.com/iotexproject/iotex-core/v2/db" | ||
) | ||
|
||
type ( | ||
bucketCache interface { | ||
Load(kvstore db.KVStore) error | ||
Copy() bucketCache | ||
PutBucket(id uint64, bkt *Bucket) | ||
DeleteBucket(id uint64) | ||
BucketIdxs() []uint64 | ||
Bucket(id uint64) *Bucket | ||
Buckets(indices []uint64) []*Bucket | ||
BucketIdsByCandidate(candidate address.Address) []uint64 | ||
TotalBucketCount() uint64 | ||
} | ||
|
||
cowCache struct { | ||
cache bucketCache | ||
dirty bool | ||
mu sync.Mutex | ||
} | ||
) | ||
|
||
func newCowCache(cache bucketCache) *cowCache { | ||
return &cowCache{ | ||
cache: cache, | ||
dirty: false, | ||
} | ||
} | ||
|
||
func (cow *cowCache) Copy() bucketCache { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
if cow.dirty { | ||
cow.dirty = false | ||
} | ||
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete? |
||
return &cowCache{ | ||
cache: cow.cache, | ||
dirty: false, | ||
} | ||
} | ||
|
||
func (cow *cowCache) Load(kvstore db.KVStore) error { | ||
return errors.New("not supported in cowCache") | ||
} | ||
|
||
func (cow *cowCache) BucketIdsByCandidate(candidate address.Address) []uint64 { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
return cow.cache.BucketIdsByCandidate(candidate) | ||
} | ||
|
||
func (cow *cowCache) PutBucket(id uint64, bkt *Bucket) { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
cow.ensureCopied() | ||
cow.cache.PutBucket(id, bkt) | ||
} | ||
|
||
func (cow *cowCache) DeleteBucket(id uint64) { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
cow.ensureCopied() | ||
cow.cache.DeleteBucket(id) | ||
} | ||
|
||
func (cow *cowCache) BucketIdxs() []uint64 { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
return cow.cache.BucketIdxs() | ||
} | ||
|
||
func (cow *cowCache) Bucket(id uint64) *Bucket { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
return cow.cache.Bucket(id) | ||
} | ||
|
||
func (cow *cowCache) Buckets(indices []uint64) []*Bucket { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
return cow.cache.Buckets(indices) | ||
} | ||
|
||
func (cow *cowCache) TotalBucketCount() uint64 { | ||
cow.mu.Lock() | ||
defer cow.mu.Unlock() | ||
return cow.cache.TotalBucketCount() | ||
} | ||
|
||
func (cow *cowCache) ensureCopied() { | ||
if !cow.dirty { | ||
cow.cache = cow.cache.Copy() | ||
cow.dirty = true | ||
} | ||
} |
This file contains hidden or 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,48 @@ | ||
package stakingindex | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotexproject/iotex-core/v2/test/identityset" | ||
) | ||
|
||
func TestCowCache(t *testing.T) { | ||
r := require.New(t) | ||
buckets := []*Bucket{ | ||
{Candidate: identityset.Address(1), Owner: identityset.Address(2), StakedAmount: big.NewInt(1000), Timestamped: true, StakedDuration: 3600, CreatedAt: 1622548800, UnlockedAt: 1622552400, UnstakedAt: 1622556000, Muted: false}, | ||
{Candidate: identityset.Address(3), Owner: identityset.Address(4), StakedAmount: big.NewInt(2000), Timestamped: false, StakedDuration: 7200, CreatedAt: 1622548801, UnlockedAt: 1622552401, UnstakedAt: 1622556001, Muted: true}, | ||
{Candidate: identityset.Address(5), Owner: identityset.Address(6), StakedAmount: big.NewInt(3000), Timestamped: true, StakedDuration: 10800, CreatedAt: 1622548802, UnlockedAt: 1622552402, UnstakedAt: 1622556002, Muted: false}, | ||
{Candidate: identityset.Address(7), Owner: identityset.Address(8), StakedAmount: big.NewInt(4000), Timestamped: true, StakedDuration: 10800, CreatedAt: 1622548802, UnlockedAt: 1622552402, UnstakedAt: 1622556002, Muted: false}, | ||
} | ||
original := newCache("testNS", "testBucketNS") | ||
original.PutBucket(0, buckets[0]) | ||
// case 1: read cowCache without modification | ||
cow := newCowCache(original) | ||
r.Equal(buckets[0], cow.Bucket(0)) | ||
|
||
// case 2: modify cowCache but not affect original cache | ||
cow.PutBucket(1, buckets[1]) | ||
r.Equal(buckets[1], cow.Bucket(1)) | ||
r.Nil(original.Bucket(1)) | ||
cow.DeleteBucket(0) | ||
r.Nil(cow.Bucket(0)) | ||
r.Equal(buckets[0], original.Bucket(0)) | ||
|
||
// case 3: not real copy before modification | ||
copi := cow.Copy() | ||
r.Equal(buckets[1], copi.Bucket(1)) | ||
r.Equal(cow.cache, copi.(*cowCache).cache) | ||
|
||
// case 4: copied not affected by original modification | ||
cow.PutBucket(2, buckets[2]) | ||
r.Equal(buckets[2], cow.Bucket(2)) | ||
r.Nil(copi.Bucket(2)) | ||
|
||
// case 5: original not affected by copied modification | ||
copi.PutBucket(3, buckets[3]) | ||
r.Equal(buckets[3], copi.Bucket(3)) | ||
r.Nil(cow.Bucket(3)) | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: load from state reader