-
Notifications
You must be signed in to change notification settings - Fork 324
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] Candidate Register without Staking #4059
Changes from 3 commits
9eeb590
f024880
fca42c3
0179f56
163be84
8c609b3
ff11122
2f7bed0
9a01979
be85af5
ffc9fb7
ef0e99a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -663,10 +663,26 @@ | |
} | ||
} | ||
|
||
bucket := NewVoteBucket(owner, owner, act.Amount(), act.Duration(), blkCtx.BlockTimeStamp, act.AutoStake()) | ||
bucketIdx, err := csm.putBucketAndIndex(bucket) | ||
if err != nil { | ||
return log, nil, err | ||
// register with self-stake | ||
var ( | ||
bucketIdx = uint64(candidateNoSelfStakeBucketIndex) | ||
txLogs []*action.TransactionLog | ||
votes = big.NewInt(0) | ||
err error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) | ||
if act.Amount().Sign() > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no hardfork here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before the hard fork, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if withSelfStake { |
||
bucket := NewVoteBucket(owner, owner, act.Amount(), act.Duration(), blkCtx.BlockTimeStamp, act.AutoStake()) | ||
bucketIdx, err = csm.putBucketAndIndex(bucket) | ||
if err != nil { | ||
return log, nil, err | ||
} | ||
txLogs = append(txLogs, &action.TransactionLog{ | ||
Type: iotextypes.TransactionLogType_CANDIDATE_SELF_STAKE, | ||
Sender: actCtx.Caller.String(), | ||
Recipient: address.StakingBucketPoolAddr, | ||
Amount: act.Amount(), | ||
}) | ||
votes = p.calculateVoteWeight(bucket, true) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think adding an else make the code more explicit
|
||
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucketIdx), owner.Bytes()) | ||
|
||
|
@@ -675,7 +691,7 @@ | |
Operator: act.OperatorAddress(), | ||
Reward: act.RewardAddress(), | ||
Name: act.Name(), | ||
Votes: p.calculateVoteWeight(bucket, true), | ||
Votes: votes, | ||
SelfStakeBucketIdx: bucketIdx, | ||
SelfStake: act.Amount(), | ||
} | ||
|
@@ -688,49 +704,43 @@ | |
csm.DirtyView().candCenter.base.recordOwner(c) | ||
} | ||
|
||
// update bucket pool | ||
if err := csm.DebitBucketPool(act.Amount(), true); err != nil { | ||
return log, nil, &handleError{ | ||
err: errors.Wrapf(err, "failed to update staking bucket pool %s", err.Error()), | ||
failureStatus: iotextypes.ReceiptStatus_ErrWriteAccount, | ||
if act.Amount().Sign() > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if withSelfStake { |
||
// update bucket pool | ||
if err := csm.DebitBucketPool(act.Amount(), true); err != nil { | ||
return log, nil, &handleError{ | ||
err: errors.Wrapf(err, "failed to update staking bucket pool %s", err.Error()), | ||
failureStatus: iotextypes.ReceiptStatus_ErrWriteAccount, | ||
} | ||
} | ||
} | ||
|
||
// update caller balance | ||
if err := caller.SubBalance(act.Amount()); err != nil { | ||
return log, nil, &handleError{ | ||
err: errors.Wrapf(err, "failed to update the balance of register %s", actCtx.Caller.String()), | ||
failureStatus: iotextypes.ReceiptStatus_ErrNotEnoughBalance, | ||
// update caller balance | ||
if err := caller.SubBalance(act.Amount()); err != nil { | ||
return log, nil, &handleError{ | ||
err: errors.Wrapf(err, "failed to update the balance of register %s", actCtx.Caller.String()), | ||
failureStatus: iotextypes.ReceiptStatus_ErrNotEnoughBalance, | ||
} | ||
} | ||
// put updated caller's account state to trie | ||
if err := accountutil.StoreAccount(csm.SM(), actCtx.Caller, caller); err != nil { | ||
return log, nil, errors.Wrapf(err, "failed to store account %s", actCtx.Caller.String()) | ||
} | ||
} | ||
// put updated caller's account state to trie | ||
if err := accountutil.StoreAccount(csm.SM(), actCtx.Caller, caller); err != nil { | ||
return log, nil, errors.Wrapf(err, "failed to store account %s", actCtx.Caller.String()) | ||
} | ||
|
||
// put registrationFee to reward pool | ||
if _, err = p.depositGas(ctx, csm.SM(), registrationFee); err != nil { | ||
if _, err := p.depositGas(ctx, csm.SM(), registrationFee); err != nil { | ||
return log, nil, errors.Wrap(err, "failed to deposit gas") | ||
} | ||
|
||
log.AddAddress(owner) | ||
log.AddAddress(actCtx.Caller) | ||
log.SetData(byteutil.Uint64ToBytesBigEndian(bucketIdx)) | ||
|
||
return log, []*action.TransactionLog{ | ||
{ | ||
Type: iotextypes.TransactionLogType_CANDIDATE_SELF_STAKE, | ||
Sender: actCtx.Caller.String(), | ||
Recipient: address.StakingBucketPoolAddr, | ||
Amount: act.Amount(), | ||
}, | ||
{ | ||
Type: iotextypes.TransactionLogType_CANDIDATE_REGISTRATION_FEE, | ||
Sender: actCtx.Caller.String(), | ||
Recipient: address.RewardingPoolAddr, | ||
Amount: registrationFee, | ||
}, | ||
}, nil | ||
txLogs = append(txLogs, &action.TransactionLog{ | ||
Type: iotextypes.TransactionLogType_CANDIDATE_REGISTRATION_FEE, | ||
Sender: actCtx.Caller.String(), | ||
Recipient: address.RewardingPoolAddr, | ||
Amount: registrationFee, | ||
}) | ||
return log, txLogs, nil | ||
} | ||
|
||
func (p *Protocol) handleCandidateUpdate(ctx context.Context, act *action.CandidateUpdate, csm CandidateStateManager, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,14 @@ package staking | |
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
|
||
"github.com/iotexproject/iotex-core/action" | ||
"github.com/iotexproject/iotex-core/action/protocol" | ||
"github.com/iotexproject/iotex-core/pkg/log" | ||
) | ||
|
||
// Errors | ||
|
@@ -66,7 +70,11 @@ func (p *Protocol) validateCandidateRegister(ctx context.Context, act *action.Ca | |
} | ||
|
||
if act.Amount().Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 { | ||
return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid") | ||
featureCtx := protocol.MustGetFeatureCtx(ctx) | ||
if featureCtx.CandidateRegisterMustWithStake || act.Amount().Cmp(big.NewInt(0)) != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !featureCtx.CandidateRegisterMustWithStake && act.Amount()==0 is easier to read than featureCtx.CandidateRegisterMustWithStake || act.Amount().Cmp(big.NewInt(0)) != 0 |
||
log.L().Warn("Candidate register amount is less than the minimum requirement", zap.Bool("feature", featureCtx.CandidateRegisterMustWithStake), zap.String("amount", act.Amount().String())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think the error has been logged upstream |
||
return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid") | ||
} | ||
} | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to L713