|
| 1 | +package docker_e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "cosmossdk.io/math" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "github.com/celestiaorg/go-square/v2/share" |
| 9 | + tastoradocker "github.com/celestiaorg/tastora/framework/docker" |
| 10 | + "github.com/celestiaorg/tastora/framework/testutil/sdkacc" |
| 11 | + "github.com/celestiaorg/tastora/framework/testutil/toml" |
| 12 | + tastoratypes "github.com/celestiaorg/tastora/framework/types" |
| 13 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 14 | + "github.com/cosmos/cosmos-sdk/types/module/testutil" |
| 15 | + "github.com/cosmos/cosmos-sdk/x/auth" |
| 16 | + "github.com/cosmos/cosmos-sdk/x/bank" |
| 17 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 18 | + "github.com/stretchr/testify/suite" |
| 19 | + "go.uber.org/zap/zaptest" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + // testChainID is the chain ID used for testing. |
| 27 | + // it must be the string "test" as it is handled explicitly in app/node. |
| 28 | + testChainID = "test" |
| 29 | +) |
| 30 | + |
| 31 | +func init() { |
| 32 | + sdkConf := sdk.GetConfig() |
| 33 | + sdkConf.SetBech32PrefixForAccount("celestia", "celestiapub") |
| 34 | + sdkConf.Seal() |
| 35 | +} |
| 36 | + |
| 37 | +func TestDockerSuite(t *testing.T) { |
| 38 | + if testing.Short() { |
| 39 | + t.Skip("skipping due to short mode") |
| 40 | + } |
| 41 | + suite.Run(t, new(DockerTestSuite)) |
| 42 | +} |
| 43 | + |
| 44 | +type DockerTestSuite struct { |
| 45 | + suite.Suite |
| 46 | + provider tastoratypes.Provider |
| 47 | + celestia tastoratypes.Chain |
| 48 | + daNetwork tastoratypes.DataAvailabilityNetwork |
| 49 | + rollkitChain tastoratypes.RollkitChain |
| 50 | +} |
| 51 | + |
| 52 | +// ConfigOption is a function type for modifying tastoradocker.Config |
| 53 | +type ConfigOption func(*tastoradocker.Config) |
| 54 | + |
| 55 | +// CreateDockerProvider creates a new tastoratypes.Provider with optional configuration modifications |
| 56 | +func (s *DockerTestSuite) CreateDockerProvider(opts ...ConfigOption) tastoratypes.Provider { |
| 57 | + t := s.T() |
| 58 | + encConfig := testutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, bank.AppModuleBasic{}) |
| 59 | + numValidators := 1 |
| 60 | + numFullNodes := 0 |
| 61 | + client, network := tastoradocker.DockerSetup(t) |
| 62 | + |
| 63 | + cfg := tastoradocker.Config{ |
| 64 | + Logger: zaptest.NewLogger(t), |
| 65 | + DockerClient: client, |
| 66 | + DockerNetworkID: network, |
| 67 | + ChainConfig: &tastoradocker.ChainConfig{ |
| 68 | + ConfigFileOverrides: map[string]any{ |
| 69 | + "config/app.toml": appOverrides(), |
| 70 | + "config/config.toml": configOverrides(), |
| 71 | + }, |
| 72 | + Type: "celestia", |
| 73 | + Name: "celestia", |
| 74 | + Version: "v4.0.0-rc6", |
| 75 | + NumValidators: &numValidators, |
| 76 | + NumFullNodes: &numFullNodes, |
| 77 | + ChainID: testChainID, |
| 78 | + Images: []tastoradocker.DockerImage{ |
| 79 | + { |
| 80 | + Repository: "ghcr.io/celestiaorg/celestia-app", |
| 81 | + Version: "v4.0.0-rc6", |
| 82 | + UIDGID: "10001:10001", |
| 83 | + }, |
| 84 | + }, |
| 85 | + Bin: "celestia-appd", |
| 86 | + Bech32Prefix: "celestia", |
| 87 | + Denom: "utia", |
| 88 | + CoinType: "118", |
| 89 | + GasPrices: "0.025utia", |
| 90 | + GasAdjustment: 1.3, |
| 91 | + EncodingConfig: &encConfig, |
| 92 | + AdditionalStartArgs: []string{ |
| 93 | + "--force-no-bbr", |
| 94 | + "--grpc.enable", |
| 95 | + "--grpc.address", |
| 96 | + "0.0.0.0:9090", |
| 97 | + "--rpc.grpc_laddr=tcp://0.0.0.0:9098", |
| 98 | + "--timeout-commit", "1s", |
| 99 | + }, |
| 100 | + }, |
| 101 | + DataAvailabilityNetworkConfig: &tastoradocker.DataAvailabilityNetworkConfig{ |
| 102 | + BridgeNodeCount: 1, |
| 103 | + Image: tastoradocker.DockerImage{ |
| 104 | + Repository: "ghcr.io/celestiaorg/celestia-node", |
| 105 | + Version: "pr-4283", |
| 106 | + UIDGID: "10001:10001", |
| 107 | + }, |
| 108 | + }, |
| 109 | + RollkitChainConfig: &tastoradocker.RollkitChainConfig{ |
| 110 | + ChainID: "rollkit-test", |
| 111 | + Bin: "testapp", |
| 112 | + AggregatorPassphrase: "12345678", |
| 113 | + NumNodes: 1, |
| 114 | + Image: getRollkitImage(), |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, opt := range opts { |
| 119 | + opt(&cfg) |
| 120 | + } |
| 121 | + |
| 122 | + return tastoradocker.NewProvider(cfg, t) |
| 123 | +} |
| 124 | + |
| 125 | +// getGenesisHash returns the genesis hash of the given chain node. |
| 126 | +func (s *DockerTestSuite) getGenesisHash(ctx context.Context) string { |
| 127 | + node := s.celestia.GetNodes()[0] |
| 128 | + c, err := node.GetRPCClient() |
| 129 | + s.Require().NoError(err, "failed to get node client") |
| 130 | + |
| 131 | + first := int64(1) |
| 132 | + block, err := c.Block(ctx, &first) |
| 133 | + s.Require().NoError(err, "failed to get block") |
| 134 | + |
| 135 | + genesisHash := block.Block.Header.Hash().String() |
| 136 | + s.Require().NotEmpty(genesisHash, "genesis hash is empty") |
| 137 | + return genesisHash |
| 138 | +} |
| 139 | + |
| 140 | +// SetupDockerResources creates a new provider and chain using the given configuration options. |
| 141 | +// none of the resources are started. |
| 142 | +func (s *DockerTestSuite) SetupDockerResources(opts ...ConfigOption) { |
| 143 | + s.provider = s.CreateDockerProvider(opts...) |
| 144 | + s.celestia = s.CreateChain() |
| 145 | + s.daNetwork = s.CreateDANetwork() |
| 146 | + s.rollkitChain = s.CreateRollkitChain() |
| 147 | +} |
| 148 | + |
| 149 | +// CreateChain creates a chain using the provider. |
| 150 | +func (s *DockerTestSuite) CreateChain() tastoratypes.Chain { |
| 151 | + ctx := context.Background() |
| 152 | + |
| 153 | + chain, err := s.provider.GetChain(ctx) |
| 154 | + s.Require().NoError(err) |
| 155 | + |
| 156 | + return chain |
| 157 | +} |
| 158 | + |
| 159 | +// CreateDANetwork creates a DA network using the provider |
| 160 | +func (s *DockerTestSuite) CreateDANetwork() tastoratypes.DataAvailabilityNetwork { |
| 161 | + ctx := context.Background() |
| 162 | + |
| 163 | + daNetwork, err := s.provider.GetDataAvailabilityNetwork(ctx) |
| 164 | + s.Require().NoError(err) |
| 165 | + |
| 166 | + return daNetwork |
| 167 | +} |
| 168 | + |
| 169 | +// CreateRollkitChain creates a Rollkit chain using the provider |
| 170 | +func (s *DockerTestSuite) CreateRollkitChain() tastoratypes.RollkitChain { |
| 171 | + ctx := context.Background() |
| 172 | + |
| 173 | + rollkitChain, err := s.provider.GetRollkitChain(ctx) |
| 174 | + s.Require().NoError(err) |
| 175 | + |
| 176 | + return rollkitChain |
| 177 | +} |
| 178 | + |
| 179 | +// StartBridgeNode initializes and starts a bridge node within the data availability network using the given parameters. |
| 180 | +func (s *DockerTestSuite) StartBridgeNode(ctx context.Context, bridgeNode tastoratypes.DANode, chainID string, genesisHash string, celestiaNodeHostname string) { |
| 181 | + s.Require().Equal(tastoratypes.BridgeNode, bridgeNode.GetType()) |
| 182 | + err := bridgeNode.Start(ctx, |
| 183 | + tastoratypes.WithChainID(chainID), |
| 184 | + tastoratypes.WithAdditionalStartArguments("--p2p.network", chainID, "--core.ip", celestiaNodeHostname, "--rpc.addr", "0.0.0.0"), |
| 185 | + tastoratypes.WithEnvironmentVariables( |
| 186 | + map[string]string{ |
| 187 | + "CELESTIA_CUSTOM": tastoratypes.BuildCelestiaCustomEnvVar(chainID, genesisHash, ""), |
| 188 | + "P2P_NETWORK": chainID, |
| 189 | + }, |
| 190 | + ), |
| 191 | + ) |
| 192 | + s.Require().NoError(err) |
| 193 | +} |
| 194 | + |
| 195 | +// FundWallet transfers the specified amount of utia from the faucet wallet to the target wallet. |
| 196 | +func (s *DockerTestSuite) FundWallet(ctx context.Context, wallet tastoratypes.Wallet, amount int64) { |
| 197 | + fromAddress, err := sdkacc.AddressFromWallet(s.celestia.GetFaucetWallet()) |
| 198 | + s.Require().NoError(err) |
| 199 | + |
| 200 | + toAddress, err := sdk.AccAddressFromBech32(wallet.GetFormattedAddress()) |
| 201 | + s.Require().NoError(err) |
| 202 | + |
| 203 | + bankSend := banktypes.NewMsgSend(fromAddress, toAddress, sdk.NewCoins(sdk.NewCoin("utia", math.NewInt(amount)))) |
| 204 | + _, err = s.celestia.BroadcastMessages(ctx, s.celestia.GetFaucetWallet(), bankSend) |
| 205 | + s.Require().NoError(err) |
| 206 | +} |
| 207 | + |
| 208 | +// StartRollkitNode initializes and starts a Rollkit node. |
| 209 | +func (s *DockerTestSuite) StartRollkitNode(ctx context.Context, bridgeNode tastoratypes.DANode, rollkitNode tastoratypes.RollkitNode) { |
| 210 | + err := rollkitNode.Init(ctx) |
| 211 | + s.Require().NoError(err) |
| 212 | + |
| 213 | + bridgeNodeHostName, err := bridgeNode.GetInternalHostName() |
| 214 | + s.Require().NoError(err) |
| 215 | + |
| 216 | + authToken, err := bridgeNode.GetAuthToken() |
| 217 | + s.Require().NoError(err) |
| 218 | + |
| 219 | + daAddress := fmt.Sprintf("http://%s:26658", bridgeNodeHostName) |
| 220 | + err = rollkitNode.Start(ctx, |
| 221 | + "--rollkit.da.address", daAddress, |
| 222 | + "--rollkit.da.gas_price", "0.025", |
| 223 | + "--rollkit.da.auth_token", authToken, |
| 224 | + "--rollkit.rpc.address", "0.0.0.0:7331", // bind to 0.0.0.0 so rpc is reachable from test host. |
| 225 | + "--rollkit.da.namespace", generateValidNamespaceHex(), |
| 226 | + ) |
| 227 | + s.Require().NoError(err) |
| 228 | +} |
| 229 | + |
| 230 | +// getRollkitImage returns the Docker image configuration for Rollkit |
| 231 | +// Uses ROLLKIT_IMAGE_REPO and ROLLKIT_IMAGE_TAG environment variables if set |
| 232 | +func getRollkitImage() tastoradocker.DockerImage { |
| 233 | + repo := strings.TrimSpace(os.Getenv("ROLLKIT_IMAGE_REPO")) |
| 234 | + if repo == "" { |
| 235 | + repo = "ghcr.io/rollkit/rollkit" |
| 236 | + } |
| 237 | + |
| 238 | + tag := strings.TrimSpace(os.Getenv("ROLLKIT_IMAGE_TAG")) |
| 239 | + if tag == "" { |
| 240 | + tag = "latest" |
| 241 | + } |
| 242 | + |
| 243 | + return tastoradocker.DockerImage{ |
| 244 | + Repository: repo, |
| 245 | + Version: tag, |
| 246 | + UIDGID: "10001:10001", |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +func generateValidNamespaceHex() string { |
| 251 | + return hex.EncodeToString(share.RandomBlobNamespaceID()) |
| 252 | +} |
| 253 | + |
| 254 | +// appOverrides enables indexing of transactions so Broadcasting of transactions works |
| 255 | +func appOverrides() toml.Toml { |
| 256 | + return toml.Toml{ |
| 257 | + "tx-index": toml.Toml{ |
| 258 | + "indexer": "kv", |
| 259 | + }, |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +// configOverrides enables indexing of transactions so Broadcasting of transactions works |
| 264 | +func configOverrides() toml.Toml { |
| 265 | + return toml.Toml{ |
| 266 | + "tx_index": toml.Toml{ |
| 267 | + "indexer": "kv", |
| 268 | + }, |
| 269 | + } |
| 270 | +} |
0 commit comments