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(gnodev): txs manipulation ability #2286

Merged
merged 40 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
538b29f
feat: initial timemachine implem
gfanton Apr 24, 2024
7b0dcdc
Merge remote-tracking branch 'origin/master' into fix/gnodev-time-dri…
gfanton May 29, 2024
a38c4ae
feat: add genesis load
gfanton Jun 5, 2024
51e02dd
chore: lint
gfanton Jun 5, 2024
b2fc5eb
Merge branch 'master' into feat/gnodev-handle-tx
gfanton Jun 5, 2024
079f82e
chore: update gnodev doc
gfanton Jun 5, 2024
c589e5d
fix: add events unknown for mock emitter
gfanton Jun 5, 2024
9af80cb
fix: add node state test
gfanton Jun 5, 2024
69f8c50
fix: unknown event emitter
gfanton Jun 5, 2024
8f5d77b
fix: use mocked null event for test
gfanton Jun 5, 2024
a18d02b
Merge remote-tracking branch 'origin/master' into feat/gnodev-handle-tx
gfanton Jun 7, 2024
9775071
fix: typo
gfanton Jun 7, 2024
1170a47
Merge remote-tracking branch 'origin/master' into feat/gnodev-handle-tx
gfanton Jun 7, 2024
e9b83b0
chore: lint
gfanton Jun 7, 2024
a35e7a7
chore: remove useless field
gfanton Jun 7, 2024
c2ec274
Merge remote-tracking branch 'origin/master' into feat/gnodev-handle-tx
gfanton Jun 10, 2024
727bf2a
fix: bade rebase
gfanton Jun 10, 2024
bc9416c
fix: remove useless config init
gfanton Jun 10, 2024
19a4e85
fix: typo
gfanton Jun 10, 2024
7316ebd
fix: use uint for exported var counter
gfanton Jun 10, 2024
fee8837
chore: improve comment
gfanton Jun 10, 2024
7133f20
fix: wrong deleted fields
gfanton Jun 10, 2024
7502ac3
Merge branch 'master' into feat/gnodev-handle-tx
gfanton Jun 10, 2024
67722e4
Merge remote-tracking branch 'origin/master' into feat/gnodev-handle-tx
gfanton Jun 24, 2024
c0ee7d1
chore: update genesis-file name to genesis for constancy
gfanton Jun 24, 2024
a39d826
chore: global error flags
gfanton Jun 24, 2024
79f2e2f
chore: lint case space
gfanton Jun 24, 2024
852eeea
chore: remove global noop logger
gfanton Jun 24, 2024
c026fb7
chore: todo
gfanton Jun 24, 2024
29afbc9
fix: noop logger
gfanton Jun 24, 2024
c721a98
fix: add empty state error
gfanton Jun 24, 2024
a6049d6
feat: rename moveFrom to moveBy
gfanton Jun 24, 2024
efc8d31
chore: wrap context generation in test
gfanton Jun 24, 2024
fd5dba2
chore: inline node rebuild error
gfanton Jun 24, 2024
e4175db
chore: wrap realm name into a const
gfanton Jun 24, 2024
3dcde7a
Merge branch 'master' into feat/gnodev-handle-tx
gfanton Jun 24, 2024
e165f62
chore: lint
gfanton Jun 24, 2024
abd8c18
Merge remote-tracking branch 'origin/master' into feat/gnodev-handle-tx
gfanton Jul 8, 2024
c6f8da2
fix: untabify helper
gfanton Jul 8, 2024
270e391
Merge branch 'master' into feat/gnodev-handle-tx
gfanton Jul 8, 2024
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
77 changes: 71 additions & 6 deletions contribs/gnodev/cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
root string
premineAccounts varPremineAccounts
balancesFile string
genesisFile string

// Node Configuration
minimal bool
Expand Down Expand Up @@ -171,6 +172,13 @@
"set node ChainID",
)

fs.StringVar(
&c.genesisFile,
"genesis-file",
defaultDevOptions.genesisFile,
"load the given genesis file",
)

Check warning on line 180 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L175-L180

Added lines #L175 - L180 were not covered by tests

