Skip to content

Commit

Permalink
Merge branch 'master' into feat/sync-assignstmt-valuedecl-1958
Browse files Browse the repository at this point in the history
  • Loading branch information
hthieu1110 authored Nov 21, 2024
2 parents ed58ab1 + 889082f commit 02d8ebf
Show file tree
Hide file tree
Showing 53 changed files with 819 additions and 84 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/fossa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ name: Dependency License Scanning

on:
workflow_dispatch:
pull_request:
paths:
- ".github/.fossa.yml"
- ".github/workflows/fossa.yml"
schedule:
- cron: '0 0 * * 6' # At 00:00 on saturdays

permissions:
contents: read
Expand Down Expand Up @@ -47,4 +41,3 @@ jobs:
run: fossa test
env:
FOSSA_API_KEY: "${{secrets.FOSSA_API_KEY}}"

4 changes: 2 additions & 2 deletions .github/workflows/test_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ jobs:
go tool covdata textfmt -v 1 $filter -i=$GOCOVERDIR,$TXTARCOVERDIR -o gocoverage.out
- name: Upload go coverage to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
disable_search: true
fail_ci_if_error: true
file: ${{ inputs.modulepath }}/gocoverage.out
files: ${{ inputs.modulepath }}/gocoverage.out
flags: ${{ inputs.modulepath }}
token: ${{ secrets.codecov-token }}
verbose: true # keep this enable as it help debugging when coverage fail randomly on the CI
Expand Down
17 changes: 17 additions & 0 deletions contribs/gnohealth/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
rundep := go run -modfile ../../misc/devdeps/go.mod
golangci_lint := $(rundep) github.com/golangci/golangci-lint/cmd/golangci-lint


.PHONY: install
install:
go install .

.PHONY: build
build:
go build -o build/gnohealth .

lint:
$(golangci_lint) --config ../../.github/golangci.yml run ./...

test:
@echo "XXX: add tests"
46 changes: 46 additions & 0 deletions contribs/gnohealth/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module github.com/gnolang/gno/contribs/gnohealth

go 1.22.4

replace github.com/gnolang/gno => ../..

require github.com/gnolang/gno v0.0.0-00010101000000-000000000000

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/btcutil v1.1.6 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/peterbourgon/ff/v3 v3.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
199 changes: 199 additions & 0 deletions contribs/gnohealth/go.sum

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions contribs/gnohealth/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/gnolang/gno/contribs/gnohealth/internal/timestamp"
"github.com/gnolang/gno/tm2/pkg/commands"
)

func newHealthCmd(io commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
ShortUsage: "<subcommand> [flags] [<arg>...]",
ShortHelp: "gno health check suite",
LongHelp: "Gno health check suite, to verify that different parts of Gno are working correctly",
},
commands.NewEmptyConfig(),
commands.HelpExec,
)

cmd.AddSubCommands(
timestamp.NewTimestampCmd(io),
)

return cmd
}
166 changes: 166 additions & 0 deletions contribs/gnohealth/internal/timestamp/timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package timestamp

import (
"context"
"flag"
"fmt"
"time"

rpcClient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
"github.com/gnolang/gno/tm2/pkg/commands"
)

const (
defaultRemoteAddress = "http://127.0.0.1:26657"
defaultWebSocket = true
defaultCheckDuration = 30 * time.Second
defaultCheckInterval = 50 * time.Millisecond
defaultMaxDelta = 10 * time.Second
defaultVerbose = false
)

type timestampCfg struct {
remoteAddress string
webSocket bool
checkDuration time.Duration
checkInterval time.Duration
maxDelta time.Duration
verbose bool
}

// NewTimestampCmd creates the gnohealth timestamp subcommand
func NewTimestampCmd(io commands.IO) *commands.Command {
cfg := &timestampCfg{}

return commands.NewCommand(
commands.Metadata{
Name: "timestamp",
ShortUsage: "[flags]",
ShortHelp: "check if block timestamps are drifting",
LongHelp: "This command checks if block timestamps are drifting on a blockchain by connecting to a specified node via RPC.",
},
cfg,
func(_ context.Context, _ []string) error {
return execTimestamp(cfg, io)
},
)
}

// RegisterFlags registers command-line flags for the timestamp command
func (c *timestampCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.remoteAddress,
"remote",
defaultRemoteAddress,
"the remote address of the node to connect to via RPC",
)

fs.BoolVar(
&c.webSocket,
"ws",
defaultWebSocket,
"flag indicating whether to use the WebSocket protocol for RPC",
)

