Skip to content

Commit

Permalink
apply PrevRealm()
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Jun 13, 2023
1 parent 771d4c2 commit edd5d2d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 60 deletions.
18 changes: 18 additions & 0 deletions gnovm/stdlibs/internal/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 == ""
}
14 changes: 9 additions & 5 deletions gnovm/stdlibs/std/banker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ func (ba bankAdapter) TotalCoin(denom string) int64 {
}

func (ba bankAdapter) IssueCoin(addr Address, denom string, amount int64) {
// XXX apply PrevRealm.PkgPath, https://github.com/gnolang/gno/pull/667

// Similar to ibc ==> "ibc/" + sha256('path' + 'base_denom')
// "realm/" + sha256('pkg_path' + 'base_denom')
// "realm/" + sha256('pkg_path' + '/' + 'base_denom')

prev := istd.PrevRealm()

// from := std.PrevRealm().PkgPath
from := istd.GetOrigCaller().String()
var from string
if prev.IsUser() {
from = string(prev.Addr())
} else {
from = prev.PkgPath()
}
to_hash := from + "/" + denom

hash := sha256.Sum256([]byte(to_hash))[:]
Expand Down
47 changes: 0 additions & 47 deletions gnovm/stdlibs/std/banker_test.gno

This file was deleted.

51 changes: 43 additions & 8 deletions gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,61 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
pn.DefineGoNativeValue("IntSize", strconv.IntSize)
pn.DefineGoNativeValue("AppendUint", strconv.AppendUint)
case "internal/std":
pn.DefineNative("GetOrigCaller",
pn.DefineNative("PrevRealm",
gno.Flds( // params
),
gno.Flds( // results
"", "Address",
"", "Realm",
),
func(m *gno.Machine) {
ctx := m.Context.(ExecContext)
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(ctx.OrigCaller),
reflect.ValueOf(Realm{
addr: lastCaller,
pkgPath: lastPkgPath,
}),
)
addrT := store.GetType(gno.DeclaredTypeID("std", "Address"))
res0.T = addrT

realmT := store.GetType(gno.DeclaredTypeID("std", "Realm"))
res0.T = realmT
m.PushValue(res0)
},
)

// XXX PrevRealm() https://github.com/gnolang/gno/pull/667
case "std":
// NOTE: some of these are overridden in tests/imports.go
// Also see stdlibs/InjectPackage.
Expand Down

0 comments on commit edd5d2d

Please sign in to comment.