Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add std.PrevRealm #667

Merged
merged 26 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/gno.land/p/demo/tests/subtests/subtests.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package subtests

import (
"std"
)

func CurrentRealmPath() string {
return std.CurrentRealmPath()
moul marked this conversation as resolved.
Show resolved Hide resolved
}

func GetRealmCaller() std.Address {
return std.GetRealmCaller()
}

func Exec(fn func()) {
fn()
}
19 changes: 19 additions & 0 deletions examples/gno.land/p/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package tests
import (
"std"

psubtests "gno.land/p/demo/tests/subtests"
"gno.land/r/demo/tests"
rtests "gno.land/r/demo/tests"
)

// IncCounter demonstrates that it's possible to call a realm function from
Expand Down Expand Up @@ -51,3 +53,20 @@ func ModifyTestRealmObject2b() {
func ModifyTestRealmObject2c() {
SomeValue3.Field = "modified"
}

func GetRealmCaller() std.Address {
return std.GetRealmCaller()
}

func GetPSubtestsCaller() std.Address {
return psubtests.GetRealmCaller()
}

func GetRTestsGetRealmCaller() std.Address {
return rtests.GetRealmCaller()
}

// Warning: unsafe pattern.
func Exec(fn func()) {
fn()
}
17 changes: 17 additions & 0 deletions examples/gno.land/r/demo/tests/subtests/subtests.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package subtests

import (
"std"
)

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

func GetRealmCaller() std.Address {
return std.GetRealmCaller()
}

func Exec(fn func()) {
fn()
}
18 changes: 17 additions & 1 deletion examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package tests

import "std"
import (
"std"

"gno.land/r/demo/tests/subtests"
)

var counter int

Expand Down Expand Up @@ -69,3 +73,15 @@ func ModTestNodes() {
func PrintTestNodes() {
println(gTestNode2.Child.Name)
}

func GetRealmCaller() std.Address {
return std.GetRealmCaller()
}

func GetPSubtestsCaller() std.Address {
return subtests.GetRealmCaller()
}

func Exec(fn func()) {
fn()
}
4 changes: 3 additions & 1 deletion gnovm/pkg/gnolang/frame.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gnolang

import "fmt"
import (
"fmt"
)

//----------------------------------------
// (runtime) Frame
Expand Down
44 changes: 44 additions & 0 deletions gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,50 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
m.PushValue(res0)
},
)
pn.DefineNative("GetRealmCaller",
gno.Flds( // params
),
gno.Flds( // results
"", "Address",
),
func(m *gno.Machine) {
var (
ctx = m.Context.(ExecContext)
// Default lastCaller is OrigCaller, the signer of the tx
lastCaller = ctx.OrigCaller
lastPkgPath = ""
)

for i := m.NumFrames() - 1; i > 0; i-- {
fr := m.Frames[i]
if fr.LastPackage == nil || !fr.LastPackage.IsRealm() {
// Ignore non-realm frame
continue
}
pkgPath := fr.LastPackage.PkgPath
if lastPkgPath == "" || lastPkgPath == pkgPath {
// Ignore first realm met
lastPkgPath = pkgPath
continue
}

// second realm met in the frames, it becomes the lastCaller
lastCaller = fr.LastPackage.GetPkgAddr().Bech32()
// we don't need to iterate further
break
}
albttx marked this conversation as resolved.
Show resolved Hide resolved

// Return the result
res0 := gno.Go2GnoValue(
m.Alloc,
m.Store,
reflect.ValueOf(lastCaller),
)
addrT := store.GetType(gno.DeclaredTypeID("std", "Address"))
res0.T = addrT
m.PushValue(res0)
},
)
pn.DefineNative("GetOrigPkgAddr",
gno.Flds( // params
),
Expand Down
5 changes: 5 additions & 0 deletions gnovm/stdlibs/stdshim/stdshim.gno
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func GetOrigSend() Coins {
return Coins{}
}

func GetRealmCaller() Address {
panic(shimWarn)
return Address("")
}

func GetOrigCaller() Address {
panic(shimWarn)
return Address("")
Expand Down
3 changes: 1 addition & 2 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (
"strconv"
"strings"

"github.com/pmezard/go-difflib/difflib"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs"
"github.com/gnolang/gno/tm2/pkg/crypto"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/pmezard/go-difflib/difflib"
)

type loggerFunc func(args ...interface{})
Expand Down
134 changes: 134 additions & 0 deletions gnovm/tests/files/zrealm_crossrealm11.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// PKGPATH: gno.land/r/crossrealm_test
package crossrealm_test

import (
"std"
"strings"

ptests "gno.land/p/demo/tests"
"gno.land/p/demo/ufmt"
rtests "gno.land/r/demo/tests"
testfoo "gno.land/r/demo/tests_foo"
)

func getRealmCaller() std.Address {
return std.GetRealmCaller()
}

func Exec(fn func()) {
fn()
}

func main() {
// Create a map of the potential callers, this will give more understandable
// output than the bech32 addresses.
callersByAddr := make(map[std.Address]string)
for _, caller := range []string{
"user1.gno", "gno.land/r/crossrealm_test", "gno.land/r/demo/tests",
} {
addr := std.DerivePkgAddr(caller)
callersByAddr[addr] = caller
}

tests := []struct {
callStackAdd string
callerFn func() std.Address
}{
{
callStackAdd: "",
callerFn: std.GetRealmCaller,
},
{
callStackAdd: " -> r/crossrealm_test.getRealmCaller",
callerFn: getRealmCaller,
},
{
callStackAdd: " -> p/demo/tests",
callerFn: ptests.GetRealmCaller,
},
{
callStackAdd: " -> p/demo/tests -> p/demo/tests/subtests",
callerFn: ptests.GetPSubtestsCaller,
},
{
callStackAdd: " -> r/demo/tests",
callerFn: rtests.GetRealmCaller,
},
{
callStackAdd: " -> r/demo/tests -> r/demo/tests/subtests",
callerFn: rtests.GetPSubtestsCaller,
},
{
callStackAdd: " -> p/demo/tests -> r/demo/tests",
callerFn: ptests.GetRTestsGetRealmCaller,
},
}

println("---") // needed to have space prefixes
printColumns("STACK", "std.GetRealmCaller")
printColumns("-----", "------------------")

baseCallStack := "user1.gno -> r/crossrealm_test.main"
for i, tt := range tests {
printColumns(baseCallStack+tt.callStackAdd, callersByAddr[tt.callerFn()])
Exec(func() {
printColumns(baseCallStack+" -> r/crossrealm_test.Exec"+tt.callStackAdd, callersByAddr[tt.callerFn()])
})
rtests.Exec(func() {
printColumns(baseCallStack+" -> r/demo/tests.Exec"+tt.callStackAdd, callersByAddr[tt.callerFn()])
})
ptests.Exec(func() {
printColumns(baseCallStack+" -> p/demo/tests.Exec"+tt.callStackAdd, callersByAddr[tt.callerFn()])
})
}
}

func printColumns(left, right string) {
const w = 105

output := ""
padding := w - len(left)

// strings.Repeat is not always available when using various imports modes.
for i := 0; i < padding; i++ {
output += " "
}

output += left
output += " = "
output += right
println(output)
}

// Output:
moul marked this conversation as resolved.
Show resolved Hide resolved
// ---
// STACK = std.GetRealmCaller
// ----- = ------------------
// user1.gno -> r/crossrealm_test.main = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.getRealmCaller = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/crossrealm_test.getRealmCaller = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/crossrealm_test.getRealmCaller = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/crossrealm_test.getRealmCaller = user1.gno
// user1.gno -> r/crossrealm_test.main -> p/demo/tests = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests = user1.gno
// user1.gno -> r/crossrealm_test.main -> p/demo/tests -> p/demo/tests/subtests = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests -> p/demo/tests/subtests = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests -> p/demo/tests/subtests = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests -> p/demo/tests/subtests = user1.gno
// user1.gno -> r/crossrealm_test.main -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests
// user1.gno -> r/crossrealm_test.main -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test
// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test
Loading