Skip to content

Commit

Permalink
feat: build evmos and init genesis commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchon committed Jul 29, 2024
1 parent aec66d2 commit 8e4d168
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 9 deletions.
78 changes: 78 additions & 0 deletions cmd/playground/buildEvmos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package playground

import (
"fmt"
"os"

"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/spf13/cobra"
)

const LocalVesrsion = "local"

// buildEvmosCmd represents the buildEvmos command
var buildEvmosCmd = &cobra.Command{
Use: "build-evmos",
Short: "Build an specific version of Evmos (hanchond playground build-evmos v18.0.0), it also supports local repositories (hanchond playground build-evmos --path /home/hanchon/evmos)",
Long: `It downloads, builds and clean up temp files for any Evmos tag. Using the --path flag will build you local repo`,
Run: func(cmd *cobra.Command, args []string) {
_ = filesmanager.SetHomeFolderFromCobraFlags(cmd)

path, err := cmd.Flags().GetString("path")
// Local build
if err == nil && path != "" {
version := LocalVesrsion
if path[len(path)-1] == '/' {
path = path[0 : len(path)-2]
}
fmt.Println("Building evmos...")
if err := filesmanager.BuildEvmos(path); err != nil {
fmt.Println("error building evmos:", err.Error())
os.Exit(1)
}
fmt.Println("Moving built binary...")
if err := filesmanager.CopyFile(path+"/build/evmosd", filesmanager.GetEvmosdPath(version)); err != nil {
fmt.Println("could not move the built binary:", err.Error())
os.Exit(1)
}
os.Exit(0)
}

// Clone from github
if len(args) == 0 {
fmt.Println("version is missing. Usage: hanchond playground build-evmosd v18.1.0")
os.Exit(1)
}
version := args[0]
if err := filesmanager.CreateTempFolder(version); err != nil {
fmt.Println("could not create temp folder:" + err.Error())
os.Exit(1)
}
fmt.Println("Cloning evmos version:", version)
if err := filesmanager.GitCloneEvmosBranch(version); err != nil {
fmt.Println("could not clone the evmos version: ", err)
os.Exit(1)
}
fmt.Println("Building evmos...")
if err := filesmanager.BuildEvmosVersion(version); err != nil {
fmt.Println("error building evmos:", err)
os.Exit(1)
}
fmt.Println("Moving built binary...")
if err := filesmanager.SaveEvmosBuiltVersion(version); err != nil {
fmt.Println("could not move the built binary:", err.Error())
os.Exit(1)
}
fmt.Println("Cleaning up...")
if err := filesmanager.CleanUpTempFolder(); err != nil {
fmt.Println("could not remove temp folder", err.Error())
os.Exit(1)
}
os.Exit(0)
},
}

func init() {
PlaygroundCmd.AddCommand(buildEvmosCmd)
buildEvmosCmd.Flags().StringP("path", "p", "", "Path to you local clone of Evmos")
}
128 changes: 128 additions & 0 deletions cmd/playground/initGenesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package playground

import (
"context"
"fmt"
"os"
"strconv"

"github.com/hanchon/hanchond/playground/database"
"github.com/hanchon/hanchond/playground/evmos"
"github.com/hanchon/hanchond/playground/filesmanager"
"github.com/hanchon/hanchond/playground/sql"
"github.com/spf13/cobra"
)

func initDB(dbPath string) (*database.Queries, error) {
db, err := sql.InitDatabase(context.Background(), dbPath+"/playground.db")
if err != nil {
return nil, err
}
return database.New(db), nil
}

