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: avoid Instantiation of banker on pure packages #2248

Merged
merged 13 commits into from
Oct 23, 2024
Merged
12 changes: 12 additions & 0 deletions gnovm/stdlibs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gnovm/stdlibs/std/banker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (b BankerType) String() string {
// GetBanker returns a new Banker, with its capabilities matching the given
// [BankerType].
func GetBanker(bt BankerType) Banker {
assertCallerIsRealm()
if bt >= maxBanker {
panic("invalid banker type")
}
Expand Down
1 change: 1 addition & 0 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ func getRealm(height int) (address string, pkgPath string)
func derivePkgAddr(pkgPath string) string
func encodeBech32(prefix string, bz [20]byte) string
func decodeBech32(addr string) (prefix string, bz [20]byte, ok bool)
func assertCallerIsRealm()
8 changes: 8 additions & 0 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package std

import (
"github.com/gnolang/gno/gnovm/pkg/gnolang"

Check failure on line 4 in gnovm/stdlibs/std/native.go

View workflow job for this annotation

GitHub Actions / Run Main / Go Linter / lint

ST1019: package "github.com/gnolang/gno/gnovm/pkg/gnolang" is being imported more than once (stylecheck)
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"

Check failure on line 5 in gnovm/stdlibs/std/native.go

View workflow job for this annotation

GitHub Actions / Run Main / Go Linter / lint

ST1019(related information): other import of "github.com/gnolang/gno/gnovm/pkg/gnolang" (stylecheck)
"github.com/gnolang/gno/tm2/pkg/bech32"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
Expand Down Expand Up @@ -158,6 +159,13 @@
return prefix, [20]byte(bz), true
}

func X_assertCallerIsRealm(m *gno.Machine) {
frame := m.Frames[m.NumFrames()-2]

Check warning on line 163 in gnovm/stdlibs/std/native.go

View check run for this annotation

Codecov / codecov/patch

gnovm/stdlibs/std/native.go#L162-L163

Added lines #L162 - L163 were not covered by tests
if path := frame.LastPackage.PkgPath; !gnolang.IsRealmPath(path) {
m.Panic(typedString("caller is not a realm"))
}

Check warning on line 166 in gnovm/stdlibs/std/native.go

View check run for this annotation

Codecov / codecov/patch

gnovm/stdlibs/std/native.go#L165-L166

Added lines #L165 - L166 were not covered by tests
}

func typedString(s string) gno.TypedValue {
tv := gno.TypedValue{T: gno.StringType}
tv.SetString(gno.StringValue(s))
Expand Down
13 changes: 13 additions & 0 deletions gnovm/tests/files/stdbanker_stdlibs.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "std"

func main() {
defer func() {
println(recover())
}()
std.TestSetRealm(std.NewCodeRealm("gno.land/p/demo/users"))
}

// Output:
// should only be called for Realms
3 changes: 3 additions & 0 deletions gnovm/tests/stdlibs/std/frame_testing.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ func NewUserRealm(user Address) Realm {
// NewCodeRealm creates a new realm for a published code realm with the given
// pkgPath.
func NewCodeRealm(pkgPath string) Realm {
if !isRealm(pkgPath){
thehowl marked this conversation as resolved.
Show resolved Hide resolved
panic("should only be called for Realms")
}
return Realm{pkgPath: pkgPath, addr: DerivePkgAddr(pkgPath)}
}
1 change: 1 addition & 0 deletions gnovm/tests/stdlibs/std/std.gno
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ func testSetOrigSend(
spentDenom []string, spentAmt []int64)
func testIssueCoins(addr string, denom []string, amt []int64)
func getRealm(height int) (address string, pkgPath string)
func isRealm(pkgPath string) bool
4 changes: 4 additions & 0 deletions gnovm/tests/stdlibs/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func X_getRealm(m *gno.Machine, height int) (address string, pkgPath string) {
return string(ctx.OrigCaller), ""
}

func X_isRealm(m *gno.Machine, pkgPath string) bool {
return gno.IsRealmPath(pkgPath)
}

func X_testSetOrigSend(m *gno.Machine,
sentDenom []string, sentAmt []int64,
spentDenom []string, spentAmt []int64,
Expand Down
Loading