Skip to content

Commit

Permalink
refactor(participation): use sdk.Int type for allocations (tender…
Browse files Browse the repository at this point in the history
…mint#894)

* fix check

* update proto

* proto compile

* refactor

* format

* fix test state initialization

* Update x/participation/keeper/msg_withdraw_allocations_test.go

Co-authored-by: Thomas Bruyelle <[email protected]>

* Update testutil/sample/participation.go

Co-authored-by: Lucas Btd <[email protected]>

* add sample.Int()

* update requiredAllocations values

* fix templates

* fix test

Co-authored-by: Thomas Bruyelle <[email protected]>
Co-authored-by: Lucas Btd <[email protected]>
  • Loading branch information
3 people authored Jul 26, 2022
1 parent e185661 commit f310279
Show file tree
Hide file tree
Showing 40 changed files with 508 additions and 363 deletions.
8 changes: 4 additions & 4 deletions localnet/spn/genesis_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,28 +355,28 @@
"participationTierList": [
{
"tierID": 1,
"requiredAllocations": 1,
"requiredAllocations": "1",
"benefits": {
"maxBidAmount": "1000"
}
},
{
"tierID": 2,
"requiredAllocations": 2,
"requiredAllocations": "2",
"benefits": {
"maxBidAmount": "2000"
}
},
{
"tierID": 3,
"requiredAllocations": 5,
"requiredAllocations": "5",
"benefits": {
"maxBidAmount": "10000"
}
},
{
"tierID": 4,
"requiredAllocations": 10,
"requiredAllocations": "10",
"benefits": {
"maxBidAmount": "30000"
}
Expand Down
8 changes: 4 additions & 4 deletions localnet/testnet/genesis_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,28 +362,28 @@
"participationTierList": [
{
"tierID": 1,
"requiredAllocations": 1,
"requiredAllocations": "1",
"benefits": {
"maxBidAmount": "1000"
}
},
{
"tierID": 2,
"requiredAllocations": 2,
"requiredAllocations": "2",
"benefits": {
"maxBidAmount": "1000"
}
},
{
"tierID": 3,
"requiredAllocations": 5,
"requiredAllocations": "5",
"benefits": {
"maxBidAmount": "1000"
}
},
{
"tierID": 4,
"requiredAllocations": 10,
"requiredAllocations": "10",
"benefits": {
"maxBidAmount": "1000"
}
Expand Down
9 changes: 7 additions & 2 deletions proto/participation/auction_used_allocations.proto
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
syntax = "proto3";
package tendermint.spn.participation;

import "gogoproto/gogo.proto";

option go_package = "github.com/tendermint/spn/x/participation/types";

// Allocations used by a user for a specific auction
message AuctionUsedAllocations {
string address = 1;
uint64 auctionID = 2;
uint64 numAllocations = 3;
bool withdrawn = 4;
bool withdrawn = 3;
string numAllocations = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
5 changes: 4 additions & 1 deletion proto/participation/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ option go_package = "github.com/tendermint/spn/x/participation/types";
message EventAllocationsUsed {
string participant = 1;
uint64 auctionID = 2;
uint64 numAllocations = 3;
string numAllocations = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message EventAllocationsWithdrawn {
Expand Down
5 changes: 4 additions & 1 deletion proto/participation/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ message AllocationPrice {
// Matches a number of required allocations with benefits
message Tier {
uint64 tierID = 1;
uint64 requiredAllocations = 2;
string requiredAllocations = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
TierBenefits benefits = 3 [(gogoproto.nullable) = false];
}

Expand Down
10 changes: 8 additions & 2 deletions proto/participation/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,21 @@ message QueryGetTotalAllocationsRequest {
}

message QueryGetTotalAllocationsResponse {
uint64 totalAllocations = 1;
string totalAllocations = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message QueryGetAvailableAllocationsRequest {
string address = 1;
}

message QueryGetAvailableAllocationsResponse {
uint64 availableAllocations = 1;
string availableAllocations = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down
7 changes: 6 additions & 1 deletion proto/participation/used_allocations.proto
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
syntax = "proto3";
package tendermint.spn.participation;

import "gogoproto/gogo.proto";

option go_package = "github.com/tendermint/spn/x/participation/types";

// Describes the number of allocations already used by a user for existing auctions
message UsedAllocations {
string address = 1;
uint64 numAllocations = 2;
string numAllocations = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
10 changes: 6 additions & 4 deletions testutil/networksuite/networksuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func populateClaim(r *rand.Rand, claimState claim.GenesisState) claim.GenesisSta
for i := 0; i < 5; i++ {
claimRecord := claim.ClaimRecord{
Address: sample.Address(r),
Claimable: sdk.NewInt(r.Int63()),
Claimable: sample.Int(r),
}
nullify.Fill(&claimRecord)
claimState.ClaimRecords = append(claimState.ClaimRecords, claimRecord)
Expand Down Expand Up @@ -234,7 +234,8 @@ func populateParticipation(r *rand.Rand, participationState participation.Genesi
// add used allocations
for i := 0; i < 5; i++ {
usedAllocations := participation.UsedAllocations{
Address: sample.Address(r),
Address: sample.Address(r),
NumAllocations: sample.Int(r),
}
nullify.Fill(&usedAllocations)
participationState.UsedAllocationsList = append(participationState.UsedAllocationsList, usedAllocations)
Expand All @@ -244,8 +245,9 @@ func populateParticipation(r *rand.Rand, participationState participation.Genesi
address := sample.Address(r)
for i := 0; i < 5; i++ {
auctionUsedAllocations := participation.AuctionUsedAllocations{
Address: address,
AuctionID: uint64(i),
Address: address,
AuctionID: uint64(i),
NumAllocations: sample.Int(r),
}
nullify.Fill(&auctionUsedAllocations)
participationState.AuctionUsedAllocationsList = append(participationState.AuctionUsedAllocationsList, auctionUsedAllocations)
Expand Down
10 changes: 5 additions & 5 deletions testutil/sample/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ParticipationParams(r *rand.Rand) participation.Params {

tiers := make([]participation.Tier, 0)
numTiers := uint64(r.Int63n(10) + 1)
allocCnt := uint64(r.Int63n(5) + 1)
allocCnt := sdk.NewInt(r.Int63n(5) + 1)
maxBidCnt := sdk.NewInt(r.Int63n(10000) + 1)
for i := uint64(1); i <= numTiers; i++ {
tier := participation.Tier{
Expand All @@ -30,7 +30,7 @@ func ParticipationParams(r *rand.Rand) participation.Params {
tiers = append(tiers, tier)

// increment values for next tier
allocCnt += uint64(r.Int63n(5) + 1)
allocCnt = allocCnt.Add(sdk.NewInt(r.Int63n(5) + 1))
maxBidCnt = maxBidCnt.AddRaw(r.Int63n(10000) + 1)
}

Expand All @@ -57,17 +57,17 @@ func ParticipationGenesisStateWithAllocations(r *rand.Rand) participation.Genesi
addr := Address(r)
usedAllocs := participation.UsedAllocations{
Address: addr,
NumAllocations: 0,
NumAllocations: sdk.ZeroInt(),
}
for j := 0; j < 3; j++ {
auctionUsedAllocs := participation.AuctionUsedAllocations{
Address: addr,
AuctionID: uint64(j),
NumAllocations: uint64(r.Int63n(5) + 1),
NumAllocations: sdk.NewInt(r.Int63n(5) + 1),
Withdrawn: false,
}
genState.AuctionUsedAllocationsList = append(genState.AuctionUsedAllocationsList, auctionUsedAllocs)
usedAllocs.NumAllocations += auctionUsedAllocs.NumAllocations
usedAllocs.NumAllocations = usedAllocs.NumAllocations.Mul(sdk.NewInt(2))
}
genState.UsedAllocationsList = append(genState.UsedAllocationsList, usedAllocs)
}
Expand Down
5 changes: 5 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,8 @@ func Duration(r *rand.Rand) time.Duration {
func DurationFromRange(r *rand.Rand, min, max time.Duration) time.Duration {
return time.Duration(r.Int63n(int64(max-min))) + min
}

// Int returns a sample sdk.Int
func Int(r *rand.Rand) sdk.Int {
return sdk.NewInt(r.Int63())
}
2 changes: 1 addition & 1 deletion x/claim/keeper/claim_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func createNClaimRecord(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.C
items := make([]types.ClaimRecord, n)
for i := range items {
items[i].Address = sample.Address(r)
items[i].Claimable = sdk.NewInt(r.Int63())
items[i].Claimable = sample.Int(r)

keeper.SetClaimRecord(ctx, items[i])
}
Expand Down
4 changes: 2 additions & 2 deletions x/claim/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestGenesisState_Validate(t *testing.T) {
require.NoError(t, err)

claimAmts := []sdk.Int{
sdk.NewInt(r.Int63()),
sdk.NewInt(r.Int63()),
sample.Int(r),
sample.Int(r),
}

for _, tt := range []struct {
Expand Down
3 changes: 2 additions & 1 deletion x/participation/keeper/auction_used_allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func createNAuctionUsedAllocations(keeper *keeper.Keeper, ctx sdk.Context, n int
for i := range items {
items[i].Address = strconv.Itoa(i)
items[i].AuctionID = uint64(i)

items[i].NumAllocations = sample.Int(r)
keeper.SetAuctionUsedAllocations(ctx, items[i])
}
return items
Expand All @@ -31,6 +31,7 @@ func createNAuctionUsedAllocationsWithSameAddress(keeper *keeper.Keeper, ctx sdk
for i := range items {
items[i].Address = address
items[i].AuctionID = uint64(i)
items[i].NumAllocations = sample.Int(r)

keeper.SetAuctionUsedAllocations(ctx, items[i])
}
Expand Down
10 changes: 5 additions & 5 deletions x/participation/keeper/available_allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

// GetAvailableAllocations returns the number of allocations that are unused
func (k Keeper) GetAvailableAllocations(ctx sdk.Context, address string) (uint64, error) {
func (k Keeper) GetAvailableAllocations(ctx sdk.Context, address string) (sdk.Int, error) {
numTotalAlloc, err := k.GetTotalAllocations(ctx, address)
if err != nil {
return 0, err
return sdk.ZeroInt(), err
}

usedAlloc, found := k.GetUsedAllocations(ctx, address)
Expand All @@ -17,11 +17,11 @@ func (k Keeper) GetAvailableAllocations(ctx sdk.Context, address string) (uint64
}

// return 0 if result would be negative
if usedAlloc.NumAllocations > numTotalAlloc {
return 0, nil
if usedAlloc.NumAllocations.GT(numTotalAlloc) {
return sdk.ZeroInt(), nil
}

availableAlloc := numTotalAlloc - usedAlloc.NumAllocations
availableAlloc := numTotalAlloc.Sub(usedAlloc.NumAllocations)

return availableAlloc, nil
}
12 changes: 6 additions & 6 deletions x/participation/keeper/available_allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,35 @@ func TestAvailableAllocationsGet(t *testing.T) {

tk.ParticipationKeeper.SetUsedAllocations(sdkCtx, types.UsedAllocations{
Address: validAddress,
NumAllocations: 2,
NumAllocations: sdk.NewInt(2),
})

// set used allocations to be greater than totalAllocations
tk.ParticipationKeeper.SetUsedAllocations(sdkCtx, types.UsedAllocations{
Address: validAddressExtraUsed,
NumAllocations: 11,
NumAllocations: sdk.NewInt(11),
})

for _, tc := range []struct {
desc string
address string
allocation uint64
allocation sdk.Int
wantError bool
}{
{
desc: "valid address with used allocations",
address: validAddress,
allocation: 8, // (100 * 10 / 100) - 2 = 8
allocation: sdk.NewInt(8), // (100 * 10 / 100) - 2 = 8
},
{
desc: "valid address with no used allocations",
address: validAddressNoUse,
allocation: 10, // (100 * 10 / 100) - 0 = 10
allocation: sdk.NewInt(10), // (100 * 10 / 100) - 0 = 10
},
{
desc: "return 0 when usedAllocations > totalAllocations",
address: validAddressExtraUsed,
allocation: 0, // 11 > 10 - > return 0
allocation: sdk.NewInt(0), // 11 > 10 - > return 0
},
{
desc: "invalid address returns error",
Expand Down
2 changes: 1 addition & 1 deletion x/participation/keeper/grpc_available_allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestShowAvailableAllocationsQuery(t *testing.T) {
request: &types.QueryGetAvailableAllocationsRequest{
Address: dels[0].DelegatorAddress,
},
response: &types.QueryGetAvailableAllocationsResponse{AvailableAllocations: 10},
response: &types.QueryGetAvailableAllocationsResponse{AvailableAllocations: sdk.NewInt(10)},
},

{
Expand Down
2 changes: 1 addition & 1 deletion x/participation/keeper/grpc_total_allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestShowTotalAllocationsQuery(t *testing.T) {
request: &types.QueryGetTotalAllocationsRequest{
Address: dels[0].DelegatorAddress,
},
response: &types.QueryGetTotalAllocationsResponse{TotalAllocations: 10},
response: &types.QueryGetTotalAllocationsResponse{TotalAllocations: sdk.NewInt(10)},
},

{
Expand Down
2 changes: 1 addition & 1 deletion x/participation/keeper/grpc_used_allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (k Keeper) UsedAllocations(c context.Context, req *types.QueryGetUsedAlloca
return &types.QueryGetUsedAllocationsResponse{
UsedAllocations: types.UsedAllocations{
Address: req.Address,
NumAllocations: 0,
NumAllocations: sdk.ZeroInt(),
},
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion x/participation/keeper/grpc_used_allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestUsedAllocationsQuerySingle(t *testing.T) {
request: &types.QueryGetUsedAllocationsRequest{
Address: validAddr,
},
response: &types.QueryGetUsedAllocationsResponse{UsedAllocations: types.UsedAllocations{Address: validAddr, NumAllocations: 0}},
response: &types.QueryGetUsedAllocationsResponse{UsedAllocations: types.UsedAllocations{Address: validAddr, NumAllocations: sdk.ZeroInt()}},
},
{
desc: "InvalidAddress",
Expand Down
6 changes: 3 additions & 3 deletions x/participation/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func MismatchUsedAllocationsInvariant(k Keeper) sdk.Invariant {
all := k.GetAllUsedAllocations(ctx)
for _, usedAllocs := range all {
auctionUsedAllocs := k.GetAllAuctionUsedAllocationsByAddress(ctx, usedAllocs.Address)
sum := uint64(0)
sum := sdk.ZeroInt()
for _, auction := range auctionUsedAllocs {
if !auction.Withdrawn {
sum += auction.NumAllocations
sum = sum.Add(auction.NumAllocations)
}
}
if sum != usedAllocs.NumAllocations {
if !sum.Equal(usedAllocs.NumAllocations) {
return sdk.FormatInvariant(
types.ModuleName, mismatchUsedAllocationsRoute,
"total used allocations not equal to sum of per-auction used allocations",
Expand Down
Loading

0 comments on commit f310279

Please sign in to comment.