Skip to content

Commit

Permalink
delete ErrInvalidAmount in staking
Browse files Browse the repository at this point in the history
  • Loading branch information
millken committed Dec 8, 2022
1 parent 389c53a commit 12111ae
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
20 changes: 10 additions & 10 deletions action/protocol/staking/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (d *Candidate) Equal(c *Candidate) bool {
// Validate does the sanity check
func (d *Candidate) Validate() error {
if d.Votes == nil {
return ErrInvalidAmount
return action.ErrInvalidAmount
}

if d.Name == "" {
Expand All @@ -90,7 +90,7 @@ func (d *Candidate) Validate() error {
}

if d.SelfStake == nil {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
return nil
}
Expand All @@ -115,7 +115,7 @@ func (d *Candidate) Collision(c *Candidate) error {
// AddVote adds vote
func (d *Candidate) AddVote(amount *big.Int) error {
if amount.Sign() < 0 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
d.Votes.Add(d.Votes, amount)
return nil
Expand All @@ -124,11 +124,11 @@ func (d *Candidate) AddVote(amount *big.Int) error {
// SubVote subtracts vote
func (d *Candidate) SubVote(amount *big.Int) error {
if amount.Sign() < 0 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}

if d.Votes.Cmp(amount) == -1 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
d.Votes.Sub(d.Votes, amount)
return nil
Expand All @@ -137,7 +137,7 @@ func (d *Candidate) SubVote(amount *big.Int) error {
// AddSelfStake adds self stake
func (d *Candidate) AddSelfStake(amount *big.Int) error {
if amount.Sign() < 0 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
d.SelfStake.Add(d.SelfStake, amount)
return nil
Expand All @@ -146,11 +146,11 @@ func (d *Candidate) AddSelfStake(amount *big.Int) error {
// SubSelfStake subtracts self stake
func (d *Candidate) SubSelfStake(amount *big.Int) error {
if amount.Sign() < 0 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}

if d.Votes.Cmp(amount) == -1 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
d.SelfStake.Sub(d.SelfStake, amount)
return nil
Expand Down Expand Up @@ -216,13 +216,13 @@ func (d *Candidate) fromProto(pb *stakingpb.Candidate) error {
var ok bool
d.Votes, ok = new(big.Int).SetString(pb.GetVotes(), 10)
if !ok {
return ErrInvalidAmount
return action.ErrInvalidAmount
}

d.SelfStakeBucketIdx = pb.GetSelfStakeBucketIdx()
d.SelfStake, ok = new(big.Int).SetString(pb.GetSelfStake(), 10)
if !ok {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
return nil
}
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 @@ -852,7 +852,7 @@ func csmErrorToHandleError(caller string, err error) error {
case ErrInvalidSelfStkIndex:
hErr.failureStatus = iotextypes.ReceiptStatus_ErrCandidateConflict
return hErr
case ErrInvalidAmount:
case action.ErrInvalidAmount:
hErr.failureStatus = iotextypes.ReceiptStatus_ErrCandidateNotExist
return hErr
case ErrInvalidOwner:
Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
1,
time.Now(),
10000,
ErrInvalidAmount,
action.ErrInvalidAmount,
iotextypes.ReceiptStatus_Failure,
},
{
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestProtocol_HandleCandidateRegister(t *testing.T) {
uint64(1000000),
big.NewInt(1),
true,
ErrInvalidAmount,
action.ErrInvalidAmount,
iotextypes.ReceiptStatus_Failure,
},
// invalid candidate name
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ func NewProtocol(

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

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

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

// new vote reviser, revise ate greenland
Expand Down Expand Up @@ -231,7 +231,7 @@ func (p *Protocol) CreateGenesisStates(

selfStake, ok := new(big.Int).SetString(bc.SelfStakingTokens, 10)
if !ok {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
bucket := NewVoteBucket(owner, owner, selfStake, 7, time.Now(), true)
bucketIdx, err := csm.putBucketAndIndex(bucket)
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func Test_CreateGenesisStates(t *testing.T) {
SelfStakingTokens: "test123",
},
},
"invalid staking amount",
"invalid amount",
},
{
[]genesis.BootstrapCandidate{
Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/receipt_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestReceiptLog(t *testing.T) {
log.AddAddress(v.cand)
log.AddAddress(v.voter)
log.SetData(v.data)
r.Nil(log.Build(ctx, ErrInvalidAmount))
r.Nil(log.Build(ctx, action.ErrInvalidAmount))
r.Equal(createLog(ctx, v.name, v.cand, v.voter, v.data), log.Build(ctx, nil))

log = newReceiptLog(v.addr, v.name, true)
Expand All @@ -144,7 +144,7 @@ func TestReceiptLog(t *testing.T) {
for i := range v.topics {
postFb.Topics = append(postFb.Topics, hash.BytesToHash256(v.topics[i]))
}
r.Equal(postFb, log.Build(ctx, ErrInvalidAmount))
r.Equal(postFb, log.Build(ctx, action.ErrInvalidAmount))
r.Equal(postFb, log.Build(ctx, nil))
}
}
Expand Down
5 changes: 2 additions & 3 deletions action/protocol/staking/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

// Errors
var (
ErrInvalidAmount = errors.New("invalid staking amount")
ErrInvalidOwner = errors.New("invalid owner address")
ErrInvalidOperator = errors.New("invalid operator address")
ErrInvalidReward = errors.New("invalid reward address")
Expand All @@ -30,7 +29,7 @@ func (p *Protocol) validateCreateStake(ctx context.Context, act *action.CreateSt
return action.ErrInvalidCanName
}
if act.Amount().Cmp(p.config.MinStakeAmount) == -1 {
return errors.Wrap(ErrInvalidAmount, "stake amount is less than the minimum requirement")
return errors.Wrap(action.ErrInvalidAmount, "stake amount is less than the minimum requirement")
}
return nil
}
Expand Down Expand Up @@ -68,7 +67,7 @@ func (p *Protocol) validateCandidateRegister(ctx context.Context, act *action.Ca
}

if act.Amount().Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 {
return errors.Wrap(ErrInvalidAmount, "self staking amount is not valid")
return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid")
}
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions action/protocol/staking/vote_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
Expand Down Expand Up @@ -69,11 +70,11 @@ func (vb *VoteBucket) Deserialize(buf []byte) error {
func (vb *VoteBucket) fromProto(pb *stakingpb.Bucket) error {
vote, ok := new(big.Int).SetString(pb.GetStakedAmount(), 10)
if !ok {
return ErrInvalidAmount
return action.ErrInvalidAmount
}

if vote.Sign() <= 0 {
return ErrInvalidAmount
return action.ErrInvalidAmount
}

candAddr, err := address.FromString(pb.GetCandidateAddress())
Expand Down

0 comments on commit 12111ae

Please sign in to comment.