Skip to content
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
213 changes: 213 additions & 0 deletions .github/workflows/e2e-local-nightly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
name: Local E2E Nightly (Android)

on:
schedule:
- cron: "35 5 * * *"
workflow_dispatch:
inputs:
e2e_branch:
description: "Ref in synonymdev/bitkit-e2e-tests"
type: string
default: main
post_to_slack:
description: "Post Local E2E Nightly summary to Slack (#bitkit-staging-nightly)"
type: boolean
default: false

permissions:
contents: read
packages: read

env:
TERM: xterm-256color
FORCE_COLOR: 1

concurrency:
group: bitkit-local-e2e-nightly-${{ github.ref }}
cancel-in-progress: false

jobs:
e2e-spark:
runs-on: ubuntu-latest
timeout-minutes: 120
outputs:
e2e-sha: ${{ steps.e2e-source.outputs.commit }}
env:
COMPOSE_PROJECT_NAME: bitkit-spark-e2e
steps:
- name: Reserve regtest host ports
run: sudo sysctl -w net.ipv4.ip_local_reserved_ports=39388,43782,60001

- uses: actions/checkout@v7
with:
path: bitkit-android
persist-credentials: false

- uses: actions/checkout@v7
id: e2e-source
with:
repository: synonymdev/bitkit-e2e-tests
ref: ${{ inputs.e2e_branch || 'main' }}
path: bitkit-e2e-tests
persist-credentials: false

- name: Check shared E2E branch for cache writes
id: cache-source
working-directory: bitkit-e2e-tests
run: |
if [[ "$(git symbolic-ref --quiet HEAD)" == refs/heads/main ]]; then
echo 'save-cache=true' >> "$GITHUB_OUTPUT"
fi

- uses: actions/setup-java@v6
with:
java-version: "17"
distribution: temurin

- uses: gradle/actions/setup-gradle@v6

- uses: actions/setup-node@v7
with:
node-version: 22
cache: npm
cache-dependency-path: bitkit-e2e-tests/package-lock.json

- name: Install Appium dependencies
working-directory: bitkit-e2e-tests
run: npm ci

- name: Restore pinned Spark images
id: spark-cache
uses: actions/cache/restore@v6
continue-on-error: true
with:
path: ${{ runner.temp }}/bitkit-spark-images.tar
key: spark-images-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('bitkit-e2e-tests/BITKIT_DOCKER_REVISION') }}

- name: Start pinned Spark fixture
id: fixture
working-directory: bitkit-e2e-tests
env:
SPARK_IMAGE_CACHE: ${{ runner.temp }}/bitkit-spark-images.tar
run: |
./scripts/local-backend spark-up
./scripts/local-backend spark-env >> "$GITHUB_ENV"
if [[ -s "$SPARK_IMAGE_CACHE" ]]; then
echo 'cache-ready=true' >> "$GITHUB_OUTPUT"
fi

- name: Save pinned Spark images
if: >-
steps.cache-source.outputs.save-cache == 'true' && steps.fixture.outputs.cache-ready == 'true' &&
steps.spark-cache.outputs.cache-hit != 'true' && steps.spark-cache.outputs.cache-primary-key != ''
uses: actions/cache/save@v6
continue-on-error: true
with:
path: ${{ runner.temp }}/bitkit-spark-images.tar
key: ${{ steps.spark-cache.outputs.cache-primary-key }}

- name: Build Android Spark app
working-directory: bitkit-e2e-tests
env:
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./scripts/build-android-apk.sh

- name: Enable KVM
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm

- name: Run E2E tests (attempt 1)
continue-on-error: true
id: test1
uses: reactivecircus/android-emulator-runner@v2
with:
profile: pixel_6
api-level: 33
arch: x86_64
avd-name: Pixel_6
force-avd-creation: false
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-front none
script: cd bitkit-e2e-tests && ./scripts/run-spark-e2e.sh android
env:
RECORD_VIDEO: true
ATTEMPT: 1

- name: Run E2E tests (attempt 2)
continue-on-error: true
id: test2
if: steps.test1.outcome != 'success'
uses: reactivecircus/android-emulator-runner@v2
with:
profile: pixel_6
api-level: 33
arch: x86_64
avd-name: Pixel_6
force-avd-creation: false
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-front none
script: cd bitkit-e2e-tests && ./scripts/run-spark-e2e.sh android
env:
RECORD_VIDEO: true
ATTEMPT: 2

