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] Cannot ChangeCandidate when Bucket is Endorsed #4133

Merged
merged 7 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions action/protocol/staking/handler_candidate_endorsement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,33 @@ func TestProtocol_HandleCandidateEndorsement(t *testing.T) {
[]expectCandidate{},
nil,
},
{
"once endorsed, bucket cannot be change candidate",
[]uint64{0, 10},
[]uint64{0, 1, 2},
1300000,
identityset.Address(1),
1,
uint64(1000000),
uint64(1000000),
big.NewInt(1000),
1,
true,
true,
&appendAction{
func() action.Action {
act, err := action.NewChangeCandidate(0, "test3", 1, []byte{}, uint64(1000000), big.NewInt(1000))
require.NoError(err)
return act
},
iotextypes.ReceiptStatus_ErrInvalidBucketType, //todo fix
nil,
},
nil,
iotextypes.ReceiptStatus_Success,
[]expectCandidate{},
nil,
},
{
"unendorse a valid bucket",
[]uint64{0, 9},
Expand Down
10 changes: 10 additions & 0 deletions action/protocol/staking/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func (p *Protocol) handleChangeCandidate(ctx context.Context, act *action.Change
) (*receiptLog, error) {
actionCtx := protocol.MustGetActionCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
log := newReceiptLog(p.addr.String(), HandleChangeCandidate, featureCtx.NewStakingReceiptFormat)

_, fetchErr := fetchCaller(ctx, csm, big.NewInt(0))
Expand All @@ -306,6 +307,9 @@ func (p *Protocol) handleChangeCandidate(ctx context.Context, act *action.Change
if fetchErr != nil {
return log, fetchErr
}
if rErr := validateBucketEndorsement(NewEndorsementStateManager(csm.SM()), bucket, false, blkCtx.BlockHeight); rErr != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. should this be protected by hard-fork flag?
  2. if bucket status = UnEndorsing, it will return nil. This case is considered OK to change candidate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I think not required, b/c there should be no endorsement before HF, so the validation should pass
  2. It actually returns error if bucket is UnEndorsing

return log, rErr
}
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes(), candidate.Owner.Bytes())

prevCandidate := csm.GetByOwner(bucket.Candidate)
Expand Down Expand Up @@ -349,6 +353,12 @@ func (p *Protocol) handleChangeCandidate(ctx context.Context, act *action.Change
failureStatus: iotextypes.ReceiptStatus_ErrNotEnoughBalance,
}
}
// if the bucket equals to the previous candidate's self-stake bucket, it must be expired endorse bucket
// so we need to clear the self-stake of the previous candidate
if !featureCtx.DisableDelegateEndorsement && prevCandidate.SelfStakeBucketIdx == bucket.Index {
prevCandidate.SelfStake.SetInt64(0)
prevCandidate.SelfStakeBucketIdx = candidateNoSelfStakeBucketIndex
}
if err := csm.Upsert(prevCandidate); err != nil {
return log, csmErrorToHandleError(prevCandidate.Owner.String(), err)
}
Expand Down
136 changes: 136 additions & 0 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/mohae/deepcopy"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -1699,6 +1700,141 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) {
}
}

