Skip to content

Commit

Permalink
feat: cosmos-hub support (#12)
Browse files Browse the repository at this point in the history
* feat: download gaiad

* chore: push the wip code

* wip

* feat: running a 2 validators network in cosmos-hub

* wip

* feat: multi chain command

* feat: start all the nodes with just one command

* feat: evmos multi node support

* wip

* fix: hermes working with 2 evmos

* feat: hermes working with evmos and cosmos

* chore: support evmos and gaia in start node

* fix: start command using the node config

* chore: remove commented code

* chore: remove print

* docs: update sidebar

* chore: fix linter issues
  • Loading branch information
hanchon authored Aug 14, 2024
1 parent 258f9d7 commit 271f422
Show file tree
Hide file tree
Showing 53 changed files with 2,322 additions and 905 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Hanchon's web3 toolkit

- [docs](https://hanchond.com)

## Requirements:

- `git` to clone the repositories that needs to be build locally
- `go` to build the Evmos client
- `wget` to download the Cosmos-Hub client
- `cargo/rust` to build Hermes

## TODOs:

- Playground queries should use the rest endpoint so the response is in JSON format. That way we can pipe it into `jq` and pipe the result into another query.
2 changes: 1 addition & 1 deletion cmd/playground/buildEvmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var buildEvmosCmd = &cobra.Command{
os.Exit(1)
}
fmt.Println("Moving built binary...")
if err := filesmanager.CopyFile(path+"/build/evmosd", filesmanager.GetEvmosdPath(version)); err != nil {
if err := filesmanager.MoveFile(path+"/build/evmosd", filesmanager.GetEvmosdPath(version)); err != nil {
fmt.Println("could not move the built binary:", err.Error())
os.Exit(1)
}
Expand Down
50 changes: 50 additions & 0 deletions cmd/playground/buildGaiad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package playground

import (
"fmt"
"os"

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

// buildGaiadCmd represents the buildGaiad command
var buildGaiadCmd = &cobra.Command{
Use: "build-gaiad",
Short: "Get the Gaiad binary from the github releases",
Long: `It downloads the already built gaiad binary from github, it accepts a version flag to specify any tag. It defaults to: v1.9.0.`,
Run: func(cmd *cobra.Command, _ []string) {
_ = filesmanager.SetHomeFolderFromCobraFlags(cmd)
version, err := cmd.Flags().GetString("version")
if err != nil {
fmt.Println("could not read the version:", err.Error())
os.Exit(1)
}

isDarwin, err := cmd.Flags().GetBool("is-darwin")
if err != nil {
fmt.Println("could not read the isDarwin:", err.Error())
os.Exit(1)
}

// Create build folder if needed
if err := filesmanager.CreateBuildsDir(); err != nil {
fmt.Println("could not create build folder:" + err.Error())
os.Exit(1)
}

fmt.Println("Downloading gaiad from github:", version)
if err = gaia.GetGaiadBinary(isDarwin, version); err != nil {
fmt.Println("could not get gaiad from github:" + err.Error())
os.Exit(1)
}
fmt.Println("Gaiad is now available")
},
}

func init() {
PlaygroundCmd.AddCommand(buildGaiadCmd)
buildGaiadCmd.PersistentFlags().StringP("version", "v", "v18.1.0", "Gaiad version to download")
buildGaiadCmd.PersistentFlags().Bool("is-darwin", true, "Is the system MacOS arm?")
}
101 changes: 63 additions & 38 deletions cmd/playground/hermesAddChannel.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,98 @@
package playground

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

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

// hermesAddChannelCmd represents the hermesAddChannel command
var hermesAddChannelCmd = &cobra.Command{
Use: "hermes-add-channel id1 id2",
Use: "hermes-add-channel [chain_id] [chain_id]",
Args: cobra.ExactArgs(2),
Short: "It uses the hermes client to open an IBC channel between two chains",
Long: `This command requires that Hermes was already built and at least one node for each chain running.`,
Run: func(cmd *cobra.Command, args []string) {
queries := sql.InitDBFromCmd(cmd)

node1 := args[0]
node2 := args[1]
fmt.Println("Getting first node data...")
firstNode := evmos.GetNodeFromDB(queries, node1)
fmt.Println("Getting second node data...")
secondNode := evmos.GetNodeFromDB(queries, node2)
// TODO: make sure that the nodes are running checking for the PID
if firstNode.Node.IsRunning != 1 {
fmt.Println("first node is not running")
chainOne := args[0]
chainOneID, err := strconv.Atoi(chainOne)
if err != nil {
fmt.Println("invalid chain id")
os.Exit(1)

}
if secondNode.Node.IsRunning != 1 {
fmt.Println("second node is not running")
chainTwo := args[1]
chainTwoID, err := strconv.Atoi(chainTwo)
if err != nil {
fmt.Println("invalid chain id")
os.Exit(1)
}
chains := make([]database.GetAllChainNodesRow, 2)
nodesChainOne, err := queries.GetAllChainNodes(context.Background(), int64(chainOneID))
if err != nil {
fmt.Println("could not find nodes for chain:", chainOne)
os.Exit(1)
}
chains[0] = nodesChainOne[0]

nodesChainTwo, err := queries.GetAllChainNodes(context.Background(), int64(chainTwoID))
if err != nil {
fmt.Println("could not find nodes for chain:", chainTwo)
os.Exit(1)
}
fmt.Println("Both chains are running")
chains[1] = nodesChainTwo[0]

h := hermes.NewHermes()
fmt.Println("Relayer initialized")

if err := h.AddEvmosChain(
firstNode.Chain.ChainID,
firstNode.Ports.P26657,
firstNode.Ports.P9090,
firstNode.Node.ValidatorKeyName,
firstNode.Node.ValidatorKey,
); err != nil {
fmt.Println("error adding first chain to the relayer:", err.Error())
os.Exit(1)
}
fmt.Println("First chain added")
for _, v := range chains {
if v.IsRunning != 1 {
fmt.Println("the node is not running, chain id:", v.ChainID)
}

if err := h.AddEvmosChain(
secondNode.Chain.ChainID,
secondNode.Ports.P26657,
secondNode.Ports.P9090,
secondNode.Node.ValidatorKeyName,
secondNode.Node.ValidatorKey,
); err != nil {
fmt.Println("error adding second chain to the relayer:", err.Error())
os.Exit(1)
}
switch {
case strings.Contains(v.BinaryVersion, "gaia"):
fmt.Println("Adding gaia chain")
if err := h.AddCosmosChain(
v.ChainID_2,
v.P26657,
v.P9090,
v.ValidatorKeyName,
v.ValidatorKey,
v.Prefix,
v.Denom,
); err != nil {
fmt.Println("error adding first chain to the relayer:", err.Error())
os.Exit(1)
}
case strings.Contains(v.BinaryVersion, "evmos"):
fmt.Println("Adding evmos chain")
if err := h.AddEvmosChain(
v.ChainID_2,
v.P26657,
v.P9090,
v.ValidatorKeyName,
v.ValidatorKey,
); err != nil {
fmt.Println("error adding first chain to the relayer:", err.Error())
os.Exit(1)
}
default:
fmt.Println("incorrect binary name")
os.Exit(1)
}

fmt.Println("Second chain added")
}

fmt.Println("Calling create channel")
err := h.CreateChannel(firstNode.Chain.ChainID, secondNode.Chain.ChainID)
err = h.CreateChannel(chains[0].ChainID_2, chains[1].ChainID_2)
if err != nil {
fmt.Println("error creating channel", err.Error())
os.Exit(1)
Expand Down
150 changes: 150 additions & 0 deletions cmd/playground/initGaia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package playground

import (
"fmt"
"os"
"path/filepath"
"strconv"

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

// initGaiaCmd represents the initGaia command
var initGaiaCmd = &cobra.Command{
Use: "init-gaia id",
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) {
queries := sql.InitDBFromCmd(cmd)
_ = queries

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.IsNodeHomeFolderInitialized(chainid, 0) {
fmt.Println("the home folder for this node was already created")
os.Exit(1)
}

chainID := "cosmoshub-99"

nodes := make([]*gaia.Gaia, 2)

var chainDB database.Chain

for k := range nodes {
path := filesmanager.GetNodeHomeFolder(chainid, int64(k))
g := gaia.NewGaia(fmt.Sprintf("moniker-%d-%d", chainid, k), path, chainID, "validator-key", "icsstake")
// Init the config files
if err := g.InitNode(); err != nil {
panic(err)
}
// Update general parameters in the genesis file
if err := g.UpdateGenesisFile(); err != nil {
panic(err)
}
if err := g.UpdateConfigFile(false); err != nil {
panic(err)
}
if err := g.UpdateAppFile(); err != nil {
panic(err)
}
if err := g.CreateGenTx(); err != nil {
panic(err)
}
// Assign random and unique ports
if err := g.AssignPorts(queries); err != nil {
panic(err)
}
// Update the Config Files
if err := g.UpdateConfigPorts(); err != nil {
panic(err)
}

nodes[k] = g
if k == 0 {
chainDB, err = g.SaveChainToDB(queries)
if err != nil {
panic(err)
}
}
_, err := g.SaveNodeToDB(chainDB, queries)
if err != nil {
panic(err)
}
}

// Join genesis transactions
for k, v := range nodes {
if k == 0 {
continue
}
files, err := filepath.Glob(v.HomeDir + "/config/gentx/*.json")
if err != nil {
panic("no files: " + err.Error())
}
if len(files) == 0 {
panic("no files 2: " + err.Error())
}

if err := filesmanager.CopyFile(
files[0],
nodes[0].HomeDir+"/config/gentx",
); err != nil {
panic(err)
}
addr, err := v.GetValidatorAddress()
if err != nil {
panic(err)
}
if err := nodes[0].AddGenesisAccount(addr); err != nil {
panic(err)
}
}

if err := nodes[0].CollectGenTxs(); err != nil {
panic(err)
}
if err := nodes[0].ValidateGenesis(); err != nil {
panic(err)
}

peers := []string{}
for k := range nodes {
peerInfo, err := nodes[k].GetPeerInfo()
if err != nil {
panic(err)
}
peers = append(peers, peerInfo)

if k == 0 {
continue
}
if err := filesmanager.CopyFile(
nodes[0].HomeDir+"/config/genesis.json",
nodes[k].HomeDir+"/config/genesis.json",
); err != nil {
panic(err)
}
}

for k := range nodes {
if err := nodes[k].AddPersistenPeers(peers); err != nil {
panic(err)
}
}
},
}

func init() {
PlaygroundCmd.AddCommand(initGaiaCmd)
initGaiaCmd.Flags().StringP("version", "v", "local", "Version of the Evmos node that you want to use, defaults to local. Tag names are supported.")
}
Loading

0 comments on commit 271f422

Please sign in to comment.