-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
207 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/gnolang/gno/gno.land/pkg/gnoland" | ||
"github.com/gnolang/gno/tm2/pkg/bft/config" | ||
"github.com/gnolang/gno/tm2/pkg/bft/privval" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/log" | ||
osm "github.com/gnolang/gno/tm2/pkg/os" | ||
) | ||
|
||
func newInitCmd(io *commands.IO) *commands.Command { | ||
cfg := &initCfg{} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "init", | ||
ShortUsage: "init [flags]", | ||
ShortHelp: "Initialize a gnoland configuration on file-system", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execInit(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
type initCfg struct { | ||
// common | ||
rootDir string | ||
config string | ||
|
||
// gnoland init specific | ||
chainID string | ||
genesisTxsFile string | ||
genesisRemote string | ||
skipFailingGenesisTxs bool | ||
genesisBalancesFile string | ||
genesisMaxVMCycles int64 | ||
} | ||
|
||
func (c *initCfg) RegisterFlags(fs *flag.FlagSet) { | ||
// common | ||
fs.StringVar(&c.rootDir, "root-dir", "testdir", "directory for config and data") | ||
fs.StringVar(&c.config, "config", "", "config file (optional)") | ||
|
||
// init specific | ||
fs.BoolVar(&c.skipFailingGenesisTxs, "skip-failing-genesis-txs", false, "don't panic when replaying invalid genesis txs") | ||
fs.StringVar(&c.genesisBalancesFile, "genesis-balances-file", "./genesis/genesis_balances.txt", "initial distribution file") | ||
fs.StringVar(&c.genesisTxsFile, "genesis-txs-file", "./genesis/genesis_txs.txt", "initial txs to replay") | ||
fs.StringVar(&c.chainID, "chainid", "dev", "the ID of the chain") | ||
fs.StringVar(&c.genesisRemote, "genesis-remote", "localhost:26657", "replacement for '%%REMOTE%%' in genesis") | ||
fs.Int64Var(&c.genesisMaxVMCycles, "genesis-max-vm-cycles", 10_000_000, "set maximum allowed vm cycles per operation. Zero means no limit.") | ||
} | ||
|
||
func execInit(c *initCfg, args []string, io *commands.IO) error { | ||
logger := log.NewTMLogger(log.NewSyncWriter(io.Out)) | ||
rootDir := c.rootDir | ||
|
||
cfg := config.LoadOrMakeConfigWithOptions(rootDir, func(cfg *config.Config) { | ||
cfg.Consensus.CreateEmptyBlocks = true | ||
cfg.Consensus.CreateEmptyBlocksInterval = 0 * time.Second | ||
}) | ||
|
||
// create priv validator first. | ||
// need it to generate genesis.json | ||
newPrivValKey := cfg.PrivValidatorKeyFile() | ||
newPrivValState := cfg.PrivValidatorStateFile() | ||
priv := privval.LoadOrGenFilePV(newPrivValKey, newPrivValState) | ||
|
||
// write genesis file if missing. | ||
genesisFilePath := filepath.Join(rootDir, cfg.Genesis) | ||
if !osm.FileExists(genesisFilePath) { | ||
genDoc := makeGenesisDoc( | ||
priv.GetPubKey(), | ||
c.chainID, | ||
c.genesisBalancesFile, | ||
loadGenesisTxs(c.genesisTxsFile, c.chainID, c.genesisRemote), | ||
) | ||
writeGenesisFile(genDoc, genesisFilePath) | ||
} | ||
|
||
// create application and node. | ||
gnoApp, err := gnoland.NewApp(rootDir, c.skipFailingGenesisTxs, logger, c.genesisMaxVMCycles) | ||
if err != nil { | ||
return fmt.Errorf("error in creating new app: %w", err) | ||
} | ||
|
||
cfg.LocalApp = gnoApp | ||
|
||
fmt.Fprintln(io.Err, "Node created.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/gnolang/gno/gno.land/pkg/gnoland" | ||
"github.com/gnolang/gno/tm2/pkg/bft/config" | ||
"github.com/gnolang/gno/tm2/pkg/bft/node" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/log" | ||
osm "github.com/gnolang/gno/tm2/pkg/os" | ||
) | ||
|
||
func newStartCmd(io *commands.IO) *commands.Command { | ||
cfg := &startCfg{} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "start", | ||
ShortUsage: "start [flags]", | ||
ShortHelp: "Run the full node", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execStart(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
type startCfg struct { | ||
init initCfg | ||
} | ||
|
||
func (s startCfg)RegisterFlags(fs *flag.FlagSet) { | ||
s.init.RegisterFlags(fs) | ||
} | ||
|
||
func execStart(c *startCfg, args []string, io *commands.IO) error { | ||
rootDir := c.init.rootDir | ||
logger := log.NewTMLogger(log.NewSyncWriter(io.Out)) | ||
|
||
// lazy init | ||
if !config.Exists(rootDir) { | ||
err := execInit(&c.init, []string{}, io) // XXX: create an helper instead of calling the cmd? | ||
if err != nil { | ||
return fmt.Errorf("lazy init: %w", err) | ||
} | ||
} | ||
|
||
// load (existing) config | ||
cfg := config.LoadOrMakeDefaultConfig(rootDir) | ||
gnoApp, err := gnoland.NewApp(rootDir, c.init.skipFailingGenesisTxs, logger, c.init.genesisMaxVMCycles) | ||
if err != nil { | ||
return fmt.Errorf("error in creating new app: %w", err) | ||
} | ||
cfg.LocalApp = gnoApp | ||
|
||
// create node | ||
gnoNode, err := node.DefaultNewNode(cfg, logger) | ||
if err != nil { | ||
return fmt.Errorf("error in creating node: %w", err) | ||
} | ||
|
||
// start node | ||
fmt.Fprintln(io.Err, "Starting Node...") | ||
if err := gnoNode.Start(); err != nil { | ||
return fmt.Errorf("error in start node: %w", err) | ||
} | ||
|
||
// run forever | ||
osm.TrapSignal(func() { | ||
if gnoNode.IsRunning() { | ||
_ = gnoNode.Stop() | ||
} | ||
}) | ||
select {} // run forever | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.