Skip to content

Commit

Permalink
chore(examples): In r/demo/boards public API, change AssertOriginCall…
Browse files Browse the repository at this point in the history
…() to PrevRealm().IsUser() (gnolang#2358)

In r/demo/boards, `std.AssertOriginCall()` blocks calling the API from
another realm. But it also prevents using MsgRun for testing as we do in
PR gnolang#1583. Therefore, we change to check `std.PrevRealm().IsUser()`. This
still blocks another realm from calling the code, but allows being
[called by
MsgRun](https://github.com/gnolang/gno/blob/d7f12167eff72cd4a12e9e8b8aaa30dc241bfb6c/misc/stress-test/stress-test-many-posts/main.go#L148-L156)
where a user keypair sends the transaction.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
</details>

---------

Signed-off-by: Jeff Thompson <[email protected]>
Co-authored-by: Leon Hudak <[email protected]>
  • Loading branch information
2 people authored and gfanton committed Jul 23, 2024
1 parent 0cc57eb commit ff6cc54
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 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,9 @@ func GetBoardIDFromName(name string) (BoardID, bool) {
}

func CreateBoard(name string) BoardID {
std.AssertOriginCall()
if !(std.IsOriginCall() || std.PrevRealm().IsUser()) {
panic("invalid non-user call")
}
bid := incGetBoardID()
caller := std.GetOrigCaller()
if usernameOf(caller) == "" {
Expand All @@ -41,7 +43,9 @@ func checkAnonFee() bool {
}

func CreateThread(bid BoardID, title string, body string) PostID {
std.AssertOriginCall()
if !(std.IsOriginCall() || std.PrevRealm().IsUser()) {
panic("invalid non-user call")
}
caller := std.GetOrigCaller()
if usernameOf(caller) == "" {
if !checkAnonFee() {
Expand All @@ -57,7 +61,9 @@ func CreateThread(bid BoardID, title string, body string) PostID {
}

func CreateReply(bid BoardID, threadid, postid PostID, body string) PostID {
std.AssertOriginCall()
if !(std.IsOriginCall() || std.PrevRealm().IsUser()) {
panic("invalid non-user call")
}
caller := std.GetOrigCaller()
if usernameOf(caller) == "" {
if !checkAnonFee() {
Expand Down Expand Up @@ -85,7 +91,9 @@ 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 {
std.AssertOriginCall()
if !(std.IsOriginCall() || std.PrevRealm().IsUser()) {
panic("invalid non-user call")
}
caller := std.GetOrigCaller()
if usernameOf(caller) == "" {
// TODO: allow with gDefaultAnonFee payment.
Expand Down Expand Up @@ -113,7 +121,9 @@ func CreateRepost(bid BoardID, postid PostID, title string, body string, dstBoar
}

func DeletePost(bid BoardID, threadid, postid PostID, reason string) {
std.AssertOriginCall()
if !(std.IsOriginCall() || std.PrevRealm().IsUser()) {
panic("invalid non-user call")
}
caller := std.GetOrigCaller()
board := getBoard(bid)
if board == nil {
Expand Down Expand Up @@ -143,7 +153,9 @@ func DeletePost(bid BoardID, threadid, postid PostID, reason string) {
}

func EditPost(bid BoardID, threadid, postid PostID, title, body string) {
std.AssertOriginCall()
if !(std.IsOriginCall() || std.PrevRealm().IsUser()) {
panic("invalid non-user call")
}
caller := std.GetOrigCaller()
board := getBoard(bid)
if board == nil {
Expand Down

0 comments on commit ff6cc54

Please sign in to comment.