Skip to content

Commit

Permalink
feat: add std.PrevRealm (gnolang#667)
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio Navarro Perez <[email protected]>
Co-authored-by: Thomas Bruyelle <[email protected]>
Co-authored-by: Manfred Touron <[email protected]>
  • Loading branch information
4 people authored and Doozers committed Aug 31, 2023
1 parent 3813cbf commit 10cb29f
Show file tree
Hide file tree
Showing 14 changed files with 529 additions and 15 deletions.
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()
}

func GetPrevRealm() std.Realm {
return std.PrevRealm()
}

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 GetPrevRealm() std.Realm {
return std.PrevRealm()
}

func GetPSubtestsPrevRealm() std.Realm {
return psubtests.GetPrevRealm()
}

func GetRTestsGetPrevRealm() std.Realm {
return rtests.GetPrevRealm()
}

// 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 GetPrevRealm() std.Realm {
return std.PrevRealm()
}

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 GetPrevRealm() std.Realm {
return std.PrevRealm()
}

func GetPSubtestsPrevRealm() std.Realm {
return subtests.GetPrevRealm()
}

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
20 changes: 20 additions & 0 deletions gnovm/stdlibs/frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package stdlibs

import "github.com/gnolang/gno/tm2/pkg/crypto"

type Realm struct {
addr crypto.Bech32Address
pkgPath string
}

func (r Realm) Addr() crypto.Bech32Address {
return r.addr
}

func (r Realm) PkgPath() string {
return r.pkgPath
}

func (r Realm) IsUser() bool {
return r.pkgPath == ""
}
18 changes: 18 additions & 0 deletions gnovm/stdlibs/std/frame.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package std

type Realm struct {
addr Address
pkgPath string
}

func (r Realm) Addr() Address {
return r.addr
}

func (r Realm) PkgPath() string {
return r.pkgPath
}

func (r Realm) IsUser() bool {
return r.pkgPath == ""
}
56 changes: 56 additions & 0 deletions gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func InjectNativeMappings(store gno.Store) {
store.AddGo2GnoMapping(reflect.TypeOf(crypto.Bech32Address("")), "std", "Address")
store.AddGo2GnoMapping(reflect.TypeOf(std.Coins{}), "std", "Coins")
store.AddGo2GnoMapping(reflect.TypeOf(std.Coin{}), "std", "Coin")
store.AddGo2GnoMapping(reflect.TypeOf(Realm{}), "std", "Realm")
}

func InjectPackage(store gno.Store, pn *gno.PackageNode) {
Expand Down Expand Up @@ -262,6 +263,61 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
m.PushValue(res0)
},
)
pn.DefineNative("PrevRealm",
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() {
// Ignore non-realm frame
continue
}
pkgPath := fr.LastPackage.PkgPath
// The first realm we encounter will be the one calling
// this function; to get the calling realm determine the first frame
// where fr.LastPackage changes.
if lastPkgPath == "" {
lastPkgPath = pkgPath
} else if lastPkgPath == pkgPath {
continue
} else {
lastCaller = fr.LastPackage.GetPkgAddr().Bech32()
lastPkgPath = pkgPath
break
}
}

// Empty the pkgPath if we return a user
if ctx.OrigCaller == lastCaller {
lastPkgPath = ""
}

// 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("GetOrigPkgAddr",
gno.Flds( // params
),
Expand Down
18 changes: 18 additions & 0 deletions gnovm/stdlibs/stdshim/frame.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package std

type Realm struct {
addr Address
pkgPath string
}

func (r Realm) Addr() Address {
return r.addr
}

func (r Realm) PkgPath() string {
return r.pkgPath
}

func (r Realm) IsUser() bool {
return r.pkgPath == ""
}
8 changes: 8 additions & 0 deletions gnovm/stdlibs/stdshim/stdshim.gno
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func GetOrigSend() Coins {
return Coins{}
}

func PrevRealm() Realm {
panic(shimWarn)
return Realm{
addr: Address(""),
pkgPath: "",
}
}

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
Loading

0 comments on commit 10cb29f

Please sign in to comment.