Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add r/demo/users.ClaimRandomName #2486

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 42 additions & 41 deletions docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
// Bid Function Test - Send Coin
func TestBidCoins(t *testing.T) {
// Sending two types of coins
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)

// Sending lower amount than the current highest bid
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)

// Sending more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
}

// Bid Function Test - Bid by two or more people
func TestBidCoins(t *testing.T) {
// bidder01 bidding with 1 coin
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 1)
shouldEqual(t, highestBidder, bidder01)
shouldEqual(t, pendingReturns.Size(), 0)

// bidder02 bidding with 1 coin
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// bidder02 bidding with 2 coins
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 2)
shouldEqual(t, highestBidder, bidder02)
shouldEqual(t, pendingReturns.Size(), 1)
}
docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
docs/how-to-guides/porting-solidity-to-gno.md: std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
docs/reference/stdlibs/std/testing.md:std.TestSetOrigSend(sent, spent Coins)
examples/gno.land/r/demo/banktest/z_0_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 100_000_000}}, nil)
examples/gno.land/r/demo/banktest/z_1_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 100000000}}, nil)
examples/gno.land/r/demo/banktest/z_2_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 100000000}}, nil)
examples/gno.land/r/demo/boards/z_12_a_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 9000000}}, nil)
examples/gno.land/r/demo/boards/z_5_b_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 9000000}}, nil)
examples/gno.land/r/demo/boards/z_5_c_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 101000000}}, nil)
examples/gno.land/r/demo/boards/z_5_d_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 9000000}}, nil)
examples/gno.land/r/demo/groups/z_1_c_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 9000000}}, nil)
examples/gno.land/r/demo/groups/z_2_d_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 9000000}}, nil)
examples/gno.land/r/demo/groups/z_2_g_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 9000000}}, nil)
examples/gno.land/r/demo/users/users_test.gno: std.TestSetOrigSend("199000000ugnot")
examples/gno.land/r/demo/users/z_0_filetest.gno: std.TestSetOrigSend(std.Coins{std.NewCoin("dontcare", 1)}, nil)
examples/gno.land/r/demo/users/z_3_filetest.gno: std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
examples/gno.land/r/demo/users/z_4_filetest.gno: std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
examples/gno.land/r/demo/users/z_5_filetest.gno: std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
examples/gno.land/r/demo/users/z_7_filetest.gno: std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
examples/gno.land/r/demo/users/z_7b_filetest.gno: std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
examples/gno.land/r/demo/users/z_8_filetest.gno: std.TestSetOrigSend(std.Coins{{"dontcare", 1}}, nil)
examples/gno.land/r/demo/wugnot/z0_filetest.gno: std.TestSetOrigSend(std.Coins{{"ugnot", 123_400}}, nil)
85 changes: 85 additions & 0 deletions examples/gno.land/p/demo/entropy/entropy.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Entropy generates fully deterministic, cost-effective, and hard to guess
// numbers.
//
// It is designed both for for single-usage, like seeding math/rand or for being
// reused which increases the entropy and its cost effectiveness.
//
// Disclaimer: this package is unsafe and won't prevent others to guess values
// in advance.
//
// It uses the Bernstein's hash djb2 to be CPU-cycle efficient.
package entropy

import (
"std"
"time"
)

type Instance struct {
value int64
}

func New() *Instance {
r := Instance{value: 5381}
r.addEntropy()
return &r
}

func FromSeed(seed int64) *Instance {
r := Instance{value: seed}
r.addEntropy()
return &r
}

func (i *Instance) Seed() int64 {
return i.value
}

func (i *Instance) djb2String(input string) {
for _, c := range input {
i.djb2Int64(int64(c))
}
}

// super fast random algorithm.
// http://www.cse.yorku.ca/~oz/hash.html
func (i *Instance) djb2Int64(input int64) {
i.value = (i.value << 5) + i.value + input
}

// AddEntropy uses various runtime variables to add entropy to the existing seed.
func (i *Instance) addEntropy() {
// FIXME: reapply the 5381 initial value?

// inherit previous entropy
// nothing to do

// handle callers
{
caller1 := std.GetCallerAt(1).String()
i.djb2String(caller1)
caller2 := std.GetCallerAt(2).String()
i.djb2String(caller2)
}

// height
{
height := std.GetHeight()
i.djb2Int64(height)
}

// time
{
secs := time.Now().Second()
i.djb2Int64(int64(secs))
nsecs := time.Now().Nanosecond()
i.djb2Int64(int64(nsecs))
}

// FIXME: compute other hard-to-guess but deterministic variables, like real gas?
}

