diff --git a/contribs/gnodev/Makefile b/contribs/gnodev/Makefile index 7395b166a3e..b98ce0fb44b 100644 --- a/contribs/gnodev/Makefile +++ b/contribs/gnodev/Makefile @@ -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 ./... diff --git a/contribs/gnodev/cmd/gnodev/main.go b/contribs/gnodev/cmd/gnodev/main.go index 648fe78b20f..945f92c9ce6 100644 --- a/contribs/gnodev/cmd/gnodev/main.go +++ b/contribs/gnodev/cmd/gnodev/main.go @@ -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" @@ -52,7 +52,6 @@ type devCfg struct { // Node Configuration minimal bool verbose bool - hotreload bool noWatch bool noReplay bool maxGas int64 @@ -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() @@ -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 @@ -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 @@ -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") @@ -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) } @@ -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 diff --git a/contribs/gnodev/pkg/dev/node.go b/contribs/gnodev/pkg/dev/node.go index 786c9349d0e..971d3010bb1 100644 --- a/contribs/gnodev/pkg/dev/node.go +++ b/contribs/gnodev/pkg/dev/node.go @@ -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 } @@ -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 } @@ -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 } @@ -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) } diff --git a/contribs/gnodev/pkg/emitter/middleware.go b/contribs/gnodev/pkg/emitter/middleware.go index 80c07ec93aa..9c53cfe158e 100644 --- a/contribs/gnodev/pkg/emitter/middleware.go +++ b/contribs/gnodev/pkg/emitter/middleware.go @@ -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()) } diff --git a/contribs/gnodev/pkg/logger/colors.go b/contribs/gnodev/pkg/logger/colors.go index b0499e01722..1f6ec097488 100644 --- a/contribs/gnodev/pkg/logger/colors.go +++ b/contribs/gnodev/pkg/logger/colors.go @@ -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) } diff --git a/contribs/gnodev/pkg/logger/log_column.go b/contribs/gnodev/pkg/logger/log_column.go index 8e145e89175..2a720525903 100644 --- a/contribs/gnodev/pkg/logger/log_column.go +++ b/contribs/gnodev/pkg/logger/log_column.go @@ -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++ diff --git a/contribs/gnodev/pkg/watcher/watch.go b/contribs/gnodev/pkg/watcher/watch.go index a9ab189947f..63158a06c4b 100644 --- a/contribs/gnodev/pkg/watcher/watch.go +++ b/contribs/gnodev/pkg/watcher/watch.go @@ -61,7 +61,7 @@ func (p *PackageWatcher) startWatching() { defer close(pkgsUpdateChan) var debounceTimer <-chan time.Time - var pathList = []string{} + pathList := []string{} var err error for err == nil { diff --git a/tm2/pkg/crypto/keys/client/export.go b/tm2/pkg/crypto/keys/client/export.go index bb368342ef8..b80942734f6 100644 --- a/tm2/pkg/crypto/keys/client/export.go +++ b/tm2/pkg/crypto/keys/client/export.go @@ -99,6 +99,13 @@ func execExport(cfg *ExportCfg, io commands.IO) error { cfg.NameOrBech32, decryptPassword, ) + + privk, err := kb.ExportPrivateKeyObject(cfg.NameOrBech32, decryptPassword) + if err != nil { + panic(err) + } + + fmt.Printf("privk:\n%x\n", privk.Bytes()) } else { // Get the armor encrypt password encryptPassword, err := io.GetCheckPassword(