From 26d56f985fdfea4a1c74ebd97137ad0833305ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20C=2E=20Morency?= <1102868+fmorency@users.noreply.github.com> Date: Fri, 5 Apr 2024 10:55:50 -0400 Subject: [PATCH] fix: precision conversion & vuln packages (#14) 1 MFX == 1000 umfx, not the other way around --- go.mod | 4 +-- go.sum | 2 ++ go.work | 2 +- interchaintest/go.mod | 4 +-- interchaintest/go.sum | 2 ++ interchaintest/migrate_on_chain_test.go | 41 ++++++++++--------------- internal/utils/precision.go | 2 +- internal/utils/precision_test.go | 23 ++++++++------ 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index 52fcfcd..796909b 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index b94efab..589f9b2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/go.work b/go.work index b2083a4..ea5261c 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.22.1 +go 1.22.2 use ( . diff --git a/interchaintest/go.mod b/interchaintest/go.mod index 47100f7..b7464dc 100644 --- a/interchaintest/go.mod +++ b/interchaintest/go.mod @@ -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 @@ -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 diff --git a/interchaintest/go.sum b/interchaintest/go.sum index 676efad..a14fa6d 100644 --- a/interchaintest/go.sum +++ b/interchaintest/go.sum @@ -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= diff --git a/interchaintest/migrate_on_chain_test.go b/interchaintest/migrate_on_chain_test.go index 381e055..0871afb 100644 --- a/interchaintest/migrate_on_chain_test.go +++ b/interchaintest/migrate_on_chain_test.go @@ -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 @@ -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}, }}, } diff --git a/internal/utils/precision.go b/internal/utils/precision.go index d6f9b12..26f0ece 100644 --- a/internal/utils/precision.go +++ b/internal/utils/precision.go @@ -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 { diff --git a/internal/utils/precision_test.go b/internal/utils/precision_test.go index ad5f550..261ff2f 100644 --- a/internal/utils/precision_test.go +++ b/internal/utils/precision_test.go @@ -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 @@ -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: "},