Skip to content

Commit

Permalink
refactor(std): replace IsOriginCall with PrevRealm().IsUser() for EOA…
Browse files Browse the repository at this point in the history
… checks
  • Loading branch information
omarsy committed Dec 21, 2024
1 parent 2e332dd commit b4d2e9f
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 351 deletions.
12 changes: 0 additions & 12 deletions docs/reference/stdlibs/std/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions examples/gno.land/r/demo/boards/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/tests/subtests/subtests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func CallAssertOriginCall() {
}

func CallIsOriginCall() bool {
return std.IsOriginCall()
return std.PrevRealm().IsUser()
}
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func CallAssertOriginCall() {
}

func CallIsOriginCall() bool {
return std.IsOriginCall()
return std.PrevRealm().IsUser()
}

func CallSubtestsAssertOriginCall() {
Expand Down
4 changes: 2 additions & 2 deletions gnovm/cmd/gno/testdata/transpile/valid_transpile_file.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ package main
import "std"

func hello() {
std.AssertOriginCall()
std.GetChainID()
}

-- main.gno.gen.go.golden --
Expand All @@ -61,5 +61,5 @@ package main
import "github.com/gnolang/gno/gnovm/stdlibs/std"

func hello() {
std.AssertOriginCall(nil)
std.GetChainID(nil)
}
14 changes: 7 additions & 7 deletions gnovm/pkg/transpiler/transpiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand All @@ -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{
Expand All @@ -388,11 +388,11 @@ func otherFunc() {
source: `
package std
func AssertOriginCall()
func GetChainID()
func origCaller() string
func testfunc() {
AssertOriginCall()
GetChainID()
println(origCaller())
}
`,
Expand All @@ -403,7 +403,7 @@ func testfunc() {
package std
func testfunc() {
AssertOriginCall(nil)
GetChainID(nil)
println(X_origCaller(nil))
}
`,
Expand Down
32 changes: 0 additions & 32 deletions gnovm/stdlibs/generated.go

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

15 changes: 8 additions & 7 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 0 additions & 16 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit b4d2e9f

Please sign in to comment.