Skip to content

Commit

Permalink
remove unused method safeMulClip / safeMul
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Nov 16, 2018
1 parent a5a4953 commit 77c05d8
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 46 deletions.
30 changes: 1 addition & 29 deletions types/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,7 @@ func RandValidatorSet(numValidators int, votingPower int64) (*ValidatorSet, []Pr
}

///////////////////////////////////////////////////////////////////////////////
// Safe multiplication and addition/subtraction

func safeMul(a, b int64) (int64, bool) {
if a == 0 || b == 0 {
return 0, false
}
if a == 1 {
return b, false
}
if b == 1 {
return a, false
}
if a == math.MinInt64 || b == math.MinInt64 {
return -1, true
}
c := a * b
return c, c/b != a
}
// Safe addition/subtraction

func safeAdd(a, b int64) (int64, bool) {
if b > 0 && a > math.MaxInt64-b {
Expand All @@ -534,17 +517,6 @@ func safeSub(a, b int64) (int64, bool) {
return a - b, false
}

func safeMulClip(a, b int64) int64 {
c, overflow := safeMul(a, b)
if overflow {
if (a < 0 || b < 0) && !(a < 0 && b < 0) {
return math.MinInt64
}
return math.MaxInt64
}
return c
}

func safeAddClip(a, b int64) int64 {
c, overflow := safeAdd(a, b)
if overflow {
Expand Down
17 changes: 0 additions & 17 deletions types/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,6 @@ func TestValidatorSetIncrementAccumUnderflows(t *testing.T) {
assert.EqualValues(t, math.MinInt64, vset.Validators[1].Accum, "1")
}

func TestSafeMul(t *testing.T) {
f := func(a, b int64) bool {
c, overflow := safeMul(a, b)
return overflow || (!overflow && c == a*b)
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}

func TestSafeAdd(t *testing.T) {
f := func(a, b int64) bool {
c, overflow := safeAdd(a, b)
Expand All @@ -348,13 +338,6 @@ func TestSafeAdd(t *testing.T) {
}
}

func TestSafeMulClip(t *testing.T) {
assert.EqualValues(t, math.MaxInt64, safeMulClip(math.MinInt64, math.MinInt64))
assert.EqualValues(t, math.MinInt64, safeMulClip(math.MaxInt64, math.MinInt64))
assert.EqualValues(t, math.MinInt64, safeMulClip(math.MinInt64, math.MaxInt64))
assert.EqualValues(t, math.MaxInt64, safeMulClip(math.MaxInt64, 2))
}

func TestSafeAddClip(t *testing.T) {
assert.EqualValues(t, math.MaxInt64, safeAddClip(math.MaxInt64, 10))
assert.EqualValues(t, math.MaxInt64, safeAddClip(math.MaxInt64, math.MaxInt64))
Expand Down

0 comments on commit 77c05d8

Please sign in to comment.