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: generate chain config during block-explorer initialization #884

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.59.1
version: v1.60.1
only-new-issues: true
21 changes: 20 additions & 1 deletion cmd/block-explorer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package run

import (
"fmt"
"path/filepath"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/utils/blockexplorer"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
Expand All @@ -14,9 +18,24 @@ func Cmd() *cobra.Command {
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()

rollerData, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
pterm.Error.Println("failed to load roller config file", err)
return
}

fmt.Println("Run the block explorer")

err := createBlockExplorerContainers()
beChainConfigPath := filepath.Join(home, "block-explorer", "config", "chains.yaml")
beChainConfig := blockexplorer.GenerateChainsYAML(rollerData.RollappID)
err = blockexplorer.WriteChainsYAML(beChainConfigPath, beChainConfig)
if err != nil {
pterm.Error.Println("failed to generate block explorer config", err)
}

err = createBlockExplorerContainers(home)
if err != nil {
pterm.Error.Println("failed to create the necessary containers: ", err)
return
Expand Down
47 changes: 35 additions & 12 deletions cmd/block-explorer/run/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
Expand All @@ -20,7 +21,7 @@ import (
dockerutils "github.com/dymensionxyz/roller/utils/docker"
)

func createBlockExplorerContainers() error {
func createBlockExplorerContainers(home string) error {
pterm.Info.Println("Creating container for block explorer")
cc, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
Expand All @@ -34,6 +35,19 @@ func createBlockExplorerContainers() error {
return err
}

// Determine the host address to use
hostAddress := "host.docker.internal"
if runtime.GOOS == "linux" {
hostAddress = "172.17.0.1" // Default Docker bridge network gateway
}

beChainConfigPath := filepath.Join(
home,
"block-explorer",
"config",
"chains.yaml",
)
fmt.Println(beChainConfigPath)
containers := map[string]dockerutils.ContainerConfigOptions{
"db": {
Name: "be-postgresql",
Expand All @@ -57,16 +71,25 @@ func createBlockExplorerContainers() error {
Image: "public.ecr.aws/a3d4b9r3/block-explorer-frontend:latest",
Port: "3000",
Envs: []string{
"DATABASE_URL=postgresql://be:psw@be-postgresql:5432/blockexplorer",
fmt.Sprintf("DATABASE_URL=postgresql://be:psw@%s:5432/blockexplorer", hostAddress),
fmt.Sprintf("HOST_ADDRESS=%s", hostAddress),
},
Mounts: []mount.Mount{},
},
"indexer": {
Name: "be-indexer",
Image: "public.ecr.aws/a3d4b9r3/block-explorer-indexer:latest",
Port: "8080",
Envs: []string{},
Mounts: []mount.Mount{},
Name: "be-indexer",
Image: "public.ecr.aws/a3d4b9r3/block-explorer-indexer:latest",
Port: "8080",
Envs: []string{
fmt.Sprintf("HOST_ADDRESS=%s", hostAddress),
},
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: beChainConfigPath,
Target: "/root/.beid/chains.yaml",
},
},
},
}

Expand Down Expand Up @@ -109,7 +132,7 @@ func createBlockExplorerContainers() error {

func ensureNetworkExists(cli *client.Client, networkName string) error {
// List all networks
networks, err := cli.NetworkList(context.Background(), types.NetworkListOptions{})
networks, err := cli.NetworkList(context.Background(), network.ListOptions{})
if err != nil {
return fmt.Errorf("failed to list networks: %w", err)
}
Expand All @@ -124,7 +147,7 @@ func ensureNetworkExists(cli *client.Client, networkName string) error {

// Create the network if it does not exist
_, err = cli.NetworkCreate(
context.Background(), networkName, types.NetworkCreate{
context.Background(), networkName, network.CreateOptions{
Driver: "bridge",
},
)
Expand Down Expand Up @@ -170,7 +193,7 @@ func runSQLMigration() error {
defer dbLocal.Close()

// Read and execute the SQL migration file
sqlFile, err := os.ReadFile("migrations/blockexplorer.sql")
sqlFile, err := os.ReadFile("migrations/block-explorer/schema.sql")
if err != nil {
return fmt.Errorf("failed to read SQL file: %w", err)
}
Expand All @@ -181,7 +204,7 @@ func runSQLMigration() error {
}

// Execute additional SQL files if needed
superSchemaFile, err := os.ReadFile("migrations/super-schema.sql")
superSchemaFile, err := os.ReadFile("migrations/block-explorer/events.sql")
if err != nil {
return fmt.Errorf("failed to read super-schema SQL file: %w", err)
}
Expand Down
16 changes: 11 additions & 5 deletions cmd/rollapp/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
cosmossdktypes "github.com/cosmos/cosmos-sdk/types"
github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types"
dymensionseqtypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
initrollapp "github.com/dymensionxyz/roller/cmd/rollapp/init"
Expand All @@ -35,6 +31,9 @@ import (
"github.com/dymensionxyz/roller/utils/errorhandling"
"github.com/dymensionxyz/roller/utils/rollapp"
sequencerutils "github.com/dymensionxyz/roller/utils/sequencer"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

// TODO: Test sequencing on 35-C and update the price
Expand Down Expand Up @@ -662,7 +661,7 @@ func Cmd() *cobra.Command {
"true",
)
if err != nil {
pterm.Error.Println("failed to update `p2p_advertising_enabled`")
pterm.Error.Println("failed to update `p2p_advertising_enabled` field")
return
}
default:
Expand Down Expand Up @@ -694,6 +693,13 @@ func Cmd() *cobra.Command {
daConfig,
)

pterm.Info.Println("enabling block explorer endpoint")
_ = globalutils.UpdateFieldInToml(
filepath.Join(home, consts.ConfigDirName.Rollapp, "config", "be-json-rpc.toml"),
"enable",
"true",
)

pterm.Info.Println("initialization complete")

pterm.Info.Println("next steps:")
Expand Down
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions utils/blockexplorer/blockexplorer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package blockexplorer

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

// GenerateChainsYAML generates the YAML content with the given chain_id
// this configuration is used by the block-explorer to index the locally running
// chain
func GenerateChainsYAML(chainID string) string {
template := `local:
chain_id: %s
be_json_rpc_urls: [ "http://host.docker.internal:11100" ]
# disable: true
`
return fmt.Sprintf(template, chainID)
}

func WriteChainsYAML(filePath, content string) error {
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o700)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()

if _, err := file.WriteString(content); err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
}
14 changes: 10 additions & 4 deletions utils/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"runtime"
"strings"
"time"

Expand All @@ -15,7 +16,6 @@ import (
"github.com/docker/go-connections/nat"
)


type ContainerConfigOptions struct {
Name string
Image string
Expand Down Expand Up @@ -46,14 +46,17 @@ func CreateContainer(
nat.Port(portString): struct{}{},
},
Env: cfg.Envs,

}

hostConfig := &container.HostConfig{
PortBindings: portBindings,
Mounts: cfg.Mounts,
}

if runtime.GOOS == "linux" {
hostConfig.ExtraHosts = []string{"host.docker.internal:host-gateway"}
}

containers, err := cli.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
return fmt.Errorf("error listing containers: %w", err)
Expand Down Expand Up @@ -98,7 +101,11 @@ func CreateContainer(
continue
}

logs, err := cli.ContainerLogs(ctx, resp.ID, container.LogsOptions{ShowStdout: true, ShowStderr: true})
logs, err := cli.ContainerLogs(
ctx,
resp.ID,
container.LogsOptions{ShowStdout: true, ShowStderr: true},
)
if err != nil {
return fmt.Errorf("error retrieving logs for container %s: %w", cfg.Name, err)
}
Expand All @@ -112,5 +119,4 @@ func CreateContainer(
}

return fmt.Errorf("failed to start container after %d attempts", maxRetries)

}
Loading