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 23 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
96 changes: 88 additions & 8 deletions contribs/gnodev/cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
Expand Down Expand Up @@ -48,8 +49,11 @@
home string
root string
premineAccounts varPremineAccounts
balancesFile string
txsFile string

// Files
balancesFile string
genesisFile string
txsFile string

// Node Configuration
minimal bool
Expand Down Expand Up @@ -144,6 +148,13 @@
"load the provided transactions file (refer to the documentation for format)",
)

fs.StringVar(
&c.genesisFile,
"genesis-file",
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
defaultDevOptions.genesisFile,
"load the given genesis file",
)

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L151-L156

Added lines #L151 - L156 were not covered by tests

fs.StringVar(
&c.deployKey,
"deploy-key",
Expand Down Expand Up @@ -201,10 +212,22 @@
)
}

func (c *devCfg) validateConfigFlags() error {
if (c.balancesFile != "" || c.txsFile != "") && c.genesisFile != "" {
return errors.New("cannot specify `balances-file` or `txs-file` along with `genesis-file`")

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L215-L217

Added lines #L215 - L217 were not covered by tests
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
}

return nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L220 was not covered by tests
}

func execDev(cfg *devCfg, args []string, io commands.IO) (err error) {
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(nil)

if err := cfg.validateConfigFlags(); err != nil {
return fmt.Errorf("validate error: %w", err)

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L227-L228

Added lines #L227 - L228 were not covered by tests
}

// Setup Raw Terminal
rt, restore, err := setupRawTerm(cfg, io)
if err != nil {
Expand Down Expand Up @@ -244,7 +267,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 := setupDevNode(ctx, cfg, nodeCfg)

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L270-L271

Added lines #L270 - L271 were not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -300,11 +324,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 @@ -315,6 +343,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
var exported uint
path, err := os.MkdirTemp("", "gnodev-export")
if err != nil {
return fmt.Errorf("unable to create `export` directory: %w", err)

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L348-L351

Added lines #L348 - L351 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L354-L356

Added lines #L354 - L356 were not covered by tests
}
}()

keyPressCh := listenForKeyPress(logger.WithGroup(KeyPressLogName), rt)
for {
var err error
Expand Down Expand Up @@ -366,6 +408,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 415 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L411 - L415 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 424 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L418-L424

Added lines #L418 - L424 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 430 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L427-L430

Added lines #L427 - L430 were not covered by tests
}
exported++

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L432 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 439 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L434-L439

Added lines #L434 - L439 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 446 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L442-L446

Added lines #L442 - L446 were not covered by tests
}

case rawterm.KeyCtrlC: // Exit
return nil
default:
Expand Down
56 changes: 42 additions & 14 deletions contribs/gnodev/cmd/gnodev/setup_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,57 @@
gnodev "github.com/gnolang/gno/contribs/gnodev/pkg/dev"
"github.com/gnolang/gno/contribs/gnodev/pkg/emitter"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/bft/types"
)

// setupDevNode initializes and returns a new DevNode.
func setupDevNode(
ctx context.Context,
logger *slog.Logger,
cfg *devCfg,
remitter emitter.Emitter,
balances gnoland.Balances,
pkgspath []gnodev.PackagePath,
devCfg *devCfg,
nodeConfig *gnodev.NodeConfig,
) (*gnodev.Node, error) {
// Load transactions.
txs, err := parseTxs(cfg.txsFile)
if err != nil {
return nil, fmt.Errorf("unable to load transactions: %w", err)
logger := nodeConfig.Logger

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L22 was not covered by tests

if devCfg.txsFile != "" { // Load txs files
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
var err error
nodeConfig.InitialTxs, err = parseTxs(devCfg.txsFile)
if err != nil {
return nil, fmt.Errorf("unable to load transactions: %w", err)

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L24-L28

Added lines #L24 - L28 were not covered by tests
}
} else if devCfg.genesisFile != "" { // Load genesis file
state, err := extractAppStateFromGenesisFile(devCfg.genesisFile)
if err != nil {
return nil, fmt.Errorf("unable to load genesis file %q: %w", devCfg.genesisFile, err)

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

View check run for this annotation

Codecov / codecov/patch

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

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

// Override balances and txs
nodeConfig.BalancesList = state.Balances
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
nodeConfig.InitialTxs = state.Txs

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L37-L38

Added lines #L37 - L38 were not covered by tests

logger.Info("genesis file loaded", "path", devCfg.genesisFile, "txs", len(nodeConfig.InitialTxs))

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L40 was not covered by tests
}

config := setupDevNodeConfig(cfg, balances, pkgspath, txs)
return gnodev.NewDevNode(ctx, logger, remitter, config)
return gnodev.NewDevNode(ctx, nodeConfig)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L43 was not covered by tests
}

// setupDevNodeConfig creates and returns a new dev.NodeConfig.
func setupDevNodeConfig(
cfg *devCfg,
logger *slog.Logger,
emitter emitter.Emitter,
balances gnoland.Balances,
pkgspath []gnodev.PackagePath,
txs []std.Tx,
) *gnodev.NodeConfig {
config := gnodev.DefaultNodeConfig(cfg.root)

config.Logger = logger
config.Emitter = emitter

Check warning on line 57 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-L57

Added lines #L56 - L57 were not covered by tests
config.BalancesList = balances.List()
config.PackagesPathList = pkgspath
config.TMConfig.RPC.ListenAddress = resolveUnixOrTCPAddr(cfg.nodeRPCListenerAddr)
config.NoReplay = cfg.noReplay
config.MaxGasPerBlock = cfg.maxGas
config.ChainID = cfg.chainId
config.Txs = txs

// other listeners
config.TMConfig.P2P.ListenAddress = defaultDevOptions.nodeP2PListenerAddr
Expand All @@ -55,6 +69,20 @@
return config
}

func extractAppStateFromGenesisFile(path string) (*gnoland.GnoGenesisState, error) {
doc, err := types.GenesisDocFromFile(path)
if err != nil {
return nil, fmt.Errorf("unable to parse doc file: %w", err)

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L72-L75

Added lines #L72 - L75 were not covered by tests
}

state, ok := doc.AppState.(gnoland.GnoGenesisState)
if !ok {
return nil, fmt.Errorf("invalid `GnoGenesisState` app state")

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/setup_node.go#L78-L80

Added lines #L78 - L80 were not covered by tests
}

return &state, nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L83 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