func (i *Instance) Value() int64 {
i.addEntropy()
return i.value
}
48 changes: 48 additions & 0 deletions examples/gno.land/p/demo/entropy/entropy_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package entropy

import (
"fmt"
"std"
"strconv"
"strings"
"testing"
)

func TestInstance(t *testing.T) {
instance := New()
if instance == nil {
t.Errorf("instance should not be nil")
}
}

func TestInstanceValue(t *testing.T) {
baseEntropy := New()
baseResult := computeValue(t, baseEntropy)

sameHeightEntropy := New()
sameHeightResult := computeValue(t, sameHeightEntropy)

if baseResult != sameHeightResult {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}

std.TestSkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue(t, differentHeightEntropy)

if baseResult == differentHeightResult {
t.Errorf("should have different result: new=%s, base=%s", differentHeightResult, baseResult)
}
}

func computeValue(t *testing.T, r *Instance) string {
t.Helper()

out := ""
for i := 0; i < 10; i++ {
val := int(r.Value())
out += strconv.Itoa(val) + " "
}

return out
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/entropy/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/entropy
56 changes: 56 additions & 0 deletions examples/gno.land/p/demo/entropy/z_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"std"

"gno.land/p/demo/entropy"
)

func main() {
// initial
println("---")
r := entropy.New()
println(r.Value())
println(r.Value())
println(r.Value())
println(r.Value())
println(r.Value())

// should be the same
println("---")
r = entropy.New()
println(r.Value())
println(r.Value())
println(r.Value())
println(r.Value())
println(r.Value())

std.TestSkipHeights(1)
println("---")
r = entropy.New()
println(r.Value())
println(r.Value())
println(r.Value())
println(r.Value())
println(r.Value())
}

// Output:
// ---
// 2206404796634629535
// 6369783909128776508
// -273967328977352263
// 3685013872379606294
// -321443877504368301
// ---
// 2206404796634629535
// 6369783909128776508
// -273967328977352263
// 3685013872379606294
// -321443877504368301
// ---
// 2389594832880495019
// 2483785090669486254
// 902493904282098385
// 5349860799650907156
// 3037689170578704503
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/uassert/uassert.gno
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Equal(t TestingT, expected, actual interface{}, msgs ...string) bool {
if av, ok := actual.(string); ok {
equal = ev == av
ok_ = true
es, as = ev, as
es, as = ev, av
}
case std.Address:
if av, ok := actual.(std.Address); ok {
Expand Down
23 changes: 23 additions & 0 deletions examples/gno.land/p/demo/wordlist/adjective.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package wordlist

import "math/rand"

var (
adjectives = []string{
"angry",
"brave",
"cocky",
"goofy",
"happy",
"jolly",
"lucid",
"nifty",
"sharp",
"stoic",
}
adjectivesL = len(adjectives)
)

func RandomAdjective(r *rand.Rand) string {
return adjectives[r.IntN(adjectivesL)]
}
28 changes: 28 additions & 0 deletions examples/gno.land/p/demo/wordlist/color.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package wordlist

import "math/rand"

var (
colors = []string{
"aqua",
"black",
"blue",
"fuchsia",
"gray",
"green",
"lime",
"maroon",
"navy",
"olive",
"purple",
"silver",
"teal",
"white",
"yellow",
}
colorsL = len(colors)
)

func RandomColor(r *rand.Rand) string {
return colors[r.IntN(colorsL)]
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/wordlist/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/wordlist
24 changes: 24 additions & 0 deletions examples/gno.land/p/demo/wordlist/job.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wordlist

import "math/rand"

var (
jobs = []string{
"architect",
"baker",
"cleaner",
"cooker",
"doctor",
"designer",
"developer",
"engineer",
"officer",
"priest",
"teacher",
}
jobsL = len(jobs)
)

func RandomJob(r *rand.Rand) string {
return jobs[r.IntN(jobsL)]
}
5 changes: 4 additions & 1 deletion examples/gno.land/r/demo/art/gnoface/gno.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module gno.land/r/demo/art/gnoface

require gno.land/p/demo/ufmt v0.0.0-latest
require (
gno.land/p/demo/entropy v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
3 changes: 2 additions & 1 deletion examples/gno.land/r/demo/art/gnoface/gnoface.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strconv"
"strings"

"gno.land/p/demo/entropy"
"gno.land/p/demo/ufmt"
)

func Render(path string) string {
seed := uint64(std.GetHeight())
seed := uint64(entropy.New().Value())

path = strings.TrimSpace(path)
if path != "" {
Expand Down
4 changes: 4 additions & 0 deletions examples/gno.land/r/demo/users/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ module gno.land/r/demo/users

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/entropy v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/p/demo/wordlist v0.0.0-latest
)
Loading
Loading