Skip to content
Closed
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
40 changes: 40 additions & 0 deletions .github/workflows/snakt.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DiscoveryQueue(
private val size = AtomicInteger()
private val buffer =
Channel<PendingDiscovery>(
capacity = properties.capacity + properties.headroom,
capacity = discoveryChannelCapacity(properties.capacity, properties.headroom),
onUndeliveredElement = ::discard,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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<IllegalArgumentException> { 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<IllegalArgumentException> { properties.validate() }
}
}
22 changes: 22 additions & 0 deletions verification/snakt/README.md
Original file line number Diff line number Diff line change
@@ -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
```
29 changes: 29 additions & 0 deletions verification/snakt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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",
)
}
}
1 change: 1 addition & 0 deletions verification/snakt/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.daemon.jvmargs=-Xss30m -XX:MaxMetaspaceSize=1g
13 changes: 13 additions & 0 deletions verification/snakt/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroup("org.jetbrains.kotlin.formver")
}
}
mavenCentral()
gradlePluginPortal()
}
}

rootProject.name = "node-snakt"
Original file line number Diff line number Diff line change
@@ -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<Int> { 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)
}
Loading