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: std.TestSetPrevRealm #890

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/inner/inner.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package inner
30 changes: 30 additions & 0 deletions examples/gno.land/r/demo/inner/inner_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package inner

import (
"std"
"testing"

"gno.land/p/demo/testutils"
)

var (
oc std.Address = testutils.TestAddress("origin caller ")
or string = "gno.land/r/demo/outer_realm"
)

func TestOuterCallInner(t *testing.T) {
std.TestSetOrigCaller(oc)
shouldEQ(t, std.GetOrigCaller(), oc)

std.TestSetPrevRealm(or)
pr := std.PrevRealm()
shouldEQ(t, pr.Addr(), std.DerivePkgAddr(or))
shouldEQ(t, pr.PkgPath(), or)
shouldEQ(t, std.GetOrigCaller(), oc)
}

func shouldEQ(t *testing.T, got, want interface{}) {
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
52 changes: 52 additions & 0 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,58 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) {
m.Context = ctx
},
)
pn.DefineNative("TestSetPrevRealm",
gno.Flds( // params
"", gno.AnyT(),
),
gno.Flds( // results
),
func(m *gno.Machine) {
arg0 := m.LastBlock().GetParams1().TV

switch arg0.T.String() {
case "std.Address":
// Set PrevRealm as an user
addr := arg0.GetString()
ctx := m.Context.(stdlibs.ExecContext)
ctx.OrigCaller = crypto.Bech32Address(addr)
m.Context = ctx

case "string":
// Set PrevRealm as a realm
realm := arg0.GetString()

// overwrite orig caller
ctx := m.Context.(stdlibs.ExecContext)
ctx.OrigCaller = crypto.Bech32Address(realm)

// Set PkgPath
for i := m.NumFrames() - 1; i > 0; i-- {
fr := m.Frames[i]
if fr.LastPackage == nil || !fr.LastPackage.IsRealm() {
if i != 1 {
fr.LastPackage = &gno.PackageValue{
PkgPath: "gno.land/r/dummy",
}

m.Frames[i] = fr
} else {
fr.LastPackage = &gno.PackageValue{
PkgPath: realm,
}

m.Frames[i] = fr
}

}
}
default:
panic(
fmt.Sprintf("TestSetPrevRealm: invlaid type %v", arg0.T),
)
}
},
)
pn.DefineNative("TestSetOrigPkgAddr",
gno.Flds( // params
"", "Address",
Expand Down