Skip to content

Commit

Permalink
Merge pull request #212 from cosmosquad-labs/211-apply-review-suggest…
Browse files Browse the repository at this point in the history
…ions-1

refactor: code review suggestions-1
  • Loading branch information
dongsam authored Feb 25, 2022
2 parents c0e414a + fb14c50 commit 5d87fc9
Show file tree
Hide file tree
Showing 26 changed files with 631 additions and 442 deletions.
9 changes: 1 addition & 8 deletions proto/squad/mint/v1beta1/mint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ import "google/protobuf/timestamp.proto";

// Params holds parameters for the mint module.
message Params {
option (gogoproto.goproto_stringer) = false;

// type of coin to mint
string mint_denom = 1;

// block_time_threshold is a safety to accept only blocktime as much as it and give inflation even if the chain stops
// or the last blocktime is huge by manipulating blocktime.
google.protobuf.Duration block_time_threshold = 2 [
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true,
(gogoproto.jsontag) = "block_time_threshold,omitempty",
(gogoproto.moretags) = "yaml:\"block_time_threshold\""
];
google.protobuf.Duration block_time_threshold = 2 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];

repeated InflationSchedule inflation_schedules = 3 [(gogoproto.nullable) = false];
}
Expand Down
15 changes: 6 additions & 9 deletions types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,18 @@ func PP(data interface{}) {
fmt.Printf("%s \n", p)
}

// MustParseRFC3339 parses string time to time in RFC3339 format.
func MustParseRFC3339(s string) time.Time {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return t
}

// DateRangesOverlap returns true if two date ranges overlap each other.
// End time is exclusive and start time is inclusive.
func DateRangesOverlap(startTimeA, endTimeA, startTimeB, endTimeB time.Time) bool {
return startTimeA.Before(endTimeB) && endTimeA.After(startTimeB)
}

// DateRangeIncludes returns true if the target date included on the start, end time range.
// End time is exclusive and start time is inclusive.
func DateRangeIncludes(startTime, endTime, targetTime time.Time) bool {
return endTime.After(targetTime) && !startTime.After(targetTime)
}

// ParseDec is a shortcut for sdk.MustNewDecFromStr.
func ParseDec(s string) sdk.Dec {
return sdk.MustNewDecFromStr(s)
Expand Down
121 changes: 94 additions & 27 deletions types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestParseTime(t *testing.T) {
require.NoError(t, err)
errorCase := "9999-12-31T00:00:00_ErrorCase"
_, err = time.Parse(time.RFC3339, errorCase)
require.PanicsWithError(t, err.Error(), func() { types.MustParseRFC3339(errorCase) })
require.Equal(t, normalRes, types.MustParseRFC3339(normalCase))
require.PanicsWithError(t, err.Error(), func() { types.ParseTime(errorCase) })
require.Equal(t, normalRes, types.ParseTime(normalCase))
}

func TestDateRangesOverlap(t *testing.T) {
Expand All @@ -55,50 +55,66 @@ func TestDateRangesOverlap(t *testing.T) {
{
"not overlapping",
false,
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-02T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.MustParseRFC3339("2021-12-04T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-04T00:00:00Z"),
},
{
"same end time and start time",
"not overlapped on same end time and start time",
false,
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-02T00:00:00Z"),
types.MustParseRFC3339("2021-12-02T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
},
{
"not overlapped on same end time and start time 2",
false,
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
},
{
"for the same time, it doesn't seem to overlap",
false,
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
},
{
"end time and start time differs by a little amount",
true,
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-02T00:00:00.001Z"),
types.MustParseRFC3339("2021-12-02T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00.01Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
},
{
"overlap #1",
true,
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.MustParseRFC3339("2021-12-02T00:00:00Z"),
types.MustParseRFC3339("2021-12-04T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-04T00:00:00Z"),
},
{
"overlap #2 - same ranges",
true,
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
},
{
"overlap #3 - one includes another",
true,
types.MustParseRFC3339("2021-12-02T00:00:00Z"),
types.MustParseRFC3339("2021-12-03T00:00:00Z"),
types.MustParseRFC3339("2021-12-01T00:00:00Z"),
types.MustParseRFC3339("2021-12-04T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-04T00:00:00Z"),
},
}
for _, tc := range testCases {
Expand All @@ -108,3 +124,54 @@ func TestDateRangesOverlap(t *testing.T) {
})
}
}

func TestDateRangeIncludes(t *testing.T) {
testCases := []struct {
name string
expectedResult bool
targeTime time.Time
startTime time.Time
endTime time.Time
}{
{
"not included, before started",
false,
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:01Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
},
{
"not included, after ended",
false,
types.ParseTime("2021-12-03T00:00:01Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
},
{
"included on start time",
true,
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-03T00:00:00Z"),
},
{
"not included on end time",
false,
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-01T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
},
{
"not included on same start time and end time",
false,
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
types.ParseTime("2021-12-02T00:00:00Z"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expectedResult, types.DateRangeIncludes(tc.startTime, tc.endTime, tc.targeTime))
})
}
}
Loading

0 comments on commit 5d87fc9

Please sign in to comment.