Skip to content

Commit

Permalink
feat(auction) msgs interfaces and validation (#2475)
Browse files Browse the repository at this point in the history
* update auction proto

* checkers: number

* tests in parallel

* msgs implementation and tests

* update checkers messages

* comment

* use coin for bid amount

* update msgs

* update coin fixtures
  • Loading branch information
robert-zaremba authored Mar 28, 2024
1 parent 1be3ed5 commit 740718f
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 127 deletions.
3 changes: 0 additions & 3 deletions proto/umee/auction/v1/auction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ option (gogoproto.goproto_getters_all) = false;
message RewardsParams {
// bid_duration is duration of the bid phase in seconds.
int64 bid_duration = 1;
// Duration (in seconds) at the end of each auction, when we start collecting revenues for
// the next auction.
int64 revenu_collection_shift = 2;
}
10 changes: 6 additions & 4 deletions proto/umee/auction/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ message GenesisState {
// Latest active (in bid phase) reward auction.
uint32 reward_auction_id = 2;
// Latest highest bid.
string highest_bidder = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string highest_bidder = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
cosmos.base.v1beta1.Coin highest_bid = 4 [(gogoproto.nullable) = false];

// Tokens collected for the current auction.
repeated cosmos.base.v1beta1.Coin current_rewards = 4 [(gogoproto.nullable) = false];
repeated cosmos.base.v1beta1.Coin current_rewards = 5 [(gogoproto.nullable) = false];
// Tokens collected for the next auction, while the current reward auction is still in the
// bid phase.
repeated cosmos.base.v1beta1.Coin next_rewards = 5 [(gogoproto.nullable) = false];
google.protobuf.Timestamp current_rewards_auction_end = 6 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
repeated cosmos.base.v1beta1.Coin next_rewards = 6 [(gogoproto.nullable) = false];
google.protobuf.Timestamp current_rewards_auction_end = 7 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}
7 changes: 3 additions & 4 deletions proto/umee/auction/v1/tx.proto
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
syntax = "proto3";
package umee.auction.v1;

import "cosmos/base/v1beta1/coin.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/msg/v1/msg.proto";
import "gogoproto/gogo.proto";
import "umee/auction/v1/auction.proto";

option go_package = "github.com/umee-network/umee/v6/x/auction";
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.messagename_all) = true;

// Msg defines the x/auction module's Msg service.
service Msg {
//
Expand Down Expand Up @@ -43,8 +42,8 @@ message MsgRewardsBid {
string sender = 1;
// the current auction ID being bid on. Fails if the ID is not an ID of the current auction.
uint32 id = 2;
// amount of the bid in the base tokens
cosmos.base.v1beta1.Coin bid_amount = 3 [(gogoproto.nullable) = false];
// amount of the bid in the bond base tokens (uumee)
cosmos.base.v1beta1.Coin amount = 3 [(gogoproto.nullable) = false];
}

// MsgRewardsBidResponse response type for Msg/RewardsBid
Expand Down
25 changes: 23 additions & 2 deletions util/checkers/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,36 @@ func IntegerMaxDiff[T constraints.Integer](a, b, maxDiff T, note string) error {
diff = b - a
}
if diff > maxDiff {
return fmt.Errorf("%s, diff (=%v) is too big", note, diff)
return fmt.Errorf("%s: diff (=%v) is too big", note, diff)
}
return nil
}

func NumberMin[T constraints.Integer](a, minVal T, note string) error {
if a < minVal {
return fmt.Errorf("%s: must be at least %v", note, minVal)
}
return nil
}

func NumberPositive[T constraints.Integer](a T, note string) error {
if a <= 0 {
return fmt.Errorf("%s: must be defined and must be positive", note)
}
return nil
}

func BigNumPositive[T interface{ IsPositive() bool }](a T, note string) error {
if !a.IsPositive() {
return fmt.Errorf("%s: must be positive", note)
}
return nil
}

