From 701ac171575aedaf255b71cf8a603eec07f3b845 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 30 Aug 2024 09:48:00 +0200 Subject: [PATCH 1/4] feat(examples): a shifumi (rock, paper, scissors) smart contract (#2629) A very simple game as a smart contract. It's possible to play against yourself, or another opponent. UI could be improved. No provision against cheating, no gain or automatic action. Related to #611. Screenshot 2024-07-24 at 16 24 28
Contributors' checklist... - [ ] Added new tests, or not needed, or not feasible - [*] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [*] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [*] Added references to related issues and PRs - [*] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
--- .../gno.land/r/demo/games/shifumi/gno.mod | 7 + .../gno.land/r/demo/games/shifumi/shifumi.gno | 120 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 examples/gno.land/r/demo/games/shifumi/gno.mod create mode 100644 examples/gno.land/r/demo/games/shifumi/shifumi.gno diff --git a/examples/gno.land/r/demo/games/shifumi/gno.mod b/examples/gno.land/r/demo/games/shifumi/gno.mod new file mode 100644 index 00000000000..7a4fc173d3d --- /dev/null +++ b/examples/gno.land/r/demo/games/shifumi/gno.mod @@ -0,0 +1,7 @@ +module gno.land/r/demo/games/shifumi + +require ( + gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/seqid v0.0.0-latest + gno.land/r/demo/users v0.0.0-latest +) diff --git a/examples/gno.land/r/demo/games/shifumi/shifumi.gno b/examples/gno.land/r/demo/games/shifumi/shifumi.gno new file mode 100644 index 00000000000..9094cb8fd69 --- /dev/null +++ b/examples/gno.land/r/demo/games/shifumi/shifumi.gno @@ -0,0 +1,120 @@ +package shifumi + +import ( + "errors" + "std" + "strconv" + + "gno.land/p/demo/avl" + "gno.land/p/demo/seqid" + + "gno.land/r/demo/users" +) + +const ( + empty = iota + rock + paper + scissors + last +) + +type game struct { + player1, player2 std.Address // shifumi is a 2 players game + move1, move2 int // can be empty, rock, paper, or scissors +} + +var games avl.Tree +var id seqid.ID + +func (g *game) play(player std.Address, move int) error { + if !(move > empty && move < last) { + return errors.New("invalid move") + } + if player != g.player1 && player != g.player2 { + return errors.New("invalid player") + } + if player == g.player1 && g.move1 == empty { + g.move1 = move + return nil + } + if player == g.player2 && g.move2 == empty { + g.move2 = move + return nil + } + return errors.New("already played") +} + +func (g *game) winner() int { + if g.move1 == empty || g.move2 == empty { + return -1 + } + if g.move1 == g.move2 { + return 0 + } + if g.move1 == rock && g.move2 == scissors || + g.move1 == paper && g.move2 == rock || + g.move1 == scissors && g.move2 == paper { + return 1 + } + return 2 +} + +// NewGame creates a new game where player1 is the caller and player2 the argument. +// A new game index is returned. +func NewGame(player std.Address) int { + games.Set(id.Next().String(), &game{player1: std.PrevRealm().Addr(), player2: player}) + return int(id) +} + +// Play executes a move for the game at index idx, where move can be: +// 1 (rock), 2 (paper), 3 (scissors). +func Play(idx, move int) { + v, ok := games.Get(seqid.ID(idx).String()) + if !ok { + panic("game not found") + } + if err := v.(*game).play(std.PrevRealm().Addr(), move); err != nil { + panic(err) + } +} + +func Render(path string) string { + mov1 := []string{"", " 🤜 ", " 🫱 ", " 👉 "} + mov2 := []string{"", " 🤛 ", " 🫲 ", " 👈 "} + win := []string{"pending", "draw", "player1", "player2"} + + output := `# 👊 ✋ ✌️ Shifumi +Actions: +* [NewGame](shifumi?help&__func=NewGame) opponentAddress +* [Play](shifumi?help&__func=Play) gameIndex move (1=rock, 2=paper, 3=scissors) + + game | player1 | | player2 | | win + --- | --- | --- | --- | --- | --- +` + // Output the 100 most recent games. + maxGames := 100 + for n := int(id); n > 0 && int(id)-n < maxGames; n-- { + v, ok := games.Get(seqid.ID(n).String()) + if !ok { + continue + } + g := v.(*game) + output += strconv.Itoa(n) + " | " + + shortName(g.player1) + " | " + mov1[g.move1] + " | " + + shortName(g.player2) + " | " + mov2[g.move2] + " | " + + win[g.winner()+1] + "\n" + } + return output +} + +func shortName(addr std.Address) string { + user := users.GetUserByAddress(addr) + if user != nil { + return user.Name + } + if len(addr) < 10 { + return string(addr) + } + return string(addr)[:10] + "..." +} From 68ef4450d02d7f2e1a19e56bba7d26cf521aff7a Mon Sep 17 00:00:00 2001 From: grepsuzette <350354+grepsuzette@users.noreply.github.com> Date: Mon, 2 Sep 2024 23:29:29 +0800 Subject: [PATCH 2/4] docs(portal-loop): mention packages can be deployed permissionlessly (#2747) Referencing #2744 --------- Co-authored-by: grepsuzette Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com> --- docs/concepts/portal-loop.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/concepts/portal-loop.md b/docs/concepts/portal-loop.md index d96738bdddf..adc341e3ae4 100644 --- a/docs/concepts/portal-loop.md +++ b/docs/concepts/portal-loop.md @@ -67,3 +67,20 @@ has some drawbacks: Gno will fail to be replayed, meaning **data will be lost**. - Since transactions are archived and replayed during genesis, block height & timestamp cannot be relied upon. + +### Deploying to the Portal Loop + +There are two ways to deploy code to the Portal Loop: + +1. *automatic* - all packages in found in the `examples/gno.land/{p,r}/` directory in the [Gno monorepo](https://github.com/gnolang/gno) get added to the + new genesis each cycle, +2. *permissionless* - this includes replayed transactions with `addpkg`, and + new transactions you can issue with `gnokey maketx addpkg`. + +Since the packages in `examples/gno.land/{p,r}` are deployed first, +permissionless deployments get superseded when packages with identical `pkgpath` +get merged into `examples/`. + +The above mechanism is also how the `examples/` on the Portal Loop +get collaboratively iterated upon, which is its main mission. + From c82e64637c4d8a1448500faddaceea672cddecf6 Mon Sep 17 00:00:00 2001 From: Morgan Date: Tue, 3 Sep 2024 12:32:32 +0200 Subject: [PATCH 3/4] chore: remove panic from uverse (#2626) This function in uverse is misleading, because it's never actually called. Panics are converted to a `*PanicStmt`, not as function calls.
Contributors' checklist... - [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 - [x] Provided any useful hints for running manual tests - [x] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
--- gnovm/pkg/gnolang/misc.go | 4 ++++ gnovm/pkg/gnolang/uverse.go | 12 +----------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/gnovm/pkg/gnolang/misc.go b/gnovm/pkg/gnolang/misc.go index a05de8c74aa..7f7ce0b3a87 100644 --- a/gnovm/pkg/gnolang/misc.go +++ b/gnovm/pkg/gnolang/misc.go @@ -150,6 +150,10 @@ func isReservedName(n Name) bool { // scans uverse static node for blocknames. (slow) func isUverseName(n Name) bool { + if n == "panic" { + // panic is not in uverse, as it is parsed as its own statement (PanicStmt) + return true + } uverseNames := UverseNode().GetBlockNames() for _, name := range uverseNames { if name == n { diff --git a/gnovm/pkg/gnolang/uverse.go b/gnovm/pkg/gnolang/uverse.go index 880a75396ca..38ccdddea85 100644 --- a/gnovm/pkg/gnolang/uverse.go +++ b/gnovm/pkg/gnolang/uverse.go @@ -928,17 +928,7 @@ func UverseNode() *PackageNode { return }, ) - defNative("panic", - Flds( // params - "err", AnyT(), // args[0] - ), - nil, // results - func(m *Machine) { - arg0 := m.LastBlock().GetParams1() - xv := arg0.Deref() - panic(xv.Sprint(m)) - }, - ) + // NOTE: panic is its own statement type, and is not defined as a function. defNative("print", Flds( // params "xs", Vrd(AnyT()), // args[0] From 65ee7a5366cfea06deee2172a2abce797104ba98 Mon Sep 17 00:00:00 2001 From: Leon Hudak <33522493+leohhhn@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:17:37 +0200 Subject: [PATCH 4/4] feat(examples): add `r/leon` (#2740) ## Description This PR adds v1 of `r/leon` to the examples folder. It contains `r/leon/home` & `r/leon/config`. For context - this is meant to be a personal realm (or set of realms) that every gno.land user should have at some point. Later, the idea is to have a [special user page](https://github.com/gnolang/gno/issues/2189) which will fetch the `Render()` of `r/username/home`. This will serve like the profile of the user; and the home realm will most likely be upgradeable down the line. The reason I am making this a PR and not simply publishing it to the Portal Loop is that it allows for further code changes down the line. `examples/` get loaded into genesis first, and all other replayed addpkg transactions after that, making it possible to have "upgradeable" realms on Portal Loop.
Contributors' checklist... - [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 - [x] Provided any useful hints for running manual tests - [x] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
--- examples/gno.land/r/leon/config/config.gno | 65 +++++++++++ examples/gno.land/r/leon/config/gno.mod | 1 + examples/gno.land/r/leon/home/gno.mod | 8 ++ examples/gno.land/r/leon/home/home.gno | 121 +++++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 examples/gno.land/r/leon/config/config.gno create mode 100644 examples/gno.land/r/leon/config/gno.mod create mode 100644 examples/gno.land/r/leon/home/gno.mod create mode 100644 examples/gno.land/r/leon/home/home.gno diff --git a/examples/gno.land/r/leon/config/config.gno b/examples/gno.land/r/leon/config/config.gno new file mode 100644 index 00000000000..cbc1e537e3f --- /dev/null +++ b/examples/gno.land/r/leon/config/config.gno @@ -0,0 +1,65 @@ +package config + +import ( + "errors" + "std" +) + +var ( + main std.Address // leon's main address + backup std.Address // backup address +) + +func init() { + main = "g125em6arxsnj49vx35f0n0z34putv5ty3376fg5" +} + +func Address() std.Address { + return main +} + +func Backup() std.Address { + return backup +} + +func SetAddress(a std.Address) error { + if !a.IsValid() { + return errors.New("config: invalid address") + } + + if err := checkAuthorized(); err != nil { + return err + } + + main = a + return nil +} + +func SetBackup(a std.Address) error { + if !a.IsValid() { + return errors.New("config: invalid address") + } + + if err := checkAuthorized(); err != nil { + return err + } + + backup = a + return nil +} + +func checkAuthorized() error { + caller := std.PrevRealm().Addr() + if caller != main || caller != backup { + return errors.New("config: unauthorized") + } + + return nil +} + +func AssertAuthorized() { + caller := std.PrevRealm().Addr() + if caller != main || caller != backup { + panic("config: unauthorized") + } +} diff --git a/examples/gno.land/r/leon/config/gno.mod b/examples/gno.land/r/leon/config/gno.mod new file mode 100644 index 00000000000..e8cd5cd85b7 --- /dev/null +++ b/examples/gno.land/r/leon/config/gno.mod @@ -0,0 +1 @@ +module gno.land/r/leon/config diff --git a/examples/gno.land/r/leon/home/gno.mod b/examples/gno.land/r/leon/home/gno.mod new file mode 100644 index 00000000000..48cf64a9d0a --- /dev/null +++ b/examples/gno.land/r/leon/home/gno.mod @@ -0,0 +1,8 @@ +module gno.land/r/leon/home + +require ( + gno.land/p/demo/ufmt v0.0.0-latest + gno.land/r/demo/art/gnoface v0.0.0-latest + gno.land/r/demo/art/millipede v0.0.0-latest + gno.land/r/leon/config v0.0.0-latest +) diff --git a/examples/gno.land/r/leon/home/home.gno b/examples/gno.land/r/leon/home/home.gno new file mode 100644 index 00000000000..1f6a07e8959 --- /dev/null +++ b/examples/gno.land/r/leon/home/home.gno @@ -0,0 +1,121 @@ +package home + +import ( + "std" + "strconv" + + "gno.land/p/demo/ufmt" + + "gno.land/r/demo/art/gnoface" + "gno.land/r/demo/art/millipede" + "gno.land/r/leon/config" +) + +var ( + pfp string // link to profile picture + pfpCaption string // profile picture caption + abtMe [2]string +) + +func init() { + pfp = "https://i.imgflip.com/91vskx.jpg" + pfpCaption = "[My favourite painting & pfp](https://en.wikipedia.org/wiki/Wanderer_above_the_Sea_of_Fog)" + abtMe = [2]string{ + `### About me +Hi, I'm Leon, a DevRel Engineer at gno.land. I am a tech enthusiast, +life-long learner, and sharer of knowledge.`, + `### Contributions +My contributions to gno.land can mainly be found +[here](https://github.com/gnolang/gno/issues?q=sort:updated-desc+author:leohhhn). + +TODO import r/gh +`, + } +} + +func UpdatePFP(url, caption string) { + config.AssertAuthorized() + pfp = url + pfpCaption = caption +} + +func UpdateAboutMe(col1, col2 string) { + config.AssertAuthorized() + abtMe[0] = col1 + abtMe[1] = col2 +} + +func Render(path string) string { + out := "# Leon's Homepage\n\n" + + out += renderAboutMe() + out += renderBlogPosts() + out += "\n\n" + out += renderArt() + + return out +} + +func renderBlogPosts() string { + out := "" + //out += "## Leon's Blog Posts" + + // todo fetch blog posts authored by @leohhhn + // and render them + return out +} + +func renderAboutMe() string { + out := "
" + + out += "
\n\n" + out += ufmt.Sprintf("![my profile pic](%s)\n\n%s\n\n", pfp, pfpCaption) + out += "
\n\n" + + out += "
\n\n" + out += abtMe[0] + "\n\n" + out += "
\n\n" + + out += "
\n\n" + out += abtMe[1] + "\n\n" + out += "
\n\n" + + out += "
\n\n" + + return out +} + +func renderArt() string { + out := `
` + "\n\n" + out += "# Gno Art\n\n" + + out += "
" + + out += renderGnoFace() + out += renderMillipede() + out += "Empty spot :/" + + out += "
\n\n" + + out += "This art is dynamic; it will change with every new block.\n\n" + out += `
` + "\n" + + return out +} + +func renderGnoFace() string { + out := "
\n\n" + out += gnoface.Render(strconv.Itoa(int(std.GetHeight()))) + out += "
\n\n" + + return out +} + +func renderMillipede() string { + out := "
\n\n" + out += "Millipede\n\n" + out += "```\n" + millipede.Draw(int(std.GetHeight())%10+1) + "```\n" + out += "
\n\n" + + return out +}