From a8a448a57ef607740699ef10ee65cad3f3e90b8a Mon Sep 17 00:00:00 2001 From: artemijspavlovs <19916123+artemijspavlovs@users.noreply.github.com> Date: Thu, 5 Sep 2024 07:42:20 +0400 Subject: [PATCH 1/5] chore: move migrations --- cmd/block-explorer/run/utils.go | 4 ++-- migrations/{ => block-explorer}/blockexplorer.sql | 0 migrations/{super-schema.sql => block-explorer/events.sql} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename migrations/{ => block-explorer}/blockexplorer.sql (100%) rename migrations/{super-schema.sql => block-explorer/events.sql} (100%) diff --git a/cmd/block-explorer/run/utils.go b/cmd/block-explorer/run/utils.go index 400e17a0..f8305d4c 100644 --- a/cmd/block-explorer/run/utils.go +++ b/cmd/block-explorer/run/utils.go @@ -170,7 +170,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 +181,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/migrations/blockexplorer.sql b/migrations/block-explorer/blockexplorer.sql similarity index 100% rename from migrations/blockexplorer.sql rename to migrations/block-explorer/blockexplorer.sql diff --git a/migrations/super-schema.sql b/migrations/block-explorer/events.sql similarity index 100% rename from migrations/super-schema.sql rename to migrations/block-explorer/events.sql From b9274bc0334b8526a95f81b1a6a38213589547a9 Mon Sep 17 00:00:00 2001 From: artemijspavlovs <19916123+artemijspavlovs@users.noreply.github.com> Date: Thu, 5 Sep 2024 08:02:33 +0400 Subject: [PATCH 2/5] chore: enable block explorer endpoint during run --- cmd/rollapp/run/run.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/rollapp/run/run.go b/cmd/rollapp/run/run.go index 23fe1ed8..efc0264f 100644 --- a/cmd/rollapp/run/run.go +++ b/cmd/rollapp/run/run.go @@ -662,7 +662,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 +694,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:") From e62bc26e7cbd751666654c86ac87f144189ef084 Mon Sep 17 00:00:00 2001 From: artemijspavlovs <19916123+artemijspavlovs@users.noreply.github.com> Date: Thu, 5 Sep 2024 09:34:25 +0400 Subject: [PATCH 3/5] feat: generate chain config during block-explorer initialization --- cmd/block-explorer/run/run.go | 21 +++++++++- cmd/block-explorer/run/utils.go | 26 ++++++++++--- cmd/rollapp/run/run.go | 7 ++-- migrations/block-explorer/events.sql | 0 .../{blockexplorer.sql => schema.sql} | 0 utils/blockexplorer/blockexplorer.go | 38 +++++++++++++++++++ 6 files changed, 81 insertions(+), 11 deletions(-) mode change 100644 => 100755 migrations/block-explorer/events.sql rename migrations/block-explorer/{blockexplorer.sql => schema.sql} (100%) mode change 100644 => 100755 create mode 100644 utils/blockexplorer/blockexplorer.go 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 f8305d4c..c488c25d 100644 --- a/cmd/block-explorer/run/utils.go +++ b/cmd/block-explorer/run/utils.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strings" "time" @@ -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,13 @@ func createBlockExplorerContainers() error { return err } + beChainConfigPath := filepath.Join( + home, + "block-explorer", + "config", + "chains.yaml", + ) + fmt.Println(beChainConfigPath) containers := map[string]dockerutils.ContainerConfigOptions{ "db": { Name: "be-postgresql", @@ -62,11 +70,17 @@ func createBlockExplorerContainers() error { 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{}, + Mounts: []mount.Mount{ + { + Type: mount.TypeBind, + Source: beChainConfigPath, + Target: "/root/.beid/chains.yaml", + }, + }, }, } diff --git a/cmd/rollapp/run/run.go b/cmd/rollapp/run/run.go index efc0264f..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 diff --git a/migrations/block-explorer/events.sql b/migrations/block-explorer/events.sql old mode 100644 new mode 100755 diff --git a/migrations/block-explorer/blockexplorer.sql b/migrations/block-explorer/schema.sql old mode 100644 new mode 100755 similarity index 100% rename from migrations/block-explorer/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 +} From 4757fdff79405b6041b7deae401bff6640501fcb Mon Sep 17 00:00:00 2001 From: artemijspavlovs <19916123+artemijspavlovs@users.noreply.github.com> Date: Thu, 5 Sep 2024 09:37:26 +0400 Subject: [PATCH 4/5] ci: update golangci-lint version --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 0cfd3f6d2a6da233957984bcfc462a5c2e5f3611 Mon Sep 17 00:00:00 2001 From: artemijspavlovs <19916123+artemijspavlovs@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:15:34 +0400 Subject: [PATCH 5/5] chore: improve be compatibility with different platforms --- cmd/block-explorer/run/utils.go | 19 ++++++++++++++----- utils/docker/docker.go | 14 ++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/cmd/block-explorer/run/utils.go b/cmd/block-explorer/run/utils.go index c488c25d..d9942f87 100644 --- a/cmd/block-explorer/run/utils.go +++ b/cmd/block-explorer/run/utils.go @@ -7,10 +7,10 @@ import ( "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" @@ -35,6 +35,12 @@ func createBlockExplorerContainers(home string) 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", @@ -65,7 +71,8 @@ func createBlockExplorerContainers(home string) 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{}, }, @@ -73,7 +80,9 @@ func createBlockExplorerContainers(home string) error { Name: "be-indexer", Image: "public.ecr.aws/a3d4b9r3/block-explorer-indexer:latest", Port: "8080", - Envs: []string{}, + Envs: []string{ + fmt.Sprintf("HOST_ADDRESS=%s", hostAddress), + }, Mounts: []mount.Mount{ { Type: mount.TypeBind, @@ -123,7 +132,7 @@ func createBlockExplorerContainers(home string) 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) } @@ -138,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", }, ) 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) - }