Skip to content

Commit 09f44be

Browse files
authored
Merge pull request #885 from tendermint/audit-release
Release v0.2.1
2 parents f5d9d70 + a217c11 commit 09f44be

20 files changed

+137
-85
lines changed

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ require (
1717
github.com/tendermint/starport v0.19.5
1818
github.com/tendermint/tendermint v0.34.16
1919
github.com/tendermint/tm-db v0.6.6
20-
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e
21-
google.golang.org/grpc v1.45.0
22-
google.golang.org/protobuf v1.27.1
20+
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd
21+
google.golang.org/grpc v1.46.2
22+
google.golang.org/protobuf v1.28.0
2323
gopkg.in/yaml.v2 v2.4.0
2424
)
2525

@@ -59,6 +59,7 @@ require (
5959
github.com/gogo/gateway v1.1.0 // indirect
6060
github.com/golang/snappy v0.0.3 // indirect
6161
github.com/google/btree v1.0.0 // indirect
62+
github.com/google/go-cmp v0.5.8 // indirect
6263
github.com/google/orderedcode v0.0.1 // indirect
6364
github.com/gorilla/handlers v1.5.1 // indirect
6465
github.com/gorilla/websocket v1.5.0 // indirect
@@ -116,7 +117,7 @@ require (
116117
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
117118
golang.org/x/text v0.3.7 // indirect
118119
gopkg.in/ini.v1 v1.66.2 // indirect
119-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
120+
gopkg.in/yaml.v3 v3.0.1 // indirect
120121
nhooyr.io/websocket v1.8.6 // indirect
121122
)
122123

go.sum

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,9 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
731731
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
732732
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
733733
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
734-
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
735734
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
735+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
736+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
736737
github.com/google/go-github/v37 v37.0.0/go.mod h1:LM7in3NmXDrX58GbEHy7FtNLbI2JijX93RnMKvWG3m4=
737738
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
738739
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
@@ -2078,7 +2079,6 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
20782079
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
20792080
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
20802081
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2081-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
20822082
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
20832083
gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
20842084
gonum.org/v1/gonum v0.8.2/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0=
@@ -2211,8 +2211,9 @@ google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9/go.mod h1:5CzLGKJ6
22112211
google.golang.org/genproto v0.0.0-20211206160659-862468c7d6e0/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
22122212
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
22132213
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
2214-
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e h1:fNKDNuUyC4WH+inqDMpfXDdfvwfYILbsX+oskGZ8hxg=
22152214
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
2215+
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd h1:e0TwkXOdbnH/1x5rc5MZ/VYyiZ4v+RdVfrGMqEwT68I=
2216+
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
22162217
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
22172218
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
22182219
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
@@ -2229,8 +2230,9 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
22292230
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
22302231
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
22312232
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
2232-
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
22332233
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
2234+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
2235+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
22342236
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
22352237
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
22362238
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -2276,8 +2278,9 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
22762278
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
22772279
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
22782280
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2279-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
22802281
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2282+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2283+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22812284
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
22822285
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
22832286
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=

x/campaign/keeper/mainnet_account.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func (k Keeper) SetMainnetAccount(ctx sdk.Context, mainnetAccount types.MainnetAccount) {
1212
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MainnetAccountKeyPrefix))
1313
b := k.cdc.MustMarshal(&mainnetAccount)
14-
store.Set(types.MainnetAccountKey(
14+
store.Set(types.AccountKeyPath(
1515
mainnetAccount.CampaignID,
1616
mainnetAccount.Address,
1717
), b)
@@ -25,7 +25,7 @@ func (k Keeper) GetMainnetAccount(
2525
) (val types.MainnetAccount, found bool) {
2626
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MainnetAccountKeyPrefix))
2727

28-
b := store.Get(types.MainnetAccountKey(campaignID, address))
28+
b := store.Get(types.AccountKeyPath(campaignID, address))
2929
if b == nil {
3030
return val, false
3131
}
@@ -41,7 +41,7 @@ func (k Keeper) RemoveMainnetAccount(
4141
address string,
4242
) {
4343
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MainnetAccountKeyPrefix))
44-
store.Delete(types.MainnetAccountKey(
44+
store.Delete(types.AccountKeyPath(
4545
campaignID,
4646
address,
4747
))

x/campaign/keeper/mainnet_vesting_account.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func (k Keeper) SetMainnetVestingAccount(ctx sdk.Context, mainnetVestingAccount types.MainnetVestingAccount) {
1212
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MainnetVestingAccountKeyPrefix))
1313
b := k.cdc.MustMarshal(&mainnetVestingAccount)
14-
store.Set(types.MainnetVestingAccountKey(
14+
store.Set(types.AccountKeyPath(
1515
mainnetVestingAccount.CampaignID,
1616
mainnetVestingAccount.Address,
1717
), b)
@@ -25,7 +25,7 @@ func (k Keeper) GetMainnetVestingAccount(
2525
) (val types.MainnetVestingAccount, found bool) {
2626
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MainnetVestingAccountKeyPrefix))
2727

28-
b := store.Get(types.MainnetVestingAccountKey(campaignID, address))
28+
b := store.Get(types.AccountKeyPath(campaignID, address))
2929
if b == nil {
3030
return val, false
3131
}
@@ -41,7 +41,7 @@ func (k Keeper) RemoveMainnetVestingAccount(
4141
address string,
4242
) {
4343
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MainnetVestingAccountKeyPrefix))
44-
store.Delete(types.MainnetVestingAccountKey(campaignID, address))
44+
store.Delete(types.AccountKeyPath(campaignID, address))
4545
}
4646

