Skip to content

Commit

Permalink
feat(stdlibs/std)!: remove std.CurrentRealmPath (#2087)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [ ] 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).
</details>

---------

Co-authored-by: Morgan Bazalgette <[email protected]>
  • Loading branch information
linhpn99 and thehowl authored May 23, 2024
1 parent f165df7 commit 11649f6
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 108 deletions.
2 changes: 1 addition & 1 deletion docs/concepts/stdlibs/gnopher-hole.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions docs/reference/stdlibs/std/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions docs/reference/stdlibs/std/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ id: testing
# Testing

```go
func TestCurrentRealm() string
func TestSkipHeights(count int64)
func TestSetOrigCaller(addr Address)
func TestSetOrigPkgAddr(addr Address)
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
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func IncCounter() {
}

func CurrentRealmPath() string {
return std.CurrentRealmPath()
return std.CurrentRealm().PkgPath()
}

//----------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Counter() int {
}

func CurrentRealmPath() string {
return std.CurrentRealmPath()
return std.CurrentRealm().PkgPath()
}

var initOrigCaller = std.GetOrigCaller()
Expand Down
19 changes: 0 additions & 19 deletions gnovm/stdlibs/native.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gnovm/stdlibs/std/emit_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 4 additions & 1 deletion gnovm/stdlibs/std/emit_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
15 changes: 7 additions & 8 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand Down
5 changes: 0 additions & 5 deletions gnovm/stdlibs/stdshim/stdshim.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ func Hash(bz []byte) (hash [20]byte) {
return
}

func CurrentRealmPath() string {
panic(shimWarn)
return ""
}

func GetChainID() string {
panic(shimWarn)
return ""
Expand Down
16 changes: 4 additions & 12 deletions gnovm/tests/files/zrealm12.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions gnovm/tests/files/zrealm_natbind0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -145,8 +145,8 @@ func main() {
// },
// "FileName": "native.gno",
// "IsMethod": false,
// "Name": "CurrentRealmPath",
// "NativeName": "CurrentRealmPath",
// "Name": "GetChainID",
// "NativeName": "GetChainID",
// "NativePkg": "std",
// "PkgPath": "std",
// "Source": {
Expand Down
4 changes: 2 additions & 2 deletions gnovm/tests/files/zrealm_std3.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
19 changes: 0 additions & 19 deletions gnovm/tests/stdlibs/native.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gnovm/tests/stdlibs/std/std.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions gnovm/tests/stdlibs/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 11649f6

Please sign in to comment.