Skip to content

Commit

Permalink
add PersistCandsMapBlock into blockchain.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Oct 12, 2022
1 parent 436b7b5 commit b8c38e6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 17 deletions.
12 changes: 12 additions & 0 deletions action/protocol/staking/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package staking

import "github.com/iotexproject/iotex-core/blockchain/genesis"

type (

// BuilderConfig returns the configuration of the builder
BuilderConfig struct {
Staking genesis.Staking
PersistCandsMapBlock uint64
}
)
11 changes: 9 additions & 2 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"encoding/hex"
"encoding/json"
"math"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -71,7 +72,10 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
require.NoError(err)

// create protocol
p, err := NewProtocol(depositGas, genesis.Default.Staking, nil, genesis.Default.GreenlandBlockHeight)
p, err := NewProtocol(depositGas, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

// set up candidate
Expand Down Expand Up @@ -2664,7 +2668,10 @@ func initAll(t *testing.T, ctrl *gomock.Controller) (protocol.StateManager, *Pro
require.NoError(err)

// create protocol
p, err := NewProtocol(depositGas, genesis.Default.Staking, nil, genesis.Default.GreenlandBlockHeight)
p, err := NewProtocol(depositGas, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

// set up candidate
Expand Down
18 changes: 10 additions & 8 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type (
WithdrawWaitingPeriod time.Duration
MinStakeAmount *big.Int
BootstrapCandidates []genesis.BootstrapCandidate
PersistCandsMapBlock uint64
}

// DepositGas deposits gas to some pool
Expand All @@ -103,42 +104,43 @@ func FindProtocol(registry *protocol.Registry) *Protocol {
}

// NewProtocol instantiates the protocol of staking
func NewProtocol(depositGas DepositGas, cfg genesis.Staking, candBucketsIndexer *CandidatesBucketsIndexer, reviseHeights ...uint64) (*Protocol, error) {
func NewProtocol(depositGas DepositGas, cfg *BuilderConfig, candBucketsIndexer *CandidatesBucketsIndexer, reviseHeights ...uint64) (*Protocol, error) {
h := hash.Hash160b([]byte(_protocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
return nil, err
}

minStakeAmount, ok := new(big.Int).SetString(cfg.MinStakeAmount, 10)
minStakeAmount, ok := new(big.Int).SetString(cfg.Staking.MinStakeAmount, 10)
if !ok {
return nil, ErrInvalidAmount
}

regFee, ok := new(big.Int).SetString(cfg.RegistrationConsts.Fee, 10)
regFee, ok := new(big.Int).SetString(cfg.Staking.RegistrationConsts.Fee, 10)
if !ok {
return nil, ErrInvalidAmount
}

minSelfStake, ok := new(big.Int).SetString(cfg.RegistrationConsts.MinSelfStake, 10)
minSelfStake, ok := new(big.Int).SetString(cfg.Staking.RegistrationConsts.MinSelfStake, 10)
if !ok {
return nil, ErrInvalidAmount
}

// new vote reviser, revise ate greenland
voteReviser := NewVoteReviser(cfg.VoteWeightCalConsts, reviseHeights...)
voteReviser := NewVoteReviser(cfg.Staking.VoteWeightCalConsts, reviseHeights...)

return &Protocol{
addr: addr,
config: Configuration{
VoteWeightCalConsts: cfg.VoteWeightCalConsts,
VoteWeightCalConsts: cfg.Staking.VoteWeightCalConsts,
RegistrationConsts: RegistrationConsts{
Fee: regFee,
MinSelfStake: minSelfStake,
},
WithdrawWaitingPeriod: cfg.WithdrawWaitingPeriod,
WithdrawWaitingPeriod: cfg.Staking.WithdrawWaitingPeriod,
MinStakeAmount: minStakeAmount,
BootstrapCandidates: cfg.BootstrapCandidates,
BootstrapCandidates: cfg.Staking.BootstrapCandidates,
PersistCandsMapBlock: cfg.PersistCandsMapBlock,
},
depositGas: depositGas,
candBucketsIndexer: candBucketsIndexer,
Expand Down
21 changes: 17 additions & 4 deletions action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package staking

import (
"context"
"math"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -86,7 +87,10 @@ func TestProtocol(t *testing.T) {
}

// test loading with no candidate in stateDB
stk, err := NewProtocol(nil, genesis.Default.Staking, nil, genesis.Default.GreenlandBlockHeight)
stk, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
r.NotNil(stk)
r.NoError(err)
buckets, _, err := csr.getAllBuckets()
Expand Down Expand Up @@ -189,7 +193,10 @@ func TestCreatePreStates(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
sm := testdb.NewMockStateManager(ctrl)
p, err := NewProtocol(nil, genesis.Default.Staking, nil, genesis.Default.GreenlandBlockHeight)
p, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)
ctx := protocol.WithBlockCtx(
genesis.WithGenesisContext(context.Background(), genesis.Default),
Expand Down Expand Up @@ -249,7 +256,10 @@ func Test_CreatePreStatesWithRegisterProtocol(t *testing.T) {

ctx := context.Background()
require.NoError(cbi.Start(ctx))
p, err := NewProtocol(nil, genesis.Default.Staking, cbi, genesis.Default.GreenlandBlockHeight)
p, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
}, cbi, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

rol := rolldpos.NewProtocol(23, 4, 3)
Expand Down Expand Up @@ -362,7 +372,10 @@ func Test_CreateGenesisStates(t *testing.T) {
ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx))
for _, test := range testBootstrapCandidates {
cfg.BootstrapCandidates = test.BootstrapCandidate
p, err := NewProtocol(nil, cfg, nil, genesis.Default.GreenlandBlockHeight)
p, err := NewProtocol(nil, &BuilderConfig{
Staking: cfg,
PersistCandsMapBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

v, err := p.Start(ctx, sm)
Expand Down
6 changes: 5 additions & 1 deletion action/protocol/staking/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package staking

import (
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -64,7 +65,10 @@ func TestIsValidCandidateName(t *testing.T) {

func initTestProtocol(t *testing.T) (*Protocol, []*Candidate) {
require := require.New(t)
p, err := NewProtocol(nil, genesis.Default.Staking, nil, genesis.Default.GreenlandBlockHeight)
p, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

var cans []*Candidate
Expand Down
6 changes: 5 additions & 1 deletion action/protocol/staking/vote_reviser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package staking

import (
"context"
"math"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -113,7 +114,10 @@ func TestVoteReviser(t *testing.T) {
// test loading with no candidate in stateDB
stk, err := NewProtocol(
nil,
genesis.Default.Staking,
&BuilderConfig{
Staking: genesis.Default.Staking,
PersistCandsMapBlock: math.MaxUint64,
},
nil,
genesis.Default.GreenlandBlockHeight,
genesis.Default.HawaiiBlockHeight,
Expand Down
3 changes: 3 additions & 0 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type (
WorkingSetCacheSize uint64 `yaml:"workingSetCacheSize"`
// StreamingBlockBufferSize
StreamingBlockBufferSize uint64 `yaml:"streamingBlockBufferSize"`
// PersistCandsMapBlock is the block to persist candidates map
PersistCandsMapBlock uint64 `yaml:"persistCandsMapBlock"`
}
)

Expand Down Expand Up @@ -103,6 +105,7 @@ var (
StateDBCacheSize: 1000,
WorkingSetCacheSize: 20,
StreamingBlockBufferSize: 200,
PersistCandsMapBlock: 19778037,
}

// ErrConfig config error
Expand Down
5 changes: 4 additions & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,10 @@ func (builder *Builder) registerStakingProtocol() error {
}
stakingProtocol, err := staking.NewProtocol(
rewarding.DepositGas,
builder.cfg.Genesis.Staking,
&staking.BuilderConfig{
Staking: builder.cfg.Genesis.Staking,
PersistCandsMapBlock: builder.cfg.Chain.PersistCandsMapBlock,
},
builder.cs.candBucketsIndexer,
builder.cfg.Genesis.GreenlandBlockHeight,
builder.cfg.Genesis.HawaiiBlockHeight,
Expand Down

0 comments on commit b8c38e6

Please sign in to comment.