- name: Run E2E tests (attempt 3)
id: test3
if: steps.test1.outcome != 'success' && steps.test2.outcome != 'success'
uses: reactivecircus/android-emulator-runner@v2
with:
profile: pixel_6
api-level: 33
arch: x86_64
avd-name: Pixel_6
force-avd-creation: false
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-front none
script: cd bitkit-e2e-tests && ./scripts/run-spark-e2e.sh android
env:
RECORD_VIDEO: true
ATTEMPT: 3

- name: Collect Spark logs
if: failure() || steps.test1.outcome == 'failure' || steps.test2.outcome == 'failure'
working-directory: bitkit-e2e-tests
run: |
mkdir -p artifacts
Comment thread
piotr-iohk marked this conversation as resolved.
Resolved
./scripts/local-backend spark-logs --timestamps > artifacts/spark-docker.log 2>&1 || true

# Preserve logs and recordings from failed attempts even when a retry passes.
- uses: actions/upload-artifact@v7
if: always()
with:
name: spark-android-${{ github.run_number }}
path: bitkit-e2e-tests/artifacts/
retention-days: 7

slack-report:
if: >-
always() &&
(github.event_name == 'schedule' ||
(github.event_name == 'workflow_dispatch' && inputs.post_to_slack == true))
needs: e2e-spark
runs-on: ubuntu-24.04
timeout-minutes: 5
permissions:
contents: read
steps:
- name: Checkout shared Slack reporter
uses: actions/checkout@v7
with:
repository: synonymdev/bitkit-e2e-tests
ref: main
persist-credentials: false

