Skip to content

Commit

Permalink
add PersistStakingPatchBlock into blockchain.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Oct 14, 2022
1 parent 75cb0a1 commit 02f8190
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 23 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
PersistStakingPatchBlock 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,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

// set up candidate
Expand Down
30 changes: 16 additions & 14 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ type (

// Configuration is the staking protocol configuration.
Configuration struct {
VoteWeightCalConsts genesis.VoteWeightCalConsts
RegistrationConsts RegistrationConsts
WithdrawWaitingPeriod time.Duration
MinStakeAmount *big.Int
BootstrapCandidates []genesis.BootstrapCandidate
VoteWeightCalConsts genesis.VoteWeightCalConsts
RegistrationConsts RegistrationConsts
WithdrawWaitingPeriod time.Duration
MinStakeAmount *big.Int
BootstrapCandidates []genesis.BootstrapCandidate
PersistStakingPatchBlock 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,
MinStakeAmount: minStakeAmount,
BootstrapCandidates: cfg.BootstrapCandidates,
WithdrawWaitingPeriod: cfg.Staking.WithdrawWaitingPeriod,
MinStakeAmount: minStakeAmount,
BootstrapCandidates: cfg.Staking.BootstrapCandidates,
PersistStakingPatchBlock: cfg.PersistStakingPatchBlock,
},
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,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: 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"`
// PersistStakingPatchBlock is the block to persist staking patch
PersistStakingPatchBlock uint64 `yaml:"persistStakingPatchBlock"`
}
)

Expand Down Expand Up @@ -103,6 +105,7 @@ var (
StateDBCacheSize: 1000,
WorkingSetCacheSize: 20,
StreamingBlockBufferSize: 200,
PersistStakingPatchBlock: 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,
PersistStakingPatchBlock: builder.cfg.Chain.PersistStakingPatchBlock,
},
builder.cs.candBucketsIndexer,
builder.cfg.Genesis.GreenlandBlockHeight,
builder.cfg.Genesis.HawaiiBlockHeight,
Expand Down

0 comments on commit 02f8190

Please sign in to comment.