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

[staking] Fix typo error #4173

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion action/protocol/staking/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ func (p *Protocol) fetchBucketAndValidate(
}
if !allowSelfStaking {
selfStaking, serr := isSelfStakeBucket(featureCtx, csm, bucket)
if err != nil {
if serr != nil {
return bucket, &handleError{
err: serr,
failureStatus: iotextypes.ReceiptStatus_ErrUnknown,
Expand Down
73 changes: 73 additions & 0 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -2632,6 +2633,78 @@ func TestProtocol_HandleDepositToStake(t *testing.T) {
}
}

func TestProtocol_FetchBucketAndValidate(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
sm, p, _, _ := initAll(t, ctrl)

t.Run("bucket not exist", func(t *testing.T) {
csm, err := NewCandidateStateManager(sm, false)
require.NoError(err)
patches := gomonkey.ApplyPrivateMethod(csm, "getBucket", func(index uint64) (*VoteBucket, error) {
return nil, state.ErrStateNotExist
})
defer patches.Reset()
_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(1), 1, true, true)
require.ErrorContains(err, "failed to fetch bucket")
})
t.Run("validate owner", func(t *testing.T) {
csm, err := NewCandidateStateManager(sm, false)
require.NoError(err)
patches := gomonkey.ApplyPrivateMethod(csm, "getBucket", func(index uint64) (*VoteBucket, error) {
return &VoteBucket{
Owner: identityset.Address(1),
}, nil
})
defer patches.Reset()
_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(2), 1, true, true)
require.ErrorContains(err, "bucket owner does not match")
_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(1), 1, true, true)
require.NoError(err)
})
t.Run("validate selfstake", func(t *testing.T) {
csm, err := NewCandidateStateManager(sm, false)
require.NoError(err)
patches := gomonkey.NewPatches()
defer patches.Reset()
patches.ApplyPrivateMethod(csm, "getBucket", func(index uint64) (*VoteBucket, error) {
return &VoteBucket{
Owner: identityset.Address(1),
}, nil
})
isSelfStake := true
isSelfStakeErr := error(nil)
patches.ApplyFunc(isSelfStakeBucket, func(featureCtx protocol.FeatureCtx, csm CandidateStateManager, bucket *VoteBucket) (bool, error) {
return isSelfStake, isSelfStakeErr
})
_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(1), 1, false, false)
require.ErrorContains(err, "self staking bucket cannot be processed")
isSelfStake = false
_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(1), 1, false, false)
require.NoError(err)
isSelfStakeErr = errors.New("unknown error")
_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(1), 1, false, false)
require.ErrorContains(err, "unknown error")
})
t.Run("validate owner and selfstake", func(t *testing.T) {
csm, err := NewCandidateStateManager(sm, false)
require.NoError(err)
patches := gomonkey.NewPatches()
patches.ApplyPrivateMethod(csm, "getBucket", func(index uint64) (*VoteBucket, error) {
return &VoteBucket{
Owner: identityset.Address(1),
}, nil
})
patches.ApplyFunc(isSelfStakeBucket, func(featureCtx protocol.FeatureCtx, csm CandidateStateManager, bucket *VoteBucket) (bool, error) {
return false, nil
})
defer patches.Reset()

_, err = p.fetchBucketAndValidate(protocol.FeatureCtx{}, csm, identityset.Address(1), 1, true, false)
require.NoError(err)
})
}

func initCreateStake(t *testing.T, sm protocol.StateManager, callerAddr address.Address, initBalance int64, gasPrice *big.Int, gasLimit uint64, nonce uint64, blkHeight uint64, blkTimestamp time.Time, blkGasLimit uint64, p *Protocol, candidate *Candidate, amount string, autoStake bool) (context.Context, *big.Int) {
require := require.New(t)
require.NoError(setupAccount(sm, callerAddr, initBalance))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/iotexproject/iotex-core
go 1.19

require (
github.com/agiledragon/gomonkey/v2 v2.11.0
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cespare/xxhash/v2 v2.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNu
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U=
github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
2 changes: 1 addition & 1 deletion go.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cat /dev/null > coverage.txt
for dir in $(go list -f '{{.ImportPath}}={{join .TestGoFiles ","}}' ./...);do
array=(${dir//=/ })
if [ -n "${array[1]}" ]; then
go test -short -v -coverprofile=coverage.out -covermode=count "${array[0]}"
go test -gcflags=all=-l -short -v -coverprofile=coverage.out -covermode=count "${array[0]}"
Copy link
Member Author

Choose a reason for hiding this comment

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

It's required by gomonkey

courtney -l=coverage.out -o=profile.out
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
Expand Down
Loading