- name: Post Slack summary
run: python3 scripts/slack_summary.py e2e-local-nightly
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_STAGING }}
PLATFORM: android
LOCAL_NIGHTLY_RESULT: ${{ needs.e2e-spark.result }}
E2E_TESTS_REF: ${{ needs.e2e-spark.outputs.e2e-sha || inputs.e2e_branch || 'main' }}
E2E_TESTS_RESULT: ${{ needs.e2e-spark.result }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
RUN_ATTEMPT: ${{ github.run_attempt }}
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ val bcp47Locales = listOf(
)
val e2eEnv = envFlag("E2E", default = false)
val e2eBackendEnv = providers.environmentVariable("E2E_BACKEND").orElse("local")
val sparkLocalConfigEnv = providers.environmentVariable("SPARK_LOCAL_CONFIG_B64").orElse("")
val sparkLocalChainUrlEnv = providers.environmentVariable("SPARK_LOCAL_CHAIN_URL").orElse("")
val e2eLocalHostEnv = providers.environmentVariable("E2E_LOCAL_HOST").orElse("10.0.2.2")
val e2eHomegateUrlEnv = providers.environmentVariable("E2E_HOMEGATE_URL")
.orElse(e2eLocalHostEnv.map { "http://$it:6288" })
Expand Down Expand Up @@ -417,6 +419,8 @@ androidComponents {
buildConfigFields.put("ESPLORA_URL", localProp("ESPLORA_URL").orElse("").stringField())
buildConfigFields.put("E2E", e2eEnv.booleanField())
buildConfigFields.put("E2E_BACKEND", e2eBackendEnv.stringField())
buildConfigFields.put("SPARK_LOCAL_CONFIG_B64", sparkLocalConfigEnv.stringField())
buildConfigFields.put("SPARK_LOCAL_CHAIN_URL", sparkLocalChainUrlEnv.stringField())
buildConfigFields.put("E2E_LOCAL_HOST", e2eLocalHostEnv.stringField())
buildConfigFields.put("E2E_HOMEGATE_URL", e2eHomegateUrlEnv.stringField())
buildConfigFields.put("E2E_HOMESERVER_PUBKY", e2eHomeserverPubkyEnv.stringField())
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/to/bitkit/data/SparkPayoutChain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ class SparkPayoutChain @Inject constructor(
) {
companion object {
private const val TAG = "SparkPayoutChain"
private const val SPARK_REGTEST_ESPLORA_URL = "https://regtest-mempool.us-west-2.sparkinfra.net/api"
private val json = Json { ignoreUnknownKeys = true }

/** The Esplora to ask on [network]: the one Spark reads there, or null where the app knows none. */
fun esploraUrl(network: Network): String? = when (network) {
fun esploraUrl(network: Network, regtestUrl: String? = Env.sparkRegtestEsploraUrl()): String? = when (network) {
Network.BITCOIN -> Env.bitkitEsploraUrl
Network.REGTEST -> SPARK_REGTEST_ESPLORA_URL
Network.REGTEST -> regtestUrl
else -> null
}
}
Expand Down
42 changes: 35 additions & 7 deletions app/src/main/java/to/bitkit/env/Env.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import java.io.File
import kotlin.io.path.Path
import com.synonym.bitkitcore.Network as BitkitCoreNetwork

internal data class LocalSparkSettings(val configBase64: String, val chainUrl: String) {
companion object {
fun fromInputs(configBase64: String, chainUrl: String): LocalSparkSettings? {
if (configBase64.isBlank() && chainUrl.isBlank()) return null
return LocalSparkSettings(configBase64, chainUrl.trimEnd('/'))
}
}
Comment thread
piotr-iohk marked this conversation as resolved.
Resolved

val isComplete: Boolean get() = configBase64.isNotBlank() && chainUrl.isNotBlank()
}

@Suppress("ConstPropertyName", "KotlinConstantConditions", "SimplifyBooleanWithConstants")
internal object Env {
val isUsdtEnabled: Boolean get() = (network == Network.BITCOIN || isDebug) &&
Expand Down Expand Up @@ -51,6 +62,11 @@ internal object Env {
const val e2eHomegateUrl = BuildConfig.E2E_HOMEGATE_URL
val e2eHomeserverPubky = BuildConfig.E2E_HOMESERVER_PUBKY.takeIf { isLocalE2eBackend && it.isNotBlank() }
val network = Network.valueOf(BuildConfig.NETWORK)
val localSparkSettings = if (isDebug && isLocalE2eBackend && network == Network.REGTEST) {
LocalSparkSettings.fromInputs(BuildConfig.SPARK_LOCAL_CONFIG_B64, BuildConfig.SPARK_LOCAL_CHAIN_URL)
} else {
null
}
val locales = BuildConfig.LOCALES.split(",")
const val walletSyncIntervalSecs = 10_uL
const val walletSyncTimeoutSecs = 10_uL
Expand Down Expand Up @@ -145,20 +161,32 @@ internal object Env {

/**
* Whether the node's chain is the one Spark watches, so an on-chain funding of Spark from Savings or a hardware
* wallet reaches it. On regtest Spark runs on Lightspark's own chain, which the node joins only through the
* Spark regtest Esplora proxy.
* wallet reaches it. Hosted regtest supports the node's Spark Esplora proxy. The local fixture config selects
* Spark's chain only; the node and hardware wallet can use independently configured Electrum servers.
*/
fun sparkSharesNodeChain(network: Network, regtestEsploraUrl: String?): Boolean =
network != Network.REGTEST || regtestEsploraUrl != null
fun sparkSharesNodeChain(network: Network, regtestEsploraUrl: String?, localSpark: LocalSparkSettings?): Boolean =
network != Network.REGTEST || (localSpark == null && regtestEsploraUrl != null)

val sparkSharesNodeChain: Boolean get() = sparkSharesNodeChain(network, regtestEsploraUrl)
val sparkSharesNodeChain: Boolean get() = sparkSharesNodeChain(network, regtestEsploraUrl, localSparkSettings)

/** Esplora REST that Spark reads the chain from; null keeps the SDK's default, on staging and regtest. */
fun sparkChainServiceUrl(network: Network): String? = when (network) {
/** REST endpoint for the Spark SDK. An unconfigured regtest uses the SDK's hosted default. */
fun sparkChainServiceUrl(
network: Network,
localSpark: LocalSparkSettings? = localSparkSettings,
): String? = when (network) {
Network.BITCOIN -> bitkitEsploraUrl
Network.REGTEST -> localSpark?.takeIf { it.isComplete }?.chainUrl
else -> null
}

/** Regtest endpoint for app-side payout reads and emergency-exit reads and broadcasts. */
fun sparkRegtestEsploraUrl(localSpark: LocalSparkSettings? = localSparkSettings): String? =
if (localSpark == null) {
"https://regtest-mempool.us-west-2.sparkinfra.net/api"
} else {
sparkChainServiceUrl(Network.REGTEST, localSpark)
}

val blocktankBaseUrl
get() = when (network) {
Network.BITCOIN -> "https://api1.blocktank.to"
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/to/bitkit/repositories/SparkExitChain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ class EsploraSparkExitChain(
* The chain the Spark SDK reads per network. Mainnet reads Bitkit's own esplora alone, which takes a package
* on `/txs/package` only, and broadcasts there first, then through the SDK's former default hosts.
*/
fun defaultHosts(sparkNetwork: String?): SparkExitHosts? = when (sparkNetwork) {
fun defaultHosts(
sparkNetwork: String?,
regtestUrl: String? = Env.sparkRegtestEsploraUrl(),
): SparkExitHosts? = when (sparkNetwork) {
"mainnet" -> SparkExitHosts(
reads = listOf(Env.bitkitEsploraUrl),
broadcasts = listOf(
Expand All @@ -180,7 +183,7 @@ class EsploraSparkExitChain(
SparkExitBroadcastHost("https://blockstream.info/api"),
),
)
"regtest" -> SparkExitHosts.of("https://regtest-mempool.us-west-2.sparkinfra.net/api")
"regtest" -> regtestUrl?.let { SparkExitHosts.of(it) }
else -> null
}

Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/to/bitkit/services/SparkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import breez_sdk_spark.SendPaymentOptions
import breez_sdk_spark.SendPaymentRequest
import breez_sdk_spark.SyncWalletRequest
import breez_sdk_spark.defaultConfig
import breez_sdk_spark.parseSparkConfig
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand All @@ -52,6 +53,7 @@ import org.lightningdevkit.ldknode.Bolt11Invoice
import org.lightningdevkit.ldknode.Network
import to.bitkit.data.keychain.Keychain
import to.bitkit.env.Env
import to.bitkit.env.LocalSparkSettings
import to.bitkit.ext.runSuspendCatching
import to.bitkit.models.safe
import to.bitkit.repositories.SparkExitRecovering
Expand All @@ -60,6 +62,7 @@ import to.bitkit.utils.Logger
import to.bitkit.utils.ServiceError
import java.io.File
import java.math.BigInteger
import java.util.Base64
import javax.inject.Inject
import javax.inject.Singleton
import breez_sdk_spark.Network as SparkNetwork
Expand Down Expand Up @@ -97,6 +100,16 @@ class SparkService @Inject constructor(
leafOptimizationConfig = leafOptimizationConfig.copy(autoEnabled = !isExiting)
}

/** Applies the pinned operator settings and selects the fixture's mempool.space REST API. */
internal fun configureLocalSpark(config: Config, settings: LocalSparkSettings?): ChainApiType {
if (settings == null) return ChainApiType.ESPLORA
require(settings.isComplete) { "Local Spark requires SPARK_LOCAL_CONFIG_B64 and SPARK_LOCAL_CHAIN_URL" }
config.sparkConfig = parseSparkConfig(Base64.getDecoder().decode(settings.configBase64).decodeToString())
// The pinned regtest operators require anchored refunds.
config.watchtowerFeeLadderEnabled = false
return ChainApiType.MEMPOOL_SPACE
}

/** Leeway in sat/vB over the recommended fee that an automatic deposit claim may pay. */
private const val DEPOSIT_CLAIM_FEE_LEEWAY_SAT_PER_VB = 2uL

Expand Down Expand Up @@ -179,10 +192,13 @@ class SparkService @Inject constructor(
val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name)
val config = configure(defaultConfig(network), Env.breezApiKey, isExiting)
val storageDir = File(Env.sparkStoragePath(walletIndex)).apply { mkdirs() }.path
val apiType = configureLocalSpark(config, Env.localSparkSettings)
val chainServiceUrl = Env.sparkChainServiceUrl(Env.network)
val connected = SdkBuilder(config, Seed.Mnemonic(mnemonic, passphrase)).use { builder ->
builder.withDefaultStorage(storageDir)
chainServiceUrl?.let { builder.withRestChainService(it, ChainApiType.ESPLORA, null) }
chainServiceUrl?.let {
builder.withRestChainService(it, apiType, null)
}
builder.build()
}
listenerId = connected.addEventListener(
Expand Down
Loading