diff --git a/docs/reference/stdlibs/std/chain.md b/docs/reference/stdlibs/std/chain.md index 6a1da6483fd..f3b2c6be0b8 100644 --- a/docs/reference/stdlibs/std/chain.md +++ b/docs/reference/stdlibs/std/chain.md @@ -4,18 +4,6 @@ id: chain # Chain-related -## IsOriginCall -```go -func IsOriginCall() bool -``` -Checks if the caller of the function is an EOA. Returns **true** if caller is an EOA, **false** otherwise. - -#### Usage -```go -if !std.IsOriginCall() {...} -``` ---- - ## AssertOriginCall ```go func AssertOriginCall() diff --git a/examples/gno.land/r/demo/boards/public.gno b/examples/gno.land/r/demo/boards/public.gno index 1d26126fcb2..db545446641 100644 --- a/examples/gno.land/r/demo/boards/public.gno +++ b/examples/gno.land/r/demo/boards/public.gno @@ -17,7 +17,7 @@ func GetBoardIDFromName(name string) (BoardID, bool) { } func CreateBoard(name string) BoardID { - if !(std.IsOriginCall() || std.PrevRealm().IsUser()) { + if !std.PrevRealm().IsUser() { panic("invalid non-user call") } bid := incGetBoardID() @@ -43,7 +43,7 @@ func checkAnonFee() bool { } func CreateThread(bid BoardID, title string, body string) PostID { - if !(std.IsOriginCall() || std.PrevRealm().IsUser()) { + if !std.PrevRealm().IsUser() { panic("invalid non-user call") } caller := std.GetOrigCaller() @@ -61,7 +61,7 @@ func CreateThread(bid BoardID, title string, body string) PostID { } func CreateReply(bid BoardID, threadid, postid PostID, body string) PostID { - if !(std.IsOriginCall() || std.PrevRealm().IsUser()) { + if !std.PrevRealm().IsUser() { panic("invalid non-user call") } caller := std.GetOrigCaller() @@ -91,7 +91,7 @@ func CreateReply(bid BoardID, threadid, postid PostID, body string) PostID { // If dstBoard is private, does not ping back. // If board specified by bid is private, panics. func CreateRepost(bid BoardID, postid PostID, title string, body string, dstBoardID BoardID) PostID { - if !(std.IsOriginCall() || std.PrevRealm().IsUser()) { + if !std.PrevRealm().IsUser() { panic("invalid non-user call") } caller := std.GetOrigCaller() @@ -121,7 +121,7 @@ func CreateRepost(bid BoardID, postid PostID, title string, body string, dstBoar } func DeletePost(bid BoardID, threadid, postid PostID, reason string) { - if !(std.IsOriginCall() || std.PrevRealm().IsUser()) { + if !std.PrevRealm().IsUser() { panic("invalid non-user call") } caller := std.GetOrigCaller() @@ -153,7 +153,7 @@ func DeletePost(bid BoardID, threadid, postid PostID, reason string) { } func EditPost(bid BoardID, threadid, postid PostID, title, body string) { - if !(std.IsOriginCall() || std.PrevRealm().IsUser()) { + if !std.PrevRealm().IsUser() { panic("invalid non-user call") } caller := std.GetOrigCaller() diff --git a/examples/gno.land/r/demo/tests/subtests/subtests.gno b/examples/gno.land/r/demo/tests/subtests/subtests.gno index 6bf43cba5eb..5043c704017 100644 --- a/examples/gno.land/r/demo/tests/subtests/subtests.gno +++ b/examples/gno.land/r/demo/tests/subtests/subtests.gno @@ -21,5 +21,5 @@ func CallAssertOriginCall() { } func CallIsOriginCall() bool { - return std.IsOriginCall() + return std.PrevRealm().IsUser() } diff --git a/examples/gno.land/r/demo/tests/tests.gno b/examples/gno.land/r/demo/tests/tests.gno index e7fde94ea08..cdeea62de66 100644 --- a/examples/gno.land/r/demo/tests/tests.gno +++ b/examples/gno.land/r/demo/tests/tests.gno @@ -32,7 +32,7 @@ func CallAssertOriginCall() { } func CallIsOriginCall() bool { - return std.IsOriginCall() + return std.PrevRealm().IsUser() } func CallSubtestsAssertOriginCall() { diff --git a/gnovm/cmd/gno/testdata/transpile/valid_transpile_file.txtar b/gnovm/cmd/gno/testdata/transpile/valid_transpile_file.txtar index 86cc6f12f7a..b79e7f9a0bc 100644 --- a/gnovm/cmd/gno/testdata/transpile/valid_transpile_file.txtar +++ b/gnovm/cmd/gno/testdata/transpile/valid_transpile_file.txtar @@ -36,7 +36,7 @@ package main import "std" func hello() { - std.AssertOriginCall() + std.GetChainID() } -- main.gno.gen.go.golden -- @@ -61,5 +61,5 @@ package main import "github.com/gnolang/gno/gnovm/stdlibs/std" func hello() { - std.AssertOriginCall(nil) + std.GetChainID(nil) } diff --git a/gnovm/pkg/transpiler/transpiler_test.go b/gnovm/pkg/transpiler/transpiler_test.go index 2a0707f7f79..63b77e49446 100644 --- a/gnovm/pkg/transpiler/transpiler_test.go +++ b/gnovm/pkg/transpiler/transpiler_test.go @@ -344,13 +344,13 @@ func Float32bits(i float32) uint32 func testfunc() { println(Float32bits(3.14159)) - std.AssertOriginCall() + std.GetChainID() } func otherFunc() { std := 1 // This is (incorrectly) changed for now. - std.AssertOriginCall() + std.GetChainID() } `, expectedOutput: ` @@ -363,13 +363,13 @@ import "github.com/gnolang/gno/gnovm/stdlibs/std" func testfunc() { println(Float32bits(3.14159)) - std.AssertOriginCall(nil) + std.GetChainID(nil) } func otherFunc() { std := 1 // This is (incorrectly) changed for now. - std.AssertOriginCall(nil) + std.GetChainID(nil) } `, expectedImports: []*ast.ImportSpec{ @@ -388,11 +388,11 @@ func otherFunc() { source: ` package std -func AssertOriginCall() +func GetChainID() func origCaller() string func testfunc() { - AssertOriginCall() + GetChainID() println(origCaller()) } `, @@ -403,7 +403,7 @@ func testfunc() { package std func testfunc() { - AssertOriginCall(nil) + GetChainID(nil) println(X_origCaller(nil)) } `, diff --git a/gnovm/stdlibs/generated.go b/gnovm/stdlibs/generated.go index 67b492a34b2..90ef55a5315 100644 --- a/gnovm/stdlibs/generated.go +++ b/gnovm/stdlibs/generated.go @@ -416,38 +416,6 @@ var nativeFuncs = [...]NativeFunc{ p0, p1) }, }, - { - "std", - "AssertOriginCall", - []gno.FieldTypeExpr{}, - []gno.FieldTypeExpr{}, - true, - func(m *gno.Machine) { - libs_std.AssertOriginCall( - m, - ) - }, - }, - { - "std", - "IsOriginCall", - []gno.FieldTypeExpr{}, - []gno.FieldTypeExpr{ - {Name: gno.N("r0"), Type: gno.X("bool")}, - }, - true, - func(m *gno.Machine) { - r0 := libs_std.IsOriginCall( - m, - ) - - m.PushValue(gno.Go2GnoValue( - m.Alloc, - m.Store, - reflect.ValueOf(&r0).Elem(), - )) - }, - }, { "std", "GetChainID", diff --git a/gnovm/stdlibs/std/native.gno b/gnovm/stdlibs/std/native.gno index 0dcde1148e1..493ade7800e 100644 --- a/gnovm/stdlibs/std/native.gno +++ b/gnovm/stdlibs/std/native.gno @@ -1,14 +1,15 @@ package std -// AssertOriginCall panics if [IsOriginCall] returns false. -func AssertOriginCall() // injected - -// IsOriginCall returns true only if the calling method is invoked via a direct -// MsgCall. It returns false for all other cases, like if the calling method +// AssertOriginCall panics if the calling method is not invoked via a direct +// MsgCall. It doesn't panic for any other cases, like if the calling method // is invoked by another method (even from the same realm or package). -// It also returns false every time when the transaction is broadcasted via +// It also doesn't panic every time when the transaction is broadcasted via // MsgRun. -func IsOriginCall() bool // injected +func AssertOriginCall() { + if !PrevRealm().IsUser() { + panic("invalid non-origin call") + } +} func GetChainID() string // injected func GetChainDomain() string // injected diff --git a/gnovm/stdlibs/std/native.go b/gnovm/stdlibs/std/native.go index fb181d9be31..b32d29c0010 100644 --- a/gnovm/stdlibs/std/native.go +++ b/gnovm/stdlibs/std/native.go @@ -7,22 +7,6 @@ import ( "github.com/gnolang/gno/tm2/pkg/std" ) -func AssertOriginCall(m *gno.Machine) { - if !IsOriginCall(m) { - m.Panic(typedString("invalid non-origin call")) - } -} - -func IsOriginCall(m *gno.Machine) bool { - n := m.NumFrames() - if n == 0 { - return false - } - firstPkg := m.Frames[0].LastPackage - isMsgCall := firstPkg != nil && firstPkg.PkgPath == "main" - return n <= 2 && isMsgCall -} - func GetChainID(m *gno.Machine) string { return GetContext(m).ChainID } diff --git a/gnovm/stdlibs/std/native_test.go b/gnovm/stdlibs/std/native_test.go deleted file mode 100644 index 851785575d7..00000000000 --- a/gnovm/stdlibs/std/native_test.go +++ /dev/null @@ -1,194 +0,0 @@ -package std - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - gno "github.com/gnolang/gno/gnovm/pkg/gnolang" - "github.com/gnolang/gno/tm2/pkg/crypto" -) - -func TestPrevRealmIsOrigin(t *testing.T) { - var ( - user = gno.DerivePkgAddr("user1.gno").Bech32() - ctx = ExecContext{ - OrigCaller: user, - } - msgCallFrame = &gno.Frame{LastPackage: &gno.PackageValue{PkgPath: "main"}} - msgRunFrame = &gno.Frame{LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/g1337/run"}} - ) - tests := []struct { - name string - machine *gno.Machine - expectedAddr crypto.Bech32Address - expectedPkgPath string - expectedIsOriginCall bool - }{ - { - name: "no frames", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{}, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "one frame w/o LastPackage", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - {LastPackage: nil}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "one package frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "one realm frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "one msgCall frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - msgCallFrame, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: true, - }, - { - name: "one msgRun frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - msgRunFrame, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "one package frame and one msgCall frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - msgCallFrame, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: true, - }, - { - name: "one realm frame and one msgCall frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - msgCallFrame, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: true, - }, - { - name: "one package frame and one msgRun frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - msgRunFrame, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "one realm frame and one msgRun frame", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - msgRunFrame, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "multiple frames with one realm", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/xxx"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/xxx"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/xxx"}}, - }, - }, - expectedAddr: user, - expectedPkgPath: "", - expectedIsOriginCall: false, - }, - { - name: "multiple frames with multiple realms", - machine: &gno.Machine{ - Context: ctx, - Frames: []*gno.Frame{ - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/zzz"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/zzz"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/yyy"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/yyy"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/p/xxx"}}, - {LastPackage: &gno.PackageValue{PkgPath: "gno.land/r/xxx"}}, - }, - }, - expectedAddr: gno.DerivePkgAddr("gno.land/r/yyy").Bech32(), - expectedPkgPath: "gno.land/r/yyy", - expectedIsOriginCall: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert := assert.New(t) - - addr, pkgPath := X_getRealm(tt.machine, 1) - isOrigin := IsOriginCall(tt.machine) - - assert.Equal(string(tt.expectedAddr), addr) - assert.Equal(tt.expectedPkgPath, pkgPath) - assert.Equal(tt.expectedIsOriginCall, isOrigin) - }) - } -} diff --git a/gnovm/tests/files/std5.gno b/gnovm/tests/files/std5.gno index e339d7a6364..926fb87f95b 100644 --- a/gnovm/tests/files/std5.gno +++ b/gnovm/tests/files/std5.gno @@ -13,10 +13,10 @@ func main() { // Stacktrace: // panic: frame not found -// callerAt(n) +// callerAt(n) // gonative:std.callerAt // std.GetCallerAt(2) -// std/native.gno:45 +// std/native.gno:46 // main() // main/files/std5.gno:10 diff --git a/gnovm/tests/files/std8.gno b/gnovm/tests/files/std8.gno index ee717bf16be..0bba25a0eb9 100644 --- a/gnovm/tests/files/std8.gno +++ b/gnovm/tests/files/std8.gno @@ -23,10 +23,10 @@ func main() { // Stacktrace: // panic: frame not found -// callerAt(n) +// callerAt(n) // gonative:std.callerAt // std.GetCallerAt(4) -// std/native.gno:45 +// std/native.gno:46 // fn() // main/files/std8.gno:16 // testutils.WrapCall(inner) diff --git a/gnovm/tests/files/std9.gno b/gnovm/tests/files/std9.gno index 95ccfb2c8a4..2f9f848ec5d 100644 --- a/gnovm/tests/files/std9.gno +++ b/gnovm/tests/files/std9.gno @@ -11,4 +11,4 @@ func main() { } // Output: -// invalid non-origin call +// undefined diff --git a/gnovm/tests/files/zrealm_natbind0.gno b/gnovm/tests/files/zrealm_natbind0.gno index 8e5f641e734..3b7e2b5217e 100644 --- a/gnovm/tests/files/zrealm_natbind0.gno +++ b/gnovm/tests/files/zrealm_natbind0.gno @@ -69,7 +69,7 @@ func main() { // "Closure": { // "@type": "/gno.RefValue", // "Escaped": true, -// "ObjectID": "a7f5397443359ea76c50be82c77f1f893a060925:9" +// "ObjectID": "a7f5397443359ea76c50be82c77f1f893a060925:8" // }, // "FileName": "native.gno", // "IsMethod": false, @@ -83,7 +83,7 @@ func main() { // "Location": { // "Column": "1", // "File": "native.gno", -// "Line": "13", +// "Line": "14", // "PkgPath": "std" // } // }, diff --git a/gnovm/tests/stdlibs/generated.go b/gnovm/tests/stdlibs/generated.go index db5ecdec05d..14575fad4ab 100644 --- a/gnovm/tests/stdlibs/generated.go +++ b/gnovm/tests/stdlibs/generated.go @@ -31,38 +31,6 @@ func (n *NativeFunc) HasMachineParam() bool { } var nativeFuncs = [...]NativeFunc{ - { - "std", - "AssertOriginCall", - []gno.FieldTypeExpr{}, - []gno.FieldTypeExpr{}, - true, - func(m *gno.Machine) { - testlibs_std.AssertOriginCall( - m, - ) - }, - }, - { - "std", - "IsOriginCall", - []gno.FieldTypeExpr{}, - []gno.FieldTypeExpr{ - {Name: gno.N("r0"), Type: gno.X("bool")}, - }, - true, - func(m *gno.Machine) { - r0 := testlibs_std.IsOriginCall( - m, - ) - - m.PushValue(gno.Go2GnoValue( - m.Alloc, - m.Store, - reflect.ValueOf(&r0).Elem(), - )) - }, - }, { "std", "TestSkipHeights", diff --git a/gnovm/tests/stdlibs/std/std.gno b/gnovm/tests/stdlibs/std/std.gno index dcb5a64dbb3..a8695e149a6 100644 --- a/gnovm/tests/stdlibs/std/std.gno +++ b/gnovm/tests/stdlibs/std/std.gno @@ -1,7 +1,5 @@ package std -func AssertOriginCall() // injected -func IsOriginCall() bool // injected func TestSkipHeights(count int64) // injected func TestSetOrigCaller(addr Address) { testSetOrigCaller(string(addr)) } diff --git a/gnovm/tests/stdlibs/std/std.go b/gnovm/tests/stdlibs/std/std.go index 675194b252f..f9b4a3bea81 100644 --- a/gnovm/tests/stdlibs/std/std.go +++ b/gnovm/tests/stdlibs/std/std.go @@ -2,7 +2,6 @@ package std import ( "fmt" - "strings" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/stdlibs/std" @@ -25,43 +24,12 @@ type RealmOverride struct { PkgPath string } -func AssertOriginCall(m *gno.Machine) { - if !IsOriginCall(m) { - m.Panic(typedString("invalid non-origin call")) - } -} - func typedString(s gno.StringValue) gno.TypedValue { tv := gno.TypedValue{T: gno.StringType} tv.SetString(s) return tv } -func IsOriginCall(m *gno.Machine) bool { - tname := m.Frames[0].Func.Name - switch tname { - case "main": // test is a _filetest - // 0. main - // 1. $RealmFuncName - // 2. std.IsOriginCall - return len(m.Frames) == 3 - case "RunTest": // test is a _test - // 0. testing.RunTest - // 1. tRunner - // 2. $TestFuncName - // 3. $RealmFuncName - // 4. std.IsOriginCall - return len(m.Frames) == 5 - } - // support init() in _filetest - // XXX do we need to distinguish from 'runtest'/_test? - // XXX pretty hacky even if not. - if strings.HasPrefix(string(tname), "init.") { - return len(m.Frames) == 3 - } - panic("unable to determine if test is a _test or a _filetest") -} - func TestSkipHeights(m *gno.Machine, count int64) { ctx := m.Context.(*TestExecContext) ctx.Height += count