From 11649f61cd5379c076ea8861fd8bdb406295884a Mon Sep 17 00:00:00 2001 From: sunspirit99 <167175638+linhpn99@users.noreply.github.com> Date: Thu, 23 May 2024 17:34:16 +0700 Subject: [PATCH] feat(stdlibs/std)!: remove `std.CurrentRealmPath` (#2087)
Contributors' checklist... - [ ] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
--------- Co-authored-by: Morgan Bazalgette --- docs/concepts/stdlibs/gnopher-hole.md | 2 +- docs/reference/stdlibs/std/chain.md | 12 ------------ docs/reference/stdlibs/std/testing.md | 11 ----------- examples/gno.land/p/demo/tests/tests.gno | 2 +- examples/gno.land/r/demo/tests/tests.gno | 2 +- gnovm/stdlibs/native.go | 19 ------------------- gnovm/stdlibs/std/emit_event.go | 2 +- gnovm/stdlibs/std/emit_event_test.go | 5 ++++- gnovm/stdlibs/std/native.gno | 9 ++++----- gnovm/stdlibs/std/native.go | 15 +++++++-------- gnovm/stdlibs/stdshim/stdshim.gno | 5 ----- gnovm/tests/files/zrealm12.gno | 16 ++++------------ gnovm/tests/files/zrealm_natbind0.gno | 10 +++++----- gnovm/tests/files/zrealm_std3.gno | 4 ++-- gnovm/tests/stdlibs/native.go | 19 ------------------- gnovm/tests/stdlibs/std/std.gno | 2 +- gnovm/tests/stdlibs/std/std.go | 4 ---- 17 files changed, 31 insertions(+), 108 deletions(-) diff --git a/docs/concepts/stdlibs/gnopher-hole.md b/docs/concepts/stdlibs/gnopher-hole.md index b8795cc5af7..b9ce0c700af 100644 --- a/docs/concepts/stdlibs/gnopher-hole.md +++ b/docs/concepts/stdlibs/gnopher-hole.md @@ -12,7 +12,7 @@ in Go. There are generally three reasons why a function should be natively defined: 1. It relies on inspecting the Gno Virtual Machine itself, i.e. `std.AssertOriginCall` - or `std.CurrentRealmPath`. + or `std.CurrentRealm`. 2. It relies on `unsafe`, or other features which are not planned to be available in the GnoVM, i.e. `math.Float64frombits`. 3. Its native Go performance significantly outperforms the Gno counterpart by diff --git a/docs/reference/stdlibs/std/chain.md b/docs/reference/stdlibs/std/chain.md index 06c40d63afc..f3dddaba938 100644 --- a/docs/reference/stdlibs/std/chain.md +++ b/docs/reference/stdlibs/std/chain.md @@ -41,18 +41,6 @@ std.Emit("MyEvent", "myKey1", "myValue1", "myKey2", "myValue2") ``` --- -## CurrentRealmPath -```go -func CurrentRealmPath() string -``` -Returns the path of the realm it is called in. - -#### Usage -```go -realmPath := std.CurrentRealmPath() // gno.land/r/demo/users -``` ---- - ## GetChainID ```go func GetChainID() string diff --git a/docs/reference/stdlibs/std/testing.md b/docs/reference/stdlibs/std/testing.md index 8c9146c81a1..102b9ed6d70 100644 --- a/docs/reference/stdlibs/std/testing.md +++ b/docs/reference/stdlibs/std/testing.md @@ -5,7 +5,6 @@ id: testing # Testing ```go -func TestCurrentRealm() string func TestSkipHeights(count int64) func TestSetOrigCaller(addr Address) func TestSetOrigPkgAddr(addr Address) @@ -13,16 +12,6 @@ func TestSetOrigSend(sent, spent Coins) func TestIssueCoins(addr Address, coins Coins) ``` -## TestCurrentRealm -```go -func TestCurrentRealm() string -``` -Returns the current realm path. - -#### Usage -```go -currentRealmPath := std.TestCurrentRealm() -``` --- ## TestSkipHeights diff --git a/examples/gno.land/p/demo/tests/tests.gno b/examples/gno.land/p/demo/tests/tests.gno index 1a2c2526d01..43732d82dac 100644 --- a/examples/gno.land/p/demo/tests/tests.gno +++ b/examples/gno.land/p/demo/tests/tests.gno @@ -18,7 +18,7 @@ func IncCounter() { } func CurrentRealmPath() string { - return std.CurrentRealmPath() + return std.CurrentRealm().PkgPath() } //---------------------------------------- diff --git a/examples/gno.land/r/demo/tests/tests.gno b/examples/gno.land/r/demo/tests/tests.gno index 2062df6903d..14adad7355c 100644 --- a/examples/gno.land/r/demo/tests/tests.gno +++ b/examples/gno.land/r/demo/tests/tests.gno @@ -17,7 +17,7 @@ func Counter() int { } func CurrentRealmPath() string { - return std.CurrentRealmPath() + return std.CurrentRealm().PkgPath() } var initOrigCaller = std.GetOrigCaller() diff --git a/gnovm/stdlibs/native.go b/gnovm/stdlibs/native.go index 3b1ab719e72..7319e393c35 100644 --- a/gnovm/stdlibs/native.go +++ b/gnovm/stdlibs/native.go @@ -425,25 +425,6 @@ var nativeFuncs = [...]nativeFunc{ )) }, }, - { - "std", - "CurrentRealmPath", - []gno.FieldTypeExpr{}, - []gno.FieldTypeExpr{ - {Name: gno.N("r0"), Type: gno.X("string")}, - }, - func(m *gno.Machine) { - r0 := libs_std.CurrentRealmPath( - m, - ) - - m.PushValue(gno.Go2GnoValue( - m.Alloc, - m.Store, - reflect.ValueOf(&r0).Elem(), - )) - }, - }, { "std", "GetChainID", diff --git a/gnovm/stdlibs/std/emit_event.go b/gnovm/stdlibs/std/emit_event.go index 46fea79d43c..8e61f67d58a 100644 --- a/gnovm/stdlibs/std/emit_event.go +++ b/gnovm/stdlibs/std/emit_event.go @@ -17,7 +17,7 @@ func X_emit(m *gno.Machine, typ string, attrs []string) { m.Panic(typedString(err.Error())) } - pkgPath := CurrentRealmPath(m) + _, pkgPath := currentRealm(m) fnIdent := getPrevFunctionNameFromTarget(m, "Emit") evt := gnoEvent{ diff --git a/gnovm/stdlibs/std/emit_event_test.go b/gnovm/stdlibs/std/emit_event_test.go index 10bd8ecacd9..147ad75dbb5 100644 --- a/gnovm/stdlibs/std/emit_event_test.go +++ b/gnovm/stdlibs/std/emit_event_test.go @@ -13,7 +13,10 @@ import ( func TestEmit(t *testing.T) { m := gno.NewMachine("emit", nil) - pkgPath := CurrentRealmPath(m) + + m.Context = ExecContext{} + + _, pkgPath := X_getRealm(m, 0) tests := []struct { name string eventType string diff --git a/gnovm/stdlibs/std/native.gno b/gnovm/stdlibs/std/native.gno index 8043df49882..ef2601eeca3 100644 --- a/gnovm/stdlibs/std/native.gno +++ b/gnovm/stdlibs/std/native.gno @@ -1,10 +1,9 @@ package std -func AssertOriginCall() // injected -func IsOriginCall() bool // injected -func CurrentRealmPath() string // injected -func GetChainID() string // injected -func GetHeight() int64 // injected +func AssertOriginCall() // injected +func IsOriginCall() bool // injected +func GetChainID() string // injected +func GetHeight() int64 // injected func GetOrigSend() Coins { den, amt := origSend() diff --git a/gnovm/stdlibs/std/native.go b/gnovm/stdlibs/std/native.go index deb0f1268d2..7148a4ba4f4 100644 --- a/gnovm/stdlibs/std/native.go +++ b/gnovm/stdlibs/std/native.go @@ -17,13 +17,6 @@ func IsOriginCall(m *gno.Machine) bool { return len(m.Frames) == 2 } -func CurrentRealmPath(m *gno.Machine) string { - if m.Realm != nil { - return m.Realm.Path - } - return "" -} - func GetChainID(m *gno.Machine) string { return m.Context.(ExecContext).ChainID } @@ -98,7 +91,7 @@ func X_callerAt(m *gno.Machine, n int) string { return string(m.MustLastCallFrame(n).LastPackage.GetPkgAddr().Bech32()) } -func X_getRealm(m *gno.Machine, height int) (address string, pkgPath string) { +func X_getRealm(m *gno.Machine, height int) (address, pkgPath string) { var ( ctx = m.Context.(ExecContext) currentCaller crypto.Bech32Address @@ -130,6 +123,12 @@ func X_getRealm(m *gno.Machine, height int) (address string, pkgPath string) { return string(ctx.OrigCaller), "" } +// currentRealm retrieves the current realm's address and pkgPath. +// It's not a native binding; but is used within this package to clarify usage. +func currentRealm(m *gno.Machine) (address, pkgPath string) { + return X_getRealm(m, 0) +} + func X_derivePkgAddr(pkgPath string) string { return string(gno.DerivePkgAddr(pkgPath).Bech32()) } diff --git a/gnovm/stdlibs/stdshim/stdshim.gno b/gnovm/stdlibs/stdshim/stdshim.gno index 62e97088209..efffea59dae 100644 --- a/gnovm/stdlibs/stdshim/stdshim.gno +++ b/gnovm/stdlibs/stdshim/stdshim.gno @@ -16,11 +16,6 @@ func Hash(bz []byte) (hash [20]byte) { return } -func CurrentRealmPath() string { - panic(shimWarn) - return "" -} - func GetChainID() string { panic(shimWarn) return "" diff --git a/gnovm/tests/files/zrealm12.gno b/gnovm/tests/files/zrealm12.gno index 0049cba6073..ee9e85d827b 100644 --- a/gnovm/tests/files/zrealm12.gno +++ b/gnovm/tests/files/zrealm12.gno @@ -3,31 +3,23 @@ package test import ( "std" - - "gno.land/r/demo/tests" ) func main() { - if std.TestCurrentRealm() != "gno.land/r/test" { - panic("should not happen") - } tests.InitTestNodes() - if std.TestCurrentRealm() != "gno.land/r/test" { + if std.CurrentRealm().PkgPath() != "gno.land/r/test" { panic("should not happen") } tests.ModTestNodes() - if std.TestCurrentRealm() != "gno.land/r/test" { + if std.CurrentRealm().PkgPath() != "gno.land/r/test" { panic("should not happen") } std.ClearStoreCache() - if std.TestCurrentRealm() != "gno.land/r/test" { + if std.CurrentRealm().PkgPath() != "gno.land/r/test" { panic("should not happen") } tests.PrintTestNodes() - if std.TestCurrentRealm() != "gno.land/r/test" { + if std.CurrentRealm().PkgPath() != "gno.land/r/test" { panic("should not happen") } } - -// Output: -// second's child diff --git a/gnovm/tests/files/zrealm_natbind0.gno b/gnovm/tests/files/zrealm_natbind0.gno index 084ddd3d18f..9cf0e809ece 100644 --- a/gnovm/tests/files/zrealm_natbind0.gno +++ b/gnovm/tests/files/zrealm_natbind0.gno @@ -12,19 +12,19 @@ func init() { } func main() { - // NOTE: this test uses GetHeight and CurrentRealmPath, which are "pure" + // NOTE: this test uses GetHeight and GetChainID, which are "pure" // natively bound functions (ie. not indirections through a wrapper fn, // to convert the types to builtin go/gno identifiers). f := node.(func() int64) println(f()) - node = std.CurrentRealmPath + node = std.GetChainID g := node.(func() string) println(g()) } // Output: // 123 -// gno.land/r/test +// dev // Realm: // switchrealm["gno.land/r/test"] @@ -145,8 +145,8 @@ func main() { // }, // "FileName": "native.gno", // "IsMethod": false, -// "Name": "CurrentRealmPath", -// "NativeName": "CurrentRealmPath", +// "Name": "GetChainID", +// "NativeName": "GetChainID", // "NativePkg": "std", // "PkgPath": "std", // "Source": { diff --git a/gnovm/tests/files/zrealm_std3.gno b/gnovm/tests/files/zrealm_std3.gno index c13feffa42c..4f1d1bc827a 100644 --- a/gnovm/tests/files/zrealm_std3.gno +++ b/gnovm/tests/files/zrealm_std3.gno @@ -6,11 +6,11 @@ import ( ) func foo() { - println("foo", std.CurrentRealmPath()) + println("foo", std.CurrentRealm().PkgPath()) } func main() { - println("main", std.CurrentRealmPath()) + println("main", std.CurrentRealm().PkgPath()) foo() } diff --git a/gnovm/tests/stdlibs/native.go b/gnovm/tests/stdlibs/native.go index 81100838784..6d0e3caa1f1 100644 --- a/gnovm/tests/stdlibs/native.go +++ b/gnovm/tests/stdlibs/native.go @@ -50,25 +50,6 @@ var nativeFuncs = [...]nativeFunc{ )) }, }, - { - "std", - "TestCurrentRealm", - []gno.FieldTypeExpr{}, - []gno.FieldTypeExpr{ - {Name: gno.N("r0"), Type: gno.X("string")}, - }, - func(m *gno.Machine) { - r0 := testlibs_std.TestCurrentRealm( - m, - ) - - m.PushValue(gno.Go2GnoValue( - m.Alloc, - m.Store, - reflect.ValueOf(&r0).Elem(), - )) - }, - }, { "std", "TestSkipHeights", diff --git a/gnovm/tests/stdlibs/std/std.gno b/gnovm/tests/stdlibs/std/std.gno index 0a4f9cc6eff..91c47fed2fd 100644 --- a/gnovm/tests/stdlibs/std/std.gno +++ b/gnovm/tests/stdlibs/std/std.gno @@ -2,7 +2,6 @@ package std func AssertOriginCall() // injected func IsOriginCall() bool // injected -func TestCurrentRealm() string // injected func TestSkipHeights(count int64) // injected func ClearStoreCache() // injected @@ -15,6 +14,7 @@ func TestSetOrigSend(sent, spent Coins) { spentDenom, spentAmt := spent.expandNative() testSetOrigSend(sentDenom, sentAmt, spentDenom, spentAmt) } + func TestIssueCoins(addr Address, coins Coins) { denom, amt := coins.expandNative() testIssueCoins(string(addr), denom, amt) diff --git a/gnovm/tests/stdlibs/std/std.go b/gnovm/tests/stdlibs/std/std.go index 72a2a7734ed..f5c30a08868 100644 --- a/gnovm/tests/stdlibs/std/std.go +++ b/gnovm/tests/stdlibs/std/std.go @@ -41,10 +41,6 @@ func IsOriginCall(m *gno.Machine) bool { panic("unable to determine if test is a _test or a _filetest") } -func TestCurrentRealm(m *gno.Machine) string { - return m.Realm.Path -} - func TestSkipHeights(m *gno.Machine, count int64) { ctx := m.Context.(std.ExecContext) ctx.Height += count