Skip to content

Commit

Permalink
Revert "feat(stdlibs): add math/rand (gnolang#2455)"
Browse files Browse the repository at this point in the history
This reverts commit f547d7d.
  • Loading branch information
moul committed Jul 2, 2024
1 parent 3b42391 commit ddb4d10
Show file tree
Hide file tree
Showing 39 changed files with 372 additions and 764 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.22'
go-version: '1.21'

- name: Install dependencies
run: go mod download
Expand All @@ -28,4 +28,4 @@ jobs:
run: make -C docs/ build

- name: Run linter
run: make -C docs/ lint
run: make -C docs/ lint
5 changes: 4 additions & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
fail-fast: false
matrix:
goversion:
- "1.21.x"
- "1.22.x"
runs-on: ubuntu-latest
timeout-minutes: 30
Expand All @@ -35,6 +36,7 @@ jobs:
fail-fast: false
matrix:
goversion:
- "1.21.x"
- "1.22.x"
# unittests: TODO: matrix with contracts
runs-on: ubuntu-latest
Expand All @@ -58,6 +60,7 @@ jobs:
fail-fast: false
matrix:
goversion:
- "1.21.x"
- "1.22.x"
# unittests: TODO: matrix with contracts
runs-on: ubuntu-latest
Expand Down Expand Up @@ -105,4 +108,4 @@ jobs:
# Find all directories containing gno.mod file
find ./examples -name "gno.mod" -execdir go run "$GNO_CMD" mod tidy \;
# Check if there are changes after running gno mod tidy
git diff --exit-code || (echo "Some gno.mod files are not tidy, please run 'make tidy'." && exit 1)
git diff --exit-code || (echo "Some gno.mod files are not tidy, please run 'make tidy'." && exit 1)
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The gno repository is primarily based on Go (Golang) and Gno.

The primary tech stack for working on the repository:

- Go (version 1.22+)
- Go (version 1.21+)
- make (for using Makefile configurations)

