diff --git a/.github/workflows/snakt.yaml b/.github/workflows/snakt.yaml new file mode 100644 index 00000000..a1efb01b --- /dev/null +++ b/.github/workflows/snakt.yaml @@ -0,0 +1,40 @@ +name: SnaKt + +on: + pull_request: + paths: + - 'src/main/kotlin/cash/atto/node/bootstrap/discovery/**' + - 'verification/snakt/**' + - '.github/workflows/snakt.yaml' + workflow_dispatch: + +permissions: + contents: read + +jobs: + verify: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/checkout@v7 + with: + repository: JetBrains/SnaKt + ref: e8d86408298192f121ba835a4f5d87ecf388356d + path: build/snakt/SnaKt + + - uses: actions/setup-java@v6 + with: + distribution: temurin + java-version: '21' + - uses: gradle/actions/setup-gradle@v6 + - uses: cda-tum/setup-z3@0401375ea1e34d77ef35bbeb634f6203f3bcde60 # v1 + id: z3 + with: + version: '4.8.7' + + - name: Build SnaKt and verify Kotlin contracts + env: + Z3_EXE: ${{ steps.z3.outputs.z3-root }}/bin/z3 + run: | + build/snakt/SnaKt/gradlew -Dmaven.repo.local="$PWD/build/snakt/maven" -p build/snakt/SnaKt publishToMavenLocal + build/snakt/SnaKt/gradlew -Dmaven.repo.local="$PWD/build/snakt/maven" -p verification/snakt compileKotlin --rerun-tasks diff --git a/README.md b/README.md index 9b0359b7..dee524c6 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,8 @@ stores it as an **unchecked transaction**: The overall effect: nodes can reconnect, observe the network’s latest heads, request missing ranges, and replay into their ledger state. +The discovery queue’s capacity calculation has a [SnaKt proof](verification/snakt/README.md). + ## Code map For implementation work, use these repository guides: diff --git a/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacity.kt b/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacity.kt new file mode 100644 index 00000000..817ea10a --- /dev/null +++ b/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacity.kt @@ -0,0 +1,8 @@ +package cash.atto.node.bootstrap.discovery + +// The SnaKt proof in verification/snakt uses the inlined production body. +@Suppress("NOTHING_TO_INLINE") +internal inline fun discoveryChannelCapacity( + capacity: Int, + headroom: Int, +): Int = capacity + headroom diff --git a/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryProperties.kt b/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryProperties.kt index c7f9f7d7..9c08dc58 100644 --- a/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryProperties.kt +++ b/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryProperties.kt @@ -20,7 +20,7 @@ class DiscoveryProperties { require(capacity <= Int.MAX_VALUE - headroom) { "Discovery capacity plus headroom exceeds the supported channel size" } - require(capacity + headroom < Channel.UNLIMITED) { + require(discoveryChannelCapacity(capacity, headroom) < Channel.UNLIMITED) { "Discovery capacity plus headroom must be a bounded channel size" } require(batchSize in 1..minOf(capacity, MAX_BATCH_SIZE)) { diff --git a/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryQueue.kt b/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryQueue.kt index 789dca93..64788749 100644 --- a/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryQueue.kt +++ b/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryQueue.kt @@ -28,7 +28,7 @@ class DiscoveryQueue( private val size = AtomicInteger() private val buffer = Channel( - capacity = properties.capacity + properties.headroom, + capacity = discoveryChannelCapacity(properties.capacity, properties.headroom), onUndeliveredElement = ::discard, ) diff --git a/src/test/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryPropertiesTest.kt b/src/test/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryPropertiesTest.kt new file mode 100644 index 00000000..8090ae5a --- /dev/null +++ b/src/test/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryPropertiesTest.kt @@ -0,0 +1,39 @@ +package cash.atto.node.bootstrap.discovery + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class DiscoveryPropertiesTest { + @Test + fun `accepts channel capacity just below the unlimited sentinel`() { + // Given + val properties = DiscoveryProperties() + properties.capacity = Int.MAX_VALUE - 2 + properties.headroom = 1 + + // When and then + properties.validate() + } + + @Test + fun `rejects the unlimited channel capacity sentinel`() { + // Given + val properties = DiscoveryProperties() + properties.capacity = Int.MAX_VALUE - 1 + properties.headroom = 1 + + // When and then + assertThrows { properties.validate() } + } + + @Test + fun `rejects channel capacity that would overflow an integer`() { + // Given + val properties = DiscoveryProperties() + properties.capacity = Int.MAX_VALUE + properties.headroom = 1 + + // When and then + assertThrows { properties.validate() } + } +} diff --git a/verification/snakt/README.md b/verification/snakt/README.md new file mode 100644 index 00000000..c7d876b4 --- /dev/null +++ b/verification/snakt/README.md @@ -0,0 +1,22 @@ +# SnaKt verification + +The discovery queue proof checks that adding headroom provides exactly the configured number of extra slots and identifies the boundary at which the coroutine channel would become unlimited. Its preconditions follow the capacity and overflow checks in `DiscoveryProperties`. + +The proof compiles the production source with SnaKt’s Kotlin 2.3.0 compiler. The node build uses its existing Kotlin toolchain. A failed proof is a compiler warning, which this build treats as an error. + +## Run locally + +Use JDK 21 and set `Z3_EXE` to the absolute path of the [Z3 4.8.7 executable](https://github.com/Z3Prover/z3/releases/tag/z3-4.8.7). From the repository root, build the pinned verifier once. + +```sh +mkdir -p build/snakt +git clone https://github.com/JetBrains/SnaKt.git build/snakt/SnaKt +git -C build/snakt/SnaKt checkout --detach e8d86408298192f121ba835a4f5d87ecf388356d +build/snakt/SnaKt/gradlew -Dmaven.repo.local="$PWD/build/snakt/maven" -p build/snakt/SnaKt publishToMavenLocal +``` + +Run the proof after changing the code or its contract. + +```sh +build/snakt/SnaKt/gradlew -Dmaven.repo.local="$PWD/build/snakt/maven" -p verification/snakt compileKotlin --rerun-tasks +``` diff --git a/verification/snakt/build.gradle.kts b/verification/snakt/build.gradle.kts new file mode 100644 index 00000000..d75f9279 --- /dev/null +++ b/verification/snakt/build.gradle.kts @@ -0,0 +1,29 @@ +plugins { + kotlin("jvm") version "2.3.0" + id("org.jetbrains.kotlin.formver") version "0.1.0-SNAPSHOT" +} + +repositories { + mavenLocal { + content { + includeGroup("org.jetbrains.kotlin.formver") + } + } + mavenCentral() +} + +kotlin { + jvmToolchain(21) + + compilerOptions { + allWarningsAsErrors.set(true) + } + + sourceSets.named("main") { + kotlin.srcDir("../../src/main/kotlin") + kotlin.include( + "cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacity.kt", + "cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacityProof.kt", + ) + } +} diff --git a/verification/snakt/gradle.properties b/verification/snakt/gradle.properties new file mode 100644 index 00000000..2307a6a8 --- /dev/null +++ b/verification/snakt/gradle.properties @@ -0,0 +1 @@ +kotlin.daemon.jvmargs=-Xss30m -XX:MaxMetaspaceSize=1g diff --git a/verification/snakt/settings.gradle.kts b/verification/snakt/settings.gradle.kts new file mode 100644 index 00000000..8461a0ac --- /dev/null +++ b/verification/snakt/settings.gradle.kts @@ -0,0 +1,13 @@ +pluginManagement { + repositories { + mavenLocal { + content { + includeGroup("org.jetbrains.kotlin.formver") + } + } + mavenCentral() + gradlePluginPortal() + } +} + +rootProject.name = "node-snakt" diff --git a/verification/snakt/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacityProof.kt b/verification/snakt/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacityProof.kt new file mode 100644 index 00000000..b528be52 --- /dev/null +++ b/verification/snakt/src/main/kotlin/cash/atto/node/bootstrap/discovery/DiscoveryChannelCapacityProof.kt @@ -0,0 +1,28 @@ +package cash.atto.node.bootstrap.discovery + +import org.jetbrains.kotlin.formver.plugin.AlwaysVerify +import org.jetbrains.kotlin.formver.plugin.postconditions +import org.jetbrains.kotlin.formver.plugin.preconditions + +@AlwaysVerify +internal fun verifyDiscoveryChannelCapacity( + capacity: Int, + headroom: Int, +): Int { + // DiscoveryProperties validates these bounds before DiscoveryQueue allocates the channel. + // The literal is Channel.UNLIMITED (Int.MAX_VALUE), which SnaKt cannot resolve as a qualified constant. + preconditions { + capacity > 0 + headroom >= 0 + capacity <= 2_147_483_647 - headroom + } + + postconditions { result -> + result >= capacity + result <= 2_147_483_647 + result - capacity == headroom + (result < 2_147_483_647) == (capacity < 2_147_483_647 - headroom) + } + + return discoveryChannelCapacity(capacity, headroom) +}