Skip to content

Commit

Permalink
chore: attempt to improve OriginCall funcs
Browse files Browse the repository at this point in the history
`AssertOriginCall` and `IsOriginCall` relies on the number of frames to
determine if the function was called by a gno transaction (definition
taken from `examples/gno.land/r/demo/banktest/README.md`). Since this
number is different in the context of test and filetest, the 2 functions
are overrided during test setup, but the override works only for
filetest files and not for test files.

This change makes `AssertOriginCall` and `IsOriginCall` to rely on
message's signers instead of frames. If the message has at east one
signer, we consider the function was called by a gno tx. As a result,
the functions no longer needs to be overrided in test setup, and they
work both for filetest files and test files.

Fix gnolang#481
  • Loading branch information
tbruyelle committed Mar 5, 2023
1 parent 2afacce commit f196834
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
8 changes: 5 additions & 3 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
pn.DefineGoNativeValue("IntSize", strconv.IntSize)
pn.DefineGoNativeValue("AppendUint", strconv.AppendUint)
case "std":
// NOTE: some of these are overridden in tests/imports_test.go
// NOTE: some of these are overridden in tests/imports.go
// Also see stdlibs/InjectPackage.
pn.DefineNative("AssertOriginCall",
gno.Flds( // params
),
gno.Flds( // results
),
func(m *gno.Machine) {
isOrigin := len(m.Frames) == 2
ctx := m.Context.(ExecContext)
isOrigin := ctx.Msg != nil && len(ctx.Msg.GetSigners()) > 0
if !isOrigin {
panic("invalid non-origin call")
}
Expand All @@ -135,7 +136,8 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
"isOrigin", "bool",
),
func(m *gno.Machine) {
isOrigin := len(m.Frames) == 2
ctx := m.Context.(ExecContext)
isOrigin := ctx.Msg != nil && len(ctx.Msg.GetSigners()) > 0
res0 := gno.TypedValue{T: gno.BoolType}
res0.SetBool(isOrigin)
m.PushValue(res0)
Expand Down
3 changes: 2 additions & 1 deletion tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/gnolang/gno/pkgs/crypto"
gno "github.com/gnolang/gno/pkgs/gnolang"
osm "github.com/gnolang/gno/pkgs/os"
"github.com/gnolang/gno/pkgs/sdk/testutils"
"github.com/gnolang/gno/pkgs/std"
"github.com/gnolang/gno/stdlibs"
)
Expand All @@ -41,7 +42,7 @@ func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAll
ChainID: "dev",
Height: 123,
Timestamp: 1234567890,
Msg: nil,
Msg: testutils.NewTestMsg(caller),
OrigCaller: caller.Bech32(),
OrigPkgAddr: pkgAddr.Bech32(),
OrigSend: send,
Expand Down
30 changes: 0 additions & 30 deletions tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,36 +465,6 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) {
pn.DefineGoNativeValue("ParseInt", strconv.ParseInt)
case "std":
// NOTE: some of these are overrides.
// Also see stdlibs/InjectPackage.
pn.DefineNativeOverride("AssertOriginCall",
/*
gno.Flds( // params
),
gno.Flds( // results
),
*/
func(m *gno.Machine) {
isOrigin := len(m.Frames) == 3
if !isOrigin {
panic("invalid non-origin call")
}
},
)
pn.DefineNativeOverride("IsOriginCall",
/*
gno.Flds( // params
),
gno.Flds( // results
"isOrigin", "bool",
),
*/
func(m *gno.Machine) {
isOrigin := len(m.Frames) == 3
res0 := gno.TypedValue{T: gno.BoolType}
res0.SetBool(isOrigin)
m.PushValue(res0)
},
)
pn.DefineNativeOverride("GetCallerAt",
/*
gno.Flds( // params
Expand Down

0 comments on commit f196834

Please sign in to comment.