Skip to content

Commit

Permalink
Fixed conversion unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSnow committed Apr 19, 2021
1 parent bb37007 commit ba4f9cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
34 changes: 18 additions & 16 deletions node/conversions/conversionlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"testing"

. "github.com/pegnet/pegnetd/node/conversions"

"github.com/pegnet/pegnet/modules/transactionid"
)

Expand Down Expand Up @@ -184,12 +186,12 @@ func TestRefund(t *testing.T) {
// Currently the PEG supply limit yields are calculated as such:
// amt pXXX -> yielded PEG + refund pXXX
t.Run("test equivalency", func(t *testing.T) {
for i := 0; i < 50; i++ {
for i := uint32(0); i < 50; i++ {
amtR := rand.Uint64() % (5 * 1e6 * 1e8) // 50K max
pegR := rand.Uint64() % (5 * 1e6 * 1e8) // 50K max

input := rand.Int63() % (1 * 1e6 * 1e8) // 1million max
maxPegYield, err := Convert(int64(input), amtR, pegR)
maxPegYield, err := Convert(i, int64(input), amtR, amtR, pegR, pegR)
if err != nil {
continue // Likely an overflow or rate is 0
}
Expand All @@ -199,11 +201,11 @@ func TestRefund(t *testing.T) {
// 2 methods to calculate the refund. We have:
// Input in pXXX, yield in PEG

refund := Refund(input, yield, amtR, pegR)
refund := Refund(i, input, yield, amtR, pegR)
if refund < 0 {
t.Error("Negative refund!")
}
CheckRefund(t, input, refund, yield, amtR, pegR)
CheckRefund(t, i, input, refund, yield, amtR, pegR)
}
}
})
Expand All @@ -214,12 +216,12 @@ func TestRefund(t *testing.T) {
pegR := rand.Uint64() % (5 * 1e6 * 1e8) // 50K max
input := rand.Int63() % (1 * 1e6 * 1e8) // 1million max

maxPegYield, err := Convert(int64(input), amtR, pegR)
maxPegYield, err := Convert(uint32(i), int64(input), amtR, amtR, pegR, pegR)
if err != nil {
continue // Likely an overflow or rate is 0
}

if r := Refund(input, maxPegYield, amtR, pegR); r != 0 {
if r := Refund(uint32(i), input, maxPegYield, amtR, pegR); r != 0 {
t.Errorf("expected a 0 refund, found %d", r)
}
}
Expand All @@ -233,10 +235,10 @@ func TestRefund(t *testing.T) {
//
// Does not hold for Asset Equivalency check
// Does hold for the 0 refund case
func RefundMethod1(input, pegYield int64, amtRate, pegRate uint64) int64 {
maxPEGYield, _ := Convert(input, amtRate, pegRate)
func RefundMethod1(height uint32, input, pegYield int64, amtRate, pegRate uint64) int64 {
maxPEGYield, _ := Convert(height, input, amtRate, amtRate, pegRate, pegRate)
refundPEG := maxPEGYield - pegYield
refund, _ := Convert(refundPEG, pegRate, amtRate)
refund, _ := Convert(height, refundPEG, pegRate, pegRate, amtRate, amtRate)
return refund
}

Expand All @@ -246,8 +248,8 @@ func RefundMethod1(input, pegYield int64, amtRate, pegRate uint64) int64 {
//
// Holds in all equivalency conditions
// Does not hold for the 0 refund case
func RefundMethod2(input, pegYield int64, amtRate, pegRate uint64) int64 {
consumedInput, _ := Convert(pegYield, pegRate, amtRate)
func RefundMethod2(height uint32, input, pegYield int64, amtRate, pegRate uint64) int64 {
consumedInput, _ := Convert(height, pegYield, pegRate, pegRate, amtRate, amtRate)
refund := input - consumedInput
return refund
}
Expand All @@ -256,23 +258,23 @@ func RefundMethod2(input, pegYield int64, amtRate, pegRate uint64) int64 {
// amt is in pXXX
// refund is in pXXX
// pegYield is in PEG
func CheckRefund(t *testing.T, input, refund, pegYield int64, amtRate, pegRate uint64) {
func CheckRefund(t *testing.T, height uint32, input, refund, pegYield int64, amtRate, pegRate uint64) {
max := func(a, b int64) int64 {
if a > b {
return a
}
return b
}

maxPegYield, err := Convert(input, amtRate, pegRate)
maxPegYield, err := Convert(height, input, amtRate, amtRate, pegRate, pegRate)
if err != nil {
return // Overflow or 0 rates
}

{
// Asset Equivalency
// This check is `input = refund + (peg converted to input)`
yieldInAsset, err := Convert(pegYield, pegRate, amtRate)
yieldInAsset, err := Convert(height, pegYield, pegRate, pegRate, amtRate, amtRate)
if err != nil {
t.Error(err) // This would be bad news
}
Expand All @@ -296,12 +298,12 @@ func CheckRefund(t *testing.T, input, refund, pegYield int64, amtRate, pegRate u
// consumed = input - refund
// consumed -> PEG + refund -> PEG = input -> PEG
consumed := int64(input) - refund
consumedPEG, err := Convert(consumed, amtRate, pegRate)
consumedPEG, err := Convert(height, consumed, amtRate, amtRate, pegRate, pegRate)
if err != nil {
t.Error(err) // This would be bad news
}

refundPEGCheck, err := Convert(refund, amtRate, pegRate)
refundPEGCheck, err := Convert(height, refund, amtRate, amtRate, pegRate, pegRate)
if err != nil {
t.Error(err) // This would be bad news
}
Expand Down
14 changes: 8 additions & 6 deletions node/conversions/conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math/rand"
"testing"

"github.com/pegnet/pegnetd/node/conversions"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -52,7 +54,7 @@ func TestConversions_Convert_Vectors(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
assert := assert.New(t)

observedY1, err := Convert(test.X1, test.XRate, test.YRate)
observedY1, err := conversions.Convert(1, test.X1, test.XRate, test.XRate, test.YRate, test.YRate)
if len(test.ErrorString) != 0 {
assert.EqualError(err, test.ErrorString)
return
Expand All @@ -63,13 +65,13 @@ func TestConversions_Convert_Vectors(t *testing.T) {
// Due to truncation in integer division, there is often error present in the
// conversion from Y back to X. Thus, we check that it is within the expected
// margin of error.
observedX2, err := Convert(test.Y1, test.YRate, test.XRate)
observedX2, err := conversions.Convert(1, test.Y1, test.YRate, test.YRate, test.XRate, test.XRate)
require.NoError(t, err)
observedError := abs(test.X1 - observedX2)
maxExpectedError := maxConversionError(test.XRate, test.YRate)
require.True(t, observedError <= maxExpectedError, "Margin of error exceeded for conversion Y1 --> X2")

observedY2, err := Convert(test.X2, test.XRate, test.YRate)
observedY2, err := conversions.Convert(1, test.X2, test.XRate, test.XRate, test.YRate, test.YRate)
require.NoError(t, err)
observedError = abs(test.Y1 - observedY2)
assert.True(observedError <= maxExpectedError, "Margin of error exceeded for conversion X2 --> Y2")
Expand All @@ -89,7 +91,7 @@ func TestConversions_Convert_Random(t *testing.T) {
t.Run(fmt.Sprintf("Iteration %d", i), func(t *testing.T) {
assert := assert.New(t)

y1, err := Convert(x1, xRate, yRate)
y1, err := conversions.Convert(1, x1, xRate, xRate, yRate, yRate)
if len(expectedErrorString) != 0 {
assert.EqualError(err, expectedErrorString)
return
Expand All @@ -99,13 +101,13 @@ func TestConversions_Convert_Random(t *testing.T) {
// Due to truncation in integer division, there is often error present in the
// conversion from Y back to X. Thus, we check that it is within the expected
// margin of error.
x2, err := Convert(y1, yRate, xRate)
x2, err := conversions.Convert(1, y1, yRate, yRate, xRate, xRate)
require.NoError(t, err)
observedError := abs(x1 - x2)
maxExpectedError := maxConversionError(yRate, xRate)
assert.True(observedError <= maxExpectedError, "Margin of error exceeded for Y1 --> X2: observedError=%d, maxError=%d", observedError, maxExpectedError)

y2, err := Convert(x2, xRate, yRate)
y2, err := conversions.Convert(1, x2, xRate, xRate, yRate, yRate)
require.NoError(t, err)
observedError = abs(y1 - y2)
maxExpectedError = maxConversionError(xRate, yRate)
Expand Down

0 comments on commit ba4f9cd

Please sign in to comment.