func TestProtocol_HandleChangeCandidate_ClearPrevCandidateSelfStake(t *testing.T) {
r := require.New(t)
ctrl := gomock.NewController(t)
t.Run("clear if bucket is an expired endorse bucket", func(t *testing.T) {
sm, p, buckets, _ := initTestStateWithHeight(t, ctrl,
[]*bucketConfig{
{identityset.Address(1), identityset.Address(5), "100000000000000000000", 1, true, true, nil, 1},
},
[]*candidateConfig{
{identityset.Address(1), identityset.Address(11), identityset.Address(21), "test1"},
{identityset.Address(2), identityset.Address(12), identityset.Address(22), "test2"},
}, 1)
r.NoError(setupAccount(sm, identityset.Address(5), 10000))
nonce := uint64(1)
act, err := action.NewChangeCandidate(nonce, "test2", buckets[0].Index, nil, 10000, big.NewInt(unit.Qev))
r.NoError(err)
intrinsic, err := act.IntrinsicGas()
r.NoError(err)
ctx := context.Background()
g := deepcopy.Copy(genesis.Default).(genesis.Genesis)
g.ToBeEnabledBlockHeight = 0
ctx = genesis.WithGenesisContext(ctx, g)
ctx = protocol.WithActionCtx(ctx, protocol.ActionCtx{
Caller: identityset.Address(5),
GasPrice: big.NewInt(unit.Qev),
IntrinsicGas: intrinsic,
Nonce: nonce,
})
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: 2,
BlockTimeStamp: time.Now(),
GasLimit: 1000000,
})
ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx))
recipt, err := p.Handle(ctx, act, sm)
r.NoError(err)
r.EqualValues(iotextypes.ReceiptStatus_Success, recipt.Status)
// test previous candidate self stake
csm, err := NewCandidateStateManager(sm, false)
r.NoError(err)
prevCand := csm.GetByOwner(identityset.Address(1))
r.Equal("0", prevCand.SelfStake.String())
r.EqualValues(uint64(candidateNoSelfStakeBucketIndex), prevCand.SelfStakeBucketIdx)
r.Equal("0", prevCand.Votes.String())
})
t.Run("not clear if bucket is a vote bucket", func(t *testing.T) {
sm, p, buckets, _ := initTestStateWithHeight(t, ctrl,
[]*bucketConfig{
{identityset.Address(1), identityset.Address(1), "120000000000000000000", 1, true, true, nil, 0},
{identityset.Address(2), identityset.Address(2), "120000000000000000000", 1, true, true, nil, 0},
{identityset.Address(1), identityset.Address(1), "100000000000000000000", 1, true, false, nil, 0},
},
[]*candidateConfig{
{identityset.Address(1), identityset.Address(11), identityset.Address(21), "test1"},
{identityset.Address(2), identityset.Address(12), identityset.Address(22), "test2"},
}, 1)
r.NoError(setupAccount(sm, identityset.Address(1), 10000))
nonce := uint64(1)
act, err := action.NewChangeCandidate(nonce, "test2", buckets[2].Index, nil, 10000, big.NewInt(unit.Qev))
r.NoError(err)
intrinsic, err := act.IntrinsicGas()
r.NoError(err)
ctx := context.Background()
g := deepcopy.Copy(genesis.Default).(genesis.Genesis)
g.ToBeEnabledBlockHeight = 0
ctx = genesis.WithGenesisContext(ctx, g)
ctx = protocol.WithActionCtx(ctx, protocol.ActionCtx{
Caller: identityset.Address(1),
GasPrice: big.NewInt(unit.Qev),
IntrinsicGas: intrinsic,
Nonce: nonce,
})
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: 2,
BlockTimeStamp: time.Now(),
GasLimit: 1000000,
})
ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx))
recipt, err := p.Handle(ctx, act, sm)
r.NoError(err)
r.EqualValues(iotextypes.ReceiptStatus_Success, recipt.Status)
// test previous candidate self stake
csm, err := NewCandidateStateManager(sm, false)
r.NoError(err)
prevCand := csm.GetByOwner(buckets[2].Candidate)
r.Equal("120000000000000000000", prevCand.SelfStake.String())
r.EqualValues(0, prevCand.SelfStakeBucketIdx)
r.Equal("124562140820308711042", prevCand.Votes.String())
})
t.Run("not clear if bucket is a vote bucket", func(t *testing.T) {
sm, p, buckets, _ := initTestStateWithHeight(t, ctrl,
[]*bucketConfig{
{identityset.Address(1), identityset.Address(1), "120000000000000000000", 1, true, true, nil, 0},
{identityset.Address(2), identityset.Address(2), "120000000000000000000", 1, true, true, nil, 0},
{identityset.Address(1), identityset.Address(1), "100000000000000000000", 1, true, false, nil, 0},
},
[]*candidateConfig{
{identityset.Address(1), identityset.Address(11), identityset.Address(21), "test1"},
{identityset.Address(2), identityset.Address(12), identityset.Address(22), "test2"},
}, 1)
r.NoError(setupAccount(sm, identityset.Address(1), 10000))
nonce := uint64(1)
act, err := action.NewChangeCandidate(nonce, "test2", buckets[2].Index, nil, 10000, big.NewInt(unit.Qev))
r.NoError(err)
intrinsic, err := act.IntrinsicGas()
r.NoError(err)
ctx := context.Background()
g := deepcopy.Copy(genesis.Default).(genesis.Genesis)
g.ToBeEnabledBlockHeight = 0
ctx = genesis.WithGenesisContext(ctx, g)
ctx = protocol.WithActionCtx(ctx, protocol.ActionCtx{
Caller: identityset.Address(1),
GasPrice: big.NewInt(unit.Qev),
IntrinsicGas: intrinsic,
Nonce: nonce,
})
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: 2,
BlockTimeStamp: time.Now(),
GasLimit: 1000000,
})
ctx = protocol.WithFeatureCtx(protocol.WithFeatureWithHeightCtx(ctx))
recipt, err := p.Handle(ctx, act, sm)
r.NoError(err)
r.EqualValues(iotextypes.ReceiptStatus_Success, recipt.Status)
// test previous candidate self stake
csm, err := NewCandidateStateManager(sm, false)
r.NoError(err)
prevCand := csm.GetByOwner(buckets[2].Candidate)
r.Equal("120000000000000000000", prevCand.SelfStake.String())
r.EqualValues(0, prevCand.SelfStakeBucketIdx)
r.Equal("124562140820308711042", prevCand.Votes.String())
})
}

func TestProtocol_HandleTransferStake(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
Expand Down
Loading