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 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
3 changes: 3 additions & 0 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,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
9 changes: 9 additions & 0 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ func (cu *CandidateUpdate) Cost() (*big.Int, error) {
return fee, nil
}

// SanityCheck validates the variables in the action
func (cu *CandidateUpdate) SanityCheck() error {
if !IsValidCandidateName(cu.Name()) {
return ErrInvalidCanName
}

return cu.AbstractAction.SanityCheck()
}

// EncodeABIBinary encodes data in abi encoding
func (cu *CandidateUpdate) EncodeABIBinary() ([]byte, error) {
return cu.encodeABIBinary()
Expand Down
12 changes: 12 additions & 0 deletions action/candidateregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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
8 changes: 8 additions & 0 deletions action/stake_changecandidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ func (cc *ChangeCandidate) Cost() (*big.Int, error) {
return changeCandidateFee, nil
}

// SanityCheck validates the variables in the action
func (cc *ChangeCandidate) SanityCheck() error {
if !IsValidCandidateName(cc.candidateName) {
return ErrInvalidCanName
}
return cc.AbstractAction.SanityCheck()
}

// EncodeABIBinary encodes data in abi encoding
func (cc *ChangeCandidate) EncodeABIBinary() ([]byte, error) {
return cc.encodeABIBinary()
Expand Down
20 changes: 10 additions & 10 deletions action/stake_changecandidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import (

func TestChangeCandidate(t *testing.T) {
require := require.New(t)
stake, err := NewChangeCandidate(_nonce, _canAddress, _index, _payload, _gaslimit, _gasprice)
stake, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, _gasprice)
require.NoError(err)

ser := stake.Serialize()
require.Equal("080a1229696f3178707136326177383575717a72636367397935686e727976386c64326e6b7079636333677a611a077061796c6f6164", hex.EncodeToString(ser))
require.Equal("080a120a63616e646964617465311a077061796c6f6164", hex.EncodeToString(ser))

require.NoError(err)
require.Equal(_gaslimit, stake.GasLimit())
require.Equal(_gasprice, stake.GasPrice())
require.Equal(_nonce, stake.Nonce())

require.Equal(_payload, stake.Payload())
require.Equal(_canAddress, stake.Candidate())
require.Equal(_canName, stake.Candidate())
require.Equal(_index, stake.BucketIndex())

gas, err := stake.IntrinsicGas()
Expand All @@ -43,11 +43,11 @@ func TestChangeCandidate(t *testing.T) {
stake2 := &ChangeCandidate{}
require.NoError(stake2.LoadProto(proto))
require.Equal(_payload, stake2.Payload())
require.Equal(_canAddress, stake2.Candidate())
require.Equal(_canName, stake2.Candidate())
require.Equal(_index, stake2.BucketIndex())

t.Run("Invalid Gas Price", func(t *testing.T) {
cc, err := NewChangeCandidate(_nonce, _canAddress, _index, _payload, _gaslimit, new(big.Int).Mul(_gasprice, big.NewInt(-1)))
cc, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, new(big.Int).Mul(_gasprice, big.NewInt(-1)))
require.NoError(err)
require.Equal(ErrNegativeValue, errors.Cause(cc.SanityCheck()))
})
Expand All @@ -56,7 +56,7 @@ func TestChangeCandidate(t *testing.T) {
func TestChangeCandidateSignVerify(t *testing.T) {
require := require.New(t)
require.Equal("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", _senderKey.HexString())
stake, err := NewChangeCandidate(_nonce, _canAddress, _index, _payload, _gaslimit, _gasprice)
stake, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, _gasprice)
require.NoError(err)

bd := &EnvelopeBuilder{}
Expand All @@ -69,24 +69,24 @@ func TestChangeCandidateSignVerify(t *testing.T) {
require.NotNil(selp)
ser, err := proto.Marshal(selp.Proto())
require.NoError(err)
require.Equal("0a43080118c0843d22023130ea0236080a1229696f3178707136326177383575717a72636367397935686e727976386c64326e6b7079636333677a611a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41d519eb3747163b945b862989b7e82a7f8468001e9683757cb88d5ddd95f81895047429e858bd48f7d59a88bfec92de231d216293aeba1e4fbe11461d9c9fc99801", hex.EncodeToString(ser))
require.Equal("0a24080118c0843d22023130ea0217080a120a63616e646964617465311a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a412da6cd3ba7e830f0b669661f88a5c03307bdb44cae6fb45a4432ab69719f051f6631276467977617be2d265fdb00a3acc3a493261e2363a60acd3512aa15a89301", hex.EncodeToString(ser))
hash, err := selp.Hash()
require.NoError(err)
require.Equal("186526b5b9fe74e25beb52c83c41780a69108160bef2ddaf3bffb9f1f1e5e73a", hex.EncodeToString(hash[:]))
require.Equal("bc65d832237134c6a38d6ba10637af097b432d6c83d267aa6235e5b7c953d30f", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.VerifySignature())
}

func TestChangeCandidateABIEncodeAndDecode(t *testing.T) {
require := require.New(t)
stake, err := NewChangeCandidate(_nonce, _canAddress, _index, _payload, _gaslimit, _gasprice)
stake, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, _gasprice)
require.NoError(err)

data, err := stake.EncodeABIBinary()
require.NoError(err)
stake, err = NewChangeCandidateFromABIBinary(data)
require.NoError(err)
require.Equal(_canAddress, stake.candidateName)
require.Equal(_canName, stake.candidateName)
require.Equal(_index, stake.bucketIndex)
require.Equal(_payload, stake.payload)
}
4 changes: 3 additions & 1 deletion action/stake_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ func (cs *CreateStake) SanityCheck() error {
if cs.Amount().Sign() <= 0 {
return errors.Wrap(ErrInvalidAmount, "negative value")
}

if !IsValidCandidateName(cs.candName) {
return ErrInvalidCanName
}
return cs.AbstractAction.SanityCheck()
}

Expand Down
10 changes: 5 additions & 5 deletions action/stakecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ var stakeCreateTestParams = []struct {
}{
// valid test
{
identityset.PrivateKey(27), uint64(10), "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "100", uint32(10000), true, []byte("payload"), uint64(1000000), big.NewInt(10), "0a29696f313964307033616834673877773964376b63786671383779786537666e7238727074683573686a120331303018904e20012a077061796c6f6164", uint64(10700), "107100", "18d76ff9f3cfed0fe84f3fd4831f11379edc5b3d689d646187520b3fe74ab44c", "0a4b080118c0843d22023130c2023e0a29696f313964307033616834673877773964376b63786671383779786537666e7238727074683573686a120331303018904e20012a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a412e8bac421bab88dcd99c26ac8ffbf27f11ee57a41e7d2537891bfed5aed8e2e026d46e55d1b856787bc1cd7c1216a6e2534c5b5d1097c3afe8e657aa27cbbb0801", "f1785e47b4200c752bb6518bd18097a41e075438b8c18c9cb00e1ae2f38ce767", nil, nil,
identityset.PrivateKey(27), uint64(10), "test", "100", uint32(10000), true, []byte("payload"), uint64(1000000), big.NewInt(10), "0a0474657374120331303018904e20012a077061796c6f6164", uint64(10700), "107100", "18d76ff9f3cfed0fe84f3fd4831f11379edc5b3d689d646187520b3fe74ab44c", "0a26080118c0843d22023130c202190a0474657374120331303018904e20012a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41563785be9d7e2d796a8aaca41dbe1a53a0bce3614ede09718e72c75cb40cdb48355964b69156008f2319e20db4a4023730c3a1664ac35dfc10a7ceff26be8ebe00", "ebb26b08e824e18cb6d38918411749351c065198603e4626bbdc10b900dde270", nil, nil,
},
// invalid test
{
identityset.PrivateKey(27), uint64(10), "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "ae-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", ErrInvalidAmount, nil,
identityset.PrivateKey(27), uint64(10), "test", "ae-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", ErrInvalidAmount, nil,
},
{
identityset.PrivateKey(27), uint64(10), "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount,
identityset.PrivateKey(27), uint64(10), "test", "-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount,
},
{
identityset.PrivateKey(27), uint64(10), "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "0", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount,
identityset.PrivateKey(27), uint64(10), "test", "0", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount,
},
{
identityset.PrivateKey(27), uint64(10), "io19d0p3ah4g8ww9d7kcxfq87yxe7fnr8rpth5shj", "100", uint32(10000), true, []byte("payload"), uint64(1000000), big.NewInt(-unit.Qev), "0a29696f313964307033616834673877773964376b63786671383779786537666e7238727074683573686a120331303018904e20012a077061796c6f6164", uint64(10700), "107100", "18d76ff9f3cfed0fe84f3fd4831f11379edc5b3d689d646187520b3fe74ab44c", "0a4b080118c0843d22023130c2023e0a29696f313964307033616834673877773964376b63786671383779786537666e7238727074683573686a120331303018904e20012a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a412e8bac421bab88dcd99c26ac8ffbf27f11ee57a41e7d2537891bfed5aed8e2e026d46e55d1b856787bc1cd7c1216a6e2534c5b5d1097c3afe8e657aa27cbbb0801", "f1785e47b4200c752bb6518bd18097a41e075438b8c18c9cb00e1ae2f38ce767", nil, ErrNegativeValue,
identityset.PrivateKey(27), uint64(10), "test", "100", uint32(10000), true, []byte("payload"), uint64(1000000), big.NewInt(-unit.Qev), "0a0474657374120331303018904e20012a077061796c6f6164", uint64(10700), "107100", "18d76ff9f3cfed0fe84f3fd4831f11379edc5b3d689d646187520b3fe74ab44c", "0a26080118c0843d22023130c202190a0474657374120331303018904e20012a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41563785be9d7e2d796a8aaca41dbe1a53a0bce3614ede09718e72c75cb40cdb48355964b69156008f2319e20db4a4023730c3a1664ac35dfc10a7ceff26be8ebe00", "ebb26b08e824e18cb6d38918411749351c065198603e4626bbdc10b900dde270", nil, ErrNegativeValue,
},
}

Expand Down
1 change: 1 addition & 0 deletions action/stakereclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
_gaslimit = uint64(1000000)
_gasprice = big.NewInt(10)
_canAddress = "io1xpq62aw85uqzrccg9y5hnryv8ld2nkpycc3gza"
_canName = "candidate1"
_payload = []byte("payload")
_nonce = uint64(0)
_index = uint64(10)
Expand Down