Skip to content

Commit

Permalink
fix: precision conversion & vuln packages (#14)
Browse files Browse the repository at this point in the history
1 MFX == 1000 umfx, not the other way around
  • Loading branch information
fmorency authored Apr 5, 2024
1 parent 7105829 commit 26d56f9
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/liftedinit/mfx-migrator

go 1.22.1
go 1.22.2

require (
cosmossdk.io/math v1.3.0
Expand Down Expand Up @@ -139,7 +139,7 @@ require (
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.22.1
go 1.22.2

use (
.
Expand Down
4 changes: 2 additions & 2 deletions interchaintest/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/liftedinit/mfx-migrator/interchaintest

go 1.22.1
go 1.22.2

replace (
cosmossdk.io/core => cosmossdk.io/core v0.11.0 // proper SDK v50 version
Expand Down Expand Up @@ -234,7 +234,7 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions interchaintest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
41 changes: 17 additions & 24 deletions interchaintest/migrate_on_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,8 @@ func TestMigrateOnChain(t *testing.T) {
"--chain-home", tmpdir,
}

defaultGenesisAmtMinOne := DefaultGenesisAmt.Sub(math.NewInt(1)) // Genesis amount - 1
defaultGenesisAmtPlusOne := DefaultGenesisAmt.Add(math.NewInt(1)) // Genesis amount + 1
currentPrecision := 9 // The precision of the "MANY" token
targetPrecision := 6 // The precision of the "MANIFEST" token
precision := currentPrecision - targetPrecision // The precision difference
multiplier := math.NewIntWithDecimal(1, precision) // 1e${precision}

defaultGenesisAmtPlusOneTargetPrec := defaultGenesisAmtPlusOne.Mul(multiplier).String()
defaultGenesisAmtMinOneTargetPrec := defaultGenesisAmtMinOne.Mul(multiplier).String()
defaultGenesisAmtPlusThousand := DefaultGenesisAmt.Add(math.NewInt(1000)) // Genesis amount + 1000
defaultGenesisAmtMinThousand := DefaultGenesisAmt.Sub(math.NewInt(1000)) // Genesis amount - 1000

tt := []struct {
name string
Expand All @@ -86,41 +79,41 @@ func TestMigrateOnChain(t *testing.T) {
expected Expected
endpoints []testutils.HttpResponder
}{
{name: "0 token after conversion", args: slice, endpoints: []testutils.HttpResponder{
{name: "1:1000 tokens", args: slice, endpoints: []testutils.HttpResponder{
{Method: "POST", Url: testutils.LoginUrl, Responder: testutils.AuthResponder},
{Method: "GET", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MustMigrationGetResponder(store.CLAIMED)},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder("1")},
{Method: "PUT", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MigrationUpdateResponder},
}, expected: Expected{
Bank: Amounts{Old: DefaultGenesisAmt},
User: Amounts{Old: math.ZeroInt()},
}, err: "amount after conversion is less than or equal to 0"},
Bank: Amounts{Old: DefaultGenesisAmt, New: defaultGenesisAmtMinThousand},
User: Amounts{Old: math.ZeroInt(), New: math.NewInt(1000)},
}},
{name: "insufficient funds", args: slice, endpoints: []testutils.HttpResponder{
{Method: "POST", Url: testutils.LoginUrl, Responder: testutils.AuthResponder},
{Method: "GET", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MustMigrationGetResponder(store.CLAIMED)},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder(defaultGenesisAmtPlusOneTargetPrec)},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder(defaultGenesisAmtPlusThousand.String())},
{Method: "PUT", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MigrationUpdateResponder},
}, expected: Expected{
Bank: Amounts{Old: DefaultGenesisAmt},
User: Amounts{Old: math.ZeroInt()},
Bank: Amounts{Old: defaultGenesisAmtMinThousand},
User: Amounts{Old: math.NewInt(1000)},
}, err: "insufficient funds"},
{name: "1000:1 tokens", args: slice, endpoints: []testutils.HttpResponder{
{name: "amount after conversion is <= 0", args: slice, endpoints: []testutils.HttpResponder{
{Method: "POST", Url: testutils.LoginUrl, Responder: testutils.AuthResponder},
{Method: "GET", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MustMigrationGetResponder(store.CLAIMED)},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder("1000")},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder("0")},
{Method: "PUT", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MigrationUpdateResponder},
}, expected: Expected{
Bank: Amounts{Old: DefaultGenesisAmt, New: defaultGenesisAmtMinOne},
User: Amounts{Old: math.ZeroInt(), New: math.OneInt()},
}},
Bank: Amounts{Old: defaultGenesisAmtMinThousand},
User: Amounts{Old: math.NewInt(1000)},
}, err: "amount after conversion is less than or equal to 0"},
{name: "all tokens from bank", args: slice, endpoints: []testutils.HttpResponder{
{Method: "POST", Url: testutils.LoginUrl, Responder: testutils.AuthResponder},
{Method: "GET", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MustMigrationGetResponder(store.CLAIMED)},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder(defaultGenesisAmtMinOneTargetPrec)},
{Method: "GET", Url: "=~^" + testutils.DefaultTransactionUrl, Responder: testutils.MustNewTransactionResponseResponder(defaultGenesisAmtMinThousand.Quo(math.NewInt(1000)).String())}, // (Genesis amount - 1000) / 1000
{Method: "PUT", Url: "=~^" + testutils.DefaultMigrationUrl, Responder: testutils.MigrationUpdateResponder},
}, expected: Expected{
Bank: Amounts{Old: defaultGenesisAmtMinOne, New: math.ZeroInt()},
User: Amounts{Old: math.OneInt(), New: DefaultGenesisAmt},
Bank: Amounts{Old: defaultGenesisAmtMinThousand, New: math.ZeroInt()},
User: Amounts{Old: math.NewInt(1000), New: DefaultGenesisAmt},
}},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/utils/precision.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ConvertPrecision(n string, currentPrecision uint64, targetPrecision uint64)
slog.Debug("Multiplier", "multiplier", multiplier)

var result *big.Int
if precisionDiff > 0 {
if precisionDiff < 0 {
// // Increase precision by multiplying
result = new(big.Int).Mul(bi, multiplier)
} else {
Expand Down
23 changes: 13 additions & 10 deletions internal/utils/precision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
)

func TestConvertPrecision(t *testing.T) {
n1 := "1" // 1e1
n3 := "1000" // 1e3
n24 := "1000000000000000000000000" // 1e24
n18 := "1000000000000000000" // 1e18
n1 := "1" // 1e1
n3 := "1000" // 1e3
n24 := "1000000000000000000000000" // 1e24
n30 := "1000000000000000000000000000000" // 1e30
n18 := "1000000000000000000" // 1e18
n12 := "1000000000000" // 1e12
nF := "12.345"

bigIntValue24, _ := new(big.Int).SetString(n24, 10)
bigIntValue18, _ := new(big.Int).SetString(n18, 10)
bigIntValue30, _ := new(big.Int).SetString(n30, 10)
bigIntValue12, _ := new(big.Int).SetString(n12, 10)

tt := []struct {
name string
Expand All @@ -28,10 +30,11 @@ func TestConvertPrecision(t *testing.T) {
err string
}{
{name: "no precision change", n: n18, currentPrecision: 18, targetPrecision: 18, expected: nil, err: "current precision is equal to target precision: 18"},
{name: "increase precision", n: n18, currentPrecision: 18, targetPrecision: 24, expected: bigIntValue24},
{name: "decrease precision", n: n24, currentPrecision: 24, targetPrecision: 18, expected: bigIntValue18},
{name: "decrease precision 2", n: n3, currentPrecision: 9, targetPrecision: 6, expected: big.NewInt(1)},
{name: "invalid conversion (amount <= 0)", n: n1, currentPrecision: 3, targetPrecision: 1, err: "amount after conversion is less than or equal to 0: 0"},
{name: "increase precision", n: n18, currentPrecision: 18, targetPrecision: 24, expected: bigIntValue12},
{name: "decrease precision", n: n24, currentPrecision: 24, targetPrecision: 18, expected: bigIntValue30},
{name: "decrease precision 2", n: n3, currentPrecision: 9, targetPrecision: 6, expected: big.NewInt(1000000)},
{name: "decrease precision 3", n: n1, currentPrecision: 9, targetPrecision: 6, expected: big.NewInt(1000)},
{name: "invalid conversion (amount <= 0)", n: n1, currentPrecision: 1, targetPrecision: 3, err: "amount after conversion is less than or equal to 0: 0"},
{name: "invalid number (scientific notation)", n: "1e18", currentPrecision: 18, targetPrecision: 24, err: "error parsing big.Int: 1e18"},
{name: "invalid number (not a number)", n: "foo", currentPrecision: 18, targetPrecision: 24, err: "error parsing big.Int: foo"},
{name: "invalid number (empty string)", n: "", currentPrecision: 18, targetPrecision: 24, err: "error parsing big.Int: "},
Expand Down

0 comments on commit 26d56f9

Please sign in to comment.