diff --git a/action/candidate_register.go b/action/candidate_register.go index 50da933192..a4ccd79703 100644 --- a/action/candidate_register.go +++ b/action/candidate_register.go @@ -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 + } return cr.AbstractAction.SanityCheck() } diff --git a/action/candidate_update.go b/action/candidate_update.go index 5707544d4f..6693f06f41 100644 --- a/action/candidate_update.go +++ b/action/candidate_update.go @@ -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() diff --git a/action/candidateregister_test.go b/action/candidateregister_test.go index 7c7956aa72..f171c05632 100644 --- a/action/candidateregister_test.go +++ b/action/candidateregister_test.go @@ -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, }, diff --git a/action/stake_changecandidate.go b/action/stake_changecandidate.go index c09c5dedba..de67b582b7 100644 --- a/action/stake_changecandidate.go +++ b/action/stake_changecandidate.go @@ -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() diff --git a/action/stake_changecandidate_test.go b/action/stake_changecandidate_test.go index 8d0f57b3b8..6e1fc1b74e 100644 --- a/action/stake_changecandidate_test.go +++ b/action/stake_changecandidate_test.go @@ -17,11 +17,11 @@ 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()) @@ -29,7 +29,7 @@ func TestChangeCandidate(t *testing.T) { 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() @@ -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())) }) @@ -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{} @@ -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) } diff --git a/action/stake_create.go b/action/stake_create.go index bbd09285e8..6c9dfaf109 100644 --- a/action/stake_create.go +++ b/action/stake_create.go @@ -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() } diff --git a/action/stakecreate_test.go b/action/stakecreate_test.go index e1202abf6b..0de18ab556 100644 --- a/action/stakecreate_test.go +++ b/action/stakecreate_test.go @@ -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, }, } diff --git a/action/stakereclaim_test.go b/action/stakereclaim_test.go index 157190ed1b..d93a573683 100644 --- a/action/stakereclaim_test.go +++ b/action/stakereclaim_test.go @@ -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)