4747
// GetAllMainnetVestingAccount returns all mainnetVestingAccount

x/campaign/keeper/msg_add_shares.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func (k msgServer) AddShares(goCtx context.Context, msg *types.MsgAddShares) (*t
5858

5959
// increase the campaign shares
6060
campaign.AllocatedShares = types.IncreaseShares(campaign.AllocatedShares, msg.Shares)
61-
if types.IsTotalSharesReached(campaign.AllocatedShares, k.GetTotalShares(ctx)) {
61+
reached, err := types.IsTotalSharesReached(campaign.AllocatedShares, k.GetTotalShares(ctx))
62+
if err != nil {
63+
return nil, spnerrors.Criticalf("verified shares are invalid %s", err.Error())
64+
}
65+
if reached {
6266
return nil, sdkerrors.Wrapf(types.ErrTotalSharesLimit, "%d", msg.CampaignID)
6367
}
6468

x/campaign/keeper/msg_add_vesting_options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func (k msgServer) AddVestingOptions(goCtx context.Context, msg *types.MsgAddVes
7575

7676
// increase the campaign shares
7777
campaign.AllocatedShares = types.IncreaseShares(campaign.AllocatedShares, totalShares)
78-
if types.IsTotalSharesReached(campaign.AllocatedShares, k.GetTotalShares(ctx)) {
78+
reached, err := types.IsTotalSharesReached(campaign.AllocatedShares, k.GetTotalShares(ctx))
79+
if err != nil {
80+
return nil, spnerrors.Criticalf("verified shares are invalid %s", err.Error())
81+
}
82+
if reached {
7983
return nil, sdkerrors.Wrapf(types.ErrTotalSharesLimit, "%d", msg.CampaignID)
8084
}
8185

x/campaign/keeper/msg_mint_vouchers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func (k msgServer) MintVouchers(goCtx context.Context, msg *types.MsgMintVoucher
3535

3636
// Increase the campaign shares
3737
campaign.AllocatedShares = types.IncreaseShares(campaign.AllocatedShares, msg.Shares)
38-
if types.IsTotalSharesReached(campaign.AllocatedShares, k.GetTotalShares(ctx)) {
38+
reached, err := types.IsTotalSharesReached(campaign.AllocatedShares, k.GetTotalShares(ctx))
39+
if err != nil {
40+
return nil, spnerrors.Criticalf("verified shares are invalid %s", err.Error())
41+
}
42+
if reached {
3943
return nil, sdkerrors.Wrapf(types.ErrTotalSharesLimit, "%d", msg.CampaignID)
4044
}
4145

x/campaign/types/campaign.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package types
22

33
import (
4-
"errors"
54
"fmt"
65

76
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/pkg/errors"
88
)
99

1010
const (
@@ -33,12 +33,12 @@ func (m Campaign) Validate(totalShareNumber uint64) error {
3333
if !m.TotalSupply.IsValid() {
3434
return errors.New("invalid total supply")
3535
}
36-
if !sdk.Coins(m.AllocatedShares).IsValid() {
37-
return errors.New("invalid allocated shares")
38-
}
39-
40-
if IsTotalSharesReached(m.AllocatedShares, totalShareNumber) {
4136

37+
reached, err := IsTotalSharesReached(m.AllocatedShares, totalShareNumber)
38+
if err != nil {
39+
return errors.Wrap(err, "invalid allocated shares")
40+
}
41+
if reached {
4242
return errors.New("more allocated shares than total shares")
4343
}
4444

x/campaign/types/campaign_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func TestCampaign_Validate(t *testing.T) {
4343
totalSharesReached.AllocatedShares = campaign.NewSharesFromCoins(sdk.NewCoins(
4444
sdk.NewCoin("foo", sdk.NewInt(spntypes.TotalShareNumber+1)),
4545
))
46-
require.True(t, campaign.IsTotalSharesReached(totalSharesReached.AllocatedShares, spntypes.TotalShareNumber))
46+
reached, err := campaign.IsTotalSharesReached(totalSharesReached.AllocatedShares, spntypes.TotalShareNumber)
47+
require.NoError(t, err)
48+
require.True(t, reached)
4749

4850
for _, tc := range []struct {
4951
desc string

x/campaign/types/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (gs GenesisState) Validate() error {
5959
return fmt.Errorf("campaign id %d doesn't exist for mainnet account %s",
6060
elem.CampaignID, elem.Address)
6161
}
62-
index := string(MainnetAccountKey(elem.CampaignID, elem.Address))
62+
index := string(AccountKeyPath(elem.CampaignID, elem.Address))
6363
if _, ok := mainnetAccountIndexMap[index]; ok {
6464
return fmt.Errorf("duplicated index for mainnetAccount")
6565
}
@@ -73,7 +73,7 @@ func (gs GenesisState) Validate() error {
7373
return fmt.Errorf("campaign id %d doesn't exist for mainnet vesting account %s",
7474
elem.CampaignID, elem.Address)
7575
}
76-
index := string(MainnetVestingAccountKey(elem.CampaignID, elem.Address))
76+
index := string(AccountKeyPath(elem.CampaignID, elem.Address))
7777
if _, ok := mainnetVestingAccountIndexMap[index]; ok {
7878
return fmt.Errorf("duplicated index for mainnetVestingAccount")
7979
}

x/campaign/types/keys.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func CampaignChainsKey(campaignID uint64) []byte {
4646
return append(spntypes.UintBytes(campaignID), byte('/'))
4747
}
4848

49-
// MainnetAccountKey returns the store key to retrieve a MainnetAccount from the index fields
50-
func MainnetAccountKey(campaignID uint64, address string) []byte {
49+
// AccountKeyPath returns the store key path without prefix for an account defined by a campaign ID and an address
50+
func AccountKeyPath(campaignID uint64, address string) []byte {
5151
campaignIDBytes := append(spntypes.UintBytes(campaignID), byte('/'))
5252
addressBytes := append([]byte(address), byte('/'))
5353
return append(campaignIDBytes, addressBytes...)
@@ -60,13 +60,6 @@ func MainnetAccountAllKey(campaignID uint64) []byte {
6060
return append(prefixBytes, campaignIDBytes...)
6161
}
6262

63-
// MainnetVestingAccountKey returns the store key to retrieve a MainnetVestingAccount from the index fields
64-
func MainnetVestingAccountKey(campaignID uint64, address string) []byte {
65-
campaignIDBytes := append(spntypes.UintBytes(campaignID), byte('/'))
66-
addressBytes := append([]byte(address), byte('/'))
67-
return append(campaignIDBytes, addressBytes...)
68-
}
69-
7063
// MainnetVestingAccountAllKey returns the store key to retrieve all MainnetVestingAccount by campaign id
7164
func MainnetVestingAccountAllKey(campaignID uint64) []byte {
7265
prefixBytes := []byte(MainnetVestingAccountKeyPrefix)

x/campaign/types/shares.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package types
22

33
import (
4-
"errors"
54
"fmt"
65
"strings"
76

87
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/pkg/errors"
99
)
1010

1111
// Shares represents the portion of a supply
@@ -73,14 +73,21 @@ func DecreaseShares(shares, toDecrease Shares) (Shares, error) {
7373
}
7474

7575
// IsTotalSharesReached checks if the provided shares overflow the total number of shares
76-
func IsTotalSharesReached(shares Shares, maximumTotalShareNumber uint64) bool {
76+
func IsTotalSharesReached(shares Shares, maximumTotalShareNumber uint64) (bool, error) {
77+
if err := sdk.Coins(shares).Validate(); err != nil {
78+
return false, errors.Wrap(err, "invalid share")
79+
}
80+
if err := CheckShares(shares); err != nil {
81+
return false, errors.Wrap(err, "invalid share format")
82+
}
83+
7784
for _, coin := range shares {
7885
if coin.Amount.Uint64() > maximumTotalShareNumber {
79-
return true
86+
return true, nil
8087
}
8188
}
8289

83-
return false
90+
return false, nil
8491
}
8592

8693
// IsAllLTE returns true iff for every denom in shares, the denom is present at

x/campaign/types/shares_test.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,61 @@ func TestDecreaseShares(t *testing.T) {
125125

126126
func TestShareIsTotalReached(t *testing.T) {
127127
for _, tc := range []struct {
128-
desc string
129-
shares campaign.Shares
130-
reached bool
128+
desc string
129+
shares campaign.Shares
130+
maxTotalShares uint64
131+
reached bool
132+
isValid bool
131133
}{
132134
{
133-
desc: "empty is false",
134-
shares: campaign.EmptyShares(),
135-
reached: false,
135+
desc: "should return false with empty shares",
136+
shares: campaign.EmptyShares(),
137+
maxTotalShares: 0,
138+
reached: false,
139+
isValid: true,
136140
},
137141
{
138-
desc: "no default total is reached",
139-
shares: tc.Shares(t, fmt.Sprintf("%dfoo,100bar", spntypes.TotalShareNumber)),
140-
reached: false,
142+
desc: "should return false when total not reached",
143+
shares: tc.Shares(t, "1000foo,100bar"),
144+
maxTotalShares: 1000,
145+
reached: false,
146+
isValid: true,
141147
},
142148
{
143-
desc: "a default total is reached",
144-
shares: tc.Shares(t, fmt.Sprintf("%dfoo,100bar", spntypes.TotalShareNumber+1)),
145-
reached: true,
149+
desc: "should return true when total reached",
150+
shares: tc.Shares(t, "1001foo,100bar"),
151+
maxTotalShares: 1000,
152+
reached: true,
153+
isValid: true,
154+
},
155+
{
156+
desc: "should return error if shares are invalid",
157+
shares: campaign.NewSharesFromCoins(sdk.Coins{
158+
sdk.NewCoin("foo", sdk.NewIntFromUint64(500)),
159+
sdk.NewCoin("foo", sdk.NewIntFromUint64(500)),
160+
}),
161+
maxTotalShares: 1000,
162+
reached: false,
163+
isValid: false,
164+
},
165+
{
166+
desc: "should return error if shares have invalid format",
167+
shares: campaign.Shares(sdk.Coins{
168+
sdk.NewCoin("foo", sdk.NewIntFromUint64(500)),
169+
}),
170+
maxTotalShares: 1000,
171+
reached: false,
172+
isValid: false,
146173
},
147174
} {
148175
t.Run(tc.desc, func(t *testing.T) {
149-
require.Equal(t, tc.reached, campaign.IsTotalSharesReached(tc.shares, spntypes.TotalShareNumber))
176+
reached, err := campaign.IsTotalSharesReached(tc.shares, tc.maxTotalShares)
177+
if tc.isValid {
178+
require.NoError(t, err)
179+
require.True(t, tc.reached == reached)
180+
} else {
181+
require.Error(t, err)
182+
}
150183
})
151184
}
152185
}

0 commit comments

Comments
 (0)