Skip to content

Commit

Permalink
fix: lint and remove unused
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Apr 17, 2024
1 parent 446fd2f commit 80e4f99
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 71 deletions.
7 changes: 6 additions & 1 deletion contribs/gnodev/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
GNOROOT_DIR ?= $(abspath $(lastword $(MAKEFILE_LIST))/../../../)
GOBUILD_FLAGS ?= -ldflags "-X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT=$(GNOROOT_DIR)"

GOBUILD_FLAGS := -ldflags "-X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT=$(GNOROOT_DIR)"
rundep := go run -modfile ../../misc/devdeps/go.mod
golangci_lint := $(rundep) github.com/golangci/golangci-lint/cmd/golangci-lint

install:
go install $(GOBUILD_FLAGS) ./cmd/gnodev

build:
go build $(GOBUILD_FLAGS) -o build/gnodev ./cmd/gnodev

lint:
$(golangci_lint) --config ../../.github/golangci.yml run ./...
22 changes: 10 additions & 12 deletions contribs/gnodev/cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/http"
"os"
"path/filepath"
"time"

"github.com/gnolang/gno/contribs/gnodev/pkg/dev"
gnodev "github.com/gnolang/gno/contribs/gnodev/pkg/dev"
"github.com/gnolang/gno/contribs/gnodev/pkg/emitter"
"github.com/gnolang/gno/contribs/gnodev/pkg/rawterm"
Expand Down Expand Up @@ -52,7 +52,6 @@ type devCfg struct {
// Node Configuration
minimal bool
verbose bool
hotreload bool
noWatch bool
noReplay bool
maxGas int64
Expand Down Expand Up @@ -235,8 +234,9 @@ func execDev(cfg *devCfg, args []string, io commands.IO) (err error) {
// Create server
mux := http.NewServeMux()
server := http.Server{
Handler: mux,
Addr: cfg.webListenerAddr,
Handler: mux,
Addr: cfg.webListenerAddr,
ReadHeaderTimeout: time.Minute,
}
defer server.Close()

Expand Down Expand Up @@ -291,10 +291,9 @@ func runEventLoop(
logger *slog.Logger,
kb keys.Keybase,
rt *rawterm.RawTerm,
dnode *dev.Node,
dnode *gnodev.Node,
watch *watcher.PackageWatcher,
) error {

keyPressCh := listenForKeyPress(logger.WithGroup(KeyPressLogName), rt)
for {
var err error
Expand Down Expand Up @@ -337,7 +336,6 @@ func runEventLoop(
if err = dnode.ReloadAll(ctx); err != nil {
logger.WithGroup(NodeLogName).
Error("unable to reload node", "err", err)

}

case rawterm.KeyCtrlR: // Reset
Expand Down Expand Up @@ -374,8 +372,8 @@ func listenForKeyPress(logger *slog.Logger, rt *rawterm.RawTerm) <-chan rawterm.
return cc
}

func resolvePackagesPathFromArgs(cfg *devCfg, kb keys.Keybase, args []string) ([]dev.PackagePath, error) {
paths := make([]dev.PackagePath, len(args))
func resolvePackagesPathFromArgs(cfg *devCfg, kb keys.Keybase, args []string) ([]gnodev.PackagePath, error) {
paths := make([]gnodev.PackagePath, 0, len(args))

if cfg.genesisCreator == "" {
return nil, fmt.Errorf("default genesis creator cannot be empty")
Expand All @@ -386,8 +384,8 @@ func resolvePackagesPathFromArgs(cfg *devCfg, kb keys.Keybase, args []string) ([
return nil, fmt.Errorf("unable to get genesis creator %q: %w", cfg.genesisCreator, err)
}

for i, arg := range args {
path, err := dev.ResolvePackagePathQuery(kb, arg)
for _, arg := range args {
path, err := gnodev.ResolvePackagePathQuery(kb, arg)
if err != nil {
return nil, fmt.Errorf("invalid package path/query %q: %w", arg, err)
}
Expand All @@ -397,7 +395,7 @@ func resolvePackagesPathFromArgs(cfg *devCfg, kb keys.Keybase, args []string) ([
path.Creator = defaultKey.GetAddress()
}

paths[i] = path
paths = append(paths, path)
}

// Add examples folder if minimal is set to false
Expand Down
107 changes: 53 additions & 54 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,39 +112,39 @@ func NewDevNode(ctx context.Context, logger *slog.Logger, emitter emitter.Emitte
return devnode, nil
}

func (d *Node) getLatestBlockNumber() uint64 {
return uint64(d.Node.BlockStore().Height())
func (n *Node) getLatestBlockNumber() uint64 {
return uint64(n.Node.BlockStore().Height())
}

func (d *Node) Close() error {
return d.Node.Stop()
func (n *Node) Close() error {
return n.Node.Stop()
}

func (d *Node) ListPkgs() []gnomod.Pkg {
return d.pkgs.toList()
func (n *Node) ListPkgs() []gnomod.Pkg {
return n.pkgs.toList()
}

func (d *Node) GetNodeReadiness() <-chan struct{} {
return gnoland.GetNodeReadiness(d.Node)
func (n *Node) GetNodeReadiness() <-chan struct{} {
return gnoland.GetNodeReadiness(n.Node)
}

func (d *Node) GetRemoteAddress() string {
return d.Node.Config().RPC.ListenAddress
func (n *Node) GetRemoteAddress() string {
return n.Node.Config().RPC.ListenAddress
}

// UpdatePackages updates the currently known packages. It will be taken into
// consideration in the next reload of the node.
func (d *Node) UpdatePackages(paths ...string) error {
var n int
func (n *Node) UpdatePackages(paths ...string) error {
var i int
for _, path := range paths {
abspath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("unable to resolve abs path of %q: %w", path, err)
}

creator := d.config.DefaultCreator
creator := n.config.DefaultCreator
var deposit std.Coins
for _, ppath := range d.config.PackagesPathList {
for _, ppath := range n.config.PackagesPathList {
if !strings.HasPrefix(abspath, ppath.Path) {
continue
}
Expand All @@ -161,117 +161,117 @@ func (d *Node) UpdatePackages(paths ...string) error {

// Update or add package in the current known list.
for _, pkg := range pkgslist {
d.pkgs[pkg.Dir] = Package{
n.pkgs[pkg.Dir] = Package{
Pkg: pkg,
Creator: creator,
Deposit: deposit,
}

d.logger.Debug("pkgs update", "name", pkg.Name, "path", pkg.Dir)
n.logger.Debug("pkgs update", "name", pkg.Name, "path", pkg.Dir)
}

n += len(pkgslist)
i += len(pkgslist)
}

d.logger.Info(fmt.Sprintf("updated %d pacakges", n))
n.logger.Info(fmt.Sprintf("updated %d pacakges", i))
return nil
}

// Reset stops the node, if running, and reloads it with a new genesis state,
// effectively ignoring the current state.
func (d *Node) Reset(ctx context.Context) error {
func (n *Node) Reset(ctx context.Context) error {
// Stop the node if it's currently running.
if err := d.stopIfRunning(); err != nil {
if err := n.stopIfRunning(); err != nil {
return fmt.Errorf("unable to stop the node: %w", err)
}

// Generate a new genesis state based on the current packages
txs, err := d.pkgs.Load(DefaultFee)
txs, err := n.pkgs.Load(DefaultFee)
if err != nil {
return fmt.Errorf("unable to load pkgs: %w", err)
}

genesis := gnoland.GnoGenesisState{
Balances: d.config.BalancesList,
Balances: n.config.BalancesList,
Txs: txs,
}

// Reset the node with the new genesis state.
err = d.reset(ctx, genesis)
err = n.reset(ctx, genesis)
if err != nil {
return fmt.Errorf("unable to initialize a new node: %w", err)
}

d.emitter.Emit(&events.Reset{})
n.emitter.Emit(&events.Reset{})
return nil
}

// ReloadAll updates all currently known packages and then reloads the node.
func (d *Node) ReloadAll(ctx context.Context) error {
pkgs := d.ListPkgs()
func (n *Node) ReloadAll(ctx context.Context) error {
pkgs := n.ListPkgs()
paths := make([]string, len(pkgs))
for i, pkg := range pkgs {
paths[i] = pkg.Dir
}

if err := d.UpdatePackages(paths...); err != nil {
if err := n.UpdatePackages(paths...); err != nil {
return fmt.Errorf("unable to reload packages: %w", err)
}

return d.Reload(ctx)
return n.Reload(ctx)
}

// Reload saves the current state, stops the node if running, starts a new node,
// and re-apply previously saved state along with packages updated by `UpdatePackages`.
// If any transaction, including 'addpkg', fails, it will be ignored.
// Use 'Reset' to completely reset the node's state in case of persistent errors.
func (d *Node) Reload(ctx context.Context) error {
if d.config.NoReplay {
func (n *Node) Reload(ctx context.Context) error {
if n.config.NoReplay {
// If NoReplay is true, reload as the same effect as reset
d.logger.Warn("replay disable")
return d.Reset(ctx)
n.logger.Warn("replay disable")
return n.Reset(ctx)
}

// Get current blockstore state
state, err := d.getBlockStoreState(ctx)
state, err := n.getBlockStoreState(ctx)
if err != nil {
return fmt.Errorf("unable to save state: %s", err.Error())
}

// Stop the node if it's currently running.
if err := d.stopIfRunning(); err != nil {
if err := n.stopIfRunning(); err != nil {
return fmt.Errorf("unable to stop the node: %w", err)
}

// Load genesis packages
pkgsTxs, err := d.pkgs.Load(DefaultFee)
pkgsTxs, err := n.pkgs.Load(DefaultFee)
if err != nil {
return fmt.Errorf("unable to load pkgs: %w", err)
}

// Create genesis with loaded pkgs + previous state
genesis := gnoland.GnoGenesisState{
Balances: d.config.BalancesList,
Balances: n.config.BalancesList,
Txs: append(pkgsTxs, state...),
}

// Reset the node with the new genesis state.
err = d.reset(ctx, genesis)
d.logger.Info("reload done", "pkgs", len(pkgsTxs), "state applied", len(state))
err = n.reset(ctx, genesis)
n.logger.Info("reload done", "pkgs", len(pkgsTxs), "state applied", len(state))

// Update node infos
d.loadedPackages = len(pkgsTxs)
n.loadedPackages = len(pkgsTxs)

d.emitter.Emit(&events.Reload{})
n.emitter.Emit(&events.Reload{})
return nil
}

func (d *Node) genesisTxHandler(ctx sdk.Context, tx std.Tx, res sdk.Result) {
func (n *Node) genesisTxHandler(ctx sdk.Context, tx std.Tx, res sdk.Result) {
if res.IsErr() {
// XXX: for now, this is only way to catch the error
before, after, found := strings.Cut(res.Log, "\n")
if !found {
d.logger.Error("unable to send tx", "err", res.Error, "log", res.Log)
n.logger.Error("unable to send tx", "err", res.Error, "log", res.Log)
return
}

Expand All @@ -287,20 +287,19 @@ func (d *Node) genesisTxHandler(ctx sdk.Context, tx std.Tx, res sdk.Result) {
attrs = append(attrs, slog.String("err", msg))

// If debug is enable, also append stack
if d.logger.Enabled(context.Background(), slog.LevelDebug) {
if n.logger.Enabled(context.Background(), slog.LevelDebug) {
attrs = append(attrs, slog.String("stack", after))

}

d.logger.LogAttrs(context.Background(), slog.LevelError, "unable to deliver tx", attrs...)
n.logger.LogAttrs(context.Background(), slog.LevelError, "unable to deliver tx", attrs...)
}
}

// GetBlockTransactions returns the transactions contained
// within the specified block, if any
func (d *Node) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) {
func (n *Node) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) {
int64BlockNum := int64(blockNum)
b, err := d.client.Block(&int64BlockNum)
b, err := n.client.Block(&int64BlockNum)
if err != nil {
return []std.Tx{}, fmt.Errorf("unable to load block at height %d: %w", blockNum, err) // nothing to see here
}
Expand All @@ -320,38 +319,38 @@ func (d *Node) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) {

// GetBlockTransactions returns the transactions contained
// within the specified block, if any
func (d *Node) CurrentBalances(blockNum uint64) ([]std.Tx, error) {
func (n *Node) CurrentBalances(blockNum uint64) ([]std.Tx, error) {
return nil, nil
}

// GetBlockTransactions returns the transactions contained
// within the specified block, if any
// GetLatestBlockNumber returns the latest block height from the chain
func (d *Node) GetLatestBlockNumber() (uint64, error) {
return d.getLatestBlockNumber(), nil
func (n *Node) GetLatestBlockNumber() (uint64, error) {
return n.getLatestBlockNumber(), nil
}

// SendTransaction executes a broadcast commit send
// of the specified transaction to the chain
func (d *Node) SendTransaction(tx *std.Tx) error {
func (n *Node) SendTransaction(tx *std.Tx) error {
aminoTx, err := amino.Marshal(tx)
if err != nil {
return fmt.Errorf("unable to marshal transaction to amino binary, %w", err)
}

// we use BroadcastTxCommit to ensure to have one block with the given tx
res, err := d.client.BroadcastTxCommit(aminoTx)
res, err := n.client.BroadcastTxCommit(aminoTx)
if err != nil {
return fmt.Errorf("unable to broadcast transaction commit: %w", err)
}

if res.CheckTx.Error != nil {
d.logger.Error("check tx error trace", "log", res.CheckTx.Log)
n.logger.Error("check tx error trace", "log", res.CheckTx.Log)
return fmt.Errorf("check transaction error: %w", res.CheckTx.Error)
}

if res.DeliverTx.Error != nil {
d.logger.Error("deliver tx error trace", "log", res.CheckTx.Log)
n.logger.Error("deliver tx error trace", "log", res.CheckTx.Log)
return fmt.Errorf("deliver transaction error: %w", res.DeliverTx.Error)
}

Expand Down
1 change: 0 additions & 1 deletion contribs/gnodev/pkg/emitter/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (m *middleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
events.EvtReload, events.EvtReset, events.EvtTxResult,
},
})

if err != nil {
panic("unable to execute template: " + err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion contribs/gnodev/pkg/logger/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func colorFromString(s string, saturation, lightness float64) lipgloss.Color {
hue := float64(hash32a(s) % 360)

r, g, b := hslToRGB(float64(hue), saturation, lightness)
r, g, b := hslToRGB(hue, saturation, lightness)
hex := rgbToHex(r, g, b)
return lipgloss.Color(hex)
}
Expand Down
2 changes: 1 addition & 1 deletion contribs/gnodev/pkg/logger/log_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (cl *columnWriter) Write(buf []byte) (n int, err error) {
buf = buf[todo:]

if cl.inline = i < 0; !cl.inline {
if _, err = cl.writer.Write([]byte(lf)); err != nil {
if _, err = cl.writer.Write(lf); err != nil {
return n, err
}
n++
Expand Down
Loading

0 comments on commit 80e4f99

Please sign in to comment.