diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index db90de21..99958168 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 diff --git a/cmd/block-explorer/run/run.go b/cmd/block-explorer/run/run.go index 2ba44c5f..ba4913c0 100644 --- a/cmd/block-explorer/run/run.go +++ b/cmd/block-explorer/run/run.go @@ -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" ) @@ -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 diff --git a/cmd/block-explorer/run/utils.go b/cmd/block-explorer/run/utils.go index 400e17a0..d9942f87 100644 --- a/cmd/block-explorer/run/utils.go +++ b/cmd/block-explorer/run/utils.go @@ -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" @@ -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 { @@ -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", @@ -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", + }, + }, }, } @@ -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) } @@ -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", }, ) @@ -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) } @@ -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) } diff --git a/cmd/rollapp/run/run.go b/cmd/rollapp/run/run.go index 23fe1ed8..5144485f 100644 --- a/cmd/rollapp/run/run.go +++ b/cmd/rollapp/run/run.go @@ -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" @@ -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 @@ -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: @@ -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:") diff --git a/migrations/super-schema.sql b/migrations/block-explorer/events.sql old mode 100644 new mode 100755 similarity index 100% rename from migrations/super-schema.sql rename to migrations/block-explorer/events.sql diff --git a/migrations/blockexplorer.sql b/migrations/block-explorer/schema.sql old mode 100644 new mode 100755 similarity index 100% rename from migrations/blockexplorer.sql rename to migrations/block-explorer/schema.sql diff --git a/utils/blockexplorer/blockexplorer.go b/utils/blockexplorer/blockexplorer.go new file mode 100644 index 00000000..df0b268f --- /dev/null +++ b/utils/blockexplorer/blockexplorer.go @@ -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 +} diff --git a/utils/docker/docker.go b/utils/docker/docker.go index 1c99fbbd..d64dc1f1 100644 --- a/utils/docker/docker.go +++ b/utils/docker/docker.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "log" + "runtime" "strings" "time" @@ -15,7 +16,6 @@ import ( "github.com/docker/go-connections/nat" ) - type ContainerConfigOptions struct { Name string Image string @@ -46,7 +46,6 @@ func CreateContainer( nat.Port(portString): struct{}{}, }, Env: cfg.Envs, - } hostConfig := &container.HostConfig{ @@ -54,6 +53,10 @@ func CreateContainer( 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) @@ -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) } @@ -112,5 +119,4 @@ func CreateContainer( } return fmt.Errorf("failed to start container after %d attempts", maxRetries) - }