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

validate candidate name #3705

Merged
merged 8 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ func (cr *CandidateRegister) SanityCheck() error {
if cr.Amount().Sign() <= 0 {
return errors.Wrap(ErrInvalidAmount, "negative value")
}
if !IsValidCandidateName(cr.Name()) {
return ErrInvalidCanName
}
dustinxie marked this conversation as resolved.
Show resolved Hide resolved

return cr.AbstractAction.SanityCheck()
}
Expand Down
4 changes: 4 additions & 0 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func NewCandidateUpdate(
return nil, err
}
}

Copy link
Member

Choose a reason for hiding this comment

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

  1. remove empty line
  2. can you check if this would cause a potential different result in workingset.pickAndRunActions()? If yes, we need to gate the code by HF

Copy link
Member

Choose a reason for hiding this comment

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

also add the check to other staking actions as we discussed

Copy link
Contributor Author

@millken millken Dec 9, 2022

Choose a reason for hiding this comment

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

  1. remove empty line
  2. can you check if this would cause a potential different result in workingset.pickAndRunActions()? If yes, we need to gate the code by HF

No difference, invalidCandName actions both will be discarded

github.com/iotexproject/iotex-core/action.IsValidCandidateName (iotex-core/action/candidate_register.go:391)
github.com/iotexproject/iotex-core/action/protocol/staking.(*Protocol).validateCandidateRegister (iotex-core/action/protocol/staking/validations.go:65)
github.com/iotexproject/iotex-core/action/protocol/staking.(*Protocol).Validate (iotex-core/action/protocol/staking/protocol.go:459)
github.com/iotexproject/iotex-core/state/factory.(*workingSet).pickAndRunActions (iotex-core/state/factory/workingset.go:447)
github.com/iotexproject/iotex-core/state/factory.(*workingSet).CreateBuilder (iotex-core/state/factory/workingset.go:553)
github.com/iotexproject/iotex-core/state/factory.(*stateDB).NewBlockBuilder (iotex-core/state/factory/statedb.go:258)
github.com/iotexproject/iotex-core/state/factory.(*minter).NewBlockBuilder (iotex-core/state/factory/minter.go:30)
github.com/iotexproject/iotex-core/blockchain.(*blockchain).MintNewBlock (iotex-core/blockchain/blockchain.go:385)

if !IsValidCandidateName(name) {
return nil, ErrInvalidCanName
}
return cu, nil
}

Expand Down
12 changes: 12 additions & 0 deletions action/candidateregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ var candidateRegisterTestParams = []struct {
{
identityset.PrivateKey(27), uint64(10), "test", "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", "io13sj9mzpewn25ymheukte4v39hvjdtrfp00mlyv", "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "ab-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", ErrInvalidAmount, nil,
},
// invalid candidate name
{
identityset.PrivateKey(27), uint64(10), "F@¥", "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", "io13sj9mzpewn25ymheukte4v39hvjdtrfp00mlyv", "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidCanName,
},
// invalid candidate name
{
identityset.PrivateKey(27), uint64(10), "", "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", "io13sj9mzpewn25ymheukte4v39hvjdtrfp00mlyv", "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidCanName,
},
// invalid candidate name
{
identityset.PrivateKey(27), uint64(10), "aaaaaaaaaaaaa", "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", "io13sj9mzpewn25ymheukte4v39hvjdtrfp00mlyv", "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidCanName,
},
{
identityset.PrivateKey(27), uint64(10), "test", "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", "io13sj9mzpewn25ymheukte4v39hvjdtrfp00mlyv", "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount,
},
Expand Down
6 changes: 5 additions & 1 deletion action/candidateupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ var (

func TestCandidateUpdate(t *testing.T) {
require := require.New(t)
cu, err := NewCandidateUpdate(_cuNonce, _cuName, _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
cu, err := NewCandidateUpdate(_cuNonce, "", _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
require.ErrorIs(err, ErrInvalidCanName)
cu, err = NewCandidateUpdate(_cuNonce, "aaaaaaaaaaaaa", _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
require.ErrorIs(err, ErrInvalidCanName)
cu, err = NewCandidateUpdate(_cuNonce, _cuName, _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
require.NoError(err)

ser := cu.Serialize()
Expand Down