From f19683475d12e760ab3681c52ec9bb6f7d91d75f Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Mon, 30 Jan 2023 18:00:27 +0100 Subject: [PATCH] chore: attempt to improve OriginCall funcs `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 #481 --- stdlibs/stdlibs.go | 8 +++++--- tests/file.go | 3 ++- tests/imports.go | 30 ------------------------------ 3 files changed, 7 insertions(+), 34 deletions(-) diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index b365e43a14e..bc8689d3b30 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -114,7 +114,7 @@ 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 @@ -122,7 +122,8 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { 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") } @@ -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) diff --git a/tests/file.go b/tests/file.go index 33afbe38ebb..f7801902c5a 100644 --- a/tests/file.go +++ b/tests/file.go @@ -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" ) @@ -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, diff --git a/tests/imports.go b/tests/imports.go index 305c2244008..236ca939cf9 100644 --- a/tests/imports.go +++ b/tests/imports.go @@ -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