Skip to content

Commit

Permalink
[action] fix issue iotexproject#3198 (iotexproject#3206)
Browse files Browse the repository at this point in the history
* fix issue iotexproject#3198
  • Loading branch information
Liuhaai authored Mar 22, 2022
1 parent a9e693b commit fb93bda
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 39 deletions.
17 changes: 3 additions & 14 deletions action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestActionProtoAndVerify(t *testing.T) {
selp, err := Sign(elp, identityset.PrivateKey(28))
require.NoError(err)

require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())

nselp := &SealedEnvelope{}
require.NoError(nselp.LoadProto(selp.Proto()))
Expand All @@ -55,18 +55,7 @@ func TestActionProtoAndVerify(t *testing.T) {

selp.srcPubkey = nil

require.EqualError(selp.Verify(), "empty public key")
})
t.Run("gas limit too low", func(t *testing.T) {
bd := &EnvelopeBuilder{}
elp := bd.SetGasPrice(big.NewInt(10)).
SetGasLimit(uint64(1000)).
SetAction(v).Build()

selp, err := Sign(elp, identityset.PrivateKey(28))
require.NoError(err)

require.Equal(ErrIntrinsicGas, errors.Cause(selp.Verify()))
require.EqualError(selp.VerifySignature(), "empty public key")
})
t.Run("invalid signature", func(t *testing.T) {
bd := &EnvelopeBuilder{}
Expand All @@ -77,7 +66,7 @@ func TestActionProtoAndVerify(t *testing.T) {
selp, err := Sign(elp, identityset.PrivateKey(28))
require.NoError(err)
selp.signature = []byte("invalid signature")
require.Equal(ErrInvalidSender, errors.Cause(selp.Verify()))
require.Equal(ErrInvalidSender, errors.Cause(selp.VerifySignature()))
})
}

Expand Down
2 changes: 1 addition & 1 deletion action/candidateregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestCandidateRegister(t *testing.T) {
require.NoError(err)
require.Equal(test.SelpHash, hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

}
Expand Down
2 changes: 1 addition & 1 deletion action/candidateupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCandidateUpdateSignVerify(t *testing.T) {
require.NoError(err)
require.Equal("ca1a28f0e9a58ffc67037cc75066dbdd8e024aa2b2e416e4d6ce16c3d86282e5", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestCandidateUpdateABIEncodeAndDecode(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions action/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestExecutionSignVerify(t *testing.T) {
require.True(ok)

w := AssembleSealedEnvelope(elp, executorKey.PublicKey(), []byte("lol"))
require.Error(w.Verify())
require.Error(w.VerifySignature())
ex2, ok := w.Envelope.Action().(*Execution)
require.True(ok)
require.Equal(ex, ex2)
Expand All @@ -50,7 +50,7 @@ func TestExecutionSignVerify(t *testing.T) {
require.NotNil(selp)

// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestExecutionSanityCheck(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions action/protocol/generic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ func NewGenericValidator(sr StateReader, accountState AccountState) *GenericVali
}

// Validate validates a generic action
func (v *GenericValidator) Validate(ctx context.Context, selp action.SealedEnvelope) error {
func (v *GenericValidator) Validate(_ context.Context, selp action.SealedEnvelope) error {
intrinsicGas, err := selp.IntrinsicGas()
if err != nil {
return err
}
if intrinsicGas > selp.GasLimit() {
return action.ErrIntrinsicGas
}

// Verify action using action sender's public key
if err := selp.Verify(); err != nil {
if err := selp.VerifySignature(); err != nil {
return err
}
caller := selp.SrcPubkey().Address()
Expand Down
2 changes: 1 addition & 1 deletion action/rlp_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestRlpDecodeVerify(t *testing.T) {
require.NoError(err)
require.True(bytes.Equal(rawHash[:], raw[:]))
require.NotEqual(raw, h)
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}
}

Expand Down
10 changes: 2 additions & 8 deletions action/sealedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,11 @@ func (sealed *SealedEnvelope) LoadProto(pbAct *iotextypes.Action) error {
return nil
}

// Verify verifies the action using sender's public key
func (sealed *SealedEnvelope) Verify() error {
// VerifySignature verifies the action using sender's public key
func (sealed *SealedEnvelope) VerifySignature() error {
if sealed.SrcPubkey() == nil {
return errors.New("empty public key")
}
// Reject action with insufficient gas limit
intrinsicGas, err := sealed.IntrinsicGas()
if intrinsicGas > sealed.GasLimit() || err != nil {
return ErrIntrinsicGas
}

h, err := sealed.envelopeHash()
if err != nil {
return errors.Wrap(err, "failed to generate envelope hash")
Expand Down
2 changes: 1 addition & 1 deletion action/stake_changecandidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestChangeCandidateSignVerify(t *testing.T) {
require.NoError(err)
require.Equal("186526b5b9fe74e25beb52c83c41780a69108160bef2ddaf3bffb9f1f1e5e73a", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestChangeCandidateABIEncodeAndDecode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion action/stake_transferownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestStakingTransferSignVerify(t *testing.T) {
require.NoError(err)
require.Equal("74b2e1d6a09ba5d1298fa422d5850991ae516865077282196295a38f93c78b85", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestStakingTransferABIEncodeAndDecode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion action/stakeadddeposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestDeposit(t *testing.T) {
require.NoError(err)
require.Equal(test.SelpHash, hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}
}

Expand Down
2 changes: 1 addition & 1 deletion action/stakecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestCreateStake(t *testing.T) {
require.NoError(err)
require.Equal(test.SelpHash, hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

}
Expand Down
4 changes: 2 additions & 2 deletions action/stakereclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestUnstakeSignVerify(t *testing.T) {
require.NoError(err)
require.Equal("bed58b64a6c4e959eca60a86f0b2149ce0e1dd527ac5fd26aef725ebf7c22a7d", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestUnstakeABIEncodeAndDecode(t *testing.T) {
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestWithdrawSignVerify(t *testing.T) {
require.NoError(err)
require.Equal("28049348cf34f1aa927caa250e7a1b08778c44efaf73b565b6fa9abe843871b4", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestWithdrawABIEncodeAndDecode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion action/stakerestake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestRestakeSignVerify(t *testing.T) {
require.NoError(err)
require.Equal("8816e8f784a1fce40b54d1cd172bb6976fd9552f1570c73d1d9fcdc5635424a9", hex.EncodeToString(hash[:]))
// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestRestakeABIEncodeAndDecode(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions action/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestTransferSignVerify(t *testing.T) {
require.True(ok)

w := AssembleSealedEnvelope(elp, senderKey.PublicKey(), []byte("lol"))
require.Error(w.Verify())
require.Error(w.VerifySignature())
tsf2, ok := w.Envelope.Action().(*Transfer)
require.True(ok)
require.Equal(tsf, tsf2)
Expand All @@ -46,7 +46,7 @@ func TestTransferSignVerify(t *testing.T) {
require.NotNil(selp)

// verify signature
require.NoError(selp.Verify())
require.NoError(selp.VerifySignature())
}

func TestTransfer(t *testing.T) {
Expand All @@ -67,7 +67,7 @@ func TestTransfer(t *testing.T) {
require.True(ok)

w := AssembleSealedEnvelope(elp, senderKey.PublicKey(), []byte("lol"))
require.Error(w.Verify())
require.Error(w.VerifySignature())

require.NoError(err)
require.Equal("10", tsf.Amount().Text(10))
Expand Down

0 comments on commit fb93bda

Please sign in to comment.