// initGenesisCmd represents the initGenesis command
var initGenesisCmd = &cobra.Command{
Use: "init-genesis",
Args: cobra.ExactArgs(1),
Short: "Init the genesis file for a new chain",
Long: `Set up the data and config folder for the new chain`,
Run: func(cmd *cobra.Command, args []string) {
home := filesmanager.SetHomeFolderFromCobraFlags(cmd)
queries, err := initDB(home)
if err != nil {
fmt.Println("could not init database", err.Error())
os.Exit(1)
}

version, err := cmd.Flags().GetString("version")
if err != nil {
fmt.Println("version flag was not set")
os.Exit(1)
}

chainid, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
fmt.Println("invalid chain id, it must be integer", err.Error())
os.Exit(1)
}

if !filesmanager.DoesEvmosdPathExist(version) {
fmt.Println("the evmos version was not found in the built folder", version)
os.Exit(1)

}

if filesmanager.IsNodeHomeFolderInitialized(chainid) {
fmt.Println("the home folder for this node was already created")
os.Exit(1)
}

path := filesmanager.GetNodeHomeFolder(chainid)
chainID := fmt.Sprintf("evmos_9001-%d", chainid)

e := evmos.NewEvmos(version, path, chainID, fmt.Sprintf("mykey%d", chainid))
if err := e.InitGenesis(); err != nil {
fmt.Println("could not init the genesis file", err.Error())
os.Exit(1)
}
if err := e.SetPorts(); err != nil {
fmt.Println("could not set the ports", err.Error())
os.Exit(1)
}

row, err := queries.InsertChain(context.Background(), database.InsertChainParams{
Name: fmt.Sprintf("chain%d", chainid),
ChainID: e.ChainID,
BinaryVersion: e.Version,
})
if err != nil {
fmt.Println("could not insert chain. ", err.Error())
os.Exit(1)
}

nodeID, err := queries.InsertNode(context.Background(), database.InsertNodeParams{
ChainID: row.ID,
ConfigFolder: path,
Moniker: e.Moniker,
ValidatorKey: e.ValMnemonic,
ValidatorKeyName: e.ValKeyName,
BinaryVersion: e.Version,
ProcessID: 0,
IsValidator: 1,
IsArchive: 0,
IsRunning: 0,
})
if err != nil {
fmt.Println("could not insert node", err.Error())
os.Exit(1)
}

err = queries.InsertPorts(context.Background(), database.InsertPortsParams{
NodeID: nodeID,
P1317: int64(e.Ports.P1317),
P8080: int64(e.Ports.P8080),
P9090: int64(e.Ports.P9090),
P9091: int64(e.Ports.P9091),
P8545: int64(e.Ports.P8545),
P8546: int64(e.Ports.P8546),
P6065: int64(e.Ports.P6065),
P26658: int64(e.Ports.P26658),
P26657: int64(e.Ports.P26657),
P6060: int64(e.Ports.P6060),
P26656: int64(e.Ports.P26656),
P26660: int64(e.Ports.P26660),
})
if err != nil {
fmt.Println("could not insert ports", err.Error())
os.Exit(1)
}

fmt.Println("Node added with id:", nodeID)
},
}

func init() {
PlaygroundCmd.AddCommand(initGenesisCmd)
initGenesisCmd.Flags().StringP("version", "v", "local", "Version of the Evmos node that you want to use, defaults to local. Tag names are supported.")
}
28 changes: 28 additions & 0 deletions docs/pages/hanchond/playground/buildEvmos.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Build Evmos

To run a network, you first need to chain binary. The command `build-evmos` allows you to build from tags

## Build from Github tags

Passing the version tag will automatically download the Github repo, build the binary and move the built binary to the playground data folder.

```sh
hanchond playground build-evmos v18.1.0
Cloning evmos version: v18.1.0
Building evmos...
Moving built binary...
```

## Build from a local repository

If you pass the `--path` flag, it will build your local Evmos and tag it for internal usage as `local`

```sh
hanchond playground build-evmos --path /Users/hanchon/devel/evmos/evmos
Building evmos...
Moving built binary..
```

:::info
Make sure that you are using the `local` version while interacting with the playground to use the Evmos built with the `--path` flag
:::
18 changes: 18 additions & 0 deletions docs/pages/hanchond/playground/initGenesis.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Init Genesis

