Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Oct 11, 2023
1 parent f88f90d commit dacbaab
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 79 deletions.
7 changes: 1 addition & 6 deletions action/protocol/staking/vote_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
)

const (
maxBlockNumber = math.MaxUint64
blockProduceInterval = 5 // produce one block per 5 seconds
maxBlockNumber = math.MaxUint64
)

type (
Expand Down Expand Up @@ -221,10 +220,6 @@ func bucketKey(index uint64) []byte {
// CalculateVoteWeight calculates the vote weight
func CalculateVoteWeight(c genesis.VoteWeightCalConsts, v *VoteBucket, selfStake bool) *big.Int {
remainingTime := v.StakedDuration.Seconds()
if !v.isNative() {
// for contract staking, use block number to calculate remaining time
remainingTime = float64(v.StakedDurationBlockNumber) * blockProduceInterval
}
weight := float64(1)
var m float64
if v.AutoStake {
Expand Down
4 changes: 4 additions & 0 deletions action/protocol/staking/vote_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestCalculateVoteWeight(t *testing.T) {
name: "NFT, auto-stake enabled, self-stake enabled",
consts: consts,
voteBucket: &VoteBucket{
StakedDuration: 30 * 24 * time.Hour,
StakedDurationBlockNumber: 30 * 17280,
AutoStake: true,
StakedAmount: big.NewInt(10000),
Expand All @@ -174,6 +175,7 @@ func TestCalculateVoteWeight(t *testing.T) {
name: "NFT, auto-stake enabled, self-stake disabled",
consts: consts,
voteBucket: &VoteBucket{
StakedDuration: 30 * 24 * time.Hour,
StakedDurationBlockNumber: 30 * 17280,
AutoStake: true,
StakedAmount: big.NewInt(10000),
Expand All @@ -186,6 +188,7 @@ func TestCalculateVoteWeight(t *testing.T) {
name: "NFT, auto-stake disabled, self-stake enabled",
consts: consts,
voteBucket: &VoteBucket{
StakedDuration: 30 * 24 * time.Hour,
StakedDurationBlockNumber: 30 * 17280,
AutoStake: false,
StakedAmount: big.NewInt(10000),
Expand All @@ -198,6 +201,7 @@ func TestCalculateVoteWeight(t *testing.T) {
name: "NFT, auto-stake disabled, self-stake disabled",
consts: consts,
voteBucket: &VoteBucket{
StakedDuration: 30 * 24 * time.Hour,
StakedDurationBlockNumber: 30 * 17280,
AutoStake: false,
StakedAmount: big.NewInt(10000),
Expand Down
5 changes: 4 additions & 1 deletion blockindex/contractstaking/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
package contractstaking

import (
"time"

"github.com/iotexproject/iotex-core/action/protocol/staking"
)

// Bucket defines the bucket struct for contract staking
type Bucket = staking.VoteBucket

func assembleBucket(token uint64, bi *bucketInfo, bt *BucketType, contractAddr string) *Bucket {
func assembleBucket(token uint64, bi *bucketInfo, bt *BucketType, contractAddr string, blockInterval time.Duration) *Bucket {
vb := Bucket{
Index: token,
StakedAmount: bt.Amount,
StakedDuration: time.Duration(bt.Duration) * blockInterval,
StakedDurationBlockNumber: bt.Duration,
CreateBlockHeight: bi.CreatedAt,
StakeStartBlockHeight: bi.CreatedAt,
Expand Down
18 changes: 7 additions & 11 deletions blockindex/contractstaking/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ type (
propertyBucketTypeMap map[int64]map[uint64]uint64 // map[amount][duration]index
totalBucketCount uint64 // total number of buckets including burned buckets
height uint64 // current block height, it's put in cache for consistency on merge
contractAddress string // contract address for the bucket
mutex sync.RWMutex // a RW mutex for the cache to protect concurrent access
calculateVoteWeight calculateVoteWeightFunc // calculateVoteWeight is a function to calculate vote weight
config Config
}

calculateVoteWeightFunc func(v *Bucket, selfStake bool) *big.Int
)

var (
Expand All @@ -41,14 +38,13 @@ var (
ErrInvalidHeight = errors.New("invalid height")
)

func newContractStakingCache(contractAddr string, calculateVoteWeight calculateVoteWeightFunc) *contractStakingCache {
func newContractStakingCache(config Config) *contractStakingCache {
return &contractStakingCache{
bucketInfoMap: make(map[uint64]*bucketInfo),
bucketTypeMap: make(map[uint64]*BucketType),
propertyBucketTypeMap: make(map[int64]map[uint64]uint64),
candidateBucketMap: make(map[string]map[uint64]bool),
contractAddress: contractAddr,
calculateVoteWeight: calculateVoteWeight,
config: config,
}
}

Expand Down Expand Up @@ -82,7 +78,7 @@ func (s *contractStakingCache) CandidateVotes(ctx context.Context, candidate add
}
bt := s.mustGetBucketType(bi.TypeIndex)
if featureCtx.FixContractStakingWeightedVotes {
votes.Add(votes, s.calculateVoteWeight(assembleBucket(id, bi, bt, s.contractAddress), false))
votes.Add(votes, s.config.CalculateVoteWeight(assembleBucket(id, bi, bt, s.config.ContractAddress, s.config.BlockInterval)))
} else {
votes.Add(votes, bt.Amount)
}
Expand All @@ -101,7 +97,7 @@ func (s *contractStakingCache) Buckets(height uint64) ([]*Bucket, error) {
vbs := []*Bucket{}
for id, bi := range s.bucketInfoMap {
bt := s.mustGetBucketType(bi.TypeIndex)
vb := assembleBucket(id, bi.clone(), bt, s.contractAddress)
vb := assembleBucket(id, bi.clone(), bt, s.config.ContractAddress, s.config.BlockInterval)
vbs = append(vbs, vb)
}
return vbs, nil
Expand Down Expand Up @@ -362,7 +358,7 @@ func (s *contractStakingCache) mustGetBucketInfo(id uint64) *bucketInfo {
func (s *contractStakingCache) mustGetBucket(id uint64) *Bucket {
bi := s.mustGetBucketInfo(id)
bt := s.mustGetBucketType(bi.TypeIndex)
return assembleBucket(id, bi, bt, s.contractAddress)
return assembleBucket(id, bi, bt, s.config.ContractAddress, s.config.BlockInterval)
}

func (s *contractStakingCache) getBucket(id uint64) (*Bucket, bool) {
Expand All @@ -371,7 +367,7 @@ func (s *contractStakingCache) getBucket(id uint64) (*Bucket, bool) {
return nil, false
}
bt := s.mustGetBucketType(bi.TypeIndex)
return assembleBucket(id, bi, bt, s.contractAddress), true
return assembleBucket(id, bi, bt, s.config.ContractAddress, s.config.BlockInterval), true
}

func (s *contractStakingCache) putBucketType(id uint64, bt *BucketType) {
Expand Down
24 changes: 12 additions & 12 deletions blockindex/contractstaking/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func _checkCacheCandidateVotes(ctx context.Context, r *require.Assertions, cache
}

func calculateVoteWeightGen(c genesis.VoteWeightCalConsts) calculateVoteWeightFunc {
return func(v *Bucket, selfStake bool) *big.Int {
return staking.CalculateVoteWeight(c, v, selfStake)
return func(v *Bucket) *big.Int {
return staking.CalculateVoteWeight(c, v, false)
}
}

Expand All @@ -38,7 +38,7 @@ func TestContractStakingCache_CandidateVotes(t *testing.T) {
}
}
require := require.New(t)
cache := newContractStakingCache(identityset.Address(1).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(1).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})
checkCacheCandidateVotes := checkCacheCandidateVotesGen(protocol.WithFeatureCtx(protocol.WithBlockCtx(genesis.WithGenesisContext(context.Background(), genesis.Default), protocol.BlockCtx{BlockHeight: 1})))
checkCacheCandidateVotesAfterRedsea := checkCacheCandidateVotesGen(protocol.WithFeatureCtx(protocol.WithBlockCtx(genesis.WithGenesisContext(context.Background(), genesis.Default), protocol.BlockCtx{BlockHeight: genesis.Default.RedseaBlockHeight})))
// no bucket
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestContractStakingCache_CandidateVotes(t *testing.T) {
func TestContractStakingCache_Buckets(t *testing.T) {
require := require.New(t)
contractAddr := identityset.Address(27).String()
cache := newContractStakingCache(contractAddr, calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: contractAddr, CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

height := uint64(0)
// no bucket
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestContractStakingCache_Buckets(t *testing.T) {
func TestContractStakingCache_BucketsByCandidate(t *testing.T) {
require := require.New(t)
contractAddr := identityset.Address(27).String()
cache := newContractStakingCache(contractAddr, calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: contractAddr, CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

height := uint64(0)
// no bucket
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestContractStakingCache_BucketsByCandidate(t *testing.T) {
func TestContractStakingCache_BucketsByIndices(t *testing.T) {
require := require.New(t)
contractAddr := identityset.Address(27).String()
cache := newContractStakingCache(contractAddr, calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: contractAddr, CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

height := uint64(0)
// no bucket
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestContractStakingCache_BucketsByIndices(t *testing.T) {

func TestContractStakingCache_TotalBucketCount(t *testing.T) {
require := require.New(t)
cache := newContractStakingCache(identityset.Address(27).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(27).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

height := uint64(0)
// no bucket
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestContractStakingCache_TotalBucketCount(t *testing.T) {

func TestContractStakingCache_ActiveBucketTypes(t *testing.T) {
require := require.New(t)
cache := newContractStakingCache(identityset.Address(27).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(27).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

height := uint64(0)
// no bucket type
Expand Down Expand Up @@ -402,7 +402,7 @@ func TestContractStakingCache_ActiveBucketTypes(t *testing.T) {

func TestContractStakingCache_Merge(t *testing.T) {
require := require.New(t)
cache := newContractStakingCache(identityset.Address(27).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(27).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})
height := uint64(1)
ctx := protocol.WithFeatureCtx(protocol.WithBlockCtx(genesis.WithGenesisContext(context.Background(), genesis.Default), protocol.BlockCtx{BlockHeight: height}))

Expand Down Expand Up @@ -460,7 +460,7 @@ func TestContractStakingCache_Merge(t *testing.T) {

func TestContractStakingCache_MatchBucketType(t *testing.T) {
require := require.New(t)
cache := newContractStakingCache(identityset.Address(27).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(27).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

// no bucket types
_, bucketType, ok := cache.MatchBucketType(big.NewInt(100), 100)
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestContractStakingCache_MatchBucketType(t *testing.T) {

func TestContractStakingCache_BucketTypeCount(t *testing.T) {
require := require.New(t)
cache := newContractStakingCache(identityset.Address(27).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(27).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

height := uint64(0)
// no bucket type
Expand Down Expand Up @@ -524,7 +524,7 @@ func TestContractStakingCache_BucketTypeCount(t *testing.T) {

func TestContractStakingCache_LoadFromDB(t *testing.T) {
require := require.New(t)
cache := newContractStakingCache(identityset.Address(27).String(), calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
cache := newContractStakingCache(Config{ContractAddress: identityset.Address(27).String(), CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts), BlockInterval: _blockInterval})

// load from empty db
path, err := testutil.PathOfTempFile("staking.db")
Expand Down
2 changes: 1 addition & 1 deletion blockindex/contractstaking/delta_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type (

func newContractStakingDelta() *contractStakingDelta {
return &contractStakingDelta{
cache: newContractStakingCache("", nil),
cache: newContractStakingCache(Config{}),
bucketTypeDeltaState: make(map[uint64]deltaState),
bucketInfoDeltaState: make(map[uint64]deltaState),
}
Expand Down
12 changes: 6 additions & 6 deletions blockindex/contractstaking/dirty_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestContractStakingDirty_getBucketType(t *testing.T) {
require := require.New(t)
clean := newContractStakingCache("", calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
clean := newContractStakingCache(Config{CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts)})
dirty := newContractStakingDirty(clean)

// no bucket type
Expand All @@ -41,7 +41,7 @@ func TestContractStakingDirty_getBucketType(t *testing.T) {

func TestContractStakingDirty_getBucketInfo(t *testing.T) {
require := require.New(t)
clean := newContractStakingCache("", calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
clean := newContractStakingCache(Config{CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts)})
dirty := newContractStakingDirty(clean)

// no bucket info
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestContractStakingDirty_getBucketInfo(t *testing.T) {

func TestContractStakingDirty_matchBucketType(t *testing.T) {
require := require.New(t)
clean := newContractStakingCache("", calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
clean := newContractStakingCache(Config{CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts)})
dirty := newContractStakingDirty(clean)

// no bucket type
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestContractStakingDirty_matchBucketType(t *testing.T) {

func TestContractStakingDirty_getBucketTypeCount(t *testing.T) {
require := require.New(t)
clean := newContractStakingCache("", calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
clean := newContractStakingCache(Config{CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts)})
dirty := newContractStakingDirty(clean)

// no bucket type
Expand All @@ -143,7 +143,7 @@ func TestContractStakingDirty_getBucketTypeCount(t *testing.T) {

func TestContractStakingDirty_finalize(t *testing.T) {
require := require.New(t)
clean := newContractStakingCache("", calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
clean := newContractStakingCache(Config{CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts)})
dirty := newContractStakingDirty(clean)

// no dirty data
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestContractStakingDirty_finalize(t *testing.T) {

func TestContractStakingDirty_noSideEffectOnClean(t *testing.T) {
require := require.New(t)
clean := newContractStakingCache("", calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts))
clean := newContractStakingCache(Config{CalculateVoteWeight: calculateVoteWeightGen(genesis.Default.VoteWeightCalConsts)})
dirty := newContractStakingDirty(clean)

// add bucket type to dirty cache
Expand Down
Loading

0 comments on commit dacbaab

Please sign in to comment.