From fe16c03f5494fad0d432750ebca88ae72b94944e Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 26 Mar 2024 11:58:26 +0900 Subject: [PATCH 01/15] feat: change denom of coin that minted by banker --- gnovm/stdlibs/std/banker.go | 11 +++++++++-- tm2/pkg/std/coin.go | 3 +-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/gnovm/stdlibs/std/banker.go b/gnovm/stdlibs/std/banker.go index 7d0b339552a..0fda88a3a45 100644 --- a/gnovm/stdlibs/std/banker.go +++ b/gnovm/stdlibs/std/banker.go @@ -88,10 +88,17 @@ func X_bankerTotalCoin(m *gno.Machine, bt uint8, denom string) int64 { func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amount int64) { // gno checks for bt == RealmIssue - m.Context.(ExecContext).Banker.IssueCoin(crypto.Bech32Address(addr), denom, amount) + + // Similar to ibc spec + // ibc_denom := 'ibc/' + hash('path' + 'base_denom') + // gno_realm_denom := 'realm/' + 'pkg_path' + '/' + 'base_denom' + newDenom := "realm/" + m.Realm.Path + "/" + denom + m.Context.(ExecContext).Banker.IssueCoin(crypto.Bech32Address(addr), newDenom, amount) } func X_bankerRemoveCoin(m *gno.Machine, bt uint8, addr string, denom string, amount int64) { // gno checks for bt == RealmIssue - m.Context.(ExecContext).Banker.IssueCoin(crypto.Bech32Address(addr), denom, amount) + + newDenom := "realm/" + m.Realm.Path + "/" + denom + m.Context.(ExecContext).Banker.RemoveCoin(crypto.Bech32Address(addr), newDenom, amount) } diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index 75063320ad3..251af0455c8 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -616,8 +616,7 @@ func (coins Coins) Sort() Coins { // Parsing var ( - // Denominations can be 3 ~ 16 characters long. - reDnmString = `[a-z][a-z0-9]{2,15}` + reDnmString = `[a-zA-Z][a-zA-Z0-9/:._-]{2,127}` // cosmos regex(https://github.com/cosmos/cosmos-sdk/blob/b2c26cdc4c3d2f5b7cb28e5153e2b458c61ee8fa/types/coin.go#L852) reAmt = `[[:digit:]]+` reSpc = `[[:space:]]*` reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString)) From 3ad07b2c7b792d19e69cd7b80e051eeae21d257f Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 26 Mar 2024 11:58:39 +0900 Subject: [PATCH 02/15] test: txtar --- .../realm-banker-issued-coin-denom.txtar | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar diff --git a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar new file mode 100644 index 00000000000..bfabcef22e4 --- /dev/null +++ b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar @@ -0,0 +1,44 @@ +# test for https://github.com/gnolang/gno/pull/875 + +## start a new node +gnoland start + +## add realm_banker +gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/test/realm_banker -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 + +## check initial balance +gnokey query bank/balances/g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d +stdout '""' + +## mint coin from banker +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 + +## check balance after minting, without patching banker will return '31337ugnot' +gnokey query bank/balances/g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d +stdout '"31337realm/gno.land/r/test/realm_banker/ugnot"' + + +## burn coin +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 + +## check balance after burning +gnokey query bank/balances/g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d +stdout '"31330realm/gno.land/r/test/realm_banker/ugnot"' + + +-- realm_banker.gno -- +package realm_banker + +import ( + "std" +) + +func Mint(addr std.Address, denom string, amount int64) { + banker := std.GetBanker(std.BankerTypeRealmIssue) + banker.IssueCoin(addr, denom, amount) +} + +func Burn(addr std.Address, denom string, amount int64) { + banker := std.GetBanker(std.BankerTypeRealmIssue) + banker.RemoveCoin(addr, denom, amount) +} \ No newline at end of file From 8a71fe03b6a79d1a550b485aedb584f84133ffe4 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 26 Mar 2024 12:52:03 +0900 Subject: [PATCH 03/15] feat: new regex for denom of coin --- tm2/pkg/std/coin.go | 2 +- tm2/pkg/std/coin_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index 251af0455c8..b09a2a5ff4a 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -616,7 +616,7 @@ func (coins Coins) Sort() Coins { // Parsing var ( - reDnmString = `[a-zA-Z][a-zA-Z0-9/:._-]{2,127}` // cosmos regex(https://github.com/cosmos/cosmos-sdk/blob/b2c26cdc4c3d2f5b7cb28e5153e2b458c61ee8fa/types/coin.go#L852) + reDnmString = `[a-z][a-z0-9/:._-]{2,127}` // cosmos regex(https://github.com/cosmos/cosmos-sdk/blob/b2c26cdc4c3d2f5b7cb28e5153e2b458c61ee8fa/types/coin.go#L852) without captial letters reAmt = `[[:digit:]]+` reSpc = `[[:space:]]*` reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString)) diff --git a/tm2/pkg/std/coin_test.go b/tm2/pkg/std/coin_test.go index 8d9a9359b61..5c84a65161f 100644 --- a/tm2/pkg/std/coin_test.go +++ b/tm2/pkg/std/coin_test.go @@ -429,11 +429,11 @@ func TestParse(t *testing.T) { {"98 bar , 1 foo ", true, Coins{{"bar", int64(98)}, {"foo", one}}}, {" 55\t \t bling\n", true, Coins{{"bling", int64(55)}}}, {"2foo, 97 bar", true, Coins{{"bar", int64(97)}, {"foo", int64(2)}}}, + {"5foo-bar", true, Coins{{"foo-bar", int64(5)}}}, {"5 mycoin,", false, nil}, // no empty coins in a list {"2 3foo, 97 bar", false, nil}, // 3foo is invalid coin name {"11me coin, 12you coin", false, nil}, // no spaces in coin names {"1.2btc", false, nil}, // amount must be integer - {"5foo-bar", false, nil}, // once more, only letters in coin name } for tcIndex, tc := range cases { From cc634e6eabedc96ee1503367f0b3234f339e55e1 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 26 Mar 2024 13:02:43 +0900 Subject: [PATCH 04/15] fix: typo --- tm2/pkg/std/coin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index b09a2a5ff4a..b98c0fa289d 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -616,7 +616,7 @@ func (coins Coins) Sort() Coins { // Parsing var ( - reDnmString = `[a-z][a-z0-9/:._-]{2,127}` // cosmos regex(https://github.com/cosmos/cosmos-sdk/blob/b2c26cdc4c3d2f5b7cb28e5153e2b458c61ee8fa/types/coin.go#L852) without captial letters + reDnmString = `[a-z][a-z0-9/:._-]{2,127}` // cosmos regex(https://github.com/cosmos/cosmos-sdk/blob/b2c26cdc4c3d2f5b7cb28e5153e2b458c61ee8fa/types/coin.go#L852) without capital letters reAmt = `[[:digit:]]+` reSpc = `[[:space:]]*` reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString)) From 69276744d937bac73360a7e31f977f5355435b03 Mon Sep 17 00:00:00 2001 From: Blake <104744707+r3v4s@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:05:19 +0900 Subject: [PATCH 05/15] feat: change newDenom format Co-authored-by: Morgan --- gnovm/stdlibs/std/banker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gnovm/stdlibs/std/banker.go b/gnovm/stdlibs/std/banker.go index 0fda88a3a45..8503ce27e33 100644 --- a/gnovm/stdlibs/std/banker.go +++ b/gnovm/stdlibs/std/banker.go @@ -92,7 +92,7 @@ func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amou // Similar to ibc spec // ibc_denom := 'ibc/' + hash('path' + 'base_denom') // gno_realm_denom := 'realm/' + 'pkg_path' + '/' + 'base_denom' - newDenom := "realm/" + m.Realm.Path + "/" + denom + newDenom := "/" + m.Realm.Path + ":" + denom m.Context.(ExecContext).Banker.IssueCoin(crypto.Bech32Address(addr), newDenom, amount) } From db04868e77e3d2789e6ed6aa7f3754c3b445a479 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 29 Mar 2024 17:39:27 +0900 Subject: [PATCH 06/15] test: add `send` transaction in txtar --- .../realm-banker-issued-coin-denom.txtar | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar index bfabcef22e4..da0427707bd 100644 --- a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar +++ b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar @@ -7,24 +7,33 @@ gnoland start gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/test/realm_banker -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check initial balance -gnokey query bank/balances/g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d -stdout '""' +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout '"9999998000000ugnot"' ## mint coin from banker -gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check balance after minting, without patching banker will return '31337ugnot' -gnokey query bank/balances/g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d -stdout '"31337realm/gno.land/r/test/realm_banker/ugnot"' - +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout '"31337/gno.land/r/test/realm_banker:ugnot,9999996000000ugnot"' ## burn coin -gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check balance after burning -gnokey query bank/balances/g16kvq0mra3atvr07lkdwc2x6jqmna8a4kt0e85d -stdout '"31330realm/gno.land/r/test/realm_banker/ugnot"' +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout '"31330/gno.land/r/test/realm_banker:ugnot,9999994000000ugnot"' + +## transfer coin +gnokey maketx send -send "1330/gno.land/r/test/realm_banker:ugnot" -to g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 + +## check sender balance +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout '"30000/gno.land/r/test/realm_banker:ugnot,9999993000000ugnot"' +## check receiver balance +gnokey query bank/balances/g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 +stdout '"1330/gno.land/r/test/realm_banker:ugnot"' -- realm_banker.gno -- package realm_banker From 5ffae5c3836b2ce30007190d5f61ba9c2c1e436c Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 29 Mar 2024 17:50:00 +0900 Subject: [PATCH 07/15] test: mint coin from long named package --- .../realm-banker-issued-coin-denom.txtar | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar index da0427707bd..1ba540be379 100644 --- a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar +++ b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar @@ -4,38 +4,46 @@ gnoland start ## add realm_banker -gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/test/realm_banker -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx addpkg -pkgdir $WORK/short -pkgpath gno.land/r/test/realm_banker -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 + +## add realm_banker with long package_name +gnokey maketx addpkg -pkgdir $WORK/long -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check initial balance gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"9999998000000ugnot"' +stdout '"9999996000000ugnot"' ## mint coin from banker gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check balance after minting, without patching banker will return '31337ugnot' gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"31337/gno.land/r/test/realm_banker:ugnot,9999996000000ugnot"' +stdout '"31337/gno.land/r/test/realm_banker:ugnot,9999994000000ugnot"' ## burn coin gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check balance after burning gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"31330/gno.land/r/test/realm_banker:ugnot,9999994000000ugnot"' +stdout '"31330/gno.land/r/test/realm_banker:ugnot,9999992000000ugnot"' ## transfer coin gnokey maketx send -send "1330/gno.land/r/test/realm_banker:ugnot" -to g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 ## check sender balance gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"30000/gno.land/r/test/realm_banker:ugnot,9999993000000ugnot"' +stdout '"30000/gno.land/r/test/realm_banker:ugnot,9999991000000ugnot"' ## check receiver balance gnokey query bank/balances/g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 stdout '"1330/gno.land/r/test/realm_banker:ugnot"' --- realm_banker.gno -- +## mint coin from long named package with banker +gnokey maketx call -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -func Mint -args "g1cq2ecdq3eyn5qa0fzznpurg87zq3k77g63q6u7" -args "ugnot" -args "100" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey query bank/balances/g1cq2ecdq3eyn5qa0fzznpurg87zq3k77g63q6u7 +stdout '"100/gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890:ugnot"' + +-- short/realm_banker.gno -- package realm_banker import ( @@ -47,6 +55,24 @@ func Mint(addr std.Address, denom string, amount int64) { banker.IssueCoin(addr, denom, amount) } +func Burn(addr std.Address, denom string, amount int64) { + banker := std.GetBanker(std.BankerTypeRealmIssue) + banker.RemoveCoin(addr, denom, amount) +} + +-- long/realm_banker.gno -- +// package name is 130 characters long +package package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 + +import ( + "std" +) + +func Mint(addr std.Address, denom string, amount int64) { + banker := std.GetBanker(std.BankerTypeRealmIssue) + banker.IssueCoin(addr, denom, amount) +} + func Burn(addr std.Address, denom string, amount int64) { banker := std.GetBanker(std.BankerTypeRealmIssue) banker.RemoveCoin(addr, denom, amount) From eea0c8ce08e9d806065abcdd1d9e720e68cfe83b Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 29 Mar 2024 17:52:26 +0900 Subject: [PATCH 08/15] feat: check origin denom first, then make a new one with pkg_path --- gnovm/stdlibs/std/banker.go | 18 ++++++++++++++++-- tm2/pkg/std/coin.go | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/gnovm/stdlibs/std/banker.go b/gnovm/stdlibs/std/banker.go index 8503ce27e33..6995bf0bdeb 100644 --- a/gnovm/stdlibs/std/banker.go +++ b/gnovm/stdlibs/std/banker.go @@ -2,6 +2,7 @@ package std import ( "fmt" + "regexp" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/tm2/pkg/crypto" @@ -89,9 +90,16 @@ func X_bankerTotalCoin(m *gno.Machine, bt uint8, denom string) int64 { func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amount int64) { // gno checks for bt == RealmIssue + // check origin denom format + matched, err := regexp.MatchString("[a-z][a-z0-9]{2,15}", denom) + if err != nil || !matched { + m.Panic(typedString("invalid denom format, must be [a-z][a-z0-9]{2,15}")) + return + } + // Similar to ibc spec // ibc_denom := 'ibc/' + hash('path' + 'base_denom') - // gno_realm_denom := 'realm/' + 'pkg_path' + '/' + 'base_denom' + // gno_realm_denom := '/' + 'pkg_path' + ':' + 'base_denom' newDenom := "/" + m.Realm.Path + ":" + denom m.Context.(ExecContext).Banker.IssueCoin(crypto.Bech32Address(addr), newDenom, amount) } @@ -99,6 +107,12 @@ func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amou func X_bankerRemoveCoin(m *gno.Machine, bt uint8, addr string, denom string, amount int64) { // gno checks for bt == RealmIssue - newDenom := "realm/" + m.Realm.Path + "/" + denom + matched, err := regexp.MatchString("[a-z][a-z0-9]{2,15}", denom) + if err != nil || !matched { + m.Panic(typedString("invalid denom format, must be [a-z][a-z0-9]{2,15}")) + return + } + + newDenom := "/" + m.Realm.Path + ":" + denom m.Context.(ExecContext).Banker.RemoveCoin(crypto.Bech32Address(addr), newDenom, amount) } diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index b98c0fa289d..38aba491224 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -616,7 +616,7 @@ func (coins Coins) Sort() Coins { // Parsing var ( - reDnmString = `[a-z][a-z0-9/:._-]{2,127}` // cosmos regex(https://github.com/cosmos/cosmos-sdk/blob/b2c26cdc4c3d2f5b7cb28e5153e2b458c61ee8fa/types/coin.go#L852) without capital letters + reDnmString = `.*` // TODO: limit pkg_path length // REF: https://github.com/gnolang/gno/pull/875#discussion_r1543384275 reAmt = `[[:digit:]]+` reSpc = `[[:space:]]*` reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString)) From e001ac78b4a8bc898f339fde191f56a0bbc7e158 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 29 Mar 2024 20:56:28 +0900 Subject: [PATCH 09/15] feat: new regex to check denom --- tm2/pkg/std/coin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index 38aba491224..445ea3650d4 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -616,7 +616,7 @@ func (coins Coins) Sort() Coins { // Parsing var ( - reDnmString = `.*` // TODO: limit pkg_path length // REF: https://github.com/gnolang/gno/pull/875#discussion_r1543384275 + reDnmString = `[a-z\/][a-z0-9_.:\/]{2,}` // TODO: limit pkg_path length // REF: https://github.com/gnolang/gno/pull/875#discussion_r1543384275 reAmt = `[[:digit:]]+` reSpc = `[[:space:]]*` reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString)) From 98978144c84d218e6a6327c79e94fb528e7c9548 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 29 Mar 2024 21:33:41 +0900 Subject: [PATCH 10/15] fix: test failing --- tm2/pkg/std/coin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/std/coin_test.go b/tm2/pkg/std/coin_test.go index 5c84a65161f..33ee7425a0c 100644 --- a/tm2/pkg/std/coin_test.go +++ b/tm2/pkg/std/coin_test.go @@ -429,7 +429,7 @@ func TestParse(t *testing.T) { {"98 bar , 1 foo ", true, Coins{{"bar", int64(98)}, {"foo", one}}}, {" 55\t \t bling\n", true, Coins{{"bling", int64(55)}}}, {"2foo, 97 bar", true, Coins{{"bar", int64(97)}, {"foo", int64(2)}}}, - {"5foo-bar", true, Coins{{"foo-bar", int64(5)}}}, + {"5foo-bar", false, nil}, {"5 mycoin,", false, nil}, // no empty coins in a list {"2 3foo, 97 bar", false, nil}, // 3foo is invalid coin name {"11me coin, 12you coin", false, nil}, // no spaces in coin names From f9b87b677028012aaf7365264a5bde532b8f1b4c Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 7 May 2024 12:40:08 +0900 Subject: [PATCH 11/15] chore: clarify why such a balance is derived --- .../realm-banker-issued-coin-denom.txtar | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar index 1ba540be379..67942c4f5f0 100644 --- a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar +++ b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar @@ -4,42 +4,42 @@ gnoland start ## add realm_banker -gnokey maketx addpkg -pkgdir $WORK/short -pkgpath gno.land/r/test/realm_banker -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx addpkg -pkgdir $WORK/short -pkgpath gno.land/r/test/realm_banker -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 ## add realm_banker with long package_name -gnokey maketx addpkg -pkgdir $WORK/long -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx addpkg -pkgdir $WORK/long -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 ## check initial balance gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"9999996000000ugnot"' +stdout '"9999998000000ugnot"' ## mint coin from banker -gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 -## check balance after minting, without patching banker will return '31337ugnot' +## check balance after minting, without patching banker will return '9999997031337ugnot' // initial balance - gas cost + 31337 gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"31337/gno.land/r/test/realm_banker:ugnot,9999994000000ugnot"' +stdout '"31337/gno.land/r/test/realm_banker:ugnot,9999997000000ugnot"' ## burn coin -gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 ## check balance after burning gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"31330/gno.land/r/test/realm_banker:ugnot,9999992000000ugnot"' +stdout '"31330/gno.land/r/test/realm_banker:ugnot,9999996000000ugnot"' ## transfer coin -gnokey maketx send -send "1330/gno.land/r/test/realm_banker:ugnot" -to g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx send -send "1330/gno.land/r/test/realm_banker:ugnot" -to g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 ## check sender balance gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"30000/gno.land/r/test/realm_banker:ugnot,9999991000000ugnot"' +stdout '"30000/gno.land/r/test/realm_banker:ugnot,9999995000000ugnot"' ## check receiver balance gnokey query bank/balances/g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 stdout '"1330/gno.land/r/test/realm_banker:ugnot"' ## mint coin from long named package with banker -gnokey maketx call -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -func Mint -args "g1cq2ecdq3eyn5qa0fzznpurg87zq3k77g63q6u7" -args "ugnot" -args "100" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -func Mint -args "g1cq2ecdq3eyn5qa0fzznpurg87zq3k77g63q6u7" -args "ugnot" -args "100" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 gnokey query bank/balances/g1cq2ecdq3eyn5qa0fzznpurg87zq3k77g63q6u7 stdout '"100/gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890:ugnot"' From 3838b51d0e8930a0a45fe663472866bd4a1446c5 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 9 May 2024 21:05:10 +0900 Subject: [PATCH 12/15] chore: constant regex --- gnovm/stdlibs/std/banker.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gnovm/stdlibs/std/banker.go b/gnovm/stdlibs/std/banker.go index 6995bf0bdeb..f7975e59a2d 100644 --- a/gnovm/stdlibs/std/banker.go +++ b/gnovm/stdlibs/std/banker.go @@ -31,6 +31,9 @@ const ( btRealmSend // Can issue and remove realm coins. btRealmIssue + + // regexp for denom format + denomRegex = "[a-z][a-z0-9]{2,15}" ) func X_bankerGetCoins(m *gno.Machine, bt uint8, addr string) (denoms []string, amounts []int64) { @@ -91,9 +94,9 @@ func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amou // gno checks for bt == RealmIssue // check origin denom format - matched, err := regexp.MatchString("[a-z][a-z0-9]{2,15}", denom) + matched, err := regexp.MatchString(denomRegex, denom) if err != nil || !matched { - m.Panic(typedString("invalid denom format, must be [a-z][a-z0-9]{2,15}")) + m.Panic(typedString("invalid denom format to issue coin, must be " + denomRegex)) return } @@ -107,9 +110,9 @@ func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amou func X_bankerRemoveCoin(m *gno.Machine, bt uint8, addr string, denom string, amount int64) { // gno checks for bt == RealmIssue - matched, err := regexp.MatchString("[a-z][a-z0-9]{2,15}", denom) + matched, err := regexp.MatchString(denomRegex, denom) if err != nil || !matched { - m.Panic(typedString("invalid denom format, must be [a-z][a-z0-9]{2,15}")) + m.Panic(typedString("invalid denom format to remove coin, must be " + denomRegex)) return } From 362cd1cc6a33dc63412c30149422d5e03c212f14 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 9 May 2024 21:07:27 +0900 Subject: [PATCH 13/15] chore --- tm2/pkg/std/coin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index 445ea3650d4..4f36757efc0 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -616,7 +616,7 @@ func (coins Coins) Sort() Coins { // Parsing var ( - reDnmString = `[a-z\/][a-z0-9_.:\/]{2,}` // TODO: limit pkg_path length // REF: https://github.com/gnolang/gno/pull/875#discussion_r1543384275 + reDnmString = `[a-z\/][a-z0-9_.:\/]{2,}` reAmt = `[[:digit:]]+` reSpc = `[[:space:]]*` reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString)) From 0d39abdc2475d9e630961a5fd92b5dba0f0fb606 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 9 May 2024 21:21:29 +0900 Subject: [PATCH 14/15] test: spend all ugnot to make it clear --- .../realm-banker-issued-coin-denom.txtar | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar index 67942c4f5f0..abc3615d79c 100644 --- a/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar +++ b/gno.land/cmd/gnoland/testdata/realm-banker-issued-coin-denom.txtar @@ -1,5 +1,8 @@ # test for https://github.com/gnolang/gno/pull/875 +## another test user, test2 +adduser test2 + ## start a new node gnoland start @@ -9,30 +12,36 @@ gnokey maketx addpkg -pkgdir $WORK/short -pkgpath gno.land/r/test/realm_banker - ## add realm_banker with long package_name gnokey maketx addpkg -pkgdir $WORK/long -pkgpath gno.land/r/test/package89_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_1234567890 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 -## check initial balance -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"9999998000000ugnot"' +## test2 spend all balance +gnokey maketx send -send "9999999ugnot" -to g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -gas-fee 1ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test2 + +## check test2 balance +gnokey query bank/balances/${USER_ADDR_test2} +stdout '' ## mint coin from banker -gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Mint -args ${USER_ADDR_test2} -args "ugnot" -args "31337" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 -## check balance after minting, without patching banker will return '9999997031337ugnot' // initial balance - gas cost + 31337 -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"31337/gno.land/r/test/realm_banker:ugnot,9999997000000ugnot"' +## check balance after minting, without patching banker will return '31337ugnot' +gnokey query bank/balances/${USER_ADDR_test2} +stdout '"31337/gno.land/r/test/realm_banker:ugnot"' ## burn coin -gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx call -pkgpath gno.land/r/test/realm_banker -func Burn -args ${USER_ADDR_test2} -args "ugnot" -args "7" -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 ## check balance after burning -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"31330/gno.land/r/test/realm_banker:ugnot,9999996000000ugnot"' +gnokey query bank/balances/${USER_ADDR_test2} +stdout '"31330/gno.land/r/test/realm_banker:ugnot"' + +## transfer 1ugnot to test2 for gas-fee of below tx +gnokey maketx send -send "1ugnot" -to ${USER_ADDR_test2} -gas-fee 1ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 ## transfer coin -gnokey maketx send -send "1330/gno.land/r/test/realm_banker:ugnot" -to g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1 +gnokey maketx send -send "1330/gno.land/r/test/realm_banker:ugnot" -to g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 -gas-fee 1ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test2 ## check sender balance -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout '"30000/gno.land/r/test/realm_banker:ugnot,9999995000000ugnot"' +gnokey query bank/balances/${USER_ADDR_test2} +stdout '"30000/gno.land/r/test/realm_banker:ugnot"' ## check receiver balance gnokey query bank/balances/g1yr0dpfgthph7y6mepdx8afuec4q3ga2lg8tjt0 From 5311666f3bd5b0b951e805a9390773b4719b28e8 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 10 May 2024 12:21:37 +0900 Subject: [PATCH 15/15] refactor: better regex --- gnovm/stdlibs/std/banker.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/gnovm/stdlibs/std/banker.go b/gnovm/stdlibs/std/banker.go index f7975e59a2d..ef66e1ae7fb 100644 --- a/gnovm/stdlibs/std/banker.go +++ b/gnovm/stdlibs/std/banker.go @@ -31,11 +31,13 @@ const ( btRealmSend // Can issue and remove realm coins. btRealmIssue - - // regexp for denom format - denomRegex = "[a-z][a-z0-9]{2,15}" ) +// regexp for denom format +const denomRegex = "[a-z][a-z0-9]{2,15}" + +var reg = regexp.MustCompile(denomRegex) + func X_bankerGetCoins(m *gno.Machine, bt uint8, addr string) (denoms []string, amounts []int64) { coins := m.Context.(ExecContext).Banker.GetCoins(crypto.Bech32Address(addr)) return ExpandCoins(coins) @@ -94,8 +96,8 @@ func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amou // gno checks for bt == RealmIssue // check origin denom format - matched, err := regexp.MatchString(denomRegex, denom) - if err != nil || !matched { + matched := reg.MatchString(denom) + if !matched { m.Panic(typedString("invalid denom format to issue coin, must be " + denomRegex)) return } @@ -110,8 +112,8 @@ func X_bankerIssueCoin(m *gno.Machine, bt uint8, addr string, denom string, amou func X_bankerRemoveCoin(m *gno.Machine, bt uint8, addr string, denom string, amount int64) { // gno checks for bt == RealmIssue - matched, err := regexp.MatchString(denomRegex, denom) - if err != nil || !matched { + matched := reg.MatchString(denom) + if !matched { m.Panic(typedString("invalid denom format to remove coin, must be " + denomRegex)) return }