fs.BoolVar(
&c.noWatch,
"no-watch",
Expand Down Expand Up @@ -236,7 +244,8 @@
// Setup Dev Node
// XXX: find a good way to export or display node logs
nodeLogger := logger.WithGroup(NodeLogName)
devNode, err := setupDevNode(ctx, nodeLogger, cfg, emitterServer, balances, pkgpaths)
nodeCfg := setupDevNodeConfig(cfg, logger, emitterServer, balances, pkgpaths)
devNode, err := gnodev.NewDevNode(ctx, nodeCfg)

Check warning on line 248 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L247-L248

Added lines #L247 - L248 were not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -292,11 +301,15 @@
}

var helper string = `
A Accounts - Display known accounts and balances
H Help - Display this message
R Reload - Reload all packages to take change into account.
Ctrl+R Reset - Reset application state.
Ctrl+C Exit - Exit the application
P Previous TX - Go to the previous tx
N Next TX - Go to the next tx
E Export - Export the current state as genesis doc
A Accounts - Display known accounts and balances
H Help - Display this message
R Reload - Reload all packages to take change into account.
Ctrl+S Save State - Save the current state
Ctrl+R Reset - Reset application to it's initial/save state.
Ctrl+C Exit - Exit the application
`

func runEventLoop(
Expand All @@ -307,6 +320,20 @@
dnode *gnodev.Node,
watch *watcher.PackageWatcher,
) error {
// XXX: move this in above, but we need to have a proper struct first
// XXX: make this configurable
exported := 0
gfanton marked this conversation as resolved.
Show resolved Hide resolved
path, err := os.MkdirTemp("", "gnodev-export")
if err != nil {
return fmt.Errorf("unable to create `export` directory: %w", err)

Check warning on line 328 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L325-L328

Added lines #L325 - L328 were not covered by tests
}

defer func() {
if exported == 0 {
_ = os.RemoveAll(path)

Check warning on line 333 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L331-L333

Added lines #L331 - L333 were not covered by tests
}
}()

keyPressCh := listenForKeyPress(logger.WithGroup(KeyPressLogName), rt)
for {
var err error
Expand Down Expand Up @@ -358,6 +385,44 @@
Error("unable to reset node state", "err", err)
}

case rawterm.KeyCtrlS: // Save
logger.WithGroup(NodeLogName).Info("saving state...")
if err := dnode.SaveCurrentState(ctx); err != nil {
logger.WithGroup(NodeLogName).
Error("unable to save node state", "err", err)

Check warning on line 392 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L388-L392

Added lines #L388 - L392 were not covered by tests
}

case rawterm.KeyE:
logger.WithGroup(NodeLogName).Info("exporting state...")
doc, err := dnode.ExportStateAsGenesis(ctx)
if err != nil {
logger.WithGroup(NodeLogName).
Error("unable to export node state", "err", err)
continue

Check warning on line 401 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L395-L401

Added lines #L395 - L401 were not covered by tests
}

docfile := filepath.Join(path, fmt.Sprintf("export_%d.jsonl", exported))
if err := doc.SaveAs(docfile); err != nil {
logger.WithGroup(NodeLogName).
Error("unable to save genesis", "err", err)

Check warning on line 407 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L404-L407

Added lines #L404 - L407 were not covered by tests
}
exported++

Check warning on line 409 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L409

Added line #L409 was not covered by tests

logger.WithGroup(NodeLogName).Info("node state exported", "file", docfile)
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
case rawterm.KeyN: // Next tx
logger.Info("moving forward...")
if err := dnode.MoveToNextTX(ctx); err != nil {
logger.WithGroup(NodeLogName).
Error("unable to move forward", "err", err)

Check warning on line 416 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L411-L416

Added lines #L411 - L416 were not covered by tests
}

case rawterm.KeyP: // Next tx
logger.Info("moving backward...")
if err := dnode.MoveToPreviousTX(ctx); err != nil {
logger.WithGroup(NodeLogName).
Error("unable to move backward", "err", err)

Check warning on line 423 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L419-L423

Added lines #L419 - L423 were not covered by tests
}

case rawterm.KeyCtrlC: // Exit
return nil
default:
Expand Down
47 changes: 33 additions & 14 deletions contribs/gnodev/cmd/gnodev/setup_node.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
package main

import (
"context"
"fmt"
"log/slog"
"net"
"strings"

gnodev "github.com/gnolang/gno/contribs/gnodev/pkg/dev"
"github.com/gnolang/gno/contribs/gnodev/pkg/emitter"
emitter "github.com/gnolang/gno/contribs/gnodev/pkg/emitter"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/std"
)

// setupDevNode initializes and returns a new DevNode.
func setupDevNode(
ctx context.Context,
logger *slog.Logger,
// setupDevNodeConfig creates and returns a new dev.NodeConfig.
gfanton marked this conversation as resolved.
Show resolved Hide resolved
func setupDevNodeConfig(
cfg *devCfg,
remitter emitter.Emitter,
logger *slog.Logger,
emitter emitter.Emitter,
balances gnoland.Balances,
pkgspath []gnodev.PackagePath,
) (*gnodev.Node, error) {
config := setupDevNodeConfig(cfg, balances, pkgspath)
return gnodev.NewDevNode(ctx, logger, remitter, config)
}

// setupDevNodeConfig creates and returns a new dev.NodeConfig.
func setupDevNodeConfig(cfg *devCfg, balances gnoland.Balances, pkgspath []gnodev.PackagePath) *gnodev.NodeConfig {
) *gnodev.NodeConfig {

Check warning on line 23 in contribs/gnodev/cmd/gnodev/setup_node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L23

Added line #L23 was not covered by tests
config := gnodev.DefaultNodeConfig(cfg.root)
if cfg.genesisFile != "" {
var err error
if config.InitialTxs, err = extractTxsFromGenesisFile(cfg.genesisFile); err != nil {
logger.Error("unable to load genesis file", "path", cfg.genesisFile, "err", err)
} else {
logger.Info("genesis file loaded", "path", cfg.genesisFile, "txs", len(config.InitialTxs))

Check warning on line 30 in contribs/gnodev/cmd/gnodev/setup_node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L25-L30

Added lines #L25 - L30 were not covered by tests
}
}

config.Logger = logger
config.Emitter = emitter

Check warning on line 35 in contribs/gnodev/cmd/gnodev/setup_node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L34-L35

Added lines #L34 - L35 were not covered by tests
config.BalancesList = balances.List()
config.PackagesPathList = pkgspath
config.TMConfig.RPC.ListenAddress = resolveUnixOrTCPAddr(cfg.nodeRPCListenerAddr)
Expand All @@ -42,6 +47,20 @@
return config
}

func extractTxsFromGenesisFile(path string) (txs []std.Tx, err error) {
doc, err := types.GenesisDocFromFile(path)
if err != nil {
return []std.Tx{}, fmt.Errorf("unable to parse doc file: %w", err)

Check warning on line 53 in contribs/gnodev/cmd/gnodev/setup_node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L50-L53

Added lines #L50 - L53 were not covered by tests
}

state, ok := doc.AppState.(gnoland.GnoGenesisState)
if !ok {
return []std.Tx{}, fmt.Errorf("invalid `GnoGenesisState` app state")

Check warning on line 58 in contribs/gnodev/cmd/gnodev/setup_node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L56-L58

Added lines #L56 - L58 were not covered by tests
}

return state.Txs, nil

Check warning on line 61 in contribs/gnodev/cmd/gnodev/setup_node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L61

Added line #L61 was not covered by tests
}

func resolveUnixOrTCPAddr(in string) (out string) {
var err error
var addr net.Addr
Expand Down
5 changes: 3 additions & 2 deletions contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/peterbourgon/ff/v3 v3.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/rs/xid v1.5.0 // indirect
Expand All @@ -67,7 +67,8 @@ require (
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
Expand Down
12 changes: 6 additions & 6 deletions contribs/gnodev/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions contribs/gnodev/internal/mock/server_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"github.com/gnolang/gno/contribs/gnodev/pkg/events"
)

// Define empty event for NextEvent empty queue
var (
eventNull = events.Custom("NULL")
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
EvtNull = eventNull.Type()
)

// ServerEmitter is an `emitter.Emitter`
var _ emitter.Emitter = (*ServerEmitter)(nil)

Expand All @@ -23,6 +29,8 @@ func (m *ServerEmitter) Emit(evt events.Event) {
}

func (m *ServerEmitter) NextEvent() (evt events.Event) {
evt = eventNull

m.muEvents.Lock()
defer m.muEvents.Unlock()

Expand Down
Loading
Loading