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

move IsValidCandidateName to action package #3706

Merged
merged 5 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
46 changes: 46 additions & 0 deletions action/candidateregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
25 changes: 13 additions & 12 deletions action/protocol/staking/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -89,7 +90,7 @@ func (d *Candidate) Validate() error {
}

if d.SelfStake == nil {
return ErrInvalidAmount
return action.ErrInvalidAmount
}
return nil
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion action/protocol/staking/candidate_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/iotexproject/iotex-address/address"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
)

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions action/protocol/staking/candidate_center_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion action/protocol/staking/candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions action/protocol/staking/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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:
Expand All @@ -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
16 changes: 8 additions & 8 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
1,
time.Now(),
10000,
ErrInvalidCanName,
action.ErrInvalidCanName,
iotextypes.ReceiptStatus_ErrCandidateNotExist,
},
{
Expand All @@ -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 All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1522,7 +1522,7 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) {
time.Now(),
10000,
false,
ErrInvalidCanName,
action.ErrInvalidCanName,
iotextypes.ReceiptStatus_Failure,
},
// invalid candidate name 2
Expand All @@ -1542,7 +1542,7 @@ func TestProtocol_HandleChangeCandidate(t *testing.T) {
time.Now(),
10000,
false,
ErrInvalidCanName,
action.ErrInvalidCanName,
iotextypes.ReceiptStatus_Failure,
},
// invalid candidate name 3
Expand All @@ -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
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
Loading