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 all 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
102 changes: 94 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 @@ -30,6 +31,8 @@
AccountsLogName = "Accounts"
)

var ErrConflictingFileArgs = errors.New("cannot specify `balances-file` or `txs-file` along with `genesis-file`")

var (
DefaultDeployerName = integration.DefaultAccount_Name
DefaultDeployerAddress = crypto.MustAddressFromString(integration.DefaultAccount_Address)
Expand All @@ -47,8 +50,11 @@
home string
root string
premineAccounts varPremineAccounts
balancesFile string
txsFile string

// Files
balancesFile string
genesisFile string
txsFile string

// Web Configuration
webListenerAddr string
Expand Down Expand Up @@ -155,6 +161,13 @@
"load the provided transactions file (refer to the documentation for format)",
)

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

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L164-L169

Added lines #L164 - L169 were not covered by tests

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

func (c *devCfg) validateConfigFlags() error {
if (c.balancesFile != "" || c.txsFile != "") && c.genesisFile != "" {
return ErrConflictingFileArgs

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

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L235-L237

Added lines #L235 - L237 were not covered by tests
}

return nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L240 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 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
}

// Setup Raw Terminal
rt, restore, err := setupRawTerm(cfg, io)
if err != nil {
Expand Down Expand Up @@ -262,7 +287,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 291 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L290-L291

Added lines #L290 - L291 were not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -337,11 +363,15 @@
var helper string = `For more in-depth documentation, visit the GNO Tooling CLI documentation:
https://docs.gno.land/gno-tooling/cli/gno-tooling-gnodev

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 @@ -352,6 +382,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 390 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L387-L390

Added lines #L387 - L390 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L393 - L395 were not covered by tests
}
}()

keyPressCh := listenForKeyPress(logger.WithGroup(KeyPressLogName), rt)
for {
var err error
Expand Down Expand Up @@ -387,8 +431,10 @@
switch key.Upper() {
case rawterm.KeyH: // Helper
logger.Info("Gno Dev Helper", "helper", helper)

case rawterm.KeyA: // Accounts
logAccounts(logger.WithGroup(AccountsLogName), bk, dnode)

case rawterm.KeyR: // Reload
logger.WithGroup(NodeLogName).Info("reloading...")
if err = dnode.ReloadAll(ctx); err != nil {
Expand All @@ -403,8 +449,48 @@
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 456 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L452-L456

Added lines #L452 - L456 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 465 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L459-L465

Added lines #L459 - L465 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 471 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L468-L471

Added lines #L468 - L471 were not covered by tests
}
exported++

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L473 was not covered by tests

logger.WithGroup(NodeLogName).Info("node state exported", "file", docfile)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L475 was not covered by tests
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 481 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L477-L481

Added lines #L477 - L481 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 488 in contribs/gnodev/cmd/gnodev/main.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/cmd/gnodev/main.go#L484-L488

Added lines #L484 - L488 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
2 changes: 1 addition & 1 deletion contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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.11.0 // indirect
github.com/rs/xid v1.5.0 // indirect
Expand Down
4 changes: 2 additions & 2 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