From 3a231c394a3e91e3666cf88c01465e44250cd5ce Mon Sep 17 00:00:00 2001 From: millken Date: Thu, 8 Dec 2022 15:52:42 +0800 Subject: [PATCH] move IsValidCandidateName to action package (#3706) * move IsValidCandidateName to action package * move IsValidCandidateName to action package * delete validations.go * update tests * delete ErrInvalidAmount in staking --- action/candidate_register.go | 16 +++++++ action/candidateregister_test.go | 46 +++++++++++++++++++ action/protocol/staking/candidate.go | 25 +++++----- action/protocol/staking/candidate_center.go | 3 +- .../protocol/staking/candidate_center_test.go | 11 +++-- action/protocol/staking/candidate_test.go | 3 +- action/protocol/staking/handlers.go | 6 +-- action/protocol/staking/handlers_test.go | 16 +++---- action/protocol/staking/protocol.go | 8 ++-- action/protocol/staking/protocol_test.go | 2 +- action/protocol/staking/receipt_log_test.go | 4 +- action/protocol/staking/validations.go | 35 ++++---------- action/protocol/staking/validations_test.go | 46 ------------------- action/protocol/staking/vote_bucket.go | 5 +- ioctl/cmd/action/stake2change.go | 5 +- ioctl/cmd/action/stake2create.go | 5 +- ioctl/cmd/action/stake2register.go | 5 +- ioctl/cmd/action/stake2update.go | 5 +- ioctl/validator/validator.go | 17 ------- 19 files changed, 124 insertions(+), 139 deletions(-) diff --git a/action/candidate_register.go b/action/candidate_register.go index 9de45207a6..e03370c838 100644 --- a/action/candidate_register.go +++ b/action/candidate_register.go @@ -87,6 +87,9 @@ var ( // ErrInvalidAmount represents that amount is 0 or negative ErrInvalidAmount = errors.New("invalid amount") + + //ErrInvalidCanName represents that candidate name is invalid + ErrInvalidCanName = errors.New("invalid candidate name") ) // CandidateRegister is the action to register a candidate @@ -380,3 +383,16 @@ func (cr *CandidateRegister) ToEthTx() (*types.Transaction, error) { } return types.NewTransaction(cr.Nonce(), ethAddr, big.NewInt(0), cr.GasLimit(), cr.GasPrice(), data), nil } + +// IsValidCandidateName check if a candidate name string is valid. +func IsValidCandidateName(s string) bool { + if len(s) == 0 || len(s) > 12 { + return false + } + for _, c := range s { + if !(('a' <= c && c <= 'z') || ('0' <= c && c <= '9')) { + return false + } + } + return true +} diff --git a/action/candidateregister_test.go b/action/candidateregister_test.go index c1fb35affd..0e9ab6c5f7 100644 --- a/action/candidateregister_test.go +++ b/action/candidateregister_test.go @@ -146,3 +146,49 @@ func TestCandidateRegisterABIEncodeAndDecode(t *testing.T) { require.Equal(test.AutoStake, stake.AutoStake()) require.Equal(test.Payload, stake.Payload()) } + +func TestIsValidCandidateName(t *testing.T) { + require := require.New(t) + tests := []struct { + input string + output bool + }{ + { + input: "abc", + output: true, + }, + { + input: "123", + output: true, + }, + { + input: "abc123abc123", + output: true, + }, + { + input: "Abc123", + output: false, + }, + { + input: "Abc 123", + output: false, + }, + { + input: "Abc-123", + output: false, + }, + { + input: "abc123abc123abc123", + output: false, + }, + { + input: "", + output: false, + }, + } + + for _, tt := range tests { + output := IsValidCandidateName(tt.input) + require.Equal(tt.output, output) + } +} diff --git a/action/protocol/staking/candidate.go b/action/protocol/staking/candidate.go index 65802f631d..e5d7e63c00 100644 --- a/action/protocol/staking/candidate.go +++ b/action/protocol/staking/candidate.go @@ -16,6 +16,7 @@ import ( "github.com/pkg/errors" "google.golang.org/protobuf/proto" + "github.com/iotexproject/iotex-core/action" "github.com/iotexproject/iotex-core/action/protocol/staking/stakingpb" "github.com/iotexproject/iotex-core/state" ) @@ -69,11 +70,11 @@ 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 == "" { - return ErrInvalidCanName + return action.ErrInvalidCanName } if d.Owner == nil { @@ -89,7 +90,7 @@ func (d *Candidate) Validate() error { } if d.SelfStake == nil { - return ErrInvalidAmount + return action.ErrInvalidAmount } return nil } @@ -100,7 +101,7 @@ func (d *Candidate) Collision(c *Candidate) error { return nil } if c.Name == d.Name { - return ErrInvalidCanName + return action.ErrInvalidCanName } if address.Equal(c.Operator, d.Operator) { return ErrInvalidOperator @@ -114,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 @@ -123,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 @@ -136,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 @@ -145,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 @@ -215,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 } diff --git a/action/protocol/staking/candidate_center.go b/action/protocol/staking/candidate_center.go index 706e6c3bba..22ca441ca9 100644 --- a/action/protocol/staking/candidate_center.go +++ b/action/protocol/staking/candidate_center.go @@ -11,6 +11,7 @@ import ( "github.com/iotexproject/iotex-address/address" + "github.com/iotexproject/iotex-core/action" "github.com/iotexproject/iotex-core/action/protocol" ) @@ -294,7 +295,7 @@ func (m *CandidateCenter) collision(d *Candidate) error { name, oper, self := m.base.collision(d) if name != nil && !m.change.containsOwner(name) { - return ErrInvalidCanName + return action.ErrInvalidCanName } if oper != nil && !m.change.containsOwner(oper) { diff --git a/action/protocol/staking/candidate_center_test.go b/action/protocol/staking/candidate_center_test.go index bba2da69c7..883c252d5f 100644 --- a/action/protocol/staking/candidate_center_test.go +++ b/action/protocol/staking/candidate_center_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/iotexproject/iotex-core/action" "github.com/iotexproject/iotex-core/action/protocol" "github.com/iotexproject/iotex-core/pkg/unit" "github.com/iotexproject/iotex-core/test/identityset" @@ -241,7 +242,7 @@ func TestCandCenter(t *testing.T) { // d1 conflict with existing conflict := list[(u1+3)%size] d1.Name = conflict.Name - r.Equal(ErrInvalidCanName, m.Upsert(d1)) + r.Equal(action.ErrInvalidCanName, m.Upsert(d1)) d1.Name = name1 d1.Operator = conflict.Operator r.Equal(ErrInvalidOperator, m.Upsert(d1)) @@ -261,7 +262,7 @@ func TestCandCenter(t *testing.T) { // d2 conflict d1 d2.Name = d1.Name - r.Equal(ErrInvalidCanName, m.Upsert(d2)) + r.Equal(action.ErrInvalidCanName, m.Upsert(d2)) d2.Name = name2 d2.Operator = d1.Operator r.Equal(ErrInvalidOperator, m.Upsert(d2)) @@ -278,7 +279,7 @@ func TestCandCenter(t *testing.T) { // new n1 conflict with existing n1 := list[(u2+size/2)%size].Clone() n1.Owner = identityset.Address(15 + i*2) - r.Equal(ErrInvalidCanName, m.Upsert(n1)) + r.Equal(action.ErrInvalidCanName, m.Upsert(n1)) n1.Name = name1 r.Equal(ErrInvalidOperator, m.Upsert(n1)) n1.Operator = op1 @@ -290,7 +291,7 @@ func TestCandCenter(t *testing.T) { // new n2 conflict with dirty d2 n2 := d2.Clone() n2.Owner = identityset.Address(16 + i*2) - r.Equal(ErrInvalidCanName, m.Upsert(n2)) + r.Equal(action.ErrInvalidCanName, m.Upsert(n2)) n2.Name = name2 r.Equal(ErrInvalidOperator, m.Upsert(n2)) n2.Operator = op2 @@ -302,7 +303,7 @@ func TestCandCenter(t *testing.T) { // verify conflict with n1 n2 = n1.Clone() n2.Owner = identityset.Address(0) - r.Equal(ErrInvalidCanName, m.Upsert(n2)) + r.Equal(action.ErrInvalidCanName, m.Upsert(n2)) n2.Name = "noconflict" r.Equal(ErrInvalidOperator, m.Upsert(n2)) n2.Operator = identityset.Address(0) diff --git a/action/protocol/staking/candidate_test.go b/action/protocol/staking/candidate_test.go index 49d9edcb31..0559cbcd77 100644 --- a/action/protocol/staking/candidate_test.go +++ b/action/protocol/staking/candidate_test.go @@ -14,6 +14,7 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/require" + "github.com/iotexproject/iotex-core/action" "github.com/iotexproject/iotex-core/pkg/unit" "github.com/iotexproject/iotex-core/state" "github.com/iotexproject/iotex-core/test/identityset" @@ -89,7 +90,7 @@ func TestClone(t *testing.T) { r.False(d.Equal(d2)) r.NoError(d.Collision(d2)) d.Owner = identityset.Address(0) - r.Equal(ErrInvalidCanName, d.Collision(d2)) + r.Equal(action.ErrInvalidCanName, d.Collision(d2)) d.Name = "noconflict" r.Equal(ErrInvalidOperator, d.Collision(d2)) d.Operator = identityset.Address(0) diff --git a/action/protocol/staking/handlers.go b/action/protocol/staking/handlers.go index 9a16245057..37a79e59d1 100644 --- a/action/protocol/staking/handlers.go +++ b/action/protocol/staking/handlers.go @@ -651,7 +651,7 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand // cannot collide with existing name if csm.ContainsName(act.Name()) && (!ownerExist || act.Name() != c.Name) { return log, nil, &handleError{ - err: ErrInvalidCanName, + err: action.ErrInvalidCanName, failureStatus: iotextypes.ReceiptStatus_ErrCandidateConflict, } } @@ -843,7 +843,7 @@ func csmErrorToHandleError(caller string, err error) error { } switch errors.Cause(err) { - case ErrInvalidCanName: + case action.ErrInvalidCanName: hErr.failureStatus = iotextypes.ReceiptStatus_ErrCandidateConflict return hErr case ErrInvalidOperator: @@ -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: diff --git a/action/protocol/staking/handlers_test.go b/action/protocol/staking/handlers_test.go index a13a8425f8..3713bdc625 100644 --- a/action/protocol/staking/handlers_test.go +++ b/action/protocol/staking/handlers_test.go @@ -147,7 +147,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) { 1, time.Now(), 10000, - ErrInvalidCanName, + action.ErrInvalidCanName, iotextypes.ReceiptStatus_ErrCandidateNotExist, }, { @@ -162,7 +162,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) { 1, time.Now(), 10000, - ErrInvalidAmount, + action.ErrInvalidAmount, iotextypes.ReceiptStatus_Failure, }, { @@ -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 @@ -379,7 +379,7 @@ func TestProtocol_HandleCandidateRegister(t *testing.T) { uint64(1000000), big.NewInt(1), true, - ErrInvalidCanName, + action.ErrInvalidCanName, iotextypes.ReceiptStatus_Failure, }, // success for the following test @@ -762,7 +762,7 @@ func TestProtocol_HandleCandidateUpdate(t *testing.T) { "!invalidname", identityset.Address(31).String(), identityset.Address(32).String(), - ErrInvalidCanName, + action.ErrInvalidCanName, iotextypes.ReceiptStatus_Failure, }, // success,update name, operator and reward address @@ -1522,7 +1522,7 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) { time.Now(), 10000, false, - ErrInvalidCanName, + action.ErrInvalidCanName, iotextypes.ReceiptStatus_Failure, }, // invalid candidate name 2 @@ -1542,7 +1542,7 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) { time.Now(), 10000, false, - ErrInvalidCanName, + action.ErrInvalidCanName, iotextypes.ReceiptStatus_Failure, }, // invalid candidate name 3 @@ -1562,7 +1562,7 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) { time.Now(), 10000, false, - ErrInvalidCanName, + action.ErrInvalidCanName, iotextypes.ReceiptStatus_Failure, }, // Upsert error cannot happen,because CreateStake already check collision diff --git a/action/protocol/staking/protocol.go b/action/protocol/staking/protocol.go index a6833b46e0..4af41f2776 100644 --- a/action/protocol/staking/protocol.go +++ b/action/protocol/staking/protocol.go @@ -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 @@ -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) diff --git a/action/protocol/staking/protocol_test.go b/action/protocol/staking/protocol_test.go index 6caff1139d..0ba31d9055 100644 --- a/action/protocol/staking/protocol_test.go +++ b/action/protocol/staking/protocol_test.go @@ -341,7 +341,7 @@ func Test_CreateGenesisStates(t *testing.T) { SelfStakingTokens: "test123", }, }, - "invalid staking amount", + "invalid amount", }, { []genesis.BootstrapCandidate{ diff --git a/action/protocol/staking/receipt_log_test.go b/action/protocol/staking/receipt_log_test.go index 52e99afd9a..1f24a7a782 100644 --- a/action/protocol/staking/receipt_log_test.go +++ b/action/protocol/staking/receipt_log_test.go @@ -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) @@ -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)) } } diff --git a/action/protocol/staking/validations.go b/action/protocol/staking/validations.go index 87abfeab60..de16121631 100644 --- a/action/protocol/staking/validations.go +++ b/action/protocol/staking/validations.go @@ -16,8 +16,6 @@ import ( // Errors var ( - ErrInvalidAmount = errors.New("invalid staking amount") - ErrInvalidCanName = errors.New("invalid candidate name") ErrInvalidOwner = errors.New("invalid owner address") ErrInvalidOperator = errors.New("invalid operator address") ErrInvalidReward = errors.New("invalid reward address") @@ -27,11 +25,11 @@ var ( ) func (p *Protocol) validateCreateStake(ctx context.Context, act *action.CreateStake) error { - if !isValidCandidateName(act.Candidate()) { - return ErrInvalidCanName + if !action.IsValidCandidateName(act.Candidate()) { + 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 } @@ -45,8 +43,8 @@ func (p *Protocol) validateWithdrawStake(ctx context.Context, act *action.Withdr } func (p *Protocol) validateChangeCandidate(ctx context.Context, act *action.ChangeCandidate) error { - if !isValidCandidateName(act.Candidate()) { - return ErrInvalidCanName + if !action.IsValidCandidateName(act.Candidate()) { + return action.ErrInvalidCanName } return nil } @@ -64,34 +62,21 @@ func (p *Protocol) validateRestake(ctx context.Context, act *action.Restake) err } func (p *Protocol) validateCandidateRegister(ctx context.Context, act *action.CandidateRegister) error { - if !isValidCandidateName(act.Name()) { - return ErrInvalidCanName + if !action.IsValidCandidateName(act.Name()) { + return action.ErrInvalidCanName } 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 } func (p *Protocol) validateCandidateUpdate(ctx context.Context, act *action.CandidateUpdate) error { if len(act.Name()) != 0 { - if !isValidCandidateName(act.Name()) { - return ErrInvalidCanName + if !action.IsValidCandidateName(act.Name()) { + return action.ErrInvalidCanName } } return nil } - -// IsValidCandidateName check if a candidate name string is valid. -func isValidCandidateName(s string) bool { - if len(s) == 0 || len(s) > 12 { - return false - } - for _, c := range s { - if !(('a' <= c && c <= 'z') || ('0' <= c && c <= '9')) { - return false - } - } - return true -} diff --git a/action/protocol/staking/validations_test.go b/action/protocol/staking/validations_test.go index c9747a7c64..09ea63aeab 100644 --- a/action/protocol/staking/validations_test.go +++ b/action/protocol/staking/validations_test.go @@ -11,58 +11,12 @@ import ( "math/big" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/blockchain/genesis" "github.com/iotexproject/iotex-core/test/identityset" ) -func TestIsValidCandidateName(t *testing.T) { - tests := []struct { - input string - output bool - }{ - { - input: "abc", - output: true, - }, - { - input: "123", - output: true, - }, - { - input: "abc123abc123", - output: true, - }, - { - input: "Abc123", - output: false, - }, - { - input: "Abc 123", - output: false, - }, - { - input: "Abc-123", - output: false, - }, - { - input: "abc123abc123abc123", - output: false, - }, - { - input: "", - output: false, - }, - } - - for _, tt := range tests { - output := isValidCandidateName(tt.input) - assert.Equal(t, tt.output, output) - } -} - func initTestProtocol(t *testing.T) (*Protocol, []*Candidate) { require := require.New(t) p, err := NewProtocol(nil, &BuilderConfig{ diff --git a/action/protocol/staking/vote_bucket.go b/action/protocol/staking/vote_bucket.go index 3b4b736168..01b758dd3d 100644 --- a/action/protocol/staking/vote_bucket.go +++ b/action/protocol/staking/vote_bucket.go @@ -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" @@ -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()) diff --git a/ioctl/cmd/action/stake2change.go b/ioctl/cmd/action/stake2change.go index 50f28e47ce..f1531771a6 100644 --- a/ioctl/cmd/action/stake2change.go +++ b/ioctl/cmd/action/stake2change.go @@ -15,7 +15,6 @@ import ( "github.com/iotexproject/iotex-core/action" "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/output" - "github.com/iotexproject/iotex-core/ioctl/validator" ) // Multi-language support @@ -50,8 +49,8 @@ func init() { func stake2Change(args []string) error { var candidateName = args[0] - if err := validator.ValidateCandidateNameForStake2(candidateName); err != nil { - return output.NewError(output.ValidationError, "invalid candidate name", err) + if !action.IsValidCandidateName(candidateName) { + return output.NewError(output.ValidationError, "", action.ErrInvalidCanName) } bucketIndex, err := strconv.ParseUint(args[1], 10, 64) diff --git a/ioctl/cmd/action/stake2create.go b/ioctl/cmd/action/stake2create.go index 79b7a895d9..26e2c8aaf6 100644 --- a/ioctl/cmd/action/stake2create.go +++ b/ioctl/cmd/action/stake2create.go @@ -15,7 +15,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/output" "github.com/iotexproject/iotex-core/ioctl/util" - "github.com/iotexproject/iotex-core/ioctl/validator" ) // Multi-language support @@ -60,8 +59,8 @@ func stake2Create(args []string) error { amountStringInRau := amountInRau.String() var candidateName = args[1] - if err := validator.ValidateCandidateNameForStake2(candidateName); err != nil { - return output.NewError(output.ValidationError, "invalid candidate name", err) + if !action.IsValidCandidateName(candidateName) { + return output.NewError(output.ValidationError, "", action.ErrInvalidCanName) } stakeDuration, err := parseStakeDuration(args[2]) if err != nil { diff --git a/ioctl/cmd/action/stake2register.go b/ioctl/cmd/action/stake2register.go index daf43e62d3..f6e0923dfe 100644 --- a/ioctl/cmd/action/stake2register.go +++ b/ioctl/cmd/action/stake2register.go @@ -15,7 +15,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/output" "github.com/iotexproject/iotex-core/ioctl/util" - "github.com/iotexproject/iotex-core/ioctl/validator" ) // Multi-language support @@ -50,8 +49,8 @@ func init() { func register(args []string) error { name := args[0] - if err := validator.ValidateCandidateNameForStake2(name); err != nil { - return output.NewError(output.ValidationError, "invalid candidate name", err) + if !action.IsValidCandidateName(name) { + return output.NewError(output.ValidationError, "", action.ErrInvalidCanName) } operatorAddrStr, err := util.Address(args[1]) diff --git a/ioctl/cmd/action/stake2update.go b/ioctl/cmd/action/stake2update.go index 6123aa0ff7..7bc0c7e6b9 100644 --- a/ioctl/cmd/action/stake2update.go +++ b/ioctl/cmd/action/stake2update.go @@ -13,7 +13,6 @@ import ( "github.com/iotexproject/iotex-core/ioctl/config" "github.com/iotexproject/iotex-core/ioctl/output" "github.com/iotexproject/iotex-core/ioctl/util" - "github.com/iotexproject/iotex-core/ioctl/validator" ) // Multi-language support @@ -47,8 +46,8 @@ func init() { func stake2Update(args []string) error { name := args[0] - if err := validator.ValidateCandidateNameForStake2(name); err != nil { - return output.NewError(output.ValidationError, "invalid candidate name", err) + if !action.IsValidCandidateName(name) { + return output.NewError(output.ValidationError, "", action.ErrInvalidCanName) } operatorAddrStr, err := util.Address(args[1]) diff --git a/ioctl/validator/validator.go b/ioctl/validator/validator.go index cfe343e5c7..631ae225dc 100644 --- a/ioctl/validator/validator.go +++ b/ioctl/validator/validator.go @@ -24,10 +24,6 @@ var ( ErrNonPositiveNumber = errors.New("invalid number that is not positive") // ErrInvalidStakeDuration indicates error for invalid stake duration ErrInvalidStakeDuration = errors.New("stake duration must be within 0 and 1050 and in multiples of 7") - // ErrLongCandidateName ErrInvalidCandidateName indicates error for invalid candidate name - ErrLongCandidateName = errors.New("invalid length of candidate name that is more than 12 ") - // ErrStake2CandidateName ErrInvalidCandidateName indicates error for invalid candidate name (for ioctl stake2 command) - ErrStake2CandidateName = errors.New("the candidate name string is not valid") ) const ( @@ -71,16 +67,3 @@ func ValidateStakeDuration(stakeDuration *big.Int) error { return nil } - -// ValidateCandidateNameForStake2 validates candidate name for native staking 2 -func ValidateCandidateNameForStake2(candidateName string) error { - if len(candidateName) == 0 || len(candidateName) > 12 { - return ErrStake2CandidateName - } - for _, c := range candidateName { - if !(('a' <= c && c <= 'z') || ('0' <= c && c <= '9')) { - return ErrStake2CandidateName - } - } - return nil -}