func DecMaxDiff(a, b, maxDiff sdk.Dec, note string) error {
diff := a.Sub(b).Abs()
if diff.GT(maxDiff) {
return fmt.Errorf("%s, diff (=%v) is too big", note, diff)
return fmt.Errorf("%s: diff (=%v) is too big", note, diff)
}
return nil
}
Expand Down
55 changes: 55 additions & 0 deletions util/checkers/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestNumberDiff(t *testing.T) {
t.Parallel()
assert := assert.New(t)

assert.NoError(IntegerMaxDiff(1, 1, 0, ""))
Expand All @@ -32,6 +33,7 @@ func TestNumberDiff(t *testing.T) {
}

func TestDecDiff(t *testing.T) {
t.Parallel()
assert := assert.New(t)
decMaxDiff := func(a, b, maxDiff float64) error {
return DecMaxDiff(tsdk.DecF(a), tsdk.DecF(b), tsdk.DecF(maxDiff), "")
Expand Down Expand Up @@ -61,6 +63,7 @@ func TestDecDiff(t *testing.T) {
}

func TestDecInZeroOne(t *testing.T) {
t.Parallel()
assert.NoError(t, DecInZeroOne(tsdk.DecF(0), "", true))
assert.NoError(t, DecInZeroOne(tsdk.DecF(0.01), "", true))
assert.NoError(t, DecInZeroOne(tsdk.DecF(0.999), "", true))
Expand All @@ -84,9 +87,61 @@ func TestDecInZeroOne(t *testing.T) {
}

func TestDecNotNegative(t *testing.T) {
t.Parallel()
assert.NotNil(t, DecNotNegative(tsdk.DecF(-1), ""))
assert.NotNil(t, DecNotNegative(sdk.Dec{}, ""))

assert.Nil(t, DecNotNegative(sdk.ZeroDec(), ""))
assert.Nil(t, DecNotNegative(tsdk.DecF(5), ""))
}

func TestNumPositive(t *testing.T) {
t.Parallel()
assert := assert.New(t)

assert.NoError(NumberPositive(1, ""))
assert.NoError(NumberPositive(2, ""))
assert.NoError(NumberPositive(9999999999999, ""))

assert.Error(NumberPositive(0, ""))
assert.Error(NumberPositive(-1, ""))
assert.Error(NumberPositive(-2, ""))
assert.Error(NumberPositive(-999999999999, ""))

assert.NoError(BigNumPositive(tsdk.DecF(1.01), ""))
assert.NoError(BigNumPositive(tsdk.DecF(0.000001), ""))
assert.NoError(BigNumPositive(tsdk.DecF(0.123), ""))
assert.NoError(BigNumPositive(tsdk.DecF(9999999999999999999), ""))

assert.Error(BigNumPositive(tsdk.DecF(0), ""))
assert.Error(BigNumPositive(tsdk.DecF(-0.01), ""))
assert.Error(BigNumPositive(sdk.NewDec(0), ""))
assert.Error(BigNumPositive(sdk.NewDec(-99999999999999999), ""))

assert.NoError(BigNumPositive(sdk.NewInt(1), ""))
assert.NoError(BigNumPositive(sdk.NewInt(2), ""))
assert.NoError(BigNumPositive(sdk.NewInt(9), ""))
n, ok := sdk.NewIntFromString("111111119999999999999999999")
assert.True(ok)
assert.NoError(BigNumPositive(n, ""))
}

func TestNumberMin(t *testing.T) {
t.Parallel()
assert := assert.New(t)

assert.NoError(NumberMin(-1, -10, ""))
assert.NoError(NumberMin(1, 0, ""))
assert.NoError(NumberMin(10, 2, ""))
assert.NoError(NumberMin(999999999999999, 2, ""))
assert.NoError(NumberMin(999999999999999, 999999999999998, ""))
assert.NoError(NumberMin(0, 0, ""))
assert.NoError(NumberMin(-10, -10, ""))
assert.NoError(NumberMin(999999999999999, 999999999999999, ""))

assert.Error(NumberMin(-10, -1, ""))
assert.Error(NumberMin(-1, 10, ""))
assert.Error(NumberMin(-10, 0, ""))
assert.Error(NumberMin(-10, 10, ""))
assert.Error(NumberMin(10, 11, ""))
}
7 changes: 6 additions & 1 deletion util/coin/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ var (

//revive:enable:var-naming

// UmeeCoins creates an Umee (uumee) sdk.Coins with given amount
// Umee creates a BondDenom sdk.Coin with the given amount
func Umee(amount int64) sdk.Coin {
return sdk.NewInt64Coin(umee, amount)
}

// UmeeCoins creates an Umee (uumee) sdk.Coins with the given amount
func UmeeCoins(amount int64) sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin(umee, amount))
}
Expand Down
48 changes: 8 additions & 40 deletions x/auction/auction.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 740718f

Please sign in to comment.