It is recommended to work on a Unix environment, as most of the tooling is built around ready-made tools in Unix (WSL2
Expand Down
2 changes: 1 addition & 1 deletion contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/gno/contribs/gnodev

go 1.22
go 1.21

toolchain go1.22.4

Expand Down
2 changes: 1 addition & 1 deletion contribs/gnofaucet/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/gno/contribs/gnofaucet

go 1.22
go 1.21

toolchain go1.22.4

Expand Down
2 changes: 1 addition & 1 deletion contribs/gnokeykc/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/gno/contribs/gnokeykc

go 1.22
go 1.21

toolchain go1.22.4

Expand Down
2 changes: 1 addition & 1 deletion contribs/gnomd/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/gno/contribs/gnomd

go 1.22
go 1.21

toolchain go1.22.4

Expand Down
16 changes: 8 additions & 8 deletions docs/getting-started/local-setup/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ id: installation
# Installation

## Overview
In this tutorial, you will learn how to set up the Gno development environment
locally, so you can get up and running writing Gno code. You will download and
In this tutorial, you will learn how to set up the Gno development environment
locally, so you can get up and running writing Gno code. You will download and
install all the necessary tooling, and validate that it is correctly configured
to run on your machine.

## Prerequisites
- **Git**
- **`make` (for running Makefiles)**
- **Go 1.22+**
- **Go 1.21+**
- **Go Environment Setup**:
- Make sure `$GOPATH` is well-defined, and `$GOPATH/bin` is added to your `$PATH` variable.
- To do this, you can add the following line to your `.bashrc`, `.zshrc` or other config file:
Expand All @@ -30,7 +30,7 @@ GitHub repository somewhere on disk:
git clone https://github.com/gnolang/gno.git
```

## 2. Installing the required tools
## 2. Installing the required tools

There are three tools that should be used for getting started with Gno development:
- `gno` - the GnoVM binary
Expand All @@ -42,7 +42,7 @@ To install all three tools, simply run the following in the root of the repo:
make install
```

## 3. Verifying installation
## 3. Verifying installation

### `gno`
`gno` provides ample functionality to the user, among which is running,
Expand All @@ -59,7 +59,7 @@ You should get the help output from the command:

![gno help](../../assets/getting-started/local-setup/local-setup/gno-help.gif)

Alternatively, if you don't want to have the binary callable system-wide, you
Alternatively, if you don't want to have the binary callable system-wide, you
can run the binary directly:

```bash
Expand All @@ -68,8 +68,8 @@ go run ./cmd/gno --help
```

### `gnodev`
`gnodev` is the go-to Gno development helper tool - it comes with a built in
Gno.land node, a `gnoweb` server to display the state of your smart contracts
`gnodev` is the go-to Gno development helper tool - it comes with a built in
Gno.land node, a `gnoweb` server to display the state of your smart contracts
(realms), and a watcher system to actively track changes in your code. Read more
about `gnodev` [here](../../gno-tooling/cli/gnodev.md).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In this tutorial, you will learn how to start a local Gno node and connect to an

- **Git**
- **`make` (for running Makefiles)**
- **Go 1.22+**
- **Go 1.21+**
- **Go Environment Setup**: Ensure you have Go set up as outlined in
the [Go official installation documentation](https://go.dev/doc/install) for your environment

Expand Down Expand Up @@ -107,4 +107,4 @@ gnoland start \

That's it! 🎉

Your new Gno node should be up and running, and syncing block data from the remote chain.
Your new Gno node should be up and running, and syncing block data from the remote chain.
4 changes: 2 additions & 2 deletions docs/gno-infrastructure/validators/setting-up-a-new-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Additionally, you will see the different options you can use to make your Gno in

- **Git**
- **`make` (for running Makefiles)**
- **Go 1.22+**
- **Go 1.21+**
- **Go Environment Setup**: Ensure you have Go set up as outlined in
the [Go official installation documentation](https://go.dev/doc/install) for your environment

Expand Down Expand Up @@ -451,4 +451,4 @@ Genesis block generation happens only once during the lifetime of a Gno chain.
This means that if you specify a balances file using `gnoland start`, and the chain has already started (advanced from
block 0), the specified balance sheet will not be applied.

:::
:::
5 changes: 2 additions & 3 deletions docs/reference/go-gno-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ id: go-gno-compatibility

Generics are currently not implemented.

Note that Gno does not support shadowing of built-in types.
Note that Gno does not support shadowing of built-in types.
While the following built-in typecasting assignment would work in Go, this is not supported in Gno.

```go
Expand Down Expand Up @@ -205,7 +205,7 @@ Legend:
| math/big | `tbd` |
| math/bits | `full` |
| math/cmplx | `tbd` |
| math/rand | `full`[^9] |
| math/rand | `todo` |
| mime | `tbd` |
| mime/multipart | `tbd` |
| mime/quotedprintable | `tbd` |
Expand Down Expand Up @@ -291,7 +291,6 @@ Legend:
determinism. Concurrent functionality (such as `time.Ticker`) is not implemented.
[^8]: `crypto/ed25519` is currently only implemented for `Verify`, which should
still cover a majority of use cases. A full implementation is welcome.
[^9]: `math/rand` in Gno ports over Go's `math/rand/v2`.

## Tooling (`gno` binary)

Expand Down
3 changes: 3 additions & 0 deletions examples/gno.land/p/demo/rand/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Draft

module gno.land/p/demo/rand
139 changes: 139 additions & 0 deletions examples/gno.land/p/demo/rand/rand.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package rand

// Disclaimer: this package is unsafe and won't prevent others to
// guess values in advance.
//
// the goal of this package is to implement a random library that
// is fully deterministic for validators while being hard to guess.
//
// We use the Bernstein's hash djb2 to be CPU-cycle efficient.

import (
"math/rand"
"std"
"time"
)

type Instance struct {
seed int64
}

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

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

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

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.seed = (i.seed << 5) + i.seed + 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) Float32() float32 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Float32()
}

func (i *Instance) Float64() float64 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Float64()
}

func (i *Instance) Int() int {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Int()
}

func (i *Instance) Intn(n int) int {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Intn(n)
}

func (i *Instance) Int63() int64 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Int63()
}

func (i *Instance) Int63n(n int64) int64 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Int63n(n)
}

func (i *Instance) Int31() int32 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Int31()
}

func (i *Instance) Int31n(n int32) int32 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Int31n(n)
}

func (i *Instance) Uint32() uint32 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Uint32()
}

func (i *Instance) Uint64() uint64 {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Uint64()
}

func (i *Instance) Read(p []byte) (n int, err error) {
i.addEntropy()
return rand.New(rand.NewSource(i.seed)).Read(p)
}

func (i *Instance) Shuffle(n int, swap func(i, j int)) {
i.addEntropy()
rand.New(rand.NewSource(i.seed)).Shuffle(n, swap)
}
Loading

0 comments on commit ddb4d10

Please sign in to comment.