Skip to content

Commit

Permalink
feat: std.TestSetPrevRealm
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Jun 13, 2023
1 parent 408fc68 commit f46505e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
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)
}
}
47 changes: 47 additions & 0 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,53 @@ 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()

// 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/this_is_dummy_pkg_path",
}

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

0 comments on commit f46505e

Please sign in to comment.