Skip to content
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

[staking] enable new staking index #4305

Merged
merged 14 commits into from
Jun 18, 2024
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type (
PanicUnrecoverableError bool
CandidateIdentifiedByOwner bool
UseTxContainer bool
LimitedStakingContract bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -271,6 +272,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
PanicUnrecoverableError: g.IsToBeEnabled(height),
CandidateIdentifiedByOwner: !g.IsToBeEnabled(height),
UseTxContainer: g.IsToBeEnabled(height),
LimitedStakingContract: !g.IsToBeEnabled(height),
},
)
}
Expand Down
7 changes: 5 additions & 2 deletions action/protocol/staking/handler_candidate_selfstake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ func initTestStateWithHeight(t *testing.T, ctrl *gomock.Controller, bucketCfgs [
require.NoError(err)

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

// set up bucket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ func TestProtocol_HandleCandidateTransferOwnership(t *testing.T) {
csm, err := NewCandidateStateManager(sm, false)
require.NoError(err)
// create protocol
p, err := NewProtocol(depositGas, &BuilderConfig{
p, err := NewProtocol(HelperCtx{
DepositGas: depositGas,
BlockInterval: getBlockInterval,
}, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, nil, genesis.Default.GreenlandBlockHeight)
}, nil, nil, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)
initCandidateCfgs := []struct {
Owner address.Address
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
}

// put registrationFee to reward pool
if _, err := p.depositGas(ctx, csm.SM(), registrationFee); err != nil {
if _, err := p.helperCtx.DepositGas(ctx, csm.SM(), registrationFee); err != nil {
return log, nil, errors.Wrap(err, "failed to deposit gas")
}

Expand Down
18 changes: 14 additions & 4 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
require.NoError(err)

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

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

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

// set up candidate
Expand Down Expand Up @@ -3264,6 +3270,10 @@ func depositGas(ctx context.Context, sm protocol.StateManager, gasFee *big.Int)
return nil, accountutil.StoreAccount(sm, actionCtx.Caller, acc)
}

func getBlockInterval(height uint64) time.Duration {
return 5 * time.Second
}

func newconsignment(r *require.Assertions, bucketIdx, nonce uint64, senderPrivate, recipient, consignTpye, reclaim string, wrongSig bool) []byte {
msg := action.ConsignMsgEther{
BucketIdx: bucketIdx,
Expand Down
54 changes: 38 additions & 16 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ type (

// Protocol defines the protocol of handling staking
Protocol struct {
addr address.Address
depositGas DepositGas
config Configuration
candBucketsIndexer *CandidatesBucketsIndexer
contractStakingIndexer ContractStakingIndexerWithBucketType
voteReviser *VoteReviser
patch *PatchStore
addr address.Address
config Configuration
candBucketsIndexer *CandidatesBucketsIndexer
contractStakingIndexer ContractStakingIndexerWithBucketType
contractStakingIndexerV2 ContractStakingIndexer
voteReviser *VoteReviser
patch *PatchStore
helperCtx HelperCtx
}

// Configuration is the staking protocol configuration.
Expand All @@ -95,6 +96,11 @@ type (
PersistStakingPatchBlock uint64
EndorsementWithdrawWaitingBlocks uint64
}
// HelperCtx is the helper context for staking protocol
HelperCtx struct {
BlockInterval func(uint64) time.Duration
DepositGas DepositGas
}

// DepositGas deposits gas to some pool
DepositGas func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error)
Expand All @@ -118,10 +124,11 @@ func FindProtocol(registry *protocol.Registry) *Protocol {

// NewProtocol instantiates the protocol of staking
func NewProtocol(
depositGas DepositGas,
helperCtx HelperCtx,
cfg *BuilderConfig,
candBucketsIndexer *CandidatesBucketsIndexer,
contractStakingIndexer ContractStakingIndexerWithBucketType,
contractStakingIndexerV2 ContractStakingIndexer,
correctCandsHeight uint64,
reviseHeights ...uint64,
) (*Protocol, error) {
Expand Down Expand Up @@ -163,11 +170,12 @@ func NewProtocol(
PersistStakingPatchBlock: cfg.PersistStakingPatchBlock,
EndorsementWithdrawWaitingBlocks: cfg.Staking.EndorsementWithdrawWaitingBlocks,
},
depositGas: depositGas,
candBucketsIndexer: candBucketsIndexer,
voteReviser: voteReviser,
patch: NewPatchStore(cfg.StakingPatchDir),
contractStakingIndexer: contractStakingIndexer,
candBucketsIndexer: candBucketsIndexer,
voteReviser: voteReviser,
patch: NewPatchStore(cfg.StakingPatchDir),
contractStakingIndexer: contractStakingIndexer,
helperCtx: helperCtx,
contractStakingIndexerV2: contractStakingIndexerV2,
}, nil
}

Expand Down Expand Up @@ -558,7 +566,14 @@ func (p *Protocol) ReadState(ctx context.Context, sr protocol.StateReader, metho
}

// stakeSR is the stake state reader including native and contract staking
stakeSR, err := newCompositeStakingStateReader(p.candBucketsIndexer, sr, p.calculateVoteWeight, p.contractStakingIndexer)
indexers := []ContractStakingIndexer{}
if p.contractStakingIndexer != nil {
indexers = append(indexers, p.contractStakingIndexer)
}
if p.contractStakingIndexerV2 != nil {
indexers = append(indexers, p.contractStakingIndexerV2)
}
stakeSR, err := newCompositeStakingStateReader(p.candBucketsIndexer, sr, p.calculateVoteWeight, indexers...)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -661,7 +676,7 @@ func (p *Protocol) settleAction(
actionCtx := protocol.MustGetActionCtx(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
gasFee := big.NewInt(0).Mul(actionCtx.GasPrice, big.NewInt(0).SetUint64(actionCtx.IntrinsicGas))
depositLog, err := p.depositGas(ctx, sm, gasFee)
depositLog, err := p.helperCtx.DepositGas(ctx, sm, gasFee)
if err != nil {
return nil, errors.Wrap(err, "failed to deposit gas")
}
Expand Down Expand Up @@ -703,8 +718,15 @@ func (p *Protocol) needToWriteCandsMap(ctx context.Context, height uint64) bool
func (p *Protocol) contractStakingVotes(ctx context.Context, candidate address.Address, height uint64) (*big.Int, error) {
featureCtx := protocol.MustGetFeatureCtx(ctx)
votes := big.NewInt(0)
indexers := []ContractStakingIndexer{}
if p.contractStakingIndexer != nil && featureCtx.AddContractStakingVotes {
btks, err := p.contractStakingIndexer.BucketsByCandidate(candidate, height)
indexers = append(indexers, p.contractStakingIndexer)
}
if p.contractStakingIndexerV2 != nil && !featureCtx.LimitedStakingContract {
indexers = append(indexers, p.contractStakingIndexerV2)
}
for _, indexer := range indexers {
btks, err := indexer.BucketsByCandidate(candidate, height)
if err != nil {
return nil, errors.Wrap(err, "failed to get BucketsByCandidate from contractStakingIndexer")
}
Expand Down
35 changes: 25 additions & 10 deletions action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ func TestProtocol(t *testing.T) {
}

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

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

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

v, err := p.Start(ctx, sm)
Expand Down Expand Up @@ -412,10 +424,13 @@ func TestProtocol_ActiveCandidates(t *testing.T) {
SelfStakingTokens: selfStake.String(),
},
}
p, err := NewProtocol(nil, &BuilderConfig{
p, err := NewProtocol(HelperCtx{
DepositGas: nil,
BlockInterval: getBlockInterval,
}, &BuilderConfig{
Staking: cfg,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, csIndexer, genesis.Default.GreenlandBlockHeight)
}, nil, csIndexer, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

blkHeight := genesis.Default.QuebecBlockHeight + 1
Expand Down
7 changes: 5 additions & 2 deletions action/protocol/staking/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import (

func initTestProtocol(t *testing.T) (*Protocol, []*Candidate) {
require := require.New(t)
p, err := NewProtocol(nil, &BuilderConfig{
p, err := NewProtocol(HelperCtx{
DepositGas: nil,
BlockInterval: getBlockInterval,
}, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, nil, genesis.Default.GreenlandBlockHeight)
}, nil, nil, 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 @@ -115,13 +115,17 @@ func TestVoteReviser(t *testing.T) {

// test loading with no candidate in stateDB
stk, err := NewProtocol(
nil,
HelperCtx{
DepositGas: nil,
BlockInterval: getBlockInterval,
},
&BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
},
nil,
nil,
nil,
genesis.Default.OkhotskBlockHeight,
genesis.Default.HawaiiBlockHeight,
genesis.Default.GreenlandBlockHeight,
Expand Down
Loading
Loading