Before starting the chain, a genesis file needs to be set up.

## Set up the genesis and config files

Running the `init-genesis` command will set up the genesis file and update the configuration files so the ports do not conflict with the rest of your network.

The ID argument is required, it must be an integer and will represent your chain.

```sh
hanchond playground init-genesis 1 --version local
Node added with id: 1
```

:::info
By default the `--version` flag will be `local`, it can be overwritten with any of the versions that were previously built
:::
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/ethereum/go-ethereum v1.11.5
github.com/evmos/evmos/v18 v18.1.0
github.com/mailru/easyjson v0.7.7
github.com/mattn/go-sqlite3 v1.11.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/miguelmota/go-ethereum-hdwallet v0.1.1
github.com/spf13/cobra v1.8.1
github.com/valyala/fasthttp v1.55.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,9 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q=
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
Expand Down
15 changes: 10 additions & 5 deletions playground/database/query.sql.go

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

2 changes: 2 additions & 0 deletions playground/filesmanager/build_evmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func BuildEvmos(path string) error {
}

func SaveEvmosBuiltVersion(version string) error {
// Ensure the path exists
_ = CreateBuildsDir()
return CopyFile(GetBranchFolder(version)+"/build/evmosd", GetEvmosdPath(version))
}

Expand Down
39 changes: 38 additions & 1 deletion playground/filesmanager/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,47 @@ func SetBaseDir(path string) {
baseDir = path
}

func SetHomeFolderFromCobraFlags(cmd *cobra.Command) {
func SetHomeFolderFromCobraFlags(cmd *cobra.Command) string {
home, err := cmd.Flags().GetString("home")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
home, _ = strings.CutSuffix(home, "/")
SetBaseDir(home)
// Ensure that the folder exists
if _, err := os.Stat(home); os.IsNotExist(err) {
if err := os.Mkdir(home, os.ModePerm); err != nil {
// We panic here because if we can not create the folder we should inmediately stop
panic(err)
}
}
return home
}

func GetDataFolder() string {
return fmt.Sprintf("%s/data", GetBaseDir())
}

func getNodeHomePath(chainID int64) string {
return fmt.Sprintf("%s/%d", GetDataFolder(), chainID)
}

func GetNodeHomeFolder(chainID int64) string {
if _, err := os.Stat(GetDataFolder()); os.IsNotExist(err) {
if err := os.Mkdir(GetDataFolder(), os.ModePerm); err != nil {
// We panic here because if we can not create the folder we should inmediately stop
panic(err)
}
}
return getNodeHomePath(chainID)
}

func IsNodeHomeFolderInitialized(chainID int64) bool {
if _, err := os.Stat(getNodeHomePath(chainID)); os.IsNotExist(err) {
return false
}
return true
}

func GetBaseDir() string {
Expand All @@ -44,6 +77,10 @@ func GetEvmosdPath(version string) string {
return GetBuildsDir() + "/evmosd" + version
}

func DoesEvmosdPathExist(version string) bool {
return DoesFileExist(GetBuildsDir() + "/evmosd" + version)
}

func GetHermesBinary() string {
return GetBuildsDir() + "/hermes"
}
Expand Down
1 change: 1 addition & 0 deletions playground/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"

_ "embed"

// blank import to support sqlite3
_ "github.com/mattn/go-sqlite3"
)
Expand Down
3 changes: 2 additions & 1 deletion playground/sql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ INSERT INTO chain(
) VALUES (
?,?,?
)
RETURNING ID;
RETURNING *;

-- name: InsertNode :one
INSERT INTO node(
Expand Down Expand Up @@ -61,3 +61,4 @@ SELECT * FROM ports;

-- name: GetChain :one
SELECT * FROM chain where id =? LIMIT 1;

Loading

0 comments on commit 8e4d168

Please sign in to comment.