From 2937a4c8bf376ead6ce6fbced773ba010b8f1dae Mon Sep 17 00:00:00 2001 From: Albert Le Batteux Date: Wed, 14 Jun 2023 14:43:38 +0100 Subject: [PATCH] feat: std.CurrentRealm --- .../p/demo/tests/subtests/subtests.gno | 4 +- .../r/demo/tests/subtests/subtests.gno | 4 +- gnovm/stdlibs/stdlibs.go | 38 +++++++++++++++++++ gnovm/stdlibs/stdshim/stdshim.gno | 8 ++++ gnovm/tests/files/zrealm_crossrealm12.gno | 37 ++++++++++++++++++ 5 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 gnovm/tests/files/zrealm_crossrealm12.gno diff --git a/examples/gno.land/p/demo/tests/subtests/subtests.gno b/examples/gno.land/p/demo/tests/subtests/subtests.gno index cdff70380ec..e8796a73081 100644 --- a/examples/gno.land/p/demo/tests/subtests/subtests.gno +++ b/examples/gno.land/p/demo/tests/subtests/subtests.gno @@ -4,8 +4,8 @@ import ( "std" ) -func CurrentRealmPath() string { - return std.CurrentRealmPath() +func GetCurrentRealm() std.Realm { + return std.CurrentRealm() } func GetPrevRealm() std.Realm { diff --git a/examples/gno.land/r/demo/tests/subtests/subtests.gno b/examples/gno.land/r/demo/tests/subtests/subtests.gno index cdff70380ec..e8796a73081 100644 --- a/examples/gno.land/r/demo/tests/subtests/subtests.gno +++ b/examples/gno.land/r/demo/tests/subtests/subtests.gno @@ -4,8 +4,8 @@ import ( "std" ) -func CurrentRealmPath() string { - return std.CurrentRealmPath() +func GetCurrentRealm() std.Realm { + return std.CurrentRealm() } func GetPrevRealm() std.Realm { diff --git a/gnovm/stdlibs/stdlibs.go b/gnovm/stdlibs/stdlibs.go index 9ac790c2b4e..fb230a0cf86 100644 --- a/gnovm/stdlibs/stdlibs.go +++ b/gnovm/stdlibs/stdlibs.go @@ -263,6 +263,44 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { m.PushValue(res0) }, ) + pn.DefineNative("CurrentRealm", + gno.Flds( // params + ), + gno.Flds( // results + "", "Realm", + ), + 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() { + lastCaller = fr.LastPackage.GetPkgAddr().Bech32() + lastPkgPath = fr.LastPackage.PkgPath + break + } + } + + // Return the result + res0 := gno.Go2GnoValue( + m.Alloc, + m.Store, + reflect.ValueOf(Realm{ + addr: lastCaller, + pkgPath: lastPkgPath, + }), + ) + + realmT := store.GetType(gno.DeclaredTypeID("std", "Realm")) + res0.T = realmT + m.PushValue(res0) + }, + ) pn.DefineNative("PrevRealm", gno.Flds( // params ), diff --git a/gnovm/stdlibs/stdshim/stdshim.gno b/gnovm/stdlibs/stdshim/stdshim.gno index 8e1fdc0da75..62e97088209 100644 --- a/gnovm/stdlibs/stdshim/stdshim.gno +++ b/gnovm/stdlibs/stdshim/stdshim.gno @@ -36,6 +36,14 @@ func GetOrigSend() Coins { return Coins{} } +func CurrentRealm() Realm { + panic(shimWarn) + return Realm{ + addr: Address(""), + pkgPath: "", + } +} + func PrevRealm() Realm { panic(shimWarn) return Realm{ diff --git a/gnovm/tests/files/zrealm_crossrealm12.gno b/gnovm/tests/files/zrealm_crossrealm12.gno new file mode 100644 index 00000000000..ef8ea141ac5 --- /dev/null +++ b/gnovm/tests/files/zrealm_crossrealm12.gno @@ -0,0 +1,37 @@ +// PKGPATH: gno.land/r/crossrealm_test +package crossrealm_test + +import ( + "std" + "fmt" + + psubtests "gno.land/p/demo/tests/subtests" + rsubtests "gno.land/r/demo/tests/subtests" +) + +func main() { + tests := []struct{ + fn func() std.Realm + }{ + { std.CurrentRealm }, + { psubtests.GetCurrentRealm }, + { rsubtests.GetCurrentRealm }, + } + + for _, test := range tests { + r := test.fn() + + if std.DerivePkgAddr(r.PkgPath()) != r.Addr() { + panic(fmt.Sprintf("ERROR: expected: %v, got: %v", + std.DerivePkgAddr(r.PkgPath()), r.Addr(), + )) + } + + println(r.PkgPath(), r.Addr()) + } +} + +// Output: +// gno.land/r/crossrealm_test g1vla5mffzum6060t99u4xhm8mnhgxr0sz4k574p +// gno.land/r/crossrealm_test g1vla5mffzum6060t99u4xhm8mnhgxr0sz4k574p +// gno.land/r/demo/tests/subtests g13g48xnr7lzxsrvny0uf6lhx0cfaxy4n0n5geuf