fs.DurationVar(
&c.checkDuration,
"duration",
defaultCheckDuration,
"duration for which checks should be performed",
)

fs.DurationVar(
&c.checkInterval,
"interval",
defaultCheckInterval,
"interval between consecutive checks",
)

fs.DurationVar(
&c.maxDelta,
"max-delta",
defaultMaxDelta,
"maximum allowable time difference between the current time and the last block time",
)

fs.BoolVar(
&c.verbose,
"verbose",
defaultVerbose,
"flag indicating whether to enable verbose logging",
)
}

func execTimestamp(cfg *timestampCfg, io commands.IO) error {
var (
client *rpcClient.RPCClient
err error
lastChecked int64
count uint64
totalDelta time.Duration
)

// Init RPC client
if cfg.webSocket {
if client, err = rpcClient.NewWSClient(cfg.remoteAddress); err != nil {
return fmt.Errorf("unable to create WS client: %w", err)
}
} else {
if client, err = rpcClient.NewHTTPClient(cfg.remoteAddress); err != nil {
return fmt.Errorf("unable to create HTTP client: %w", err)
}
}

// Create a ticker for check interval
ticker := time.NewTicker(cfg.checkInterval)
defer ticker.Stop()

// Create a context that will stop this check when specified duration is elapsed
ctx, cancel := context.WithTimeout(context.Background(), cfg.checkDuration)
defer cancel()

for {
select {
case <-ctx.Done():
average := totalDelta / time.Duration(count)
io.Printf("no timestamp drifted beyond the maximum delta (average %s)\n", average)
return nil

case <-ticker.C:
// Fetch the latest block number from the chain
status, err := client.Status()
if err != nil {
return fmt.Errorf("unable to fetch latest block number: %w", err)
}

latest := status.SyncInfo.LatestBlockHeight

// Check if there have been blocks since the last check
if lastChecked == latest {
continue
}

// Fetch the latest block from the chain
lastBlock, err := client.Block(&latest)
if err != nil {
return fmt.Errorf("unable to fetch latest block content: %w", err)
}

// Check if the last block timestamp is not drifting
delta := time.Until(lastBlock.Block.Time).Abs()
if delta > cfg.maxDelta {
return fmt.Errorf("block %d drifted of %s (max %s): KO", latest, delta, cfg.maxDelta)
}

// Increment counters to calculate average on exit
count += 1
totalDelta += delta

// Update the last checked block number
lastChecked = latest
if cfg.verbose {
io.Printf("block %d drifted of %s (max %s): OK\n", latest, delta, cfg.maxDelta)
}
}
}
}
14 changes: 14 additions & 0 deletions contribs/gnohealth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"context"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
)

func main() {
cmd := newHealthCmd(commands.NewDefaultIO())

cmd.Execute(context.Background(), os.Args[1:])
}
10 changes: 10 additions & 0 deletions examples/gno.land/r/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# `r/`

This directory primarily contains realms. It further branches out into namespaces:
- `demo` - realms meant to demonstrate Gno functionality
- `docs` - realms meant to teach about specific packages and concepts
- `gnoland` - official gno.land realms
- `gov` - governance realms
- `sys` - system realms
- `x` - experimental realms
- `*` - can include personal namespaces, such as `manfred`, `leon`, etc.
1 change: 0 additions & 1 deletion examples/gno.land/r/demo/hello_world/gno.mod

This file was deleted.

17 changes: 0 additions & 17 deletions examples/gno.land/r/demo/hello_world/hello.gno

This file was deleted.

3 changes: 3 additions & 0 deletions examples/gno.land/r/demo/mirror/doc.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package mirror demonstrates that users can pass realm functions
// as arguments to other realms.
package mirror
3 changes: 3 additions & 0 deletions examples/gno.land/r/demo/mirror/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module gno.land/r/demo/mirror

require gno.land/p/demo/avl v0.0.0-latest
33 changes: 33 additions & 0 deletions examples/gno.land/r/demo/mirror/mirror.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mirror

import (
"gno.land/p/demo/avl"
)

var store avl.Tree

func Register(pkgpath string, rndr func(string) string) {
if store.Has(pkgpath) {
return
}

if rndr == nil {
return
}

store.Set(pkgpath, rndr)
}

func Render(path string) string {
if raw, ok := store.Get(path); ok {
return raw.(func(string) string)("")
}

if store.Size() == 0 {
return "None are fair."
}

return "Mirror, mirror on the wall, which realm's the fairest of them all?"
}

// Credits to @jeronimoalbi
Loading

0 comments on commit 02d8ebf

Please sign in to comment.