diff --git a/.github/workflows/code-check.yml b/.github/workflows/code-check.yml index 580b531..c278bd1 100644 --- a/.github/workflows/code-check.yml +++ b/.github/workflows/code-check.yml @@ -179,21 +179,12 @@ jobs: uses: actions-rs/cargo@9e120dd99b0fbad1c065f686657e914e76bd7b72 # Version 1.0.1 with: command: nextest - # The Cucumber runners need node binaries and are run locally for now. + # The Cucumber runners need node binaries and have their own workflow. args: >- run --workspace --locked --lib --bins --examples --tests --retries 2 --all-features --no-fail-fast --exclude logos-blockchain-zone-tests --exclude logos-blockchain-sql-tests - # Include the support libraries without invoking their Cucumber runners. - - name: Run client test support unit tests - uses: actions-rs/cargo@9e120dd99b0fbad1c065f686657e914e76bd7b72 # Version 1.0.1 - with: - command: nextest - args: >- - run --locked --lib --retries 2 --no-fail-fast - -p logos-blockchain-zone-tests -p logos-blockchain-sql-tests - # Stable required check for nextest unit tests. # # On pull requests, this job succeeds only when both nextest unit diff --git a/.github/workflows/cucumber-integration-tests.yml b/.github/workflows/cucumber-integration-tests.yml index 2c7c22a..aec740a 100644 --- a/.github/workflows/cucumber-integration-tests.yml +++ b/.github/workflows/cucumber-integration-tests.yml @@ -54,26 +54,26 @@ jobs: with: ref: ${{ github.sha }} - # Every node crate in `Cargo.toml` is pinned to one revision; the node + # Every node crate in `Cargo.toml` is pinned to one release tag; the node # binary must come from the same one. - - name: Read the pinned node revision + - name: Read the pinned node release id: node run: | - rev="$(sed -nE 's/^lb-core += \{.*rev = "([0-9a-f]{40})".*$/\1/p' Cargo.toml)" - if [ -z "$rev" ]; then - echo "could not read the pinned node revision from Cargo.toml" >&2 + tag="$(sed -nE 's/^lb-core += \{.*tag = "([^"]+)".*$/\1/p' Cargo.toml)" + if [ -z "$tag" ]; then + echo "could not read the pinned node release from Cargo.toml" >&2 exit 1 fi - echo "rev=$rev" >> "$GITHUB_OUTPUT" + echo "tag=$tag" >> "$GITHUB_OUTPUT" # `--features testing` matches the node harness's own node build. - - name: Build the node binary at the pinned revision + - name: Build the node binary at the pinned release env: CARGO_INCREMENTAL: 0 # Disable incremental compilation run: >- cargo install --locked --git https://github.com/logos-blockchain/logos-blockchain.git - --rev "${{ steps.node.outputs.rev }}" + --tag "${{ steps.node.outputs.tag }}" --features testing --target-dir target/node-target --root target/node diff --git a/Cargo.lock b/Cargo.lock index ba06eeb..35328da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,16 +17,6 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" -[[package]] -name = "aead" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" -dependencies = [ - "crypto-common", - "generic-array 0.14.7", -] - [[package]] name = "ahash" version = "0.8.12" @@ -381,6 +371,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "asn1_der" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4858a9d740c5007a9069007c3b4e91152d0506f13c1b31dd49051fd537656156" + [[package]] name = "astro-float" version = "0.9.6" @@ -668,6 +664,12 @@ version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + [[package]] name = "base256emoji" version = "1.0.2" @@ -791,70 +793,6 @@ dependencies = [ "objc2", ] -[[package]] -name = "blockchain-test-logos" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" -dependencies = [ - "anyhow", - "async-trait", - "blockchain-test-support", - "cfgsync-adapter", - "cfgsync-artifacts", - "flate2", - "futures", - "hex", - "logos-blockchain-binary-codec", - "logos-blockchain-blend-service", - "logos-blockchain-chain-service", - "logos-blockchain-common-http-client", - "logos-blockchain-config", - "logos-blockchain-core", - "logos-blockchain-http-api-common", - "logos-blockchain-key-management-system-service", - "logos-blockchain-libp2p", - "logos-blockchain-network-service", - "logos-blockchain-node", - "logos-blockchain-sdp-service", - "logos-blockchain-tx-service", - "logos-blockchain-utils", - "rand 0.8.8", - "reqwest 0.12.28", - "serde", - "serde_json", - "serde_yaml", - "sysinfo", - "tar", - "tempfile", - "testing-framework-app", - "testing-framework-core", - "testing-framework-runner-compose", - "testing-framework-runner-k8s", - "testing-framework-runner-local", - "thiserror 2.0.21", - "time", - "tokio", - "tracing", - "uuid", -] - -[[package]] -name = "blockchain-test-support" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" -dependencies = [ - "hex", - "logos-blockchain-binary-codec", - "logos-blockchain-key-management-system-service", - "logos-blockchain-libp2p", - "num-bigint", - "rand 0.8.8", - "serde", - "testing-framework-core", - "testing-framework-tools", - "thiserror 2.0.21", -] - [[package]] name = "bs58" version = "0.5.1" @@ -944,17 +882,37 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" +[[package]] +name = "cfgsync-adapter" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" +dependencies = [ + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "cfgsync-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "serde", + "serde_json", +] + [[package]] name = "cfgsync-adapter" version = "0.1.0" source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" dependencies = [ - "cfgsync-artifacts", - "cfgsync-core", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "cfgsync-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", "serde", "serde_json", ] +[[package]] +name = "cfgsync-artifacts" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "cfgsync-artifacts" version = "0.1.0" @@ -967,11 +925,11 @@ dependencies = [ [[package]] name = "cfgsync-core" version = "0.1.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" dependencies = [ "anyhow", "axum 0.7.9", - "cfgsync-artifacts", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", "reqwest 0.12.28", "serde", "serde_json", @@ -981,14 +939,19 @@ dependencies = [ ] [[package]] -name = "chacha20" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +name = "cfgsync-core" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" dependencies = [ - "cfg-if", - "cipher", - "cpufeatures 0.2.17", + "anyhow", + "axum 0.7.9", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "reqwest 0.12.28", + "serde", + "serde_json", + "serde_yaml", + "thiserror 2.0.21", + "tokio", ] [[package]] @@ -1002,19 +965,6 @@ dependencies = [ "rand_core 0.10.1", ] -[[package]] -name = "chacha20poly1305" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" -dependencies = [ - "aead", - "chacha20 0.9.1", - "cipher", - "poly1305", - "zeroize", -] - [[package]] name = "chkstk_stub" version = "0.1.0" @@ -1058,17 +1008,6 @@ dependencies = [ "phf_codegen", ] -[[package]] -name = "cipher" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" -dependencies = [ - "crypto-common", - "inout", - "zeroize", -] - [[package]] name = "clang-sys" version = "1.9.1" @@ -1317,6 +1256,18 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array 0.14.7", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.7" @@ -1324,7 +1275,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array 0.14.7", - "rand_core 0.6.4", "typenum", ] @@ -1641,6 +1591,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", + "const-oid", "crypto-common", "subtle", ] @@ -1663,7 +1614,7 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1706,6 +1657,20 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c3cf4824e2d5f025c7b531afcb2325364084a16806f6d47fbc1f5fbd9960590" +[[package]] +name = "ecdsa" +version = "0.16.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +dependencies = [ + "der", + "digest", + "elliptic-curve", + "rfc6979", + "signature", + "spki", +] + [[package]] name = "ed25519" version = "2.2.3" @@ -1762,6 +1727,25 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" +[[package]] +name = "elliptic-curve" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +dependencies = [ + "base16ct", + "crypto-bigint", + "digest", + "ff", + "generic-array 0.14.7", + "group", + "pkcs8", + "rand_core 0.6.4", + "sec1", + "subtle", + "zeroize", +] + [[package]] name = "encode_unicode" version = "1.0.0" @@ -1813,7 +1797,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1888,6 +1872,16 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "ff" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b50bfb653653f9ca9095b427bed08ab8d75a137839d9ad64eb11810d5b6393" +dependencies = [ + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "fiat-crypto" version = "0.2.9" @@ -2112,6 +2106,7 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", + "zeroize", ] [[package]] @@ -2229,6 +2224,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff", + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "h2" version = "0.4.20" @@ -2849,15 +2855,6 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" -[[package]] -name = "inout" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" -dependencies = [ - "generic-array 0.14.7", -] - [[package]] name = "inventory" version = "0.3.25" @@ -3054,6 +3051,20 @@ dependencies = [ "time", ] +[[package]] +name = "k256" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6e3919bbaa2945715f0bb6d3934a173d1e9a59ac23767fbaaef277265a7411b" +dependencies = [ + "cfg-if", + "ecdsa", + "elliptic-curve", + "once_cell", + "sha2", + "signature", +] + [[package]] name = "k8s-openapi" version = "0.27.1" @@ -3394,9 +3405,11 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9525f3831544f7ae497bde79adf114ef127b0fbbb97edbbf692a80408636421c" dependencies = [ + "asn1_der", "bs58", "ed25519-dalek", "hkdf", + "k256", "multihash", "prost 0.14.4", "rand 0.8.8", @@ -3700,8 +3713,8 @@ checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" [[package]] name = "logos-blockchain-api-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "futures", @@ -3733,8 +3746,8 @@ dependencies = [ [[package]] name = "logos-blockchain-binary-codec" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "bincode", "bytes", @@ -3749,8 +3762,8 @@ dependencies = [ [[package]] name = "logos-blockchain-binary-codec-macros" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "hex", "proc-macro2", @@ -3760,8 +3773,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-blend-crypto", "logos-blockchain-blend-membership", @@ -3775,8 +3788,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-crypto" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "blake2", "logos-blockchain-groth16", @@ -3790,8 +3803,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-membership" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-key-management-system-keys", "multiaddr", @@ -3801,8 +3814,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-message" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "blake2", "educe 0.7.6", @@ -3828,8 +3841,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-network" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "either", "futures", @@ -3849,8 +3862,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-primitives" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "futures", "tokio", @@ -3858,8 +3871,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-proofs" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "ed25519-dalek", "generic-array 1.4.5", @@ -3880,8 +3893,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-provers" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "educe 0.7.6", @@ -3904,8 +3917,8 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-scheduling" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "fork_stream", "futures", @@ -3925,10 +3938,11 @@ dependencies = [ [[package]] name = "logos-blockchain-blend-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", + "fork_stream", "futures", "hex", "libp2p", @@ -3958,6 +3972,7 @@ dependencies = [ "rand_chacha 0.3.1", "rayon", "serde", + "serde_with", "thiserror 2.0.21", "tokio", "tokio-stream", @@ -3966,8 +3981,8 @@ dependencies = [ [[package]] name = "logos-blockchain-chain-broadcast-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "educe 0.7.6", @@ -3981,8 +3996,8 @@ dependencies = [ [[package]] name = "logos-blockchain-chain-leader-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "futures", @@ -4014,8 +4029,8 @@ dependencies = [ [[package]] name = "logos-blockchain-chain-network-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "backon", @@ -4048,8 +4063,8 @@ dependencies = [ [[package]] name = "logos-blockchain-chain-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "bytes", @@ -4061,7 +4076,6 @@ dependencies = [ "logos-blockchain-cryptarchia-engine", "logos-blockchain-cryptarchia-sync", "logos-blockchain-groth16", - "logos-blockchain-key-management-system-keys", "logos-blockchain-ledger", "logos-blockchain-log-targets", "logos-blockchain-network-service", @@ -4133,8 +4147,8 @@ dependencies = [ [[package]] name = "logos-blockchain-circuits-prover" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "rust-rapidsnark", ] @@ -4160,8 +4174,8 @@ dependencies = [ [[package]] name = "logos-blockchain-common-http-client" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "futures", "hex", @@ -4174,7 +4188,6 @@ dependencies = [ "logos-blockchain-key-management-system-keys", "logos-blockchain-log-targets", "logos-blockchain-sdp-service", - "logos-blockchain-utils", "logos-blockchain-version", "reqwest 0.12.28", "serde", @@ -4186,8 +4199,8 @@ dependencies = [ [[package]] name = "logos-blockchain-config" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "hex", "logos-blockchain-binary-codec", @@ -4202,14 +4215,13 @@ dependencies = [ "logos-blockchain-utils", "num-bigint", "rand 0.8.8", - "testing-framework-tools", "time", ] [[package]] name = "logos-blockchain-core" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "ark-ff", "bincode", @@ -4226,7 +4238,6 @@ dependencies = [ "logos-blockchain-poc", "logos-blockchain-pol", "logos-blockchain-poseidon2", - "logos-blockchain-time", "logos-blockchain-utils", "logos-blockchain-utxotree", "logos-blockchain-zksign", @@ -4243,33 +4254,36 @@ dependencies = [ [[package]] name = "logos-blockchain-cryptarchia-engine" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ + "logos-blockchain-binary-codec", "logos-blockchain-log-targets", "logos-blockchain-pol", - "logos-blockchain-time", "logos-blockchain-utils", "rpds", "serde", + "serde_with", "thiserror 2.0.21", + "time", + "tokio", "tracing", "utoipa", ] [[package]] name = "logos-blockchain-cryptarchia-sync" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "bytes", - "educe 0.7.6", "futures", "libp2p", "libp2p-stream", "logos-blockchain-binary-codec", "logos-blockchain-core", "logos-blockchain-cryptarchia-engine", + "logos-blockchain-key-management-system-keys", "logos-blockchain-log-targets", "logos-blockchain-utils", "rand 0.8.8", @@ -4282,16 +4296,16 @@ dependencies = [ [[package]] name = "logos-blockchain-dynamic-merkle" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "serde", ] [[package]] name = "logos-blockchain-faucet" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "axum 0.8.9", "clap", @@ -4300,15 +4314,17 @@ dependencies = [ "logos-blockchain-groth16", "logos-blockchain-http-api-common", "logos-blockchain-key-management-system-keys", + "logos-blockchain-node", "reqwest 0.12.28", "serde_json", + "serde_yaml", "tokio", ] [[package]] name = "logos-blockchain-groth16" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "ark-bn254", "ark-ec", @@ -4328,8 +4344,8 @@ dependencies = [ [[package]] name = "logos-blockchain-http-api-common" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "axum 0.8.9", "logos-blockchain-core", @@ -4349,8 +4365,8 @@ dependencies = [ [[package]] name = "logos-blockchain-key-management-system-keys" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "arbitrary-int", "async-trait", @@ -4380,8 +4396,8 @@ dependencies = [ [[package]] name = "logos-blockchain-key-management-system-macros" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "proc-macro2", "quote", @@ -4390,8 +4406,8 @@ dependencies = [ [[package]] name = "logos-blockchain-key-management-system-operators" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "logos-blockchain-blend-proofs", @@ -4408,8 +4424,8 @@ dependencies = [ [[package]] name = "logos-blockchain-key-management-system-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "logos-blockchain-key-management-system-keys", @@ -4426,8 +4442,8 @@ dependencies = [ [[package]] name = "logos-blockchain-ledger" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "educe 0.7.6", "logos-blockchain-binary-codec", @@ -4441,7 +4457,6 @@ dependencies = [ "logos-blockchain-log-targets", "logos-blockchain-mmr", "logos-blockchain-pol", - "logos-blockchain-time", "logos-blockchain-utils", "logos-blockchain-utxotree", "num-bigint", @@ -4455,8 +4470,8 @@ dependencies = [ [[package]] name = "logos-blockchain-libp2p" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "backon", @@ -4467,7 +4482,6 @@ dependencies = [ "hickory-resolver", "igd-next", "libp2p", - "logos-blockchain-core", "logos-blockchain-cryptarchia-sync", "logos-blockchain-log-targets", "logos-blockchain-utils", @@ -4486,16 +4500,16 @@ dependencies = [ [[package]] name = "logos-blockchain-log-targets" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-log-targets-macros", ] [[package]] name = "logos-blockchain-log-targets-macros" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "proc-macro2", "quote", @@ -4504,8 +4518,8 @@ dependencies = [ [[package]] name = "logos-blockchain-merkle-tree" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-dynamic-merkle", "rpds", @@ -4515,8 +4529,8 @@ dependencies = [ [[package]] name = "logos-blockchain-mmr" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "ark-ff", "logos-blockchain-groth16", @@ -4529,19 +4543,15 @@ dependencies = [ [[package]] name = "logos-blockchain-network-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", - "educe 0.7.6", "futures", "logos-blockchain-core", "logos-blockchain-cryptarchia-sync", "logos-blockchain-libp2p", "logos-blockchain-log-targets", - "logos-blockchain-services-utils", - "logos-blockchain-time", - "logos-blockchain-time-service", "logos-blockchain-tracing", "logos-blockchain-utils", "overwatch", @@ -4556,8 +4566,8 @@ dependencies = [ [[package]] name = "logos-blockchain-node" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "axum 0.8.9", @@ -4591,7 +4601,6 @@ dependencies = [ "logos-blockchain-services-utils", "logos-blockchain-storage-service", "logos-blockchain-system-sig-service", - "logos-blockchain-time", "logos-blockchain-time-service", "logos-blockchain-tracing", "logos-blockchain-tracing-service", @@ -4602,13 +4611,12 @@ dependencies = [ "num-bigint", "nutype", "overwatch", + "percent-encoding", "rand 0.8.8", - "regex", "serde", "serde_json", "serde_with", "serde_yaml", - "strum", "thiserror 2.0.21", "time", "tokio", @@ -4624,8 +4632,8 @@ dependencies = [ [[package]] name = "logos-blockchain-poc" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-circuits-poc-sys", "logos-blockchain-circuits-prover", @@ -4641,8 +4649,8 @@ dependencies = [ [[package]] name = "logos-blockchain-pol" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "astro-float", "logos-blockchain-circuits-pol-sys", @@ -4661,8 +4669,8 @@ dependencies = [ [[package]] name = "logos-blockchain-poq" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-circuits-poq-sys", "logos-blockchain-circuits-prover", @@ -4679,8 +4687,8 @@ dependencies = [ [[package]] name = "logos-blockchain-poseidon2" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "ark-bn254", "ark-ff", @@ -4690,8 +4698,8 @@ dependencies = [ [[package]] name = "logos-blockchain-pow-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "futures", @@ -4705,7 +4713,6 @@ dependencies = [ "logos-blockchain-log-targets", "logos-blockchain-services-utils", "logos-blockchain-storage-service", - "logos-blockchain-time", "logos-blockchain-time-service", "logos-blockchain-utils", "logos-blockchain-wallet-service", @@ -4722,8 +4729,8 @@ dependencies = [ [[package]] name = "logos-blockchain-proofs-error" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-circuits-types", "logos-blockchain-groth16", @@ -4733,8 +4740,8 @@ dependencies = [ [[package]] name = "logos-blockchain-sdp-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "logos-blockchain-chain-service", @@ -4756,8 +4763,8 @@ dependencies = [ [[package]] name = "logos-blockchain-services-utils" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "bytes", @@ -4767,37 +4774,24 @@ dependencies = [ "overwatch", "serde", "thiserror 2.0.21", + "tokio", "tracing", ] [[package]] name = "logos-blockchain-sql-tests" -version = "0.0.0" +version = "0.3.6" dependencies = [ - "blockchain-test-logos", "cucumber", - "futures", - "hex", - "logos-blockchain-common-http-client", - "logos-blockchain-core", - "logos-blockchain-groth16", "logos-blockchain-tests", - "logos-blockchain-zone-sdk", - "logos-blockchain-zone-tests", - "logos-sql", - "rand 0.8.8", - "rusqlite", - "serde", - "serde_json", - "thiserror 2.0.21", + "testing-framework", "tokio", - "tracing", ] [[package]] name = "logos-blockchain-storage-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "bytes", @@ -4819,8 +4813,8 @@ dependencies = [ [[package]] name = "logos-blockchain-system-sig-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-ctrlc", "async-trait", @@ -4831,14 +4825,11 @@ dependencies = [ [[package]] name = "logos-blockchain-tests" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ - "async-trait", "axum 0.8.9", "bincode", - "blockchain-test-logos", - "blockchain-test-support", "cucumber", "ed25519", "educe 0.7.6", @@ -4867,11 +4858,14 @@ dependencies = [ "logos-blockchain-utils", "logos-blockchain-wallet", "logos-blockchain-zksign", + "logos-blockchain-zone-sdk", + "logos-sql", "num-bigint", "rand 0.8.8", "rand_chacha 0.3.1", "reqwest 0.12.28", "rpds", + "rusqlite", "serde", "serde_json", "serde_yaml", @@ -4879,9 +4873,10 @@ dependencies = [ "strum", "strum_macros", "tempfile", - "testing-framework-app", - "testing-framework-core", - "testing-framework-runner-local", + "testing-framework", + "testing-framework-app 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-runner-local 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", "thiserror 2.0.21", "time", "tokio", @@ -4889,29 +4884,16 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "logos-blockchain-time" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" -dependencies = [ - "logos-blockchain-binary-codec", - "logos-blockchain-utils", - "serde", - "thiserror 2.0.21", - "time", - "utoipa", -] - [[package]] name = "logos-blockchain-time-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "futures", "log", + "logos-blockchain-cryptarchia-engine", "logos-blockchain-log-targets", - "logos-blockchain-time", "logos-blockchain-tracing", "logos-blockchain-utils", "overwatch", @@ -4927,8 +4909,8 @@ dependencies = [ [[package]] name = "logos-blockchain-tracing" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "flate2", "logos-blockchain-log-targets", @@ -4954,8 +4936,8 @@ dependencies = [ [[package]] name = "logos-blockchain-tracing-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "logos-blockchain-log-targets", @@ -4972,8 +4954,8 @@ dependencies = [ [[package]] name = "logos-blockchain-tx-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "futures", @@ -4986,8 +4968,6 @@ dependencies = [ "logos-blockchain-network-service", "logos-blockchain-services-utils", "logos-blockchain-storage-service", - "logos-blockchain-time", - "logos-blockchain-time-service", "logos-blockchain-tracing", "logos-blockchain-utils", "overwatch", @@ -5002,8 +4982,8 @@ dependencies = [ [[package]] name = "logos-blockchain-utils" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "const-hex", @@ -5026,8 +5006,8 @@ dependencies = [ [[package]] name = "logos-blockchain-utxotree" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "ark-ff", "logos-blockchain-dynamic-merkle", @@ -5039,8 +5019,8 @@ dependencies = [ [[package]] name = "logos-blockchain-version" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "serde", "utoipa", @@ -5048,8 +5028,8 @@ dependencies = [ [[package]] name = "logos-blockchain-wallet" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-core", "logos-blockchain-cryptarchia-engine", @@ -5067,14 +5047,13 @@ dependencies = [ [[package]] name = "logos-blockchain-wallet-service" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "async-trait", "bytes", "futures", "hex", - "logos-blockchain-binary-codec", "logos-blockchain-chain-service", "logos-blockchain-core", "logos-blockchain-cryptarchia-engine", @@ -5085,8 +5064,6 @@ dependencies = [ "logos-blockchain-mmr", "logos-blockchain-services-utils", "logos-blockchain-storage-service", - "logos-blockchain-time", - "logos-blockchain-time-service", "logos-blockchain-utils", "logos-blockchain-wallet", "overwatch", @@ -5099,8 +5076,8 @@ dependencies = [ [[package]] name = "logos-blockchain-zksign" -version = "0.0.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain.git?rev=a159f8c155df68d7e2f045a418ae737de246354e#a159f8c155df68d7e2f045a418ae737de246354e" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" dependencies = [ "logos-blockchain-circuits-prover", "logos-blockchain-circuits-signature-sys", @@ -5119,10 +5096,9 @@ dependencies = [ [[package]] name = "logos-blockchain-zone-sdk" -version = "0.0.0" +version = "0.3.6" dependencies = [ "async-trait", - "blockchain-test-logos", "futures", "hex", "logos-blockchain-binary-codec", @@ -5132,55 +5108,40 @@ dependencies = [ "logos-blockchain-http-api-common", "logos-blockchain-key-management-system-service", "logos-blockchain-log-targets", - "logos-blockchain-time", "num-bigint", "rand 0.8.8", "reqwest 0.12.28", "rpds", "serde", "serde_json", - "testing-framework-app", - "testing-framework-core", + "testing-framework", + "testing-framework-app 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", "thiserror 2.0.21", - "time", "tokio", "tracing", ] [[package]] name = "logos-blockchain-zone-tests" -version = "0.0.0" +version = "0.3.6" dependencies = [ - "blockchain-test-logos", "cucumber", - "futures", - "logos-blockchain-common-http-client", - "logos-blockchain-core", - "logos-blockchain-groth16", - "logos-blockchain-http-api-common", - "logos-blockchain-key-management-system-service", "logos-blockchain-tests", - "logos-blockchain-utils", - "logos-blockchain-zone-sdk", - "rand 0.8.8", - "reqwest 0.12.28", - "thiserror 2.0.21", + "testing-framework", "tokio", - "tracing", ] [[package]] name = "logos-sql" -version = "0.0.0" +version = "0.3.6" dependencies = [ "bincode", "blake2", - "chacha20poly1305", "clap", "flate2", "hex", "logos-blockchain-binary-codec", - "logos-blockchain-core", "logos-blockchain-groth16", "logos-blockchain-key-management-system-service", "logos-blockchain-log-targets", @@ -5193,7 +5154,6 @@ dependencies = [ "thiserror 2.0.21", "tokio", "tracing", - "zeroize", ] [[package]] @@ -5623,7 +5583,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -5773,12 +5733,6 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" -[[package]] -name = "opaque-debug" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" - [[package]] name = "openssl" version = "0.10.81" @@ -5947,7 +5901,7 @@ dependencies = [ [[package]] name = "overwatch" version = "0.2.0" -source = "git+https://github.com/logos-co/Overwatch?rev=fc71a844886f9fb8f382ecfadcebff455e33474b#fc71a844886f9fb8f382ecfadcebff455e33474b" +source = "git+https://github.com/logos-co/Overwatch.git?rev=f25266fb32b3c0651fdc7c368db8d66a544dcf68#f25266fb32b3c0651fdc7c368db8d66a544dcf68" dependencies = [ "async-trait", "futures", @@ -5962,7 +5916,7 @@ dependencies = [ [[package]] name = "overwatch-derive" version = "0.2.0" -source = "git+https://github.com/logos-co/Overwatch?rev=fc71a844886f9fb8f382ecfadcebff455e33474b#fc71a844886f9fb8f382ecfadcebff455e33474b" +source = "git+https://github.com/logos-co/Overwatch.git?rev=f25266fb32b3c0651fdc7c368db8d66a544dcf68#f25266fb32b3c0651fdc7c368db8d66a544dcf68" dependencies = [ "convert_case 0.8.0", "manyhow", @@ -6202,17 +6156,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "poly1305" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" -dependencies = [ - "cpufeatures 0.2.17", - "opaque-debug", - "universal-hash", -] - [[package]] name = "portable-atomic" version = "1.15.0" @@ -6354,7 +6297,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0" dependencies = [ "bitflags 2.13.2", - "chacha20 0.10.2", + "chacha20", "core_detect", "num-traits", "rand 0.10.3", @@ -6521,7 +6464,7 @@ dependencies = [ "once_cell", "socket2 0.6.5", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -6572,7 +6515,7 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ - "chacha20 0.10.2", + "chacha20", "getrandom 0.4.3", "rand_core 0.10.1", ] @@ -6822,6 +6765,16 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e061d1b48cb8d38042de4ae0a7a6401009d6143dc80d2e2d6f31f0bdd6470c7" +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac", + "subtle", +] + [[package]] name = "ring" version = "0.17.14" @@ -6959,7 +6912,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -7068,6 +7021,20 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct", + "der", + "generic-array 0.14.7", + "pkcs8", + "subtle", + "zeroize", +] + [[package]] name = "secrecy" version = "0.10.3" @@ -7332,6 +7299,7 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" dependencies = [ + "digest", "rand_core 0.6.4", ] @@ -7684,7 +7652,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -7716,7 +7684,68 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" dependencies = [ "rustix", - "windows-sys 0.59.0", + "windows-sys 0.61.2", +] + +[[package]] +name = "testing-framework" +version = "0.3.6" +source = "git+https://github.com/logos-blockchain/logos-blockchain.git?tag=0.3.6#075203096dd287f12dfd0a4a405d685c99ed6908" +dependencies = [ + "anyhow", + "async-trait", + "cfgsync-adapter 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "flate2", + "futures", + "hex", + "logos-blockchain-binary-codec", + "logos-blockchain-blend-service", + "logos-blockchain-chain-service", + "logos-blockchain-common-http-client", + "logos-blockchain-config", + "logos-blockchain-core", + "logos-blockchain-http-api-common", + "logos-blockchain-key-management-system-service", + "logos-blockchain-libp2p", + "logos-blockchain-network-service", + "logos-blockchain-node", + "logos-blockchain-sdp-service", + "logos-blockchain-tx-service", + "logos-blockchain-utils", + "num-bigint", + "rand 0.8.8", + "reqwest 0.12.28", + "serde", + "serde_json", + "serde_yaml", + "sysinfo", + "tar", + "tempfile", + "testing-framework-app 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-runner-compose", + "testing-framework-runner-k8s", + "testing-framework-runner-local 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "thiserror 2.0.21", + "time", + "tokio", + "tracing", + "uuid", +] + +[[package]] +name = "testing-framework-app" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" +dependencies = [ + "async-trait", + "rand 0.8.8", + "testing-framework-container 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-runner-local 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "thiserror 2.0.21", + "tokio", ] [[package]] @@ -7726,20 +7755,52 @@ source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?r dependencies = [ "async-trait", "rand 0.8.8", - "testing-framework-container", - "testing-framework-core", - "testing-framework-runner-local", + "testing-framework-container 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "testing-framework-runner-local 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", "thiserror 2.0.21", "tokio", ] +[[package]] +name = "testing-framework-container" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" +dependencies = [ + "async-trait", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", +] + [[package]] name = "testing-framework-container" version = "0.1.0" source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" dependencies = [ "async-trait", - "testing-framework-core", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", +] + +[[package]] +name = "testing-framework-core" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" +dependencies = [ + "anyhow", + "async-trait", + "cfgsync-adapter 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "cfgsync-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "futures", + "parking_lot", + "prometheus-http-query", + "rand 0.8.8", + "reqwest 0.12.28", + "serde", + "serde_json", + "serde_yaml", + "thiserror 2.0.21", + "tokio", + "tracing", ] [[package]] @@ -7749,9 +7810,9 @@ source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?r dependencies = [ "anyhow", "async-trait", - "cfgsync-adapter", - "cfgsync-artifacts", - "cfgsync-core", + "cfgsync-adapter 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", + "cfgsync-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", "futures", "parking_lot", "prometheus-http-query", @@ -7768,7 +7829,7 @@ dependencies = [ [[package]] name = "testing-framework-runner-compose" version = "0.1.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" dependencies = [ "anyhow", "async-trait", @@ -7778,8 +7839,8 @@ dependencies = [ "serde_json", "tempfile", "tera", - "testing-framework-container", - "testing-framework-core", + "testing-framework-container 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", "testing-framework-tools", "thiserror 2.0.21", "tokio", @@ -7792,12 +7853,12 @@ dependencies = [ [[package]] name = "testing-framework-runner-k8s" version = "0.1.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" dependencies = [ "anyhow", "async-trait", - "cfgsync-artifacts", - "cfgsync-core", + "cfgsync-artifacts 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "cfgsync-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", "k8s-openapi", "kube", "reqwest 0.12.28", @@ -7806,7 +7867,7 @@ dependencies = [ "serde_json", "serde_yaml", "tempfile", - "testing-framework-core", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", "thiserror 2.0.21", "tokio", "tokio-retry", @@ -7814,6 +7875,25 @@ dependencies = [ "url", ] +[[package]] +name = "testing-framework-runner-local" +version = "0.1.0" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" +dependencies = [ + "async-trait", + "fs_extra", + "reqwest 0.12.28", + "serde", + "serde_yaml", + "sha2", + "tempfile", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47)", + "thiserror 2.0.21", + "tokio", + "tokio-retry", + "tracing", +] + [[package]] name = "testing-framework-runner-local" version = "0.1.0" @@ -7826,7 +7906,7 @@ dependencies = [ "serde_yaml", "sha2", "tempfile", - "testing-framework-core", + "testing-framework-core 0.1.0 (git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df)", "thiserror 2.0.21", "tokio", "tokio-retry", @@ -7836,7 +7916,7 @@ dependencies = [ [[package]] name = "testing-framework-tools" version = "0.1.0" -source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=a35ec04869ec06bee2e4bf1af1ffebf8e7a815df#a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" +source = "git+https://github.com/logos-blockchain/logos-blockchain-testing.git?rev=8d87f69108685c6685857a8206d9bb9532b24f47#8d87f69108685c6685857a8206d9bb9532b24f47" [[package]] name = "textwrap" @@ -8407,9 +8487,9 @@ checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" [[package]] name = "unicode-width" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" [[package]] name = "unicode-xid" @@ -8417,16 +8497,6 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" -[[package]] -name = "universal-hash" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" -dependencies = [ - "crypto-common", - "subtle", -] - [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -8781,7 +8851,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index be593d7..ef4bcac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,31 +10,28 @@ keywords = ["blockchain", "privacy"] license = "MIT or Apache-2.0" readme = "README.md" repository = "https://github.com/logos-blockchain/logos-zone-sdk" -version = "0.0.0" +version = "0.3.6" [workspace.dependencies] async-trait = { default-features = false, version = "0.1" } bincode = { default-features = false, version = "1.3" } blake2 = { default-features = false, version = "0.10" } -chacha20poly1305 = { default-features = false, version = "0.10.1" } clap = { default-features = false, version = "4" } cucumber = { default-features = false, version = "=0.23.0" } flate2 = { default-features = false, version = "1.1.9" } futures = { default-features = false, version = "0.3.32" } hex = { default-features = false, version = "0.4" } -lb-binary-codec = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-binary-codec", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-common-http-client = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-common-http-client", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-core = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-core", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-groth16 = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-groth16", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-http-api-common = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-http-api-common", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-key-management-system-service = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-key-management-system-service", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-log-targets = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-log-targets", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-testing-framework = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "blockchain-test-logos", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-time = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-time", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -lb-utils = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-utils", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } +lb-binary-codec = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-binary-codec", tag = "0.3.6" } +lb-common-http-client = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-common-http-client", tag = "0.3.6" } +lb-core = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-core", tag = "0.3.6" } +lb-groth16 = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-groth16", tag = "0.3.6" } +lb-http-api-common = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-http-api-common", tag = "0.3.6" } +lb-key-management-system-service = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-key-management-system-service", tag = "0.3.6" } +lb-log-targets = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-log-targets", tag = "0.3.6" } +lb-testing-framework = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "testing-framework", tag = "0.3.6" } +lb-utils = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", package = "logos-blockchain-utils", tag = "0.3.6" } lb-zone-sdk = { default-features = false, package = "logos-blockchain-zone-sdk", path = "zone-sdk" } -logos-blockchain-tests = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", rev = "a159f8c155df68d7e2f045a418ae737de246354e" } -logos-blockchain-zone-tests = { default-features = false, path = "tests/zone-sdk" } +logos-blockchain-tests = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain.git", tag = "0.3.6" } logos-sql = { default-features = false, path = "logos_sql" } num-bigint = { default-features = false, version = "0.4" } rand = { default-features = false, version = "0.8" } @@ -47,10 +44,14 @@ tempfile = { default-features = false, version = "3" } testing-framework-app = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain-testing.git", rev = "a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" } testing-framework-core = { default-features = false, git = "https://github.com/logos-blockchain/logos-blockchain-testing.git", rev = "a35ec04869ec06bee2e4bf1af1ffebf8e7a815df" } thiserror = { default-features = false, version = "2.0" } -time = { default-features = false, version = "0.3" } tokio = { default-features = false, version = "1" } tracing = { default-features = false, version = "0.1" } -zeroize = { default-features = false, version = "1" } + +# The node's test harness runs its zone and Logos SQL suites against the +# libraries of this repository instead of the node's own copies. +[patch."https://github.com/logos-blockchain/logos-blockchain.git"] +logos-blockchain-zone-sdk = { path = "zone-sdk" } +logos-sql = { path = "logos_sql" } [profile.release] codegen-units = 1 # (Much) slower build time, much better performances diff --git a/logos_sql/Cargo.toml b/logos_sql/Cargo.toml index 00d4131..64f6384 100644 --- a/logos_sql/Cargo.toml +++ b/logos_sql/Cargo.toml @@ -24,17 +24,13 @@ lb-utils = { workspace = true } lb-zone-sdk = { workspace = true } rand = { features = ["getrandom"], workspace = true } reqwest = { workspace = true } -rusqlite = { features = ["backup", "bundled", "fallible_uint", "functions", "hooks", "limits"], workspace = true } +rusqlite = { features = ["backup", "bundled", "functions", "hooks"], workspace = true } thiserror = { workspace = true } tokio = { features = ["macros", "rt", "sync", "time"], workspace = true } tracing = { workspace = true } [dev-dependencies] -chacha20poly1305 = { features = ["alloc", "rand_core"], workspace = true } -clap = { features = ["derive", "env", "help", "std"], workspace = true } -hex = { workspace = true } -lb-core = { features = ["test-utils"], workspace = true } -lb-groth16 = { workspace = true } -tempfile = { workspace = true } -tokio = { features = ["io-util", "net"], workspace = true } -zeroize = { features = ["alloc"], workspace = true } +clap = { features = ["derive", "env", "help", "std"], workspace = true } +hex = { workspace = true } +lb-groth16 = { workspace = true } +tempfile = { workspace = true } diff --git a/logos_sql/examples/password_manager/config.rs b/logos_sql/examples/password_manager/config.rs index be99b8e..2a87c2e 100644 --- a/logos_sql/examples/password_manager/config.rs +++ b/logos_sql/examples/password_manager/config.rs @@ -6,19 +6,12 @@ use clap::Parser; use lb_groth16::fr_from_bytes; use lb_key_management_system_service::keys::{Ed25519Key, ZkPublicKey}; use lb_zone_sdk::{node_types::ChannelId, sequencer::FundingConfig}; -use logos_sql::{LogosSqlConfig, PublicationConfig, WriterConfig}; +use logos_sql::{LogosSqlConfig, WriterConfig}; use reqwest::Url; -use crate::{AppResult, encryption::Encryption}; - #[derive(Parser)] #[command(name = "password-manager")] struct Options { - /// File containing a separate random 32-byte encryption key (not a - /// password). - #[arg(long, env = "LOGOS_SQL_ENCRYPTION_KEY_FILE")] - encryption_key_file: PathBuf, - /// Channel carrying the shared password-manager writes. #[arg(long, env = "LOGOS_SQL_CHANNEL_ID", value_parser = parse_channel_id)] channel_id: ChannelId, @@ -53,16 +46,13 @@ struct Options { } /// Parses startup options and builds the `λSQL` configuration. -pub fn from_args() -> AppResult<(LogosSqlConfig, Encryption)> { +pub fn from_args() -> LogosSqlConfig { let options = Options::parse(); - let encryption = - Encryption::from_file(&options.encryption_key_file, options.channel_id.as_ref())?; - let config = LogosSqlConfig { + LogosSqlConfig { channel_id: options.channel_id, node_url: options.node_url, writer: Some(WriterConfig { - publication: PublicationConfig::default(), signing_key: options.signing_key, funding: FundingConfig { funding_pk: options.funding_key, @@ -72,9 +62,7 @@ pub fn from_args() -> AppResult<(LogosSqlConfig, Encryption)> { }, }), state_dir: options.state_dir, - }; - - Ok((config, encryption)) + } } fn parse_channel_id(value: &str) -> Result { diff --git a/logos_sql/examples/password_manager/encryption.rs b/logos_sql/examples/password_manager/encryption.rs deleted file mode 100644 index 7988dd7..0000000 --- a/logos_sql/examples/password_manager/encryption.rs +++ /dev/null @@ -1,157 +0,0 @@ -//! Encrypt fields before SQL sees them. The key never enters the database. - -use std::{fs::File, io::Read as _, path::Path}; - -use chacha20poly1305::{ - XChaCha20Poly1305, - aead::{Aead as _, AeadCore as _, KeyInit as _, Payload}, -}; -use rand::rngs::OsRng; -use zeroize::Zeroizing; - -use crate::{AppError, AppResult}; - -const NONCE_BYTES: usize = 24; - -pub struct Encryption { - cipher: XChaCha20Poly1305, - channel: String, -} - -impl Encryption { - pub fn from_file(path: &Path, channel: &[u8]) -> AppResult { - let file = File::open(path)?; - - let mut key = Zeroizing::new(Vec::new()); - file.take(33).read_to_end(&mut key)?; - - Self::new(&key, channel) - } - - fn new(key: &[u8], channel: &[u8]) -> AppResult { - let cipher = - XChaCha20Poly1305::new_from_slice(key).map_err(|_| AppError::InvalidEncryptionKey)?; - - Ok(Self { - cipher, - channel: hex::encode(channel), - }) - } - - pub fn encrypt(&self, label: &str, field: &str, value: &str) -> AppResult> { - let nonce = XChaCha20Poly1305::generate_nonce(&mut OsRng); - - let context = self.context(label, field); - let encrypted = self - .cipher - .encrypt( - &nonce, - Payload { - msg: value.as_bytes(), - aad: context.as_bytes(), - }, - ) - .map_err(|_| AppError::EncryptionFailed)?; - - // Each stored value carries its own nonce so another replica can decrypt it. - let mut stored = nonce.to_vec(); - stored.extend(encrypted); - - Ok(stored) - } - - pub fn decrypt(&self, label: &str, field: &str, stored: &[u8]) -> AppResult { - let (nonce, encrypted) = stored - .split_first_chunk::() - .ok_or(AppError::DecryptionFailed)?; - let context = self.context(label, field); - let plaintext = self - .cipher - .decrypt( - nonce.into(), - Payload { - msg: encrypted, - aad: context.as_bytes(), - }, - ) - .map_err(|_| AppError::DecryptionFailed)?; - - String::from_utf8(plaintext).map_err(|_| AppError::DecryptionFailed) - } - - fn context(&self, label: &str, field: &str) -> String { - // Binding ciphertext to its channel, field and label prevents moving it - // to another entry or interpreting encrypted notes as a password. - format!( - "logos-sql-password-manager:v1:{}:{field}:{label}", - self.channel - ) - } -} - -#[cfg(test)] -mod tests { - use std::io::Write as _; - - use tempfile::NamedTempFile; - - use super::*; - - #[test] - fn key_file_can_be_reopened_but_must_have_the_right_length() { - let mut file = NamedTempFile::new().unwrap(); - file.write_all(&[1; 32]).unwrap(); - let writer = Encryption::from_file(file.path(), &[2; 32]).unwrap(); - let stored = writer.encrypt("email", "notes", "").unwrap(); - let reader = Encryption::from_file(file.path(), &[2; 32]).unwrap(); - - assert_eq!(reader.decrypt("email", "notes", &stored).unwrap(), ""); - - file.write_all(&[0]).unwrap(); - assert!(matches!( - Encryption::from_file(file.path(), &[2; 32]), - Err(AppError::InvalidEncryptionKey) - )); - file.as_file().set_len(31).unwrap(); - assert!(matches!( - Encryption::from_file(file.path(), &[2; 32]), - Err(AppError::InvalidEncryptionKey) - )); - } - - #[test] - fn replicas_decrypt_fields_but_reject_the_wrong_key_or_context() { - let writer = Encryption::new(&[1; 32], &[2; 32]).unwrap(); - let reader = Encryption::new(&[1; 32], &[2; 32]).unwrap(); - let stored = writer.encrypt("email", "password", "secret").unwrap(); - - assert_eq!( - reader.decrypt("email", "password", &stored).unwrap(), - "secret" - ); - assert_ne!( - stored, - writer.encrypt("email", "password", "secret").unwrap() - ); - assert!(reader.decrypt("other", "password", &stored).is_err()); - assert!(reader.decrypt("email", "notes", &stored).is_err()); - assert!( - Encryption::new(&[3; 32], &[2; 32]) - .unwrap() - .decrypt("email", "password", &stored) - .is_err() - ); - assert!( - Encryption::new(&[1; 32], &[3; 32]) - .unwrap() - .decrypt("email", "password", &stored) - .is_err() - ); - - let mut corrupted = stored; - corrupted[NONCE_BYTES] ^= 1; - assert!(reader.decrypt("email", "password", &corrupted).is_err()); - assert!(reader.decrypt("email", "password", &[0; 24]).is_err()); - assert!(reader.decrypt("email", "password", &[0; 23]).is_err()); - } -} diff --git a/logos_sql/examples/password_manager/main.rs b/logos_sql/examples/password_manager/main.rs index 63c38f5..901532d 100644 --- a/logos_sql/examples/password_manager/main.rs +++ b/logos_sql/examples/password_manager/main.rs @@ -1,16 +1,8 @@ //! A password manager built on `λSQL`. //! -//! Accounts, passwords and notes are encrypted before being passed to `λSQL`. -//! Labels and write activity remain public. This is still a demonstration, -//! not an audited password manager; use test credentials only. -//! -//! Generate a separate encryption key with `umask 077; openssl rand -out -//! vault.key 32` and pass `--encryption-key-file vault.key`. Keep this file -//! private and backed up, and copy it securely to each app instance using the -//! same channel. Losing the key means losing access to the encrypted data. Do -//! not reuse the channel signing key. Key rotation and plaintext-data migration -//! are not supported. Use a fresh channel: encrypting new writes cannot hide -//! old plaintext history. +//! This first version stores and replicates passwords in plaintext. It exists +//! only to demonstrate the `λSQL` application API. Do not enter real +//! credentials. Application-side encryption will be added in a follow-up. //! //! Run with `--help` to see startup configuration. Each option also accepts its //! corresponding `LOGOS_SQL_*` environment variable. After startup, enter @@ -39,8 +31,7 @@ //! successful retry marks that displacement handled; a failed retry leaves it //! available for review. //! -//! Password edits check the stored password ciphertext, and notes edits check -//! the stored notes ciphertext. Retries reuse those exact encrypted values. +//! Password edits check the old password, and notes edits check the old notes. //! Changing notes does not prevent a password retry, but changing the password //! does. Deletes only remove an entry if its account, password, and notes still //! match what the app read when preparing the command. This prevents a retried @@ -58,13 +49,14 @@ //! Marking a displacement handled clears it from the review list. //! It does not change the database or channel history. //! -//! An old add can succeed again after deletion. This is why retries are a user -//! decision, not a background loop. +//! These checks compare values, not edit +//! history: an old add can succeed again after deletion, and a condition can +//! pass again if a value changes back. This is why retries are a user decision, +//! not a background loop. //! //! Use a fresh channel and state directory for this example. mod config; -mod encryption; mod passwords; mod repl; @@ -97,21 +89,11 @@ enum AppError { #[error("displacement not listed; run `displacements` to review it first")] DisplacementNotListed, - - #[error("encryption key file must contain exactly 32 random bytes")] - InvalidEncryptionKey, - - #[error("could not encrypt credential")] - EncryptionFailed, - - #[error("could not decrypt credential: incorrect key or modified data")] - DecryptionFailed, } #[tokio::main(flavor = "current_thread")] async fn main() -> AppResult<()> { - let (config, encryption) = config::from_args()?; - let manager = PasswordManager::start(config, encryption).await?; + let manager = PasswordManager::start(config::from_args()).await?; let result = repl::run(&manager).await; diff --git a/logos_sql/examples/password_manager/passwords.rs b/logos_sql/examples/password_manager/passwords.rs index 54d70d1..7a6c973 100644 --- a/logos_sql/examples/password_manager/passwords.rs +++ b/logos_sql/examples/password_manager/passwords.rs @@ -10,7 +10,7 @@ use logos_sql::{ }; use rusqlite::OptionalExtension as _; -use crate::{AppError, AppResult, encryption::Encryption}; +use crate::{AppError, AppResult}; const SELECT_CREDENTIALS: &str = " SELECT label, account @@ -28,29 +28,24 @@ fn prepare_schema() -> TransactionBuilder { TransactionBuilder::new( "CREATE TABLE IF NOT EXISTS credentials ( label TEXT PRIMARY KEY, - account BLOB NOT NULL, - password BLOB NOT NULL, - notes BLOB NOT NULL + account TEXT NOT NULL, + password TEXT NOT NULL, + notes TEXT NOT NULL DEFAULT '' )", ) } -fn prepare_credential_insert(credential: &StoredCredential) -> TransactionBuilder { +fn prepare_credential_insert(label: &str, account: &str, password: &str) -> TransactionBuilder { TransactionBuilder::new( - "INSERT INTO credentials (label, account, password, notes) - VALUES (?1, ?2, ?3, ?4)", + "INSERT INTO credentials (label, account, password) + VALUES (?1, ?2, ?3)", ) - .bind(&credential.label) - .bind(credential.account.as_slice()) - .bind(credential.password.as_slice()) - .bind(credential.notes.as_slice()) + .bind(label) + .bind(account) + .bind(password) } -fn prepare_password_update( - label: &str, - password: &[u8], - old_password: &[u8], -) -> TransactionBuilder { +fn prepare_password_update(label: &str, password: &str, old_password: &str) -> TransactionBuilder { TransactionBuilder::new( "UPDATE credentials SET password = ?2 WHERE label = ?1 AND password = ?3", @@ -60,7 +55,7 @@ fn prepare_password_update( .bind(old_password) } -fn prepare_notes_update(label: &str, notes: &[u8], old_notes: &[u8]) -> TransactionBuilder { +fn prepare_notes_update(label: &str, notes: &str, old_notes: &str) -> TransactionBuilder { TransactionBuilder::new( "UPDATE credentials SET notes = ?2 WHERE label = ?1 AND notes = ?3", @@ -70,21 +65,21 @@ fn prepare_notes_update(label: &str, notes: &[u8], old_notes: &[u8]) -> Transact .bind(old_notes) } -fn prepare_credential_delete(credential: &StoredCredential) -> TransactionBuilder { +fn prepare_credential_delete(credential: &Credential) -> TransactionBuilder { TransactionBuilder::new( "DELETE FROM credentials WHERE label = ?1 AND account = ?2 AND password = ?3 AND notes = ?4", ) .bind(&credential.label) - .bind(credential.account.as_slice()) - .bind(credential.password.as_slice()) - .bind(credential.notes.as_slice()) + .bind(&credential.account) + .bind(&credential.password) + .bind(&credential.notes) } /// One credential in the current local database view. /// -/// Fields are decrypted for display. Do not use this example with real -/// credentials. +/// Passwords are deliberately plaintext in this first example slice. Do not +/// use this example with real credentials. #[derive(Debug)] pub struct Credential { pub label: String, @@ -93,25 +88,7 @@ pub struct Credential { pub notes: String, } -struct StoredCredential { - label: String, - account: Vec, - password: Vec, - notes: Vec, -} - -impl StoredCredential { - fn decrypt(&self, encryption: &Encryption) -> AppResult { - Ok(Credential { - label: self.label.clone(), - account: encryption.decrypt(&self.label, "account", &self.account)?, - password: encryption.decrypt(&self.label, "password", &self.password)?, - notes: encryption.decrypt(&self.label, "notes", &self.notes)?, - }) - } -} - -/// Credential labels and decrypted accounts shown when listing the database. +/// Non-secret credential information shown when listing the database. #[derive(Debug)] pub struct CredentialSummary { pub label: String, @@ -129,39 +106,27 @@ pub struct DisplacementForReview { /// Application-facing password-manager operations over one `λSQL` database. pub struct PasswordManager { logos_sql: LogosSql, - encryption: Encryption, } impl PasswordManager { /// Starts `λSQL`, catches up with the channel, and prepares the database. - pub async fn start(config: LogosSqlConfig, encryption: Encryption) -> AppResult { + pub async fn start(config: LogosSqlConfig) -> AppResult { let manager = Self { logos_sql: LogosSql::start(config).await?, - encryption, }; match manager.initialize().await { // Keep the terminal available if schema creation was displaced. - Ok(()) | Err(AppError::LogosSql(LogosSqlError::UnhandledDisplacements)) => {} - Err(error) => { - manager.shutdown().await?; - return Err(error); - } + Ok(()) | Err(LogosSqlError::UnhandledDisplacements) => {} + Err(error) => return Err(error.into()), } Ok(manager) } - async fn initialize(&self) -> AppResult<()> { + async fn initialize(&self) -> Result<(), LogosSqlError> { if !self.schema_exists()? { self.logos_sql.execute(prepare_schema()).await?; - - return Ok(()); - } - - // Check the key against existing entries before accepting new writes. - for credential in self.credentials()? { - self.credential(&credential.label)?; } Ok(()) @@ -180,28 +145,20 @@ impl PasswordManager { /// Concurrent attempts to use the same label produce a primary-key /// conflict. pub async fn add(&self, label: String, account: String, password: String) -> AppResult { - let credential = StoredCredential { - account: self.encryption.encrypt(&label, "account", &account)?, - password: self.encryption.encrypt(&label, "password", &password)?, - notes: self.encryption.encrypt(&label, "notes", "")?, - label, - }; - + // TODO(security): Encrypt the password before it enters `λSQL`. The + // resulting ciphertext, salt, and nonce should be bound instead. Ok(self .logos_sql - .execute(prepare_credential_insert(&credential)) + .execute(prepare_credential_insert(&label, &account, &password)) .await?) } /// Replaces the password only if it still matches the value read here. pub async fn update_password(&self, label: String, password: String) -> AppResult { let credential = self - .stored_credential(&label)? + .credential(&label)? .ok_or(AppError::CredentialNotFound)?; - credential.decrypt(&self.encryption)?; - let password = self.encryption.encrypt(&label, "password", &password)?; - Ok(self .logos_sql .execute(prepare_password_update( @@ -215,12 +172,9 @@ impl PasswordManager { /// Updates notes independently of password changes. pub async fn update_notes(&self, label: String, notes: String) -> AppResult { let credential = self - .stored_credential(&label)? + .credential(&label)? .ok_or(AppError::CredentialNotFound)?; - credential.decrypt(&self.encryption)?; - let notes = self.encryption.encrypt(&label, "notes", ¬es)?; - Ok(self .logos_sql .execute(prepare_notes_update(&label, ¬es, &credential.notes)) @@ -230,11 +184,9 @@ impl PasswordManager { /// Removes a credential only if all its values still match the read. pub async fn remove(&self, label: String) -> AppResult { let credential = self - .stored_credential(&label)? + .credential(&label)? .ok_or(AppError::CredentialNotFound)?; - credential.decrypt(&self.encryption)?; - Ok(self .logos_sql .execute(prepare_credential_delete(&credential)) @@ -243,12 +195,6 @@ impl PasswordManager { /// Reads one credential from the local `SQLite` database. pub fn credential(&self, label: &str) -> AppResult> { - self.stored_credential(label)? - .map(|credential| credential.decrypt(&self.encryption)) - .transpose() - } - - fn stored_credential(&self, label: &str) -> AppResult> { if !self.schema_exists()? { return Ok(None); } @@ -262,7 +208,7 @@ impl PasswordManager { Ok(statement .query_row([label], |row| { - Ok(StoredCredential { + Ok(Credential { label: row.get("label")?, account: row.get("account")?, password: row.get("password")?, @@ -281,20 +227,14 @@ impl PasswordManager { let connection = self.logos_sql.read_connection()?; let mut statement = connection.prepare(SELECT_CREDENTIALS)?; - let rows = statement.query_map([], |row| { - Ok(( - row.get::<_, String>("label")?, - row.get::<_, Vec>("account")?, - )) - })?; - - rows.map(|row| { - let (label, stored) = row?; - let account = self.encryption.decrypt(&label, "account", &stored)?; - - Ok(CredentialSummary { label, account }) - }) - .collect() + Ok(statement + .query_map([], |row| { + Ok(CredentialSummary { + label: row.get("label")?, + account: row.get("account")?, + }) + })? + .collect::, _>>()?) } /// Retries the saved SQL with its original conditions, not new reads. diff --git a/logos_sql/examples/password_manager/repl.rs b/logos_sql/examples/password_manager/repl.rs index bedf8f3..d2928e1 100644 --- a/logos_sql/examples/password_manager/repl.rs +++ b/logos_sql/examples/password_manager/repl.rs @@ -76,7 +76,7 @@ impl Command { /// Reads commands from the terminal until the user exits or input closes. pub async fn run(manager: &PasswordManager) -> AppResult<()> { println!("Password manager is running. Enter `help` to list commands."); - println!("Credentials are encrypted; labels remain public. Use test credentials only."); + println!("WARNING: passwords are replicated in plaintext; do not enter real credentials."); let mut reported = Vec::new(); diff --git a/logos_sql/src/applier.rs b/logos_sql/src/applier.rs index 7a02101..0af797b 100644 --- a/logos_sql/src/applier.rs +++ b/logos_sql/src/applier.rs @@ -9,9 +9,9 @@ use lb_zone_sdk::{ }; use crate::{ - db::{Databases, ObservedWrite, PendingPublish}, + db::{Databases, PendingPublish, SuffixWrite}, error::Error, - protocol::{self, ChannelBatch, ChannelWrite, TxId}, + protocol::{self, ChannelInscription, TxId}, }; const TARGET: &str = lb_log_targets::logos_sql::APPLIER; @@ -35,11 +35,9 @@ enum RebuildCause { /// transaction automatically, so the rebuild removes its effect and /// reports it as displaced. This also resumes a rebuild interrupted after /// the displacement was recorded. - PendingWriteInvalidated(Vec), + PendingWriteInvalidated(PendingPublish), /// Canonical channel history changed and `LIVE.db` must drop orphaned SQL. ChannelFork, - /// A previous rebuild stopped before its checkpoint was saved. - Interrupted, } /// Selects how one block event is reflected in `LIVE.db`. @@ -52,7 +50,7 @@ enum ApplicationPlan { } impl ApplyTarget { - fn apply(self, db: &mut Databases, write: &ChannelWrite) -> Result<(), Error> { + fn apply(self, db: &mut Databases, write: &ChannelInscription) -> Result<(), Error> { match self { Self::Live => db.apply_adopted_write(write), Self::Lib => db.apply_finalized_write_to_lib(write), @@ -78,19 +76,13 @@ struct SqlChanges { /// block. /// Finalized writes apply to both finalized and live state, while newly /// adopted writes apply only to live state. A branch change reconstructs live -/// state from finalized history and the SDK's current channel history. -/// Returns an unfinished rebuild when the event does not contain the history -/// needed to reconstruct LIVE. The runtime supplies that history from the SDK. +/// state from finalized history and the retained canonical suffix. /// /// # Errors /// /// Returns an error if SQL cannot be applied, a channel payload cannot be /// decoded, or the checkpoint cannot be persisted. -pub fn on_event<'a>( - db: &mut Databases, - event: &'a Event, - channel_id: ChannelId, -) -> Result>, Error> { +pub fn on_event(db: &mut Databases, event: &Event, channel_id: ChannelId) -> Result<(), Error> { match event { Event::BlocksProcessed { checkpoint, @@ -100,9 +92,9 @@ pub fn on_event<'a>( } => process_blocks(db, checkpoint, channel_update, finalized, channel_id), Event::Ready => { tracing::info!(target: TARGET, "sequencer ready"); - Ok(None) + Ok(()) } - Event::TurnNotification { .. } => Ok(None), + Event::TurnNotification { .. } => Ok(()), } } @@ -110,13 +102,13 @@ pub fn on_event<'a>( /// /// If any state change fails, the checkpoint remains behind and `ZoneSDK` /// delivers the same channel position again after retry or restart. -fn process_blocks<'a>( +fn process_blocks( db: &mut Databases, - checkpoint: &'a SequencerCheckpoint, + checkpoint: &SequencerCheckpoint, channel_update: &ChannelUpdate, finalized: &[FinalizedTx], channel_id: ChannelId, -) -> Result>, Error> { +) -> Result<(), Error> { let changes = SqlChanges::from_block(channel_update, finalized, channel_id); tracing::debug!( @@ -130,54 +122,13 @@ fn process_blocks<'a>( let plan = changes.application_plan(db)?; match plan { - ApplicationPlan::ApplyChanges => { - changes.apply(db)?; - db.persist_checkpoint(checkpoint)?; - Ok(None) - } - ApplicationPlan::Rebuild(cause) => { - changes.prepare_rebuild(db, &cause)?; - - // Conflict events already contain the complete surviving history. - if let Some(history) = channel_update.canonical_chain() { - let history = history.cloned().collect::>(); - changes.finish_rebuild(db, &history, channel_id, checkpoint)?; - Ok(None) - } else { - Ok(Some(PendingRebuild { - changes, - checkpoint, - channel_id, - })) - } - } + ApplicationPlan::ApplyChanges => changes.apply(db)?, + ApplicationPlan::Rebuild(cause) => changes.rebuild(db, &cause)?, } -} -/// A rebuild already recorded in the database, waiting for channel history. -pub struct PendingRebuild<'a> { - changes: SqlChanges, - checkpoint: &'a SequencerCheckpoint, - channel_id: ChannelId, -} + db.persist_checkpoint(checkpoint)?; -impl PendingRebuild<'_> { - /// Finish with the SDK's current history. During finalized backfill it may - /// be unavailable; save progress but leave the rebuild flag set for later. - pub fn finish( - self, - db: &mut Databases, - history: Option>, - ) -> Result<(), Error> { - let Some(history) = history else { - self.changes - .update_write_statuses(db, &self.changes.adopted)?; - return db.persist_checkpoint(self.checkpoint); - }; - - self.changes - .finish_rebuild(db, &history, self.channel_id, self.checkpoint) - } + Ok(()) } impl SqlChanges { @@ -237,23 +188,22 @@ impl SqlChanges { /// optimistic local effect must move or disappear, or canonical history /// removes an effect that is already present in `LIVE.db`. fn application_plan(&self, db: &Databases) -> Result { - let pending = db.pending_writes()?; - - if db.rebuild_required()? { - // The previous rebuild may have stopped before recording the - // pending write's displacement or replacing LIVE. - let cause = if pending.is_empty() { - RebuildCause::Interrupted - } else { - RebuildCause::PendingWriteInvalidated(pending) - }; - return Ok(ApplicationPlan::Rebuild(cause)); - } + if let Some(pending) = db.pending_publish()? { + // The rebuild records the displacement first, then replaces + // `LIVE.db`, which removes its pending-write record. If both records + // still exist, the process stopped between those two steps and the + // rebuild must resume. + if db.pending_write_rebuild_was_interrupted(pending.tx_id)? { + return Ok(ApplicationPlan::Rebuild( + RebuildCause::PendingWriteInvalidated(pending), + )); + } - if !pending.is_empty() && self.changes_history_before_pending_write(db)? { - return Ok(ApplicationPlan::Rebuild( - RebuildCause::PendingWriteInvalidated(pending), - )); + if self.changes_history_before_pending_write(db)? { + return Ok(ApplicationPlan::Rebuild( + RebuildCause::PendingWriteInvalidated(pending), + )); + } } if !self.orphaned.is_empty() { @@ -266,24 +216,18 @@ impl SqlChanges { /// Reports whether this event changed the history below a pending local /// write. /// - /// Orphans change the base. Adopting or finalizing a write already in the - /// live database does not: its effects preceded the queued local writes. + /// Adoptions and orphans always change that base. Finalizing a write + /// already retained in the live suffix only moves the finalized + /// boundary and does not invalidate the pending write's execution + /// order. fn changes_history_before_pending_write(&self, db: &Databases) -> Result { - if !self.orphaned.is_empty() { + if !self.adopted.is_empty() || !self.orphaned.is_empty() { return Ok(true); } - for inscription in self.adopted.iter().chain(&self.finalized) { - match ChannelBatch::decode(inscription.payload.as_ref()) { - Ok(batch) => { - for write in batch.into_writes() { - if !db.live_processed_write(inscription.this_msg, &write)? { - return Ok(true); - } - } - } - Err(error) if is_rejected_write(&error) => {} - Err(error) => return Err(error), + for inscription in &self.finalized { + if !db.live_suffix_contains(inscription.this_msg)? { + return Ok(true); } } @@ -295,40 +239,27 @@ impl SqlChanges { fn apply(&self, db: &mut Databases) -> Result<(), Error> { Self::apply_inscriptions(db, &self.finalized, ApplyTarget::LibAndLive)?; Self::apply_inscriptions(db, &self.adopted, ApplyTarget::Live)?; - self.update_write_statuses(db, &self.adopted) + self.apply_history_delta(db) } - /// Records displacement and advances finalized state before rebuilding - /// LIVE. + /// Records displacement, advances finalized state, and reconstructs LIVE. /// - /// Mark the rebuild before changing either database. Restart can then - /// repeat it using the SDK's history, even if the next event is not a fork. - fn prepare_rebuild(&self, db: &mut Databases, cause: &RebuildCause) -> Result<(), Error> { - db.require_rebuild()?; - + /// The displacement, when applicable, and suffix are persisted before + /// replacing `LIVE.db` so an interrupted rebuild can be recognized and + /// safely repeated. + fn rebuild(&self, db: &mut Databases, cause: &RebuildCause) -> Result<(), Error> { match cause { RebuildCause::PendingWriteInvalidated(pending) => { - for write in pending { - db.record_pending_write_displacement(write)?; - } + db.record_pending_write_displacement(pending)?; } - RebuildCause::ChannelFork | RebuildCause::Interrupted => {} + RebuildCause::ChannelFork => {} } - Self::apply_inscriptions(db, &self.finalized, ApplyTarget::Lib) - } + Self::apply_inscriptions(db, &self.finalized, ApplyTarget::Lib)?; + self.apply_history_delta(db)?; + rebuild_live_from_suffix(db)?; - fn finish_rebuild( - &self, - db: &mut Databases, - history: &[ChannelUpdateTx], - channel_id: ChannelId, - checkpoint: &SequencerCheckpoint, - ) -> Result<(), Error> { - let inscriptions = Self::collect_channel_inscriptions(history, channel_id); - self.update_write_statuses(db, &inscriptions)?; - rebuild_live(db, &inscriptions)?; - db.persist_rebuilt_checkpoint(checkpoint) + Ok(()) } fn apply_inscriptions( @@ -343,69 +274,74 @@ impl SqlChanges { Ok(()) } - /// Updates our writes' statuses without retaining foreign channel history. - fn update_write_statuses( - &self, - db: &mut Databases, - adopted: &[InscriptionInfo], - ) -> Result<(), Error> { + /// Applies this event to the replayable suffix and local write statuses. + fn apply_history_delta(&self, db: &mut Databases) -> Result<(), Error> { let finalized = self .finalized .iter() .map(|inscription| inscription.this_msg) .collect::>(); - let orphaned = Self::observe_writes(db, &self.orphaned)?; - let adopted = Self::observe_writes(db, adopted)?; + let orphaned = self + .orphaned + .iter() + .map(|inscription| inscription.this_msg) + .collect::>(); + let adopted = self.collect_adopted_suffix(db)?; - db.update_write_statuses(&finalized, &orphaned, &adopted) + db.apply_history_delta(&finalized, &orphaned, &adopted) } - fn observe_writes( - db: &Databases, - inscriptions: &[InscriptionInfo], - ) -> Result, Error> { + /// Prepares newly adopted writes for durable replay during a later rebuild. + fn collect_adopted_suffix(&self, db: &Databases) -> Result, Error> { let mut writes = Vec::new(); - for inscription in inscriptions { + for inscription in &self.adopted { let payload = inscription.payload.as_ref(); - let batch = match ChannelBatch::decode(payload) { - Ok(batch) => batch, + let write = match ChannelInscription::decode(payload) { + Ok(write) => write, Err(error) => { handle_write_error(db, inscription, None, error)?; continue; } }; - for write in batch.into_writes() { - writes.push(ObservedWrite { - this_msg: inscription.this_msg, - tx_id: write.tx_id, - payload: write.encode_stored()?, - }); - } + writes.push(SuffixWrite { + this_msg: inscription.this_msg, + tx_id: write.tx_id, + payload: payload.to_vec(), + local: false, + }); } Ok(writes) } } -/// Copies finalized state, then replays the SDK's surviving channel history. -fn rebuild_live(db: &mut Databases, history: &[InscriptionInfo]) -> Result<(), Error> { +/// Reconstructs `LIVE.db` as finalized state followed by the canonical suffix. +/// +/// Stored payloads are local durable state: malformed or mismatched records +/// indicate corruption. Deterministically rejected SQL is recorded and skipped +/// exactly as it is during normal channel application. +fn rebuild_live_from_suffix(db: &mut Databases) -> Result<(), Error> { + let suffix = db.live_suffix()?; let mut rebuild = db.begin_live_rebuild()?; - for inscription in history { - let batch = match ChannelBatch::decode(inscription.payload.as_ref()) { - Ok(write) => write, - Err(error) => { - handle_write_error(db, inscription, None, error)?; - continue; - } - }; + for retained in suffix { + let write = ChannelInscription::decode(&retained.payload) + .map_err(|_| Error::InvalidLocalState("stored live suffix payload is malformed"))?; + + if write.tx_id != retained.tx_id { + return Err(Error::InvalidLocalState( + "stored live suffix transaction id does not match its payload", + )); + } - for write in batch.into_writes() { - if let Err(error) = rebuild.apply_write(&write) { - handle_write_error(db, inscription, Some(write.tx_id), error)?; + match rebuild.apply_write(&write) { + Ok(()) => {} + Err(error) if is_rejected_write(&error) => { + db.record_rejected_write(retained.this_msg, Some(write.tx_id), &error.to_string())?; } + Err(error) => return Err(error), } } @@ -427,26 +363,23 @@ fn apply_inscription( ) -> Result<(), Error> { let payload = inscription.payload.as_ref(); - let batch = match ChannelBatch::decode(payload) { - Ok(batch) => batch, + let write = match ChannelInscription::decode(payload) { + Ok(write) => write, Err(error) => return handle_write_error(db, inscription, None, error), }; - for write in batch.into_writes() { - if let Err(error) = target.apply(db, &write) { - handle_write_error(db, inscription, Some(write.tx_id), error)?; - continue; - } - - tracing::debug!( - target: TARGET, - tx_id = ?write.tx_id, - statements = write.transaction.statements().len(), - ?target, - "channel write processed" - ); + if let Err(error) = target.apply(db, &write) { + return handle_write_error(db, inscription, Some(write.tx_id), error); } + tracing::debug!( + target: TARGET, + tx_id = ?write.tx_id, + statements = write.transaction.statements().len(), + ?target, + "channel write processed" + ); + Ok(()) } @@ -487,10 +420,7 @@ fn record_rejection( } const fn is_rejected_write(error: &Error) -> bool { - matches!( - error, - Error::InvalidPayload(_) | Error::RejectedSql(_) | Error::ExecutionBudgetExceeded - ) + matches!(error, Error::InvalidPayload(_) | Error::RejectedSql(_)) } fn is_logos_sql_inscription(inscription: &InscriptionInfo) -> bool { @@ -501,7 +431,7 @@ fn is_logos_sql_inscription(inscription: &InscriptionInfo) -> bool { mod tests { use lb_zone_sdk::{ UnverifiedEd25519PublicKey, - node_types::{ChannelId, HeaderId, Inscription, MsgId, Slot, TxHash}, + node_types::{ChannelId, HeaderId, MsgId, Slot, TxHash}, sequencer::{ ChannelUpdate, ChannelUpdateTx, Event, FinalizedOp, FinalizedTx, InscriptionInfo, SequencerCheckpoint, @@ -510,253 +440,18 @@ mod tests { use rusqlite::types::Value; use tempfile::TempDir; + use super::on_event; use crate::{ - PublicationConfig, - db::{ - Databases, - tests::{MAX_INSCRIPTION_SIZE, open_databases}, - }, + db::{Databases, SuffixWrite}, protocol::{ - CapturedFunctionCalls, ChannelBatch, ChannelWrite, EncodedWrite, PAYLOAD_MARKER, - Statement, Transaction, TxId, + CapturedFunctionCalls, ChannelInscription, EncodedWrite, PAYLOAD_MARKER, Statement, + Transaction, TxId, }, - publication::Publication, status::WriteStatus, }; const CHANNEL_ID: [u8; 32] = [9; 32]; - #[test] - fn a_failed_transaction_does_not_rollback_other_batch_members() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let sql = [ - "CREATE TABLE items(value INTEGER PRIMARY KEY)", - "INSERT INTO items VALUES (1)", - "INSERT INTO items VALUES (2); INSERT INTO missing VALUES (1)", - "INSERT INTO items VALUES (1)", - "INSERT INTO items VALUES (3)", - ]; - let writes = sql - .into_iter() - .map(|sql| ChannelWrite { - tx_id: TxId::generate(), - transaction: Transaction::new( - sql.split(';') - .map(|sql| Statement::new(sql.to_owned(), Vec::new()).unwrap()) - .collect(), - ) - .unwrap(), - captured_function_calls: CapturedFunctionCalls::empty(), - }) - .collect(); - let payload = ChannelBatch::new(writes) - .unwrap() - .encode(MAX_INSCRIPTION_SIZE) - .unwrap(); - let event = blocks_processed( - checkpoint(1, 1), - vec![ChannelUpdateTx::Inscription(inscription(&payload, 1))], - Vec::new(), - Vec::new(), - ); - - on_event(&mut db, &event, ChannelId::from(CHANNEL_ID)).unwrap(); - on_event(&mut db, &event, ChannelId::from(CHANNEL_ID)).unwrap(); - - let connection = Databases::open_reader(db.live_path()).unwrap(); - let values: Vec = connection - .prepare("SELECT value FROM items ORDER BY value") - .unwrap() - .query_map([], |row| row.get(0)) - .unwrap() - .collect::>() - .unwrap(); - - assert_eq!(values, vec![1, 3]); - assert_eq!(db.rejected_write_count().unwrap(), 2); - - let event = blocks_processed( - checkpoint(2, 2), - Vec::new(), - Vec::new(), - vec![finalized(&payload, 1)], - ); - on_event(&mut db, &event, ChannelId::from(CHANNEL_ID)).unwrap(); - assert_eq!(item_count(db.lib_path()), 2); - } - - #[test] - fn batched_writes_are_displaced_restored_and_finalized_together() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let first = TxId::from([2; 32]); - let second = TxId::from([1; 32]); - - db.commit_local_write( - first, - &transaction("CREATE TABLE items(value INTEGER)", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - db.commit_local_write( - second, - &transaction("INSERT INTO items VALUES (random())", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - let original: i64 = Databases::open_reader(db.live_path()) - .unwrap() - .query_row("SELECT value FROM items", [], |row| row.get(0)) - .unwrap(); - let publication = Publication::prepare( - db.pending_writes().unwrap(), - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - db.complete_batch(&checkpoint(1, 1), MsgId::from([1; 32]), &publication.writes) - .unwrap(); - - let orphan = blocks_processed( - checkpoint(2, 2), - Vec::new(), - vec![ChannelUpdateTx::Inscription(inscription( - &publication.payload, - 1, - ))], - Vec::new(), - ); - on_event(&mut db, &orphan, ChannelId::from(CHANNEL_ID)).unwrap(); - - assert!(!table_exists(db.live_path(), "items")); - let displaced: Vec<_> = db - .unhandled_displacements() - .unwrap() - .into_iter() - .map(|write| write.tx_id) - .collect(); - assert_eq!(displaced, vec![first, second]); - drop(db); - - let mut db = open_databases(dir.path()).unwrap(); - let restore = blocks_processed( - checkpoint(3, 3), - vec![ChannelUpdateTx::Inscription(inscription( - &publication.payload, - 1, - ))], - Vec::new(), - Vec::new(), - ); - on_event(&mut db, &restore, ChannelId::from(CHANNEL_ID)).unwrap(); - - assert_eq!(item_count(db.live_path()), 1); - let restored: i64 = Databases::open_reader(db.live_path()) - .unwrap() - .query_row("SELECT value FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(restored, original); - assert_eq!(db.unhandled_displacements().unwrap(), Vec::new()); - assert_eq!(db.write_status(first).unwrap(), Some(WriteStatus::Live)); - assert_eq!(db.write_status(second).unwrap(), Some(WriteStatus::Live)); - - let finish = blocks_processed( - checkpoint(4, 4), - Vec::new(), - Vec::new(), - vec![finalized(&publication.payload, 1)], - ); - on_event(&mut db, &finish, ChannelId::from(CHANNEL_ID)).unwrap(); - - assert_eq!(item_count(db.lib_path()), 1); - assert_eq!( - db.write_status(first).unwrap(), - Some(WriteStatus::Finalized) - ); - assert_eq!( - db.write_status(second).unwrap(), - Some(WriteStatus::Finalized) - ); - assert_eq!(db.unhandled_displacements().unwrap(), Vec::new()); - } - - #[test] - fn a_changed_base_displaces_every_queued_write() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let first = TxId::generate(); - let second = TxId::generate(); - db.commit_local_write( - first, - &transaction("CREATE TABLE items(value INTEGER)", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - db.commit_local_write( - second, - &transaction("INSERT INTO items VALUES (1)", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - - let foreign = encoded_write(&transaction( - "CREATE TABLE foreign_data(value INTEGER)", - Vec::new(), - )); - let event = blocks_processed( - checkpoint(1, 1), - vec![ChannelUpdateTx::Inscription(inscription( - &foreign.payload, - 1, - ))], - Vec::new(), - Vec::new(), - ); - on_event(&mut db, &event, ChannelId::from(CHANNEL_ID)).unwrap(); - - assert!(db.pending_writes().unwrap().is_empty()); - assert!(!table_exists(db.live_path(), "items")); - assert!(table_exists(db.live_path(), "foreign_data")); - let displaced: Vec<_> = db - .unhandled_displacements() - .unwrap() - .into_iter() - .map(|write| write.tx_id) - .collect(); - assert_eq!(displaced, vec![first, second]); - } - - // Extension fixtures use their adopted writes as the complete SDK history. - // Tests with earlier surviving writes supply an explicit history instead. - fn on_event( - db: &mut Databases, - event: &Event, - channel_id: ChannelId, - ) -> Result<(), crate::Error> { - let history = match event { - Event::BlocksProcessed { channel_update, .. } => { - Some(channel_update.adopted().to_vec()) - } - _ => None, - }; - apply_with_history(db, event, channel_id, history) - } - - fn apply_with_history( - db: &mut Databases, - event: &Event, - channel_id: ChannelId, - history: Option>, - ) -> Result<(), crate::Error> { - if let Some(rebuild) = super::on_event(db, event, channel_id)? { - rebuild.finish(db, history)?; - } - - Ok(()) - } - fn checkpoint(byte: u8, slot: u64) -> SequencerCheckpoint { SequencerCheckpoint { last_msg_id: MsgId::root(), @@ -774,7 +469,10 @@ mod tests { tx_hash: TxHash::from([byte; 32]), parent_msg: MsgId::root(), this_msg: MsgId::from([byte; 32]), - payload: Inscription::new_unchecked(payload.to_vec()), + payload: payload + .to_vec() + .try_into() + .expect("test payload should fit an inscription"), signer: Some(UnverifiedEd25519PublicKey::from_bytes(&[0u8; 32]).unwrap()), } } @@ -822,7 +520,6 @@ mod tests { TxId::generate(), transaction, CapturedFunctionCalls::empty(), - MAX_INSCRIPTION_SIZE, ) .expect("payload should encode") } @@ -879,7 +576,7 @@ mod tests { #[test] fn adopted_writes_apply_to_live_in_channel_order() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let lib_path = db.lib_path().to_owned(); @@ -915,7 +612,7 @@ mod tests { #[test] fn finalized_backfill_applies_to_lib_and_live_once() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let lib_path = db.lib_path().to_owned(); @@ -947,7 +644,7 @@ mod tests { #[test] fn replay_after_apply_completes_checkpoint_without_duplicate_effects() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let transaction = Transaction::new(vec![ Statement::new( @@ -963,7 +660,7 @@ mod tests { ]) .expect("transaction should be valid"); let encoded = encoded_write(&transaction); - let write = ChannelWrite::decode(&encoded.payload).expect("payload should decode"); + let write = ChannelInscription::decode(&encoded.payload).expect("payload should decode"); // Simulate a crash after SQL and its applied marker commit but before // the ZoneSDK checkpoint is persisted. @@ -971,7 +668,7 @@ mod tests { .expect("write should apply before the simulated crash"); drop(db); - let mut db = open_databases(dir.path()).expect("databases should reopen"); + let mut db = Databases::open(dir.path()).expect("databases should reopen"); let expected_checkpoint = checkpoint(2, 2); let event = blocks_processed( @@ -1000,12 +697,12 @@ mod tests { #[test] fn locally_applied_write_is_not_executed_when_adopted() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let setup = transaction("CREATE TABLE items(value INTEGER NOT NULL)", Vec::new()); let setup_tx_id = db - .commit_local_write(TxId::generate(), &setup, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &setup) .expect("setup write should commit"); let setup_pending = db .pending_publish() @@ -1020,7 +717,7 @@ mod tests { vec![Value::Integer(1)], ); let insert_tx_id = db - .commit_local_write(TxId::generate(), &insert, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &insert) .expect("insert write should commit"); let insert_pending = db .pending_publish() @@ -1050,13 +747,13 @@ mod tests { #[test] fn finalizing_an_earlier_local_write_preserves_the_next_pending_write() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let lib_path = db.lib_path().to_owned(); let create = transaction("CREATE TABLE items(value INTEGER NOT NULL)", Vec::new()); let create_tx_id = db - .commit_local_write(TxId::generate(), &create, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &create) .expect("schema write should commit"); let create_pending = db .pending_publish() @@ -1073,7 +770,7 @@ mod tests { vec![Value::Integer(1)], ); let insert_tx_id = db - .commit_local_write(TxId::generate(), &insert, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &insert) .expect("next write should commit"); let finalization = blocks_processed( @@ -1098,82 +795,10 @@ mod tests { assert_status(&db, create_tx_id, Some(WriteStatus::Finalized)); } - #[test] - fn endless_channel_sql_is_rejected_and_replay_continues() { - assert_rejected_sql_allows_replay( - "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM c) - SELECT x FROM c WHERE x < 0", - ); - } - - #[test] - fn oversized_channel_values_are_rejected_and_replay_continues() { - assert_rejected_sql_allows_replay( - "CREATE TABLE oversized AS SELECT zeroblob(100000000) AS value", - ); - } - - fn assert_rejected_sql_allows_replay(sql: &str) { - let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); - let rejected = encoded_write(&transaction(sql, vec![])); - let create = encoded_write(&transaction("CREATE TABLE items(value INTEGER)", vec![])); - let expected_checkpoint = checkpoint(2, 2); - let event = blocks_processed( - expected_checkpoint.clone(), - vec![ - ChannelUpdateTx::Inscription(inscription(&rejected.payload, 1)), - ChannelUpdateTx::Inscription(inscription(&create.payload, 2)), - ], - vec![], - vec![], - ); - - on_event(&mut db, &event, ChannelId::from(CHANNEL_ID)).unwrap(); - assert!(table_exists(db.live_path(), "items")); - assert_eq!(db.rejected_write_count().unwrap(), 1); - assert_eq!( - db.load_checkpoint().unwrap().unwrap().lib, - expected_checkpoint.lib - ); - - drop(db); - let mut db = open_databases(dir.path()).unwrap(); - - // A rebuild replays the SDK history, including rejected SQL. - super::rebuild_live( - &mut db, - &[ - inscription(&rejected.payload, 1), - inscription(&create.payload, 2), - ], - ) - .unwrap(); - assert!(table_exists(db.live_path(), "items")); - - let event = blocks_processed( - checkpoint(3, 3), - vec![], - vec![], - vec![ - finalized(&rejected.payload, 1), - finalized(&create.payload, 2), - ], - ); - on_event(&mut db, &event, ChannelId::from(CHANNEL_ID)).unwrap(); - - assert!(table_exists(db.lib_path(), "items")); - assert_eq!(db.rejected_write_count().unwrap(), 1); - assert_eq!( - db.load_checkpoint().unwrap().unwrap().lib, - checkpoint(3, 3).lib - ); - } - #[test] fn rejected_sql_does_not_block_following_writes() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let insert = encoded_write(&transaction( @@ -1208,7 +833,7 @@ mod tests { assert!(table_exists(&live_path, "items")); drop(db); - let db = open_databases(dir.path()).expect("databases should reopen"); + let db = Databases::open(dir.path()).expect("databases should reopen"); assert_eq!( db.rejected_write_count().expect("rejections should load"), @@ -1216,54 +841,10 @@ mod tests { ); } - #[test] - fn finalizing_rejected_sql_preserves_the_pending_local_write() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let rejected = encoded_write(&transaction( - "INSERT INTO missing_table VALUES (1)", - Vec::new(), - )); - let adoption = blocks_processed( - checkpoint(1, 1), - vec![ChannelUpdateTx::Inscription(inscription( - &rejected.payload, - 1, - ))], - Vec::new(), - Vec::new(), - ); - on_event(&mut db, &adoption, CHANNEL_ID.into()).unwrap(); - - let local_id = db - .commit_local_write( - TxId::generate(), - &transaction("CREATE TABLE local_write(value TEXT)", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - let event = blocks_processed( - checkpoint(2, 2), - Vec::new(), - Vec::new(), - vec![finalized(&rejected.payload, 1)], - ); - assert!( - super::on_event(&mut db, &event, CHANNEL_ID.into()) - .unwrap() - .is_none(), - "a previously rejected write must not request rebuild history" - ); - - assert!(table_exists(db.live_path(), "local_write")); - assert_eq!(db.pending_publish().unwrap().unwrap().tx_id, local_id); - assert_eq!(db.unhandled_displacements().unwrap(), []); - } - #[test] fn malformed_payload_does_not_block_following_writes() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let mut malformed = encoded_write(&transaction( @@ -1308,19 +889,19 @@ mod tests { #[test] fn reused_transaction_id_does_not_block_following_writes() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let first = encoded_write(&transaction( "CREATE TABLE first_write(value INTEGER)", Vec::new(), )); - let conflicting = ChannelWrite { + let conflicting = ChannelInscription { tx_id: first.tx_id, transaction: transaction("CREATE TABLE conflicting_write(value INTEGER)", Vec::new()), captured_function_calls: CapturedFunctionCalls::empty(), } - .encode(MAX_INSCRIPTION_SIZE) + .encode() .expect("conflicting write should encode"); let following = encoded_write(&transaction( "CREATE TABLE following_write(value INTEGER)", @@ -1360,7 +941,7 @@ mod tests { #[test] fn unsupported_protocol_does_not_block_following_writes() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let mut unsupported = encoded_write(&transaction( @@ -1407,11 +988,11 @@ mod tests { #[test] fn active_reader_keeps_its_snapshot_during_live_rebuild() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); - db.commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + db.commit_local_write(TxId::generate(), &local) .expect("local write should commit"); let local_pending = db .pending_publish() @@ -1492,12 +1073,12 @@ mod tests { #[test] fn local_write_reports_orphaned_adopted_and_finalized() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); let local_tx_id = db - .commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &local) .expect("local write should commit"); let pending = db .pending_publish() @@ -1522,7 +1103,7 @@ mod tests { assert_status(&db, local_tx_id, Some(WriteStatus::Displaced)); drop(db); - let mut db = open_databases(dir.path()).expect("databases should reopen"); + let mut db = Databases::open(dir.path()).expect("databases should reopen"); let restore = blocks_processed( checkpoint(3, 3), @@ -1553,92 +1134,9 @@ mod tests { } #[test] - fn different_sql_with_our_id_does_not_restore_our_write() { - for finalize_foreign in [false, true] { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let tx_id = TxId::generate(); - let original = transaction("CREATE TABLE original(value INTEGER)", vec![]); - - db.commit_local_write(tx_id, &original, MAX_INSCRIPTION_SIZE) - .unwrap(); - let pending = db.pending_publish().unwrap().unwrap(); - db.complete_publish(&checkpoint(1, 1), MsgId::from([1; 32]), &pending) - .unwrap(); - - let orphan = blocks_processed( - checkpoint(2, 2), - vec![], - vec![ChannelUpdateTx::Inscription(inscription( - &pending.payload, - 1, - ))], - vec![], - ); - on_event(&mut db, &orphan, ChannelId::from(CHANNEL_ID)).unwrap(); - let displacements = db.unhandled_displacements().unwrap(); - assert_eq!(displacements.len(), 1); - assert!(!table_exists(db.live_path(), "original")); - - drop(db); - let mut db = open_databases(dir.path()).unwrap(); - let foreign = EncodedWrite::new( - tx_id, - &transaction("CREATE TABLE foreign_write(value INTEGER)", vec![]), - CapturedFunctionCalls::empty(), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - let adopt = blocks_processed( - checkpoint(3, 3), - vec![ChannelUpdateTx::Inscription(inscription( - &foreign.payload, - 2, - ))], - vec![], - vec![], - ); - on_event(&mut db, &adopt, ChannelId::from(CHANNEL_ID)).unwrap(); - - assert!(table_exists(db.live_path(), "foreign_write")); - assert_status(&db, tx_id, Some(WriteStatus::Displaced)); - assert_eq!(db.unhandled_displacements().unwrap(), displacements); - - let next = if finalize_foreign { - blocks_processed( - checkpoint(4, 4), - vec![], - vec![], - vec![finalized(&foreign.payload, 2)], - ) - } else { - blocks_processed( - checkpoint(4, 4), - vec![], - vec![ChannelUpdateTx::Inscription(inscription( - &foreign.payload, - 2, - ))], - vec![], - ) - }; - on_event(&mut db, &next, ChannelId::from(CHANNEL_ID)).unwrap(); - - // Neither finalizing nor orphaning the foreign write can replace - // the original SQL offered to the application for review. - assert_status(&db, tx_id, Some(WriteStatus::Displaced)); - assert_eq!(db.unhandled_displacements().unwrap(), displacements); - assert_eq!( - db.unhandled_displacements().unwrap()[0].transaction, - original - ); - } - } - - #[test] - fn branch_change_replays_the_common_prefix() { + fn branch_change_preserves_the_unchanged_live_suffix() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let create = encoded_write(&transaction( @@ -1661,16 +1159,16 @@ mod tests { ); on_event(&mut db, &initial, ChannelId::from(CHANNEL_ID)) - .expect("initial history should apply"); + .expect("initial suffix should apply"); drop(db); - let mut db = open_databases(dir.path()).expect("databases should reopen"); + let mut db = Databases::open(dir.path()).expect("databases should reopen"); let replacement = encoded_write(&transaction( "INSERT INTO items(value) VALUES (?1)", vec![Value::Text("new".to_owned())], )); - let mut branch_change = blocks_processed( + let branch_change = blocks_processed( checkpoint(2, 2), vec![ChannelUpdateTx::Inscription(inscription( &replacement.payload, @@ -1683,19 +1181,8 @@ mod tests { Vec::new(), ); - if let Event::BlocksProcessed { - channel_update: ChannelUpdate::Conflict { common_prefix, .. }, - .. - } = &mut branch_change - { - common_prefix.push(ChannelUpdateTx::Inscription(inscription( - &create.payload, - 1, - ))); - } - on_event(&mut db, &branch_change, ChannelId::from(CHANNEL_ID)) - .expect("replacement history should rebuild"); + .expect("replacement suffix should rebuild"); on_event(&mut db, &branch_change, ChannelId::from(CHANNEL_ID)) .expect("replayed branch update should be idempotent"); @@ -1706,12 +1193,12 @@ mod tests { #[test] fn foreign_adoption_displaces_an_unpublished_local_write() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); let local_tx_id = db - .commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &local) .expect("local write should commit"); let foreign = encoded_write(&transaction( @@ -1741,106 +1228,10 @@ mod tests { assert_status(&db, local_tx_id, Some(WriteStatus::Displaced)); } - #[test] - fn extension_rebuild_uses_the_sdk_history_before_the_new_write() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let create = encoded_write(&transaction("CREATE TABLE items(value TEXT)", Vec::new())); - let prefix = ChannelUpdateTx::Inscription(inscription(&create.payload, 1)); - let initial = blocks_processed( - checkpoint(1, 1), - vec![prefix.clone()], - Vec::new(), - Vec::new(), - ); - on_event(&mut db, &initial, CHANNEL_ID.into()).unwrap(); - - let local_id = db - .commit_local_write( - TxId::generate(), - &transaction("INSERT INTO items VALUES ('local')", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - let foreign = encoded_write(&transaction( - "INSERT INTO items VALUES ('foreign')", - Vec::new(), - )); - let adopted = ChannelUpdateTx::Inscription(inscription(&foreign.payload, 2)); - let event = blocks_processed( - checkpoint(2, 2), - vec![adopted.clone()], - Vec::new(), - Vec::new(), - ); - - apply_with_history( - &mut db, - &event, - CHANNEL_ID.into(), - Some(vec![prefix, adopted]), - ) - .unwrap(); - - assert_eq!(text_values(db.live_path(), "items"), vec!["foreign"]); - assert_status(&db, local_id, Some(WriteStatus::Displaced)); - assert!(!db.rebuild_required().unwrap()); - } - - #[test] - fn interrupted_fork_rebuild_resumes_without_another_conflict_event() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let old = encoded_write(&transaction( - "CREATE TABLE abandoned(value TEXT)", - Vec::new(), - )); - let initial = blocks_processed( - checkpoint(1, 1), - vec![ChannelUpdateTx::Inscription(inscription(&old.payload, 1))], - Vec::new(), - Vec::new(), - ); - on_event(&mut db, &initial, CHANNEL_ID.into()).unwrap(); - - // The fork was detected, but the process stopped before replacing LIVE. - // On restart the SDK catches up with an extension, not the old conflict. - db.require_rebuild().unwrap(); - drop(db); - - let mut db = open_databases(dir.path()).unwrap(); - let current = encoded_write(&transaction( - "CREATE TABLE surviving(value TEXT)", - Vec::new(), - )); - let event = blocks_processed(checkpoint(2, 2), Vec::new(), Vec::new(), Vec::new()); - apply_with_history( - &mut db, - &event, - CHANNEL_ID.into(), - Some(vec![ChannelUpdateTx::Inscription(inscription( - ¤t.payload, - 2, - ))]), - ) - .unwrap(); - - assert!(!table_exists(db.live_path(), "abandoned")); - assert!(table_exists(db.live_path(), "surviving")); - assert!(!db.rebuild_required().unwrap()); - - assert!( - super::on_event(&mut db, &event, CHANNEL_ID.into()) - .unwrap() - .is_none(), - "completed rebuild must not request history again" - ); - } - #[test] fn rollback_displaces_an_unpublished_write_based_on_removed_state() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let previous = encoded_write(&transaction( @@ -1861,7 +1252,7 @@ mod tests { let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); let local_tx_id = db - .commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &local) .expect("local write should commit"); let rollback = blocks_processed( @@ -1882,22 +1273,15 @@ mod tests { } #[test] - fn replay_finishes_an_interrupted_rebuild() { + fn replay_finishes_a_rebuild_after_the_suffix_commit() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); let local_tx_id = db - .commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &local) .expect("local write should commit"); - let second_id = db - .commit_local_write( - TxId::generate(), - &transaction("INSERT INTO local_write VALUES (1)", Vec::new()), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); let pending = db .pending_publish() .expect("pending write should load") @@ -1909,14 +1293,26 @@ mod tests { )); let foreign_inscription = inscription(&foreign.payload, 2); - // Stop after recording only the first displacement. Recovery must - // also displace the rest of the queue before replacing LIVE. - db.require_rebuild().unwrap(); + // Simulate a crash after the control-state transaction commits but + // before the replacement LIVE database is installed. db.record_pending_write_displacement(&pending) .expect("displacement should be recorded"); + db.apply_history_delta( + &[], + &[], + &[SuffixWrite { + this_msg: foreign_inscription.this_msg, + tx_id: ChannelInscription::decode(&foreign.payload) + .expect("payload should decode") + .tx_id, + payload: foreign.payload.clone(), + local: false, + }], + ) + .expect("suffix update should commit"); drop(db); - let mut db = open_databases(dir.path()).expect("databases should reopen"); + let mut db = Databases::open(dir.path()).expect("databases should reopen"); let replayed_event = blocks_processed( checkpoint(2, 2), vec![ChannelUpdateTx::Inscription(foreign_inscription)], @@ -1935,19 +1331,17 @@ mod tests { .is_none() ); assert_status(&db, local_tx_id, Some(WriteStatus::Displaced)); - assert_status(&db, second_id, Some(WriteStatus::Displaced)); - assert!(db.pending_writes().unwrap().is_empty()); } #[test] - fn restart_backfill_waits_for_live_history_before_finishing_the_rebuild() { + fn finalized_backfill_finishes_a_rebuild_after_the_suffix_commit() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); let local_tx_id = db - .commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &local) .expect("local write should commit"); let pending = db .pending_publish() @@ -1962,12 +1356,24 @@ mod tests { // Simulate a crash after the control-state transaction commits but // before the replacement LIVE database is installed. - db.require_rebuild().unwrap(); db.record_pending_write_displacement(&pending) .expect("displacement should be recorded"); + db.apply_history_delta( + &[], + &[], + &[SuffixWrite { + this_msg: foreign_inscription.this_msg, + tx_id: ChannelInscription::decode(&foreign.payload) + .expect("payload should decode") + .tx_id, + payload: foreign.payload.clone(), + local: false, + }], + ) + .expect("suffix update should commit"); drop(db); - let mut db = open_databases(dir.path()).expect("databases should reopen"); + let mut db = Databases::open(dir.path()).expect("databases should reopen"); let backfilled_event = blocks_processed( checkpoint(2, 2), Vec::new(), @@ -1979,18 +1385,8 @@ mod tests { }], ); - apply_with_history(&mut db, &backfilled_event, CHANNEL_ID.into(), None) - .expect("finalized backfill should advance LIB without a live history yet"); - - assert!(table_exists(db.lib_path(), "foreign_write")); - assert!(db.rebuild_required().unwrap()); - drop(db); - - // Even another restart during backfill must not lose the rebuild. - let mut db = open_databases(dir.path()).unwrap(); - let live_event = blocks_processed(checkpoint(3, 3), Vec::new(), Vec::new(), Vec::new()); - apply_with_history(&mut db, &live_event, CHANNEL_ID.into(), Some(Vec::new())) - .expect("the first live block should finish the rebuild"); + on_event(&mut db, &backfilled_event, ChannelId::from(CHANNEL_ID)) + .expect("finalized backfill should finish the rebuild"); assert!(!table_exists(&live_path, "local_write")); assert!(table_exists(&live_path, "foreign_write")); @@ -2000,18 +1396,17 @@ mod tests { .is_none() ); assert_status(&db, local_tx_id, Some(WriteStatus::Displaced)); - assert!(!db.rebuild_required().unwrap()); } #[test] fn newly_discovered_finalized_write_displaces_an_unpublished_local_write() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let live_path = db.live_path().to_owned(); let local = transaction("CREATE TABLE local_write(value INTEGER)", Vec::new()); let local_tx_id = db - .commit_local_write(TxId::generate(), &local, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &local) .expect("local write should commit"); let foreign = encoded_write(&transaction( diff --git a/logos_sql/src/db.rs b/logos_sql/src/db.rs index fef94ed..c9c5c85 100644 --- a/logos_sql/src/db.rs +++ b/logos_sql/src/db.rs @@ -2,12 +2,11 @@ use std::{ fs, - num::NonZeroUsize, ops::Deref, path::{Path, PathBuf}, sync::{ Arc, - atomic::{AtomicBool, AtomicU64, Ordering}, + atomic::{AtomicBool, Ordering}, }, time::Duration, }; @@ -18,7 +17,6 @@ use rusqlite::{ Connection, ErrorCode as SqliteErrorCode, OpenFlags, OptionalExtension as _, Row, backup::Backup, hooks::{AuthAction, AuthContext, Authorization}, - limits::Limit, params, params_from_iter, types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}, }; @@ -26,26 +24,11 @@ use rusqlite::{ use crate::{ error::Error, functions::FunctionOverrides, - protocol::{ChannelWrite, EncodedWrite, MAX_BODY_BYTES, Transaction, TxId}, + protocol::{ChannelInscription, EncodedWrite, Transaction, TxId}, status::{Displacement, DisplacementReason, WriteStatus}, }; const DATABASE_BUSY_TIMEOUT: Duration = Duration::from_secs(5); - -// One allowance for all statements in an application transaction. Charge 1,000 -// steps per statement plus each progress callback, so many short statements -// cannot avoid the limit. Count work rather than elapsed time so slower -// replicas do not reject more writes. -// This is a replication rule, not a per-node tuning option. Changes to this -// limit or the SQLite execution engine require coordinated upgrades. -const MAX_SQL_STEPS: u64 = 10_000_000; -const SQL_PROGRESS_INTERVAL: i32 = 1_000; - -// SQL-generated values and complete rows must fit the same 64 MiB allowance -// as an uncompressed write. A step budget alone cannot bound a large blob -// operation. These limits do not cap the total data written by a transaction. -const MAX_SQL_BYTES: i32 = MAX_BODY_BYTES as i32; - const LIB_DATABASE_FILE: &str = "LIB.db"; const LIVE_DATABASE_FILE: &str = "LIVE.db"; const CONTROL_DATABASE_FILE: &str = "control.db"; @@ -58,10 +41,10 @@ const DISPLACED_STATUS: &str = "displaced"; const FINALIZED_STATUS: &str = "finalized"; // Present in both state databases so replicated SQL observes the same schema. -// Only LIVE.db stores queued writes, committed atomically with their effects. +// Only LIVE.db stores a row, committed atomically with the local write. const PENDING_PUBLISH_SCHEMA: &str = " CREATE TABLE IF NOT EXISTS __logos_sql_pending_publish ( - sequence INTEGER PRIMARY KEY, + singleton INTEGER PRIMARY KEY CHECK (singleton = 1), tx_id BLOB NOT NULL UNIQUE CHECK (length(tx_id) = 32), payload BLOB NOT NULL ) STRICT; @@ -82,29 +65,28 @@ const APPLIED_WRITE_SCHEMA: &str = " const CONTROL_SCHEMA: &str = " CREATE TABLE IF NOT EXISTS __logos_sql_state ( singleton INTEGER PRIMARY KEY CHECK (singleton = 1), - checkpoint BLOB, - rebuild_required INTEGER NOT NULL DEFAULT 0 CHECK (rebuild_required IN (0, 1)) + checkpoint BLOB ) STRICT; CREATE TABLE IF NOT EXISTS __logos_sql_rejected_writes ( - this_msg BLOB NOT NULL CHECK (length(this_msg) = 32), + this_msg BLOB PRIMARY KEY CHECK (length(this_msg) = 32), tx_id BLOB CHECK (tx_id IS NULL OR length(tx_id) = 32), - reason TEXT NOT NULL, - UNIQUE (this_msg, tx_id) + reason TEXT NOT NULL ) STRICT; - CREATE UNIQUE INDEX IF NOT EXISTS __logos_sql_rejected_payloads - ON __logos_sql_rejected_writes(this_msg) WHERE tx_id IS NULL; + CREATE TABLE IF NOT EXISTS __logos_sql_live_suffix ( + position INTEGER PRIMARY KEY AUTOINCREMENT, + this_msg BLOB NOT NULL UNIQUE CHECK (length(this_msg) = 32), + tx_id BLOB NOT NULL CHECK (length(tx_id) = 32), + payload BLOB NOT NULL, + local INTEGER NOT NULL CHECK (local IN (0, 1)) + ) STRICT; - CREATE TABLE IF NOT EXISTS __logos_sql_local_writes ( - this_msg BLOB NOT NULL CHECK (length(this_msg) = 32), + CREATE TABLE IF NOT EXISTS __logos_sql_displaced_writes ( tx_id BLOB PRIMARY KEY CHECK (length(tx_id) = 32), - content_digest BLOB NOT NULL CHECK (length(content_digest) = 32) + this_msg BLOB NOT NULL UNIQUE CHECK (length(this_msg) = 32) ) STRICT; - CREATE INDEX IF NOT EXISTS __logos_sql_local_write_positions - ON __logos_sql_local_writes(this_msg); - CREATE TABLE IF NOT EXISTS __logos_sql_write_statuses ( sequence INTEGER PRIMARY KEY AUTOINCREMENT, tx_id BLOB NOT NULL UNIQUE CHECK (length(tx_id) = 32), @@ -114,9 +96,6 @@ const CONTROL_SCHEMA: &str = " displacement_payload BLOB, displacement_handled INTEGER NOT NULL DEFAULT 1 CHECK (displacement_handled IN (0, 1)) ) STRICT; - - CREATE INDEX IF NOT EXISTS __logos_sql_unhandled_displacements - ON __logos_sql_write_statuses(sequence) WHERE displacement_handled = 0; "; const INITIALIZE_CONTROL_STATE: &str = " @@ -178,48 +157,59 @@ const UPDATE_CHECKPOINT: &str = " const INSERT_REJECTED_WRITE: &str = " INSERT INTO __logos_sql_rejected_writes (this_msg, tx_id, reason) VALUES (?1, ?2, ?3) - ON CONFLICT DO NOTHING + ON CONFLICT (this_msg) DO NOTHING "; const INSERT_PENDING_PUBLISH: &str = " - INSERT INTO __logos_sql_pending_publish (tx_id, payload) - VALUES (?1, ?2) + INSERT INTO __logos_sql_pending_publish (singleton, tx_id, payload) + VALUES (1, ?1, ?2) "; const SELECT_PENDING_PUBLISH: &str = " SELECT tx_id, payload FROM __logos_sql_pending_publish - ORDER BY sequence + WHERE singleton = 1 "; const CLEAR_PENDING_PUBLISH: &str = " DELETE FROM __logos_sql_pending_publish - WHERE tx_id = ?1 + WHERE singleton = 1 AND tx_id = ?1 "; -// Keep our publication's identity until finality, even while it is orphaned. -// The SDK supplies its payload if it is removed or restored; no replay history -// is retained here. -const UPSERT_LOCAL_WRITE: &str = " - INSERT INTO __logos_sql_local_writes (this_msg, tx_id, content_digest) - VALUES (?1, ?2, ?3) - ON CONFLICT (tx_id) DO UPDATE SET this_msg = excluded.this_msg +const INSERT_SUFFIX_WRITE: &str = " + INSERT INTO __logos_sql_live_suffix (this_msg, tx_id, payload, local) + VALUES (?1, ?2, ?3, ?4) + ON CONFLICT (this_msg) DO NOTHING "; -const DELETE_LOCAL_WRITE: &str = " - DELETE FROM __logos_sql_local_writes +const DELETE_SUFFIX_WRITE: &str = " + DELETE FROM __logos_sql_live_suffix WHERE this_msg = ?1 "; -const SELECT_LOCAL_WRITE: &str = " - SELECT tx_id, content_digest - FROM __logos_sql_local_writes - WHERE this_msg = ?1 +const SELECT_SUFFIX_WRITES: &str = " + SELECT this_msg, tx_id, payload, local + FROM __logos_sql_live_suffix + ORDER BY position "; -const HAS_LOCAL_PUBLICATION: &str = " - SELECT EXISTS ( - SELECT 1 FROM __logos_sql_local_writes WHERE this_msg = ?1 +const SELECT_LOCAL_SUFFIX_WRITE: &str = " + SELECT tx_id, payload + FROM __logos_sql_live_suffix + WHERE this_msg = ?1 AND local = 1 +"; + +const SELECT_LOCAL_SUFFIX_WRITE_BY_TX: &str = " + SELECT EXISTS( + SELECT 1 + FROM __logos_sql_live_suffix + WHERE tx_id = ?1 AND local = 1 + ) +"; + +const SELECT_SUFFIX_WRITE_EXISTS: &str = " + SELECT EXISTS( + SELECT 1 FROM __logos_sql_live_suffix WHERE this_msg = ?1 ) "; @@ -229,32 +219,40 @@ const UPSERT_WRITE_STATUS: &str = " ON CONFLICT (tx_id) DO UPDATE SET status = excluded.status "; -const SELECT_WRITE_STATUS: &str = " - SELECT status - FROM __logos_sql_write_statuses - WHERE tx_id = ?1 +const INSERT_DISPLACED_WRITE: &str = " + INSERT INTO __logos_sql_displaced_writes (tx_id, this_msg) + VALUES (?1, ?2) + ON CONFLICT (tx_id) DO UPDATE SET this_msg = excluded.this_msg "; -const SELECT_PENDING_QUEUE_SIZE: &str = " - SELECT count(*), coalesce(sum(length(payload)), 0) - FROM __logos_sql_pending_publish +const DELETE_DISPLACED_WRITE: &str = " + DELETE FROM __logos_sql_displaced_writes + WHERE this_msg = ?1 "; -const HAS_PENDING_WRITES: &str = " - SELECT EXISTS(SELECT 1 FROM __logos_sql_pending_publish) +const DELETE_DISPLACED_WRITE_BY_TX: &str = " + DELETE FROM __logos_sql_displaced_writes + WHERE tx_id = ?1 "; -const HAS_PENDING_WRITE: &str = " - SELECT EXISTS(SELECT 1 FROM __logos_sql_pending_publish WHERE tx_id = ?1) +const SELECT_WRITE_STATUS: &str = " + SELECT status + FROM __logos_sql_write_statuses + WHERE tx_id = ?1 "; -const MAX_PENDING_BYTES: usize = 64 * 1024 * 1024; +const SELECT_DISPLACED_WRITE_AT_POSITION: &str = " + SELECT tx_id + FROM __logos_sql_displaced_writes + WHERE this_msg = ?1 +"; -const SELECT_DISPLACED_PAYLOAD: &str = " - SELECT displacement_payload - FROM __logos_sql_write_statuses - WHERE tx_id = ?1 AND status = 'displaced' - AND displacement_reason = 'orphaned' +const SELECT_DISPLACED_WRITE_BY_TX: &str = " + SELECT EXISTS( + SELECT 1 + FROM __logos_sql_displaced_writes + WHERE tx_id = ?1 + ) "; const SELECT_APPLIED_WRITE: &str = " @@ -297,8 +295,8 @@ const UNSUPPORTED_FUNCTIONS: [&str; 9] = [ /// persisted. /// /// `ZoneSDK` may already hold the write in memory. Until its returned -/// checkpoint and local write metadata commit, this record remains the durable -/// source used to recover the publication. +/// checkpoint and suffix entry commit, this record remains the durable source +/// used to recover the publication. pub struct PendingPublish { pub tx_id: TxId, pub payload: Vec, @@ -319,12 +317,12 @@ impl StoredPendingPublish { } } -/// A decoded identity and its original payload from a channel event. -/// Payloads are used during application, not retained as a replay history. -pub struct ObservedWrite { +/// One Logos SQL inscription retained above the finalized boundary. +pub struct SuffixWrite { pub this_msg: MsgId, pub tx_id: TxId, pub payload: Vec, + pub local: bool, } /// A replicated database connection and the function state attached to it. @@ -334,18 +332,17 @@ struct ReplicatedDatabase { hooks: ApplicationHooks, } -/// The authorizer and the execution budget that constrain application SQL. -/// Installed once per connection; active only while armed. +/// The authorizer that constrains application SQL. Installed once per +/// connection, since setting it needs `&mut Connection`; active only while +/// armed. struct ApplicationHooks { armed: Arc, - work: Arc, } impl ApplicationHooks { fn install(connection: &mut Connection) -> Result { let hooks = Self { armed: Arc::new(AtomicBool::new(false)), - work: Arc::new(AtomicU64::new(0)), }; let armed = Arc::clone(&hooks.armed); @@ -357,22 +354,10 @@ impl ApplicationHooks { } }))?; - let armed = Arc::clone(&hooks.armed); - let work = Arc::clone(&hooks.work); - connection.progress_handler( - SQL_PROGRESS_INTERVAL, - Some(move || { - armed.load(Ordering::Relaxed) - && work.fetch_add(SQL_PROGRESS_INTERVAL as u64, Ordering::Relaxed) - >= MAX_SQL_STEPS - }), - )?; - Ok(hooks) } fn arm(&self) { - self.work.store(0, Ordering::Relaxed); self.armed.store(true, Ordering::Relaxed); } @@ -381,14 +366,6 @@ impl ApplicationHooks { } } -/// Pending write count and payload bytes, cached to avoid scanning the -/// queue on every write. Reloaded after publication or a live rebuild. -#[derive(Clone, Copy)] -struct PendingPublishUsage { - writes: usize, - bytes: usize, -} - /// An isolated replacement for `LIVE.db` while canonical history is replayed. pub struct LiveRebuild { database: ReplicatedDatabase, @@ -396,7 +373,7 @@ pub struct LiveRebuild { } impl LiveRebuild { - pub(crate) fn apply_write(&mut self, write: &ChannelWrite) -> Result<(), Error> { + pub(crate) fn apply_write(&mut self, write: &ChannelInscription) -> Result<(), Error> { apply_channel_write(&mut self.database, write) } } @@ -411,8 +388,6 @@ impl Deref for ReplicatedDatabase { /// Owns the participant-local database connections. pub struct Databases { - max_pending_writes: NonZeroUsize, - pending_publish_usage: Option, lib: ReplicatedDatabase, live: ReplicatedDatabase, control: Connection, @@ -421,8 +396,8 @@ pub struct Databases { } impl Databases { - /// Opens or creates local state with a limit on unpublished writes. - pub(crate) fn open(directory: &Path, max_pending_writes: NonZeroUsize) -> Result { + /// Opens or creates the participant state under `directory`. + pub(crate) fn open(directory: &Path) -> Result { fs::create_dir_all(directory)?; let lib_path = directory.join(LIB_DATABASE_FILE); @@ -445,8 +420,6 @@ impl Databases { control.execute(INITIALIZE_CONTROL_STATE, [])?; Ok(Self { - max_pending_writes, - pending_publish_usage: None, lib, live, control, @@ -478,15 +451,7 @@ impl Databases { .map_err(Error::from) } - pub(crate) fn persist_checkpoint(&self, checkpoint: &SequencerCheckpoint) -> Result<(), Error> { - let encoded = checkpoint_options().serialize(checkpoint)?; - self.control.execute(UPDATE_CHECKPOINT, [encoded])?; - Ok(()) - } - - /// Saves the event checkpoint and clears its rebuild flag together, only - /// after the replacement LIVE database has been installed. - pub(crate) fn persist_rebuilt_checkpoint( + pub(crate) fn persist_checkpoint( &mut self, checkpoint: &SequencerCheckpoint, ) -> Result<(), Error> { @@ -494,73 +459,51 @@ impl Databases { let transaction = self.control.transaction()?; transaction.execute(UPDATE_CHECKPOINT, [encoded])?; - transaction.execute( - "UPDATE __logos_sql_state SET rebuild_required = 0 WHERE singleton = 1", - [], - )?; transaction.commit()?; Ok(()) } - /// Persists `ZoneSDK` ownership and the identity of our published write - /// before removing the pending publication from `LIVE.db`. - #[cfg(test)] + /// Persists `ZoneSDK` ownership of a local write and adds it to the live + /// suffix before removing the pending publication from `LIVE.db`. pub(crate) fn complete_publish( &mut self, checkpoint: &SequencerCheckpoint, this_msg: MsgId, pending: &PendingPublish, - ) -> Result<(), Error> { - self.complete_batch(checkpoint, this_msg, std::slice::from_ref(pending)) - } - - /// Saves the SDK checkpoint and every batch member before clearing the - /// local queue. Queue removal is atomic, so recovery sees the whole batch. - pub(crate) fn complete_batch( - &mut self, - checkpoint: &SequencerCheckpoint, - this_msg: MsgId, - pending: &[PendingPublish], ) -> Result<(), Error> { let encoded_checkpoint = checkpoint_options().serialize(checkpoint)?; let transaction = self.control.transaction()?; transaction.execute(UPDATE_CHECKPOINT, [encoded_checkpoint])?; + transaction.execute( + INSERT_SUFFIX_WRITE, + params![ + this_msg.as_ref(), + pending.tx_id.as_ref(), + pending.payload, + true + ], + )?; - for pending in pending { - let content_digest = ChannelWrite::decode(&pending.payload)?.content_digest(); - transaction.execute( - UPSERT_LOCAL_WRITE, - params![this_msg.as_ref(), pending.tx_id.as_ref(), content_digest], - )?; - - // Record the order now, not when a later event displaces the write. - set_write_status(&transaction, pending.tx_id, WriteStatus::Live)?; - } - - transaction.commit()?; - - // Recount once after removing a batch, including recovery retries. - self.pending_publish_usage = None; - let transaction = self.live.connection.transaction()?; - - for pending in pending { - transaction.execute(CLEAR_PENDING_PUBLISH, [pending.tx_id.as_ref()])?; - } - + // Record the order now, not when a later event displaces the write. + set_write_status(&transaction, pending.tx_id, WriteStatus::Live)?; transaction.commit()?; - Ok(()) + self.clear_pending_publish(pending.tx_id) } - /// Updates our own writes from a channel event. Foreign writes are not - /// retained; the SDK supplies their history when a rebuild is needed. - pub(crate) fn update_write_statuses( + /// Applies one channel event to retained unfinalized history and local + /// write statuses. + /// + /// Removes finalized and orphaned writes from the live suffix, adds + /// adopted writes, and updates local write statuses. These changes commit + /// in one transaction so a crash cannot leave a partially recorded update. + pub(crate) fn apply_history_delta( &mut self, finalized: &[MsgId], - orphaned: &[ObservedWrite], - adopted: &[ObservedWrite], + orphaned: &[MsgId], + adopted: &[SuffixWrite], ) -> Result<(), Error> { let transaction = self.control.transaction()?; @@ -568,8 +511,8 @@ impl Databases { finalize_write(&transaction, this_msg)?; } - for write in orphaned { - orphan_write(&transaction, write)?; + for this_msg in orphaned { + orphan_write(&transaction, this_msg)?; } for write in adopted { @@ -581,49 +524,36 @@ impl Databases { Ok(()) } - /// Records the rebuild decision before any database is changed. The flag - /// survives a crash even if the next SDK event is only an extension. - /// `persist_rebuilt_checkpoint` clears it after the replacement is - /// installed. - pub(crate) fn require_rebuild(&self) -> Result<(), Error> { - self.control.execute( - "UPDATE __logos_sql_state SET rebuild_required = 1 WHERE singleton = 1", - [], - )?; - Ok(()) - } + pub(crate) fn live_suffix(&self) -> Result, Error> { + let mut statement = self.control.prepare(SELECT_SUFFIX_WRITES)?; + let rows = statement.query_map([], |row| { + Ok(( + row.get::<_, Vec>(0)?, + row.get::<_, Vec>(1)?, + row.get::<_, Vec>(2)?, + row.get::<_, bool>(3)?, + )) + })?; - pub(crate) fn rebuild_required(&self) -> Result { - Ok(self.control.query_row( - "SELECT rebuild_required FROM __logos_sql_state WHERE singleton = 1", - [], - |row| row.get(0), - )?) + rows.map(|row| { + let (this_msg, tx_id, payload, local) = row?; + + Ok(SuffixWrite { + this_msg: decode_msg_id(this_msg)?, + tx_id: decode_tx_id(tx_id)?, + payload, + local, + }) + }) + .collect() } - /// Finalizing an already applied or rejected inscription does not change - /// the state against which a pending local write ran. - pub(crate) fn live_processed_write( - &self, - this_msg: MsgId, - write: &ChannelWrite, - ) -> Result { - let digest: Option> = self - .live - .query_row(SELECT_APPLIED_WRITE, [write.tx_id.as_ref()], |row| { + pub(crate) fn live_suffix_contains(&self, this_msg: MsgId) -> Result { + self.control + .query_row(SELECT_SUFFIX_WRITE_EXISTS, [this_msg.as_ref()], |row| { row.get(0) }) - .optional()?; - if digest.is_some_and(|digest| digest.as_slice() == write.content_digest()) { - return Ok(true); - } - - Ok(self.control.query_row( - "SELECT EXISTS(SELECT 1 FROM __logos_sql_rejected_writes - WHERE this_msg = ?1 AND tx_id = ?2)", - params![this_msg.as_ref(), write.tx_id.as_ref()], - |row| row.get(0), - )?) + .map_err(Error::from) } pub(crate) fn unhandled_displacements(&self) -> Result, Error> { @@ -649,7 +579,7 @@ impl Databases { tx_id: decode_tx_id(tx_id)?, id: decode_tx_id(id)?, reason, - transaction: ChannelWrite::decode(&payload)?.transaction, + transaction: ChannelInscription::decode(&payload)?.transaction, }) }) .collect() @@ -703,19 +633,35 @@ impl Databases { return Ok(Some(status)); } - let pending: bool = - self.live - .connection - .query_row(HAS_PENDING_WRITE, [tx_id.as_ref()], |row| row.get(0))?; - - if pending { + if self + .pending_publish()? + .is_some_and(|pending| pending.tx_id == tx_id) + { return Ok(Some(WriteStatus::Live)); } - Ok(None) + let live: bool = + self.control + .query_row(SELECT_LOCAL_SUFFIX_WRITE_BY_TX, [tx_id.as_ref()], |row| { + row.get(0) + })?; + + Ok(live.then_some(WriteStatus::Live)) + } + + /// Reports whether recovery must finish removing a pending local write. + /// + /// A rebuild saves the displaced status before replacing `LIVE.db`. + /// If the pending record still exists, the rebuild has not finished. + pub(crate) fn pending_write_rebuild_was_interrupted(&self, tx_id: TxId) -> Result { + Ok(self.write_status(tx_id)? == Some(WriteStatus::Displaced)) } /// Records a local write whose base changed before `ZoneSDK` accepted it. + /// + /// The pending record remains in `LIVE.db` until the rebuild replaces the + /// database. If recovery is interrupted, that record makes the same event + /// request another rebuild. pub(crate) fn record_pending_write_displacement( &mut self, pending: &PendingPublish, @@ -753,7 +699,6 @@ impl Databases { /// Atomically replaces the contents of `LIVE.db` for current and future /// readers using `SQLite`'s online backup API. pub(crate) fn finish_live_rebuild(&mut self, rebuild: LiveRebuild) -> Result<(), Error> { - self.pending_publish_usage = None; backup_database(&rebuild.database.connection, &mut self.live.connection)?; let path = rebuild.path.clone(); @@ -791,32 +736,17 @@ impl Databases { &mut self, tx_id: TxId, transaction: &Transaction, - max_inscription_size: u32, ) -> Result { - let usage = self.pending_publish_usage()?; - - if usage.writes >= self.max_pending_writes.get() { + if self.pending_publish()?.is_some() { return Err(Error::PublishPending); } - self.pending_publish_usage = None; let capture = self.live.functions.capture(); let db_transaction = self.live.connection.transaction()?; apply_statements(&db_transaction, &self.live.hooks, transaction)?; let captured_function_calls = capture.finish()?; - let encoded = EncodedWrite::new( - tx_id, - transaction, - captured_function_calls, - max_inscription_size, - )?; - - let bytes = usage.bytes + encoded.payload.len(); - - if bytes > MAX_PENDING_BYTES { - return Err(Error::PublishPending); - } + let encoded = EncodedWrite::new(tx_id, transaction, captured_function_calls)?; db_transaction.execute( INSERT_APPLIED_WRITE, @@ -829,32 +759,11 @@ impl Databases { )?; db_transaction.commit()?; - self.pending_publish_usage = Some(PendingPublishUsage { - writes: usage.writes + 1, - bytes, - }); - Ok(encoded.tx_id) } - fn pending_publish_usage(&mut self) -> Result { - if let Some(usage) = self.pending_publish_usage { - return Ok(usage); - } - - let usage = self.live.query_row(SELECT_PENDING_QUEUE_SIZE, [], |row| { - Ok(PendingPublishUsage { - writes: row.get(0)?, - bytes: row.get(1)?, - }) - })?; - self.pending_publish_usage = Some(usage); - - Ok(usage) - } - /// Applies a newly adopted channel write to the live database. - pub(crate) fn apply_adopted_write(&mut self, write: &ChannelWrite) -> Result<(), Error> { + pub(crate) fn apply_adopted_write(&mut self, write: &ChannelInscription) -> Result<(), Error> { apply_channel_write(&mut self.live, write) } @@ -862,7 +771,7 @@ impl Databases { /// is being built from that state. pub(crate) fn apply_finalized_write_to_lib( &mut self, - write: &ChannelWrite, + write: &ChannelInscription, ) -> Result<(), Error> { apply_channel_write(&mut self.lib, write) } @@ -872,7 +781,10 @@ impl Databases { /// Applying to `LIVE.db` as well covers writes first discovered through /// finalized backfill. Writes already applied locally or while adopted /// are skipped using their `TxId`. - pub(crate) fn apply_finalized_write(&mut self, write: &ChannelWrite) -> Result<(), Error> { + pub(crate) fn apply_finalized_write( + &mut self, + write: &ChannelInscription, + ) -> Result<(), Error> { let content_digest = write.content_digest(); is_write_applied(&self.lib.connection, write.tx_id, &content_digest)?; @@ -896,7 +808,6 @@ impl Databases { .map_err(Error::from) } - #[cfg(test)] pub(crate) fn pending_publish(&self) -> Result, Error> { let record = self .live @@ -916,41 +827,7 @@ impl Databases { })) } - pub(crate) fn has_pending_writes(&self) -> Result { - self.live - .connection - .query_row(HAS_PENDING_WRITES, [], |row| row.get(0)) - .map_err(Error::from) - } - - pub(crate) fn has_local_publication(&self, this_msg: MsgId) -> Result { - self.control - .query_row(HAS_LOCAL_PUBLICATION, [this_msg.as_ref()], |row| row.get(0)) - .map_err(Error::from) - } - - pub(crate) fn pending_write_count(&mut self) -> Result { - Ok(self.pending_publish_usage()?.writes) - } - - pub(crate) fn pending_writes(&self) -> Result, Error> { - let mut statement = self.live.connection.prepare(SELECT_PENDING_PUBLISH)?; - let rows = statement.query_map([], StoredPendingPublish::from_row)?; - - rows.map(|row| { - let record = row?; - - Ok(PendingPublish { - tx_id: decode_tx_id(record.tx_id)?, - payload: record.payload, - }) - }) - .collect() - } - - #[cfg(test)] - pub(crate) fn clear_pending_publish(&mut self, tx_id: TxId) -> Result<(), Error> { - self.pending_publish_usage = None; + pub(crate) fn clear_pending_publish(&self, tx_id: TxId) -> Result<(), Error> { let changed = self .live .connection @@ -974,79 +851,99 @@ impl Databases { } } -/// Finalizes our own write and releases its non-finalized tracking records. +/// Removes a write's suffix and displacement records; marks it finalized if +/// local. fn finalize_write(transaction: &rusqlite::Transaction<'_>, this_msg: &MsgId) -> Result<(), Error> { - let mut local = transaction.prepare(SELECT_LOCAL_WRITE)?; - let local_ids = local.query_map([this_msg.as_ref()], |row| row.get::<_, Vec>(0))?; - for tx_id in local_ids { - let tx_id = tx_id?; + let mut local_tx_id = transaction + .query_row(SELECT_LOCAL_SUFFIX_WRITE, [this_msg.as_ref()], |row| { + row.get::<_, Vec>(0) + }) + .optional()?; + + if local_tx_id.is_none() { + local_tx_id = transaction + .query_row( + SELECT_DISPLACED_WRITE_AT_POSITION, + [this_msg.as_ref()], + |row| row.get::<_, Vec>(0), + ) + .optional()?; + } + + if let Some(tx_id) = local_tx_id { let tx_id = decode_tx_id(tx_id)?; set_write_status(transaction, tx_id, WriteStatus::Finalized)?; transaction.execute(CLEAR_DISPLACEMENT, [tx_id.as_ref()])?; } - transaction.execute(DELETE_LOCAL_WRITE, [this_msg.as_ref()])?; + transaction.execute(DELETE_DISPLACED_WRITE, [this_msg.as_ref()])?; + transaction.execute(DELETE_SUFFIX_WRITE, [this_msg.as_ref()])?; Ok(()) } -/// Reports a removed local write using the payload supplied by the SDK. -fn orphan_write( - transaction: &rusqlite::Transaction<'_>, - write: &ObservedWrite, -) -> Result<(), Error> { +/// Removes a write from the live suffix and marks it displaced if local. +fn orphan_write(transaction: &rusqlite::Transaction<'_>, this_msg: &MsgId) -> Result<(), Error> { let local_write = transaction - .query_row( - "SELECT content_digest FROM __logos_sql_local_writes - WHERE this_msg = ?1 AND tx_id = ?2", - params![write.this_msg.as_ref(), write.tx_id.as_ref()], - |row| row.get::<_, Vec>(0), - ) + .query_row(SELECT_LOCAL_SUFFIX_WRITE, [this_msg.as_ref()], |row| { + Ok((row.get::<_, Vec>(0)?, row.get::<_, Vec>(1)?)) + }) .optional()?; - if let Some(digest) = local_write { - if digest.as_slice() != ChannelWrite::decode(&write.payload)?.content_digest() { - return Err(Error::InvalidLocalState( - "orphaned write does not match our publication", - )); - } + if let Some((tx_id, payload)) = local_write { + let tx_id = decode_tx_id(tx_id)?; + + transaction.execute( + INSERT_DISPLACED_WRITE, + params![tx_id.as_ref(), this_msg.as_ref()], + )?; - record_displacement(transaction, write.tx_id, "orphaned", &write.payload)?; + record_displacement(transaction, tx_id, "orphaned", &payload)?; } + transaction.execute(DELETE_SUFFIX_WRITE, [this_msg.as_ref()])?; + Ok(()) } -/// Recognizes the return of our displaced write, without storing foreign -/// history. -fn adopt_write( - transaction: &rusqlite::Transaction<'_>, - write: &ObservedWrite, -) -> Result<(), Error> { - let original: Option> = transaction - .query_row(SELECT_DISPLACED_PAYLOAD, [write.tx_id.as_ref()], |row| { - row.get(0) - }) +/// Adds a write to the live suffix; marks a displaced local write live again. +fn adopt_write(transaction: &rusqlite::Transaction<'_>, write: &SuffixWrite) -> Result<(), Error> { + let restored_tx_id = transaction + .query_row( + SELECT_DISPLACED_WRITE_AT_POSITION, + [write.this_msg.as_ref()], + |row| row.get::<_, Vec>(0), + ) .optional()?; - let Some(original) = original else { - return Ok(()); - }; - let content_digest = ChannelWrite::decode(&write.payload)?.content_digest(); - if ChannelWrite::decode(&original)?.content_digest() != content_digest { - return Ok(()); + if let Some(tx_id) = restored_tx_id + && decode_tx_id(tx_id)? != write.tx_id + { + return Err(Error::InvalidLocalState( + "stored displaced write does not match its channel payload", + )); } - set_write_status(transaction, write.tx_id, WriteStatus::Live)?; - transaction.execute(CLEAR_DISPLACEMENT, [write.tx_id.as_ref()])?; + let restored_local = transaction.query_row( + SELECT_DISPLACED_WRITE_BY_TX, + [write.tx_id.as_ref()], + |row| row.get::<_, bool>(0), + )?; + + if restored_local { + set_write_status(transaction, write.tx_id, WriteStatus::Live)?; + transaction.execute(CLEAR_DISPLACEMENT, [write.tx_id.as_ref()])?; + transaction.execute(DELETE_DISPLACED_WRITE_BY_TX, [write.tx_id.as_ref()])?; + } transaction.execute( - UPSERT_LOCAL_WRITE, + INSERT_SUFFIX_WRITE, params![ write.this_msg.as_ref(), write.tx_id.as_ref(), - content_digest + write.payload, + write.local || restored_local ], )?; @@ -1187,7 +1084,7 @@ fn configure_connection(conn: &Connection) -> Result<(), Error> { fn apply_channel_write( database: &mut ReplicatedDatabase, - write: &ChannelWrite, + write: &ChannelInscription, ) -> Result<(), Error> { let content_digest = write.content_digest(); let replay = database.functions.replay(&write.captured_function_calls); @@ -1292,38 +1189,16 @@ fn apply_statements( hooks: &ApplicationHooks, transaction: &Transaction, ) -> Result<(), Error> { - let previous_length = db_transaction.set_limit(Limit::SQLITE_LIMIT_LENGTH, MAX_SQL_BYTES)?; - let previous_sql_length = - db_transaction.set_limit(Limit::SQLITE_LIMIT_SQL_LENGTH, MAX_SQL_BYTES)?; - hooks.arm(); let result = transaction.statements().iter().try_for_each(|statement| { - if hooks - .work - .fetch_add(SQL_PROGRESS_INTERVAL as u64, Ordering::Relaxed) - >= MAX_SQL_STEPS - { - return Err(Error::ExecutionBudgetExceeded); - } - db_transaction.execute(statement.sql(), params_from_iter(statement.params()))?; Ok::<_, Error>(()) }); - // Bookkeeping and rollback must not inherit the application's spent budget. hooks.disarm(); - // Internal rows also contain replication metadata and encoded payloads; - // they are not application rows and must not inherit these size limits. - db_transaction.set_limit(Limit::SQLITE_LIMIT_LENGTH, previous_length)?; - db_transaction.set_limit(Limit::SQLITE_LIMIT_SQL_LENGTH, previous_sql_length)?; - - if hooks.work.load(Ordering::Relaxed) > MAX_SQL_STEPS { - return Err(Error::ExecutionBudgetExceeded); - } - result } @@ -1419,6 +1294,14 @@ fn decode_tx_id(bytes: Vec) -> Result { Ok(bytes.into()) } +fn decode_msg_id(bytes: Vec) -> Result { + let bytes: [u8; 32] = bytes + .try_into() + .map_err(|_| Error::InvalidLocalState("stored message id is malformed"))?; + + Ok(bytes.into()) +} + fn checkpoint_options() -> impl bincode::Options { bincode::DefaultOptions::new() .with_little_endian() @@ -1427,9 +1310,7 @@ fn checkpoint_options() -> impl bincode::Options { } #[cfg(test)] -pub mod tests { - use std::path::Path; - +mod tests { use lb_zone_sdk::{ node_types::{HeaderId, MsgId, Slot}, sequencer::SequencerCheckpoint, @@ -1439,22 +1320,14 @@ pub mod tests { use super::{Databases, PendingPublish}; use crate::{ - PublicationConfig, error::Error, protocol::{ - CapturedFunction, CapturedFunctionCall, CapturedFunctionCalls, ChannelWrite, Statement, - Transaction, TxId, + CapturedFunction, CapturedFunctionCall, CapturedFunctionCalls, ChannelInscription, + Statement, Transaction, TxId, }, status::WriteStatus, }; - /// The most bytes an inscription may take in the tests' era: today's limit. - pub const MAX_INSCRIPTION_SIZE: u32 = 1_835_008; - - pub fn open_databases(directory: &Path) -> Result { - Databases::open(directory, PublicationConfig::default().max_pending_writes) - } - fn checkpoint(byte: u8, slot: u64) -> SequencerCheckpoint { SequencerCheckpoint { last_msg_id: MsgId::root(), @@ -1491,7 +1364,7 @@ pub mod tests { transaction: &Transaction, this_msg: MsgId, ) -> PendingPublish { - db.commit_local_write(tx_id, transaction, MAX_INSCRIPTION_SIZE) + db.commit_local_write(tx_id, transaction) .expect("local write should commit"); let pending = db @@ -1505,14 +1378,6 @@ pub mod tests { pending } - fn observed(pending: &PendingPublish, this_msg: MsgId) -> super::ObservedWrite { - super::ObservedWrite { - this_msg, - tx_id: pending.tx_id, - payload: pending.payload.clone(), - } - } - fn row_values(connection: &Connection, table: &str) -> Vec { connection .query_row(&format!("SELECT * FROM {table}"), [], |row| { @@ -1525,7 +1390,7 @@ pub mod tests { fn rejected_application_sql(sql: &str) -> Error { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value INTEGER)", []) @@ -1533,7 +1398,7 @@ pub mod tests { let transaction = transaction(sql); let error = db - .commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &transaction) .expect_err("application SQL should be rejected"); assert!( @@ -1584,13 +1449,13 @@ pub mod tests { fn checkpoint_survives_reopen() { let dir = TempDir::new().expect("temporary directory should be created"); let expected = checkpoint(7, 42); - let db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.persist_checkpoint(&expected) .expect("checkpoint should persist"); drop(db); - let db = open_databases(dir.path()).expect("databases should reopen"); + let db = Databases::open(dir.path()).expect("databases should reopen"); let actual = db .load_checkpoint() .expect("checkpoint should load") @@ -1600,166 +1465,10 @@ pub mod tests { assert_eq!(actual.lib_slot, expected.lib_slot); } - #[test] - fn a_full_publication_queue_does_not_apply_the_next_write() { - let dir = TempDir::new().unwrap(); - let capacity = 3; - let mut db = Databases::open(dir.path(), capacity.try_into().unwrap()).unwrap(); - db.commit_local_write( - TxId::generate(), - &transaction("CREATE TABLE items(value TEXT)"), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - - for _ in 1..capacity { - db.commit_local_write(TxId::generate(), &insert("queued"), MAX_INSCRIPTION_SIZE) - .unwrap(); - } - - assert!(matches!( - db.commit_local_write(TxId::generate(), &insert("overflow"), MAX_INSCRIPTION_SIZE), - Err(Error::PublishPending) - )); - let overflow: i64 = db - .live - .query_row( - "SELECT count(*) FROM items WHERE value = 'overflow'", - [], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(overflow, 0); - assert_eq!(db.pending_writes().unwrap().len(), capacity); - } - - #[test] - fn publication_and_rebuild_make_room_for_new_writes() { - let dir = TempDir::new().unwrap(); - let capacity = 2.try_into().unwrap(); - let mut db = Databases::open(dir.path(), capacity).unwrap(); - let schema = transaction("CREATE TABLE items(value TEXT)"); - - db.commit_local_write(TxId::generate(), &schema, MAX_INSCRIPTION_SIZE) - .unwrap(); - assert!( - db.commit_local_write(TxId::generate(), &schema, MAX_INSCRIPTION_SIZE) - .is_err() - ); - db.commit_local_write(TxId::generate(), &insert("queued"), MAX_INSCRIPTION_SIZE) - .unwrap(); - drop(db); - - let mut db = Databases::open(dir.path(), capacity).unwrap(); - assert!(matches!( - db.commit_local_write(TxId::generate(), &insert("overflow"), MAX_INSCRIPTION_SIZE), - Err(Error::PublishPending) - )); - - let pending = db.pending_writes().unwrap(); - db.complete_batch(&checkpoint(1, 1), MsgId::from([1; 32]), &pending) - .unwrap(); - db.commit_local_write( - TxId::generate(), - &insert("after publication"), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - db.commit_local_write( - TxId::generate(), - &insert("fills queue"), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - - let rebuild = db.begin_live_rebuild().unwrap(); - db.finish_live_rebuild(rebuild).unwrap(); - db.commit_local_write(TxId::generate(), &schema, MAX_INSCRIPTION_SIZE) - .unwrap(); - db.commit_local_write( - TxId::generate(), - &insert("after rebuild"), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - - assert_eq!(db.pending_writes().unwrap().len(), 2); - assert_eq!( - row_values(&db.live, "items"), - vec![Value::Text("after rebuild".to_owned())] - ); - } - - #[test] - fn batch_publication_recovers_before_removing_queued_writes() { - let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); - let first = TxId::generate(); - let second = TxId::generate(); - db.commit_local_write( - first, - &transaction("CREATE TABLE items(value TEXT)"), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - db.commit_local_write(second, &insert("second"), MAX_INSCRIPTION_SIZE) - .unwrap(); - let publication = crate::publication::Publication::prepare( - db.pending_writes().unwrap(), - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - - // Stop after control.db commits, before any queue record is removed. - db.live.connection.execute_batch( - "CREATE TRIGGER interrupt_queue_removal BEFORE DELETE ON __logos_sql_pending_publish - BEGIN SELECT RAISE(FAIL, 'interrupted'); END;" - ).unwrap(); - - assert!( - db.complete_batch(&checkpoint(1, 1), MsgId::from([1; 32]), &publication.writes) - .is_err() - ); - drop(db); - - let mut db = open_databases(dir.path()).unwrap(); - let pending = db.pending_writes().unwrap(); - assert_eq!(pending.len(), 2); - assert_eq!( - crate::publication::Publication::matches_pending(&publication.payload, &pending) - .unwrap(), - Some(2) - ); - - db.live - .connection - .execute_batch("DROP TRIGGER interrupt_queue_removal") - .unwrap(); - db.complete_batch(&checkpoint(1, 1), MsgId::from([1; 32]), &pending) - .unwrap(); - - assert!(db.pending_writes().unwrap().is_empty()); - let published: i64 = db - .control - .query_row("SELECT count(*) FROM __logos_sql_local_writes", [], |row| { - row.get(0) - }) - .unwrap(); - assert_eq!(published, 2); - assert_eq!( - row_values(&db.live, "items"), - vec![Value::Text("second".to_owned())] - ); - assert_eq!(db.write_status(first).unwrap(), Some(WriteStatus::Live)); - assert_eq!(db.write_status(second).unwrap(), Some(WriteStatus::Live)); - } - #[test] fn displacements_keep_the_original_write_order() { let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); + let mut db = Databases::open(dir.path()).unwrap(); let first_id = TxId::from([3; 32]); let second_id = TxId::from([2; 32]); let pending_id = TxId::from([1; 32]); @@ -1772,23 +1481,16 @@ pub mod tests { &transaction("CREATE TABLE items(value TEXT)"), first_position, ); - let second = publish_write(&mut db, second_id, &insert("second"), second_position); + publish_write(&mut db, second_id, &insert("second"), second_position); - db.commit_local_write(pending_id, &insert("pending"), MAX_INSCRIPTION_SIZE) + db.commit_local_write(pending_id, &insert("pending")) .unwrap(); let pending = db.pending_publish().unwrap().unwrap(); // Rebuilds record the pending displacement before the older orphans. db.record_pending_write_displacement(&pending).unwrap(); - db.update_write_statuses( - &[], - &[ - observed(&first, first_position), - observed(&second, second_position), - ], - &[], - ) - .unwrap(); + db.apply_history_delta(&[], &[first_position, second_position], &[]) + .unwrap(); let expected = vec![first_id, second_id, pending_id]; let displaced_ids = |db: &Databases| { @@ -1801,13 +1503,21 @@ pub mod tests { assert_eq!(displaced_ids(&db), expected); - db.update_write_statuses(&[], &[], &[observed(&first, first_position)]) - .unwrap(); - db.update_write_statuses(&[], &[observed(&first, first_position)], &[]) - .unwrap(); + db.apply_history_delta( + &[], + &[], + &[super::SuffixWrite { + this_msg: first_position, + tx_id: first_id, + payload: first.payload, + local: true, + }], + ) + .unwrap(); + db.apply_history_delta(&[], &[first_position], &[]).unwrap(); drop(db); - let db = open_databases(dir.path()).unwrap(); + let db = Databases::open(dir.path()).unwrap(); assert_eq!(displaced_ids(&db), expected); } @@ -1815,7 +1525,7 @@ pub mod tests { fn displaced_sql_can_be_resubmitted_after_rebuild_and_restart() { for published in [false, true] { let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); + let mut db = Databases::open(dir.path()).unwrap(); let (original_id, original) = crate::TransactionBuilder::new( "CREATE TABLE credentials(label TEXT, secret BLOB, updated INTEGER)", ) @@ -1825,16 +1535,14 @@ pub mod tests { .finish() .unwrap(); - db.commit_local_write(original_id, &original, MAX_INSCRIPTION_SIZE) - .unwrap(); + db.commit_local_write(original_id, &original).unwrap(); let pending = db.pending_publish().unwrap().unwrap(); if published { let position = MsgId::from([7; 32]); db.complete_publish(&checkpoint(1, 1), position, &pending) .unwrap(); - db.update_write_statuses(&[], &[observed(&pending, position)], &[]) - .unwrap(); + db.apply_history_delta(&[], &[position], &[]).unwrap(); } else { db.record_pending_write_displacement(&pending).unwrap(); } @@ -1843,7 +1551,7 @@ pub mod tests { db.finish_live_rebuild(rebuild).unwrap(); drop(db); - let mut db = open_databases(dir.path()).unwrap(); + let mut db = Databases::open(dir.path()).unwrap(); let displacement = db.unhandled_displacements().unwrap().remove(0); let retry_id = TxId::generate(); let retry = &displacement.transaction; @@ -1852,11 +1560,10 @@ pub mod tests { assert_eq!(retry, &original); db.mark_displacement_handled(&displacement).unwrap(); - db.commit_local_write(retry_id, retry, MAX_INSCRIPTION_SIZE) - .unwrap(); + db.commit_local_write(retry_id, retry).unwrap(); let retried = db.pending_publish().unwrap().unwrap(); - let inscription = ChannelWrite::decode(&retried.payload).unwrap(); + let inscription = ChannelInscription::decode(&retried.payload).unwrap(); assert_eq!(inscription.tx_id, retry_id); assert_eq!(inscription.transaction, original); } @@ -1865,15 +1572,13 @@ pub mod tests { #[test] fn handling_a_displacement_survives_restart_without_erasing_recovery_state() { let dir = TempDir::new().unwrap(); - let mut db = open_databases(dir.path()).unwrap(); + let mut db = Databases::open(dir.path()).unwrap(); db.commit_local_write( TxId::generate(), &transaction("CREATE TABLE local_write(value INTEGER)"), - MAX_INSCRIPTION_SIZE, ) .unwrap(); let pending = db.pending_publish().unwrap().unwrap(); - db.require_rebuild().unwrap(); db.record_pending_write_displacement(&pending).unwrap(); let displacement = db.unhandled_displacements().unwrap()[0].clone(); @@ -1884,7 +1589,7 @@ pub mod tests { db.mark_displacement_handled(&displacement).unwrap(); drop(db); - let mut db = open_databases(dir.path()).unwrap(); + let mut db = Databases::open(dir.path()).unwrap(); db.record_pending_write_displacement(&pending).unwrap(); assert!(!db.has_unhandled_displacements().unwrap()); @@ -1892,13 +1597,16 @@ pub mod tests { db.write_status(pending.tx_id).unwrap(), Some(WriteStatus::Displaced) ); - assert!(db.rebuild_required().unwrap()); + assert!( + db.pending_write_rebuild_was_interrupted(pending.tx_id) + .unwrap() + ); } #[test] fn republished_write_returns_to_live_status_at_its_new_position() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let tx_id = TxId::generate(); let original_position = MsgId::from([7; 32]); let pending = publish_write( @@ -1908,7 +1616,7 @@ pub mod tests { original_position, ); - db.update_write_statuses(&[], &[observed(&pending, original_position)], &[]) + db.apply_history_delta(&[], &[original_position], &[]) .expect("local write should be orphaned"); let first_displacement = db.unhandled_displacements().unwrap()[0].clone(); @@ -1917,12 +1625,17 @@ pub mod tests { crate::DisplacementReason::Orphaned ); - db.mark_displacement_handled(&first_displacement).unwrap(); - drop(db); - let mut db = open_databases(dir.path()).unwrap(); - - db.update_write_statuses(&[], &[], &[observed(&pending, MsgId::from([8; 32]))]) - .expect("the same write should be recognized at its new position"); + db.apply_history_delta( + &[], + &[], + &[super::SuffixWrite { + this_msg: MsgId::from([8; 32]), + tx_id, + payload: pending.payload, + local: false, + }], + ) + .expect("different channel position should be retained as a foreign write"); assert_eq!( db.write_status(tx_id).expect("write status should load"), @@ -1931,7 +1644,7 @@ pub mod tests { assert_eq!(db.unhandled_displacements().unwrap(), []); assert!(!db.has_unhandled_displacements().unwrap()); - db.update_write_statuses(&[], &[observed(&pending, MsgId::from([8; 32]))], &[]) + db.apply_history_delta(&[], &[MsgId::from([8; 32])], &[]) .unwrap(); let second_displacement = db.unhandled_displacements().unwrap()[0].clone(); assert_ne!(first_displacement, second_displacement); @@ -1942,7 +1655,7 @@ pub mod tests { db.unhandled_displacements().unwrap(), vec![second_displacement.clone()] ); - db.update_write_statuses(&[], &[observed(&pending, MsgId::from([8; 32]))], &[]) + db.apply_history_delta(&[], &[MsgId::from([8; 32])], &[]) .unwrap(); assert_eq!( db.unhandled_displacements().unwrap(), @@ -1961,7 +1674,7 @@ pub mod tests { #[test] fn invalid_stored_write_status_returns_invalid_local_state() { let dir = TempDir::new().expect("temporary directory should be created"); - let db = open_databases(dir.path()).expect("databases should open"); + let db = Databases::open(dir.path()).expect("databases should open"); let tx_id = TxId::generate(); db.control @@ -1983,18 +1696,18 @@ pub mod tests { #[test] fn finalized_write_no_longer_needs_displacement_review_after_restart() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let tx_id = TxId::generate(); let this_msg = MsgId::from([7; 32]); - let pending = publish_write( + publish_write( &mut db, tx_id, &transaction("CREATE TABLE local_write(value INTEGER)"), this_msg, ); - db.update_write_statuses(&[], &[observed(&pending, this_msg)], &[]) + db.apply_history_delta(&[], &[this_msg], &[]) .expect("local write should be displaced"); assert!(db.has_unhandled_displacements().unwrap()); @@ -2005,7 +1718,7 @@ pub mod tests { Some(WriteStatus::Displaced) ); - db.update_write_statuses(&[this_msg], &[], &[]) + db.apply_history_delta(&[this_msg], &[], &[]) .expect("local write should finalize"); assert!(!db.has_unhandled_displacements().unwrap()); @@ -2017,7 +1730,7 @@ pub mod tests { ); drop(db); - let db = open_databases(dir.path()).expect("databases should reopen"); + let db = Databases::open(dir.path()).expect("databases should reopen"); assert!(!db.has_unhandled_displacements().unwrap()); assert_eq!(db.unhandled_displacements().unwrap(), []); @@ -2031,7 +1744,7 @@ pub mod tests { #[test] fn application_write_and_pending_publish_commit_together() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value TEXT NOT NULL)", []) @@ -2039,7 +1752,7 @@ pub mod tests { let transaction = insert("hello"); let tx_id = db - .commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &transaction) .expect("write should commit"); let count: i64 = db @@ -2064,7 +1777,7 @@ pub mod tests { #[test] fn state_databases_have_the_same_internal_schema() { let dir = TempDir::new().expect("temporary directory should be created"); - let db = open_databases(dir.path()).expect("databases should open"); + let db = Databases::open(dir.path()).expect("databases should open"); assert_eq!(internal_schema(&db.lib), internal_schema(&db.live)); } @@ -2072,7 +1785,7 @@ pub mod tests { #[test] fn repeated_write_is_a_new_transaction() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value TEXT NOT NULL)", []) @@ -2080,13 +1793,13 @@ pub mod tests { let transaction = insert("hello"); let first_tx_id = db - .commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &transaction) .expect("first write should commit"); db.clear_pending_publish(first_tx_id) .expect("first publication should complete"); let second_tx_id = db - .commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + .commit_local_write(TxId::generate(), &transaction) .expect("second write should commit"); assert_ne!(second_tx_id, first_tx_id); @@ -2102,7 +1815,7 @@ pub mod tests { #[test] fn function_results_are_replayed_exactly() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let schema = "CREATE TABLE captured( random_value, random_blob_value, @@ -2140,7 +1853,7 @@ pub mod tests { CURRENT_TIMESTAMP )", ); - db.commit_local_write(TxId::generate(), &write, MAX_INSCRIPTION_SIZE) + db.commit_local_write(TxId::generate(), &write) .expect("local write should commit"); let pending = db @@ -2148,7 +1861,7 @@ pub mod tests { .expect("pending publication should load") .expect("pending publication should exist"); let channel_inscription = - ChannelWrite::decode(&pending.payload).expect("payload should decode"); + ChannelInscription::decode(&pending.payload).expect("payload should decode"); let functions = channel_inscription .captured_function_calls .as_slice() @@ -2186,7 +1899,7 @@ pub mod tests { #[test] fn function_calls_inside_defaults_and_triggers_are_captured() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let schema = " CREATE TABLE items( value TEXT, @@ -2205,7 +1918,7 @@ pub mod tests { } let write = transaction("INSERT INTO items(value) VALUES ('hello')"); - db.commit_local_write(TxId::generate(), &write, MAX_INSCRIPTION_SIZE) + db.commit_local_write(TxId::generate(), &write) .expect("local write should commit"); let pending = db @@ -2213,7 +1926,7 @@ pub mod tests { .expect("pending publication should load") .expect("pending publication should exist"); let channel_inscription = - ChannelWrite::decode(&pending.payload).expect("payload should decode"); + ChannelInscription::decode(&pending.payload).expect("payload should decode"); assert_eq!( channel_inscription.captured_function_calls.as_slice().len(), @@ -2230,13 +1943,13 @@ pub mod tests { #[test] fn missing_function_result_rejects_channel_inscription() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value INTEGER)", []) .expect("application table should be created"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([7; 32]), transaction: transaction("INSERT INTO items VALUES (random())"), captured_function_calls: CapturedFunctionCalls::empty(), @@ -2258,7 +1971,7 @@ pub mod tests { #[test] fn unused_function_result_rejects_channel_inscription() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value INTEGER)", []) @@ -2266,7 +1979,7 @@ pub mod tests { let captured = CapturedFunctionCall::new(CapturedFunction::Random, Value::Integer(7)) .expect("captured result should be valid"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([7; 32]), transaction: transaction("INSERT INTO items VALUES (1)"), captured_function_calls: CapturedFunctionCalls::new(vec![captured]) @@ -2286,184 +1999,22 @@ pub mod tests { ); } - #[test] - fn endless_local_sql_rolls_back_and_allows_the_next_write() { - let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); - db.live - .execute("CREATE TABLE items(value TEXT)", []) - .unwrap(); - - let write = Transaction::new(vec![ - Statement::new( - "INSERT INTO items VALUES ('rolled back')".to_owned(), - vec![], - ) - .unwrap(), - Statement::new( - "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM c) - SELECT x FROM c WHERE x < 0" - .to_owned(), - vec![], - ) - .unwrap(), - ]) - .unwrap(); - - assert!(matches!( - db.commit_local_write(TxId::generate(), &write, MAX_INSCRIPTION_SIZE), - Err(Error::ExecutionBudgetExceeded) - )); - assert!(db.pending_publish().unwrap().is_none()); - - db.commit_local_write( - TxId::generate(), - &insert("next write"), - MAX_INSCRIPTION_SIZE, - ) - .expect("a rejected transaction must not block later writes"); - - assert_eq!( - row_values(&db.live, "items"), - vec![Value::Text("next write".into())] - ); - let count: i64 = db - .live - .query_row("SELECT count(*) FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(count, 1); - } - - #[test] - fn oversized_values_roll_back_and_allow_the_next_write() { - for function in ["zeroblob", "randomblob"] { - let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); - db.live - .execute("CREATE TABLE items(value TEXT)", []) - .unwrap(); - - let write = Transaction::new(vec![ - Statement::new( - "INSERT INTO items VALUES ('rolled back')".to_owned(), - vec![], - ) - .unwrap(), - Statement::new( - format!("INSERT INTO items VALUES ({function}(?1))"), - vec![Value::Integer(i64::from(super::MAX_SQL_BYTES) + 1)], - ) - .unwrap(), - ]) - .unwrap(); - - let error = db - .commit_local_write(TxId::generate(), &write, MAX_INSCRIPTION_SIZE) - .unwrap_err(); - assert!( - matches!(error, Error::Database(ref error) - if super::is_deterministic_sql_error(error)), - "{function}: {error:?}" - ); - assert!(db.pending_publish().unwrap().is_none()); - - db.commit_local_write( - TxId::generate(), - &insert("next write"), - MAX_INSCRIPTION_SIZE, - ) - .expect("oversized values must not block later writes"); - assert_eq!( - row_values(&db.live, "items"), - vec![Value::Text("next write".into())] - ); - } - } - - #[test] - fn short_statements_cannot_bypass_the_execution_budget() { - let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); - db.live - .execute("CREATE TABLE items(value INTEGER)", []) - .unwrap(); - - let count = (super::MAX_SQL_STEPS / super::SQL_PROGRESS_INTERVAL as u64 + 1) as usize; - let statement = Statement::new("INSERT INTO items VALUES (1)".to_owned(), vec![]).unwrap(); - let write = Transaction::new(vec![statement; count]).unwrap(); - - assert!(matches!( - db.commit_local_write(TxId::generate(), &write, MAX_INSCRIPTION_SIZE), - Err(Error::ExecutionBudgetExceeded) - )); - let count: i64 = db - .live - .query_row("SELECT count(*) FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(count, 0); - assert!(db.pending_publish().unwrap().is_none()); - } - - #[test] - fn statements_share_one_execution_budget() { - let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); - db.live - .execute("CREATE TABLE items(value INTEGER)", []) - .unwrap(); - - // One bounded calculation fits, but two in one transaction exceed the - // allowance. Splitting expensive work into statements must not evade it. - let statement = Statement::new( - "WITH RECURSIVE c(x) AS ( - SELECT 1 UNION ALL SELECT x+1 FROM c WHERE x < 300000 - ) INSERT INTO items SELECT sum(x) FROM c" - .to_owned(), - vec![], - ) - .unwrap(); - let write = ChannelWrite { - tx_id: TxId::generate(), - transaction: Transaction::new(vec![statement.clone()]).unwrap(), - captured_function_calls: CapturedFunctionCalls::empty(), - }; - - db.apply_adopted_write(&write) - .expect("one calculation fits"); - - let expensive = ChannelWrite { - tx_id: TxId::generate(), - transaction: Transaction::new(vec![statement.clone(), statement]).unwrap(), - captured_function_calls: CapturedFunctionCalls::empty(), - }; - assert!(matches!( - db.apply_adopted_write(&expensive), - Err(Error::ExecutionBudgetExceeded) - )); - - let count: i64 = db - .live - .query_row("SELECT count(*) FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(count, 1, "neither statement of the rejected write commits"); - } - #[test] fn reused_transaction_id_with_different_content_is_rejected() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value TEXT NOT NULL)", []) .expect("application table should be created"); let tx_id = TxId::from([7; 32]); - let first = ChannelWrite { + let first = ChannelInscription { tx_id, transaction: insert("first"), captured_function_calls: CapturedFunctionCalls::empty(), }; - let conflicting = ChannelWrite { + let conflicting = ChannelInscription { tx_id, transaction: insert("conflicting"), captured_function_calls: CapturedFunctionCalls::empty(), @@ -2489,7 +2040,7 @@ pub mod tests { #[test] fn conflicting_finalized_write_does_not_modify_lib() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); for connection in [&db.lib, &db.live] { connection @@ -2498,12 +2049,12 @@ pub mod tests { } let tx_id = TxId::from([7; 32]); - let first = ChannelWrite { + let first = ChannelInscription { tx_id, transaction: insert("first"), captured_function_calls: CapturedFunctionCalls::empty(), }; - let conflicting = ChannelWrite { + let conflicting = ChannelInscription { tx_id, transaction: insert("conflicting"), captured_function_calls: CapturedFunctionCalls::empty(), @@ -2529,9 +2080,9 @@ pub mod tests { #[test] fn application_write_can_create_persistent_schema() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let transaction = transaction("CREATE TABLE items(value TEXT NOT NULL)"); - db.commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + db.commit_local_write(TxId::generate(), &transaction) .expect("schema write should commit"); db.live @@ -2543,7 +2094,7 @@ pub mod tests { fn application_sql_cannot_control_wrapper_transaction() { for control in ["COMMIT", "ROLLBACK", "SAVEPOINT application"] { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value TEXT NOT NULL)", []) @@ -2558,7 +2109,7 @@ pub mod tests { Statement::new(control.to_owned(), Vec::new()).expect("statement should be valid"), ]) .expect("transaction should be valid"); - db.commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + db.commit_local_write(TxId::generate(), &transaction) .expect_err("transaction control should be rejected"); let count: i64 = db @@ -2625,17 +2176,17 @@ pub mod tests { #[test] fn reserved_name_check_accepts_non_ascii_identifiers() { let dir = TempDir::new().expect("temporary directory should be created"); - let mut db = open_databases(dir.path()).expect("databases should open"); + let mut db = Databases::open(dir.path()).expect("databases should open"); let transaction = transaction("CREATE TABLE aaaaaaaaaa\u{65e5}(value INTEGER)"); - db.commit_local_write(TxId::generate(), &transaction, MAX_INSCRIPTION_SIZE) + db.commit_local_write(TxId::generate(), &transaction) .expect("non-reserved Unicode name should be accepted"); } #[test] fn application_read_connection_is_read_only() { let dir = TempDir::new().expect("temporary directory should be created"); - let db = open_databases(dir.path()).expect("databases should open"); + let db = Databases::open(dir.path()).expect("databases should open"); db.live .execute("CREATE TABLE items(value TEXT NOT NULL)", []) diff --git a/logos_sql/src/error.rs b/logos_sql/src/error.rs index 7b49d07..df51116 100644 --- a/logos_sql/src/error.rs +++ b/logos_sql/src/error.rs @@ -48,8 +48,8 @@ pub enum Error { #[error("invalid local state: {0}")] InvalidLocalState(&'static str), - /// The local publication queue is full or its SDK checkpoint needs saving. - #[error("publication is pending; retry once the local queue can accept writes")] + /// A local write is committed but has not yet been accepted by `ZoneSDK`. + #[error("a committed write is still waiting for ZoneSDK to accept it")] PublishPending, /// The encoded transaction does not conform to the `λSQL` protocol. @@ -61,10 +61,6 @@ pub enum Error { #[error("channel SQL was rejected: {0}")] RejectedSql(#[source] rusqlite::Error), - /// The application transaction exceeded the SQLite execution allowance. - #[error("SQL transaction exceeded its execution budget")] - ExecutionBudgetExceeded, - /// The encoded payload exceeds the inscription limit. #[error("transaction is too large for one inscription")] InscriptionTooLarge, diff --git a/logos_sql/src/functions.rs b/logos_sql/src/functions.rs index 084d71c..36af653 100644 --- a/logos_sql/src/functions.rs +++ b/logos_sql/src/functions.rs @@ -13,14 +13,13 @@ use std::{ use rusqlite::{ Connection, functions::{Context, FunctionFlags}, - limits::Limit, params_from_iter, types::Value, }; use crate::{ error::Error, - protocol::{CapturedFunction, CapturedFunctionCall, CapturedFunctionCalls, MAX_BODY_BYTES}, + protocol::{CapturedFunction, CapturedFunctionCall, CapturedFunctionCalls}, }; /// Describes one `SQLite` built-in intercepted by the replicated connection. @@ -106,12 +105,7 @@ impl FunctionOverrides { /// used to evaluate the original built-ins during capture. pub fn install(connection: &Connection) -> Result { let state = Arc::new(Mutex::new(Mode::Passthrough)); - let evaluator = Connection::open_in_memory()?; - - // Captured functions must not allocate values that application SQL - // would reject on the writer connection. - evaluator.set_limit(Limit::SQLITE_LIMIT_LENGTH, MAX_BODY_BYTES as i32)?; - let evaluator = Arc::new(Mutex::new(evaluator)); + let evaluator = Arc::new(Mutex::new(Connection::open_in_memory()?)); for definition in FUNCTIONS { let state = Arc::clone(&state); diff --git a/logos_sql/src/lib.rs b/logos_sql/src/lib.rs index e1ec716..27efdb0 100644 --- a/logos_sql/src/lib.rs +++ b/logos_sql/src/lib.rs @@ -1,15 +1,9 @@ //! `λSQL`: replicated `SQLite` state over Logos Blockchain. //! //! Applications read through a normal `SQLite` connection and submit replicated -//! writes through [`LogosSql::execute`]. One dedicated runtime thread owns the -//! zone sequencer and database writer, so the SQL effects and pending -//! publication commit together before the payload is given to `ZoneSDK`. -//! -//! Application transactions share a fixed SQLite execution budget across their -//! statements. Exceeding it rolls back the transaction; channel replay records -//! the rejection and continues. SQL text, individual values and complete rows -//! are limited to 64 MiB, including values generated by SQL functions. These -//! limits are not a wall-clock timeout or a total memory/disk quota. +//! writes through [`LogosSql::execute`]. One runtime task owns the zone +//! sequencer and database writer, so the SQL effects and pending publication +//! commit together before the payload is given to `ZoneSDK`. mod applier; mod db; @@ -17,13 +11,12 @@ mod error; mod functions; mod logos_sql; mod protocol; -mod publication; mod runtime; mod sql; mod status; pub use error::Error; -pub use logos_sql::{LogosSql, LogosSqlConfig, PublicationConfig, WriterConfig}; +pub use logos_sql::{LogosSql, LogosSqlConfig, WriterConfig}; pub use protocol::TxId; pub use rusqlite::types::ToSql; pub use sql::TransactionBuilder; diff --git a/logos_sql/src/logos_sql.rs b/logos_sql/src/logos_sql.rs index e7eb9dd..8f51142 100644 --- a/logos_sql/src/logos_sql.rs +++ b/logos_sql/src/logos_sql.rs @@ -1,9 +1,6 @@ //! Application entry point: a running `λSQL` participant. -use std::{ - num::{NonZeroU16, NonZeroUsize}, - path::PathBuf, -}; +use std::path::PathBuf; use lb_key_management_system_service::keys::{Ed25519Key, ZkPublicKey}; use lb_zone_sdk::{ @@ -37,45 +34,17 @@ pub struct LogosSqlConfig { pub writer: Option, } -/// Credentials and publication preferences for publishing to the channel. +/// Credentials needed to publish writes to the channel. pub struct WriterConfig { /// Key used to sign published inscriptions. pub signing_key: Ed25519Key, /// Fee funding for published transactions. pub funding: FundingConfig, - /// Publication size and local backpressure settings. - pub publication: PublicationConfig, -} - -/// Local publication preferences. Replicas can read batches of any supported -/// size. -/// -/// Defaults to 1,024 transactions per inscription and 8,192 queued writes. -#[derive(Clone, Copy, Debug)] -pub struct PublicationConfig { - /// Maximum SQL transactions in one inscription. Byte limits can make a - /// batch smaller. Larger batches share publication fees across more writes. - pub max_transactions: NonZeroU16, - /// Maximum writes waiting for SDK publication. A full queue rejects new - /// writes with [`Error::PublishPending`] before committing them locally. - /// A separate 64 MiB byte limit also bounds the queue. - /// Larger queues absorb bursts but allow more unpublished local work to - /// accumulate; they do not increase the channel's capacity. - pub max_pending_writes: NonZeroUsize, -} - -impl Default for PublicationConfig { - fn default() -> Self { - Self { - max_transactions: NonZeroU16::new(1024).unwrap(), - max_pending_writes: NonZeroUsize::new(8192).unwrap(), - } - } } /// A running `λSQL` database. /// -/// `LogosSql` owns one background task on a dedicated thread. It owns both the +/// `LogosSql` owns one background task. That task is the only owner of both the /// `ZoneSDK` sequencer and the database writer. Dropping `LogosSql` aborts the /// task; call [`Self::shutdown`] to stop it gracefully and observe errors. pub struct LogosSql { @@ -97,26 +66,33 @@ impl LogosSql { /// /// Returns an error if no Tokio runtime is active, the local state cannot /// be opened, or the replication task stops before becoming ready. - /// Read-only startup also rejects state with pending local publications. + /// Read-only startup also rejects state with pending publications. pub async fn start(config: LogosSqlConfig) -> Result { tokio::runtime::Handle::try_current().map_err(|_| Error::RuntimeUnavailable)?; - let publication = config - .writer - .as_ref() - .map_or_else(PublicationConfig::default, |writer| writer.publication); - let db = Databases::open(&config.state_dir, publication.max_pending_writes)?; + let db = Databases::open(&config.state_dir)?; let lib_path = db.lib_path().to_owned(); let live_path = db.live_path().to_owned(); let checkpoint = db.load_checkpoint()?; let node = NodeHttpClient::new(CommonHttpClient::new(None), config.node_url); let read_only = config.writer.is_none(); - let writer = config.writer.unwrap_or_else(|| { + let writer = if let Some(writer) = config.writer { + writer + } else { + if db.pending_publish()?.is_some() + || checkpoint + .as_ref() + .is_some_and(|cp| !cp.pending_txs.is_empty()) + { + return Err(Error::InvalidLocalState( + "read-only startup cannot resume pending publications", + )); + } + // ZoneSDK's observer setup still requires a key and funding config. // Use a fresh key and inert funding; the runtime blocks publication. WriterConfig { - publication, signing_key: Ed25519Key::generate(&mut OsRng), funding: FundingConfig { funding_pk: ZkPublicKey::zero(), @@ -125,7 +101,7 @@ impl LogosSql { priority_fee_percent: 0, }, } - }); + }; let sequencer = ZoneSequencer::init( config.channel_id, @@ -135,14 +111,7 @@ impl LogosSql { checkpoint.clone(), ); - let runtime = runtime::spawn( - sequencer, - db, - config.channel_id, - checkpoint, - read_only, - publication.max_transactions, - )?; + let runtime = runtime::spawn(sequencer, db, config.channel_id, checkpoint, read_only); let mut logos_sql = Self { lib_path, @@ -180,8 +149,6 @@ impl LogosSql { /// /// A successful return means the SQL effects and recovery record are /// committed locally. Publication and finality remain asynchronous. - /// Nearby writes share an inscription, but each remains a separate SQL - /// transaction with its own identity and outcome. /// [`TransactionBuilder::tx_id`] exposes the same identity before the /// asynchronous call, allowing the application to record how transaction /// outcomes map to its own operations. @@ -338,21 +305,16 @@ mod tests { use tempfile::TempDir; use super::{LogosSql, LogosSqlConfig}; - use crate::{ - db::tests::{MAX_INSCRIPTION_SIZE, open_databases}, - error::Error, - sql::TransactionBuilder, - }; + use crate::{db::Databases, error::Error, sql::TransactionBuilder}; #[tokio::test] async fn read_only_start_does_not_abandon_a_pending_write() { let directory = TempDir::new().unwrap(); - let mut db = open_databases(directory.path()).unwrap(); + let mut db = Databases::open(directory.path()).unwrap(); let (tx_id, transaction) = TransactionBuilder::new("CREATE TABLE items(value INTEGER)") .finish() .unwrap(); - db.commit_local_write(tx_id, &transaction, MAX_INSCRIPTION_SIZE) - .unwrap(); + db.commit_local_write(tx_id, &transaction).unwrap(); drop(db); let result = LogosSql::start(LogosSqlConfig { @@ -365,7 +327,7 @@ mod tests { assert!(matches!(result, Err(Error::InvalidLocalState(_)))); - let db = open_databases(directory.path()).unwrap(); + let db = Databases::open(directory.path()).unwrap(); assert_eq!(db.pending_publish().unwrap().unwrap().tx_id, tx_id); } } diff --git a/logos_sql/src/protocol/fixtures.rs b/logos_sql/src/protocol/fixtures.rs index fbdb0ce..0d2ff4e 100644 --- a/logos_sql/src/protocol/fixtures.rs +++ b/logos_sql/src/protocol/fixtures.rs @@ -4,7 +4,7 @@ use lb_binary_codec::canonical::codec_fixtures; use rusqlite::types::Value; use super::{ - CapturedFunction, CapturedFunctionCall, CapturedFunctionCalls, ChannelBatch, ChannelWrite, + CapturedFunction, CapturedFunctionCall, CapturedFunctionCalls, ChannelInscription, SqlParameter, SqlText, Statement, Transaction, TxId, }; @@ -109,8 +109,8 @@ codec_fixtures!( transaction_fixture() => "010000000800000053454c454354203100000000" ); -fn channel_write_fixture() -> ChannelWrite { - ChannelWrite { +fn channel_inscription_fixture() -> ChannelInscription { + ChannelInscription { tx_id: TxId::from([3; 32]), transaction: transaction_fixture(), captured_function_calls: CapturedFunctionCalls::empty(), @@ -118,18 +118,8 @@ fn channel_write_fixture() -> ChannelWrite { } codec_fixtures!( - ChannelWrite, - channel_write_fixture() => concat!( - "0303030303030303030303030303030303030303030303030303030303030303", - "010000000800000053454c454354203100000000", - "00000000" - ) -); - -codec_fixtures!( - ChannelBatch, - ChannelBatch::new(vec![channel_write_fixture()]).expect("valid batch") => concat!( - "0100", + ChannelInscription, + channel_inscription_fixture() => concat!( "0303030303030303030303030303030303030303030303030303030303030303", "010000000800000053454c454354203100000000", "00000000" diff --git a/logos_sql/src/protocol/mod.rs b/logos_sql/src/protocol/mod.rs index 9b30632..0c6de87 100644 --- a/logos_sql/src/protocol/mod.rs +++ b/logos_sql/src/protocol/mod.rs @@ -1,13 +1,11 @@ //! SQL transactions exchanged by `λSQL` instances. -use std::{ - collections::HashSet, - fmt::{self, Display, Formatter}, -}; +use std::fmt::{self, Display, Formatter}; use blake2::{Blake2b, Digest as _, digest::consts::U32}; -use lb_binary_codec::canonical::{BinaryDecode, BinaryEncode}; +use lb_binary_codec::canonical::{BinaryCodec, BinaryDecode, BinaryEncode as _}; use lb_utils::bounded::{NonEmptyBoundedVec, UpperBoundedVec}; +use lb_zone_sdk::node_types::Inscription; use rand::RngCore as _; use rusqlite::types::{ToSql, ToSqlOutput, Value}; @@ -22,16 +20,15 @@ pub const PAYLOAD_MARKER: [u8; 9] = *b"LOGOS_SQL"; const PAYLOAD_VERSION: u16 = 2; const PAYLOAD_HEADER_LEN: usize = PAYLOAD_MARKER.len() + size_of::() + 1; +// Published bytes, including our header, must fit into one chain inscription. +const MAX_PAYLOAD_BYTES: usize = Inscription::MAX; + // Allow up to 64 MiB before compression. This caps decompression allocations; // the compressed payload must still fit the chain's smaller inscription limit. -pub const MAX_BODY_BYTES: usize = 64 * 1024 * 1024; - -// The batch count uses a u16 prefix. Local batching preferences can be smaller; -// encoded and decompressed byte limits still bound every inscription. -pub const MAX_BATCH_WRITES: usize = u16::MAX as usize; +const MAX_BODY_BYTES: usize = 64 * 1024 * 1024; /// Stable identity of one application write. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, BinaryEncode, BinaryDecode)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, BinaryCodec)] pub struct TxId([u8; 32]); impl Display for TxId { @@ -131,7 +128,7 @@ impl SqlParameter { } /// One parameterized SQL statement. -#[derive(Clone, Debug, PartialEq, BinaryEncode, BinaryDecode)] +#[derive(Clone, Debug, PartialEq, BinaryCodec)] pub struct Statement { sql: SqlText, params: UpperBoundedVec, @@ -166,7 +163,7 @@ impl Statement { } /// Statements applied atomically at one channel position. -#[derive(Clone, Debug, PartialEq, BinaryEncode, BinaryDecode)] +#[derive(Clone, Debug, PartialEq, BinaryCodec)] pub struct Transaction { statements: NonEmptyBoundedVec, } @@ -214,7 +211,7 @@ pub enum CapturedFunction { } /// One captured `SQLite` function call and the value returned locally. -#[derive(Clone, Debug, PartialEq, BinaryEncode, BinaryDecode)] +#[derive(Clone, Debug, PartialEq, BinaryCodec)] pub struct CapturedFunctionCall { pub function: CapturedFunction, pub result: SqlParameter, @@ -230,7 +227,7 @@ impl CapturedFunctionCall { } /// Function results captured while executing one replicated transaction. -#[derive(Clone, Debug, PartialEq, BinaryEncode, BinaryDecode)] +#[derive(Clone, Debug, PartialEq, BinaryCodec)] pub struct CapturedFunctionCalls { calls: UpperBoundedVec, } @@ -253,73 +250,16 @@ impl CapturedFunctionCalls { } } -/// One independently applied transaction within a channel inscription. -#[derive(Clone, Debug, PartialEq, BinaryEncode, BinaryDecode)] -pub struct ChannelWrite { +/// Transaction payload carried by a `λSQL` channel inscription. +#[derive(Clone, Debug, PartialEq, BinaryCodec)] +pub struct ChannelInscription { pub tx_id: TxId, pub transaction: Transaction, pub captured_function_calls: CapturedFunctionCalls, } -impl ChannelWrite { - pub fn encode(&self, max_inscription_size: u32) -> Result, Error> { - ChannelBatch::new(vec![self.clone()])?.encode(max_inscription_size) - } - - pub fn decode(payload: &[u8]) -> Result { - let mut writes = ChannelBatch::decode(payload)?.into_writes(); - - if writes.len() != 1 { - return Err(Error::InvalidPayload("expected one stored transaction")); - } - - Ok(writes.remove(0)) - } - - /// Retained batch members are local records, not separate inscriptions. - /// They need not individually meet the compressed chain-size limit. - pub fn encode_stored(&self) -> Result, Error> { - ChannelBatch::new(vec![self.clone()])?.encode_body() - } - - pub fn content_digest(&self) -> [u8; 32] { - Blake2b::::digest(self.encode_to_vec()).into() - } -} - -/// Ordered transactions sharing one publication, not one SQL transaction. -#[derive(Clone, Debug, PartialEq, BinaryEncode, BinaryDecode)] -pub struct ChannelBatch { - writes: NonEmptyBoundedVec, -} - -impl ChannelBatch { - pub fn new(writes: Vec) -> Result { - if !unique_write_ids(&writes) { - return Err(Error::InvalidTransaction("batch repeats a transaction id")); - } - - let writes = NonEmptyBoundedVec::try_from(writes) - .map_err(|_| Error::InvalidTransaction("invalid publication batch size"))?; - - Ok(Self { writes }) - } - - pub fn into_writes(self) -> Vec { - self.writes.into_inner() - } - - pub fn encode(&self, max_inscription_size: u32) -> Result, Error> { - let payload = self.encode_body()?; - - if payload.len() > max_inscription_size as usize { - return Err(Error::InscriptionTooLarge); - } - - Ok(payload) - } - - fn encode_body(&self) -> Result, Error> { +impl ChannelInscription { + pub fn encode(&self) -> Result, Error> { if self.encoded_length() > MAX_BODY_BYTES { return Err(Error::InvalidTransaction( "transaction exceeds the uncompressed size limit", @@ -327,7 +267,7 @@ impl ChannelBatch { } let (encoding, body) = compression::encode(self.encode_to_vec())?; - let mut payload = Vec::with_capacity(PAYLOAD_HEADER_LEN + body.len()); + let mut payload = Vec::with_capacity(payload_len(body.len())?); payload.extend_from_slice(&PAYLOAD_MARKER); payload.extend_from_slice(&PAYLOAD_VERSION.to_le_bytes()); @@ -338,10 +278,8 @@ impl ChannelBatch { } pub fn decode(payload: &[u8]) -> Result { - if payload.len() > MAX_BODY_BYTES + PAYLOAD_HEADER_LEN { - return Err(Error::InvalidPayload( - "stored payload exceeds the size limit", - )); + if payload.len() > MAX_PAYLOAD_BYTES { + return Err(Error::InvalidPayload("payload exceeds the protocol limit")); } let (header, body) = payload @@ -361,21 +299,13 @@ impl ChannelBatch { let body = compression::decode(header[PAYLOAD_HEADER_LEN - 1], body)?; - let batch = ::decode_all(&body, &()) - .map_err(|_| Error::InvalidPayload("body cannot be decoded"))?; - - if !unique_write_ids(batch.writes.as_slice()) { - return Err(Error::InvalidPayload("batch repeats a transaction id")); - } - - Ok(batch) + ::decode_all(&body, &()) + .map_err(|_| Error::InvalidPayload("body cannot be decoded")) } -} -fn unique_write_ids(writes: &[ChannelWrite]) -> bool { - let mut ids = HashSet::with_capacity(writes.len()); - - writes.iter().all(|write| ids.insert(write.tx_id)) + pub fn content_digest(&self) -> [u8; 32] { + Blake2b::::digest(self.encode_to_vec()).into() + } } /// A local write after its identity and channel payload have been encoded. @@ -390,16 +320,15 @@ impl EncodedWrite { tx_id: TxId, transaction: &Transaction, captured_function_calls: CapturedFunctionCalls, - max_inscription_size: u32, ) -> Result { - let write = ChannelWrite { + let channel_inscription = ChannelInscription { tx_id, transaction: transaction.clone(), captured_function_calls, }; - let content_digest = write.content_digest(); - let payload = write.encode(max_inscription_size)?; + let content_digest = channel_inscription.content_digest(); + let payload = channel_inscription.encode()?; Ok(Self { tx_id, @@ -409,6 +338,18 @@ impl EncodedWrite { } } +fn payload_len(body_len: usize) -> Result { + let payload_len = PAYLOAD_HEADER_LEN + .checked_add(body_len) + .ok_or(Error::InscriptionTooLarge)?; + + if payload_len > MAX_PAYLOAD_BYTES { + return Err(Error::InscriptionTooLarge); + } + + Ok(payload_len) +} + /// Returns whether an inscription belongs to `λSQL`. #[must_use] pub fn is_logos_sql_payload(payload: &[u8]) -> bool { @@ -417,40 +358,13 @@ pub fn is_logos_sql_payload(payload: &[u8]) -> bool { #[cfg(test)] mod tests { - use lb_binary_codec::canonical::BinaryEncode as _; use rand::{RngCore as _, SeedableRng as _, rngs::StdRng}; use rusqlite::types::Value; use super::{ - CapturedFunctionCalls, ChannelWrite, EncodedWrite, MAX_BODY_BYTES, Statement, Transaction, - TxId, + CapturedFunctionCalls, ChannelInscription, EncodedWrite, MAX_BODY_BYTES, MAX_PAYLOAD_BYTES, + Statement, Transaction, TxId, }; - use crate::db::tests::MAX_INSCRIPTION_SIZE; - - #[test] - fn a_batch_cannot_repeat_a_transaction_id() { - let write = ChannelWrite { - tx_id: TxId::generate(), - transaction: Transaction::new(vec![ - Statement::new("SELECT 1".to_owned(), Vec::new()).unwrap(), - ]) - .unwrap(), - captured_function_calls: CapturedFunctionCalls::empty(), - }; - assert!(super::ChannelBatch::new(vec![write.clone(), write.clone()]).is_err()); - - let mut payload = super::PAYLOAD_MARKER.to_vec(); - payload.extend_from_slice(&super::PAYLOAD_VERSION.to_le_bytes()); - payload.push(0); - payload.extend_from_slice(&2u16.to_le_bytes()); - payload.extend_from_slice(&write.encode_to_vec()); - payload.extend_from_slice(&write.encode_to_vec()); - - assert!(matches!( - super::ChannelBatch::decode(&payload), - Err(crate::Error::InvalidPayload(_)) - )); - } #[test] fn transaction_id_is_displayed_as_hex() { @@ -474,11 +388,10 @@ mod tests { TxId::generate(), &transaction, CapturedFunctionCalls::empty(), - MAX_INSCRIPTION_SIZE, ) .expect("payload should encode"); - let decoded = - ChannelWrite::decode(&encoded.payload).expect("channel inscription should decode"); + let decoded = ChannelInscription::decode(&encoded.payload) + .expect("channel inscription should decode"); assert_eq!(decoded.tx_id, encoded.tx_id); assert_eq!(decoded.transaction, transaction); @@ -491,17 +404,15 @@ mod tests { Statement::new("SELECT 1".to_owned(), Vec::new()).expect("statement should be valid"), ]) .expect("transaction should be valid"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([3; 32]), transaction, captured_function_calls: CapturedFunctionCalls::empty(), }; - let mut payload = write - .encode(MAX_INSCRIPTION_SIZE) - .expect("payload should encode"); + let mut payload = write.encode().expect("payload should encode"); payload.push(0); - assert!(ChannelWrite::decode(&payload).is_err()); + assert!(ChannelInscription::decode(&payload).is_err()); } #[test] @@ -510,7 +421,7 @@ mod tests { Statement::new("SELECT 1".to_owned(), Vec::new()).expect("statement should be valid"), ]) .expect("transaction should be valid"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([3; 32]), transaction, captured_function_calls: CapturedFunctionCalls::empty(), @@ -518,31 +429,30 @@ mod tests { let plain = hex::decode(concat!( "4c4f474f535f53514c020000", - "0100", "0303030303030303030303030303030303030303030303030303030303030303", "010000000800000053454c45435420310000000000000000" )) .expect("fixture should be valid hex"); let expected = hex::decode(concat!( - "4c4f474f535f53514c0200013a000000", - "7801636460260018191818388038d8d5c7d53944c110c80403002af9027c" + "4c4f474f535f53514c02000138000000", + "78018586c10d000004c44e7c8c610533889f1ffbcf422ca06993f2030190ad22c35b6df71c2abd027b" )) .expect("fixture should be valid hex"); assert_eq!( - ChannelWrite::decode(&expected).expect("compressed fixture should decode"), + ChannelInscription::decode(&expected).expect("compressed fixture should decode"), write ); assert_eq!( - ChannelWrite::decode(&plain).expect("plain fixture should decode"), + ChannelInscription::decode(&plain).expect("plain fixture should decode"), write ); let mut trailing_plain = plain; trailing_plain.push(0); - assert!(ChannelWrite::decode(&trailing_plain).is_err()); + assert!(ChannelInscription::decode(&trailing_plain).is_err()); } #[test] @@ -551,7 +461,7 @@ mod tests { Statement::new("SELECT 1".to_owned(), Vec::new()).expect("statement should be valid"), ]) .expect("transaction should be valid"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([3; 32]), transaction, captured_function_calls: CapturedFunctionCalls::empty(), @@ -568,7 +478,7 @@ mod tests { let transaction = Transaction::new(vec![ Statement::new( "SELECT ?1".to_owned(), - vec![Value::Blob(vec![0; MAX_INSCRIPTION_SIZE as usize + 1])], + vec![Value::Blob(vec![0; MAX_PAYLOAD_BYTES + 1])], ) .expect("statement should be valid"), ]) @@ -577,19 +487,18 @@ mod tests { TxId::generate(), &transaction, CapturedFunctionCalls::empty(), - MAX_INSCRIPTION_SIZE, ) .expect("compressed transaction should fit"); - assert!(encoded.payload.len() <= MAX_INSCRIPTION_SIZE as usize); - let decoded = ChannelWrite::decode(&encoded.payload).expect("payload should decode"); + assert!(encoded.payload.len() <= MAX_PAYLOAD_BYTES); + let decoded = ChannelInscription::decode(&encoded.payload).expect("payload should decode"); assert_eq!(decoded.transaction, transaction); } #[test] fn published_payload_must_still_fit_one_inscription() { - let mut data = vec![0; MAX_INSCRIPTION_SIZE as usize + 1]; + let mut data = vec![0; MAX_PAYLOAD_BYTES + 1]; StdRng::seed_from_u64(7).fill_bytes(&mut data); let transaction = Transaction::new(vec![ @@ -597,14 +506,14 @@ mod tests { .expect("statement should be valid"), ]) .expect("transaction should be valid"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([3; 32]), transaction, captured_function_calls: CapturedFunctionCalls::empty(), }; assert!(matches!( - write.encode(MAX_INSCRIPTION_SIZE), + write.encode(), Err(crate::Error::InscriptionTooLarge) )); } @@ -619,14 +528,14 @@ mod tests { .expect("statement should be valid"), ]) .expect("transaction should be valid"); - let write = ChannelWrite { + let write = ChannelInscription { tx_id: TxId::from([3; 32]), transaction, captured_function_calls: CapturedFunctionCalls::empty(), }; assert!(matches!( - write.encode(MAX_INSCRIPTION_SIZE), + write.encode(), Err(crate::Error::InvalidTransaction(_)) )); } diff --git a/logos_sql/src/publication.rs b/logos_sql/src/publication.rs deleted file mode 100644 index 9ec14c6..0000000 --- a/logos_sql/src/publication.rs +++ /dev/null @@ -1,215 +0,0 @@ -//! Packs the oldest queued writes into one bounded channel inscription. - -use std::num::{NonZeroU16, NonZeroUsize}; - -use lb_binary_codec::canonical::BinaryEncode as _; - -use crate::{ - db::PendingPublish, - error::Error, - protocol::{ChannelBatch, ChannelWrite, MAX_BODY_BYTES}, -}; - -/// The exact queue prefix owned by one SDK publication and checkpoint. -pub struct Publication { - pub writes: Vec, - pub payload: Vec, -} - -impl Publication { - pub fn prepare( - mut pending: Vec, - max_transactions: NonZeroU16, - max_inscription_size: u32, - ) -> Result, Error> { - if pending.is_empty() { - return Ok(None); - } - - let mut writes = Vec::new(); - let mut body_len = size_of::(); - - for queued in pending - .iter() - .take(NonZeroUsize::from(max_transactions).get()) - { - let write = ChannelWrite::decode(&queued.payload) - .map_err(|_| Error::InvalidLocalState("queued write cannot be decoded"))?; - - if write.tx_id != queued.tx_id { - return Err(Error::InvalidLocalState( - "queued write identity does not match", - )); - } - - body_len += write.encoded_length(); - - if body_len > MAX_BODY_BYTES { - break; - } - - writes.push(write); - } - - // Compression size cannot be predicted from the individual writes. - // Halving bounds the number of attempts; remaining writes stay queued. - loop { - let batch = ChannelBatch::new(writes.clone())?; - - match batch.encode(max_inscription_size) { - Ok(payload) => { - pending.truncate(writes.len()); - - return Ok(Some(Self { - writes: pending, - payload, - })); - } - Err(Error::InscriptionTooLarge) if writes.len() > 1 => { - writes.truncate(writes.len() / 2); - } - Err(error) => return Err(error), - } - } - } - - /// Matches a saved SDK publication even if newer writes remain queued. - pub fn matches_pending( - payload: &[u8], - pending: &[PendingPublish], - ) -> Result, Error> { - let Ok(batch) = ChannelBatch::decode(payload) else { - return Ok(None); - }; - let writes = batch.into_writes(); - - if writes.len() > pending.len() { - return Ok(None); - } - - for (write, queued) in writes.iter().zip(pending) { - let original = ChannelWrite::decode(&queued.payload) - .map_err(|_| Error::InvalidLocalState("queued write cannot be decoded"))?; - - if write.tx_id != queued.tx_id || write.content_digest() != original.content_digest() { - return Ok(None); - } - } - - Ok(Some(writes.len())) - } -} - -#[cfg(test)] -mod tests { - use rand::{RngCore as _, SeedableRng as _, rngs::StdRng}; - use rusqlite::types::Value; - - use super::Publication; - use crate::{ - PublicationConfig, - db::{PendingPublish, tests::MAX_INSCRIPTION_SIZE}, - protocol::{ - CapturedFunctionCalls, ChannelBatch, EncodedWrite, Statement, Transaction, TxId, - }, - }; - - fn queued_write(value: Value) -> PendingPublish { - let transaction = Transaction::new(vec![ - Statement::new("SELECT ?1".to_owned(), vec![value]).unwrap(), - ]) - .unwrap(); - let write = EncodedWrite::new( - TxId::generate(), - &transaction, - CapturedFunctionCalls::empty(), - MAX_INSCRIPTION_SIZE, - ) - .unwrap(); - - PendingPublish { - tx_id: write.tx_id, - payload: write.payload, - } - } - - #[test] - fn queued_writes_share_one_inscription_in_order() { - let pending = vec![ - queued_write(Value::Integer(1)), - queued_write(Value::Integer(2)), - ]; - let ids: Vec<_> = pending.iter().map(|write| write.tx_id).collect(); - let publication = Publication::prepare( - pending, - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - let decoded = ChannelBatch::decode(&publication.payload) - .unwrap() - .into_writes(); - - assert_eq!( - decoded.iter().map(|write| write.tx_id).collect::>(), - ids - ); - assert_eq!( - Publication::matches_pending(&publication.payload, &publication.writes).unwrap(), - Some(2) - ); - } - - #[test] - fn a_batch_takes_only_its_configured_prefix_of_the_queue() { - let pending: Vec<_> = (0..1500) - .map(|value| queued_write(Value::Integer(value))) - .collect(); - let expected: Vec<_> = pending.iter().take(1024).map(|write| write.tx_id).collect(); - let publication = - Publication::prepare(pending, 1024.try_into().unwrap(), MAX_INSCRIPTION_SIZE) - .unwrap() - .unwrap(); - let decoded = ChannelBatch::decode(&publication.payload) - .unwrap() - .into_writes(); - - assert_eq!( - decoded.iter().map(|write| write.tx_id).collect::>(), - expected - ); - assert_eq!(publication.writes.len(), 1024); - } - - #[test] - fn an_oversized_batch_leaves_later_writes_for_the_next_publication() { - let mut random = StdRng::seed_from_u64(4); - let mut pending = Vec::new(); - - for _ in 0..2 { - let mut blob = vec![0; 1_100_000]; - random.fill_bytes(&mut blob); - pending.push(queued_write(Value::Blob(blob))); - } - - let first = pending[0].tx_id; - let publication = Publication::prepare( - pending, - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - - assert_eq!(publication.writes.len(), 1); - assert_eq!(publication.writes[0].tx_id, first); - assert_eq!( - ChannelBatch::decode(&publication.payload) - .unwrap() - .into_writes() - .len(), - 1 - ); - } -} diff --git a/logos_sql/src/runtime.rs b/logos_sql/src/runtime.rs index 3bc189a..2780cb8 100644 --- a/logos_sql/src/runtime.rs +++ b/logos_sql/src/runtime.rs @@ -1,6 +1,6 @@ //! Single-owner runtime for SQL writes and channel events. -use std::{num::NonZeroU16, thread, time::Duration}; +use std::time::Duration; use lb_zone_sdk::{ adapter::NodeHttpClient, @@ -8,25 +8,20 @@ use lb_zone_sdk::{ sequencer::{Event, SequencerCheckpoint, ZoneSequencer, channel_inscriptions}, }; use tokio::{ - runtime::Builder, sync::{mpsc, oneshot}, task::JoinHandle, - time::{Instant, MissedTickBehavior, interval, sleep_until}, }; use crate::{ applier, - db::Databases, + db::{Databases, PendingPublish}, error::Error, protocol::{Transaction, TxId}, - publication::Publication, status::{Displacement, WriteStatus}, }; const COMMAND_CHANNEL_CAPACITY: usize = 16; const PUBLISH_RETRY_INTERVAL: Duration = Duration::from_secs(5); -// A short window lets sequential execute calls share publication overhead. -const BATCH_DELAY: Duration = Duration::from_millis(10); const TARGET: &str = lb_log_targets::logos_sql::RUNTIME; /// Requests processed by the task that owns the sequencer and database writer. @@ -61,43 +56,36 @@ pub struct RuntimeHandle { task: JoinHandle>, } -/// Starts the dedicated thread that owns the sequencer and database writer. +/// Starts the task that owns the sequencer and writable database connections. pub fn spawn( sequencer: ZoneSequencer, db: Databases, channel_id: ChannelId, restored_checkpoint: Option, read_only: bool, - max_batch_transactions: NonZeroU16, -) -> Result { - if read_only { - check_read_only_state(&db, restored_checkpoint.as_ref(), channel_id)?; - } - +) -> RuntimeHandle { let (command_tx, command_rx) = mpsc::channel(COMMAND_CHANNEL_CAPACITY); let (ready_tx, ready_rx) = oneshot::channel(); let runtime = Runtime { + sequencer, db, channel_id, read_only, - max_batch_transactions, command_rx, sequencer_ready: false, - max_inscription_size: None, ready_checkpoint_pending: false, ready_tx: Some(ready_tx), event_pending_retry: None, publish_state: PublishState::Idle, - next_publish_at: Instant::now() + BATCH_DELAY, }; - let task = runtime.spawn(sequencer, restored_checkpoint)?; + let task = tokio::spawn(runtime.run(restored_checkpoint)); - Ok(RuntimeHandle { + RuntimeHandle { command_tx, ready_rx, task, - }) + } } impl RuntimeHandle { @@ -203,7 +191,7 @@ impl RuntimeHandle { enum PublishState { Idle, CheckpointPending { - pending: Publication, + pending: PendingPublish, this_msg: lb_zone_sdk::node_types::MsgId, checkpoint: Box, }, @@ -211,20 +199,16 @@ enum PublishState { /// State owned exclusively by the participant's background task. struct Runtime { + sequencer: ZoneSequencer, db: Databases, channel_id: ChannelId, read_only: bool, - max_batch_transactions: NonZeroU16, command_rx: mpsc::Receiver, sequencer_ready: bool, - /// The most bytes an inscription may take in the era of the node's - /// current slot, as last fetched: what writes and publications must fit. - max_inscription_size: Option, ready_checkpoint_pending: bool, ready_tx: Option>, event_pending_retry: Option, publish_state: PublishState, - next_publish_at: Instant, } /// A channel event retained with the error from its latest application attempt. @@ -234,132 +218,47 @@ struct PendingEvent { } impl Runtime { - fn spawn( - self, - sequencer: ZoneSequencer, - restored_checkpoint: Option, - ) -> Result>, Error> { - // SQL and database rebuilds are synchronous. Keep the existing owner - // loop on its own thread so they cannot block the application's async - // workers. Its executor still drives the sequencer's network requests. - let executor = Builder::new_current_thread().enable_all().build()?; - let (finished_tx, finished_rx) = oneshot::channel(); - let task = executor.spawn(async move { - let result = self.run(sequencer, restored_checkpoint).await; - let _ = finished_tx.send(()); - result - }); - - thread::Builder::new() - .name("logos-sql".to_owned()) - .spawn(move || { - // Cancellation or panic drops the sender too, allowing the - // executor and thread to stop even without graceful shutdown. - executor.block_on(async { drop(finished_rx.await) }); - })?; - - Ok(task) - } - - async fn run( - mut self, - mut sequencer: ZoneSequencer, - restored_checkpoint: Option, - ) -> Result<(), Error> { + async fn run(mut self, restored_checkpoint: Option) -> Result<(), Error> { self.recover_published_write(restored_checkpoint.as_ref())?; - let mut retry = interval(PUBLISH_RETRY_INTERVAL); - retry.set_missed_tick_behavior(MissedTickBehavior::Delay); + let mut retry = tokio::time::interval(PUBLISH_RETRY_INTERVAL); + retry.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); loop { - let event = { - // Keep the same SDK event future across commands. Restarting it - // for each write can starve channel processing under sustained load. - // Leave this scope before publishing to release the sequencer borrow. - let event = sequencer.next_event(); - tokio::pin!(event); - - loop { - let retry_event = self.event_pending_retry.is_some(); - let save_publication = - matches!(self.publish_state, PublishState::CheckpointPending { .. }); - - // Finish the previous event before taking another. Also save - // the publication checkpoint before polling the SDK, since - // polling sends queued publications to the node. - let ready_for_events = !retry_event && !save_publication; - - tokio::select! { - event = &mut event, if ready_for_events => { - break Some(event); - } - command = self.command_rx.recv() => { - let Some(command) = command else { - return self.shutdown_result(); - }; - - if !self.handle_command(command) { - return self.shutdown_result(); - } - - if !ready_for_events { - break None; - } - } - _ = retry.tick(), if retry_event => { - break None; - } - () = sleep_until(self.next_publish_at), - if save_publication && !retry_event && self.can_publish() => break None, - } - } - }; - - // Release the event future before accessing SDK history for a rebuild. - if let Some(event) = event { - self.handle_event(event, &sequencer); - } else { - self.retry_pending_event(&sequencer)?; - } - - // Writes wait for the inscription limit of the node's era, asked - // for as soon as the sequencer is ready. - if self.sequencer_ready && self.max_inscription_size.is_none() { - match sequencer.max_inscription_size().await { - Ok(max_inscription_size) => { - self.max_inscription_size = Some(max_inscription_size); - } - Err(error) => { - tracing::warn!(target: TARGET, %error, "writes wait for the node's inscription limit"); + let retry_needed = self.has_pending_work()?; + + // `ZoneSDK::publish` only queues the node post; `next_event` drives + // it. Do not poll events until the returned checkpoint commits. A + // crash while the write remains pending therefore means it never + // reached the node, which the applier's recovery plan relies on. + tokio::select! { + command = self.command_rx.recv() => { + let Some(command) = command else { + return Ok(()); + }; + + if !self.handle_command(command).await { + return self.shutdown_result(); } - } - } - - // Publish against fully applied channel history, after the batch - // collection delay or failure backoff has elapsed. - let event_applied = self.event_pending_retry.is_none(); - let publish_delay_elapsed = Instant::now() >= self.next_publish_at; - - if self.can_publish() && event_applied && publish_delay_elapsed { - let save_publication = - matches!(self.publish_state, PublishState::CheckpointPending { .. }); - - // An accepted publication must be saved even if the queue is empty. - if save_publication || self.db.has_pending_writes()? { - self.publish_queued_writes(&mut sequencer).await; + }, + event = self.sequencer.next_event(), if self.event_pending_retry.is_none() && !matches!(self.publish_state, PublishState::CheckpointPending { .. }) => { + self.handle_event(event).await; + }, + _ = retry.tick(), if retry_needed => { + self.retry_pending_work().await?; } } } } - fn handle_command(&mut self, command: Command) -> bool { + async fn handle_command(&mut self, command: Command) -> bool { match command { Command::Execute { tx_id, transaction, response_tx, } => { - let result = self.execute(tx_id, &transaction); + let result = self.execute(tx_id, transaction).await; drop(response_tx.send(result)); true @@ -368,7 +267,7 @@ impl Runtime { displacement, response_tx, } => { - let result = self.retry_displacement(&displacement); + let result = self.retry_displacement(&displacement).await; drop(response_tx.send(result)); true @@ -411,8 +310,8 @@ impl Runtime { } } - fn execute(&mut self, tx_id: TxId, transaction: &Transaction) -> Result { - let max_inscription_size = self.ensure_ready_to_write()?; + async fn execute(&mut self, tx_id: TxId, transaction: Transaction) -> Result { + self.ensure_ready_to_write()?; // No await between checking and committing: a channel event cannot // displace local state between these two operations. @@ -420,22 +319,15 @@ impl Runtime { return Err(Error::UnhandledDisplacements); } - let queue_was_empty = !self.db.has_pending_writes()?; - self.db - .commit_local_write(tx_id, transaction, max_inscription_size)?; - - if queue_was_empty { - self.next_publish_at = Instant::now() + BATCH_DELAY; - } - - tracing::trace!(target: TARGET, ?tx_id, "local write queued for publication"); + self.db.commit_local_write(tx_id, &transaction)?; + self.publish_committed_write(tx_id).await; Ok(tx_id) } /// An explicit retry may write while other displacements await handling. - fn retry_displacement(&mut self, displacement: &Displacement) -> Result { - let max_inscription_size = self.ensure_ready_to_write()?; + async fn retry_displacement(&mut self, displacement: &Displacement) -> Result { + self.ensure_ready_to_write()?; // Check the exact occurrence before committing, without yielding to // channel events that could restore or displace the write again. @@ -444,22 +336,19 @@ impl Runtime { } let tx_id = TxId::generate(); - let queue_was_empty = !self.db.has_pending_writes()?; self.db - .commit_local_write(tx_id, &displacement.transaction, max_inscription_size)?; - - if queue_was_empty { - self.next_publish_at = Instant::now() + BATCH_DELAY; - } + .commit_local_write(tx_id, &displacement.transaction)?; // This control.db update is separate from the LIVE.db commit. // A failure here does not roll back the retry's SQL changes. self.db.mark_displacement_handled(displacement)?; + self.publish_committed_write(tx_id).await; + Ok(tx_id) } - fn ensure_ready_to_write(&self) -> Result { + const fn ensure_ready_to_write(&self) -> Result<(), Error> { if self.read_only { return Err(Error::ReadOnly); } @@ -468,70 +357,47 @@ impl Runtime { return Err(Error::RuntimeHalted); } - if matches!(self.publish_state, PublishState::CheckpointPending { .. }) { - return Err(Error::PublishPending); - } - if !self.sequencer_ready || self.ready_checkpoint_pending { return Err(Error::SequencerNotReady); } - // The SDK is still loading existing unfinalized history. - // Block new writes until that history has been used to rebuild LIVE.db. - if self.db.rebuild_required()? { - return Err(Error::SequencerNotReady); - } - - self.max_inscription_size.ok_or(Error::SequencerNotReady) + Ok(()) } /// Publication failures leave the committed write pending for retry. - async fn publish_queued_writes(&mut self, sequencer: &mut ZoneSequencer) { - let result = self - .advance_publish(sequencer) - .await - .and_then(|()| self.db.pending_write_count()); - - let delay = match result { - Ok(queued) if queued >= usize::from(self.max_batch_transactions.get()) => { - Duration::ZERO - } - Ok(_) => BATCH_DELAY, - Err(error) => { - tracing::warn!( - target: TARGET, - %error, - "committed writes remain queued for publication" - ); - - PUBLISH_RETRY_INTERVAL - } - }; - - // A full batch needs no collection delay. Smaller batches still get - // time to fill; the run loop drives the SDK before either is published. - self.next_publish_at = Instant::now() + delay; - } + async fn publish_committed_write(&mut self, tx_id: TxId) { + tracing::trace!( + target: TARGET, + ?tx_id, + "local write committed" + ); - fn apply_event( - &mut self, - event: &Event, - sequencer: &ZoneSequencer, - ) -> Result<(), Error> { - if let Some(rebuild) = applier::on_event(&mut self.db, event, self.channel_id)? { - let history = sequencer.channel_history(); - rebuild.finish(&mut self.db, history)?; + if let Err(error) = self.advance_publish().await { + tracing::warn!( + target: TARGET, + %error, + ?tx_id, + "write committed; publication remains pending" + ); } - - Ok(()) } - fn handle_event(&mut self, event: Event, sequencer: &ZoneSequencer) { - let result = self.apply_event(&event, sequencer); + async fn handle_event(&mut self, event: Event) { + let result = applier::on_event(&mut self.db, &event, self.channel_id); match result { Ok(()) => { self.record_applied_event(&event); + + if self.can_publish() + && let Err(error) = self.advance_publish().await + { + tracing::warn!( + target: TARGET, + %error, + "write publication remains pending" + ); + } } Err(error) => { tracing::error!(target: TARGET, %error, "applier halted"); @@ -540,25 +406,24 @@ impl Runtime { } } - fn retry_pending_event( - &mut self, - sequencer: &ZoneSequencer, - ) -> Result<(), Error> { + async fn retry_pending_work(&mut self) -> Result<(), Error> { if let Some(pending) = self.event_pending_retry.take() { - return self.retry_event(pending, sequencer); + return self.retry_event(pending).await; + } + + if self.can_publish() + && let Err(error) = self.advance_publish().await + { + tracing::warn!(target: TARGET, %error, "pending publication retry failed"); } Ok(()) } - fn retry_event( - &mut self, - pending: PendingEvent, - sequencer: &ZoneSequencer, - ) -> Result<(), Error> { + async fn retry_event(&mut self, pending: PendingEvent) -> Result<(), Error> { let event = pending.event; - match self.apply_event(&event, sequencer) { + match applier::on_event(&mut self.db, &event, self.channel_id) { Ok(()) => {} Err(error) => { if !is_retryable_apply_error(&error) { @@ -572,6 +437,12 @@ impl Runtime { } self.record_applied_event(&event); + if self.can_publish() + && let Err(error) = self.advance_publish().await + { + tracing::warn!(target: TARGET, %error, "pending publication retry failed"); + } + Ok(()) } @@ -603,40 +474,26 @@ impl Runtime { !self.read_only && self.sequencer_ready && !self.ready_checkpoint_pending } - async fn advance_publish( - &mut self, - sequencer: &mut ZoneSequencer, - ) -> Result<(), Error> { - // Do not publish a pending write that the unfinished rebuild removes. - if self.db.rebuild_required()? { - return Ok(()); - } + async fn advance_publish(&mut self) -> Result<(), Error> { self.persist_publish_checkpoint()?; - // A publication, and the writes after it, are held to the era the node - // is in now. - let max_inscription_size = sequencer.max_inscription_size().await?; - self.max_inscription_size = Some(max_inscription_size); - - let Some(pending) = Publication::prepare( - self.db.pending_writes()?, - self.max_batch_transactions, - max_inscription_size, - )? - else { + let Some(pending) = self.db.pending_publish()? else { return Ok(()); }; - let inscription = Inscription::try_new(pending.payload.clone(), max_inscription_size) + let inscription: Inscription = pending + .payload + .clone() + .try_into() .map_err(|_| Error::InscriptionTooLarge)?; - let (published, checkpoint) = sequencer.handle().publish(inscription).await?; + let (published, checkpoint) = self.sequencer.handle().publish(inscription).await?; let this_msg = published.tx.inscription().this_msg; tracing::trace!( target: TARGET, - writes = pending.writes.len(), - "batch accepted by ZoneSDK" + tx_id = ?pending.tx_id, + "write accepted by ZoneSDK" ); self.publish_state = PublishState::CheckpointPending { @@ -658,13 +515,12 @@ impl Runtime { return Ok(()); }; - self.db - .complete_batch(checkpoint, *this_msg, &pending.writes)?; + self.db.complete_publish(checkpoint, *this_msg, pending)?; tracing::trace!( target: TARGET, - writes = pending.writes.len(), - "batch publication recorded" + tx_id = ?pending.tx_id, + "write publication recorded" ); self.publish_state = PublishState::Idle; @@ -676,35 +532,45 @@ impl Runtime { &mut self, checkpoint: Option<&SequencerCheckpoint>, ) -> Result<(), Error> { - let pending = self.db.pending_writes()?; + let Some(pending) = self.db.pending_publish()? else { + return Ok(()); + }; let Some(checkpoint) = checkpoint else { return Ok(()); }; - for inscription in checkpoint + let submitted = checkpoint .pending_txs .iter() .flat_map(|(_, transaction)| channel_inscriptions(transaction, self.channel_id)) - { - let Some(count) = Publication::matches_pending(inscription.payload.as_ref(), &pending)? - else { - continue; - }; + .find(|inscription| inscription.payload.as_inner() == &pending.payload); + + if let Some(inscription) = submitted { self.db - .complete_batch(checkpoint, inscription.this_msg, &pending[..count])?; + .complete_publish(checkpoint, inscription.this_msg, &pending)?; tracing::debug!( target: TARGET, - writes = count, - "restored ZoneSDK checkpoint matched queued batch" + tx_id = ?pending.tx_id, + "restored ZoneSDK checkpoint matched pending write" ); - break; } Ok(()) } + /// Whether the retry tick has anything to do. An applier retry and a + /// pending checkpoint are local work. A pending publish needs the node, so + /// it only counts while the sequencer is connected: ticking for it while + /// disconnected would fail fast and, worse, cancel the reconnect that + /// `next_event` has in progress. + fn has_pending_work(&self) -> Result { + Ok(self.event_pending_retry.is_some() + || matches!(self.publish_state, PublishState::CheckpointPending { .. }) + || (self.sequencer.is_connected() && self.db.pending_publish()?.is_some())) + } + fn shutdown_result(&mut self) -> Result<(), Error> { match self.event_pending_retry.take() { Some(pending) => Err(pending.error), @@ -713,51 +579,17 @@ impl Runtime { } } -fn check_read_only_state( - db: &Databases, - checkpoint: Option<&SequencerCheckpoint>, - channel_id: ChannelId, -) -> Result<(), Error> { - if db.has_pending_writes()? { - return Err(Error::InvalidLocalState( - "read-only startup cannot resume pending publications", - )); - } - - // The SDK also retains transactions observed from other writers. Only - // our own unfinished publications require reopening as a writer. - if let Some(checkpoint) = checkpoint { - for (_, tx) in &checkpoint.pending_txs { - for inscription in channel_inscriptions(tx, channel_id) { - if db.has_local_publication(inscription.this_msg)? { - return Err(Error::InvalidLocalState( - "read-only startup cannot resume pending publications", - )); - } - } - } - } - - Ok(()) -} - const fn is_retryable_apply_error(error: &Error) -> bool { !matches!(error, Error::InvalidLocalState(_)) } #[cfg(test)] mod tests { - use std::time::{Duration, Instant}; - - use lb_core::{ - era::ForkDigest, - mantle::{Op, SignedOps, ops::channel::inscribe::InscriptionOp, transactions::Ops}, - }; - use lb_key_management_system_service::keys::{Ed25519Key, UnverifiedEd25519PublicKey}; + use lb_key_management_system_service::keys::Ed25519Key; use lb_zone_sdk::{ CommonHttpClient, adapter::NodeHttpClient, - node_types::{ChannelId, HeaderId, Inscription, MsgId, Slot, TxHash}, + node_types::{ChannelId, HeaderId, MsgId, Slot, TxHash}, sequencer::{ ChannelUpdate, ChannelUpdateTx, Event, FundingConfig, InscriptionInfo, SequencerCheckpoint, ZoneSequencer, @@ -765,375 +597,10 @@ mod tests { }; use rusqlite::Connection; use tempfile::TempDir; - use tokio::{ - io::AsyncReadExt as _, - net::TcpListener, - sync::{mpsc, oneshot}, - time::{sleep, timeout}, - }; - - use super::{ - COMMAND_CHANNEL_CAPACITY, Command, PendingEvent, PublishState, Runtime, RuntimeHandle, - channel_inscriptions, check_read_only_state, - }; - use crate::{ - PublicationConfig, - db::{ - Databases, - tests::{MAX_INSCRIPTION_SIZE, open_databases}, - }, - error::Error, - protocol::{CapturedFunctionCalls, ChannelWrite, TxId}, - publication::Publication, - sql::TransactionBuilder, - status::WriteStatus, - }; - - #[test] - fn read_only_resume_distinguishes_observed_and_local_publications() { - let directory = TempDir::new().unwrap(); - let channel_id = [1; 32].into(); - let (tx_id, transaction) = TransactionBuilder::new("CREATE TABLE items(value INTEGER)") - .finish() - .unwrap(); - let payload = ChannelWrite { - tx_id, - transaction: transaction.clone(), - captured_function_calls: CapturedFunctionCalls::new(Vec::new()).unwrap(), - } - .encode(MAX_INSCRIPTION_SIZE) - .unwrap(); - let tx = SignedOps::from_ops_with_sample_proofs(Ops::new( - ForkDigest::new([0x11; 32]), - [Op::ChannelInscribe(InscriptionOp { - channel_id, - inscription: Inscription::new_unchecked(payload), - parent: MsgId::root(), - signer: UnverifiedEd25519PublicKey::from_bytes(&[0; 32]).unwrap(), - })], - )); - let inscription = channel_inscriptions(&tx, channel_id).remove(0); - let checkpoint = SequencerCheckpoint { - last_msg_id: inscription.this_msg, - pending_txs: vec![(inscription.tx_hash, tx)], - lib: HeaderId::from([2; 32]), - lib_slot: Slot::from(1), - channel_notes: Vec::new(), - finalized_config: MsgId::root(), - funding: Vec::new(), - }; - - let db = open_databases(directory.path()).unwrap(); - db.persist_checkpoint(&checkpoint).unwrap(); - drop(db); - - // Observing another writer's publication must not prevent a restart. - let mut db = open_databases(directory.path()).unwrap(); - let restored = db.load_checkpoint().unwrap().unwrap(); - assert_eq!(restored.pending_txs.len(), 1); - check_read_only_state(&db, Some(&restored), channel_id).unwrap(); - - // Once the SDK accepts our own write, it leaves the SQL queue but - // still needs the writer until its publication finishes. - db.commit_local_write(tx_id, &transaction, MAX_INSCRIPTION_SIZE) - .unwrap(); - let pending = db.pending_publish().unwrap().unwrap(); - db.complete_publish(&checkpoint, inscription.this_msg, &pending) - .unwrap(); - drop(db); - - let db = open_databases(directory.path()).unwrap(); - assert!(!db.has_pending_writes().unwrap()); - assert!(matches!( - check_read_only_state(&db, db.load_checkpoint().unwrap().as_ref(), channel_id), - Err(Error::InvalidLocalState(_)) - )); - } - - #[tokio::test] - async fn commands_do_not_cancel_an_unfinished_event() { - let (_dir, mut runtime, _) = runtime(); - let (command_tx, command_rx) = mpsc::channel(COMMAND_CHANNEL_CAPACITY); - runtime.command_rx = command_rx; - - let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); - let sequencer = sequencer_at(&format!("http://{}", listener.local_addr().unwrap())); - - let commands = async { - // Hold the SDK's first node request open while commands arrive. - // Restarting next_event would abandon it and issue another request. - let (mut connection, _) = listener.accept().await.unwrap(); - let mut request = Vec::new(); - - while !request.ends_with(b"\r\n\r\n") { - request.push(connection.read_u8().await.unwrap()); - } - - for _ in 0..4 { - let (response_tx, response_rx) = oneshot::channel(); - command_tx - .send(Command::UnhandledDisplacements { response_tx }) - .await - .unwrap(); - assert_eq!(response_rx.await.unwrap().unwrap(), Vec::new()); - } - - let restarted = timeout(Duration::from_millis(250), listener.accept()) - .await - .is_ok(); - command_tx.send(Command::Shutdown).await.unwrap(); - - restarted - }; - - let (result, restarted) = timeout(Duration::from_secs(5), async { - tokio::join!(runtime.run(sequencer, None), commands) - }) - .await - .expect("commands and the event should both complete"); - - result.unwrap(); - assert!( - !restarted, - "commands must not restart the SDK's node request" - ); - } - - #[tokio::test] - async fn shutdown_does_not_wait_for_an_event() { - let (_dir, mut runtime, _) = runtime(); - let (command_tx, command_rx) = mpsc::channel(COMMAND_CHANNEL_CAPACITY); - runtime.command_rx = command_rx; - command_tx.send(Command::Shutdown).await.unwrap(); - - let result = timeout(Duration::from_secs(5), runtime.run(sequencer(), None)) - .await - .expect("shutdown should interrupt the wait"); - - result.unwrap(); - } - - #[tokio::test] - async fn writes_queue_locally_and_survive_a_publication_failure() { - let (dir, mut runtime, _) = runtime(); - runtime.sequencer_ready = true; - let mut ids = Vec::new(); - - for sql in [ - "CREATE TABLE items(value INTEGER)", - "INSERT INTO items VALUES (1)", - "INSERT INTO items VALUES (2)", - ] { - let (tx_id, transaction) = TransactionBuilder::new(sql).finish().unwrap(); - runtime.execute(tx_id, &transaction).unwrap(); - ids.push(tx_id); - } - - for tx_id in &ids { - assert_eq!( - runtime.db.write_status(*tx_id).unwrap(), - Some(WriteStatus::Live) - ); - } - - // This test sequencer has no node connection and cannot accept a publish. - runtime.publish_queued_writes(&mut sequencer()).await; - assert_eq!(runtime.db.pending_writes().unwrap().len(), 3); - drop(runtime); - - let db = open_databases(dir.path()).unwrap(); - let publication = Publication::prepare( - db.pending_writes().unwrap(), - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - assert_eq!( - publication - .writes - .iter() - .map(|write| write.tx_id) - .collect::>(), - ids - ); - let connection = Databases::open_reader(db.live_path()).unwrap(); - let count: i64 = connection - .query_row("SELECT count(*) FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(count, 2); - } - - #[tokio::test] - async fn adopting_our_batch_keeps_later_queued_writes() { - let (_dir, mut runtime, _) = runtime(); - runtime.sequencer_ready = true; - - for sql in [ - "CREATE TABLE items(value INTEGER)", - "INSERT INTO items VALUES (1)", - ] { - let (tx_id, transaction) = TransactionBuilder::new(sql).finish().unwrap(); - runtime.execute(tx_id, &transaction).unwrap(); - } - - let publication = Publication::prepare( - runtime.db.pending_writes().unwrap(), - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - let mut event = blocks_processed(); - let Event::BlocksProcessed { - checkpoint, - channel_update, - .. - } = &mut event - else { - unreachable!() - }; - let this_msg = MsgId::from([4; 32]); - runtime - .db - .complete_batch(checkpoint, this_msg, &publication.writes) - .unwrap(); - *channel_update = ChannelUpdate::Extension { - adopted: vec![ChannelUpdateTx::Inscription(InscriptionInfo { - tx_hash: TxHash::from([4; 32]), - parent_msg: MsgId::root(), - this_msg, - payload: Inscription::new_unchecked(publication.payload), - signer: None, - })], - }; - - let (tx_id, transaction) = TransactionBuilder::new("INSERT INTO items VALUES (2)") - .finish() - .unwrap(); - runtime.execute(tx_id, &transaction).unwrap(); - runtime.handle_event(event, &sequencer()); - - assert!(runtime.event_pending_retry.is_none()); - assert_eq!(runtime.db.unhandled_displacements().unwrap(), Vec::new()); - assert_eq!(runtime.db.pending_writes().unwrap()[0].tx_id, tx_id); - let connection = Databases::open_reader(runtime.db.live_path()).unwrap(); - let count: i64 = connection - .query_row("SELECT count(*) FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(count, 2); - } - - #[tokio::test(flavor = "current_thread")] - async fn waiting_for_sqlite_does_not_block_the_application() { - let (_dir, mut runtime, ready_rx) = runtime(); - runtime.sequencer_ready = true; - - let connection = Connection::open(runtime.db.live_path()).unwrap(); - connection - .execute_batch("CREATE TABLE items(value INTEGER); BEGIN IMMEDIATE") - .unwrap(); - - let (command_tx, command_rx) = mpsc::channel(COMMAND_CHANNEL_CAPACITY); - runtime.command_rx = command_rx; - let (response_tx, response_rx) = oneshot::channel(); - let (tx_id, transaction) = TransactionBuilder::new("INSERT INTO items VALUES (1)") - .query("INSERT INTO missing VALUES (1)") - .finish() - .unwrap(); - command_tx - .try_send(Command::Execute { - tx_id, - transaction, - response_tx, - }) - .unwrap(); - - let task = runtime.spawn(sequencer(), None).unwrap(); - let handle = RuntimeHandle { - command_tx, - ready_rx, - task, - }; - - // SQLite waits for our lock. This single-threaded application must - // still wake up and release it, rather than wait for SQLite's timeout. - let started = Instant::now(); - sleep(Duration::from_millis(100)).await; - connection.execute_batch("ROLLBACK").unwrap(); - assert!(started.elapsed() < Duration::from_secs(2)); - - // The second statement deliberately fails: no publication is needed - // to test the worker, and the first statement must be rolled back. - let result = timeout(Duration::from_secs(5), response_rx) - .await - .unwrap() - .unwrap(); - assert!(matches!(result, Err(Error::Database(_)))); - let rows: i64 = connection - .query_row("SELECT count(*) FROM items", [], |row| row.get(0)) - .unwrap(); - assert_eq!(rows, 0); - - timeout(Duration::from_secs(5), handle.shutdown()) - .await - .unwrap() - .unwrap(); - } - - #[tokio::test] - async fn worker_startup_failure_reaches_the_caller() { - let (_dir, runtime, _) = runtime(); - Connection::open(runtime.db.live_path()) - .unwrap() - .execute_batch("DROP TABLE __logos_sql_pending_publish") - .unwrap(); + use tokio::sync::{mpsc, oneshot}; - let mut handle = super::spawn( - sequencer(), - runtime.db, - runtime.channel_id, - None, - false, - PublicationConfig::default().max_transactions, - ) - .unwrap(); - let result = timeout(Duration::from_secs(5), handle.wait_until_ready()) - .await - .unwrap(); - assert!(result.is_err()); - } - - #[tokio::test] - async fn worker_can_be_cancelled_while_waiting_for_the_node() { - let (_dir, runtime, _) = runtime(); - let handle = super::spawn( - sequencer(), - runtime.db, - runtime.channel_id, - None, - false, - PublicationConfig::default().max_transactions, - ) - .unwrap(); - - // A command response confirms the worker started and can serve the - // application while the node is unavailable. - assert!( - handle - .write_status(TxId::generate()) - .await - .unwrap() - .is_none() - ); - handle.abort(); - - let error = timeout(Duration::from_secs(5), handle.task) - .await - .unwrap() - .unwrap_err(); - assert!(error.is_cancelled()); - } + use super::{COMMAND_CHANNEL_CAPACITY, Command, PendingEvent, PublishState, Runtime}; + use crate::{db::Databases, error::Error, sql::TransactionBuilder, status::WriteStatus}; #[tokio::test] async fn read_only_accepts_channel_writes_only() { @@ -1143,7 +610,7 @@ mod tests { reader.read_only = true; reader.sequencer_ready = true; - reader.handle_event(adopt_event(inscription.clone()), &sequencer()); + reader.handle_event(adopt_event(inscription.clone())).await; let connection = Databases::open_reader(reader.db.live_path()).unwrap(); let count: i64 = connection @@ -1158,7 +625,7 @@ mod tests { .unwrap(); assert!(matches!( - reader.execute(tx_id, &transaction), + reader.execute(tx_id, transaction).await, Err(Error::ReadOnly) )); assert!( @@ -1168,11 +635,11 @@ mod tests { .is_err() ); - writer.handle_event(orphan_event(inscription), &sequencer()); + writer.handle_event(orphan_event(inscription)).await; let displacement = writer.db.unhandled_displacements().unwrap().remove(0); assert!(matches!( - reader.retry_displacement(&displacement), + reader.retry_displacement(&displacement).await, Err(Error::ReadOnly) )); assert!(reader.db.pending_publish().unwrap().is_none()); @@ -1183,16 +650,18 @@ mod tests { let (_dir, mut runtime, _) = runtime(); let first = published_local_write(&mut runtime, 2); let second = published_local_write(&mut runtime, 3); - runtime.handle_event(orphan_event(first), &sequencer()); - runtime.handle_event(orphan_event(second), &sequencer()); + runtime.handle_event(orphan_event(first)).await; + runtime.handle_event(orphan_event(second)).await; let displacements = runtime.db.unhandled_displacements().unwrap(); let displacement = &displacements[0]; let (response_tx, response_rx) = oneshot::channel(); - runtime.handle_command(Command::RetryDisplacement { - displacement: displacement.clone(), - response_tx, - }); + runtime + .handle_command(Command::RetryDisplacement { + displacement: displacement.clone(), + response_tx, + }) + .await; assert!(matches!( response_rx.await.unwrap(), Err(Error::SequencerNotReady) @@ -1201,10 +670,12 @@ mod tests { runtime.sequencer_ready = true; let (response_tx, response_rx) = oneshot::channel(); - runtime.handle_command(Command::RetryDisplacement { - displacement: displacement.clone(), - response_tx, - }); + runtime + .handle_command(Command::RetryDisplacement { + displacement: displacement.clone(), + response_tx, + }) + .await; let retry_id = response_rx.await.unwrap().unwrap(); assert_ne!(retry_id, displacement.tx_id); @@ -1221,59 +692,16 @@ mod tests { .finish() .unwrap(); assert!(matches!( - runtime.execute(tx_id, &transaction), + runtime.execute(tx_id, transaction).await, Err(Error::UnhandledDisplacements) )); } - #[tokio::test] - async fn unfinished_rebuild_blocks_writes_and_publication() { - let (_dir, mut runtime, _) = runtime(); - runtime.sequencer_ready = true; - let (tx_id, transaction) = prepare_transaction("CREATE TABLE pending(value TEXT)") - .finish() - .unwrap(); - runtime - .db - .commit_local_write(tx_id, &transaction, MAX_INSCRIPTION_SIZE) - .unwrap(); - runtime.db.require_rebuild().unwrap(); - - runtime - .advance_publish(&mut sequencer()) - .await - .expect("publication waits without contacting the node"); - assert_eq!(runtime.db.pending_publish().unwrap().unwrap().tx_id, tx_id); - - let (next_id, next) = prepare_transaction("CREATE TABLE next(value TEXT)") - .finish() - .unwrap(); - assert!(matches!( - runtime.execute(next_id, &next), - Err(Error::SequencerNotReady) - )); - } - - #[tokio::test] - async fn writes_wait_for_the_inscription_limit() { - let (_dir, mut runtime, _) = runtime(); - runtime.sequencer_ready = true; - runtime.max_inscription_size = None; - let (tx_id, transaction) = prepare_transaction("CREATE TABLE items(value INTEGER)") - .finish() - .unwrap(); - - assert!(matches!( - runtime.execute(tx_id, &transaction), - Err(Error::SequencerNotReady) - )); - } - #[tokio::test] async fn failed_retry_keeps_the_displacement_available_for_review() { let (_dir, mut runtime, _) = runtime(); let original = published_local_write(&mut runtime, 2); - runtime.handle_event(orphan_event(original.clone()), &sequencer()); + runtime.handle_event(orphan_event(original.clone())).await; let displacement = runtime.db.unhandled_displacements().unwrap().remove(0); // The table now exists, but the original write is still displaced. @@ -1284,10 +712,12 @@ mod tests { runtime.sequencer_ready = true; let (response_tx, response_rx) = oneshot::channel(); - runtime.handle_command(Command::RetryDisplacement { - displacement: displacement.clone(), - response_tx, - }); + runtime + .handle_command(Command::RetryDisplacement { + displacement: displacement.clone(), + response_tx, + }) + .await; assert!(matches!( response_rx.await.unwrap(), @@ -1305,11 +735,11 @@ mod tests { for restored in [false, true] { let (_dir, mut runtime, _) = runtime(); let original = published_local_write(&mut runtime, 2); - runtime.handle_event(orphan_event(original.clone()), &sequencer()); + runtime.handle_event(orphan_event(original.clone())).await; let displacement = runtime.db.unhandled_displacements().unwrap().remove(0); if restored { - runtime.handle_event(adopt_event(original), &sequencer()); + runtime.handle_event(adopt_event(original)).await; } else { runtime.db.mark_displacement_handled(&displacement).unwrap(); } @@ -1317,7 +747,7 @@ mod tests { runtime.sequencer_ready = true; assert!(matches!( - runtime.retry_displacement(&displacement), + runtime.retry_displacement(&displacement).await, Err(Error::StaleDisplacement) )); assert!(runtime.db.pending_publish().unwrap().is_none()); @@ -1328,16 +758,16 @@ mod tests { async fn an_old_displacement_cannot_retry_a_new_occurrence() { let (_dir, mut runtime, _) = runtime(); let original = published_local_write(&mut runtime, 2); - runtime.handle_event(orphan_event(original.clone()), &sequencer()); + runtime.handle_event(orphan_event(original.clone())).await; let old = runtime.db.unhandled_displacements().unwrap().remove(0); - runtime.handle_event(adopt_event(original.clone()), &sequencer()); - runtime.handle_event(orphan_event(original), &sequencer()); + runtime.handle_event(adopt_event(original.clone())).await; + runtime.handle_event(orphan_event(original)).await; let current = runtime.db.unhandled_displacements().unwrap(); runtime.sequencer_ready = true; assert!(matches!( - runtime.retry_displacement(&old), + runtime.retry_displacement(&old).await, Err(Error::StaleDisplacement) )); assert_eq!(runtime.db.unhandled_displacements().unwrap(), current); @@ -1350,41 +780,43 @@ mod tests { runtime.sequencer_ready = true; let first = published_local_write(&mut runtime, 2); let second = published_local_write(&mut runtime, 3); - runtime.handle_event(orphan_event(first), &sequencer()); + runtime.handle_event(orphan_event(first)).await; let first = runtime.db.unhandled_displacements().unwrap()[0].clone(); let (tx_id, transaction) = TransactionBuilder::new("CREATE TABLE resumed(value INTEGER)") .finish() .unwrap(); assert!(matches!( - runtime.execute(tx_id, &transaction), + runtime.execute(tx_id, transaction).await, Err(Error::UnhandledDisplacements) )); assert!(runtime.db.pending_publish().unwrap().is_none()); - runtime.handle_event(orphan_event(second), &sequencer()); + runtime.handle_event(orphan_event(second)).await; runtime.db.mark_displacement_handled(&first).unwrap(); let (tx_id, transaction) = TransactionBuilder::new("CREATE TABLE resumed(value INTEGER)") .finish() .unwrap(); assert!(matches!( - runtime.execute(tx_id, &transaction), + runtime.execute(tx_id, transaction).await, Err(Error::UnhandledDisplacements) )); let remaining = runtime.db.unhandled_displacements().unwrap(); assert_eq!(remaining.len(), 1); let (response_tx, response_rx) = oneshot::channel(); - runtime.handle_command(Command::HandleDisplacement { - displacement: remaining[0].clone(), - response_tx, - }); + runtime + .handle_command(Command::HandleDisplacement { + displacement: remaining[0].clone(), + response_tx, + }) + .await; response_rx.await.unwrap().unwrap(); let (tx_id, transaction) = TransactionBuilder::new("CREATE TABLE resumed(value INTEGER)") .finish() .unwrap(); - assert_eq!(runtime.execute(tx_id, &transaction).unwrap(), tx_id); + assert_eq!(runtime.execute(tx_id, transaction).await.unwrap(), tx_id); assert_eq!( runtime.db.write_status(first.tx_id).unwrap(), Some(WriteStatus::Displaced) @@ -1401,10 +833,7 @@ mod tests { .finish() .unwrap(); - runtime - .db - .commit_local_write(tx_id, &transaction, MAX_INSCRIPTION_SIZE) - .unwrap(); + runtime.db.commit_local_write(tx_id, &transaction).unwrap(); let pending = runtime.db.pending_publish().unwrap().unwrap(); let Event::BlocksProcessed { checkpoint, .. } = blocks_processed() else { @@ -1421,7 +850,7 @@ mod tests { tx_hash: TxHash::from([position; 32]), parent_msg: MsgId::root(), this_msg, - payload: Inscription::new_unchecked(pending.payload), + payload: pending.payload.try_into().unwrap(), signer: None, } } @@ -1448,7 +877,7 @@ mod tests { .expect("transaction should be valid"); runtime .db - .commit_local_write(tx_id, &transaction, MAX_INSCRIPTION_SIZE) + .commit_local_write(tx_id, &transaction) .expect("write should commit"); let pending = runtime .db @@ -1459,7 +888,11 @@ mod tests { tx_hash: TxHash::from([2; 32]), parent_msg: MsgId::root(), this_msg: MsgId::from([2; 32]), - payload: Inscription::new_unchecked(pending.payload.clone()), + payload: pending + .payload + .clone() + .try_into() + .expect("payload should fit"), signer: None, }; let event = orphan_event(inscription.clone()); @@ -1480,7 +913,7 @@ mod tests { ) .expect("failure should be installed"); - runtime.handle_event(event, &sequencer()); + runtime.handle_event(event).await; assert!(runtime.event_pending_retry.is_some()); assert_eq!( @@ -1492,65 +925,14 @@ mod tests { .execute_batch("DROP TRIGGER fail_checkpoint") .expect("failure should be removed"); runtime - .retry_pending_event(&sequencer()) + .retry_pending_work() + .await .expect("event should recover"); assert!(runtime.event_pending_retry.is_none()); assert!(runtime.db.has_unhandled_displacements().unwrap()); } - #[tokio::test] - async fn writes_resume_after_the_publication_checkpoint_is_saved() { - let (dir, mut runtime, _) = runtime(); - runtime.sequencer_ready = true; - let (tx_id, transaction) = TransactionBuilder::new("CREATE TABLE items(value INTEGER)") - .finish() - .unwrap(); - runtime.execute(tx_id, &transaction).unwrap(); - - let pending = Publication::prepare( - runtime.db.pending_writes().unwrap(), - PublicationConfig::default().max_transactions, - MAX_INSCRIPTION_SIZE, - ) - .unwrap() - .unwrap(); - let Event::BlocksProcessed { checkpoint, .. } = blocks_processed() else { - unreachable!() - }; - runtime.publish_state = PublishState::CheckpointPending { - pending, - this_msg: MsgId::from([2; 32]), - checkpoint: Box::new(checkpoint), - }; - - let control = Connection::open(dir.path().join("control.db")).unwrap(); - control - .execute_batch( - "CREATE TRIGGER fail_checkpoint BEFORE UPDATE OF checkpoint ON __logos_sql_state - BEGIN SELECT RAISE(FAIL, 'test checkpoint failure'); END;", - ) - .unwrap(); - - assert!(runtime.persist_publish_checkpoint().is_err()); - let (next_id, next_write) = TransactionBuilder::new("INSERT INTO items VALUES (1)") - .finish() - .unwrap(); - assert!(matches!( - runtime.execute(next_id, &next_write), - Err(Error::PublishPending) - )); - - control - .execute_batch("DROP TRIGGER fail_checkpoint") - .unwrap(); - runtime.persist_publish_checkpoint().unwrap(); - runtime.execute(next_id, &next_write).unwrap(); - - assert!(matches!(runtime.publish_state, PublishState::Idle)); - assert_eq!(runtime.db.pending_writes().unwrap()[0].tx_id, next_id); - } - #[tokio::test] async fn ready_waits_for_its_block_checkpoint_before_enabling_writes() { let (_dir, mut runtime, ready_rx) = runtime(); @@ -1593,51 +975,44 @@ mod tests { fn runtime() -> (TempDir, Runtime, oneshot::Receiver<()>) { let dir = TempDir::new().expect("temporary directory should be created"); - let db = open_databases(dir.path()).expect("databases should open"); + let db = Databases::open(dir.path()).expect("databases should open"); let channel_id = ChannelId::from([9; 32]); + let node = NodeHttpClient::new( + CommonHttpClient::new(None), + "http://127.0.0.1:1" + .parse() + .expect("test node URL should parse"), + ); + let sequencer = ZoneSequencer::init( + channel_id, + Ed25519Key::from_bytes(&[7; 32]), + node, + FundingConfig { + funding_pk: lb_groth16::Fr::from(1u64).into(), + change_pk: None, + max_tx_fee: u64::MAX.into(), + priority_fee_percent: FundingConfig::DEFAULT_PRIORITY_FEE_PERCENT, + }, + None, + ); let (_command_tx, command_rx) = mpsc::channel(COMMAND_CHANNEL_CAPACITY); let (ready_tx, ready_rx) = oneshot::channel(); let runtime = Runtime { + sequencer, db, channel_id, read_only: false, - max_batch_transactions: PublicationConfig::default().max_transactions, command_rx, sequencer_ready: false, - max_inscription_size: Some(MAX_INSCRIPTION_SIZE), ready_checkpoint_pending: false, ready_tx: Some(ready_tx), event_pending_retry: None, publish_state: PublishState::Idle, - next_publish_at: tokio::time::Instant::now() + super::BATCH_DELAY, }; (dir, runtime, ready_rx) } - fn sequencer() -> ZoneSequencer { - sequencer_at("http://127.0.0.1:1") - } - - fn sequencer_at(url: &str) -> ZoneSequencer { - let node = NodeHttpClient::new( - CommonHttpClient::new(None), - url.parse().expect("test node URL should parse"), - ); - ZoneSequencer::init( - ChannelId::from([9; 32]), - Ed25519Key::from_bytes(&[7; 32]), - node, - FundingConfig { - funding_pk: lb_groth16::Fr::from(1u64).into(), - change_pk: None, - max_tx_fee: u64::MAX.into(), - priority_fee_percent: FundingConfig::DEFAULT_PRIORITY_FEE_PERCENT, - }, - None, - ) - } - fn blocks_processed() -> Event { update_event(ChannelUpdate::Extension { adopted: Vec::new(), diff --git a/tests/README.md b/tests/README.md index 9d196b1..e0f2df9 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,25 +1,25 @@ # SDK and SQL integration tests -The client suites reuse `logos-blockchain-tests` and `blockchain-test-logos` -from the pinned node revision. Node deployment, funding, shared steps and -diagnostics remain owned by the node repository. +On this release branch the suites are the node's own zone and Logos SQL +Cucumber suites at the pinned node release: steps and features come from +`logos-blockchain-tests`. The `[patch]` section of the workspace manifest +builds that harness against the `ZoneSDK` and Logos SQL of this repository +instead of the node's own copies (Logos SQL here is the node's at that release), and the two packages here only hold the +runner. ## Node binary The suites start real node processes. The node harness is a Git dependency here, so without a supplied binary it would build the node inside Cargo's checkout of the node repository, or fail in CI. Always provide a binary built -from the pinned node revision with the `testing` feature: +from the pinned node release with the `testing` feature: ```sh -rev="$(sed -nE 's/^lb-core += \{.*rev = "([0-9a-f]{40})".*$/\1/p' Cargo.toml)" -cargo install --locked --git https://github.com/logos-blockchain/logos-blockchain.git --rev "$rev" --features testing --root target/node logos-blockchain-node +tag="$(sed -nE 's/^lb-core += \{.*tag = "([^"]+)".*$/\1/p' Cargo.toml)" +cargo install --locked --git https://github.com/logos-blockchain/logos-blockchain.git --tag "$tag" --features testing --root target/node logos-blockchain-node export LOGOS_BLOCKCHAIN_NODE_BIN="$PWD/target/node/bin/logos-blockchain-node" ``` -`LOGOS_BLOCKCHAIN_NODE_DOWNLOAD_URL` with `LOGOS_BLOCKCHAIN_NODE_DOWNLOAD_SHA256` -selects a node release tarball instead, once the pinned revision is a release. - ## Running the suites ```sh @@ -39,13 +39,9 @@ export PASSWORD_MANAGER_BIN="$PWD/target/debug/examples/password_manager" cargo test --locked -p logos-blockchain-sql-tests --features cucumber --test cucumber -- --tags '@password_manager' ``` -`PASSWORD_MANAGER_BIN` defaults to that debug path; set it for other build -directories or profiles. The settlement benchmark belongs to -`logos-blockchain-sql-tests`; run it with `--release` and -`--tags '@logos_sql_benchmark'`. +Set `PASSWORD_MANAGER_BIN` explicitly: the default points into the node checkout. -Compose and Kubernetes deployment modes need the node repository's images, -cfgsync and deployment setup. These suites default to local process deployment. +The runner only supports local process deployment. ## CI @@ -54,6 +50,6 @@ binary and runs both suites and the password-manager scenario on the self-hosted runners, in the merge queue and on demand, as the node repository's lane did before the move. -After updating the node revision, run both suites before marking the new -revision compatible. Compilation and unit tests alone do not establish node +After updating the node release, run both suites before marking the new +release compatible. Compilation and unit tests alone do not establish node interoperability. diff --git a/tests/logos-sql/Cargo.toml b/tests/logos-sql/Cargo.toml index d88e1c8..5a9358c 100644 --- a/tests/logos-sql/Cargo.toml +++ b/tests/logos-sql/Cargo.toml @@ -1,6 +1,6 @@ [package] categories = { workspace = true } -description = "Logos SQL integration tests and settlement benchmark" +description = "Logos SQL integration tests" edition = { workspace = true } keywords = { workspace = true } license = { workspace = true } @@ -12,25 +12,11 @@ version = { workspace = true } [lints] workspace = true -[dependencies] -cucumber = { features = ["macros", "output-junit"], workspace = true } -futures = { workspace = true } -hex = { workspace = true } -lb-common-http-client = { workspace = true } -lb-core = { workspace = true } -lb-groth16 = { workspace = true } -lb-testing-framework = { workspace = true } -lb-zone-sdk = { workspace = true } -logos-blockchain-tests = { workspace = true } -logos-blockchain-zone-tests = { workspace = true } -logos-sql = { workspace = true } -rand = { workspace = true } -rusqlite = { workspace = true } -serde = { features = ["derive"], workspace = true } -serde_json = { workspace = true } -thiserror = { workspace = true } -tokio = { workspace = true } -tracing = { workspace = true } +[dev-dependencies] +cucumber = { features = ["macros", "output-junit"], workspace = true } +lb-testing-framework = { workspace = true } +logos-blockchain-tests = { workspace = true } +tokio = { features = ["macros", "rt-multi-thread"], workspace = true } [features] cucumber = [] @@ -38,5 +24,5 @@ cucumber = [] [[test]] harness = false name = "cucumber" -path = "tests/cucumber.rs" +path = "../zone-sdk/tests/cucumber.rs" required-features = ["cucumber"] diff --git a/tests/logos-sql/features/logos_sql.feature b/tests/logos-sql/features/logos_sql.feature deleted file mode 100644 index 3a5485b..0000000 --- a/tests/logos-sql/features/logos_sql.feature +++ /dev/null @@ -1,363 +0,0 @@ -Feature: Logos SQL - Applications submit writes through Logos SQL and read the resulting channel - history from their local SQLite databases. - - Rule: Replicas converge on channel history - - # Invariants covered: - # - accepted SQL writes replicate to peer instances in the live view and - # subsequently appear identically in finalized state; - # - nondeterministic SQL expressions are evaluated once in accepted history, - # and replicas reproduce the resulting values rather than re-evaluating them; - # - foreign channel inscriptions and malformed Logos SQL payloads are ignored - # without blocking later valid SQL writes or replica convergence. - # - # These phases deliberately share one channel and SQL replica pair: each phase - # adds independent accepted history while retaining the assertions that - # distinguish the individual invariants. - @logos_sql_ci - Scenario: Replicas preserve accepted SQL results and ignore unrelated channel traffic - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start Logos SQL instances: - | alias | sequencer | - | SQL_A | SEQ_A | - | SQL_B | SEQ_B | - And Logos SQL instance "SQL_A" executes write "CREATE_MESSAGES": - """ - CREATE TABLE messages ( - id INTEGER PRIMARY KEY, - body TEXT NOT NULL - ) - """ - Then Logos SQL instance "SQL_B" has 0 rows in table "messages" in its live database in 180 seconds - When Logos SQL instance "SQL_A" executes write "ADD_MESSAGE": - """ - INSERT INTO messages (id, body) VALUES (1, 'hello') - """ - Then Logos SQL instance "SQL_A" has 1 rows in table "messages" in its live database in 30 seconds - And Logos SQL instance "SQL_B" has 1 rows in table "messages" in its live database in 180 seconds - And Logos SQL instance "SQL_A" has 1 rows in table "messages" in its finalized database in 180 seconds - And Logos SQL instance "SQL_B" has 1 rows in table "messages" in its finalized database in 180 seconds - When Logos SQL instance "SQL_A" executes write "CREATE_OBSERVATIONS": - """ - CREATE TABLE observations ( - id INTEGER PRIMARY KEY, - random_value INTEGER NOT NULL, - random_bytes BLOB NOT NULL, - created_at TEXT NOT NULL - ) - """ - Then Logos SQL instance "SQL_B" has 0 rows in table "observations" in its live database in 180 seconds - When Logos SQL instance "SQL_A" executes write "ADD_OBSERVATION": - """ - INSERT INTO observations (id, random_value, random_bytes, created_at) - VALUES (1, random(), randomblob(16), CURRENT_TIMESTAMP) - """ - Then Logos SQL instance "SQL_B" has 1 rows in table "observations" in its live database in 180 seconds - And Logos SQL instances "SQL_A" and "SQL_B" agree on this live query in 30 seconds: - """ - SELECT id, random_value, random_bytes, created_at - FROM observations - ORDER BY id - """ - When I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | FOREIGN_TRAFFIC | not-a-logos-sql-protocol-inscription | - | MALFORMED_LOGOS_SQL | LOGOS_SQLjunk | - Then all zone messages are safe in 180 seconds - When Logos SQL instance "SQL_A" executes write "ADD_AFTER_MALFORMED": - """ - INSERT INTO messages (id, body) VALUES (2, 'still running') - """ - Then Logos SQL instance "SQL_B" has 2 rows in table "messages" in its live database in 180 seconds - And Logos SQL instances "SQL_A" and "SQL_B" agree on this live query in 30 seconds: - """ - SELECT id, body - FROM messages - ORDER BY id - """ - - @logos_sql_ci - Scenario: Backfill a new database from channel history - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start Logos SQL instances: - | alias | sequencer | - | SQL_A | SEQ_A | - And Logos SQL instance "SQL_A" executes write "CREATE_MESSAGES": - """ - CREATE TABLE messages ( - id INTEGER PRIMARY KEY, - body TEXT NOT NULL - ) - """ - Then Logos SQL instance "SQL_A" has 0 rows in table "messages" in its finalized database in 180 seconds - When Logos SQL instance "SQL_A" executes write "ADD_MESSAGE": - """ - INSERT INTO messages (id, body) VALUES (1, 'from history') - """ - Then Logos SQL instance "SQL_A" has 1 rows in table "messages" in its finalized database in 180 seconds - When I start Logos SQL instances: - | alias | sequencer | - | SQL_B | SEQ_B | - Then Logos SQL instance "SQL_B" has 1 rows in table "messages" in its finalized database in 30 seconds - And Logos SQL instances "SQL_A" and "SQL_B" agree on this finalized query in 30 seconds: - """ - SELECT id, body - FROM messages - ORDER BY id - """ - - @logos_sql_ci - Scenario: Read-only replica catches up and follows new writes - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start Logos SQL instances: - | alias | sequencer | - | WRITER | SEQ_A | - And Logos SQL instance "WRITER" executes write "CREATE_MESSAGES": - """ - CREATE TABLE messages ( - id INTEGER PRIMARY KEY, - body TEXT NOT NULL - ) - """ - Then Logos SQL instance "WRITER" has 0 rows in table "messages" in its finalized database in 180 seconds - When Logos SQL instance "WRITER" executes write "ADD_MESSAGE": - """ - INSERT INTO messages (id, body) VALUES (1, 'from history') - """ - Then Logos SQL instance "WRITER" has 1 rows in table "messages" in its finalized database in 180 seconds - When I start read-only Logos SQL instances: - | alias | sequencer | - | READER | SEQ_A | - Then Logos SQL instance "READER" returns text "from history" from this finalized query in 30 seconds: - """ - SELECT body FROM messages WHERE id = 1 - """ - When Logos SQL instance "WRITER" executes write "UPDATE_MESSAGE": - """ - UPDATE messages SET body = 'after reader started' WHERE id = 1 - """ - Then Logos SQL instance "READER" returns text "after reader started" from this live query in 180 seconds: - """ - SELECT body FROM messages WHERE id = 1 - """ - And Logos SQL instance "READER" returns text "after reader started" from this finalized query in 180 seconds: - """ - SELECT body FROM messages WHERE id = 1 - """ - - @logos_sql_ci - Scenario: Concurrent writes converge on accepted channel history - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I stop zone sequencer "SEQ_A" - And I start Logos SQL instances: - | alias | sequencer | - | SQL_A | SEQ_A | - | SQL_B | SEQ_B | - And Logos SQL instance "SQL_A" executes write "CREATE_CLAIMS": - """ - CREATE TABLE claims ( - name TEXT PRIMARY KEY, - owner TEXT NOT NULL - ) - """ - Then Logos SQL instance "SQL_B" has 0 rows in table "claims" in its live database in 180 seconds - When the following Logos SQL writes execute concurrently: - | instance | write | sql | - | SQL_A | CLAIM_A | INSERT INTO claims (name, owner) VALUES ('claim-a', 'A') | - | SQL_B | CLAIM_B | INSERT INTO claims (name, owner) VALUES ('claim-b', 'B') | - Then Logos SQL instance "SQL_A" has 1 rows in table "claims" in its live database in 180 seconds - And Logos SQL instance "SQL_B" has 1 rows in table "claims" in its live database in 180 seconds - And Logos SQL instances "SQL_A" and "SQL_B" agree on this live query in 180 seconds: - """ - SELECT name, owner FROM claims ORDER BY name - """ - And exactly one of Logos SQL writes "CLAIM_A" and "CLAIM_B" is displaced in 180 seconds - - @logos_sql_ci - Scenario: A disconnected database catches up from its checkpoint - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start Logos SQL instances: - | alias | sequencer | - | SQL_A | SEQ_A | - | SQL_B | SEQ_B | - And Logos SQL instance "SQL_A" executes write "CREATE_MESSAGES": - """ - CREATE TABLE messages ( - id INTEGER PRIMARY KEY, - body TEXT NOT NULL - ) - """ - Then Logos SQL instance "SQL_B" has 0 rows in table "messages" in its finalized database in 180 seconds - When I stop Logos SQL instance "SQL_B" - And Logos SQL instance "SQL_A" executes write "ADD_WHILE_DISCONNECTED": - """ - INSERT INTO messages (id, body) VALUES (1, 'while disconnected') - """ - Then Logos SQL instance "SQL_A" has 1 rows in table "messages" in its finalized database in 180 seconds - When I start Logos SQL instances: - | alias | sequencer | - | SQL_B | SEQ_B | - Then Logos SQL instance "SQL_B" has 1 rows in table "messages" in its finalized database in 30 seconds - And Logos SQL instances "SQL_A" and "SQL_B" agree on this finalized query in 30 seconds: - """ - SELECT id, body - FROM messages - ORDER BY id - """ - - Rule: Only valid canonical history changes replicated state - - @logos_sql_ci - Scenario: A reorganization removes the write from one abandoned branch - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - | 2 | 3 | 100000 | - And I have a cluster with capacity of 7 nodes - And no nodes are declared as blend providers - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_A | 1 | WALLET_A | | SEQ_A, SEQ_OBSERVER | - | NODE_B | 2 | WALLET_B | NODE_A | SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_OBSERVER | - | SEQ_B | - When node "NODE_A" is at height 1 in 120 seconds - And wallet "WALLET_A" sends 30 notes of 1000 LGO to node "NODE_A" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_A" in 180 seconds - And wallet "WALLET_B" sends 30 notes of 1000 LGO to node "NODE_B" funding wallet as "FUNDING_TOPUP_B" - And transaction "FUNDING_TOPUP_B" is included on node "NODE_A" in 180 seconds - And I start Logos SQL instances: - | alias | sequencer | - | SQL_A | SEQ_A | - | SQL_OBSERVER | SEQ_OBSERVER | - And Logos SQL instance "SQL_A" executes write "CREATE_MESSAGES": - """ - CREATE TABLE messages ( - id INTEGER PRIMARY KEY, - body TEXT NOT NULL - ) - """ - Then Logos SQL instance "SQL_OBSERVER" has 0 rows in table "messages" in its finalized database in 180 seconds - And all nodes have at least 10 blocks and converged to within 1 blocks in 180 seconds - When I stop node "NODE_B" - And I start peer node "NODE_A2" connected to node "NODE_A" - And I start peer node "NODE_A3" connected to node "NODE_A2" - And Logos SQL instance "SQL_A" executes write "BRANCH_A_WRITE": - """ - INSERT INTO messages (id, body) VALUES (1, 'branch A') - """ - Then Logos SQL instance "SQL_OBSERVER" has 1 rows in table "messages" in its live database in 180 seconds - # Branch A must stop while its write is still provisional so reconnecting - # the branches can displace it. - And Logos SQL instance "SQL_A" has 0 rows in table "messages" in its finalized database in 5 seconds - When I stop node "NODE_A3" - And I stop node "NODE_A2" - And I stop node "NODE_A" - When I restart node "NODE_B" - And I start peer node "NODE_B2" connected to node "NODE_B" - And I start peer node "NODE_B3" connected to node "NODE_B2" - And I start Logos SQL instances: - | alias | sequencer | - | SQL_B | SEQ_B | - And Logos SQL instance "SQL_B" executes write "BRANCH_B_WRITE": - """ - INSERT INTO messages (id, body) VALUES (1, 'branch B') - """ - And I start Logos SQL instances: - | alias | sequencer | - | SQL_B_OBSERVER | SEQ_B | - Then Logos SQL instance "SQL_B_OBSERVER" has 1 rows in table "messages" in its live database in 180 seconds - And Logos SQL instance "SQL_B" returns text "branch B" from this finalized query in 180 seconds: - """ - SELECT body - FROM messages - WHERE id = 1 - """ - When I restart node "NODE_A" - And I restart node "NODE_A2" - And I restart node "NODE_A3" - And I start peer node "NODE_JOIN" connected to node "NODE_A3" and node "NODE_B3" - Then all nodes have at least 10 blocks and converged to within 1 blocks in 300 seconds - When I start Logos SQL instances: - | alias | sequencer | - | SQL_CANONICAL | SEQ_B | - Then Logos SQL instance "SQL_CANONICAL" has 1 rows in table "messages" in its live database in 180 seconds - And Logos SQL instance "SQL_A" has 1 rows in table "messages" in its live database in 180 seconds - And Logos SQL instance "SQL_B" has 1 rows in table "messages" in its live database in 180 seconds - And Logos SQL instances "SQL_A" and "SQL_B" agree on this live query in 180 seconds: - """ - SELECT id, body - FROM messages - ORDER BY id - """ - And Logos SQL instance "SQL_A" returns text "branch B" from this live query in 30 seconds: - """ - SELECT body - FROM messages - WHERE id = 1 - """ - And Logos SQL write "BRANCH_A_WRITE" is displaced in 180 seconds - And exactly one of Logos SQL writes "BRANCH_A_WRITE" and "BRANCH_B_WRITE" is displaced in 180 seconds diff --git a/tests/logos-sql/features/logos_sql_benchmark.feature b/tests/logos-sql/features/logos_sql_benchmark.feature deleted file mode 100644 index 1397a67..0000000 --- a/tests/logos-sql/features/logos_sql_benchmark.feature +++ /dev/null @@ -1,42 +0,0 @@ -@logos_sql_benchmark @retry(0) -Feature: Logos SQL settlement benchmark - Measure a single writer without channel conflicts. Each SQL transaction inserts - one row. TPS counts new rows in the replica's finalized database during the - measurement window, excluding initial finality delay. After submission stops, - the replica must finalize every submitted row and match its original contents. - - Scenario: Measure finalized SQL throughput - # Keep fee inputs reserved through warm-up, measurement, and draining on - # this accelerated chain, rather than reusing them for later publications. - Given I have user config override "wallet.pending_note_expiry_blocks" as "1000" - And the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 1000000000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 250 notes of 10000000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - # Storage gas can double from its genesis price while funded writes wait. - # Budget for that increase; this benchmark measures throughput, not fee bidding. - # Capacity configuration through the public API, not library defaults. - And I start Logos SQL instances with a 100% fee reserve, batches of 6000 writes and a queue of 32768 writes: - | alias | sequencer | - | SQL_A | SEQ_A | - And I start read-only Logos SQL instances: - | alias | sequencer | - | SQL_B | SEQ_B | - And Logos SQL instance "SQL_A" executes write "CREATE_BENCHMARK": - """ - CREATE TABLE benchmark_writes ( - id INTEGER PRIMARY KEY, - payload BLOB NOT NULL - ) - """ - Then Logos SQL instance "SQL_B" has 0 rows in table "benchmark_writes" in its finalized database in 300 seconds - When I benchmark Logos SQL writer "SQL_A" and replica "SQL_B" on sequencer "SEQ_A" for 180 seconds with 256 byte rows diff --git a/tests/logos-sql/features/password_manager.feature b/tests/logos-sql/features/password_manager.feature deleted file mode 100644 index 1416f9e..0000000 --- a/tests/logos-sql/features/password_manager.feature +++ /dev/null @@ -1,33 +0,0 @@ -@password_manager -Feature: Password manager - The terminal app stores credentials using Logos SQL and reads them locally. - All passwords in these scenarios are fake. - - Scenario: Add, edit, and remove a credential through the password manager - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And the password manager using sequencer "SEQ_A" runs this session in 180 seconds: - | command | output | - | add email andrus@example.org fake-password | committed locally as | - | show email | password: fake-password | - | update email another-fake-password | committed locally as | - | notes email recovery codes stored elsewhere | committed locally as | - | show email | password: another-fake-password | - | show email | notes: recovery codes stored elsewhere | - | list | email (andrus@example.org) | - | displacements | no displacements awaiting review | - And the password manager using sequencer "SEQ_A" runs this session in 180 seconds: - | command | output | - | show email | password: another-fake-password | - | show email | notes: recovery codes stored elsewhere | - | list | email (andrus@example.org) | - | remove email | committed locally as | - | show email | credential not found | diff --git a/tests/logos-sql/src/benchmarks/logos_sql/mod.rs b/tests/logos-sql/src/benchmarks/logos_sql/mod.rs deleted file mode 100644 index 7341b93..0000000 --- a/tests/logos-sql/src/benchmarks/logos_sql/mod.rs +++ /dev/null @@ -1,369 +0,0 @@ -//! Settlement throughput of a single SQL writer and a read-only replica. -//! -//! Cucumber owns the chain setup and creates the `benchmark_writes` table. -//! This workload keeps inserting one row per transaction while the replica -//! samples finalized state. Warm-up and draining the last writes are outside -//! the throughput measurement. -//! -//! TPS is the increase in finalized rows divided by elapsed sampling time. -//! Each row represents one SQL transaction, not one inscription or block. - -use std::{path::Path, time::Duration}; - -use lb_core::header::HeaderId; -use lb_testing_framework::NodeHttpClient; -use lb_zone_sdk::node_types::ChannelId; -use logos_blockchain_tests::cucumber::error::StepError; -use logos_sql::{Error as SqlError, LogosSql, TransactionBuilder}; -use rand::{RngCore as _, SeedableRng as _, rngs::StdRng}; -use rusqlite::Connection; -use tokio::{ - sync::watch, - time::{Instant, sleep, timeout}, -}; - -mod progress; -mod report; - -use progress::SqlProgressMonitor; -use report::{ChainTraffic, SettlementReport, SettlementSample}; - -use crate::cucumber::error::{Error, StepResult}; - -const SAMPLE_INTERVAL: Duration = Duration::from_millis(500); -const QUEUE_RETRY_DELAY: Duration = Duration::from_millis(5); -// A failure deadline for warm-up and draining, not an assumed finality delay. -const SETTLEMENT_TIMEOUT: Duration = Duration::from_secs(300); -const WORKLOAD_SEED: u64 = 42; - -const COUNT_ROWS: &str = "SELECT COUNT(*) FROM benchmark_writes"; -const SELECT_ROWS: &str = "SELECT id, payload FROM benchmark_writes ORDER BY id"; - -/// One SQL transaction inserts one row. Seeded random blobs avoid inflating -/// compression results with repeated dummy data. -pub struct SqlWorkload { - pub payload_bytes: usize, - pub measure_for: Duration, -} - -/// Recreates the same row sequence for submission and verification. -struct WorkloadRows { - random: StdRng, - next_id: u64, - payload_bytes: usize, -} - -impl WorkloadRows { - fn new(payload_bytes: usize) -> Self { - Self { - random: StdRng::seed_from_u64(WORKLOAD_SEED), - next_id: 1, - payload_bytes, - } - } - - fn next_row(&mut self) -> Result { - let id = i64::try_from(self.next_id).map_err(|error| StepError::InvalidArgument { - message: error.to_string(), - })?; - let mut payload = vec![0; self.payload_bytes]; - self.random.fill_bytes(&mut payload); - self.next_id += 1; - - Ok(WorkloadRow { id, payload }) - } -} - -struct WorkloadRow { - id: i64, - payload: Vec, -} - -impl WorkloadRow { - fn transaction(&self) -> TransactionBuilder { - TransactionBuilder::new("INSERT INTO benchmark_writes (id, payload) VALUES (?1, ?2)") - .bind(self.id) - .bind(self.payload.clone()) - } -} - -enum Submission { - Committed, - Stopped, -} - -/// Runs against instances already started and funded by the test harness. -pub struct LogosSqlBenchmark<'a> { - pub writer: &'a LogosSql, - pub replica: &'a LogosSql, - pub node: &'a NodeHttpClient, - pub channel_id: ChannelId, - pub output_dir: &'a Path, - pub workload: SqlWorkload, -} - -impl LogosSqlBenchmark<'_> { - /// The schema must already be finalized on both instances. The table must - /// be empty and this benchmark must be the channel's only writer. - pub async fn run(&self) -> StepResult { - self.check_preconditions()?; - - let setup_lib = self.node.consensus_info().await?.cryptarchia_info.lib; - let monitor = SqlProgressMonitor::new( - self.node, - self.channel_id, - setup_lib, - self.replica, - self.output_dir, - )?; - - // Diagnostics run alongside the workload, including the final drain. - // They help explain stalls but do not supply the TPS measurement. - tokio::select! { - result = self.measure_and_verify(setup_lib) => result, - result = monitor.record_progress() => result, - } - } - - async fn measure_and_verify(&self, setup_lib: HeaderId) -> StepResult { - let report = self.submit_and_measure().await?; - report.save_samples(self.output_dir)?; - - self.drain_and_verify(report.submitted).await?; - - let traffic = - ChainTraffic::read(self.node, self.channel_id, setup_lib, report.submitted).await?; - - report.save(self.output_dir, traffic) - } - - /// Submit continuously through warm-up and measurement, then stop the - /// writer. The remaining writes are drained separately from this timing. - async fn submit_and_measure(&self) -> StepResult { - let finalized = self.replica.finalized_read_connection()?; - let (stop_tx, stop_rx) = watch::channel(false); - let observe = async move { - let samples = measure_settlement(finalized, self.workload.measure_for).await?; - stop_tx.send_replace(true); - - Ok::<_, Error>(samples) - }; - - let (submitted, samples) = timeout(SETTLEMENT_TIMEOUT + self.workload.measure_for, async { - tokio::try_join!(self.submit_writes(stop_rx), observe) - }) - .await - .map_err(|_| StepError::Timeout { - message: "SQL settlement benchmark stalled during warm-up or measurement".to_owned(), - })??; - - Ok(SettlementReport::from_samples( - self.workload.payload_bytes, - submitted, - samples, - )?) - } - - /// Drain and check every submitted row before accepting the measurements. - async fn drain_and_verify(&self, submitted: u64) -> StepResult { - let finalized = self.replica.finalized_read_connection()?; - let finalized = wait_for_finalized(finalized, submitted).await?; - - verify_rows(&finalized, submitted, self.workload.payload_bytes) - } - - fn check_preconditions(&self) -> StepResult { - if self.workload.measure_for.is_zero() || self.workload.payload_bytes == 0 { - return Err(StepError::InvalidArgument { - message: "measurement duration and payload size must be positive".to_owned(), - } - .into()); - } - - let finalized = self.replica.finalized_read_connection()?; - - if row_count(&finalized)? != 0 { - return Err(StepError::InvalidArgument { - message: "benchmark table must start empty".to_owned(), - } - .into()); - } - - Ok(()) - } - - /// Returns the number of rows committed by the writer before `stop`. - async fn submit_writes(&self, stop: watch::Receiver) -> StepResult { - let mut rows = WorkloadRows::new(self.workload.payload_bytes); - let mut submitted = 0u64; - - while !*stop.borrow() { - let row = rows.next_row()?; - - match self.submit_row(&row, &stop).await? { - Submission::Committed => submitted += 1, - Submission::Stopped => break, - } - } - - Ok(submitted) - } - - /// A full queue has not committed the transaction, so the same input is - /// retried until `stop`. Any other error invalidates the run rather than - /// disappearing from the reported throughput. - async fn submit_row( - &self, - row: &WorkloadRow, - stop: &watch::Receiver, - ) -> StepResult { - loop { - match self.writer.execute(row.transaction()).await { - Ok(_) => return Ok(Submission::Committed), - Err(SqlError::PublishPending) if *stop.borrow() => return Ok(Submission::Stopped), - Err(SqlError::PublishPending) => sleep(QUEUE_RETRY_DELAY).await, - Err(error) => return Err(error.into()), - } - } - } -} - -/// Samples the finalized row count every [`SAMPLE_INTERVAL`] for `duration`, -/// starting the window at the first observed finalized write rather than at -/// submission. That first count is the baseline, so the initial batch is not -/// counted toward measured throughput. -/// -/// The connection is owned because [`Connection`] is not `Sync`, and borrowing -/// it across an await would make the benchmark future non-`Send`. -async fn measure_settlement( - connection: Connection, - duration: Duration, -) -> StepResult> { - let started = Instant::now(); - - let first = loop { - let sample = sample_finalized(&connection, started)?; - - if sample.finalized > 0 { - break sample; - } - - sleep(SAMPLE_INTERVAL).await; - }; - - let deadline = Instant::now() + duration; - let mut samples = vec![first]; - - loop { - sleep(SAMPLE_INTERVAL).await; - samples.push(sample_finalized(&connection, started)?); - - if Instant::now() >= deadline { - return Ok(samples); - } - } -} - -fn sample_finalized(connection: &Connection, started: Instant) -> StepResult { - let finalized = row_count(connection)?; - - Ok(SettlementSample { - seconds: started.elapsed().as_secs_f64(), - finalized, - }) -} - -/// Returns the connection once the replica finalized `expected` rows, so the -/// caller can verify them. See [`measure_settlement`] for why it is owned. -async fn wait_for_finalized(connection: Connection, expected: u64) -> StepResult { - timeout(SETTLEMENT_TIMEOUT, async move { - while row_count(&connection)? != expected { - sleep(SAMPLE_INTERVAL).await; - } - - Ok(connection) - }) - .await - .map_err(|_| StepError::Timeout { - message: format!("replica did not finalize all {expected} submitted SQL transactions"), - })? -} - -fn row_count(connection: &Connection) -> StepResult { - let count = connection.query_row(COUNT_ROWS, [], |row| row.get(0))?; - - Ok(count) -} - -fn verify_rows(connection: &Connection, expected: u64, payload_bytes: usize) -> StepResult { - let mut statement = connection.prepare(SELECT_ROWS)?; - let mut rows = statement.query([])?; - - let mut expected_rows = WorkloadRows::new(payload_bytes); - - for _ in 0..expected { - let expected_row = expected_rows.next_row()?; - let id = expected_row.id; - - let row = rows.next()?.ok_or_else(|| StepError::StepFail { - message: format!("finalized row {id} is missing"), - })?; - - if row.get::<_, i64>(0)? != id || row.get::<_, Vec>(1)? != expected_row.payload { - return Err(StepError::StepFail { - message: format!("finalized row {id} differs from the submitted write"), - } - .into()); - } - } - - if rows.next()?.is_some() { - return Err(StepError::StepFail { - message: "replica contains unexpected rows".to_owned(), - } - .into()); - } - - Ok(()) -} - -#[cfg(test)] -mod tests { - use rand::{RngCore as _, SeedableRng as _, rngs::StdRng}; - use rusqlite::{Connection, params}; - - use super::{WORKLOAD_SEED, verify_rows}; - - #[test] - fn verification_checks_contents_not_just_row_count() { - let connection = Connection::open_in_memory().unwrap(); - connection - .execute_batch( - "CREATE TABLE benchmark_writes (id INTEGER PRIMARY KEY, payload BLOB NOT NULL)", - ) - .unwrap(); - let mut random = StdRng::seed_from_u64(WORKLOAD_SEED); - - for id in 1..=3 { - let mut payload = vec![0; 32]; - random.fill_bytes(&mut payload); - connection - .execute( - "INSERT INTO benchmark_writes VALUES (?1, ?2)", - params![id, payload], - ) - .unwrap(); - } - - verify_rows(&connection, 3, 32).unwrap(); - - connection - .execute( - "UPDATE benchmark_writes SET payload = zeroblob(32) WHERE id = 2", - [], - ) - .unwrap(); - - assert!(verify_rows(&connection, 3, 32).is_err()); - } -} diff --git a/tests/logos-sql/src/benchmarks/logos_sql/progress.rs b/tests/logos-sql/src/benchmarks/logos_sql/progress.rs deleted file mode 100644 index 0f49646..0000000 --- a/tests/logos-sql/src/benchmarks/logos_sql/progress.rs +++ /dev/null @@ -1,155 +0,0 @@ -//! Correlates chain inclusion, chain finality and SQL replay during a -//! benchmark. These diagnostics explain where progress stops; TPS is measured -//! separately from the replica's finalized row counts. - -use std::{ - collections::HashMap, - fs::{self, File}, - io::Write as _, - path::Path, - time::Duration, -}; - -use lb_core::header::HeaderId; -use lb_testing_framework::NodeHttpClient; -use lb_zone_sdk::{node_types::ChannelId, sequencer::channel_inscriptions}; -use logos_blockchain_tests::cucumber::error::StepError; -use logos_sql::LogosSql; -use rusqlite::Connection; -use serde::Serialize; -use tokio::time::{Instant, sleep, timeout}; - -use super::row_count; -use crate::cucumber::error::StepResult; - -const SAMPLE_INTERVAL: Duration = Duration::from_secs(2); -const SAMPLE_TIMEOUT: Duration = Duration::from_secs(30); - -/// Writes chain and database observations to JSONL, including on failed runs. -pub(super) struct SqlProgressMonitor<'a> { - client: &'a NodeHttpClient, - channel_id: ChannelId, - history: HashMap, - live: Connection, - finalized: Connection, - output: File, -} - -#[derive(Clone, Copy, Default, Serialize)] -struct HistoryCount { - blocks: u64, - inscriptions: u64, -} - -#[derive(Serialize)] -struct ProgressSample { - seconds: f64, - collection_seconds: f64, - tip_height: u64, - tip: HeaderId, - lib: HeaderId, - lib_slot: u64, - included_since_setup: HistoryCount, - finalized_since_setup: HistoryCount, - replica_live_rows: u64, - replica_finalized_rows: u64, -} - -impl<'a> SqlProgressMonitor<'a> { - pub(super) fn new( - client: &'a NodeHttpClient, - channel_id: ChannelId, - setup_lib: HeaderId, - replica: &LogosSql, - directory: &Path, - ) -> StepResult { - fs::create_dir_all(directory)?; - - Ok(Self { - client, - channel_id, - history: HashMap::from([(setup_lib, HistoryCount::default())]), - live: replica.read_connection()?, - finalized: replica.finalized_read_connection()?, - output: File::create(directory.join("logos-sql-progress.jsonl"))?, - }) - } - - /// Persist each sample immediately so failed runs still leave diagnostics. - /// The caller stops this loop when verification finishes. A collection - /// failure fails the benchmark rather than leaving an unexplained gap. - pub(super) async fn record_progress(mut self) -> StepResult { - let started = Instant::now(); - - loop { - let sample = timeout(SAMPLE_TIMEOUT, self.sample(started)) - .await - .map_err(|_| StepError::Timeout { - message: "benchmark progress sampling stalled".to_owned(), - })??; - - serde_json::to_writer(&mut self.output, &sample).map_err(|error| { - StepError::StepFail { - message: error.to_string(), - } - })?; - writeln!(self.output)?; - - sleep(SAMPLE_INTERVAL).await; - } - } - - async fn sample(&mut self, started: Instant) -> StepResult { - let collecting = Instant::now(); - let info = self.client.consensus_info().await?.cryptarchia_info; - let included = self.history_at(info.tip).await?; - let finalized = self.history_at(info.lib).await?; - - Ok(ProgressSample { - seconds: started.elapsed().as_secs_f64(), - collection_seconds: collecting.elapsed().as_secs_f64(), - tip_height: info.height, - tip: info.tip, - lib: info.lib, - lib_slot: info.lib_slot.into(), - included_since_setup: included, - finalized_since_setup: finalized, - replica_live_rows: row_count(&self.live)?, - replica_finalized_rows: row_count(&self.finalized)?, - }) - } - - /// Cache counts by block ID, so polling fetches only unfamiliar blocks. - /// A fork uses its own parent's count rather than adding orphaned traffic. - async fn history_at(&mut self, mut block_id: HeaderId) -> StepResult { - let mut missing = Vec::new(); - - while !self.history.contains_key(&block_id) { - let block = self - .client - .block(&block_id) - .await? - .ok_or_else(|| StepError::StepFail { - message: format!("benchmark progress block {block_id:?} is unavailable"), - })?; - let inscriptions = block - .transactions - .iter() - .map(|tx| channel_inscriptions(tx, self.channel_id).len() as u64) - .sum::(); - - missing.push((block_id, inscriptions)); - block_id = block.header.parent_block; - } - - let mut count = self.history[&block_id]; - - for (id, inscriptions) in missing.into_iter().rev() { - count.blocks += 1; - count.inscriptions += inscriptions; - self.history.insert(id, count); - } - - Ok(count) - } -} diff --git a/tests/logos-sql/src/benchmarks/logos_sql/report.rs b/tests/logos-sql/src/benchmarks/logos_sql/report.rs deleted file mode 100644 index ceec283..0000000 --- a/tests/logos-sql/src/benchmarks/logos_sql/report.rs +++ /dev/null @@ -1,192 +0,0 @@ -//! Throughput measurements and the saved benchmark report. - -use std::{fs, path::Path}; - -use lb_core::header::HeaderId; -use lb_testing_framework::NodeHttpClient; -use lb_zone_sdk::{node_types::ChannelId, sequencer::channel_inscriptions}; -use logos_blockchain_tests::cucumber::error::StepError; -use serde::Serialize; - -use crate::cucumber::error::StepResult; - -const SAMPLES_FILE: &str = "logos-sql-samples.json"; -const REPORT_FILE: &str = "logos-sql-benchmark.json"; - -/// Sample times are measured from workload startup, including the initial -/// wait for finalization. -#[derive(Serialize)] -pub(super) struct SettlementSample { - pub(super) seconds: f64, - pub(super) finalized: u64, -} - -/// Finalized SQL throughput for the sampling window, excluding warm-up and -/// drain. -#[derive(Serialize)] -pub(super) struct SettlementReport { - payload_bytes: usize, - pub(super) submitted: u64, - measured_seconds: f64, - measured_transactions: u64, - finalized_transactions_per_second: f64, - #[serde(skip)] - samples: Vec, -} - -#[derive(Serialize)] -struct BenchmarkReport<'a> { - workload: &'static str, - measurement: &'a SettlementReport, - whole_workload_traffic: ChainTraffic, -} - -/// Average batching efficiency and chain payload cost for the full workload. -#[derive(Serialize)] -pub(super) struct ChainTraffic { - sql_transactions_per_inscription: f64, - published_bytes_per_sql_transaction: f64, -} - -impl SettlementReport { - pub(super) fn from_samples( - payload_bytes: usize, - submitted: u64, - samples: Vec, - ) -> Result { - let (Some(first), Some(last)) = (samples.first(), samples.last()) else { - return Err(StepError::StepFail { - message: "no settlement samples were observed".to_owned(), - }); - }; - - let measured_seconds = last.seconds - first.seconds; - let measured_transactions = last.finalized - first.finalized; - - if measured_transactions == 0 { - return Err(StepError::StepFail { - message: "no SQL transactions finalized during the measurement window".to_owned(), - }); - } - - Ok(Self { - payload_bytes, - submitted, - measured_seconds, - measured_transactions, - finalized_transactions_per_second: measured_transactions as f64 / measured_seconds, - samples, - }) - } - - /// Retain observations even if draining or result verification fails. - pub(super) fn save_samples(&self, directory: &Path) -> StepResult { - let observations = serde_json::json!({ - "submitted": self.submitted, - "samples": self.samples, - }); - - write_json(directory, SAMPLES_FILE, &observations)?; - - Ok(()) - } - - /// Publish the report only after all submitted rows have been verified. - pub(super) fn save(&self, directory: &Path, traffic: ChainTraffic) -> StepResult { - let report = BenchmarkReport { - workload: "one blob insert per SQL transaction; one writer; one read-only replica", - measurement: self, - whole_workload_traffic: traffic, - }; - - let json = write_json(directory, REPORT_FILE, &report)?; - println!("Logos SQL benchmark:\n{json}"); - - Ok(()) - } -} - -impl ChainTraffic { - /// Counts finalized channel payloads across warm-up, measurement and drain. - /// The setup block excludes schema creation. Payload sizes exclude the - /// enclosing chain transaction and proofs, so they are not gas estimates. - pub(super) async fn read( - client: &NodeHttpClient, - channel_id: ChannelId, - setup_lib: HeaderId, - submitted: u64, - ) -> Result { - let mut current = client.consensus_info().await?.cryptarchia_info.lib; - let mut inscription_count = 0u64; - let mut payload_bytes = 0u64; - - while current != setup_lib { - let block = client - .block(¤t) - .await? - .ok_or_else(|| StepError::StepFail { - message: format!("finalized block {current:?} is unavailable"), - })?; - - let inscriptions = block - .transactions - .iter() - .flat_map(|transaction| channel_inscriptions(transaction, channel_id)); - - for inscription in inscriptions { - inscription_count += 1; - payload_bytes += inscription.payload.len() as u64; - } - - current = block.header.parent_block; - } - - if inscription_count == 0 { - return Err(StepError::StepFail { - message: "no workload inscriptions found".to_owned(), - }); - } - - Ok(Self { - sql_transactions_per_inscription: submitted as f64 / inscription_count as f64, - published_bytes_per_sql_transaction: payload_bytes as f64 / submitted as f64, - }) - } -} - -/// Writes `value` as pretty JSON into `directory` and returns the text. -fn write_json( - directory: &Path, - file_name: &str, - value: &impl Serialize, -) -> Result { - let json = serde_json::to_string_pretty(value).map_err(|error| StepError::StepFail { - message: error.to_string(), - })?; - - fs::create_dir_all(directory)?; - fs::write(directory.join(file_name), &json)?; - - Ok(json) -} - -#[cfg(test)] -mod tests { - use super::{SettlementReport, SettlementSample}; - - #[test] - fn throughput_counts_only_writes_finalized_during_measurement() { - let samples = [(2.0, 20), (5.0, 20), (8.0, 50), (12.0, 50)] - .into_iter() - .map(|(seconds, finalized)| SettlementSample { seconds, finalized }) - .collect(); - - let report = SettlementReport::from_samples(256, 80, samples) - .expect("measurement should produce a report"); - - assert_eq!(report.measured_transactions, 30); - assert!((report.finalized_transactions_per_second - 3.0).abs() < f64::EPSILON); - assert!((report.measured_seconds - 10.0).abs() < f64::EPSILON); - assert_eq!(report.submitted, 80); - } -} diff --git a/tests/logos-sql/src/benchmarks/mod.rs b/tests/logos-sql/src/benchmarks/mod.rs deleted file mode 100644 index 3db807e..0000000 --- a/tests/logos-sql/src/benchmarks/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod logos_sql; diff --git a/tests/logos-sql/src/cucumber/error.rs b/tests/logos-sql/src/cucumber/error.rs deleted file mode 100644 index 9494f76..0000000 --- a/tests/logos-sql/src/cucumber/error.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::io::Error as IoError; - -use lb_common_http_client::Error as HttpError; -use lb_testing_framework::configs::wallet::WalletConfigError; -use logos_blockchain_tests::cucumber::error::StepError; -use logos_sql::Error as SqlError; -use rusqlite::Error as SqliteError; -use thiserror::Error; - -#[derive(Debug, Error)] -pub enum Error { - #[error(transparent)] - Harness(#[from] StepError), - #[error(transparent)] - LogosSql(#[from] SqlError), - #[error(transparent)] - Sqlite(#[from] SqliteError), - #[error(transparent)] - Io(#[from] IoError), - #[error(transparent)] - Http(#[from] HttpError), - #[error(transparent)] - WalletConfig(#[from] WalletConfigError), -} - -pub type StepResult = Result; diff --git a/tests/logos-sql/src/cucumber/logos_sql.rs b/tests/logos-sql/src/cucumber/logos_sql.rs deleted file mode 100644 index 6f837f4..0000000 --- a/tests/logos-sql/src/cucumber/logos_sql.rs +++ /dev/null @@ -1,139 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - fmt, -}; - -use logos_blockchain_tests::cucumber::error::StepError; -use logos_sql::{LogosSql, TxId, WriteStatus}; - -use crate::cucumber::error::StepResult; - -/// Logos SQL runtimes and application writes owned by one Cucumber scenario. -#[derive(Default)] -pub struct LogosSqlState { - instances: HashMap, - writes: HashMap, -} - -impl LogosSqlState { - pub fn insert(&mut self, alias: String, instance: LogosSql) -> StepResult { - if self.instances.contains_key(&alias) { - return Err(StepError::LogicalError { - message: format!("Logos SQL instance '{alias}' is already running"), - } - .into()); - } - - self.instances.insert(alias, instance); - - Ok(()) - } - - pub fn instance(&self, alias: &str) -> Result<&LogosSql, StepError> { - self.instances - .get(alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Logos SQL instance '{alias}' is not running"), - }) - } - - pub async fn stop(&mut self, alias: &str) -> StepResult { - let instance = self - .instances - .remove(alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Logos SQL instance '{alias}' is not running"), - })?; - - instance - .shutdown() - .await - .map_err(|error| StepError::StepFail { - message: format!("failed to stop Logos SQL instance '{alias}': {error}"), - })?; - - Ok(()) - } - - pub fn remember_write(&mut self, alias: String, tx_id: TxId) -> StepResult { - if self.writes.contains_key(&alias) { - return Err(StepError::LogicalError { - message: format!("Logos SQL write '{alias}' is already recorded"), - } - .into()); - } - - self.writes.insert(alias, tx_id); - - Ok(()) - } - - pub fn write(&self, alias: &str) -> Result { - self.writes - .get(alias) - .copied() - .ok_or_else(|| StepError::LogicalError { - message: format!("Logos SQL write '{alias}' is not recorded"), - }) - } - - pub async fn displaced_writes(&self) -> StepResult> { - let mut displaced = HashSet::new(); - - for tx_id in self.writes.values().copied() { - for instance in self.instances.values() { - match instance.write_status(tx_id).await? { - Some(WriteStatus::Displaced) => { - displaced.insert(tx_id); - break; - } - Some(WriteStatus::Live | WriteStatus::Finalized) | None => {} - } - } - } - - Ok(displaced) - } - - pub async fn shutdown_all(&mut self) -> Result<(), StepError> { - let instances = std::mem::take(&mut self.instances); - let mut errors = Vec::new(); - - for (alias, instance) in instances { - if let Err(error) = instance.shutdown().await { - errors.push(format!( - "failed to stop Logos SQL instance '{alias}': {error}" - )); - } - } - - if errors.is_empty() { - Ok(()) - } else { - Err(StepError::StepFail { - message: errors.join("; "), - }) - } - } - - pub fn clear(&mut self) { - self.instances.clear(); - self.writes.clear(); - } -} - -impl fmt::Debug for LogosSqlState { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut instances = self.instances.keys().collect::>(); - instances.sort_unstable(); - - let mut writes = self.writes.keys().collect::>(); - writes.sort_unstable(); - - formatter - .debug_struct("LogosSqlState") - .field("instances", &instances) - .field("writes", &writes) - .finish() - } -} diff --git a/tests/logos-sql/src/cucumber/mod.rs b/tests/logos-sql/src/cucumber/mod.rs deleted file mode 100644 index 7fd0ea3..0000000 --- a/tests/logos-sql/src/cucumber/mod.rs +++ /dev/null @@ -1,61 +0,0 @@ -use std::{path::Path, process::ExitCode}; - -use cucumber::World as _; -use logos_blockchain_tests::cucumber::{deployment::LocalImplementation, runner}; -use logos_blockchain_zone_tests::cucumber::shared_steps::with_zone_steps; - -pub mod error; -pub mod logos_sql; -pub mod steps; -pub mod world; - -use world::SqlWorld; - -#[expect( - clippy::future_not_send, - reason = "Cucumber uses non-Send futures; the entry point awaits the runner directly" -)] -pub async fn run() -> ExitCode { - runner::run_suite::( - LocalImplementation::Logos, - Path::new(env!("CARGO_MANIFEST_DIR")).join("features"), - with_zone_steps(SqlWorld::collection()), - SqlWorld::full_debug_info_string, - ) - .await -} - -#[cfg(test)] -mod tests { - use std::fs; - - use cucumber::gherkin::{Feature, GherkinEnv}; - - use super::*; - - #[test] - fn all_sql_scenario_steps_are_registered() { - let steps = with_zone_steps(SqlWorld::collection()); - let features = Path::new(env!("CARGO_MANIFEST_DIR")).join("features"); - - for entry in fs::read_dir(features).expect("feature directory exists") { - let path = entry.expect("feature entry is readable").path(); - let feature = Feature::parse_path(&path, GherkinEnv::default()).expect("valid feature"); - let scenarios = feature - .scenarios - .iter() - .chain(feature.rules.iter().flat_map(|rule| &rule.scenarios)); - - for scenario in scenarios { - for step in &scenario.steps { - assert!( - steps.find(step).expect("step is unambiguous").is_some(), - "{}: {}: undefined step {step}", - path.display(), - scenario.name, - ); - } - } - } - } -} diff --git a/tests/logos-sql/src/cucumber/steps/logos_sql/actions.rs b/tests/logos-sql/src/cucumber/steps/logos_sql/actions.rs deleted file mode 100644 index 1284e8b..0000000 --- a/tests/logos-sql/src/cucumber/steps/logos_sql/actions.rs +++ /dev/null @@ -1,202 +0,0 @@ -//! Starting Logos SQL instances and submitting application writes. - -use futures::future::join_all; -use lb_core::mantle::gas::GasCost; -use lb_zone_sdk::sequencer::FundingConfig; -use logos_blockchain_tests::cucumber::error::StepError; -use logos_sql::{LogosSql, LogosSqlConfig, PublicationConfig, TransactionBuilder, WriterConfig}; -use tracing::info; - -use super::tables::{InstanceRow, WriteRow}; -use crate::{ - benchmarks::logos_sql::{LogosSqlBenchmark, SqlWorkload}, - cucumber::{ - error::{Error, StepResult}, - steps::TARGET, - world::SqlWorld, - }, -}; - -#[derive(Clone, Copy)] -pub(super) enum InstanceMode { - ReadOnly, - Writer { - priority_fee_percent: u64, - publication: PublicationConfig, - }, -} - -impl Default for InstanceMode { - fn default() -> Self { - Self::Writer { - priority_fee_percent: FundingConfig::DEFAULT_PRIORITY_FEE_PERCENT, - publication: PublicationConfig::default(), - } - } -} - -impl InstanceMode { - const fn is_read_only(self) -> bool { - matches!(self, Self::ReadOnly) - } -} - -pub(super) async fn start_instances( - world: &mut SqlWorld, - rows: Vec, - mode: InstanceMode, -) -> StepResult { - let test_context = world - .sdk - .node - .lifecycle - .test_context - .clone() - .ok_or_else(|| StepError::LogicalError { - message: "Cucumber test context is not initialized".to_owned(), - })?; - - for row in rows { - let writer = match mode { - InstanceMode::ReadOnly => None, - InstanceMode::Writer { - priority_fee_percent, - publication, - } => { - let node_name = world - .sdk - .zone - .sequencer_node_name(&row.sequencer)? - .to_owned(); - let funding_pk = world.sdk.node.funding_wallet(&node_name)?.public_key()?; - - Some(WriterConfig { - publication, - signing_key: world - .sdk - .zone - .sequencer_signing_key(&row.sequencer)? - .clone(), - funding: FundingConfig { - funding_pk, - change_pk: None, - max_tx_fee: GasCost::new(u64::MAX), - priority_fee_percent, - }, - }) - } - }; - - let config = LogosSqlConfig { - channel_id: world.sdk.zone.sequencer_channel_id(&row.sequencer)?, - node_url: world.sdk.zone_node_url_for_sequencer(&row.sequencer)?, - writer, - state_dir: world - .sdk - .node - .lifecycle - .scenario_base_dir - .join("logos_sql") - .join(&test_context) - .join(&row.alias), - }; - - info!( - target: TARGET, - instance = %row.alias, - sequencer = %row.sequencer, - read_only = mode.is_read_only(), - "Starting Logos SQL instance" - ); - - let instance = LogosSql::start(config).await?; - world.logos_sql.insert(row.alias, instance)?; - } - - Ok(()) -} - -pub(super) async fn stop_instance(world: &mut SqlWorld, alias: &str) -> StepResult { - info!(target: TARGET, instance = alias, "Stopping Logos SQL instance"); - - world.logos_sql.stop(alias).await -} - -pub(super) async fn execute_write( - world: &mut SqlWorld, - instance_alias: &str, - write_alias: String, - sql: String, -) -> StepResult { - let tx_id = world - .logos_sql - .instance(instance_alias)? - .execute(TransactionBuilder::new(sql)) - .await?; - - info!( - target: TARGET, - instance = instance_alias, - write = %write_alias, - %tx_id, - "Committed Logos SQL write locally" - ); - - world.logos_sql.remember_write(write_alias, tx_id) -} - -pub(super) async fn execute_writes_concurrently( - world: &mut SqlWorld, - rows: Vec, -) -> StepResult { - let executions = rows.into_iter().map(async |row| { - let result = world - .logos_sql - .instance(&row.instance)? - .execute(TransactionBuilder::new(row.sql)) - .await; - - Ok::<_, Error>((row.instance, row.write, result?)) - }); - - for result in join_all(executions).await { - let (instance_alias, write_alias, tx_id) = result?; - - info!( - target: TARGET, - instance = %instance_alias, - write = %write_alias, - %tx_id, - "Committed concurrent Logos SQL write locally" - ); - - world.logos_sql.remember_write(write_alias, tx_id)?; - } - - Ok(()) -} - -/// Connects the benchmark to the instances and output directory of this -/// scenario. -pub(super) async fn benchmark( - world: &SqlWorld, - writer_alias: &str, - replica_alias: &str, - sequencer_alias: &str, - workload: SqlWorkload, -) -> StepResult { - let client = world - .sdk - .zone_node_http_client_for_sequencer(sequencer_alias)?; - let channel_id = world.sdk.zone.sequencer_channel_id(sequencer_alias)?; - let benchmark = LogosSqlBenchmark { - writer: world.logos_sql.instance(writer_alias)?, - replica: world.logos_sql.instance(replica_alias)?, - node: &client, - channel_id, - output_dir: &world.sdk.node.lifecycle.scenario_base_dir, - workload, - }; - - benchmark.run().await -} diff --git a/tests/logos-sql/src/cucumber/steps/logos_sql/assertions.rs b/tests/logos-sql/src/cucumber/steps/logos_sql/assertions.rs deleted file mode 100644 index 9e34ec3..0000000 --- a/tests/logos-sql/src/cucumber/steps/logos_sql/assertions.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! Polling assertions over replicated database state and write outcomes. - -use std::time::Duration; - -use logos_blockchain_tests::cucumber::error::StepError; -use rusqlite::{Connection, types::Value}; -use tokio::time::{Instant, sleep}; - -use super::DatabaseKind; -use crate::cucumber::{error::StepResult, world::SqlWorld}; - -const POLL_INTERVAL: Duration = Duration::from_millis(500); - -pub(super) async fn wait_for_table_row_count( - world: &SqlWorld, - instance_alias: &str, - table: &str, - expected: usize, - database: DatabaseKind, - timeout_seconds: u64, -) -> StepResult { - if !table - .chars() - .all(|character| character.is_ascii_alphanumeric() || character == '_') - { - return Err(StepError::InvalidArgument { - message: format!("invalid SQLite table name '{table}'"), - } - .into()); - } - - let query = format!("SELECT COUNT(*) FROM \"{table}\""); - let deadline = Instant::now() + Duration::from_secs(timeout_seconds); - - loop { - let observation = match query_rows(world, instance_alias, &query, database) { - Ok(rows) => { - let actual = - rows.first() - .and_then(|row| row.first()) - .and_then(|value| match value { - Value::Integer(value) => usize::try_from(*value).ok(), - _ => None, - }); - - if actual == Some(expected) { - return Ok(()); - } - - format!("returned {actual:?} rows") - } - Err(error) => error.to_string(), - }; - - if Instant::now() >= deadline { - return Err(StepError::Timeout { - message: format!( - "Logos SQL instance '{instance_alias}' did not reach {expected} rows in table '{table}' in its {} database: {observation}", - database.name() - ), - }.into()); - } - - sleep(POLL_INTERVAL).await; - } -} - -pub(super) async fn wait_for_query_agreement( - world: &SqlWorld, - left_alias: &str, - right_alias: &str, - query: &str, - database: DatabaseKind, - timeout_seconds: u64, -) -> StepResult { - let deadline = Instant::now() + Duration::from_secs(timeout_seconds); - - loop { - let left = query_rows(world, left_alias, query, database); - let right = query_rows(world, right_alias, query, database); - - let observation = match (left, right) { - (Ok(left), Ok(right)) if left == right => return Ok(()), - (Ok(left), Ok(right)) => { - format!("left returned {left:?}; right returned {right:?}") - } - (Err(error), _) | (_, Err(error)) => error.to_string(), - }; - - if Instant::now() >= deadline { - return Err(StepError::Timeout { - message: format!( - "Logos SQL instances '{left_alias}' and '{right_alias}' did not agree on their {} query: {observation}", - database.name() - ), - }.into()); - } - - sleep(POLL_INTERVAL).await; - } -} - -pub(super) async fn wait_for_text_query_result( - world: &SqlWorld, - instance_alias: &str, - query: &str, - expected: &str, - database: DatabaseKind, - timeout_seconds: u64, -) -> StepResult { - let deadline = Instant::now() + Duration::from_secs(timeout_seconds); - let expected_rows = vec![vec![Value::Text(expected.to_owned())]]; - - loop { - let observation = match query_rows(world, instance_alias, query, database) { - Ok(actual) if actual == expected_rows => return Ok(()), - Ok(actual) => format!("returned {actual:?}"), - Err(error) => error.to_string(), - }; - - if Instant::now() >= deadline { - return Err(StepError::Timeout { - message: format!( - "Logos SQL instance '{instance_alias}' did not return text '{expected}' from its {} query: {observation}", - database.name() - ), - }.into()); - } - - sleep(POLL_INTERVAL).await; - } -} - -pub(super) async fn wait_for_one_displaced_write( - world: &SqlWorld, - left_alias: &str, - right_alias: &str, - timeout_seconds: u64, -) -> StepResult { - let left = world.logos_sql.write(left_alias)?; - let right = world.logos_sql.write(right_alias)?; - let deadline = Instant::now() + Duration::from_secs(timeout_seconds); - - loop { - let displaced = world.logos_sql.displaced_writes().await?; - if displaced.contains(&left) ^ displaced.contains(&right) { - return Ok(()); - } - - if Instant::now() >= deadline { - return Err(StepError::Timeout { - message: format!( - "expected exactly one of Logos SQL writes '{left_alias}' and '{right_alias}' to be displaced; observed {displaced:?}" - ), - }.into()); - } - - sleep(POLL_INTERVAL).await; - } -} - -pub(super) async fn wait_for_displaced_write( - world: &SqlWorld, - write_alias: &str, - timeout_seconds: u64, -) -> StepResult { - let tx_id = world.logos_sql.write(write_alias)?; - let deadline = Instant::now() + Duration::from_secs(timeout_seconds); - - loop { - if world.logos_sql.displaced_writes().await?.contains(&tx_id) { - return Ok(()); - } - - if Instant::now() >= deadline { - return Err(StepError::Timeout { - message: format!("Logos SQL write '{write_alias}' was not reported as displaced"), - } - .into()); - } - - sleep(POLL_INTERVAL).await; - } -} - -fn query_rows( - world: &SqlWorld, - instance_alias: &str, - query: &str, - database: DatabaseKind, -) -> StepResult>> { - let instance = world.logos_sql.instance(instance_alias)?; - let connection = match database { - DatabaseKind::Live => instance.read_connection()?, - DatabaseKind::Finalized => instance.finalized_read_connection()?, - }; - - Ok(collect_rows(&connection, query)?) -} - -fn collect_rows(connection: &Connection, query: &str) -> rusqlite::Result>> { - let mut statement = connection.prepare(query)?; - let column_count = statement.column_count(); - let rows = statement.query_map([], |row| { - (0..column_count) - .map(|column| row.get(column)) - .collect::>>() - })?; - - rows.collect() -} diff --git a/tests/logos-sql/src/cucumber/steps/logos_sql/mod.rs b/tests/logos-sql/src/cucumber/steps/logos_sql/mod.rs deleted file mode 100644 index 4c8583f..0000000 --- a/tests/logos-sql/src/cucumber/steps/logos_sql/mod.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! Logos SQL Cucumber bindings, split into step entrypoints and the behavior -//! they exercise. - -mod actions; -mod assertions; -mod password_manager; -mod steps; -mod tables; - -use std::str::FromStr; - -use cucumber::Parameter; - -#[derive(Clone, Copy, Debug, Parameter)] -#[param(name = "logos_sql_database", regex = "live|finalized")] -enum DatabaseKind { - Live, - Finalized, -} - -impl FromStr for DatabaseKind { - type Err = String; - - fn from_str(value: &str) -> Result { - match value { - "live" => Ok(Self::Live), - "finalized" => Ok(Self::Finalized), - value => Err(format!("unknown Logos SQL database '{value}'")), - } - } -} - -impl DatabaseKind { - const fn name(self) -> &'static str { - match self { - Self::Live => "live", - Self::Finalized => "finalized", - } - } -} diff --git a/tests/logos-sql/src/cucumber/steps/logos_sql/password_manager.rs b/tests/logos-sql/src/cucumber/steps/logos_sql/password_manager.rs deleted file mode 100644 index 1d2a5b0..0000000 --- a/tests/logos-sql/src/cucumber/steps/logos_sql/password_manager.rs +++ /dev/null @@ -1,202 +0,0 @@ -//! Runs the password-manager executable, so scenarios use its actual SQL -//! builders. - -use std::{ - env, fs, - io::Write as _, - path::{Path, PathBuf}, - process::{Output, Stdio}, - time::Duration, -}; - -use cucumber::{gherkin::Step, when}; -use lb_groth16::fr_to_bytes; -use logos_blockchain_tests::cucumber::{error::StepError, steps::parse_steps::parse_table_rows}; -use tokio::{ - io::AsyncWriteExt as _, - process::{Child, Command}, - time::timeout, -}; - -use crate::cucumber::{error::StepResult, world::SqlWorld}; - -#[when(expr = "the password manager using sequencer {string} runs this session in {int} seconds:")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber requires a mutable world for step functions" -)] -async fn run_session( - world: &mut SqlWorld, - step: &Step, - sequencer: String, - seconds: u64, -) -> StepResult { - let session = Session::from_step(step)?; - let directory = app_directory(world); - fs::create_dir_all(&directory)?; - - let child = start_password_manager(world, &sequencer, &directory)?; - let output = session.run(child, Duration::from_secs(seconds)).await?; - - fs::write(directory.join("stdout.log"), &output.stdout)?; - fs::write(directory.join("stderr.log"), &output.stderr)?; - - Ok(session.check_output(&output)?) -} - -fn app_directory(world: &SqlWorld) -> PathBuf { - // Keep restarts in one scenario together, but never reuse an earlier run's - // database. - let run = world - .sdk - .node - .lifecycle - .test_context - .as_ref() - .expect("scenario has a test context"); - - world - .sdk - .node - .lifecycle - .scenario_base_dir - .join("password_manager") - .join(run) -} - -/// The terminal commands and expected responses from the scenario table. -struct Session { - commands: Vec<(String, String)>, -} - -impl Session { - fn from_step(step: &Step) -> Result { - let commands = parse_table_rows( - step, - &["command", "output"], - "password-manager session", - |row| match row { - [command, output] => Ok((command.clone(), output.clone())), - _ => Err(StepError::InvalidArgument { - message: "password-manager session requires command and output columns" - .to_owned(), - }), - }, - )?; - - Ok(Self { commands }) - } - - async fn run(&self, mut child: Child, duration: Duration) -> Result { - let mut input = self - .commands - .iter() - .map(|(command, _)| command.as_str()) - .collect::>() - .join("\n"); - input.push_str("\nexit\n"); - let mut stdin = child.stdin.take().ok_or_else(|| StepError::LogicalError { - message: "password-manager stdin was not piped".to_owned(), - })?; - - Ok(timeout(duration, async move { - stdin.write_all(input.as_bytes()).await?; - drop(stdin); - child.wait_with_output().await - }) - .await - .map_err(|_| StepError::Timeout { - message: "password-manager session did not finish".to_owned(), - })??) - } - - fn check_output(&self, output: &Output) -> Result<(), StepError> { - let stdout = String::from_utf8_lossy(&output.stdout); - let stderr = String::from_utf8_lossy(&output.stderr); - - if !output.status.success() || stderr.contains("error:") { - return Err(StepError::StepFail { - message: format!("password-manager session failed\n{stdout}\n{stderr}"), - }); - } - - let responses = stdout - .split("password-manager> ") - .skip(1) - .collect::>(); - for (index, (command, expected)) in self.commands.iter().enumerate() { - let actual = responses.get(index).copied().unwrap_or_default(); - if !actual.contains(expected) { - return Err(StepError::StepFail { - message: format!( - "command `{command}`: expected `{expected}`, got `{actual}`\n{stderr}" - ), - }); - } - } - - Ok(()) - } -} - -fn start_password_manager( - world: &SqlWorld, - sequencer: &str, - directory: &Path, -) -> Result { - let binary = env::var_os("PASSWORD_MANAGER_BIN").map_or_else( - || { - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("../../target/debug/examples/password_manager") - }, - PathBuf::from, - ); - if !binary.is_file() { - return Err(StepError::Preflight { - message: "Build `cargo build -p logos-sql --example password_manager`, or set PASSWORD_MANAGER_BIN".to_owned(), - }); - } - - let node = world.sdk.zone.sequencer_node_name(sequencer)?; - let funding_key = world.sdk.node.funding_wallet(node)?.public_key()?; - let signing_key = world - .sdk - .zone - .sequencer_signing_key(sequencer)? - .clone() - .into_unsecured(); - let channel = world.sdk.zone.sequencer_channel_id(sequencer)?; - let key_path = directory.join("encryption.key"); - let mut options = fs::OpenOptions::new(); - options.write(true).create(true).truncate(true); - - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt as _; - - options.mode(0o600) - }; - - // A fixed test key lets subsequent sessions read the same credentials. - options.open(&key_path)?.write_all(&[42; 32])?; - - Ok(Command::new(binary) - .env("LOGOS_SQL_ENCRYPTION_KEY_FILE", key_path) - .env("LOGOS_SQL_CHANNEL_ID", hex::encode(channel.as_ref())) - .env("LOGOS_SQL_SIGNING_KEY", hex::encode(signing_key.as_bytes())) - .env( - "LOGOS_SQL_FUNDING_KEY", - hex::encode(fr_to_bytes(&funding_key.into())), - ) - .env("LOGOS_SQL_MAX_TX_FEE", u64::MAX.to_string()) - .env( - "LOGOS_SQL_NODE_URL", - world.sdk.zone_node_url_for_sequencer(sequencer)?.as_str(), - ) - .env("LOGOS_SQL_STATE_DIR", directory.join("state")) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .kill_on_drop(true) - .spawn()?) -} diff --git a/tests/logos-sql/src/cucumber/steps/logos_sql/steps.rs b/tests/logos-sql/src/cucumber/steps/logos_sql/steps.rs deleted file mode 100644 index c04101c..0000000 --- a/tests/logos-sql/src/cucumber/steps/logos_sql/steps.rs +++ /dev/null @@ -1,231 +0,0 @@ -//! Cucumber entrypoints for Logos SQL scenarios. - -use std::{ - num::{NonZeroU16, NonZeroUsize}, - time::Duration, -}; - -use cucumber::{gherkin::Step, given, then, when}; -use logos_blockchain_tests::cucumber::error::StepError; -use logos_sql::PublicationConfig; - -use super::{ - DatabaseKind, - actions::{self, InstanceMode}, - assertions, - tables::{instance_rows, write_rows}, -}; -use crate::{ - benchmarks::logos_sql::SqlWorkload, - cucumber::{error::StepResult, world::SqlWorld}, -}; - -#[given("I start Logos SQL instances:")] -#[when("I start Logos SQL instances:")] -async fn step_start_logos_sql_instances(world: &mut SqlWorld, step: &Step) -> StepResult { - actions::start_instances(world, instance_rows(step)?, InstanceMode::default()).await -} - -#[given( - expr = "I start Logos SQL instances with a {int}% fee reserve, batches of {int} writes and a queue of {int} writes:" -)] -#[when( - expr = "I start Logos SQL instances with a {int}% fee reserve, batches of {int} writes and a queue of {int} writes:" -)] -async fn step_start_logos_sql_instances_with_batching( - world: &mut SqlWorld, - priority_fee_percent: u64, - max_transactions: NonZeroU16, - max_pending_writes: NonZeroUsize, - step: &Step, -) -> StepResult { - let mode = InstanceMode::Writer { - priority_fee_percent, - publication: PublicationConfig { - max_transactions, - max_pending_writes, - }, - }; - - actions::start_instances(world, instance_rows(step)?, mode).await -} - -#[given("I start read-only Logos SQL instances:")] -#[when("I start read-only Logos SQL instances:")] -async fn step_start_read_only_logos_sql_instances(world: &mut SqlWorld, step: &Step) -> StepResult { - actions::start_instances(world, instance_rows(step)?, InstanceMode::ReadOnly).await -} - -#[when(expr = "I stop Logos SQL instance {string}")] -async fn step_stop_logos_sql_instance(world: &mut SqlWorld, instance_alias: String) -> StepResult { - actions::stop_instance(world, &instance_alias).await -} - -#[when(expr = "Logos SQL instance {string} executes write {string}:")] -async fn step_execute_logos_sql_write( - world: &mut SqlWorld, - step: &Step, - instance_alias: String, - write_alias: String, -) -> StepResult { - actions::execute_write(world, &instance_alias, write_alias, sql_docstring(step)?).await -} - -#[when("the following Logos SQL writes execute concurrently:")] -async fn step_execute_logos_sql_writes_concurrently( - world: &mut SqlWorld, - step: &Step, -) -> StepResult { - actions::execute_writes_concurrently(world, write_rows(step)?).await -} - -#[when( - expr = "I benchmark Logos SQL writer {string} and replica {string} on sequencer {string} for {int} seconds with {int} byte rows" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first mutable argument" -)] -async fn step_benchmark_logos_sql( - world: &mut SqlWorld, - writer_alias: String, - replica_alias: String, - sequencer_alias: String, - seconds: u64, - payload_bytes: usize, -) -> StepResult { - let workload = SqlWorkload { - payload_bytes, - measure_for: Duration::from_secs(seconds), - }; - - actions::benchmark( - world, - &writer_alias, - &replica_alias, - &sequencer_alias, - workload, - ) - .await -} - -#[then( - expr = "Logos SQL instance {string} has {int} rows in table {string} in its {logos_sql_database} database in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first mutable argument" -)] -async fn step_table_row_count( - world: &mut SqlWorld, - instance_alias: String, - expected: usize, - table: String, - database: DatabaseKind, - timeout_seconds: u64, -) -> StepResult { - assertions::wait_for_table_row_count( - world, - &instance_alias, - &table, - expected, - database, - timeout_seconds, - ) - .await -} - -#[then( - expr = "Logos SQL instances {string} and {string} agree on this {logos_sql_database} query in {int} seconds:" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first mutable argument" -)] -async fn step_query_agreement( - world: &mut SqlWorld, - step: &Step, - left_alias: String, - right_alias: String, - database: DatabaseKind, - timeout_seconds: u64, -) -> StepResult { - assertions::wait_for_query_agreement( - world, - &left_alias, - &right_alias, - &sql_docstring(step)?, - database, - timeout_seconds, - ) - .await -} - -#[then( - expr = "Logos SQL instance {string} returns text {string} from this {logos_sql_database} query in {int} seconds:" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first mutable argument" -)] -async fn step_text_query_result( - world: &mut SqlWorld, - step: &Step, - instance_alias: String, - expected: String, - database: DatabaseKind, - timeout_seconds: u64, -) -> StepResult { - assertions::wait_for_text_query_result( - world, - &instance_alias, - &sql_docstring(step)?, - &expected, - database, - timeout_seconds, - ) - .await -} - -#[then( - expr = "exactly one of Logos SQL writes {string} and {string} is displaced in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first mutable argument" -)] -async fn step_one_write_is_displaced( - world: &mut SqlWorld, - left_alias: String, - right_alias: String, - timeout_seconds: u64, -) -> StepResult { - assertions::wait_for_one_displaced_write(world, &left_alias, &right_alias, timeout_seconds) - .await -} - -#[then(expr = "Logos SQL write {string} is displaced in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first mutable argument" -)] -async fn step_write_is_displaced( - world: &mut SqlWorld, - write_alias: String, - timeout_seconds: u64, -) -> StepResult { - assertions::wait_for_displaced_write(world, &write_alias, timeout_seconds).await -} - -fn sql_docstring(step: &Step) -> Result { - let sql = step - .docstring - .as_deref() - .map(str::trim) - .filter(|sql| !sql.is_empty()) - .ok_or_else(|| StepError::InvalidArgument { - message: format!("step '{}' requires a non-empty SQL doc string", step.value), - })?; - - Ok(sql.to_owned()) -} diff --git a/tests/logos-sql/src/cucumber/steps/logos_sql/tables.rs b/tests/logos-sql/src/cucumber/steps/logos_sql/tables.rs deleted file mode 100644 index 36764d3..0000000 --- a/tests/logos-sql/src/cucumber/steps/logos_sql/tables.rs +++ /dev/null @@ -1,70 +0,0 @@ -//! Parsing for the tables used by Logos SQL Cucumber steps. - -use cucumber::gherkin::Step; -use logos_blockchain_tests::cucumber::{error::StepError, steps::parse_steps::parse_table_rows}; - -pub(super) struct InstanceRow { - pub alias: String, - pub sequencer: String, -} - -pub(super) struct WriteRow { - pub instance: String, - pub write: String, - pub sql: String, -} - -pub(super) fn instance_rows(step: &Step) -> Result, StepError> { - parse_table_rows( - step, - &["alias", "sequencer"], - "Logos SQL instance", - |row| match row { - [alias, sequencer] if !alias.trim().is_empty() && !sequencer.trim().is_empty() => { - Ok(InstanceRow { - alias: alias.clone(), - sequencer: sequencer.clone(), - }) - } - [_, _] => Err(StepError::InvalidArgument { - message: "Logos SQL instance aliases and sequencers cannot be empty".to_owned(), - }), - _ => Err(StepError::InvalidArgument { - message: format!( - "Logos SQL instance rows must have exactly 2 columns, got {}", - row.len() - ), - }), - }, - ) -} - -pub(super) fn write_rows(step: &Step) -> Result, StepError> { - parse_table_rows( - step, - &["instance", "write", "sql"], - "Logos SQL concurrent write", - |row| match row { - [instance, write, sql] - if !instance.trim().is_empty() - && !write.trim().is_empty() - && !sql.trim().is_empty() => - { - Ok(WriteRow { - instance: instance.clone(), - write: write.clone(), - sql: sql.clone(), - }) - } - [_, _, _] => Err(StepError::InvalidArgument { - message: "Logos SQL concurrent write fields cannot be empty".to_owned(), - }), - _ => Err(StepError::InvalidArgument { - message: format!( - "Logos SQL concurrent write rows must have exactly 3 columns, got {}", - row.len() - ), - }), - }, - ) -} diff --git a/tests/logos-sql/src/cucumber/steps/mod.rs b/tests/logos-sql/src/cucumber/steps/mod.rs deleted file mode 100644 index 2abbcd2..0000000 --- a/tests/logos-sql/src/cucumber/steps/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod logos_sql; - -const TARGET: &str = "cucumber_steps"; diff --git a/tests/logos-sql/src/cucumber/world.rs b/tests/logos-sql/src/cucumber/world.rs deleted file mode 100644 index 125391e..0000000 --- a/tests/logos-sql/src/cucumber/world.rs +++ /dev/null @@ -1,52 +0,0 @@ -use cucumber::World; -use logos_blockchain_tests::cucumber::{ - error::StepResult, runner::ScenarioCleanup, world::CucumberWorld, -}; -use logos_blockchain_zone_tests::cucumber::world::ZoneWorld; - -use super::logos_sql::LogosSqlState; - -#[derive(Debug, Default, World)] -pub struct SqlWorld { - pub sdk: ZoneWorld, - pub logos_sql: LogosSqlState, -} - -impl SqlWorld { - pub fn full_debug_info_string(&self) -> String { - format!( - "{}\n\nLogos SQL state:\n{:#?}", - self.sdk.full_debug_info_string(), - self.logos_sql, - ) - } -} - -impl AsMut for SqlWorld { - fn as_mut(&mut self) -> &mut CucumberWorld { - &mut self.sdk.node - } -} - -impl ScenarioCleanup for SqlWorld { - async fn cleanup(&mut self) -> StepResult { - let sql = self.logos_sql.shutdown_all().await; - let sdk = self.sdk.cleanup().await; - - // Always clean up the SDK and nodes, even if a SQL shutdown failed. - sql.and(sdk) - } -} - -impl AsMut for SqlWorld { - fn as_mut(&mut self) -> &mut ZoneWorld { - &mut self.sdk - } -} - -impl Drop for SqlWorld { - fn drop(&mut self) { - // Stop SQL replicas before dropping their channel and node support. - self.logos_sql.clear(); - } -} diff --git a/tests/logos-sql/src/lib.rs b/tests/logos-sql/src/lib.rs deleted file mode 100644 index d50b8cb..0000000 --- a/tests/logos-sql/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod benchmarks; -pub mod cucumber; diff --git a/tests/logos-sql/tests/cucumber.rs b/tests/logos-sql/tests/cucumber.rs deleted file mode 100644 index 7cdd52e..0000000 --- a/tests/logos-sql/tests/cucumber.rs +++ /dev/null @@ -1,8 +0,0 @@ -use std::process::ExitCode; - -use logos_blockchain_sql_tests::cucumber::run; - -#[tokio::main] -async fn main() -> ExitCode { - run().await -} diff --git a/tests/zone-sdk/Cargo.toml b/tests/zone-sdk/Cargo.toml index 9da8a51..0cd60fa 100644 --- a/tests/zone-sdk/Cargo.toml +++ b/tests/zone-sdk/Cargo.toml @@ -12,23 +12,11 @@ version = { workspace = true } [lints] workspace = true -[dependencies] -cucumber = { features = ["macros", "output-junit"], workspace = true } -futures = { workspace = true } -lb-common-http-client = { workspace = true } -lb-core = { workspace = true } -lb-groth16 = { workspace = true } -lb-http-api-common = { workspace = true } -lb-key-management-system-service = { workspace = true } -lb-testing-framework = { workspace = true } -lb-utils = { workspace = true } -lb-zone-sdk = { workspace = true } -logos-blockchain-tests = { workspace = true } -rand = { workspace = true } -reqwest = { workspace = true } -thiserror = { workspace = true } -tokio = { workspace = true } -tracing = { workspace = true } +[dev-dependencies] +cucumber = { features = ["macros", "output-junit"], workspace = true } +lb-testing-framework = { workspace = true } +logos-blockchain-tests = { workspace = true } +tokio = { features = ["macros", "rt-multi-thread"], workspace = true } [features] cucumber = [] diff --git a/tests/zone-sdk/features/zone.feature b/tests/zone-sdk/features/zone.feature deleted file mode 100644 index c6757a8..0000000 --- a/tests/zone-sdk/features/zone.feature +++ /dev/null @@ -1,947 +0,0 @@ -Feature: Zone SDK - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_sequencer_publish_and_indexer_read - Scenario: Publish messages and read them from the zone indexer - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | Hello, Zone! | - | MSG_2 | Second message | - | MSG_3 | Third message | - Then all zone messages are safe in 120 seconds - And all zone messages are finalized in 180 seconds - And the zone indexer returns messages in this order: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - When sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 0 | 0 | SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is included in 180 seconds - And zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_sequencer_checkpoint_resume - Scenario: Resume zone sequencer from checkpoint - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | Message 1 | - | MSG_2 | Message 2 | - And I save current checkpoint of sequencer "SEQ_A" as "CHECKPOINT_1" - And I restart zone sequencer "SEQ_A" from checkpoint "CHECKPOINT_1" - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_3 | Message 3 | - | MSG_4 | Message 4 | - Then all zone messages are safe in 120 seconds - And all zone messages are finalized in 180 seconds - And the zone indexer returns messages in this order: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - | MSG_4 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_sequencer_stale_checkpoint_resume - Scenario: Resume zone sequencer from stale checkpoint - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | msg-1 | - | MSG_2 | msg-2 | - And I save current checkpoint of sequencer "SEQ_A" as "STALE_CHECKPOINT" - Then all zone messages are finalized in 180 seconds - When I restart zone sequencer "SEQ_A" fresh - And the zone LIB advances in 120 seconds - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_3 | msg-3 | - | MSG_4 | msg-4 | - Then all zone messages are finalized in 180 seconds - When I restart zone sequencer "SEQ_A" from checkpoint "STALE_CHECKPOINT" - And the zone LIB advances in 120 seconds - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_5 | msg-5 | - Then all zone messages are finalized in 180 seconds - And the zone indexer returns each of these messages exactly once in this order: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - | MSG_4 | - | MSG_5 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Publishes issued while the node is down fail fast and succeed after reconnect - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - # Take the node down: the sequencer enters its reconnect loop, but its - # in-process SequencerClient stays alive. With funding configured, - # publishing needs the node's wallet, so publishes are rejected while - # it is down; a fresh Ready event fires once the reconnect completes. - When I stop node "NODE_1" - Then publishing zone message with data "while down" via sequencer "SEQ_A" fails while the node is down - # Bring the node back; publishes retry through the reconnect window and - # succeed once the sequencer re-emits Ready. - When I restart node "NODE_1" - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | After down (1) | - | MSG_2 | After down (2) | - | MSG_3 | After down (3) | - Then all zone messages are safe in 120 seconds - And all zone messages are finalized in 180 seconds - And the zone indexer returns messages in this order: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_sequential_multi_sequencer - Scenario: Sequential multi-sequencer publishing keeps channel order - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | a1 | - | MSG_2 | a2 | - | MSG_3 | a3 | - Then the zone indexer returns messages in any order in 360 seconds: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - When I stop zone sequencer "SEQ_A" - And I start zone sequencer "SEQ_B" - And sequencer "SEQ_B" publishes the following zone messages: - | alias | data | - | MSG_4 | b1 | - | MSG_5 | b2 | - | MSG_6 | b3 | - Then the zone indexer returns messages in any order in 360 seconds: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - | MSG_4 | - | MSG_5 | - | MSG_6 | - When I stop zone sequencer "SEQ_B" - And I start zone sequencer "SEQ_A" - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_7 | a4 | - | MSG_8 | a5 | - | MSG_9 | a6 | - Then the zone indexer returns messages in this order: - | alias | - | MSG_1 | - | MSG_2 | - | MSG_3 | - | MSG_4 | - | MSG_5 | - | MSG_6 | - | MSG_7 | - | MSG_8 | - | MSG_9 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_concurrent_multi_sequencer - Scenario: Concurrent multi-sequencer publishing converges without duplicates - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 1000000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B, SEQ_C | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - | SEQ_C | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 200 notes of 3000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - When I stop zone sequencer "SEQ_A" - And each listed zone sequencer publishes 20 generated zone messages concurrently with republish policy: - | sequencer | data_prefix | - | SEQ_A | a | - | SEQ_B | b | - | SEQ_C | c | - Then the zone indexer returns all zone messages exactly once in any order in 1200 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_sorted_conflict_resolution - Scenario: Sorted conflict policy preserves per-sequencer order and converges without duplicates - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 10 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I stop zone sequencer "SEQ_A" - And the following zone messages are published concurrently with sorted conflict policy: - | sequencer | alias | data | - | SEQ_A | MSG_1 | aa | - | SEQ_A | MSG_2 | cc | - | SEQ_A | MSG_3 | ee | - | SEQ_A | MSG_4 | gg | - | SEQ_A | MSG_5 | ii | - | SEQ_B | MSG_6 | bb | - | SEQ_B | MSG_7 | dd | - | SEQ_B | MSG_8 | ff | - | SEQ_B | MSG_9 | hh | - | SEQ_B | MSG_10 | jj | - Then the zone indexer preserves per-sequencer order and converges without duplicates in 600 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # End-to-end multi-sig driven by the sequencers' own drive loops: three - # sequencers run the reactive lifecycle policy, escalate the config - # single-signer -> 2-of-2 -> 2-of-3, then a deposit fires and they pin and - # withdraw it under 2-of-3, exchanging signatures over the shared bus. - Scenario: Multi-sig deposit lifecycle reacts across independent sequencers - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B, SEQ_C | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I do a coin split for "WALLET_1A" of 3 UTXOs valued at 5 LGO tokens each - # Each sequencer runs its own drive loop + policy, sharing one bus. - And I start multi-sig lifecycle zone sequencers with withdraw outputs "3": - | alias | - | SEQ_A | - | SEQ_B | - | SEQ_C | - # Config: single-signer -> 2-of-2 -> 2-of-3. Timeframe 15 lets a full - # prepare -> sign -> submit round land within a turn while still rotating. - And sequencer "SEQ_A" prepares zone config transaction "CHANNEL_CONFIG_1" with threshold 1 authorizing: - | alias | - | SEQ_A | - And sequencer "SEQ_A" submits prepared zone config transaction "CHANNEL_CONFIG_1" - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When sequencer "SEQ_A" prepares zone config transaction "CHANNEL_CONFIG_2" with threshold 2 authorizing: - | alias | - | SEQ_A | - | SEQ_B | - And sequencer "SEQ_A" signs prepared zone config transaction "CHANNEL_CONFIG_2" - And sequencer "SEQ_A" submits prepared zone config transaction "CHANNEL_CONFIG_2" - Then zone transaction "CHANNEL_CONFIG_2" is finalized in 180 seconds - When sequencer "SEQ_A" prepares zone config transaction "CHANNEL_CONFIG_3" with threshold 2 and posting timeframe 15 authorizing: - | alias | - | SEQ_A | - | SEQ_B | - | SEQ_C | - And sequencer "SEQ_B" signs prepared zone config transaction "CHANNEL_CONFIG_3" - And sequencer "SEQ_A" signs prepared zone config transaction "CHANNEL_CONFIG_3" - And sequencer "SEQ_B" submits prepared zone config transaction "CHANNEL_CONFIG_3" - Then zone transaction "CHANNEL_CONFIG_3" is finalized in 180 seconds - # Fire the deposit — the only external trigger; the sequencers react. - When I submit zone deposit transaction "DEPOSIT_1" into channel of "SEQ_A" of 5 with metadata "Mint 5 to channel" - # Exactly one pin + one withdraw finalized (no surviving double-publish), and - # the channel keeps only the value-2 change: proves 5 pinned, 3 withdrawn. - Then the zone indexer returns exactly one finalized pin and withdraw for deposit "DEPOSIT_1" in 300 seconds - And the channel wallet of "SEQ_A" has exactly 1 finalized and 0 unfinalized notes in 120 seconds - And the channel wallet of "SEQ_A" contains a finalized note of value 2 in 120 seconds - And the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Round-robin waits for turn and submits pending messages - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_A | true | 2 | false | - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_B | false | 2 | false | - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 0 | 120 | - # Prepare three signed pending messages while SEQ_B is not on turn — tests bounded submit depth - When sequencer "SEQ_B" submits the following zone messages to queue immediately: - | alias | data | - | MSG_B1 | rr-queued-b1 | - | MSG_B2 | rr-queued-b2 | - | MSG_B3 | rr-queued-b3 | - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 3 | 120 | - # Save checkpoint with signed pending txs, restart, verify pending outbox restored - When I save current checkpoint of sequencer "SEQ_B" as "CHECKPOINT_B_PENDING" - And I stop zone sequencer "SEQ_B" - And I restart zone sequencer "SEQ_B" from checkpoint "CHECKPOINT_B_PENDING" - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 3 | 120 | - # The first turn submits only the configured active depth, so two txs are posted but remain pending until finalized - And zone messages queued by sequencer "SEQ_B" are included after its turn in 180 seconds: - | alias | - | MSG_B1 | - | MSG_B2 | - Then sequencer "SEQ_B" has 3 pending publish txs in 180 seconds - Then sequencer "SEQ_B" has 1 pending publish txs in 180 seconds - And the zone indexer returns messages in any order in 360 seconds: - | alias | - | MSG_B1 | - | MSG_B2 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Round-robin submits all pending messages with no active depth limit - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_A | true | unlimited | false | - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_B | false | unlimited | false | - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 0 | 120 | - When sequencer "SEQ_B" submits the following zone messages to queue immediately: - | alias | data | - | MSG_C1 | rr-unbounded-1 | - | MSG_C2 | rr-unbounded-2 | - | MSG_C3 | rr-unbounded-3 | - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 3 | 120 | - When I save current checkpoint of sequencer "SEQ_B" as "CHECKPOINT_B_NO_LIMIT" - And I stop zone sequencer "SEQ_B" - And I restart zone sequencer "SEQ_B" from checkpoint "CHECKPOINT_B_NO_LIMIT" - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 3 | 120 | - And zone messages queued by sequencer "SEQ_B" are included after its turn in 180 seconds: - | alias | - | MSG_C1 | - | MSG_C2 | - | MSG_C3 | - Then sequencer "SEQ_B" has 3 pending publish txs in 180 seconds - And the zone indexer returns messages in any order in 360 seconds: - | alias | - | MSG_C1 | - | MSG_C2 | - | MSG_C3 | - Then sequencer "SEQ_B" has 0 pending publish txs in 180 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # A config landing changes the channel view without moving the message tip, so - # the sequencer sheds its pending inscriptions (reports them orphaned). With - # orphan-republish on, they must be re-posted respecting lineage and end up - # indexed in order — i.e. a config that lands while messages are pending must - # not lose or reorder them. SEQ_B holds three messages pending (it is off - # turn) while SEQ_A lands a second config, which triggers the shed. - Scenario: A config landing orphans pending inscriptions which then re-adopt in order - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_A | true | unlimited | false | - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_B | false | unlimited | true | - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 0 | 120 | - # SEQ_B queues messages while off turn — they sit pending. - When sequencer "SEQ_B" submits the following zone messages to queue immediately: - | alias | data | - | MSG_B1 | cfg-orphan-b1 | - | MSG_B2 | cfg-orphan-b2 | - | MSG_B3 | cfg-orphan-b3 | - Then sequencer "SEQ_B" reaches sequencing state: - | own_key_index | turn_to_write | pending_transactions | time_out | - | 1 | NOT_OUR_TURN | 3 | 120 | - # A second config lands while SEQ_B's three messages are pending: the new - # config view sheds them, and orphan-republish must recover them. - When sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_2 | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_2" is finalized in 180 seconds - # The shed inscriptions survive: re-posted and indexed in lineage order. - And the zone indexer returns messages in this order: - | alias | - | MSG_B1 | - | MSG_B2 | - | MSG_B3 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Round-robin publishes immediately when it is our turn - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I start zone sequencer "SEQ_B" - Then sequencer "SEQ_A" is notified it is their turn to write in 120 seconds - And sequencer "SEQ_B" is notified it is their turn to write in 120 seconds - And sequencer "SEQ_A" is notified it is their turn to write in 120 seconds - When I submit zone message "MSG_A1" to sequencer "SEQ_A" with data "decentralized-immediate-publish" immediately - Then zone message "MSG_A1" published by sequencer "SEQ_A" is included while in turn in 120 seconds - And the zone indexer returns messages in any order in 360 seconds: - | alias | - | MSG_A1 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Round-robin with multiple sequencers dynamically added - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B, SEQ_C | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_A | true | default | true | - # Start with A-only round-robin config (single key), then prove immediate publish. - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 2 | 0 | SEQ_A | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I submit zone message "MSG_A_1" to sequencer "SEQ_A" with data "seq_a-msg1" on its turn - # Auth B without stopping A. - When sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CONFIG_B | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CONFIG_B" is finalized in 180 seconds - When I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_B | false | default | true | - When I submit zone message "MSG_B_1" to sequencer "SEQ_B" with data "seq_b-msg1" on its turn - # Auth C without stopping A or B. - When sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CONFIG_C | 2 | 0 | SEQ_A, SEQ_B, SEQ_C | - Then zone transaction "CONFIG_C" is finalized in 180 seconds - When I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_C | false | default | true | - When I submit zone message "MSG_C_1" to sequencer "SEQ_C" with data "seq_c-msg1" on its turn - # Now publish more messages from all sequencers and check they are all indexed without duplicates - When I submit zone message "MSG_A_2" to sequencer "SEQ_A" with data "seq_a-msg2" immediately - When I submit zone message "MSG_B_2" to sequencer "SEQ_B" with data "seq_b-msg2" immediately - When I submit zone message "MSG_C_2" to sequencer "SEQ_C" with data "seq_c-msg2" immediately - # Final check: all messages on chain, exactly once (catches duplicate republishes). - Then the zone indexer returns all zone messages exactly once in any order in 120 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_balance_conditioned_republish - Scenario: Balance-aware republish policy drops unaffordable zone updates - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B, SEQ_C | - And the following zone account balances exist: - | account | balance | - | alice | 10 | - | bob | 10 | - | charlie | 10 | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 60 | 0 | SEQ_A, SEQ_B, SEQ_C | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I stop zone sequencer "SEQ_A" - And the following zone balance updates are published concurrently with balance-aware policy: - | sequencer | alias | account | delta | - | SEQ_A | a-alice | alice | -6 | - | SEQ_A | a-bob | bob | -3 | - | SEQ_A | a-charlie | charlie | -2 | - | SEQ_B | b-alice | alice | -5 | - | SEQ_B | b-bob | bob | -4 | - | SEQ_B | b-charlie | charlie | -8 | - | SEQ_C | c-alice | alice | -4 | - | SEQ_C | c-bob | bob | -7 | - | SEQ_C | c-charlie | charlie | -1 | - Then zone balance updates keep all accounts non-negative after 60 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_concurrent_identical_payloads - Scenario: Concurrent identical payloads converge to one inscription per publish - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 1000000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B, SEQ_C | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 200 notes of 3000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 60 | 0 | SEQ_A, SEQ_B, SEQ_C | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I stop zone sequencer "SEQ_A" - And each listed zone sequencer publishes 10 copies of zone message "shared-message" concurrently with republish policy: - | sequencer | - | SEQ_A | - | SEQ_B | - | SEQ_C | - Then the zone indexer returns 30 copies of zone message "shared-message" in 600 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [zone-sdk channel wallet] a multi-input deposit's recreated notes surface in - # the sequencer's channel wallet with their exact per-note values, move to the - # finalized layer with LIB, and survive checkpoint restore and a fresh restart. - Scenario: Channel wallet tracks deposited notes with exact per-note values through finalization - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 2 in 300 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I do a coin split for "WALLET_1A" of 1 UTXOs valued at 1 LGO tokens each - And I do a coin split for "WALLET_1A" of 1 UTXOs valued at 2 LGO tokens each - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | initial inscription | - Then all zone messages are safe in 120 seconds - When I submit zone deposit transaction "DEPOSIT_1" into channel of "SEQ_A" consuming notes valued "1,2" with metadata "Mint 1 and 2 into the channel" - Then zone transaction "DEPOSIT_1" is included in 120 seconds - And the channel wallet of "SEQ_A" contains a note of value 1 in 120 seconds - And the channel wallet of "SEQ_A" contains a note of value 2 in 120 seconds - And zone transaction "DEPOSIT_1" is finalized in 120 seconds - And the channel wallet of "SEQ_A" contains a finalized note of value 1 in 120 seconds - And the channel wallet of "SEQ_A" contains a finalized note of value 2 in 120 seconds - And the channel wallet of "SEQ_A" has exactly 2 finalized and 0 unfinalized notes in 120 seconds - When I save current checkpoint of sequencer "SEQ_A" as "WALLET_CHECKPOINT" - And I restart zone sequencer "SEQ_A" from checkpoint "WALLET_CHECKPOINT" - Then the channel wallet of "SEQ_A" has exactly 2 finalized and 0 unfinalized notes in 120 seconds - When I restart zone sequencer "SEQ_A" fresh - Then the channel wallet of "SEQ_A" has exactly 2 finalized and 0 unfinalized notes in 120 seconds - Then the channel view contract holds for all zone sequencers - - # [tests/src/tests/zone_sdk/e2e.rs] test_subscribe_to_finalized_deposit - Scenario: Finalized deposits are returned by the zone indexer - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 2 in 300 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I do a coin split for "WALLET_1A" of 3 UTXOs valued at 1 LGO tokens each - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | initial inscription | - Then all zone messages are safe in 120 seconds - When I submit zone deposit transaction "DEPOSIT_1" into channel of "SEQ_A" of 1 with metadata "Mint 1 to Alice in Zone" - Then zone transaction "DEPOSIT_1" is included in 120 seconds - And zone transaction "DEPOSIT_1" is finalized in 120 seconds - And the zone indexer returns finalized deposit "DEPOSIT_1" in 120 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_atomic_deposit_inscription - Scenario: Atomic deposit and inscription are finalized together - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | initial inscription | - Then all zone messages are finalized in 120 seconds - When sequencer "SEQ_A" submits atomic zone deposit transaction "ATOMIC_1" with inscription "MSG_2" of 1 with metadata "Mint 1 to Alice in Zone" - Then zone transaction "ATOMIC_1" is included in 120 seconds - And zone transaction "ATOMIC_1" is finalized in 120 seconds - And the zone indexer returns finalized deposit "ATOMIC_1" in 120 seconds - And the zone indexer returns messages in this order: - | alias | - | MSG_1 | - | MSG_2 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - # [tests/src/tests/zone_sdk/e2e.rs] test_subscribe_to_finalized_withdraw - Scenario: Finalized withdraws are returned by the zone indexer and sequencer - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 2 in 300 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I do a coin split for "WALLET_1A" of 3 UTXOs valued at 3 LGO tokens each - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | initial inscription | - Then all zone messages are finalized in 120 seconds - When I submit zone deposit transaction "DEPOSIT_1" into channel of "SEQ_A" of 3 with metadata "Mint 3 to Alice in Zone" - Then zone transaction "DEPOSIT_1" is included in 120 seconds - And zone transaction "DEPOSIT_1" is finalized in 120 seconds - And the zone indexer returns finalized deposit "DEPOSIT_1" in 120 seconds - And sequencer "SEQ_A" finalizes deposit "DEPOSIT_1" in 120 seconds - When sequencer "SEQ_A" submits zone withdraw transaction "WITHDRAW_1" with inscription "MSG_2" of 2 - Then zone transaction "WITHDRAW_1" is included in 120 seconds - And zone transaction "WITHDRAW_1" is finalized in 120 seconds - And the zone indexer returns finalized withdraw "WITHDRAW_1" in 120 seconds - And sequencer "SEQ_A" finalizes withdraw "WITHDRAW_1" in 120 seconds - And the zone indexer returns messages in this order: - | alias | - | MSG_1 | - | MSG_2 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - # Deposit 5 → withdraw "1,1,2" leaves a value-1 change note that can only exist - # if both the integration inscription and the withdraw landed, so its - # finalization proves the whole reactive chain. - Scenario: Observed deposit is integrated then withdrawn without waiting for finalization - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - And the following zone sequencers share the signing key of "SEQ_A": - | alias | - | SEQ_B | - When node "NODE_1" is at height 2 in 300 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I do a coin split for "WALLET_1A" of 3 UTXOs valued at 5 LGO tokens each - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_INIT | initial inscription | - Then all zone messages are finalized in 120 seconds - When I start zone sequencer "SEQ_B" pinning then withdrawing observed deposits with outputs "1,1,2" - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_A1 | a1 | - | MSG_A2 | a2 | - When I submit zone deposit transaction "DEPOSIT_1" into channel of "SEQ_A" of 5 with metadata "Mint 5 for atomic integration" - Then the channel wallet of "SEQ_B" contains a finalized note of value 1 in 300 seconds - And the zone indexer returns a finalized channel transfer consuming 1 inputs in 300 seconds - And the zone indexer returns messages in this order: - | alias | - | MSG_INIT | - | MSG_A1 | - | MSG_A2 | - Then the channel view contract holds for all zone sequencers - - @zone_ci - # A 10000 withdraw against a channel holding 255 dust notes (value 1) plus a - # single 10000 note exercises the Auto note-selection policy: it covers the - # amount with the big note and sweeps dust into the remaining input slots, up - # to the 255-input transaction limit — so the transfer consumes 255 inputs - # (the covering note + 254 swept dust) and compacts the channel wallet instead - # of leaving the dust unspent, while never exceeding the 255-input bound. The - # dust is minted by depositing one value-255 note and splitting it into 255 - # value-1 notes in a single channel transfer (bounded by the 255-output - # limit), rather than one ZK-signed deposit per 32 notes (a deposit is capped - # at 32 inputs by the ZK signing-key limit). - # The withdraw is reactive: SEQ_A reacts to the big deposit and publishes the - # sweeping withdraw. Dust exists before the big deposit, so it is swept. - Scenario: Reactive withdrawal stays valid under a dust flood - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 1000000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A | - When node "NODE_1" is at height 2 in 300 seconds - And wallet "WALLET_1A" sends 60 notes of 2000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I do a coin split for "WALLET_1A" of 1 UTXOs valued at 255 LGO tokens each - And I do a coin split for "WALLET_1A" of 1 UTXOs valued at 10000 LGO tokens each - And I start zone sequencer "SEQ_A" withdrawing observed deposit of 10000 with outputs "10000" - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_INIT | initial inscription | - Then all zone messages are finalized in 120 seconds - When I submit zone deposit transaction "DEPOSIT_DUST_SEED" into channel of "SEQ_A" of 255 with metadata "Dust seed" - And the channel wallet of "SEQ_A" contains a note of value 255 in 240 seconds - When sequencer "SEQ_A" splits deposit "DEPOSIT_DUST_SEED" into 255 dust notes as "SPLIT_DUST" - Then zone transaction "SPLIT_DUST" is finalized in 240 seconds - When I submit zone deposit transaction "DEPOSIT_BIG" into channel of "SEQ_A" of 10000 with metadata "Big note" - Then the zone indexer returns a finalized channel transfer consuming 255 inputs in 300 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Concurrent custom multi-inscription transactions recover from conflicts - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 1000000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 200 notes of 3000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencer "SEQ_A" with indexer - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CHANNEL_CONFIG_1 | 10 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CHANNEL_CONFIG_1" is finalized in 180 seconds - When I stop zone sequencer "SEQ_A" - And the following custom transactions are published concurrently with custom republish policy: - | sequencer | transactions | inscriptions | - | SEQ_A | 5 | 5 | - | SEQ_B | 5 | 5 | - Then the zone indexer returns all custom payloads in 600 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - # Config signatures must claim the signer's index in the current accredited - # list, not index 0 — CONFIG_BA below is signed by SEQ_B from index 1. - Scenario: Non-leading sequencer re-keys the channel and in-flight inscriptions recover - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 3 | 100000 | - And I have a cluster with capacity of 1 nodes - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | | SEQ_A, SEQ_B | - When node "NODE_1" is at height 1 in 120 seconds - And wallet "WALLET_1A" sends 30 notes of 1000 LGO to node "NODE_1" funding wallet as "FUNDING_TOPUP" - And transaction "FUNDING_TOPUP" is included on node "NODE_1" in 180 seconds - And I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_A | true | default | true | - And sequencer "SEQ_A" publishes the following zone messages: - | alias | data | - | MSG_1 | inscription before re-key | - And sequencer "SEQ_A" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CONFIG_AB | 2 | 0 | SEQ_A, SEQ_B | - Then zone transaction "CONFIG_AB" is finalized in 180 seconds - When I start zone sequencers: - | alias | indexer | pending_submit_depth | passive_republish_orphans | - | SEQ_B | false | default | true | - And sequencer "SEQ_A" submits the following zone messages without waiting for inclusion: - | alias | data | - | MSG_2 | in flight during re-key | - And sequencer "SEQ_B" submits zone config transaction: - | config_name | posting_timeframe | posting_timeout | authorized_sequencers | - | CONFIG_BA | 2 | 0 | SEQ_B, SEQ_A | - And sequencer "SEQ_B" submits the following zone messages without waiting for inclusion: - | alias | data | - | MSG_3 | post re-key from B | - And sequencer "SEQ_A" submits the following zone messages without waiting for inclusion: - | alias | data | - | MSG_4 | post re-key from A | - Then zone transaction "CONFIG_BA" is included in 180 seconds - And the zone indexer returns all zone messages exactly once in any order in 600 seconds - Then the channel view contract holds for all zone sequencers - - @zone_ci - Scenario: Zone sequencer re-funds a double-handed inscription after node restart - Given the genesis block has the following wallet resources: - | account_index | token_count | token_amount | - | 1 | 2 | 12500 | - | 4 | 4 | 25000 | - And I have a cluster with capacity of 2 nodes - # NODE_4 is only a fork-provider; SEQ_B is registered but never started, so - # the table's "at least one sequencer" rule is satisfied without running one - And I start nodes with wallet and sequencer resources: - | node_name | account_index | wallet_name | connected_to | sequencers | - | NODE_1 | 1 | WALLET_1A | NODE_4 | SEQ_A | - | NODE_4 | 4 | WALLET_4A | | SEQ_B | - When node "NODE_1" is at height 2 in 240 seconds - # Starve the funding wallet to two distinct notes so the reused note is - # deterministic (largest-first selection picks 9000 for both publishes) - And I drain all node "NODE_1" wallets into "WALLET_1A" - And wallet "WALLET_1A" has 14000 or more LGO in 240 seconds - And wallet "WALLET_1A" sends 1 notes of 9000 LGO to node "NODE_1" funding wallet as "TOPUP_A" - And wallet "WALLET_1A" sends 1 notes of 8000 LGO to node "NODE_1" funding wallet as "TOPUP_B" - And transaction "TOPUP_A" is included on node "NODE_1" in 240 seconds - And transaction "TOPUP_B" is included on node "NODE_1" in 240 seconds - And I start zone sequencer "SEQ_A" with indexer - # Isolate NODE_4 before MSG_1 exists so its branch can never carry it - When I stop node "NODE_4" - And sequencer "SEQ_A" submits the following zone messages without waiting for inclusion: - | alias | data | - | MSG_1 | message 1 | - And zone message "MSG_1" from sequencer "SEQ_A" is included on node "NODE_1" in 240 seconds - # NODE_1 down wipes its wallet reservations; NODE_4 out-builds it blind - When I stop node "NODE_1" - And I restart node "NODE_4" - And node "NODE_4" alone reaches height 14 in 300 seconds - # NODE_1 rejoins, adopts NODE_4's longer branch and re-admits the orphaned - # MSG_1 to its mempool; SEQ_A reconnects. The next publish double-hands - # MSG_1's note if it is funded before MSG_1 is mined again - When I restart node "NODE_1" - And node "NODE_1" is at height 14 in 240 seconds - And sequencer "SEQ_A" submits the following zone messages without waiting for inclusion: - | alias | data | - | MSG_2 | message 2 | - # The rebuild must work from a restored checkpoint too: the funding - # record travels with it - When I save current checkpoint of sequencer "SEQ_A" as "REFUND_CHECKPOINT" - And I restart zone sequencer "SEQ_A" from checkpoint "REFUND_CHECKPOINT" - Then the zone indexer returns all zone messages exactly once in any order in 600 seconds - And zone messages double-handed in checkpoint "REFUND_CHECKPOINT" finalized with the losers re-funded: - | alias | - | MSG_1 | - | MSG_2 | - Then the channel view contract holds for all zone sequencers diff --git a/tests/zone-sdk/src/cucumber/mod.rs b/tests/zone-sdk/src/cucumber/mod.rs deleted file mode 100644 index 8f48aad..0000000 --- a/tests/zone-sdk/src/cucumber/mod.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::{path::Path, process::ExitCode}; - -use cucumber::World as _; -use logos_blockchain_tests::cucumber::{ - deployment::LocalImplementation, runner, shared_steps::with_node_steps, -}; - -use self::world::ZoneWorld; - -pub mod shared_steps; -pub mod steps; -pub mod world; -mod zone; - -#[expect( - clippy::future_not_send, - reason = "Cucumber uses non-Send futures; the entry point awaits the runner directly" -)] -pub async fn run() -> ExitCode { - runner::run_suite::( - LocalImplementation::Logos, - Path::new(env!("CARGO_MANIFEST_DIR")).join("features"), - with_node_steps(ZoneWorld::collection()), - ZoneWorld::full_debug_info_string, - ) - .await -} diff --git a/tests/zone-sdk/src/cucumber/shared_steps.rs b/tests/zone-sdk/src/cucumber/shared_steps.rs deleted file mode 100644 index eb10382..0000000 --- a/tests/zone-sdk/src/cucumber/shared_steps.rs +++ /dev/null @@ -1,93 +0,0 @@ -use std::sync::LazyLock; - -use cucumber::{ - World as _, - codegen::LocalBoxFuture, - step::{Collection, Context}, -}; -use logos_blockchain_tests::cucumber::{ - shared_steps::{register_steps, with_node_steps}, - world::CucumberWorld, -}; - -use super::world::ZoneWorld; - -static ZONE_STEPS: LazyLock> = LazyLock::new(ZoneWorld::collection); - -// SQL scenarios reuse ZoneSDK steps, but their callbacks expect ZoneWorld, -// not SqlWorld. Look up the original callback and its parsed arguments, then -// pass it the embedded ZoneWorld returned by AsMut. -fn forward_zone>(world: &mut W, context: Context) -> LocalBoxFuture<'_, ()> { - Box::pin(async move { - let (step, _, _, context) = ZONE_STEPS - .find(&context.step) - .expect("zone step must be unambiguous") - .expect("zone step was registered"); - - step(world.as_mut(), context).await; - }) -} - -/// Adds both node and `ZoneSDK` steps to a suite, such as the SQL scenarios. -pub fn with_zone_steps + AsMut>( - steps: Collection, -) -> Collection { - register_steps::(with_node_steps(steps), forward_zone) -} - -#[cfg(test)] -mod tests { - use std::{fs, path::Path}; - - use cucumber::{ - World as _, - gherkin::{Feature, GherkinEnv}, - }; - use logos_blockchain_tests::cucumber::world::DeployerKind; - - use super::{ZoneWorld, with_node_steps}; - - #[test] - fn all_zone_scenario_steps_are_registered() { - let steps = with_node_steps(ZoneWorld::collection()); - let features = Path::new(env!("CARGO_MANIFEST_DIR")).join("features"); - - for entry in fs::read_dir(features).expect("feature directory exists") { - let path = entry.expect("feature entry is readable").path(); - let feature = Feature::parse_path(&path, GherkinEnv::default()).expect("valid feature"); - let scenarios = feature - .scenarios - .iter() - .chain(feature.rules.iter().flat_map(|rule| &rule.scenarios)); - - for scenario in scenarios { - for step in &scenario.steps { - assert!( - steps.find(step).expect("step is unambiguous").is_some(), - "{}: {}: undefined step {step}", - path.display(), - scenario.name, - ); - } - } - } - } - - #[tokio::test] - async fn node_steps_update_embedded_node_state() { - let feature = Feature::parse( - "Feature: Shared node setup\n Scenario: Configure deployment\n Given deployer is \"local\"\n", - GherkinEnv::default(), - ).expect("valid feature"); - let steps = with_node_steps(ZoneWorld::collection()); - let (step, _, _, context) = steps - .find(&feature.scenarios[0].steps[0]) - .expect("unambiguous step") - .expect("registered node step"); - let mut world = ZoneWorld::default(); - - step(&mut world, context).await; - - assert_eq!(world.node.lifecycle.deployer, Some(DeployerKind::Local)); - } -} diff --git a/tests/zone-sdk/src/cucumber/steps/mod.rs b/tests/zone-sdk/src/cucumber/steps/mod.rs deleted file mode 100644 index e33e428..0000000 --- a/tests/zone-sdk/src/cucumber/steps/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod zone; - -const TARGET: &str = "cucumber_steps"; diff --git a/tests/zone-sdk/src/cucumber/steps/zone/actions/channel.rs b/tests/zone-sdk/src/cucumber/steps/zone/actions/channel.rs deleted file mode 100644 index 931cc8a..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/actions/channel.rs +++ /dev/null @@ -1,616 +0,0 @@ -use super::{ - AtomicZoneDepositRequest, Duration, Ed25519Key, Inscription, Metadata, PublishDeadline, - PublishResult, SequencerCheckpoint, Step, StepError, StepResult, TxHash, Utxo, - VerifiedChannelKeys, WalletInfo, WalletReservedInputs, ZONE_CHANNEL_DEPOSIT_THRESHOLD, - ZONE_CHANNEL_WITHDRAW_THRESHOLD, ZoneDeposit, ZoneTestError, ZoneWorld, build_zone_deposit, - build_zone_deposit_from_values, current_available_utxos_for_wallet, log_step_error, - make_inscription, publish_atomic_zone_withdraw, submit_atomic_zone_deposit, - submit_zone_channel_split, submit_zone_deposit, submit_zone_withdraw, timeout, zone_step_error, -}; - -pub(in super::super) async fn submit_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - transaction_alias: String, - authorized_aliases: Vec, - posting_timeframe: u32, - posting_timeout: u32, -) -> StepResult { - let handle = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?; - let mut ordered_aliases = vec![sequencer_alias.to_owned()]; - - for alias in authorized_aliases { - if ordered_aliases.iter().any(|existing| existing == &alias) { - continue; - } - - ordered_aliases.push(alias); - } - - let authorized_keys = ordered_aliases - .into_iter() - .map(|alias| { - world - .zone - .sequencer_signing_key(&alias) - .map(Ed25519Key::public_key) - }) - .collect::, _>>()?; - - let mut checkpoint_rx = world - .zone - .checkpoint_receiver(sequencer_alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone sequencer '{sequencer_alias}' has no checkpoint watch"), - })?; - checkpoint_rx.mark_unchanged(); - - let ((result, post_call_checkpoint), _signed_tx) = handle - .channel_config( - VerifiedChannelKeys::new_unchecked(authorized_keys), - posting_timeframe.into(), - posting_timeout.into(), - ZONE_CHANNEL_WITHDRAW_THRESHOLD, - ZONE_CHANNEL_DEPOSIT_THRESHOLD, - ) - .await - .map_err(|error| StepError::LogicalError { - message: format!("Zone channel_config failed: {error}"), - })?; - - record_submitted_config( - world, - sequencer_alias, - transaction_alias, - &result, - post_call_checkpoint, - &mut checkpoint_rx, - ) - .await -} - -/// `threshold` sets both the config and transfer thresholds; -/// `posting_timeframe` is in slots (0 = no rotation). -pub(in super::super) async fn prepare_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - transaction_alias: String, - authorized_aliases: Vec, - threshold: u16, - posting_timeframe: u32, -) -> StepResult { - let client = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?.clone(); - - let authorized_keys = authorized_aliases - .iter() - .map(|alias| world.zone.sequencer_public_key(alias)) - .collect::, _>>()?; - - let prepared = client - .prepare_channel_config( - VerifiedChannelKeys::new_unchecked(authorized_keys.clone()), - posting_timeframe.into(), - 0.into(), - threshold, - threshold, - ) - .await - .map_err(|error| StepError::LogicalError { - message: format!("Zone prepare_channel_config failed: {error}"), - })?; - - // Read the funded config back the way a remote signer would (via - // `proposed_config`) and confirm the SDK encoded exactly the key set and - // thresholds we requested — i.e. inspect the config before it is signed. - let proposed = prepared.proposed_config(); - let proposed_keys = proposed.keys().iter().copied().collect::>(); - if proposed_keys != authorized_keys - || proposed.configuration_threshold().get() != threshold - || proposed.transfer_threshold().get() != threshold - { - return Err(StepError::LogicalError { - message: format!( - "prepared config '{transaction_alias}' does not match the requested keys/thresholds", - ), - }); - } - - world - .zone - .remember_prepared_config(transaction_alias, prepared); - - Ok(()) -} - -/// Signs using only `signer_alias`'s own key. -pub(in super::super) fn sign_prepared_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - signer_alias: &str, - transaction_alias: String, -) -> StepResult { - let signing_key = log_step_error(step, world.zone.sequencer_signing_key(signer_alias))?; - let prepared = log_step_error(step, world.zone.prepared_config(&transaction_alias))?; - - // `prepare_zone_channel_config` already verified `proposed_config()` against - // the requested key set; a real remote signer would run that same check - // here before signing rather than trusting the preparer. - let signature = prepared - .sign_with(signing_key) - .map_err(|error| StepError::LogicalError { - message: format!( - "signing prepared config '{transaction_alias}' as '{signer_alias}' failed: {error}", - ), - })?; - - world - .zone - .add_prepared_config_signature(transaction_alias, signature); - - Ok(()) -} - -pub(in super::super) async fn submit_prepared_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - transaction_alias: String, -) -> StepResult { - let client = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?.clone(); - let prepared = log_step_error(step, world.zone.prepared_config(&transaction_alias))?.clone(); - // Collected in arbitrary signer order, which the proof's binary tree-based map - // puts in index order. - let signatures = world.zone.prepared_config_signatures(&transaction_alias); - - let mut checkpoint_rx = world - .zone - .checkpoint_receiver(sequencer_alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone sequencer '{sequencer_alias}' has no checkpoint watch"), - })?; - checkpoint_rx.mark_unchanged(); - - let (result, post_call_checkpoint) = client - .submit_channel_config(prepared, signatures) - .await - .map_err(|error| StepError::LogicalError { - message: format!("Zone submit_channel_config failed: {error}"), - })?; - - record_submitted_config( - world, - sequencer_alias, - transaction_alias, - &result, - post_call_checkpoint, - &mut checkpoint_rx, - ) - .await -} - -async fn record_submitted_config( - world: &mut ZoneWorld, - sequencer_alias: &str, - transaction_alias: String, - result: &PublishResult, - post_call_checkpoint: SequencerCheckpoint, - checkpoint_rx: &mut tokio::sync::watch::Receiver>, -) -> StepResult { - let tx_hash = result.inscription_id(); - let checkpoint = if post_call_checkpoint - .pending_txs - .iter() - .any(|(hash, _)| *hash == tx_hash) - { - post_call_checkpoint - } else { - timeout( - Duration::from_secs(30), - wait_for_checkpoint_with_tx(checkpoint_rx, tx_hash), - ) - .await - .map_err(|_| StepError::LogicalError { - message: format!( - "timed out waiting for sequencer '{sequencer_alias}' checkpoint to include {tx_hash:?}", - ), - })? - .map_err(|message| StepError::LogicalError { message })? - }; - - world - .zone - .set_latest_checkpoint_for(sequencer_alias, checkpoint.clone()); - world - .zone - .remember_checkpoint(format!("{transaction_alias}_CHECKPOINT"), checkpoint); - world - .node - .remember_submitted_transaction(transaction_alias, tx_hash); - - Ok(()) -} - -pub(in super::super) fn stop_zone_sequencer( - world: &mut ZoneWorld, - sequencer_alias: impl AsRef, -) -> StepResult { - world.zone.stop_sequencer(sequencer_alias.as_ref())?; - - Ok(()) -} - -pub(in super::super) fn save_zone_checkpoint( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: impl AsRef, - checkpoint_alias: String, -) -> StepResult { - let sequencer_alias = sequencer_alias.as_ref(); - let checkpoint = log_step_error(step, world.zone.current_checkpoint_for(sequencer_alias))?; - - world.zone.remember_checkpoint(checkpoint_alias, checkpoint); - - Ok(()) -} - -pub(in super::super) fn remember_published_zone_message( - world: &mut ZoneWorld, - sequencer_alias: &str, - message_alias: String, - payload: Inscription, - result: &PublishResult, -) { - let checkpoint = world.zone.current_checkpoint_for(sequencer_alias).ok(); - world.zone.remember_zone_message( - message_alias, - payload, - Some(result.inscription_id()), - Some(result.tx.inscription().this_msg), - Some(sequencer_alias), - checkpoint, - ); -} - -async fn wait_for_checkpoint_with_tx( - rx: &mut tokio::sync::watch::Receiver>, - tx_hash: TxHash, -) -> Result { - loop { - let snapshot = rx.borrow_and_update().clone(); - if let Some(checkpoint) = snapshot - && checkpoint - .pending_txs - .iter() - .any(|(hash, _)| *hash == tx_hash) - { - return Ok(checkpoint); - } - - rx.changed() - .await - .map_err(|error| format!("checkpoint watch closed: {error}"))?; - } -} - -fn resolve_zone_wallet(world: &ZoneWorld, sequencer_alias: &str) -> Result { - let wallet_name = world.zone.sequencer_default_wallet_name(sequencer_alias)?; - - world.node.resolve_wallet(wallet_name) -} - -fn record_zone_wallet_submission( - world: &ZoneWorld, - wallet_name: &str, - tx_hash: TxHash, - reserved_inputs: Vec, -) -> StepResult { - world.node.with_wallets_mut(|wallets| { - wallets.record_wallet_reservation( - wallet_name.to_owned(), - tx_hash, - WalletReservedInputs::new(reserved_inputs, Vec::new()), - 0, - ); - }) -} - -pub(in super::super) async fn submit_zone_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - channel_alias: String, - amount: u64, - metadata: Metadata, -) -> StepResult { - let node_url = log_step_error(step, world.zone_node_url_for_sequencer(&channel_alias))?; - let wallet = log_step_error(step, resolve_zone_wallet(world, &channel_alias))?; - let public_key = log_step_error(step, wallet.public_key())?; - let available_utxos = log_step_error( - step, - current_available_utxos_for_wallet(&mut world.node, &step.value, &wallet.wallet_name).await, - )?; - let ZoneDeposit { - deposit, - reserved_inputs, - channel_notes, - } = build_zone_deposit( - available_utxos, - world.zone.sequencer_channel_id(&channel_alias)?, - amount, - metadata, - ) - .map_err(|error| zone_step_error(step, &error))?; - - let response = submit_zone_deposit(&node_url, &deposit, public_key) - .await - .map_err(|error| zone_step_error(step, &error))?; - - world - .zone - .remember_deposit_channel_notes(transaction_alias.clone(), channel_notes); - world - .zone - .remember_submitted_deposit(transaction_alias.clone(), deposit, amount); - record_zone_wallet_submission(world, &wallet.wallet_name, response, reserved_inputs)?; - world - .node - .remember_submitted_transaction(transaction_alias, response); - - Ok(()) -} - -/// Consumes one wallet note per listed value. -pub(in super::super) async fn submit_zone_multi_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - channel_alias: String, - input_values: Vec, - metadata: Metadata, -) -> StepResult { - let node_url = log_step_error(step, world.zone_node_url_for_sequencer(&channel_alias))?; - let wallet = log_step_error(step, resolve_zone_wallet(world, &channel_alias))?; - let public_key = log_step_error(step, wallet.public_key())?; - let available_utxos = log_step_error( - step, - current_available_utxos_for_wallet(&mut world.node, &step.value, &wallet.wallet_name).await, - )?; - let amount: u64 = input_values.iter().sum(); - let ZoneDeposit { - deposit, - reserved_inputs, - channel_notes, - } = build_zone_deposit_from_values( - available_utxos, - world.zone.sequencer_channel_id(&channel_alias)?, - &input_values, - metadata, - ) - .map_err(|error| zone_step_error(step, &error))?; - - let response = submit_zone_deposit(&node_url, &deposit, public_key) - .await - .map_err(|error| zone_step_error(step, &error))?; - - world - .zone - .remember_deposit_channel_notes(transaction_alias.clone(), channel_notes); - world - .zone - .remember_submitted_deposit(transaction_alias.clone(), deposit, amount); - record_zone_wallet_submission(world, &wallet.wallet_name, response, reserved_inputs)?; - world - .node - .remember_submitted_transaction(transaction_alias, response); - - Ok(()) -} - -pub(in super::super) async fn submit_zone_channel_split_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - deposit_alias: &str, - dust_count: usize, - transaction_alias: String, -) -> StepResult { - let node_url = log_step_error(step, world.zone_node_url_for_sequencer(sequencer_alias))?; - let wallet = log_step_error(step, resolve_zone_wallet(world, sequencer_alias))?; - let public_key = log_step_error(step, wallet.public_key())?; - let channel_id = world.zone.sequencer_channel_id(sequencer_alias)?; - let signing_key = - log_step_error(step, world.zone.sequencer_signing_key(sequencer_alias))?.clone(); - let input_note = *log_step_error( - step, - world.zone.resolve_deposit_channel_notes(deposit_alias), - )? - .first() - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone deposit '{deposit_alias}' created no channel notes to split"), - })?; - - let tx_hash = submit_zone_channel_split( - &node_url, - channel_id, - &signing_key, - public_key, - input_note, - dust_count, - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - world - .node - .remember_submitted_transaction(transaction_alias, tx_hash); - - Ok(()) -} - -pub(in super::super) async fn submit_atomic_zone_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - transaction_alias: String, - message_alias: String, - amount: u64, - metadata: Metadata, -) -> StepResult { - let node_url = log_step_error(step, world.zone_node_url_for_sequencer(sequencer_alias))?; - let wallet = log_step_error(step, resolve_zone_wallet(world, sequencer_alias))?; - let public_key = log_step_error(step, wallet.public_key())?; - let available_utxos = log_step_error( - step, - current_available_utxos_for_wallet(&mut world.node, &step.value, &wallet.wallet_name).await, - )?; - let sequencer = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?; - let inscription_data = make_inscription(&format!("Mint {amount} to Alice")); - - let submission = submit_atomic_zone_deposit( - &node_url, - sequencer, - AtomicZoneDepositRequest { - channel_id: world.zone.sequencer_channel_id(sequencer_alias)?, - funding_public_key: public_key, - available_utxos, - amount, - metadata, - inscription_data: inscription_data.clone(), - }, - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - world - .zone - .remember_submitted_deposit(transaction_alias.clone(), submission.deposit, amount); - remember_published_zone_message( - world, - sequencer_alias, - message_alias, - inscription_data, - &submission.publish, - ); - record_zone_wallet_submission( - world, - &wallet.wallet_name, - submission.publish.inscription_id(), - submission.reserved_inputs, - )?; - world - .node - .remember_submitted_transaction(transaction_alias, submission.publish.inscription_id()); - - Ok(()) -} - -pub(in super::super) async fn submit_zone_withdraw_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - transaction_alias: String, - message_alias: String, - amount: u64, -) -> StepResult { - let wallet = log_step_error(step, resolve_zone_wallet(world, sequencer_alias))?; - let public_key = log_step_error(step, wallet.public_key())?; - let sequencer = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?; - let inscription_data = make_inscription(&format!("Burn {amount}")); - - let submission = submit_zone_withdraw( - sequencer, - world.zone.sequencer_channel_id(sequencer_alias)?, - public_key, - amount, - inscription_data.clone(), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - world - .zone - .remember_submitted_withdraw(transaction_alias.clone(), submission.withdraw); - remember_published_zone_message( - world, - sequencer_alias, - message_alias, - inscription_data, - &submission.publish, - ); - world - .node - .remember_submitted_transaction(transaction_alias, submission.publish.inscription_id()); - - Ok(()) -} - -/// `withdraw_rows` carries one `(alias, outputs)` per `WithdrawArg`; each -/// withdraw is remembered under its own alias for per-withdraw indexer -/// assertions, and `bundle_alias` under the bundle's tx hash. -pub(in super::super) async fn publish_atomic_zone_withdraw_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - bundle_alias: String, - message_alias: String, - withdraw_rows: Vec<(String, Vec)>, -) -> StepResult { - let wallet = log_step_error(step, resolve_zone_wallet(world, sequencer_alias))?; - let public_key = log_step_error(step, wallet.public_key())?; - let total: u64 = withdraw_rows - .iter() - .flat_map(|(_, outputs)| outputs.iter()) - .sum(); - let inscription_data = make_inscription(&format!("Burn {total}")); - let outputs_per_arg: Vec> = withdraw_rows - .iter() - .map(|(_, outputs)| outputs.clone()) - .collect(); - - let submission = { - let sequencer = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?.clone(); - - publish_atomic_zone_withdraw( - &sequencer, - public_key, - outputs_per_arg, - inscription_data.clone(), - PublishDeadline::from_now(Duration::from_mins(3)), - ) - .await - .map_err(|error| zone_step_error(step, &error))? - }; - - // A bundle carries a single `ChannelWithdrawOp` regardless of how many - // withdraw args were passed; every row alias resolves to that one op. - let [withdraw_op] = submission.withdraws.as_slice() else { - return Err(zone_step_error( - step, - &ZoneTestError::SubmitWithdraw { - message: format!( - "atomic withdraw bundle produced {} withdraw ops, expected exactly 1", - submission.withdraws.len(), - ), - }, - )); - }; - for (alias, _) in &withdraw_rows { - world - .zone - .remember_submitted_withdraw(alias.clone(), withdraw_op.clone()); - } - remember_published_zone_message( - world, - sequencer_alias, - message_alias, - inscription_data, - &submission.publish, - ); - world - .node - .remember_submitted_transaction(bundle_alias, submission.publish.inscription_id()); - - Ok(()) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/actions/cluster.rs b/tests/zone-sdk/src/cucumber/steps/zone/actions/cluster.rs deleted file mode 100644 index 08207dd..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/actions/cluster.rs +++ /dev/null @@ -1,117 +0,0 @@ -use super::{ - Duration, HashMap, NodesToStartUnordered, NonZero, Step, StepResult, TARGET, WalletStartInfo, - ZONE_SECURITY_PARAM, ZoneNodeResourcesRow, ZoneWorld, keygen, set_deployment_config_override, - start_node, start_nodes_order_respecting_dependencies, warn, -}; - -pub(in super::super) fn register_zone_sequencers_with_shared_key( - world: &mut ZoneWorld, - source_alias: &str, - aliases: Vec, -) -> StepResult { - let signing_key = world.zone.sequencer_signing_key(source_alias)?.clone(); - - for alias in aliases { - world.zone.register_sequencer(alias, signing_key.clone()); - } - - Ok(()) -} - -pub(in super::super) async fn start_nodes_with_zone_resources( - world: &mut ZoneWorld, - step: &Step, - rows: Vec, -) -> StepResult { - apply_zone_timing_defaults(world, &step.value)?; - - let nodes = collect_zone_nodes_to_start(&rows); - let nodes = - start_nodes_order_respecting_dependencies(nodes, world.node.nodes_info.keys().cloned()) - .inspect_err(|error| { - warn!(target: TARGET, "Step `{}` error: {error}", step.value); - })?; - - for (node_name, wallet_start_info, mut initial_peers) in nodes { - initial_peers.sort(); - initial_peers.dedup(); - - start_node( - &mut world.node, - &step.value, - &node_name, - &wallet_start_info, - &initial_peers, - false, - &[], - ) - .await?; - } - - register_zone_resources(world, rows) -} - -fn apply_zone_timing_defaults(world: &mut ZoneWorld, step: &str) -> StepResult { - world.node.set_cryptarchia_security_param( - NonZero::new(ZONE_SECURITY_PARAM).expect("zone security parameter is non-zero"), - ); - world.node.set_prolonged_bootstrap_period(Duration::ZERO); - - set_deployment_config_override( - &mut world.node, - step, - "eras.0.time.slot_duration_in_seconds", - "1", - )?; - set_deployment_config_override( - &mut world.node, - step, - "eras.0.cryptarchia.slot_activation_coeff.numerator", - "1", - )?; - set_deployment_config_override( - &mut world.node, - step, - "eras.0.cryptarchia.slot_activation_coeff.denominator", - "2", - ) -} - -fn collect_zone_nodes_to_start(rows: &[ZoneNodeResourcesRow]) -> NodesToStartUnordered { - let mut nodes = HashMap::new(); - - for row in rows { - let entry = nodes - .entry(row.node_name.clone()) - .or_insert_with(|| (Vec::new(), Vec::new())); - - entry.0.push(WalletStartInfo { - wallet_name: row.wallet_name.clone(), - account_index: row.account_index, - }); - - if let Some(peer) = &row.connected_to { - entry.1.push(peer.clone()); - } - } - - nodes -} - -fn register_zone_resources(world: &mut ZoneWorld, rows: Vec) -> StepResult { - for row in rows { - for alias in row.sequencers { - if !world.zone.has_sequencer(&alias) { - world.zone.register_sequencer(alias.clone(), keygen()); - } - - world.zone.attach_sequencer_resources( - &alias, - row.node_name.clone(), - row.wallet_name.clone(), - )?; - } - } - - Ok(()) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/actions/mod.rs b/tests/zone-sdk/src/cucumber/steps/zone/actions/mod.rs deleted file mode 100644 index dccc33f..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/actions/mod.rs +++ /dev/null @@ -1,166 +0,0 @@ -use std::{collections::HashMap, num::NonZero, time::Duration}; - -use cucumber::gherkin::Step; -use futures::future::join_all; -use lb_common_http_client::CommonHttpClient; -use lb_core::mantle::{ - TxHash, Utxo, - gas::GasCost, - ops::channel::{VerifiedChannelKeys, deposit::Metadata, inscribe::Inscription}, -}; -use lb_key_management_system_service::keys::{Ed25519Key, ZkPublicKey}; -use lb_testing_framework::NodeHttpClient; -use lb_zone_sdk::{ - adapter::NodeHttpClient as ZoneNodeHttpClient, - sequencer::{FundingConfig, ZoneSequencer}, -}; -use logos_blockchain_tests::{ - common::{ - mantle_inscription::make_inscription, manual_cluster::wait_for_height, - wallet::WalletReservedInputs, - }, - cucumber::{ - error::{StepError, StepResult}, - steps::nodes::{ - NodesToStartUnordered, WalletStartInfo, set_deployment_config_override, start_node, - start_nodes_order_respecting_dependencies, - }, - wallet::sync::current_available_utxos_for_wallet, - world::WalletInfo, - }, -}; -use tokio::{ - sync::broadcast, - task::JoinHandle, - time::{error::Elapsed, timeout}, -}; -use tracing::warn; - -use super::{ - AtomicZoneDepositRequest, BundleAnnounce, CustomRepublishDeps, DiscardedPayloads, MultiSigBus, - PolicyRuntime, PublishDeadline, ZoneAccountBalances, ZoneDeposit, ZoneTestError, - build_zone_deposit, build_zone_deposit_from_values, ensure_zone_transactions_included, - errors::{log_step_error, zone_step_error}, - keygen, publish_atomic_zone_withdraw, publish_message_with_retry, - runner::{Event, PublishResult, SequencerCheckpoint, SequencerClient, ViewViolation}, - sequencer_config, sequencer_config_with_pending_submit_depth, start_balance_aware_policy, - start_custom_republish_policy, start_deposit_lifecycle_policy, start_deposit_withdraw_policy, - start_multisig_lifecycle_policy, start_republish_lineage_policy, start_sequencer_event_loop, - start_sorted_conflict_policy, - steps::DEFAULT_ZONE_SEQUENCER, - submit_atomic_zone_deposit, submit_zone_channel_split, submit_zone_deposit, - submit_zone_withdraw, - tables::{ConcurrentZoneMessageRow, ZoneNodeResourcesRow, group_zone_messages_by_sequencer}, -}; -use crate::cucumber::{ - steps::TARGET, - world::{ZoneReaderConfig, ZoneWorld}, -}; - -const ZONE_CHANNEL_WITHDRAW_THRESHOLD: u16 = 1; -const ZONE_CHANNEL_DEPOSIT_THRESHOLD: u16 = 1; -const SEQUENCER_READY_TIMEOUT: Duration = Duration::from_mins(2); -const SEQUENCER_READY_POLL_TIMEOUT: Duration = Duration::from_secs(10); -const SEQUENCER_READY_HEIGHT_ADVANCE_TIMEOUT: Duration = Duration::from_secs(30); -const ZONE_SECURITY_PARAM: u32 = 5; -// These high-volume scenarios can move storage prices before all queued -// publishes are included. Keep their test funding comfortably above the -// public 12% default so they exercise sequencing rather than fee starvation. -const ZONE_TEST_PRIORITY_FEE_PERCENT: u64 = 50; - -pub(super) enum DriveMode { - Passive { - republish_orphans: bool, - }, - RepublishLineage { - planned: Vec, - }, - Sorted { - discarded: DiscardedPayloads, - }, - BalanceAware { - initial_balances: ZoneAccountBalances, - planned_payloads: Vec, - }, - CustomRepublish { - deps: Box, - }, - /// Pin then withdraw each observed deposit. - DepositReaction { - withdraw_outputs: Vec, - recipient: ZkPublicKey, - }, - /// Withdraw the observed deposit of `target_amount`. - DepositWithdraw { - target_amount: u64, - withdraw_outputs: Vec, - recipient: ZkPublicKey, - }, - /// Reactively pin then withdraw each observed deposit under multi-sig, - /// gathering signatures on the shared `bus` via the `announce` channel. - MultiSigLifecycle { - withdraw_outputs: Vec, - recipient: ZkPublicKey, - bus: MultiSigBus, - announce: BundleAnnounce, - // Boxed: `Ed25519Key` is large and would bloat every `DriveMode` value. - signing_key: Box, - }, -} - -impl DriveMode { - pub(super) const fn passive() -> Self { - Self::Passive { - republish_orphans: false, - } - } - - pub(super) const fn passive_republish_orphans() -> Self { - Self::Passive { - republish_orphans: true, - } - } -} - -struct PublishedZoneMessage { - alias: String, - payload: Inscription, - result: PublishResult, -} - -struct StartedSequencerRuntime { - task: JoinHandle<()>, - view_violation: ViewViolation, - client: SequencerClient, - events: broadcast::Receiver, - checkpoint_rx: tokio::sync::watch::Receiver>, - ready_rx: tokio::sync::watch::Receiver, - channel_view_rx: tokio::sync::watch::Receiver, - turn_to_write_rx: tokio::sync::watch::Receiver, - discarded_payloads: Option, -} - -mod channel; -mod cluster; -mod publishing; -mod sequencer; - -pub(super) use channel::{ - prepare_zone_channel_config, publish_atomic_zone_withdraw_transaction, - remember_published_zone_message, save_zone_checkpoint, sign_prepared_zone_channel_config, - stop_zone_sequencer, submit_atomic_zone_deposit_transaction, - submit_prepared_zone_channel_config, submit_zone_channel_config, - submit_zone_channel_split_transaction, submit_zone_deposit_transaction, - submit_zone_multi_deposit_transaction, submit_zone_withdraw_transaction, -}; -pub(super) use cluster::{ - register_zone_sequencers_with_shared_key, start_nodes_with_zone_resources, -}; -pub(super) use publishing::{ - initialize_zone_indexer, publish_zone_messages, publish_zone_messages_concurrently, -}; -pub(super) use sequencer::{ - start_deposit_reaction_sequencer, start_deposit_withdraw_sequencer, - start_multisig_lifecycle_sequencer, start_named_sequencer, - start_named_sequencer_with_pending_submit_depth, -}; diff --git a/tests/zone-sdk/src/cucumber/steps/zone/actions/publishing.rs b/tests/zone-sdk/src/cucumber/steps/zone/actions/publishing.rs deleted file mode 100644 index 4d1bc2f..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/actions/publishing.rs +++ /dev/null @@ -1,129 +0,0 @@ -use super::{ - ConcurrentZoneMessageRow, DEFAULT_ZONE_SEQUENCER, Duration, Inscription, PublishDeadline, - PublishedZoneMessage, Step, StepError, StepResult, ZoneReaderConfig, ZoneWorld, - ensure_zone_transactions_included, group_zone_messages_by_sequencer, join_all, log_step_error, - publish_message_with_retry, remember_published_zone_message, zone_step_error, -}; - -pub(in super::super) fn initialize_zone_indexer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: impl AsRef, -) -> StepResult { - let sequencer_alias = sequencer_alias.as_ref(); - let node_url = log_step_error(step, world.zone_node_url_for_sequencer(sequencer_alias))?; - let indexer = ZoneReaderConfig { - channel_id: world.zone.sequencer_channel_id(sequencer_alias)?, - node_url, - }; - - world.zone.set_indexer(indexer); - - Ok(()) -} - -pub(in super::super) async fn publish_zone_messages( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: impl AsRef, - rows: Vec<(String, Inscription)>, -) -> StepResult { - let sequencer_alias = sequencer_alias.as_ref().to_owned(); - let node = log_step_error( - step, - world.zone_node_http_client_for_sequencer(&sequencer_alias), - )?; - - let published = { - let sequencer = - log_step_error(step, world.zone.sequencer_client(&sequencer_alias))?.clone(); - - let publish_deadline = PublishDeadline::from_now(Duration::from_mins(3)); - let mut published = Vec::with_capacity(rows.len()); - - for (alias, payload) in &rows { - let result = publish_message_with_retry(&sequencer, payload, publish_deadline) - .await - .map_err(|error| zone_step_error(step, &error))?; - - ensure_zone_transactions_included( - &node, - &[result.inscription_id()], - Duration::from_mins(3), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - published.push(PublishedZoneMessage { - alias: alias.clone(), - payload: payload.clone(), - result, - }); - } - - published - }; - - for message in published { - remember_published_zone_message( - world, - &sequencer_alias, - message.alias, - message.payload, - &message.result, - ); - } - - Ok(()) -} - -pub(in super::super) async fn publish_zone_messages_concurrently( - world: &mut ZoneWorld, - step: &Step, - rows: Vec, -) -> StepResult { - let grouped = group_zone_messages_by_sequencer(&rows); - let handles = grouped - .keys() - .map(|sequencer_alias| { - log_step_error(step, world.zone.sequencer_client(sequencer_alias)) - .map(|handle| (sequencer_alias.clone(), handle.clone())) - }) - .collect::, _>>()?; - - join_all(handles.into_iter().map(|(sequencer_alias, handle)| { - let payloads = grouped[&sequencer_alias] - .iter() - .map(|message| message.payload.clone()) - .collect::>(); - - async move { - for payload in payloads { - handle.publish(payload).await.map_err(|error| { - StepError::LogicalError { - message: format!( - "Zone concurrent publish failed for sequencer '{sequencer_alias}': {error}" - ), - } - })?; - } - - Ok::<(), StepError>(()) - } - })) - .await - .into_iter() - .collect::, _>>()?; - - for row in rows { - world - .zone - .remember_zone_message(row.message_alias, row.payload, None, None, None, None); - } - - if world.zone.indexer().is_err() { - initialize_zone_indexer(world, step, DEFAULT_ZONE_SEQUENCER)?; - } - - Ok(()) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/actions/sequencer.rs b/tests/zone-sdk/src/cucumber/steps/zone/actions/sequencer.rs deleted file mode 100644 index 3dfc0df..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/actions/sequencer.rs +++ /dev/null @@ -1,307 +0,0 @@ -use super::{ - BundleAnnounce, CommonHttpClient, DiscardedPayloads, DriveMode, Elapsed, FundingConfig, - GasCost, MultiSigBus, NodeHttpClient, PolicyRuntime, SEQUENCER_READY_HEIGHT_ADVANCE_TIMEOUT, - SEQUENCER_READY_POLL_TIMEOUT, SEQUENCER_READY_TIMEOUT, SequencerCheckpoint, - StartedSequencerRuntime, Step, StepError, StepResult, ZONE_TEST_PRIORITY_FEE_PERCENT, - ZoneNodeHttpClient, ZoneSequencer, ZoneWorld, initialize_zone_indexer, log_step_error, - sequencer_config, sequencer_config_with_pending_submit_depth, start_balance_aware_policy, - start_custom_republish_policy, start_deposit_lifecycle_policy, start_deposit_withdraw_policy, - start_multisig_lifecycle_policy, start_republish_lineage_policy, start_sequencer_event_loop, - start_sorted_conflict_policy, timeout, wait_for_height, -}; - -pub(in super::super) async fn start_named_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: impl AsRef, - checkpoint: Option, - mode: DriveMode, -) -> StepResult { - let funding = log_step_error(step, sequencer_funding(world, sequencer_alias.as_ref()))?; - start_named_sequencer_with_config( - world, - step, - sequencer_alias, - checkpoint, - mode, - sequencer_config(funding), - ) - .await -} - -/// Fund sequencer transactions from the node's own funding wallet. -fn sequencer_funding(world: &ZoneWorld, sequencer_alias: &str) -> Result { - let node_name = world.zone.sequencer_node_name(sequencer_alias)?; - let funding_pk = world.node.funding_wallet(node_name)?.public_key()?; - Ok(FundingConfig { - funding_pk, - change_pk: None, - max_tx_fee: GasCost::new(u64::MAX), - priority_fee_percent: ZONE_TEST_PRIORITY_FEE_PERCENT, - }) -} - -pub(in super::super) async fn start_named_sequencer_with_pending_submit_depth( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: impl AsRef, - checkpoint: Option, - mode: DriveMode, - max_pending_publish_depth: usize, -) -> StepResult { - let funding = log_step_error(step, sequencer_funding(world, sequencer_alias.as_ref()))?; - let config = sequencer_config_with_pending_submit_depth(max_pending_publish_depth, funding); - - start_named_sequencer_with_config(world, step, sequencer_alias, checkpoint, mode, config).await -} - -/// Start `sequencer_alias` with the deposit-lifecycle policy and an indexer. -pub(in super::super) async fn start_deposit_reaction_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - withdraw_outputs: Vec, -) -> StepResult { - let recipient = log_step_error(step, sequencer_funding(world, sequencer_alias))?.funding_pk; - start_named_sequencer( - world, - step, - sequencer_alias, - None, - DriveMode::DepositReaction { - withdraw_outputs, - recipient, - }, - ) - .await?; - initialize_zone_indexer(world, step, sequencer_alias) -} - -/// Start `sequencer_alias` with the deposit-withdraw policy and an indexer. -pub(in super::super) async fn start_deposit_withdraw_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - target_amount: u64, - withdraw_outputs: Vec, -) -> StepResult { - let recipient = log_step_error(step, sequencer_funding(world, sequencer_alias))?.funding_pk; - start_named_sequencer( - world, - step, - sequencer_alias, - None, - DriveMode::DepositWithdraw { - target_amount, - withdraw_outputs, - recipient, - }, - ) - .await?; - initialize_zone_indexer(world, step, sequencer_alias) -} - -/// Start `sequencer_alias` running the reactive multi-sig deposit lifecycle -/// policy, sharing the signature `bus` with its peers, plus an indexer. -pub(in super::super) async fn start_multisig_lifecycle_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - withdraw_outputs: Vec, - bus: MultiSigBus, - announce: BundleAnnounce, -) -> StepResult { - let recipient = log_step_error(step, sequencer_funding(world, sequencer_alias))?.funding_pk; - let signing_key = - log_step_error(step, world.zone.sequencer_signing_key(sequencer_alias))?.clone(); - start_named_sequencer( - world, - step, - sequencer_alias, - None, - DriveMode::MultiSigLifecycle { - withdraw_outputs, - recipient, - bus, - announce, - signing_key: Box::new(signing_key), - }, - ) - .await?; - initialize_zone_indexer(world, step, sequencer_alias) -} - -async fn start_named_sequencer_with_config( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: impl AsRef, - checkpoint: Option, - mode: DriveMode, - config: lb_zone_sdk::sequencer::SequencerConfig, -) -> StepResult { - let sequencer_alias = sequencer_alias.as_ref().to_owned(); - let signing_key = - log_step_error(step, world.zone.sequencer_signing_key(&sequencer_alias))?.clone(); - let node_client = log_step_error( - step, - world.zone_node_http_client_for_sequencer(&sequencer_alias), - )?; - let node_url = log_step_error(step, world.zone_node_url_for_sequencer(&sequencer_alias))?; - let sequencer = ZoneSequencer::init_with_config( - world.zone.sequencer_channel_id(&sequencer_alias)?, - signing_key, - ZoneNodeHttpClient::new(CommonHttpClient::new(None), node_url), - config, - checkpoint, - ); - - let runtime = start_sequencer_runtime(sequencer, mode); - let mut ready_rx = runtime.ready_rx.clone(); - - if let Err(error) = - wait_for_sequencer_ready(&sequencer_alias, &node_client, &mut ready_rx).await - { - runtime.task.abort(); - return Err(error); - } - - world.zone.set_sequencer_runtime( - sequencer_alias, - runtime.client, - runtime.task, - runtime.events, - runtime.checkpoint_rx, - runtime.channel_view_rx, - runtime.turn_to_write_rx, - runtime.discarded_payloads, - runtime.view_violation, - ); - - Ok(()) -} - -async fn wait_for_sequencer_ready( - sequencer_alias: &str, - node_client: &NodeHttpClient, - ready_rx: &mut tokio::sync::watch::Receiver, -) -> StepResult { - timeout(SEQUENCER_READY_TIMEOUT, async { - let mut last_height = node_client.consensus_info().await?.cryptarchia_info.height; - - loop { - let poll = timeout(SEQUENCER_READY_POLL_TIMEOUT, async { - loop { - if ready_rx.changed().await.is_err() { - return Err(()); - } - if *ready_rx.borrow() { - return Ok(()); - } - } - }) - .await; - if matches!(poll, Ok(Ok(()))) { - return Ok(()); - } - - let _ = wait_for_height( - node_client, - last_height.saturating_add(1), - SEQUENCER_READY_HEIGHT_ADVANCE_TIMEOUT, - ) - .await; - - last_height = node_client - .consensus_info() - .await? - .cryptarchia_info - .height - .max(last_height); - } - }) - .await - .map_err(|_: Elapsed| StepError::Timeout { - message: format!( - "Sequencer `{sequencer_alias}` did not become ready within {} seconds", - SEQUENCER_READY_TIMEOUT.as_secs() - ), - })? -} - -fn from_policy_runtime( - rt: PolicyRuntime, - discarded_payloads: Option, -) -> StartedSequencerRuntime { - StartedSequencerRuntime { - task: rt.task, - view_violation: rt.view_violation, - client: rt.client, - events: rt.events, - checkpoint_rx: rt.checkpoint_rx, - ready_rx: rt.ready_rx, - channel_view_rx: rt.channel_view_rx, - turn_to_write_rx: rt.turn_to_write_rx, - discarded_payloads, - } -} - -fn start_sequencer_runtime( - sequencer: ZoneSequencer, - mode: DriveMode, -) -> StartedSequencerRuntime { - match mode { - DriveMode::Passive { republish_orphans } => from_policy_runtime( - start_sequencer_event_loop(sequencer, republish_orphans), - None, - ), - DriveMode::RepublishLineage { planned } => { - from_policy_runtime(start_republish_lineage_policy(sequencer, planned), None) - } - DriveMode::Sorted { discarded } => from_policy_runtime( - start_sorted_conflict_policy(sequencer, &discarded), - Some(discarded), - ), - DriveMode::BalanceAware { - initial_balances, - planned_payloads, - } => from_policy_runtime( - start_balance_aware_policy(sequencer, initial_balances, planned_payloads), - None, - ), - DriveMode::CustomRepublish { deps } => { - from_policy_runtime(start_custom_republish_policy(sequencer, *deps), None) - } - DriveMode::DepositReaction { - withdraw_outputs, - recipient, - } => from_policy_runtime( - start_deposit_lifecycle_policy(sequencer, withdraw_outputs, recipient), - None, - ), - DriveMode::DepositWithdraw { - target_amount, - withdraw_outputs, - recipient, - } => from_policy_runtime( - start_deposit_withdraw_policy(sequencer, target_amount, withdraw_outputs, recipient), - None, - ), - DriveMode::MultiSigLifecycle { - withdraw_outputs, - recipient, - bus, - announce, - signing_key, - } => from_policy_runtime( - start_multisig_lifecycle_policy( - sequencer, - withdraw_outputs, - recipient, - bus, - announce, - *signing_key, - ), - None, - ), - } -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/assertions.rs b/tests/zone-sdk/src/cucumber/steps/zone/assertions.rs deleted file mode 100644 index d0172b6..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/assertions.rs +++ /dev/null @@ -1,169 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - time::{Duration, Instant}, -}; - -use lb_core::mantle::ops::channel::inscribe::Inscription; -use logos_blockchain_tests::cucumber::error::{StepError, StepResult}; - -use super::{ - DiscardedPayloads, ZoneTestError, replay_finalized_history, replayed_inscription_payloads, -}; -use crate::cucumber::world::ZoneReaderConfig; - -pub(super) async fn wait_for_indexer_unordered( - reader: &ZoneReaderConfig, - expected: &HashSet, - timeout_duration: Duration, -) -> Result, ZoneTestError> { - let start = Instant::now(); - - loop { - if start.elapsed() > timeout_duration { - return Err(ZoneTestError::IndexerTimeout); - } - - let seen: HashSet = - replayed_inscription_payloads(&replay_finalized_history(reader).await?) - .into_iter() - .filter(|payload| expected.contains(payload)) - .collect(); - - if seen == *expected { - return Ok(seen); - } - - tokio::time::sleep(Duration::from_millis(500)).await; - } -} - -pub(super) async fn scan_indexer_for_payloads( - reader: &ZoneReaderConfig, - expected: &HashSet, -) -> Result, ZoneTestError> { - Ok( - replayed_inscription_payloads(&replay_finalized_history(reader).await?) - .into_iter() - .filter(|payload| expected.contains(payload)) - .collect(), - ) -} - -pub(super) async fn wait_until_sorted_conflict_settles( - reader: &ZoneReaderConfig, - expected: &HashSet, - discarded: &DiscardedPayloads, - total: usize, - timeout_duration: Duration, -) -> Result, ZoneTestError> { - let start = Instant::now(); - - loop { - if start.elapsed() > timeout_duration { - return Err(ZoneTestError::IndexerTimeout); - } - - let mut on_chain: Vec = Vec::new(); - for payload in replayed_inscription_payloads(&replay_finalized_history(reader).await?) { - if expected.contains(&payload) && !on_chain.contains(&payload) { - on_chain.push(payload); - } - } - - let discarded_snapshot = discarded.lock().await.clone(); - let expected_count = total.saturating_sub(discarded_snapshot.len()); - let has_discarded_on_chain = on_chain - .iter() - .any(|payload| discarded_snapshot.contains(payload)); - if expected_count > 0 && on_chain.len() >= expected_count && !has_discarded_on_chain { - return Ok(on_chain); - } - - tokio::time::sleep(Duration::from_millis(500)).await; - } -} - -pub(super) fn assert_sorted_outcome( - on_chain: &[Inscription], - discarded: &HashSet, - total: usize, - expected_by_sequencer: &HashMap>, -) -> StepResult { - let issues = sorted_outcome_issues(on_chain, discarded, total, expected_by_sequencer); - if issues.is_empty() { - return Ok(()); - } - - Err(StepError::LogicalError { - message: issues.join("; "), - }) -} - -fn sorted_outcome_issues( - on_chain: &[Inscription], - discarded: &HashSet, - total: usize, - expected_by_sequencer: &HashMap>, -) -> Vec { - let mut issues = Vec::new(); - let unique: HashSet<&Inscription> = on_chain.iter().collect(); - if unique.len() != on_chain.len() { - issues.push("Duplicate inscriptions detected on chain".to_owned()); - } - - let on_chain_set: HashSet = on_chain.iter().cloned().collect(); - let overlap: Vec = on_chain_set.intersection(discarded).cloned().collect(); - if !overlap.is_empty() { - issues.push(format!( - "Payloads appeared both on-chain and discarded: {:?}", - render_payloads(&overlap) - )); - } - - if on_chain.len() + discarded.len() != total { - issues.push(format!( - "sorted conflict accounting mismatch: on_chain={} discarded={} total={total}", - on_chain.len(), - discarded.len() - )); - } - - for (sequencer_alias, expected_payloads) in expected_by_sequencer { - let surviving = on_chain - .iter() - .filter(|payload| expected_payloads.contains(*payload)) - .cloned() - .collect::>(); - - let mut last_index = None; - for payload in &surviving { - let Some(index) = expected_payloads - .iter() - .position(|expected| expected == payload) - else { - continue; - }; - - if let Some(previous_index) = last_index - && index <= previous_index - { - issues.push(format!( - "Per-sequencer order was not preserved for {sequencer_alias}: {:?}", - render_payloads(&surviving) - )); - break; - } - - last_index = Some(index); - } - } - - issues -} - -fn render_payloads(payloads: &[Inscription]) -> Vec { - payloads - .iter() - .map(|payload| String::from_utf8_lossy(payload).to_string()) - .collect() -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/errors.rs b/tests/zone-sdk/src/cucumber/steps/zone/errors.rs deleted file mode 100644 index ccecc2d..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/errors.rs +++ /dev/null @@ -1,24 +0,0 @@ -use cucumber::gherkin::Step; -use logos_blockchain_tests::cucumber::error::StepError; -use tracing::warn; - -use super::ZoneTestError; -use crate::cucumber::steps::TARGET; - -pub(super) fn zone_step_error(step: &Step, error: &ZoneTestError) -> StepError { - log_zone_error(step, error); - - StepError::LogicalError { - message: error.to_string(), - } -} - -pub(super) fn log_step_error(step: &Step, result: Result) -> Result { - result.inspect_err(|error| { - warn!(target: TARGET, "Step `{}` error: {error}", step.value); - }) -} - -pub(super) fn log_zone_error(step: &Step, error: &ZoneTestError) { - warn!(target: TARGET, "Step `{}` error: {error}", step.value); -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/mod.rs b/tests/zone-sdk/src/cucumber/steps/zone/mod.rs deleted file mode 100644 index b7a0429..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -mod actions; -mod assertions; -mod errors; -mod operations; -pub mod runner; -pub mod steps; -mod tables; - -use operations::{ - AtomicZoneDepositRequest, BundleAnnounce, CustomRepublishDeps, DiscardedPayloads, MultiSigBus, - PolicyRuntime, PublishDeadline, ZoneAccountBalances, ZoneDeposit, ZoneTestError, - balance_update_payload, build_zone_deposit, build_zone_deposit_from_values, - collect_indexed_messages, collect_indexed_messages_exactly_once, ensure_zone_message_included, - ensure_zone_transactions_included, keygen, parse_balance_payload, pin_payload, - publish_atomic_zone_withdraw, publish_message_with_retry, replay_finalized_history, - replayed_inscription_payloads, sequencer_config, sequencer_config_with_pending_submit_depth, - start_balance_aware_policy, start_custom_republish_policy, start_deposit_lifecycle_policy, - start_deposit_withdraw_policy, start_multisig_lifecycle_policy, start_republish_lineage_policy, - start_sequencer_event_loop, start_sorted_conflict_policy, submit_atomic_zone_deposit, - submit_zone_channel_split, submit_zone_deposit, submit_zone_withdraw, - wait_for_channel_transfer_input_count, wait_for_channel_view, wait_for_channel_wallet_counts, - wait_for_channel_wallet_note, wait_for_deposit, wait_for_exact_indexed_payload_count, - wait_for_finalized_deposit_via_sequencer, wait_for_finalized_withdraw_via_sequencer, - wait_for_lib_advance, wait_for_transactions_finalized, wait_for_turn_to_write, - wait_for_withdraw, withdraw_payload, -}; diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/atomic.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/atomic.rs deleted file mode 100644 index 7278674..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/atomic.rs +++ /dev/null @@ -1,182 +0,0 @@ -use lb_core::mantle::{ledger::BoundedInputs, transactions::Ops}; - -use super::*; - -/// Generous fee margin: the mandatory fee varies with input count and change, -/// and an underfunded atomic deposit tx is silently evicted at block assembly. -const ATOMIC_DEPOSIT_FEE_MARGIN: u64 = 10_000; - -pub(super) fn build_atomic_deposit_transfer( - available_utxos: Vec, - funding_public_key: ZkPublicKey, - amount: Value, -) -> Result<(TransferOp, Vec), ZoneTestError> { - let deposit_note = Note::new(amount, funding_public_key); - let funded_transfer = build_wallet_funded_transfer( - available_utxos, - vec![deposit_note], - funding_public_key, - ATOMIC_DEPOSIT_FEE_MARGIN, - ) - .map_err(|error| ZoneTestError::BuildAtomicDeposit { - message: error.to_string(), - })?; - - Ok(funded_transfer.into_parts()) -} - -/// Points the channel deposit at the note created by the funding transfer. -pub(super) fn build_atomic_deposit_op( - channel_id: ChannelId, - metadata: Metadata, - transfer: &TransferOp, -) -> Result { - let deposit_note_id = transfer - .outputs - .utxo_by_index(0, transfer) - .ok_or_else(|| ZoneTestError::BuildAtomicDeposit { - message: "transfer did not produce the deposit note".to_owned(), - })? - .id(); - - Ok(DepositOp { - channel_id, - inputs: BoundedInputs::from(deposit_note_id).into(), - metadata, - }) -} - -/// Self-withdraw of a single note of `amount` back to `funding_public_key`, -/// published with its inscription in one SDK flow. Inputs auto-selected. -pub async fn submit_zone_withdraw( - client: &SequencerClient, - _channel_id: ChannelId, - funding_public_key: ZkPublicKey, - amount: Value, - inscription_data: Inscription, -) -> Result { - let (result, _cp) = client - .publish_atomic_withdraw( - inscription_data, - vec![WithdrawArg { - outputs: Outputs::new([Note::new(amount, funding_public_key)]), - }], - WithdrawInputs::Auto, - ) - .await - .map_err(|error| ZoneTestError::SubmitWithdraw { - message: error.to_string(), - })?; - - let PendingTx::AtomicWithdraw(info) = result.tx else { - return Err(ZoneTestError::SubmitWithdraw { - message: "publish_atomic_withdraw returned a non-AtomicWithdraw publish result" - .to_owned(), - }); - }; - let withdraw = info - .withdraws - .first() - .ok_or_else(|| ZoneTestError::SubmitWithdraw { - message: "atomic withdraw bundle had no withdraw ops".to_owned(), - })? - .op - .clone(); - - Ok(ZoneWithdrawSubmission { - withdraw, - publish: PublishResult { - tx: PendingTx::AtomicWithdraw(info), - }, - }) -} - -/// Carries every withdraw op the SDK produced (one per `WithdrawArg`, in -/// submission order) so a multi-withdraw scenario can match each by its -/// outputs. -pub struct ZoneAtomicWithdrawSubmission { - pub withdraws: Vec, - pub publish: PublishResult, -} - -/// `outputs_per_arg` carries one entry per `WithdrawArg`; each inner `Vec` -/// becomes that arg's `Outputs` (one `Note::new(amount, funding_pk)` per listed -/// amount), exercising the SDK API at full width. -pub async fn publish_atomic_zone_withdraw( - client: &SequencerClient, - funding_public_key: ZkPublicKey, - outputs_per_arg: Vec>, - inscription_data: Inscription, - _deadline: PublishDeadline, -) -> Result { - if outputs_per_arg.is_empty() { - return Err(ZoneTestError::SubmitWithdraw { - message: "publish_atomic_zone_withdraw requires at least one withdraw arg".to_owned(), - }); - } - let withdraw_args: Vec = outputs_per_arg - .iter() - .map(|amounts| { - Ok::(WithdrawArg { - outputs: Outputs::try_new( - amounts - .iter() - .map(|amount| Note::new(*amount, funding_public_key)) - .collect::>(), - )?, - }) - }) - .collect::, ZoneTestError>>()?; - - let (result, _cp) = client - .publish_atomic_withdraw(inscription_data, withdraw_args, WithdrawInputs::Auto) - .await - .map_err(|error| ZoneTestError::SubmitWithdraw { - message: error.to_string(), - })?; - - let PendingTx::AtomicWithdraw(info) = result.tx else { - return Err(ZoneTestError::SubmitWithdraw { - message: "publish_atomic_withdraw returned a non-AtomicWithdraw publish result" - .to_owned(), - }); - }; - if info.withdraws.is_empty() { - return Err(ZoneTestError::SubmitWithdraw { - message: "atomic withdraw bundle had no withdraw ops".to_owned(), - }); - } - Ok(ZoneAtomicWithdrawSubmission { - withdraws: info.withdraws.iter().map(|w| w.op.clone()).collect(), - publish: PublishResult { - tx: PendingTx::AtomicWithdraw(info), - }, - }) -} - -pub(super) async fn sign_tx_zk( - node_url: &Url, - ops: &Ops, - public_keys: Vec, -) -> Result { - let request_url = - node_url - .join("wallet/sign/zk") - .map_err(|error| ZoneTestError::SignTransaction { - message: error.to_string(), - })?; - let response: WalletSignTxZkResponseBody = CommonHttpClient::new(None) - .post( - request_url, - &WalletSignTxZkRequestBody { - tx_hash: ops.hash(), - pks: ZkPublicKeys::try_from(public_keys)?, - }, - ) - .await - .map_err(|error| ZoneTestError::SignTransaction { - message: error.to_string(), - })?; - - Ok(response.sig) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/custom_policy.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/custom_policy.rs deleted file mode 100644 index 2eb94b3..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/custom_policy.rs +++ /dev/null @@ -1,160 +0,0 @@ -use super::{ - ChannelId, ChannelUpdateTx, Ed25519Key, Event, HashSet, Inscription, MsgId, NodeHttpClient, - PolicyRuntime, SequencerChannelView, VecDeque, ZkPublicKey, ZoneNodeHttpClient, ZoneSequencer, - build_funded_custom_tx, channel_inscriptions, finalized_inscriptions, runner, - to_policy_runtime, warn, -}; - -pub struct CustomRepublishDeps { - pub node_client: NodeHttpClient, - pub channel_id: ChannelId, - pub signing_key: Ed25519Key, - pub funding_pk: ZkPublicKey, - pub batches: VecDeque>, -} - -pub fn start_custom_republish_policy( - sequencer: ZoneSequencer, - deps: CustomRepublishDeps, -) -> PolicyRuntime { - let view_rx = sequencer.subscribe_channel_view(); - let policy = CustomRepublishPolicy { - deps, - view_rx, - finalized: HashSet::new(), - chain_tip: None, - ready: false, - }; - to_policy_runtime(runner::spawn(sequencer, policy)) -} - -/// [`OrphanRepublishPolicy`] for the custom-tx flow: orphans that are -/// neither in the view nor finalized are rebuilt and re-submitted. -struct CustomRepublishPolicy { - deps: CustomRepublishDeps, - view_rx: tokio::sync::watch::Receiver, - finalized: HashSet, - /// Where our own submitted chain ends; reset on orphans so rebuilds - /// chain from the channel tip instead. - chain_tip: Option, - /// No submissions until ready — a fail-fast submit would leak its - /// funding reservation. - ready: bool, -} - -impl CustomRepublishPolicy { - async fn submit( - &mut self, - sequencer: &mut ZoneSequencer, - payloads: Vec, - ) -> bool - where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, - { - let parent = self - .chain_tip - .unwrap_or_else(|| self.view_rx.borrow().tip_message); - let built = build_funded_custom_tx( - &self.deps.node_client, - self.deps.channel_id, - &self.deps.signing_key, - self.deps.funding_pk, - &payloads, - parent, - ) - .await; - let (signed_tx, msg_id) = match built { - Ok(built) => built, - Err(error) => { - warn!(%error, "Failed to build custom zone tx"); - return false; - } - }; - match sequencer.handle().submit_signed_tx(signed_tx, msg_id) { - Ok((_result, _checkpoint)) => { - self.chain_tip = Some(msg_id); - true - } - Err(error) => { - warn!(%error, "Failed to submit custom zone tx"); - false - } - } - } - - fn entry_payloads(&self, entry: &ChannelUpdateTx) -> Vec { - match entry { - ChannelUpdateTx::Custom(tx) => channel_inscriptions(tx, self.deps.channel_id) - .into_iter() - .map(|info| info.payload) - .collect(), - typed => typed - .inscription() - .map(|info| info.payload.clone()) - .into_iter() - .collect(), - } - } -} - -impl runner::Policy for CustomRepublishPolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - let (channel_update, finalized) = match event { - Event::Ready => { - self.ready = true; - (None, None) - } - Event::BlocksProcessed { - channel_update, - finalized, - .. - } => (Some(channel_update), Some(finalized)), - Event::TurnNotification { .. } => return, - }; - - if let Some(finalized) = finalized { - self.finalized - .extend(finalized_inscriptions(finalized).map(|info| info.payload.clone())); - } - - if let Some(channel_update) = channel_update { - let orphaned: HashSet = channel_update - .orphaned() - .iter() - .flat_map(|entry| self.entry_payloads(entry)) - .collect(); - let view: HashSet = channel_update - .canonical_chain() - .into_iter() - .flatten() - .flat_map(|entry| self.entry_payloads(entry)) - .collect(); - - let republish: Vec = orphaned - .into_iter() - .filter(|payload| !view.contains(payload) && !self.finalized.contains(payload)) - .collect(); - if self.ready && !republish.is_empty() { - self.chain_tip = None; - if !self.submit(sequencer, republish.clone()).await { - self.deps.batches.push_back(republish); - } - } - } - - // One attempt per batch per event: a failed submission stops the - // drain and is retried on the next event. - while self.ready { - let Some(batch) = self.deps.batches.pop_front() else { - break; - }; - if !self.submit(sequencer, batch.clone()).await { - self.deps.batches.push_front(batch); - break; - } - } - } -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/deposit_policy.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/deposit_policy.rs deleted file mode 100644 index 00abc3a..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/deposit_policy.rs +++ /dev/null @@ -1,655 +0,0 @@ -use std::sync::{Arc, Mutex}; - -use lb_key_management_system_service::keys::Ed25519Key; -use tokio::sync::broadcast; - -use super::{ - ChannelUpdate, ChannelUpdateTx, Event, FinalizedTx, Hash, HashMap, HashSet, IndexedSignatures, - Inscription, InscriptionId, Inscriptions as _, Note, NoteId, Outputs, PolicyRuntime, - PreparedAtomicBundle, WithdrawArg, WithdrawInputs, ZkPublicKey, ZoneNodeHttpClient, - ZoneSequencer, contributed, finalized_inscriptions, make_inscription, runner, - to_policy_runtime, warn, -}; - -/// Reactively drive the full deposit lifecycle (pin, then withdraw the -/// re-created note), no wait for finalization. See [`DepositLifecyclePolicy`]. -pub fn start_deposit_lifecycle_policy( - sequencer: ZoneSequencer, - withdraw_outputs: Vec, - recipient: ZkPublicKey, -) -> PolicyRuntime { - let policy = DepositLifecyclePolicy { - withdraw_outputs, - recipient, - deposits: HashMap::new(), - finalized: HashSet::new(), - }; - to_policy_runtime(runner::spawn(sequencer, policy)) -} - -struct DepositLifecycleState { - notes: Vec, - pinned: bool, - withdrawn: bool, -} - -/// Reconciles each observed deposit against branch state (fork- and -/// multi-sequencer-correct), matching phases by the deposit's `op_id`: -/// pin it, then withdraw the re-created note. -struct DepositLifecyclePolicy { - withdraw_outputs: Vec, - recipient: ZkPublicKey, - deposits: HashMap, - /// Finalized payloads: permanently on chain. - finalized: HashSet, -} - -pub fn pin_payload(op_id: &Hash) -> Inscription { - make_inscription(&format!("pin deposit {op_id:?}")) -} - -pub fn withdraw_payload(op_id: &Hash) -> Inscription { - make_inscription(&format!("withdraw deposit {op_id:?}")) -} - -async fn publish_deposit_inscription( - sequencer: &mut ZoneSequencer, - inscription: Inscription, - notes: Vec, -) -> Option -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - match sequencer - .handle() - .publish_pin_deposit(inscription, notes) - .await - { - Ok((result, _)) => Some(result.inscription_id()), - Err(error) => { - warn!(%error, "deposit-pin inscription failed"); - None - } - } -} - -async fn publish_deposit_withdraw( - sequencer: &mut ZoneSequencer, - inscription: Inscription, - withdraw_outputs: &[u64], - recipient: ZkPublicKey, -) -> Option -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - let outputs: Vec = withdraw_outputs - .iter() - .map(|&value| Note::new(value, recipient)) - .collect(); - let outputs = Outputs::try_new(outputs).ok()?; - match sequencer - .handle() - .publish_atomic_withdraw( - inscription, - vec![WithdrawArg { outputs }], - WithdrawInputs::Auto, - ) - .await - { - Ok((result, _)) => Some(result.inscription_id()), - Err(error) => { - warn!(%error, "deposit-withdraw failed"); - None - } - } -} - -impl runner::Policy for DepositLifecyclePolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - let Event::BlocksProcessed { - channel_update, - deposits: observed, - finalized: finalized_txs, - .. - } = event - else { - return; - }; - - let Self { - withdraw_outputs, - recipient, - deposits, - finalized, - } = self; - - for deposit in observed { - deposits - .entry(deposit.op_id) - .or_insert_with(|| DepositLifecycleState { - notes: deposit.notes.iter().map(|note| note.note_id).collect(), - pinned: false, - withdrawn: false, - }); - } - finalized.extend(finalized_inscriptions(finalized_txs).map(|info| info.payload.clone())); - // An extension only adds steps; a conflict recomputes them from the view. - let rebuild = matches!(channel_update, ChannelUpdate::Conflict { .. }); - let payloads: HashSet<&Inscription> = contributed(channel_update) - .inscriptions() - .map(|i| &i.payload) - .collect(); - for (op_id, state) in deposits.iter_mut() { - let present = |payload: &Inscription, was: bool| { - finalized.contains(payload) || payloads.contains(payload) || (!rebuild && was) - }; - state.pinned = present(&pin_payload(op_id), state.pinned); - state.withdrawn = present(&withdraw_payload(op_id), state.withdrawn); - } - - let wallet: HashSet = { - let view = sequencer.channel_wallet(); - view.finalized - .iter() - .chain(view.unfinalized.iter()) - .map(|note| note.note_id) - .collect() - }; - - for (op_id, state) in deposits.iter_mut() { - if state.notes.is_empty() { - continue; - } - let deposit_on_branch = state.notes.iter().all(|id| wallet.contains(id)); - if !state.pinned && deposit_on_branch { - let inscription = pin_payload(op_id); - if publish_deposit_inscription(sequencer, inscription, state.notes.clone()) - .await - .is_some() - { - state.pinned = true; - } - } else if !state.withdrawn && state.pinned && !deposit_on_branch { - let inscription = withdraw_payload(op_id); - if publish_deposit_withdraw(sequencer, inscription, withdraw_outputs, *recipient) - .await - .is_some() - { - state.withdrawn = true; - } - } - } - } -} - -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] -pub enum MultiSigPhase { - Pin, - Withdraw, -} - -/// A bundle's identity: its own signing payload. Distinct per proposer, since -/// each bundle carries that proposer's own inscription. -type BundleId = Vec; - -pub struct MultiSigRound { - prepared: Arc, - signatures: IndexedSignatures, - op_id: Hash, -} - -/// Signatures gathered per bundle; the proposer reads it back to submit. -pub type MultiSigBus = Arc>>; - -/// Fanout channel proposers announce bundles on and signers read — the test's -/// stand-in for gossip, so signing reacts to announcements, not block events. -pub type BundleAnnounce = broadcast::Sender>; - -/// Multi-sig counterpart of [`start_deposit_lifecycle_policy`], with no turn -/// logic: each sequencer proposes its own bundles, everyone signs every bundle, -/// each submits its own. The SDK holds a submission until that sequencer's -/// turn, so one lands and note-consumption kills the losers. One task per -/// sequencer, `select!`ing the SDK event stream (chain) against the -/// announcement bus (signing) — the shape a real zone's sequencer has. -pub fn start_multisig_lifecycle_policy( - mut sequencer: ZoneSequencer, - withdraw_outputs: Vec, - recipient: ZkPublicKey, - bus: MultiSigBus, - announce: BundleAnnounce, - signing_key: Ed25519Key, -) -> PolicyRuntime { - // Subscriptions taken before the sequencer moves into the task (what - // `runner::spawn` does; bypassed here for the extra `select!` arm). - let checkpoint_rx = sequencer.subscribe_checkpoint(); - let ready_rx = sequencer.subscribe_ready(); - let channel_view_rx = sequencer.subscribe_channel_view(); - let turn_to_write_rx = sequencer.subscribe_turn_to_write(); - let event_rx = sequencer.subscribe_events(); - let client = sequencer.client(); - let view_violation = runner::ViewViolation::default(); - - let mut announcements = announce.subscribe(); - let mut policy = MultiSigLifecyclePolicy { - withdraw_outputs, - recipient, - bus, - announce, - signing_key, - signed: HashSet::new(), - deposits: HashMap::new(), - finalized: HashSet::new(), - mine: HashMap::new(), - }; - let mut view = runner::ViewChecker::new(Arc::clone(&view_violation)); - - let task = tokio::spawn(async move { - loop { - tokio::select! { - event = sequencer.next_event() => { - view.observe(&event); - policy.on_event(&mut sequencer, &event).await; - } - announced = announcements.recv() => match announced { - Ok(bundle) => policy.sign_announced(&bundle), - Err(broadcast::error::RecvError::Lagged(_)) => {} - Err(broadcast::error::RecvError::Closed) => break, - }, - } - } - }); - - to_policy_runtime(runner::Runtime { - task, - view_violation, - client, - event_rx, - checkpoint_rx, - ready_rx, - channel_view_rx, - turn_to_write_rx, - }) -} - -struct MultiSigLifecyclePolicy { - withdraw_outputs: Vec, - recipient: ZkPublicKey, - bus: MultiSigBus, - announce: BundleAnnounce, - signing_key: Ed25519Key, - signed: HashSet, - deposits: HashMap, - /// Finalized payloads: permanently on chain. - finalized: HashSet, - /// The bundle we ourselves proposed per work item — the one we submit. - mine: HashMap<(Hash, MultiSigPhase), BundleId>, -} - -async fn prepare_pin( - sequencer: &mut ZoneSequencer, - op_id: &Hash, - notes: &[NoteId], -) -> Option -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - match sequencer - .handle() - .prepare_pin_deposit(pin_payload(op_id), notes.to_vec()) - .await - { - Ok(prepared) => Some(prepared), - Err(error) => { - warn!(%error, "multi-sig pin prepare failed"); - None - } - } -} - -async fn prepare_withdraw( - sequencer: &mut ZoneSequencer, - op_id: &Hash, - withdraw_outputs: &[u64], - recipient: ZkPublicKey, -) -> Option -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - let outputs: Vec = withdraw_outputs - .iter() - .map(|&value| Note::new(value, recipient)) - .collect(); - let outputs = Outputs::try_new(outputs).ok()?; - match sequencer - .handle() - .prepare_atomic_withdraw( - withdraw_payload(op_id), - vec![WithdrawArg { outputs }], - WithdrawInputs::Auto, - ) - .await - { - Ok(prepared) => Some(prepared), - Err(error) => { - warn!(%error, "multi-sig withdraw prepare failed"); - None - } - } -} - -/// Record our prepared bundle on the bus, mark it ours, and announce it. -fn open_round( - bus: &MultiSigBus, - announce: &BundleAnnounce, - mine: &mut HashMap<(Hash, MultiSigPhase), BundleId>, - work: (Hash, MultiSigPhase), - prepared: PreparedAtomicBundle, -) { - let id = prepared.sign_payload.clone(); - let prepared = Arc::new(prepared); - bus.lock().unwrap().insert( - id.clone(), - MultiSigRound { - prepared: Arc::clone(&prepared), - signatures: IndexedSignatures::empty(), - op_id: work.0, - }, - ); - mine.insert(work, id); - // Insert before announcing so every signer sees the round. Best-effort: - // errors only once all signer tasks have exited (teardown). - drop(announce.send(prepared)); -} - -/// Forget our bundle for `work` so the next event prepares a fresh one. -fn close_round( - bus: &MultiSigBus, - mine: &mut HashMap<(Hash, MultiSigPhase), BundleId>, - work: &(Hash, MultiSigPhase), -) { - if let Some(id) = mine.remove(work) { - bus.lock().unwrap().remove(&id); - } -} - -/// Submit the bundle once it has a threshold of signatures: `Ok(true)` when -/// submitted, `Ok(false)` while signatures are still being gathered. No turn -/// check: the SDK holds the posted tx until our own write turn. -fn submit_ready( - sequencer: &mut ZoneSequencer, - bus: &MultiSigBus, - id: &BundleId, -) -> Result -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - let ready = { - let bus = bus.lock().unwrap(); - bus.get(id) - .filter(|round| round.signatures.len() >= usize::from(round.prepared.signing_threshold)) - .map(|round| { - let threshold = usize::from(round.prepared.signing_threshold); - let signatures = IndexedSignatures::try_from_iter( - round - .signatures - .iter() - .take(threshold) - .map(|(index, signature)| (*index, *signature)), - ) - .expect("a subset of a bounded map fits"); - (round.prepared.as_ref().clone(), signatures) - }) - }; - let Some((prepared, signatures)) = ready else { - return Ok(false); - }; - sequencer - .handle() - .submit_atomic_bundle(prepared, signatures) - .map(|_| true) -} - -impl MultiSigLifecyclePolicy { - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) - where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, - { - let Event::BlocksProcessed { - channel_update, - deposits: observed, - finalized: finalized_txs, - .. - } = event - else { - return; - }; - let Self { - withdraw_outputs, - recipient, - bus, - announce, - deposits, - finalized, - mine, - .. - } = self; - - for deposit in observed { - deposits - .entry(deposit.op_id) - .or_insert_with(|| DepositLifecycleState { - notes: deposit.notes.iter().map(|note| note.note_id).collect(), - pinned: false, - withdrawn: false, - }); - } - finalized.extend(finalized_inscriptions(finalized_txs).map(|info| info.payload.clone())); - // An extension only adds steps; a conflict recomputes them from the view. - let rebuild = matches!(channel_update, ChannelUpdate::Conflict { .. }); - let payloads: HashSet<&Inscription> = contributed(channel_update) - .inscriptions() - .map(|i| &i.payload) - .collect(); - for (op_id, state) in deposits.iter_mut() { - let present = |payload: &Inscription, was: bool| { - finalized.contains(payload) || payloads.contains(payload) || (!rebuild && was) - }; - state.pinned = present(&pin_payload(op_id), state.pinned); - state.withdrawn = present(&withdraw_payload(op_id), state.withdrawn); - } - - let wallet: HashSet = { - let view = sequencer.channel_wallet(); - view.finalized - .iter() - .chain(view.unfinalized.iter()) - .map(|note| note.note_id) - .collect() - }; - // Drive off canonical state: pin while the note is on-branch and - // unpinned, then withdraw once pinned and the note is consumed. "Drive" = - // prepare our own bundle if we have none, else submit it and set the flag - // optimistically; the reconcile above clears it on a conflict so we drive - // again. A refused submit drops the bundle so the next event re-prepares. - for (op_id, state) in deposits.iter_mut() { - if state.notes.is_empty() { - continue; - } - let on_branch = state.notes.iter().all(|id| wallet.contains(id)); - let phase = if !state.pinned && on_branch { - MultiSigPhase::Pin - } else if !state.withdrawn && state.pinned && !on_branch { - MultiSigPhase::Withdraw - } else { - continue; - }; - let work = (*op_id, phase); - let Some(id) = mine.get(&work) else { - let prepared = match phase { - MultiSigPhase::Pin => prepare_pin(sequencer, op_id, &state.notes).await, - MultiSigPhase::Withdraw => { - prepare_withdraw(sequencer, op_id, withdraw_outputs, *recipient).await - } - }; - if let Some(prepared) = prepared { - open_round(bus, announce, mine, work, prepared); - } - continue; - }; - match submit_ready(sequencer, bus, id) { - Ok(true) => match phase { - MultiSigPhase::Pin => state.pinned = true, - MultiSigPhase::Withdraw => state.withdrawn = true, - }, - Ok(false) => {} - Err(error) => { - warn!(%error, "multi-sig bundle submit failed"); - close_round(bus, mine, &work); - } - } - } - - // Drop a deposit's bundles once it is fully withdrawn. - let withdrawn: HashSet = deposits - .iter() - .filter(|(_, state)| state.withdrawn) - .map(|(op_id, _)| *op_id) - .collect(); - if !withdrawn.is_empty() { - bus.lock() - .unwrap() - .retain(|_, round| !withdrawn.contains(&round.op_id)); - mine.retain(|(op_id, _), _| !withdrawn.contains(op_id)); - } - } - - /// Sign an announced bundle once with our key and record it on the bus. - fn sign_announced(&mut self, prepared: &PreparedAtomicBundle) { - let id = prepared.sign_payload.clone(); - if !self.signed.insert(id.clone()) { - return; - } - match prepared.sign_with(&self.signing_key) { - Ok(signature) => { - if let Some(round) = self.bus.lock().unwrap().get_mut(&id) - && let Err(error) = round - .signatures - .try_insert(signature.channel_key_index, signature.signature) - { - warn!(?error, "multi-sig signer: signature not recorded"); - } - } - Err(error) => warn!(%error, "multi-sig signer: cannot sign bundle"), - } - } -} - -/// Single-phase sibling of [`start_deposit_lifecycle_policy`]: reactively -/// withdraw the deposit of `target_amount`, `Auto` sweeping other notes. -pub fn start_deposit_withdraw_policy( - sequencer: ZoneSequencer, - target_amount: u64, - withdraw_outputs: Vec, - recipient: ZkPublicKey, -) -> PolicyRuntime { - let policy = DepositWithdrawPolicy { - target_amount, - withdraw_outputs, - recipient, - deposits: HashMap::new(), - }; - to_policy_runtime(runner::spawn(sequencer, policy)) -} - -struct DepositWithdrawState { - notes: Vec, - withdraw_tx: Option, -} - -/// Reactively withdraw the deposit of `target_amount` (no pin step), -/// reconciled against branch state; the withdraw consumes the deposit note -/// directly, so a foreign withdraw removes it and we back off. -struct DepositWithdrawPolicy { - target_amount: u64, - withdraw_outputs: Vec, - recipient: ZkPublicKey, - deposits: HashMap, -} - -/// On a conflict, forget a withdraw that is neither in the view nor -/// finalized: it was shed. Nothing leaves on an extension. -fn retain_live_withdraws( - deposits: &mut HashMap, - channel_update: &ChannelUpdate, - finalized: &[FinalizedTx], -) { - let Some(chain) = channel_update.canonical_chain() else { - return; - }; - let live: HashSet = chain - .map(ChannelUpdateTx::tx_hash) - .chain(finalized.iter().map(|tx| tx.tx_hash)) - .collect(); - for state in deposits.values_mut() { - state.withdraw_tx = state.withdraw_tx.filter(|hash| live.contains(hash)); - } -} - -impl runner::Policy for DepositWithdrawPolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - let Event::BlocksProcessed { - channel_update, - deposits: observed, - finalized, - .. - } = event - else { - return; - }; - - let Self { - target_amount, - withdraw_outputs, - recipient, - deposits, - } = self; - - for deposit in observed { - if deposit.amount == *target_amount { - deposits - .entry(deposit.op_id) - .or_insert_with(|| DepositWithdrawState { - notes: deposit.notes.iter().map(|note| note.note_id).collect(), - withdraw_tx: None, - }); - } - } - retain_live_withdraws(deposits, channel_update, finalized); - - let wallet: HashSet = { - let view = sequencer.channel_wallet(); - view.finalized - .iter() - .chain(view.unfinalized.iter()) - .map(|note| note.note_id) - .collect() - }; - - for (op_id, state) in deposits.iter_mut() { - let deposit_on_branch = - !state.notes.is_empty() && state.notes.iter().all(|id| wallet.contains(id)); - if state.withdraw_tx.is_none() && deposit_on_branch { - let inscription = make_inscription(&format!("withdraw deposit {op_id:?}")); - state.withdraw_tx = - publish_deposit_withdraw(sequencer, inscription, withdraw_outputs, *recipient) - .await; - } - } - } -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/mod.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/mod.rs deleted file mode 100644 index 9a28d1e..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/mod.rs +++ /dev/null @@ -1,302 +0,0 @@ -//! Zone SDK test helpers shared by Cucumber steps. -//! -//! The helpers in this module keep the feature steps focused on scenario -//! intent: start a zone-backed node, run sequencers, publish messages, observe -//! the indexer, and submit the channel operations that the zone layer relies -//! on. - -use std::{ - collections::{BTreeSet, HashMap, HashSet, VecDeque}, - sync::{Arc, LazyLock}, - time::{Duration, Instant}, -}; - -use lb_common_http_client::{ApiBlock, CommonHttpClient, Slot}; -use lb_core::{ - crypto::Hash, - mantle::{ - Note, Op, OpProof, Utxo, Value, - gas::GasCost, - ledger::{Inputs, NoteId, Outputs, OutputsError}, - ops::{ - OpId as _, - channel::{ - ChannelId, MsgId, - channel_transfer::ChannelTransferOp, - deposit::{DepositOp, Metadata}, - inscribe::{Inscription, InscriptionOp}, - withdraw::ChannelWithdrawOp, - }, - transfer::TransferOp, - }, - traits::Hashable as _, - transactions::{builder::MantleTxBuilder, states::Unverified}, - }, - proofs::channel_multi_sig_proof::{ChannelMultiSigProof, IndexedSignatures}, -}; -use lb_http_api_common::bodies::{ - channel::{ChannelDepositRequestBody, ChannelDepositResponseBody}, - wallet::{ - fund::WalletFundRequestBody, - sign::{WalletSignTxZkRequestBody, WalletSignTxZkResponseBody}, - }, -}; -use lb_key_management_system_service::keys::{Ed25519Key, ZkPublicKey, ZkPublicKeys, ZkSignature}; -use lb_testing_framework::NodeHttpClient; -use lb_zone_sdk::{ - adapter::NodeHttpClient as ZoneNodeHttpClient, - sequencer::{ZoneSequencer, channel_inscriptions}, -}; -use rand::{Rng as _, thread_rng}; -use reqwest::Url; -use tokio::{ - task::JoinHandle, - time::{sleep, timeout}, -}; -use tracing::warn; - -use super::runner::{ - self, ChannelUpdate, ChannelUpdateTx, Event, FinalizedOp, FinalizedTx, FundingConfig, - InscriptionId, InscriptionInfo, PendingTx, PreparedAtomicBundle, PublishResult, - SequencerChannelView, SequencerCheckpoint, SequencerClient, SequencerConfig, TurnNotification, - WithdrawArg, WithdrawInputs, -}; - -/// Inscriptions in the just-finalized txs — the permanent, settled part of the -/// channel. Once a payload finalizes it's on chain for good, so a policy pins -/// these and never re-homes a finalized payload when it later drops off a -/// non-canonical branch. -fn finalized_inscriptions(finalized: &[FinalizedTx]) -> impl Iterator { - finalized - .iter() - .flat_map(|tx| tx.ops.iter()) - .filter_map(|op| match op { - FinalizedOp::Inscription(info) => Some(info), - FinalizedOp::Deposit(_) - | FinalizedOp::Withdraw(_) - | FinalizedOp::Config(_) - | FinalizedOp::ChannelTransfer(_) => None, - }) -} - -/// The inscriptions carried by channel-update entries, in order — the -/// non-finalized counterpart of [`finalized_inscriptions`]. -trait Inscriptions<'a> { - fn inscriptions(self) -> impl Iterator; -} - -impl<'a, I: IntoIterator> Inscriptions<'a> for I { - fn inscriptions(self) -> impl Iterator { - self.into_iter().filter_map(ChannelUpdateTx::inscription) - } -} - -/// The entries an update contributes to a consumer's non-finalized view: a -/// conflict's whole `canonical_chain()` (the consumer clears first), an -/// extension's `adopted` (the consumer keeps what it holds). -fn contributed(update: &ChannelUpdate) -> impl Iterator { - let prefix = match update { - ChannelUpdate::Conflict { common_prefix, .. } => common_prefix.as_slice(), - ChannelUpdate::Extension { .. } => &[], - }; - prefix.iter().chain(update.adopted()) -} -use logos_blockchain_tests::common::{ - chain::{scan_chain_until, wait_for_transactions_inclusion}, - mantle_inscription::make_inscription, - wallet::build_wallet_funded_transfer, -}; - -use crate::cucumber::world::ZoneReaderConfig; - -#[derive(Debug, thiserror::Error)] -pub enum ZoneTestError { - #[error("timed out waiting for zone sequencer to accept a publish request")] - PublishTimeout, - #[error("zone indexer request failed: {message}")] - Indexer { message: String }, - #[error("timed out waiting for zone indexer to return all messages")] - IndexerTimeout, - #[error("zone indexer returned {actual} copies of '{payload}', expected {expected}")] - IndexedPayloadCountMismatch { - payload: String, - expected: usize, - actual: usize, - }, - #[error("timed out waiting for zone transactions to appear on the canonical chain")] - InclusionTimeout, - #[error("failed to fetch consensus info while checking finalized transactions: {message}")] - Consensus { message: String }, - #[error("failed to fetch block while checking finalized transactions: {message}")] - Block { message: String }, - #[error("timed out waiting for zone transactions to finalize")] - FinalizationTimeout, - #[error("channel wallet request failed: {message}")] - ChannelWallet { message: String }, - #[error("timed out waiting for a channel wallet note")] - ChannelWalletTimeout, - #[error("timed out waiting for zone LIB to advance")] - LibAdvanceTimeout, - #[error("timed out waiting for zone sequencer channel view condition: {message}")] - ChannelViewTimeout { message: String }, - #[error("failed to find a funding note with exact value {value}")] - MissingExactFundingNote { value: Value }, - #[error("failed to submit zone deposit: {message}")] - SubmitDeposit { message: String }, - #[error("failed to submit zone channel split transfer: {message}")] - SplitTransfer { message: String }, - #[error("failed to sign zone transaction: {message}")] - SignTransaction { message: String }, - #[error("failed to build atomic zone deposit transaction: {message}")] - BuildAtomicDeposit { message: String }, - #[error("failed to submit atomic zone deposit transaction: {message}")] - SubmitAtomicDeposit { message: String }, - #[error("failed to submit zone withdraw transaction: {message}")] - SubmitWithdraw { message: String }, - #[error("timed out waiting for zone withdraw to appear in the indexer")] - WithdrawTimeout, - #[error("failed to build custom zone transaction: {message}")] - BuildCustomTx { message: String }, - #[error("failed to submit custom zone transaction: {message}")] - SubmitCustomTx { message: String }, - #[error("zone sequencer event stream stopped before observing the expected event")] - SequencerStopped, - #[error(transparent)] - BoundedError(#[from] lb_utils::bounded::BoundedError), - #[error(transparent)] - OutputsError(#[from] OutputsError), -} - -/// Result of an atomic deposit scenario where a deposit and zone inscription -/// are submitted as one Mantle transaction. -pub struct AtomicZoneDepositSubmission { - pub deposit: DepositOp, - pub publish: PublishResult, - pub reserved_inputs: Vec, -} - -pub struct AtomicZoneDepositRequest { - pub channel_id: ChannelId, - pub funding_public_key: ZkPublicKey, - pub available_utxos: Vec, - pub amount: Value, - pub inscription_data: Inscription, - pub metadata: Metadata, -} - -/// Result of a withdraw scenario where the zone sequencer signs the channel -/// withdraw and publishes the accompanying inscription. -pub struct ZoneWithdrawSubmission { - pub withdraw: ChannelWithdrawOp, - pub publish: PublishResult, -} - -pub struct ZoneDeposit { - pub deposit: DepositOp, - pub reserved_inputs: Vec, - /// The channel notes the deposit re-creates (1:1 with its inputs). Computed - /// deterministically from the deposit's `op_id` and its input notes so a - /// later channel transfer can spend them without waiting on the indexer. - pub channel_notes: Vec, -} - -/// The channel notes a deposit re-creates: one per input, same note (value + -/// pk), re-homed under the deposit's `op_id`. Matches the ledger's deposit -/// execution (`DepositOp::outputs` re-creates inputs 1:1) and the zone-sdk's -/// channel-note derivation. -fn recreated_channel_notes(deposit: &DepositOp, reserved_inputs: &[Utxo]) -> Vec { - let op_id = deposit.op_id(); - reserved_inputs - .iter() - .enumerate() - .map(|(index, utxo)| Utxo::new(op_id, index, utxo.note)) - .collect() -} - -pub type DiscardedPayloads = Arc>>; -pub type ZoneAccountBalances = HashMap; - -/// Shared deadline for a publish attempt and the matching event wait so the -/// whole operation has one timeout budget. -#[derive(Clone, Copy)] -pub struct PublishDeadline { - started_at: Instant, - timeout: Duration, -} - -impl PublishDeadline { - #[must_use] - pub fn from_now(timeout: Duration) -> Self { - Self { - started_at: Instant::now(), - timeout, - } - } - - fn is_expired(self) -> bool { - self.started_at.elapsed() > self.timeout - } -} - -/// Bundle returned from policy starters so callers can wire the cucumber -/// world. Wraps [`runner::Runtime`] — events and checkpoints are exposed -/// uniformly across all policies because the policy runs inline on the -/// drive task; the event mpsc is purely for test observation. -pub struct PolicyRuntime { - pub task: JoinHandle<()>, - pub view_violation: runner::ViewViolation, - pub client: SequencerClient, - pub events: tokio::sync::broadcast::Receiver, - pub checkpoint_rx: tokio::sync::watch::Receiver>, - pub ready_rx: tokio::sync::watch::Receiver, - pub channel_view_rx: tokio::sync::watch::Receiver, - pub turn_to_write_rx: tokio::sync::watch::Receiver, -} - -fn to_policy_runtime(rt: runner::Runtime) -> PolicyRuntime { - PolicyRuntime { - task: rt.task, - view_violation: rt.view_violation, - client: rt.client, - events: rt.event_rx, - checkpoint_rx: rt.checkpoint_rx, - ready_rx: rt.ready_rx, - channel_view_rx: rt.channel_view_rx, - turn_to_write_rx: rt.turn_to_write_rx, - } -} - -mod atomic; -mod custom_policy; -mod deposit_policy; -mod observation; -mod policies; -mod transactions; - -use atomic::{build_atomic_deposit_op, build_atomic_deposit_transfer, sign_tx_zk}; -pub(super) use atomic::{publish_atomic_zone_withdraw, submit_zone_withdraw}; -pub(super) use custom_policy::{CustomRepublishDeps, start_custom_republish_policy}; -pub(super) use deposit_policy::{ - BundleAnnounce, MultiSigBus, pin_payload, start_deposit_lifecycle_policy, - start_deposit_withdraw_policy, start_multisig_lifecycle_policy, withdraw_payload, -}; -pub(super) use observation::{ - balance_update_payload, collect_indexed_messages, collect_indexed_messages_exactly_once, - ensure_zone_message_included, ensure_zone_transactions_included, keygen, parse_balance_payload, - publish_message_with_retry, replay_finalized_history, replayed_inscription_payloads, - sequencer_config, sequencer_config_with_pending_submit_depth, - wait_for_channel_transfer_input_count, wait_for_channel_view, wait_for_channel_wallet_counts, - wait_for_channel_wallet_note, wait_for_deposit, wait_for_exact_indexed_payload_count, - wait_for_finalized_deposit_via_sequencer, wait_for_finalized_withdraw_via_sequencer, - wait_for_lib_advance, wait_for_transactions_finalized, wait_for_turn_to_write, - wait_for_withdraw, -}; -pub(super) use policies::{ - start_balance_aware_policy, start_republish_lineage_policy, start_sequencer_event_loop, - start_sorted_conflict_policy, -}; -use transactions::build_funded_custom_tx; -pub(super) use transactions::{ - build_zone_deposit, build_zone_deposit_from_values, submit_atomic_zone_deposit, - submit_zone_channel_split, submit_zone_deposit, -}; diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/observation.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/observation.rs deleted file mode 100644 index c6537d2..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/observation.rs +++ /dev/null @@ -1,641 +0,0 @@ -use super::*; - -/// Creates a scenario-local sequencer key. -#[must_use] -pub fn keygen() -> Ed25519Key { - let mut key_bytes = [0u8; 32]; - thread_rng().fill(&mut key_bytes); - Ed25519Key::from_bytes(&key_bytes) -} - -/// Encodes a balance-affecting zone payload used by balance-aware sequencer -/// scenarios. -#[must_use] -pub fn balance_update_payload(uuid: &str, account: &str, delta: i64) -> Inscription { - make_inscription(&format!("{uuid}:{account}:{delta}")) -} - -/// Parses a balance-affecting payload in the same format produced by -/// [`balance_update_payload`]. -pub fn parse_balance_payload(payload: &Inscription) -> Option<(String, String, i64)> { - let payload = std::str::from_utf8(payload.as_slice()).ok()?; - let parts = payload.splitn(3, ':').collect::>(); - let [uuid, account, delta] = parts.as_slice() else { - return None; - }; - - Some(( - (*uuid).to_owned(), - (*account).to_owned(), - delta.parse().ok()?, - )) -} - -/// Uses a short resubmit interval so retry-sensitive zone scenarios settle -/// quickly enough for CI. -#[must_use] -pub const fn sequencer_config(funding: FundingConfig) -> SequencerConfig { - SequencerConfig { - resubmit_interval: Duration::from_secs(3), - min_slots_remaining_in_turn: 2, - ..SequencerConfig::new(funding) - } -} - -/// Uses the same retry profile while overriding pending publish submit depth. -#[must_use] -pub const fn sequencer_config_with_pending_submit_depth( - max_pending_publish_depth: usize, - funding: FundingConfig, -) -> SequencerConfig { - SequencerConfig { - max_pending_publish_depth, - ..sequencer_config(funding) - } -} - -/// Publishes a zone payload through the runner and returns the SDK's -/// [`PublishResult`] inline. Retries transient publish errors until the -/// deadline elapses. No "wait for event" — the SDK accepts the publish -/// inline (funding it via the node when configured) and the runner forwards -/// the call through the drive task. -pub async fn publish_message_with_retry( - client: &SequencerClient, - data: &Inscription, - deadline: PublishDeadline, -) -> Result { - loop { - if deadline.is_expired() { - return Err(ZoneTestError::PublishTimeout); - } - match client.publish(data.clone()).await { - Ok((result, _cp)) => return Ok(result), - Err(error) => { - warn!(error = %error, "Zone sequencer publish failed, retrying"); - sleep(Duration::from_millis(500)).await; - } - } - } -} - -/// Waits until the subscribed channel view satisfies the supplied predicate. -pub async fn wait_for_channel_view( - view_rx: &mut tokio::sync::watch::Receiver, - duration: Duration, - predicate: impl Fn(&SequencerChannelView) -> bool + Send + Sync, -) -> Result { - timeout(duration, async { - loop { - let current = view_rx.borrow().clone(); - if predicate(¤t) { - return Ok(current); - } - - view_rx - .changed() - .await - .map_err(|error| ZoneTestError::Indexer { - message: format!("channel view sender closed: {error}"), - })?; - } - }) - .await - .map_err(|_| ZoneTestError::ChannelViewTimeout { - message: format!( - "condition not reached within {} seconds", - duration.as_secs() - ), - })? -} - -/// Waits until the sequencer emits a turn-to-write notification. -pub async fn wait_for_turn_to_write( - turn_rx: &mut tokio::sync::watch::Receiver, - duration: Duration, -) -> Result { - timeout(duration, async { - loop { - let current = turn_rx.borrow().clone(); - if current.our_turn_to_write { - return Ok(current); - } - - turn_rx - .changed() - .await - .map_err(|error| ZoneTestError::Indexer { - message: format!("turn-to-write sender closed: {error}"), - })?; - } - }) - .await - .map_err(|_| ZoneTestError::ChannelViewTimeout { - message: format!( - "turn to write not reached within {} seconds", - duration.as_secs() - ), - })? -} - -/// Replays the channel's finalized history by cold-starting a fresh -/// read-only sequencer: a random signing key that is not part of the channel -/// rotation, so the instance can never publish or repost anything — -/// inscription posting is turn-gated. Finalized txs are collected from the -/// backfill events until the sequencer reports `Ready`, then the instance is -/// dropped; each call observes a fresh snapshot up to the LIB at connect -/// time. -pub async fn replay_finalized_history( - reader: &ZoneReaderConfig, -) -> Result, ZoneTestError> { - let node = ZoneNodeHttpClient::new(CommonHttpClient::new(None), reader.node_url.clone()); - // Placeholder funding: the reader never publishes (random key, posting is - // turn-gated), so the funding wallet is never exercised. - let funding = FundingConfig { - funding_pk: lb_groth16::Fr::from(1u64).into(), - change_pk: None, - max_tx_fee: GasCost::new(u64::MAX), - priority_fee_percent: FundingConfig::DEFAULT_PRIORITY_FEE_PERCENT, - }; - let mut sequencer = ZoneSequencer::init(reader.channel_id, keygen(), node, funding, None); - - timeout(Duration::from_mins(3), async { - let mut finalized = Vec::new(); - loop { - match sequencer.next_event().await { - Event::BlocksProcessed { - finalized: batch, .. - } => finalized.extend(batch), - Event::Ready => return finalized, - Event::TurnNotification { .. } => {} - } - } - }) - .await - .map_err(|_| ZoneTestError::IndexerTimeout) -} - -/// Ordered inscription payloads within a finalized-history replay. -pub fn replayed_inscription_payloads(history: &[FinalizedTx]) -> Vec { - finalized_inscriptions(history) - .map(|info| info.payload.clone()) - .collect() -} - -/// Collects indexed block payloads until all expected messages have appeared. -/// -/// The returned order is the finalized on-chain order, which lets assertions -/// decide whether ordering matters for the scenario. -pub async fn collect_indexed_messages( - reader: &ZoneReaderConfig, - expected_messages: &[Inscription], - duration: Duration, -) -> Result, ZoneTestError> { - let expected: HashSet = expected_messages.iter().cloned().collect(); - - timeout(duration, async { - loop { - let payloads = replayed_inscription_payloads(&replay_finalized_history(reader).await?); - let mut seen: HashSet = HashSet::new(); - let mut ordered: Vec = Vec::new(); - for payload in payloads { - if expected.contains(&payload) && seen.insert(payload.clone()) { - ordered.push(payload); - } - } - - if seen == expected { - return Ok(ordered); - } - - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::IndexerTimeout)? -} - -/// Replays the finalized history until it exactly matches the expected -/// message sequence without duplicates. -pub async fn collect_indexed_messages_exactly_once( - reader: &ZoneReaderConfig, - expected_messages: &[Inscription], - duration: Duration, -) -> Result, ZoneTestError> { - let expected: HashSet = expected_messages.iter().cloned().collect(); - - timeout(duration, async { - loop { - let ordered: Vec = - replayed_inscription_payloads(&replay_finalized_history(reader).await?) - .into_iter() - .filter(|payload| expected.contains(payload)) - .collect(); - - if ordered == expected_messages { - return Ok(ordered); - } - - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::IndexerTimeout)? -} - -/// Waits until the finalized history contains exactly `expected_count` copies -/// of one payload after a short settle period. -/// -/// This intentionally counts duplicate payload bytes, which is required for -/// shared-payload zone tests where each inscription has the same data but a -/// distinct transaction lineage. -pub async fn wait_for_exact_indexed_payload_count( - reader: &ZoneReaderConfig, - expected_payload: Inscription, - expected_count: usize, - duration: Duration, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let count = count_indexed_payload(reader, &expected_payload).await?; - - if count >= expected_count { - sleep(Duration::from_secs(30)).await; - - let final_count = count_indexed_payload(reader, &expected_payload).await?; - if final_count == expected_count { - return Ok(()); - } - - return Err(ZoneTestError::IndexedPayloadCountMismatch { - payload: String::from_utf8_lossy(expected_payload.as_slice()).to_string(), - expected: expected_count, - actual: final_count, - }); - } - - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::IndexerTimeout)? -} - -async fn count_indexed_payload( - reader: &ZoneReaderConfig, - expected_payload: &Inscription, -) -> Result { - Ok( - replayed_inscription_payloads(&replay_finalized_history(reader).await?) - .iter() - .filter(|payload| *payload == expected_payload) - .count(), - ) -} - -/// Polls until the wallet holds exactly the given number of finalized and -/// unfinalized notes — an exact-count check that catches double-counting. -pub async fn wait_for_channel_wallet_counts( - client: &SequencerClient, - finalized: usize, - unfinalized: usize, - duration: Duration, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let view = - client - .channel_wallet() - .await - .map_err(|error| ZoneTestError::ChannelWallet { - message: error.to_string(), - })?; - if view.finalized.len() == finalized && view.unfinalized.len() == unfinalized { - return Ok(()); - } - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::ChannelWalletTimeout)? -} - -/// Polls the sequencer's channel wallet until a note of `value` is present. -/// With `finalized_only`, only the finalized layer counts. -pub async fn wait_for_channel_wallet_note( - client: &SequencerClient, - value: Value, - finalized_only: bool, - duration: Duration, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let view = - client - .channel_wallet() - .await - .map_err(|error| ZoneTestError::ChannelWallet { - message: error.to_string(), - })?; - let unfinalized = (!finalized_only) - .then_some(view.unfinalized.iter()) - .into_iter() - .flatten(); - if view - .finalized - .iter() - .chain(unfinalized) - .any(|note| note.value == value) - { - return Ok(()); - } - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::ChannelWalletTimeout)? -} - -/// Waits until the finalized channel history contains the expected channel -/// deposit, including its amount. -pub async fn wait_for_deposit( - reader: &ZoneReaderConfig, - expected: &DepositOp, - expected_amount: Value, - duration: Duration, -) -> Result<(), ZoneTestError> { - poll_replayed_history_until(reader, duration, ZoneTestError::IndexerTimeout, |op| { - matches!(op, FinalizedOp::Deposit(deposit) - if deposit.inputs == expected.inputs - && deposit.amount == expected_amount - && deposit.metadata == expected.metadata) - }) - .await -} - -/// Waits until the finalized channel history contains the expected withdraw. -pub async fn wait_for_withdraw( - reader: &ZoneReaderConfig, - expected: &ChannelWithdrawOp, - timeout_duration: Duration, -) -> Result<(), ZoneTestError> { - poll_replayed_history_until( - reader, - timeout_duration, - ZoneTestError::WithdrawTimeout, - |op| matches!(op, FinalizedOp::Withdraw(withdraw) if withdraw.op.inputs == expected.inputs), - ) - .await -} - -/// Waits until the finalized channel history contains a channel transfer whose -/// input set has exactly `expected_inputs` notes. -/// -/// This is the on-chain record of the withdrawal's note selection: in the -/// dust-flood scenario it proves best-fit largest-first consumed a single -/// covering note rather than sweeping the >255-note dust flood into a transfer -/// the ledger would reject. -pub async fn wait_for_channel_transfer_input_count( - reader: &ZoneReaderConfig, - expected_inputs: usize, - timeout_duration: Duration, -) -> Result<(), ZoneTestError> { - poll_replayed_history_until( - reader, - timeout_duration, - ZoneTestError::IndexerTimeout, - move |op| { - matches!(op, FinalizedOp::ChannelTransfer(transfer) - if transfer.op.inputs.len() == expected_inputs) - }, - ) - .await -} - -async fn poll_replayed_history_until( - reader: &ZoneReaderConfig, - duration: Duration, - timeout_error: ZoneTestError, - mut predicate: impl FnMut(&FinalizedOp) -> bool, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let history = replay_finalized_history(reader).await?; - if history - .iter() - .flat_map(|tx| tx.ops.iter()) - .any(&mut predicate) - { - return Ok(()); - } - - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| timeout_error)? -} - -/// Waits until the sequencer's event stream surfaces the expected deposit -/// in [`Event::BlocksProcessed::finalized`] (matched by `inputs`, `amount`, -/// and `metadata`). Drains the events channel as it goes — call this after -/// any earlier event consumers in the scenario have moved past the relevant -/// publish events. -pub async fn wait_for_finalized_deposit_via_sequencer( - events: &mut tokio::sync::broadcast::Receiver, - expected: &DepositOp, - expected_amount: Value, - duration: Duration, -) -> Result<(), ZoneTestError> { - poll_sequencer_finalized_until(events, duration, ZoneTestError::IndexerTimeout, |op| { - matches!(op, FinalizedOp::Deposit(d) - if d.inputs == expected.inputs - && d.amount == expected_amount - && d.metadata == expected.metadata) - }) - .await -} - -/// Waits until the sequencer's event stream surfaces the expected withdraw -/// (matched by `outputs`). Drains the events channel as it goes. -pub async fn wait_for_finalized_withdraw_via_sequencer( - events: &mut tokio::sync::broadcast::Receiver, - expected: &ChannelWithdrawOp, - duration: Duration, -) -> Result<(), ZoneTestError> { - poll_sequencer_finalized_until( - events, - duration, - ZoneTestError::WithdrawTimeout, - |op| matches!(op, FinalizedOp::Withdraw(w) if w.op.inputs == expected.inputs), - ) - .await -} - -async fn poll_sequencer_finalized_until( - events: &mut tokio::sync::broadcast::Receiver, - duration: Duration, - timeout_error: ZoneTestError, - mut predicate: impl FnMut(&FinalizedOp) -> bool, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let event = match events.recv().await { - Ok(event) => event, - Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => { - warn!("event subscriber lagged by {n}, recovering"); - continue; - } - Err(tokio::sync::broadcast::error::RecvError::Closed) => { - return Err(ZoneTestError::SequencerStopped); - } - }; - let Event::BlocksProcessed { finalized, .. } = event else { - continue; - }; - for tx in finalized { - if tx.ops.iter().any(&mut predicate) { - return Ok(()); - } - } - } - }) - .await - .map_err(|_| timeout_error)? -} - -/// Waits until node mempool/chain observation confirms the submitted zone -/// transactions reached the canonical chain. -pub async fn ensure_zone_transactions_included( - client: &NodeHttpClient, - tx_hashes: &[InscriptionId], - duration: Duration, -) -> Result<(), ZoneTestError> { - let included = wait_for_transactions_inclusion(client, tx_hashes, duration).await; - - if included { - return Ok(()); - } - - Err(ZoneTestError::InclusionTimeout) -} - -/// Waits until the inscription with `msg_id` sits on `client`'s tip chain, -/// under whatever tx hash: a stale refund rebuilds a message under a new hash -/// but keeps its id, so inclusion is a property of the message. -pub async fn ensure_zone_message_included( - client: &NodeHttpClient, - channel_id: ChannelId, - msg_id: MsgId, - duration: Duration, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let consensus = - client - .consensus_info() - .await - .map_err(|error| ZoneTestError::Consensus { - message: error.to_string(), - })?; - let mut scanned = HashSet::new(); - let found = scan_chain_until( - consensus.cryptarchia_info.tip, - &mut scanned, - async |header_id| client.block(&header_id).await.ok().flatten(), - |block: &ApiBlock| { - block - .transactions - .iter() - .flat_map(|tx| channel_inscriptions(tx, channel_id)) - .any(|info| info.this_msg == msg_id) - .then_some(()) - }, - ) - .await - .is_some(); - if found { - return Ok(()); - } - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::InclusionTimeout)? -} - -/// Walks back from LIB until every expected zone transaction is found in the -/// finalized chain. -pub async fn wait_for_transactions_finalized( - node_url: Url, - tx_hashes: &[InscriptionId], - duration: Duration, -) -> Result<(), ZoneTestError> { - let client = CommonHttpClient::new(None); - let expected: HashSet<_> = tx_hashes.iter().copied().collect(); - - timeout(duration, async { - loop { - let info = client - .consensus_info(node_url.clone()) - .await - .map_err(|error| ZoneTestError::Consensus { - message: error.to_string(), - })?; - - let mut found = HashSet::new(); - let mut current = info.cryptarchia_info.lib; - - while let Some(block) = client - .get_block_by_id(node_url.clone(), current) - .await - .map_err(|error| ZoneTestError::Block { - message: error.to_string(), - })? - { - for tx in &block.transactions { - let hash = tx.hash(); - if expected.contains(&hash) { - found.insert(hash); - } - } - - current = block.header.parent_block; - } - - if found == expected { - return Ok(()); - } - - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::FinalizationTimeout)? -} - -/// Waits for LIB movement after a restart so stale-checkpoint scenarios can -/// distinguish old local state from new canonical chain progress. -pub async fn wait_for_lib_advance( - client: &NodeHttpClient, - initial_lib_slot: Slot, - duration: Duration, -) -> Result<(), ZoneTestError> { - timeout(duration, async { - loop { - let info = client - .consensus_info() - .await - .map_err(|error| ZoneTestError::Consensus { - message: error.to_string(), - })?; - - if info.cryptarchia_info.lib_slot > initial_lib_slot { - return Ok(()); - } - - sleep(Duration::from_millis(500)).await; - } - }) - .await - .map_err(|_| ZoneTestError::LibAdvanceTimeout)? -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/policies.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/policies.rs deleted file mode 100644 index b590bfa..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/policies.rs +++ /dev/null @@ -1,538 +0,0 @@ -use super::{ - Arc, BTreeSet, ChannelUpdate, ChannelUpdateTx, DiscardedPayloads, Event, FinalizedTx, HashMap, - HashSet, Inscription, InscriptionInfo, Inscriptions as _, LazyLock, MsgId, PolicyRuntime, - SequencerChannelView, VecDeque, ZoneAccountBalances, ZoneNodeHttpClient, ZoneSequencer, - contributed, finalized_inscriptions, parse_balance_payload, runner, to_policy_runtime, warn, -}; - -/// Spawn a sequencer drive task with a no-op policy. Step bodies drive -/// publishes via [`SequencerClient`]; events flow to `PolicyRuntime.events`. -/// If `republish_orphans` is set, the [`OrphanRepublishPolicy`] runs inline -/// inside the drive loop. -pub fn start_sequencer_event_loop( - sequencer: ZoneSequencer, - republish_orphans: bool, -) -> PolicyRuntime { - if republish_orphans { - to_policy_runtime(runner::spawn(sequencer, OrphanRepublishPolicy::default())) - } else { - to_policy_runtime(runner::spawn(sequencer, runner::PassivePolicy)) - } -} - -/// Drives a competing-sequencer policy that publishes `planned` once ready and -/// re-publishes its own orphans (tracked by intent lineage) until they land — -/// correct even when payloads repeat. -pub fn start_republish_lineage_policy( - sequencer: ZoneSequencer, - planned: Vec, -) -> PolicyRuntime { - let policy = RepublishLineagePolicy { - planned, - published_initial: false, - lineage: LineageTracker::default(), - }; - to_policy_runtime(runner::spawn(sequencer, policy)) -} - -/// Drives a policy that republishes orphaned balance updates only when the -/// local canonical view can still apply the update without going negative, -/// and lays planned balance updates whenever it's our turn to write. -pub fn start_balance_aware_policy( - sequencer: ZoneSequencer, - initial_balances: ZoneAccountBalances, - planned_payloads: Vec, -) -> PolicyRuntime { - let view_rx = sequencer.subscribe_channel_view(); - let policy = BalanceAwarePolicy { - balances: BalanceAwareState::new(initial_balances), - planned: VecDeque::from(planned_payloads), - view_rx, - }; - to_policy_runtime(runner::spawn(sequencer, policy)) -} - -/// Drives a deterministic conflict policy used by tests that expect the final -/// zone chain to converge to sorted payload order. -pub fn start_sorted_conflict_policy( - sequencer: ZoneSequencer, - discarded: &DiscardedPayloads, -) -> PolicyRuntime { - let policy = SortedConflictPolicy { - state: SortedConflictState::new(Arc::clone(discarded)), - }; - to_policy_runtime(runner::spawn(sequencer, policy)) -} - -/// Inline policy: republish orphaned inscriptions not already back on the -/// canonical chain. Plain inscriptions only — bundles re-prepare themselves. -/// Assumes unique payloads; for repeating payloads see -/// [`RepublishLineagePolicy`]. -/// -/// The non-finalized view is read from each update (`common_prefix ++ -/// adopted`), so a payload still on chain — a live twin of a dead one — is -/// never re-homed. -#[derive(Default)] -struct OrphanRepublishPolicy { - /// Finalized payloads: permanently on chain, never orphaned. - finalized: HashSet, -} - -impl runner::Policy for OrphanRepublishPolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - let Event::BlocksProcessed { - channel_update, - finalized, - .. - } = event - else { - return; - }; - self.finalized - .extend(finalized_inscriptions(finalized).map(|info| info.payload.clone())); - let Some(chain) = channel_update.canonical_chain() else { - return; - }; - let on_chain: HashSet<&Inscription> = - chain.inscriptions().map(|info| &info.payload).collect(); - for entry in channel_update.orphaned() { - let ChannelUpdateTx::Inscription(info) = entry else { - continue; - }; - if on_chain.contains(&info.payload) || self.finalized.contains(&info.payload) { - continue; - } - if let Err(error) = sequencer.handle().publish(info.payload.clone()).await { - warn!(%error, "Failed to re-publish orphaned zone payload"); - } - } - } -} - -/// Tracks our published inscriptions by intent lineage, so republishing works -/// even when payloads repeat (identical bytes published as distinct messages). -/// -/// Each original publish is its own intent, rooted at its `this_msg`; every -/// republish we issue for an orphaned member is recorded under the same root. -/// An intent is "live" while any of its `this_msg`s is on the channel -/// (`adopted`) or in flight as a publish/republish we issued. Identical -/// payloads form distinct intents (distinct `this_msg`s), so each lands once, -/// and other sequencers' inscriptions are never in our map, so we never -/// republish theirs. -#[derive(Default)] -struct LineageTracker { - /// Every `this_msg` we've published (originals + republishes) → intent - /// root. - intent_root: HashMap, - /// Per intent root, the `this_msg`s in the non-finalized view, plus those - /// published since the last event. - pending: HashMap>, - /// Intent roots that have finalized — permanently landed, so the intent is - /// considered live forever and never re-homed again. - finalized_roots: HashSet, -} - -impl LineageTracker { - /// Record an original publish as its own intent, in flight. - fn record_publish(&mut self, this_msg: MsgId) { - self.intent_root.insert(this_msg, this_msg); - self.pending.entry(this_msg).or_default().insert(this_msg); - } - - /// Record a republish of `orphan` as a new live member of its intent. - fn record_republish(&mut self, orphan: MsgId, republished: MsgId) { - let root = self.intent_root.get(&orphan).copied().unwrap_or(orphan); - self.intent_root.insert(republished, root); - self.pending.entry(root).or_default().insert(republished); - } - - /// Refresh per-intent liveness. On a conflict our members in - /// `common_prefix ++ adopted` are the live ones; a republish issued last - /// event is in the prefix as pending, so nothing carries over. On an - /// extension nothing of ours leaves, and our own publishes never appear - /// in `adopted`, so there is nothing to fold. - fn observe(&mut self, channel_update: &ChannelUpdate) { - let Some(chain) = channel_update.canonical_chain() else { - return; - }; - self.pending.clear(); - for info in chain.inscriptions() { - if let Some(&root) = self.intent_root.get(&info.this_msg) { - self.pending.entry(root).or_default().insert(info.this_msg); - } - } - } - - /// Pin the intents of any finalized `this_msg`s of ours as permanently - /// live — once a member finalizes the payload is on chain for good. - fn observe_finalized(&mut self, finalized: impl Iterator) { - for this_msg in finalized { - if let Some(&root) = self.intent_root.get(&this_msg) { - self.finalized_roots.insert(root); - } - } - } - - /// True if `this_msg` is one of ours. - fn is_ours(&self, this_msg: &MsgId) -> bool { - self.intent_root.contains_key(this_msg) - } - - /// True if the intent of `this_msg` has finalized, or still has a live - /// member. - fn intent_live(&self, this_msg: &MsgId) -> bool { - let root = self.intent_root.get(this_msg).copied().unwrap_or(*this_msg); - self.finalized_roots.contains(&root) - || self - .pending - .get(&root) - .is_some_and(|members| !members.is_empty()) - } -} - -/// Inline republish policy for channels whose payloads can repeat. Publishes -/// its own `planned` payloads once the sequencer is ready, then republishes any -/// of *our* orphans whose intent has no live member, tracking msg-id lineage -/// (the payload can't identify the message when it repeats). Owning the -/// publishes is what gives the policy its outbox: every `this_msg` it sends is -/// recorded. -struct RepublishLineagePolicy { - planned: Vec, - published_initial: bool, - lineage: LineageTracker, -} - -impl runner::Policy for RepublishLineagePolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - match event { - Event::Ready if !self.published_initial => { - self.published_initial = true; - for payload in self.planned.clone() { - match sequencer.handle().publish(payload).await { - Ok((result, _checkpoint)) => { - self.lineage - .record_publish(result.tx.inscription().this_msg); - } - Err(error) => warn!(%error, "Failed to publish planned zone payload"), - } - } - } - Event::BlocksProcessed { - channel_update, - finalized, - .. - } => { - self.lineage - .observe_finalized(finalized_inscriptions(finalized).map(|i| i.this_msg)); - self.lineage.observe(channel_update); - for entry in channel_update.orphaned() { - let ChannelUpdateTx::Inscription(info) = entry else { - continue; - }; - if !self.lineage.is_ours(&info.this_msg) - || self.lineage.intent_live(&info.this_msg) - { - continue; - } - match sequencer.handle().publish(info.payload.clone()).await { - Ok((result, _checkpoint)) => { - self.lineage - .record_republish(info.this_msg, result.tx.inscription().this_msg); - } - Err(error) => warn!(%error, "Failed to re-publish orphaned zone payload"), - } - } - } - _ => {} - } - } -} - -/// Inline policy: republish orphans only when the local balance view still -/// allows it; publish planned payloads as soon as it's our turn to write. -/// -/// The balance view follows each update: an extension's `adopted` is applied -/// on top, a conflict rebuilds the non-finalized deltas from -/// `common_prefix ++ adopted` over the finalized ones. A payload we published -/// since the last event is in the prefix as pending, so it is never applied -/// twice. -struct BalanceAwarePolicy { - balances: BalanceAwareState, - planned: VecDeque, - view_rx: tokio::sync::watch::Receiver, -} - -impl runner::Policy for BalanceAwarePolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - if let Event::BlocksProcessed { - channel_update, - finalized, - .. - } = event - { - self.balances.record_finalized_payloads(finalized); - self.balances.observe(channel_update); - let orphaned_inscriptions: Vec = channel_update - .orphaned() - .iter() - .filter_map(|o| match o { - ChannelUpdateTx::Inscription(i) => Some(i.clone()), - ChannelUpdateTx::AtomicWithdraw(_) - | ChannelUpdateTx::PinDeposit(_) - | ChannelUpdateTx::Custom(_) - | ChannelUpdateTx::Config(_) => None, - }) - .collect(); - for info in orphaned_inscriptions { - if !self.balances.should_republish(&info.payload) { - continue; - } - if let Err(error) = sequencer.handle().publish(info.payload.clone()).await { - warn!(%error, "Failed to re-publish balance-aware zone payload"); - continue; - } - self.balances.record_republished_payload(&info.payload); - } - } - - if !self.view_rx.borrow().our_turn_to_write { - return; - } - while let Some(payload) = self.planned.pop_front() { - if !self.balances.should_republish(&payload) { - continue; - } - if let Err(error) = sequencer.handle().publish(payload.clone()).await { - warn!(%error, "Failed to publish planned balance-aware zone payload"); - self.planned.push_front(payload); - break; - } - self.balances.record_republished_payload(&payload); - } - } -} - -/// Inline policy: republish orphans only when they preserve sorted-payload -/// order; otherwise mark them as discarded. -/// -/// The on-chain payload set follows each update: an extension's `adopted` -/// joins it, a conflict rebuilds it from `common_prefix ++ adopted`, so the -/// order floor we gate republishing on falls back correctly when the highest -/// payload is orphaned. -struct SortedConflictPolicy { - state: SortedConflictState, -} - -impl runner::Policy for SortedConflictPolicy -where - Node: lb_zone_sdk::adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, sequencer: &mut ZoneSequencer, event: &Event) { - let Event::BlocksProcessed { - channel_update, - finalized, - .. - } = event - else { - return; - }; - self.state.record_finalized(finalized); - self.state.observe(channel_update); - let (orphaned, adopted) = (channel_update.orphaned(), channel_update.adopted()); - let orphaned_inscriptions: Vec<&InscriptionInfo> = orphaned - .iter() - .filter_map(|o| match o { - ChannelUpdateTx::Inscription(i) => Some(i), - ChannelUpdateTx::AtomicWithdraw(_) - | ChannelUpdateTx::PinDeposit(_) - | ChannelUpdateTx::Custom(_) - | ChannelUpdateTx::Config(_) => None, - }) - .collect(); - - self.state.record_adoptions(adopted).await; - - let readopted: HashSet<&Inscription> = adopted - .iter() - .filter_map(|tx| tx.inscription().map(|i| &i.payload)) - .collect(); - - // Consider this round's fresh orphans together with everything parked, - // in sorted order (a `BTreeSet` iterates ascending). A payload parked - // under a higher floor on another branch then slots in ahead of a higher - // fresh orphan instead of being locked out, and the chain stays sorted. - // Finalized payloads are excluded — they're already permanently landed. - let mut candidates: BTreeSet = orphaned_inscriptions - .iter() - .map(|i| i.payload.clone()) - .filter(|payload| !readopted.contains(payload) && !self.state.is_finalized(payload)) - .collect(); - candidates.extend(self.state.discarded_snapshot().await); - - for payload in candidates { - if self.state.is_finalized(&payload) { - continue; - } - if self.state.preserves_order(&payload) { - if let Err(error) = sequencer.handle().publish(payload.clone()).await { - warn!(%error, "Failed to re-publish sorted zone payload"); - continue; - } - self.state.record_published_payload(payload).await; - } else { - self.state.discard(payload).await; - } - } - } -} - -struct BalanceAwareState { - initial_balances: ZoneAccountBalances, - applied: HashMap>, - finalized: HashSet, -} - -impl BalanceAwareState { - fn new(initial_balances: ZoneAccountBalances) -> Self { - Self { - initial_balances, - applied: HashMap::new(), - finalized: HashSet::new(), - } - } - - /// Pin finalized payloads. - fn record_finalized_payloads(&mut self, finalized: &[FinalizedTx]) { - for inscription in finalized_inscriptions(finalized) { - if let Some((uuid, _, _)) = parse_balance_payload(&inscription.payload) { - self.finalized.insert(uuid); - } - self.record_applied_payload(&inscription.payload); - } - } - - fn record_applied_payload(&mut self, payload: &Inscription) { - let Some((uuid, account, delta)) = parse_balance_payload(payload) else { - return; - }; - - self.applied.entry(account).or_default().insert(uuid, delta); - } - - /// Apply an extension's `adopted`; on a conflict replace the - /// non-finalized deltas with the view's. Finalized deltas stay. - fn observe(&mut self, channel_update: &ChannelUpdate) { - if let ChannelUpdate::Conflict { .. } = channel_update { - let finalized = &self.finalized; - for updates in self.applied.values_mut() { - updates.retain(|uuid, _| finalized.contains(uuid)); - } - } - for info in contributed(channel_update).inscriptions() { - self.record_applied_payload(&info.payload); - } - } - - fn should_republish(&self, payload: &Inscription) -> bool { - let Some((uuid, account, delta)) = parse_balance_payload(payload) else { - return false; - }; - - if self.finalized.contains(&uuid) || self.account_updates(&account).contains_key(&uuid) { - return false; - } - - self.available_balance(&account) + delta >= 0 - } - - fn record_republished_payload(&mut self, payload: &Inscription) { - self.record_applied_payload(payload); - } - - fn available_balance(&self, account: &str) -> i64 { - self.initial_balances.get(account).copied().unwrap_or(0) - + self.account_updates(account).values().sum::() - } - - fn account_updates(&self, account: &str) -> &HashMap { - self.applied.get(account).unwrap_or(&EMPTY_BALANCE_UPDATES) - } -} - -static EMPTY_BALANCE_UPDATES: LazyLock> = LazyLock::new(HashMap::new); - -struct SortedConflictState { - /// The ordering floor: the non-finalized view as of the last update plus - /// what we published since, over the pinned finalized base. - channel_view: BTreeSet, - discarded: DiscardedPayloads, - finalized: HashSet, -} - -impl SortedConflictState { - fn new(discarded: DiscardedPayloads) -> Self { - Self { - channel_view: BTreeSet::new(), - discarded, - finalized: HashSet::new(), - } - } - - /// Pin finalized payloads into the channel view permanently. - fn record_finalized(&mut self, finalized: &[FinalizedTx]) { - for inscription in finalized_inscriptions(finalized) { - self.finalized.insert(inscription.payload.clone()); - self.channel_view.insert(inscription.payload.clone()); - } - } - - fn is_finalized(&self, payload: &Inscription) -> bool { - self.finalized.contains(payload) - } - - /// Extend the floor with an extension's `adopted`; on a conflict rebuild - /// it from the view over the finalized base. - fn observe(&mut self, channel_update: &ChannelUpdate) { - if let ChannelUpdate::Conflict { .. } = channel_update { - self.channel_view = self.finalized.iter().cloned().collect(); - } - self.channel_view.extend( - contributed(channel_update) - .inscriptions() - .map(|info| info.payload.clone()), - ); - } - - /// A discarded payload that landed anyway is no longer ours to re-home. - async fn record_adoptions(&self, adopted: &[ChannelUpdateTx]) { - for info in adopted.iter().filter_map(ChannelUpdateTx::inscription) { - self.discarded.lock().await.remove(&info.payload); - } - } - - async fn record_published_payload(&mut self, payload: Inscription) { - self.discarded.lock().await.remove(&payload); - self.channel_view.insert(payload); - } - - fn preserves_order(&self, payload: &Inscription) -> bool { - self.channel_view.last().is_none_or(|max| payload >= max) - } - - async fn discard(&self, payload: Inscription) { - self.discarded.lock().await.insert(payload); - } - - async fn discarded_snapshot(&self) -> Vec { - self.discarded.lock().await.iter().cloned().collect() - } -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/operations/transactions.rs b/tests/zone-sdk/src/cucumber/steps/zone/operations/transactions.rs deleted file mode 100644 index da3443f..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/operations/transactions.rs +++ /dev/null @@ -1,324 +0,0 @@ -use lb_core::mantle::{ - SignedOps, - ledger::{BoundedInputs, verification_mode::StandardMode}, - transactions::{OpProofs, Ops}, -}; - -use super::*; - -/// Builds a regular channel deposit for an existing funding note with the -/// exact deposit value. -pub fn build_zone_deposit( - available_utxos: Vec, - channel_id: ChannelId, - amount: Value, - metadata: Metadata, -) -> Result { - let note = available_utxos - .into_iter() - .find(|utxo| utxo.note.value == amount) - .ok_or(ZoneTestError::MissingExactFundingNote { value: amount })?; - - let deposit = DepositOp { - channel_id, - inputs: BoundedInputs::from(note.id()).into(), - metadata, - }; - let reserved_inputs = vec![note]; - let channel_notes = recreated_channel_notes(&deposit, &reserved_inputs); - Ok(ZoneDeposit { - deposit, - reserved_inputs, - channel_notes, - }) -} - -/// Build a deposit that consumes one wallet note per listed value, in order. -/// A deposit re-creates its inputs 1:1 as channel notes, so this yields a -/// multi-input deposit whose recreated notes carry those exact per-note values -/// — used to cover the channel wallet's per-note tracking. -pub fn build_zone_deposit_from_values( - available_utxos: Vec, - channel_id: ChannelId, - input_values: &[Value], - metadata: Metadata, -) -> Result { - let mut remaining = available_utxos; - let mut reserved_inputs = Vec::new(); - for &value in input_values { - let index = remaining - .iter() - .position(|utxo| utxo.note.value == value) - .ok_or(ZoneTestError::MissingExactFundingNote { value })?; - reserved_inputs.push(remaining.remove(index)); - } - - let input_ids: Vec<_> = reserved_inputs.iter().map(Utxo::id).collect(); - let deposit = DepositOp { - channel_id, - inputs: Inputs::try_new(input_ids).map_err(|error| ZoneTestError::SubmitDeposit { - message: format!("deposit input set exceeds bound: {error:?}"), - })?, - metadata, - }; - let channel_notes = recreated_channel_notes(&deposit, &reserved_inputs); - Ok(ZoneDeposit { - deposit, - reserved_inputs, - channel_notes, - }) -} - -/// Generous cap on channel transaction fees at genesis gas prices; actual -/// fees are a few hundred gas units for these small transactions. -const MAX_ZONE_DEPOSIT_TX_FEE: u64 = 10_000; - -/// Submits a regular channel deposit through the node wallet API. -pub async fn submit_zone_deposit( - node_url: &Url, - deposit: &DepositOp, - funding_public_key: ZkPublicKey, -) -> Result { - let body = ChannelDepositRequestBody { - tip: None, - deposit: deposit.clone(), - change_public_key: funding_public_key, - funding_public_keys: vec![funding_public_key], - max_tx_fee: MAX_ZONE_DEPOSIT_TX_FEE.into(), - }; - - let request_url = - node_url - .join("/channel/deposit") - .map_err(|error| ZoneTestError::SubmitDeposit { - message: error.to_string(), - })?; - - let response: ChannelDepositResponseBody = CommonHttpClient::new(None) - .post(request_url, &body) - .await - .map_err(|error| ZoneTestError::SubmitDeposit { - message: error.to_string(), - })?; - - Ok(response.hash) -} - -/// Splits one channel note into `dust_count` value-1 dust notes via a raw, -/// node-funded `ChannelTransfer`, submitted straight to the node mempool (no -/// zone-sdk involvement — mirrors how deposits are submitted). -/// -/// The transfer is value-preserving, so the input note's value must equal -/// `dust_count` (every output is value 1). The channel authorizes the transfer -/// with the sequencer's accredited key at index 0 (single-signer channel, -/// `transfer_threshold == 1`); the node appends and proves the fee transfer. -pub async fn submit_zone_channel_split( - node_url: &Url, - channel_id: ChannelId, - signing_key: &Ed25519Key, - funding_pk: ZkPublicKey, - input_note: Utxo, - dust_count: usize, -) -> Result { - let input_value = input_note.note.value; - if input_value != dust_count as u64 { - return Err(ZoneTestError::SplitTransfer { - message: format!( - "channel note value {input_value} must equal dust count {dust_count} \ - (each dust note is value 1)" - ), - }); - } - - let outputs = - Outputs::try_new(vec![Note::new(1, funding_pk); dust_count]).map_err(|error| { - ZoneTestError::SplitTransfer { - message: format!("dust outputs exceed bound: {error:?}"), - } - })?; - let transfer = ChannelTransferOp { - channel_id, - inputs: BoundedInputs::from(input_note.id()).into(), - outputs, - }; - - let tx_builder = MantleTxBuilder::new() - .push_op(Op::ChannelTransfer(transfer)) - .map_err(|error| ZoneTestError::SplitTransfer { - message: format!("too many ops: {error}"), - })?; - - let node = NodeHttpClient::from_url(node_url.clone()); - let response = node - .fund_tx(WalletFundRequestBody { - tip: None, - priority_fee_percent: 0, - tx_builder, - change_public_key: funding_pk, - funding_public_keys: vec![funding_pk], - max_tx_fee: GasCost::new(u64::MAX), - }) - .await - .map_err(|error| ZoneTestError::SplitTransfer { - message: format!("funding failed: {error}"), - })?; - - // The channel multi-sig proves the transfer over the funded tx hash; the - // funding appends its own fee transfer proof as the last op. - let funded_tx = response.funded_tx; - let tx_hash = funded_tx.hash(); - let signature = signing_key.sign_payload(tx_hash.as_signing_bytes()); - let proof = ChannelMultiSigProof::new(IndexedSignatures::from((0, signature))); - let mut op_proofs = OpProofs::from([OpProof::ChannelMultiSigProof(proof)]); - if let Some(transfer_proof) = response.transfer_proof { - op_proofs - .try_push(transfer_proof) - .map_err(|error| ZoneTestError::SplitTransfer { - message: format!("too many operation proofs: {error:?}"), - })?; - } - - let signed_ops = SignedOps::from_parts(funded_tx, op_proofs).map_err(|error| { - ZoneTestError::SplitTransfer { - message: format!("signed ops assembly failed: {error}"), - } - })?; - - node.submit_transaction(&signed_ops) - .await - .map_err(|error| ZoneTestError::SplitTransfer { - message: format!("submit failed: {error}"), - })?; - - Ok(tx_hash) -} - -/// Builds and submits a single transaction that both creates the deposit note -/// and publishes the zone inscription that consumes it. -pub async fn submit_atomic_zone_deposit( - node_url: &Url, - client: &SequencerClient, - request: AtomicZoneDepositRequest, -) -> Result { - let AtomicZoneDepositRequest { - channel_id, - funding_public_key, - available_utxos, - amount, - metadata, - inscription_data, - } = request; - let (transfer, reserved_inputs) = - build_atomic_deposit_transfer(available_utxos, funding_public_key, amount)?; - let deposit = build_atomic_deposit_op(channel_id, metadata, &transfer)?; - - let fork_digest = CommonHttpClient::new(None) - .chain_fork(node_url.clone()) - .await - .map_err(|error| ZoneTestError::BuildAtomicDeposit { - message: format!("fork digest request failed: {error}"), - })?; - let (ops, msg_id, sequencer_sig) = client - .prepare_tx( - Ops::new( - fork_digest, - [Op::Transfer(transfer), Op::ChannelDeposit(deposit.clone())], - ), - inscription_data, - ) - .await - .map_err(|error| ZoneTestError::BuildAtomicDeposit { - message: error.to_string(), - })?; - - let user_sig = sign_tx_zk(node_url, &ops, vec![funding_public_key]).await?; - let op_proofs = OpProofs::from([ - OpProof::ZkSig(user_sig.clone()), - OpProof::ZkSig(user_sig), - OpProof::Ed25519Sig(sequencer_sig), - ]); - - let signed_tx = SignedOps::from_parts(ops, op_proofs).map_err(|error| { - ZoneTestError::BuildAtomicDeposit { - message: format!("signed ops assembly failed: {error}"), - } - })?; - - let (result, _cp) = client - .submit_signed_tx(signed_tx, msg_id) - .await - .map_err(|error| ZoneTestError::SubmitAtomicDeposit { - message: error.to_string(), - })?; - - Ok(AtomicZoneDepositSubmission { - deposit, - publish: result, - reserved_inputs, - }) -} - -pub(super) async fn build_funded_custom_tx( - node_client: &NodeHttpClient, - channel_id: ChannelId, - signing_key: &Ed25519Key, - funding_pk: ZkPublicKey, - payloads: &[Inscription], - mut parent: MsgId, -) -> Result<(SignedOps, MsgId), ZoneTestError> { - let signer = signing_key.public_key().into_unverified(); - let mut tx_builder = MantleTxBuilder::new(); - for payload in payloads { - let op = InscriptionOp { - channel_id, - inscription: payload.clone(), - parent, - signer, - }; - parent = op.id(); - tx_builder = tx_builder - .push_op(Op::ChannelInscribe(op)) - .map_err(|error| ZoneTestError::BuildCustomTx { - message: format!("too many ops: {error}"), - })?; - } - - let response = node_client - .fund_tx(WalletFundRequestBody { - tip: None, - priority_fee_percent: 0, - tx_builder, - change_public_key: funding_pk, - funding_public_keys: vec![funding_pk], - max_tx_fee: GasCost::new(u64::MAX), - }) - .await - .map_err(|error| ZoneTestError::SubmitCustomTx { - message: format!("funding failed: {error}"), - })?; - - // Funding appends the fee transfer as the last op; every inscription is - // proven by the sequencer key over the funded tx hash. - let funded_tx = response.funded_tx; - let signature = signing_key.sign_payload(funded_tx.hash().as_signing_bytes()); - let mut op_proofs = OpProofs::try_from(vec![OpProof::Ed25519Sig(signature); payloads.len()]) - .map_err(|error| ZoneTestError::BuildCustomTx { - message: format!("too many operation proofs: {error:?}"), - })?; - - if let Some(proof) = response.transfer_proof { - op_proofs - .try_push(proof) - .map_err(|error| ZoneTestError::BuildCustomTx { - message: format!("too many operation proofs: {error:?}"), - })?; - } - - let signed_ops = SignedOps::from_parts(funded_tx, op_proofs).map_err(|error| { - ZoneTestError::BuildCustomTx { - message: format!("signed ops assembly failed: {error}"), - } - })?; - - Ok((signed_ops, parent)) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/runner.rs b/tests/zone-sdk/src/cucumber/steps/zone/runner.rs deleted file mode 100644 index 44f1d52..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/runner.rs +++ /dev/null @@ -1,268 +0,0 @@ -//! Single per-scenario drive loop for a -//! [`lb_zone_sdk::sequencer::ZoneSequencer`]. -//! -//! The drive task owns the sequencer and polls -//! [`ZoneSequencer::next_event`] in a loop, invoking a [`Policy`] inline for -//! each event. Step bodies issue commands via the SDK's -//! [`SequencerClient`] (handed out from [`Runtime::client`]) — those route -//! through the SDK's internal request channel and get processed by the same -//! drive loop, so reactions land before the next event is observed. -//! -//! Because the policy runs on the same task as event handling, reactions -//! (e.g. orphan republish) happen before the event is forwarded to tests — -//! there is no window where an event was observed but the policy's reaction -//! hasn't been applied to the SDK's state. - -use std::{ - collections::HashSet, - sync::{Arc, Mutex}, -}; - -use lb_core::mantle::{ops::channel::MsgId, transactions::hash::TxHash}; -pub use lb_zone_sdk::sequencer::{ - AtomicWithdrawInfo, ChannelUpdate, ChannelUpdateTx, DepositInfo, Error, Event, FinalizedOp, - FinalizedTx, FundingConfig, IndexedSignature, InscriptionId, InscriptionInfo, PendingTx, - PreparedAtomicBundle, PreparedChannelConfig, PublishResult, SequencerChannelView, - SequencerCheckpoint, SequencerClient, SequencerConfig, TurnNotification, WithdrawArg, - WithdrawInfo, WithdrawInputs, -}; -use lb_zone_sdk::{adapter, sequencer::ZoneSequencer}; -use tokio::{ - sync::{broadcast, watch}, - task::JoinHandle, -}; -use tracing::error; - -/// Inline policy executed on the drive task for each SDK event before the -/// event is forwarded to test observers. -/// -/// Implementations may call `sequencer.handle().publish(...)` to issue -/// state changes; those land on the same task that produced the event, -/// preserving observe-then-act ordering. -pub trait Policy: Send + 'static -where - Node: adapter::Node + Clone + Send + Sync + 'static, -{ - fn on_event( - &mut self, - sequencer: &mut ZoneSequencer, - event: &Event, - ) -> impl Future + Send; -} - -/// No-op policy — used when the test wants to drive the sequencer entirely -/// from step bodies via [`SequencerClient`]. -pub struct PassivePolicy; - -impl Policy for PassivePolicy -where - Node: adapter::Node + Clone + Send + Sync + 'static, -{ - async fn on_event(&mut self, _sequencer: &mut ZoneSequencer, _event: &Event) {} -} - -/// Bundle returned from [`spawn`]. -/// -/// Drive task handle + the SDK's cloneable [`SequencerClient`] + receivers -/// for events and the SDK's watch channels. `event_rx` is the SDK's broadcast -/// subscriber — fire-and-forget; consumers who fall behind get a -/// `RecvError::Lagged` they can recover from. -pub struct Runtime { - pub task: JoinHandle<()>, - /// The first channel-view contract violation the drive loop's checker - /// recorded; asserted by the `channel view contract holds` step. - pub view_violation: ViewViolation, - pub client: SequencerClient, - pub event_rx: broadcast::Receiver, - pub checkpoint_rx: watch::Receiver>, - pub ready_rx: watch::Receiver, - pub channel_view_rx: watch::Receiver, - pub turn_to_write_rx: watch::Receiver, -} - -/// Drive loop body. Owns the sequencer and pumps -/// [`ZoneSequencer::next_event`] until aborted. For each event invokes the -/// policy inline. Events reach observers via the SDK's own broadcast channel -/// (subscribed in [`spawn`]) — the drive loop does not forward them. -/// -/// Step-issued commands ride through the SDK's internal request channel and -/// are processed inside `next_event`; no separate command path is needed. -/// -/// Pulled out as a free async function so callers can spawn it themselves. -pub(super) async fn run( - mut sequencer: ZoneSequencer, - mut policy: P, - violation: ViewViolation, -) where - Node: adapter::Node + Clone + Send + Sync + 'static, - P: Policy, -{ - let mut view = ViewChecker::new(violation); - loop { - let ev = sequencer.next_event().await; - view.observe(&ev); - policy.on_event(&mut sequencer, &ev).await; - // Event already broadcast via the SDK's `emit_now`; nothing for the - // runner to forward. - } -} - -/// Asserts the [`ChannelUpdate`] contract on every block event. An extension -/// only adds entries the consumer does not hold. On a conflict, what a -/// consumer holds from the stream (adopted, not orphaned since, not -/// finalized) must equal `common_prefix ++ adopted`, up to the sequencer's own -/// in-flight publishes, which the prefix carries and the stream never echoes. -pub(super) struct ViewChecker { - held: HashSet, - violation: ViewViolation, -} - -/// The first contract violation a [`ViewChecker`] recorded. -pub type ViewViolation = Arc>>; - -impl ViewChecker { - pub(super) fn new(violation: ViewViolation) -> Self { - Self { - held: HashSet::new(), - violation, - } - } - - /// Keep the first violation; a broken invariant makes later checks noise. - fn record(&self, message: String) { - let mut violation = self.violation.lock().expect("checker mutex"); - if violation.is_none() { - error!("channel view contract violated: {message}"); - *violation = Some(message); - } - } - - pub(super) fn observe(&mut self, event: &Event) { - let Event::BlocksProcessed { - checkpoint, - channel_update, - finalized, - .. - } = event - else { - return; - }; - match channel_update { - ChannelUpdate::Extension { adopted } => self.add_extension(adopted), - ChannelUpdate::Conflict { - adopted, orphaned, .. - } => { - self.remove_orphaned(orphaned); - self.add_adopted(adopted); - } - } - self.remove_finalized(finalized); - self.check_view(channel_update, checkpoint); - } - - /// An extension only adds entries the consumer does not hold. - fn add_extension(&mut self, adopted: &[ChannelUpdateTx]) { - for tx in adopted { - if !self.held.insert(tx.tx_hash()) { - self.record(format!( - "{:?} adopted on an extension while already held", - tx.tx_hash() - )); - } - } - } - - fn remove_orphaned(&mut self, orphaned: &[ChannelUpdateTx]) { - if orphaned.is_empty() { - self.record("a Conflict with nothing orphaned".to_owned()); - } - for tx in orphaned { - self.held.remove(&tx.tx_hash()); - } - } - - fn add_adopted(&mut self, adopted: &[ChannelUpdateTx]) { - for tx in adopted { - self.held.insert(tx.tx_hash()); - } - } - - fn remove_finalized(&mut self, finalized: &[FinalizedTx]) { - for tx in finalized { - self.held.remove(&tx.tx_hash); - } - } - - /// On a conflict, `canonical_chain()` carries only what is held or in - /// flight, everything held, and every message after its parent. - fn check_view(&self, update: &ChannelUpdate, checkpoint: &SequencerCheckpoint) { - let Some(chain) = update.canonical_chain() else { - return; - }; - let chain: Vec<&ChannelUpdateTx> = chain.collect(); - let ids: HashSet = chain - .iter() - .filter_map(|tx| tx.inscription()) - .map(|info| info.this_msg) - .collect(); - let mut seen: HashSet = HashSet::new(); - for info in chain.iter().filter_map(|tx| tx.inscription()) { - if ids.contains(&info.parent_msg) && !seen.contains(&info.parent_msg) { - self.record(format!( - "{:?} listed before its parent {:?}", - info.this_msg, info.parent_msg - )); - } - seen.insert(info.this_msg); - } - - let view: HashSet = chain.iter().map(|tx| tx.tx_hash()).collect(); - let pending: HashSet = checkpoint.pending_txs.iter().map(|(h, _)| *h).collect(); - for tx in &view { - if !self.held.contains(tx) && !pending.contains(tx) { - self.record(format!( - "common_prefix carries {tx:?}, which was never adopted and is not pending" - )); - } - } - for tx in &self.held { - if !view.contains(tx) { - self.record(format!( - "{tx:?} was adopted and never orphaned or finalized, but left common_prefix" - )); - } - } - } -} - -/// Spawn the drive loop on the current tokio runtime. -/// -/// Grabs SDK watch subscriptions and the SDK client before moving the -/// sequencer into the task. Events flow via the SDK's broadcast channel — -/// `event_rx` is a fresh subscriber. -pub fn spawn(sequencer: ZoneSequencer, policy: P) -> Runtime -where - Node: adapter::Node + Clone + Send + Sync + 'static, - P: Policy, -{ - let checkpoint_rx = sequencer.subscribe_checkpoint(); - let ready_rx = sequencer.subscribe_ready(); - let channel_view_rx = sequencer.subscribe_channel_view(); - let turn_to_write_rx = sequencer.subscribe_turn_to_write(); - let event_rx = sequencer.subscribe_events(); - let client = sequencer.client(); - - let view_violation = ViewViolation::default(); - let task = tokio::spawn(run(sequencer, policy, Arc::clone(&view_violation))); - - Runtime { - task, - view_violation, - client, - event_rx, - checkpoint_rx, - ready_rx, - channel_view_rx, - turn_to_write_rx, - } -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/assertions.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/assertions.rs deleted file mode 100644 index c93acac..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/assertions.rs +++ /dev/null @@ -1,802 +0,0 @@ -use lb_core::mantle::{ - ledger::NoteId, - ops::{OpId as _, OpRef}, -}; - -use super::{ - CONCURRENT_DUPLICATE_SETTLE_SECS, DEFAULT_ZONE_SEQUENCER, Duration, HashMap, HashSet, - Inscription, Step, StepError, StepResult, ZoneWorld, assert_sorted_outcome, - collect_indexed_messages, collect_indexed_messages_exactly_once, ensure_zone_message_included, - ensure_zone_transactions_included, log_step_error, make_inscription, parse_balance_payload, - pin_payload, replay_finalized_history, scan_indexer_for_payloads, single_column_table, - wait_for_channel_transfer_input_count, wait_for_channel_wallet_counts, - wait_for_channel_wallet_note, wait_for_deposit, wait_for_exact_indexed_payload_count, - wait_for_finalized_deposit_via_sequencer, wait_for_finalized_withdraw_via_sequencer, - wait_for_indexer_unordered, wait_for_transactions_finalized, wait_for_withdraw, - wait_until_sorted_conflict_settles, withdraw_payload, zone_step_error, -}; - -#[cucumber::then("the channel view contract holds for all zone sequencers")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -fn step_channel_view_contract_holds(world: &mut ZoneWorld) -> StepResult { - let violations = world.zone.view_violations(); - if violations.is_empty() { - return Ok(()); - } - Err(StepError::LogicalError { - message: violations - .iter() - .map(|(alias, message)| format!("{alias}: {message}")) - .collect::>() - .join("; "), - }) -} - -#[cucumber::then(expr = "all zone messages are safe in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_all_zone_messages_are_safe( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let inscription_ids = log_step_error(step, world.zone.ordered_inscription_ids())?; - - if !world.zone.has_published_messages() { - return Err(StepError::LogicalError { - message: "No zone messages have been published".to_owned(), - }); - } - - let node = log_step_error(step, world.zone_node_http_client())?; - - ensure_zone_transactions_included( - &node, - &inscription_ids, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then(expr = "all zone messages are finalized in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_all_zone_messages_are_finalized( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let inscription_ids = log_step_error(step, world.zone.ordered_inscription_ids())?; - - if !world.zone.has_published_messages() { - return Err(StepError::LogicalError { - message: "No zone messages have been published".to_owned(), - }); - } - - let node_url = log_step_error(step, world.zone_node_url())?; - - wait_for_transactions_finalized( - node_url, - &inscription_ids, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then("the zone indexer returns messages in this order:")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_messages_in_order( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let aliases = single_column_table(step, "alias", "zone message aliases")?; - let expected = log_step_error(step, world.zone.message_payloads_for_aliases(&aliases))?; - let indexer = log_step_error(step, world.zone.indexer())?; - - let actual = collect_indexed_messages(indexer, &expected, Duration::from_mins(3)) - .await - .map_err(|error| zone_step_error(step, &error))?; - - if actual == expected { - return Ok(()); - } - - Err(StepError::LogicalError { - message: format!( - "Zone indexer returned messages in unexpected order: expected {} messages, got {}", - expected.len(), - actual.len() - ), - }) -} - -#[cucumber::then(expr = "the zone indexer returns messages in any order in {int} seconds:")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_messages_in_any_order( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let aliases = single_column_table(step, "alias", "zone message aliases")?; - let expected = log_step_error(step, world.zone.message_payloads_for_aliases(&aliases))?; - let expected = expected.into_iter().collect::>(); - let indexer = log_step_error(step, world.zone.indexer())?; - - wait_for_indexer_unordered(indexer, &expected, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error))?; - - Ok(()) -} - -#[cucumber::then("the zone indexer returns each of these messages exactly once in this order:")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_messages_exactly_once_in_order( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let aliases = single_column_table(step, "alias", "zone message aliases")?; - let expected = log_step_error(step, world.zone.message_payloads_for_aliases(&aliases))?; - let indexer = log_step_error(step, world.zone.indexer())?; - - let actual = collect_indexed_messages_exactly_once(indexer, &expected, Duration::from_mins(3)) - .await - .map_err(|error| zone_step_error(step, &error))?; - - if actual == expected { - return Ok(()); - } - - Err(StepError::LogicalError { - message: format!( - "Zone indexer returned duplicate or out-of-order messages: expected {} messages, got {}", - expected.len(), - actual.len() - ), - }) -} - -#[cucumber::then(expr = "zone transaction {string} is included in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_transaction_is_included( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - timeout_seconds: u64, -) -> StepResult { - let tx_hash = world - .node - .resolve_submitted_transaction(&transaction_alias)?; - let node = log_step_error(step, world.zone_node_http_client())?; - - ensure_zone_transactions_included(&node, &[tx_hash], Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -/// The message is mined on the named node's tip chain, under whatever tx -/// hash carries it. Use it where a scenario needs the inscription in a block -/// before the next step acts, instead of a height wait that may already be -/// satisfied. -#[cucumber::when( - expr = "zone message {string} from sequencer {string} is included on node {string} in {int} seconds" -)] -#[cucumber::then( - expr = "zone message {string} from sequencer {string} is included on node {string} in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_message_is_included_on_node( - world: &mut ZoneWorld, - step: &Step, - message_alias: String, - sequencer_alias: String, - node_name: String, - timeout_seconds: u64, -) -> StepResult { - let msg_id = log_step_error(step, world.zone.published_message(&message_alias))? - .msg_id - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone message '{message_alias}' has no tracked message id"), - })?; - let channel_id = world.zone.sequencer_channel_id(&sequencer_alias)?; - let node = world.node.resolve_node_http_client(&node_name)?; - - ensure_zone_message_included( - &node, - channel_id, - msg_id, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then(expr = "zone transaction {string} is finalized in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_transaction_is_finalized( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - timeout_seconds: u64, -) -> StepResult { - let tx_hash = world - .node - .resolve_submitted_transaction(&transaction_alias)?; - let node_url = log_step_error(step, world.zone_node_url())?; - - wait_for_transactions_finalized(node_url, &[tx_hash], Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then(expr = "the zone indexer returns finalized deposit {string} in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_finalized_deposit( - world: &mut ZoneWorld, - step: &Step, - deposit_alias: String, - timeout_seconds: u64, -) -> StepResult { - let (deposit, amount) = world - .zone - .resolve_submitted_deposit(&deposit_alias)? - .clone(); - let indexer = log_step_error(step, world.zone.indexer())?; - - wait_for_deposit( - indexer, - &deposit, - amount, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then(expr = "the zone indexer returns finalized withdraw {string} in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_finalized_withdraw( - world: &mut ZoneWorld, - step: &Step, - withdraw_alias: String, - timeout_seconds: u64, -) -> StepResult { - let withdraw = world - .zone - .resolve_submitted_withdraw(&withdraw_alias)? - .clone(); - let indexer = log_step_error(step, world.zone.indexer())?; - - wait_for_withdraw(indexer, &withdraw, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then( - expr = "the zone indexer returns a finalized channel transfer consuming {int} inputs in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_finalized_channel_transfer_input_count( - world: &mut ZoneWorld, - step: &Step, - expected_inputs: usize, - timeout_seconds: u64, -) -> StepResult { - let indexer = log_step_error(step, world.zone.indexer())?; - - wait_for_channel_transfer_input_count( - indexer, - expected_inputs, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then( - expr = "the channel wallet of {string} contains a note of value {int} in {int} seconds" -)] -#[cucumber::when( - expr = "the channel wallet of {string} contains a note of value {int} in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_channel_wallet_contains_note( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - value: u64, - timeout_seconds: u64, -) -> StepResult { - let client = log_step_error(step, world.zone.sequencer_client(&sequencer_alias))?; - wait_for_channel_wallet_note(client, value, false, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then( - expr = "the channel wallet of {string} contains a finalized note of value {int} in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_channel_wallet_contains_finalized_note( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - value: u64, - timeout_seconds: u64, -) -> StepResult { - let client = log_step_error(step, world.zone.sequencer_client(&sequencer_alias))?; - wait_for_channel_wallet_note(client, value, true, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then( - expr = "the channel wallet of {string} has exactly {int} finalized and {int} unfinalized notes in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_channel_wallet_has_exact_counts( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - finalized: usize, - unfinalized: usize, - timeout_seconds: u64, -) -> StepResult { - let client = log_step_error(step, world.zone.sequencer_client(&sequencer_alias))?; - wait_for_channel_wallet_counts( - client, - finalized, - unfinalized, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then(expr = "sequencer {string} finalizes deposit {string} in {int} seconds")] -async fn step_zone_sequencer_finalizes_deposit( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - deposit_alias: String, - timeout_seconds: u64, -) -> StepResult { - let (deposit, amount) = world - .zone - .resolve_submitted_deposit(&deposit_alias)? - .clone(); - let events = log_step_error(step, world.zone.sequencer_events_mut(&sequencer_alias))?; - - wait_for_finalized_deposit_via_sequencer( - events, - &deposit, - amount, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - Ok(()) -} - -#[cucumber::then(expr = "sequencer {string} finalizes withdraw {string} in {int} seconds")] -async fn step_zone_sequencer_finalizes_withdraw( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - withdraw_alias: String, - timeout_seconds: u64, -) -> StepResult { - let withdraw = world - .zone - .resolve_submitted_withdraw(&withdraw_alias)? - .clone(); - let events = log_step_error(step, world.zone.sequencer_events_mut(&sequencer_alias))?; - - wait_for_finalized_withdraw_via_sequencer( - events, - &withdraw, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - Ok(()) -} - -#[cucumber::then( - expr = "the zone indexer returns all zone messages exactly once in any order in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_all_messages_exactly_once_any_order( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let expected_set = published_payload_set(world, step)?; - let indexer = log_step_error(step, world.zone.indexer())?; - - let seen = - wait_for_indexer_unordered(indexer, &expected_set, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error))?; - - tokio::time::sleep(Duration::from_secs(CONCURRENT_DUPLICATE_SETTLE_SECS)).await; - - let all_payloads = scan_indexer_for_payloads(indexer, &expected_set) - .await - .map_err(|error| zone_step_error(step, &error))?; - - ensure_indexed_payloads_match_once(&expected_set, &seen, &all_payloads) -} - -/// The message finalized, but not as the tx the publish returned: the SDK -/// re-funded it under a new hash after the original was stuck. -#[cucumber::then(expr = "zone message {string} finalized under a different tx hash than submitted")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_message_finalized_under_a_rebuilt_tx( - world: &mut ZoneWorld, - step: &Step, - message_alias: String, -) -> StepResult { - let message = log_step_error(step, world.zone.published_message(&message_alias))?; - let submitted = message - .inscription_id - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone message '{message_alias}' has no submitted tx hash"), - })?; - let payload = message.payload.clone(); - let reader = log_step_error(step, world.zone.indexer())?; - let history = replay_finalized_history(reader) - .await - .map_err(|error| zone_step_error(step, &error))?; - - let finalized_as: Vec<_> = history - .iter() - .filter(|tx| { - tx.ops.iter().any(|op| { - matches!(op, lb_zone_sdk::sequencer::FinalizedOp::Inscription(info) if info.payload == payload) - }) - }) - .map(|tx| tx.tx_hash) - .collect(); - if finalized_as.is_empty() { - return Err(StepError::LogicalError { - message: format!("Zone message '{message_alias}' is not finalized"), - }); - } - if finalized_as.contains(&submitted) { - return Err(StepError::LogicalError { - message: format!( - "Zone message '{message_alias}' finalized as the tx it was submitted with; nothing was re-funded" - ), - }); - } - Ok(()) -} - -/// Messages whose checkpointed txs spend a common fee note: at most one of -/// them finalizes as submitted, every other one under a rebuilt tx hash. -/// Messages sharing no note have nothing to re-fund and pass. -#[cucumber::then( - expr = "zone messages double-handed in checkpoint {string} finalized with the losers re-funded:" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_double_handed_messages_are_refunded( - world: &mut ZoneWorld, - step: &Step, - checkpoint_alias: String, -) -> StepResult { - let aliases = single_column_table(step, "alias", "zone message aliases")?; - let checkpoint = log_step_error(step, world.zone.resolve_checkpoint(&checkpoint_alias))?; - let mut messages = Vec::new(); - for alias in aliases { - let message = log_step_error(step, world.zone.published_message(&alias))?; - let submitted = message - .inscription_id - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone message '{alias}' has no submitted tx hash"), - })?; - let fee_notes: HashSet = checkpoint - .pending_txs - .iter() - .filter(|(hash, _)| *hash == submitted) - .flat_map(|(_, tx)| tx.op_refs()) - .filter_map(|op| match op { - OpRef::Transfer(transfer) => Some(transfer.inputs.iter().copied()), - _ => None, - }) - .flatten() - .collect(); - messages.push((alias, submitted, message.payload.clone(), fee_notes)); - } - - let reader = log_step_error(step, world.zone.indexer())?; - let history = replay_finalized_history(reader) - .await - .map_err(|error| zone_step_error(step, &error))?; - let finalized_as = |payload: &Inscription| { - history - .iter() - .filter(|tx| { - tx.ops.iter().any(|op| { - matches!(op, lb_zone_sdk::sequencer::FinalizedOp::Inscription(info) if info.payload == *payload) - }) - }) - .map(|tx| tx.tx_hash) - .collect::>() - }; - - for (index, (alias, submitted, payload, fee_notes)) in messages.iter().enumerate() { - let rivals: Vec<&str> = messages - .iter() - .enumerate() - .filter(|(other, (_, _, _, notes))| *other != index && !notes.is_disjoint(fee_notes)) - .map(|(_, (alias, ..))| alias.as_str()) - .collect(); - if rivals.is_empty() { - continue; - } - let finalized = finalized_as(payload); - if finalized.is_empty() { - return Err(StepError::LogicalError { - message: format!( - "Zone message '{alias}' shares a fee note with {rivals:?} and is not finalized" - ), - }); - } - if finalized.contains(submitted) { - let other_winner = messages - .iter() - .filter(|(other, other_submitted, other_payload, _)| { - rivals.contains(&other.as_str()) - && finalized_as(other_payload).contains(other_submitted) - }) - .map(|(other, ..)| other.as_str()) - .next(); - if let Some(other) = other_winner { - return Err(StepError::LogicalError { - message: format!( - "Zone messages '{alias}' and '{other}' both finalized as submitted while spending the same fee note" - ), - }); - } - } - } - Ok(()) -} - -fn published_payload_set( - world: &ZoneWorld, - step: &Step, -) -> Result, StepError> { - let expected_payloads = log_step_error(step, world.zone.published_message_payloads())?; - - Ok(expected_payloads.into_iter().collect()) -} - -fn ensure_indexed_payloads_match_once( - expected: &HashSet, - seen: &HashSet, - all_payloads: &[Inscription], -) -> StepResult { - let unique: HashSet<&Inscription> = all_payloads.iter().collect(); - - if unique.len() != all_payloads.len() { - return Err(StepError::LogicalError { - message: format!( - "Duplicate inscriptions detected on chain: expected {} unique, got {} total", - unique.len(), - all_payloads.len() - ), - }); - } - - if unique.len() != expected.len() || seen != expected { - return Err(StepError::LogicalError { - message: format!( - "Zone indexer did not return the expected message set: expected {}, got {}", - expected.len(), - unique.len() - ), - }); - } - - Ok(()) -} - -#[cucumber::then( - expr = "the zone indexer returns exactly one finalized pin and withdraw for deposit {string} in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_exact_reactive_lifecycle( - world: &mut ZoneWorld, - step: &Step, - deposit_alias: String, - timeout_seconds: u64, -) -> StepResult { - // Assert each reactive-policy payload for this op_id appears exactly once: - // no missing phase, no surviving duplicate from a benign double-publish. - let op_id = log_step_error(step, world.zone.resolve_submitted_deposit(&deposit_alias))? - .0 - .op_id(); - let indexer = log_step_error(step, world.zone.indexer())?; - let duration = Duration::from_secs(timeout_seconds); - wait_for_exact_indexed_payload_count(indexer, pin_payload(&op_id), 1, duration) - .await - .map_err(|error| zone_step_error(step, &error))?; - wait_for_exact_indexed_payload_count(indexer, withdraw_payload(&op_id), 1, duration) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then( - expr = "the zone indexer returns {int} copies of zone message {string} in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_payload_count( - world: &mut ZoneWorld, - step: &Step, - expected_count: usize, - payload: String, - timeout_seconds: u64, -) -> StepResult { - let indexer = log_step_error(step, world.zone.indexer())?; - wait_for_exact_indexed_payload_count( - indexer, - make_inscription(&payload), - expected_count, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[cucumber::then(expr = "zone balance updates keep all accounts non-negative after {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_balance_updates_keep_accounts_non_negative( - world: &mut ZoneWorld, - step: &Step, - settle_seconds: u64, -) -> StepResult { - tokio::time::sleep(Duration::from_secs(settle_seconds)).await; - - let mut balances = world.zone.zone_account_balances()?; - let expected_set = published_payload_set(world, step)?; - let indexer = log_step_error(step, world.zone.indexer())?; - let on_chain = scan_indexer_for_payloads(indexer, &expected_set) - .await - .map_err(|error| zone_step_error(step, &error))?; - - apply_indexed_balance_updates(&mut balances, &on_chain); - - ensure_balances_non_negative(&balances) -} - -#[cucumber::then( - expr = "the zone indexer preserves per-sequencer order and converges without duplicates in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_preserves_per_sequencer_order_without_duplicates( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let expected_set = published_payload_set(world, step)?; - let total = world.zone.sorted_total_payloads()?; - let expected_by_sequencer = world.zone.sorted_expected_by_sequencer()?; - let discarded = log_step_error(step, world.zone.discarded_payloads(DEFAULT_ZONE_SEQUENCER))?; - let indexer = log_step_error(step, world.zone.indexer())?; - - let on_chain = wait_until_sorted_conflict_settles( - indexer, - &expected_set, - &discarded, - total, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - let discarded_snapshot = discarded.lock().await.clone(); - assert_sorted_outcome( - &on_chain, - &discarded_snapshot, - total, - &expected_by_sequencer, - ) -} - -fn apply_indexed_balance_updates(balances: &mut HashMap, payloads: &[Inscription]) { - for payload in payloads { - let Some((_, account, delta)) = parse_balance_payload(payload) else { - continue; - }; - - *balances.entry(account).or_default() += delta; - } -} - -fn ensure_balances_non_negative(balances: &HashMap) -> StepResult { - let negative = balances - .iter() - .filter(|(_, balance)| **balance < 0) - .map(|(account, balance)| format!("{account}={balance}")) - .collect::>(); - - if negative.is_empty() { - return Ok(()); - } - - Err(StepError::LogicalError { - message: format!( - "Zone account balances went negative: {}", - negative.join(", ") - ), - }) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/channel.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/channel.rs deleted file mode 100644 index 406f09f..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/channel.rs +++ /dev/null @@ -1,347 +0,0 @@ -use lb_core::mantle::ops::channel::deposit::Metadata; - -use super::{ - Step, StepResult, ZoneWorld, prepare_zone_channel_config, - publish_atomic_zone_withdraw_transaction, save_zone_checkpoint, - sign_prepared_zone_channel_config, single_column_table, start_named_sequencer_with_startup, - submit_atomic_zone_deposit_transaction, submit_prepared_zone_channel_config, - submit_zone_channel_config, submit_zone_channel_split_transaction, - submit_zone_deposit_transaction, submit_zone_multi_deposit_transaction, - submit_zone_withdraw_transaction, when, zone_atomic_withdraw_rows, zone_config_row, -}; - -#[when(expr = "I save current checkpoint of sequencer {string} as {string}")] -fn step_save_zone_checkpoint( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - checkpoint_alias: String, -) -> StepResult { - save_zone_checkpoint(world, step, sequencer_alias, checkpoint_alias) -} - -#[when(expr = "I restart zone sequencer {string} from checkpoint {string}")] -async fn step_restart_zone_sequencer_from_checkpoint( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - checkpoint_alias: String, -) -> StepResult { - let checkpoint = world.zone.resolve_checkpoint(checkpoint_alias)?; - let startup = world.zone.sequencer_startup_for(&sequencer_alias); - - start_named_sequencer_with_startup(world, step, &sequencer_alias, Some(checkpoint), startup) - .await -} - -#[when(expr = "I restart zone sequencer {string} fresh")] -async fn step_restart_zone_sequencer_fresh( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - let startup = world.zone.sequencer_startup_for(&sequencer_alias); - start_named_sequencer_with_startup(world, step, &sequencer_alias, None, startup).await -} - -#[when(expr = "sequencer {string} submits zone config transaction {string} authorizing:")] -async fn step_submit_zone_channel_config_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, -) -> StepResult { - let authorized_aliases = - single_column_table(step, "alias", "authorized zone sequencer aliases")?; - - submit_zone_channel_config( - world, - step, - &sequencer_alias, - transaction_alias, - authorized_aliases, - 0, - 0, - ) - .await -} - -#[when( - expr = "sequencer {string} submits zone config transaction {string} with posting timeframe {int} and timeout {int} authorizing:" -)] -async fn step_submit_zone_channel_config_transaction_with_posting_window( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, - posting_timeframe: u32, - posting_timeout: u32, -) -> StepResult { - let authorized_aliases = - single_column_table(step, "alias", "authorized zone sequencer aliases")?; - - submit_zone_channel_config( - world, - step, - &sequencer_alias, - transaction_alias, - authorized_aliases, - posting_timeframe, - posting_timeout, - ) - .await -} - -#[when( - expr = "sequencer {string} prepares zone config transaction {string} with threshold {int} authorizing:" -)] -async fn step_prepare_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, - threshold: u16, -) -> StepResult { - let authorized_aliases = - single_column_table(step, "alias", "authorized zone sequencer aliases")?; - - prepare_zone_channel_config( - world, - step, - &sequencer_alias, - transaction_alias, - authorized_aliases, - threshold, - 0, - ) - .await -} - -#[when( - expr = "sequencer {string} prepares zone config transaction {string} with threshold {int} and posting timeframe {int} authorizing:" -)] -async fn step_prepare_zone_channel_config_with_timeframe( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, - threshold: u16, - posting_timeframe: u32, -) -> StepResult { - let authorized_aliases = - single_column_table(step, "alias", "authorized zone sequencer aliases")?; - - prepare_zone_channel_config( - world, - step, - &sequencer_alias, - transaction_alias, - authorized_aliases, - threshold, - posting_timeframe, - ) - .await -} - -#[when(expr = "sequencer {string} signs prepared zone config transaction {string}")] -#[expect( - clippy::needless_pass_by_value, - reason = "Cucumber string captures are provided as owned `String`s" -)] -fn step_sign_prepared_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - signer_alias: String, - transaction_alias: String, -) -> StepResult { - sign_prepared_zone_channel_config(world, step, &signer_alias, transaction_alias) -} - -#[when(expr = "sequencer {string} submits prepared zone config transaction {string}")] -async fn step_submit_prepared_zone_channel_config( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, -) -> StepResult { - submit_prepared_zone_channel_config(world, step, &sequencer_alias, transaction_alias).await -} - -#[when(expr = "sequencer {string} submits zone config transaction:")] -async fn step_submit_zone_channel_config_transaction_from_table( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - let row = zone_config_row(step)?; - - submit_zone_channel_config( - world, - step, - &sequencer_alias, - row.config_name, - row.authorized_sequencers, - row.posting_timeframe, - row.posting_timeout, - ) - .await -} - -#[when( - expr = "I submit zone deposit transaction {string} into channel of {string} of {int} with metadata {string}" -)] -async fn step_submit_zone_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - channel_alias: String, - amount: u64, - metadata: String, -) -> StepResult { - submit_zone_deposit_transaction( - world, - step, - transaction_alias, - channel_alias, - amount, - Metadata::new_unchecked(metadata.into_bytes()), - ) - .await -} - -#[when( - expr = "I submit zone deposit transaction {string} into channel of {string} consuming notes valued {string} with metadata {string}" -)] -async fn step_submit_zone_multi_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - channel_alias: String, - values: String, - metadata: String, -) -> StepResult { - let input_values = values - .split(',') - .map(|part| part.trim().parse::()) - .collect::, _>>() - .expect("deposit note values must be a comma-separated list of integers"); - submit_zone_multi_deposit_transaction( - world, - step, - transaction_alias, - channel_alias, - input_values, - Metadata::new_unchecked(metadata.into_bytes()), - ) - .await -} - -#[when( - expr = "I submit zone deposit transaction {string} into channel of {string} consuming {int} notes of value {int} with metadata {string}" -)] -async fn step_submit_zone_bulk_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - transaction_alias: String, - channel_alias: String, - count: usize, - value: u64, - metadata: String, -) -> StepResult { - let input_values = vec![value; count]; - submit_zone_multi_deposit_transaction( - world, - step, - transaction_alias, - channel_alias, - input_values, - Metadata::new_unchecked(metadata.into_bytes()), - ) - .await -} - -#[when(expr = "sequencer {string} splits deposit {string} into {int} dust notes as {string}")] -async fn step_submit_zone_channel_split_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - deposit_alias: String, - dust_count: usize, - transaction_alias: String, -) -> StepResult { - submit_zone_channel_split_transaction( - world, - step, - &sequencer_alias, - &deposit_alias, - dust_count, - transaction_alias, - ) - .await -} - -#[when( - expr = "sequencer {string} submits atomic zone deposit transaction {string} with inscription {string} of {int} with metadata {string}" -)] -async fn step_submit_atomic_zone_deposit_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, - message_alias: String, - amount: u64, - metadata: String, -) -> StepResult { - submit_atomic_zone_deposit_transaction( - world, - step, - &sequencer_alias, - transaction_alias, - message_alias, - amount, - Metadata::new_unchecked(metadata.into_bytes()), - ) - .await -} - -#[when( - expr = "sequencer {string} submits zone withdraw transaction {string} with inscription {string} of {int}" -)] -async fn step_submit_zone_withdraw_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - transaction_alias: String, - message_alias: String, - amount: u64, -) -> StepResult { - submit_zone_withdraw_transaction( - world, - step, - &sequencer_alias, - transaction_alias, - message_alias, - amount, - ) - .await -} - -#[when(expr = "sequencer {string} publishes atomic withdraw {string} with inscription {string}:")] -async fn step_publish_atomic_zone_withdraw_transaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - bundle_alias: String, - message_alias: String, -) -> StepResult { - let withdraw_rows = zone_atomic_withdraw_rows(step)?; - publish_atomic_zone_withdraw_transaction( - world, - step, - &sequencer_alias, - bundle_alias, - message_alias, - withdraw_rows, - ) - .await -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/lifecycle.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/lifecycle.rs deleted file mode 100644 index 0f8c031..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/lifecycle.rs +++ /dev/null @@ -1,241 +0,0 @@ -use super::{ - BundleAnnounce, DriveMode, Duration, MultiSigBus, Step, StepError, StepResult, - ZoneSequencerStartup, ZoneWorld, given, initialize_zone_indexer, log_step_error, - parse_optional_submit_depth, register_zone_sequencers_with_shared_key, single_column_table, - start_deposit_reaction_sequencer, start_deposit_withdraw_sequencer, - start_multisig_lifecycle_sequencer, start_named_sequencer, start_named_sequencer_with_startup, - start_nodes_with_zone_resources, stop_zone_sequencer, wait_for_lib_advance, when, - zone_account_balances, zone_node_resource_rows, zone_sequencer_start_rows, zone_step_error, -}; - -#[given("I start nodes with wallet and sequencer resources:")] -#[when("I start nodes with wallet and sequencer resources:")] -async fn step_start_nodes_with_wallet_and_sequencer_resources( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let rows = zone_node_resource_rows(step)?; - - start_nodes_with_zone_resources(world, step, rows).await -} - -#[given(expr = "the following zone sequencers share the signing key of {string}:")] -#[expect( - clippy::needless_pass_by_value, - reason = "Cucumber string captures are provided as owned `String`s" -)] -fn step_zone_sequencers_share_signing_key( - world: &mut ZoneWorld, - step: &Step, - source_alias: String, -) -> StepResult { - let aliases = single_column_table(step, "alias", "zone sequencer aliases")?; - register_zone_sequencers_with_shared_key(world, &source_alias, aliases) -} - -#[given("the following zone account balances exist:")] -fn step_zone_account_balances(world: &mut ZoneWorld, step: &Step) -> StepResult { - let balances = zone_account_balances(step)? - .into_iter() - .map(|row| (row.account, row.balance)) - .collect(); - - world.zone.set_zone_account_balances(balances); - - Ok(()) -} - -#[when(expr = "I start zone sequencer {string}")] -async fn step_start_zone_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - start_named_sequencer(world, step, sequencer_alias, None, DriveMode::passive()).await -} - -#[when(expr = "I start zone sequencer {string} with indexer")] -async fn step_start_zone_sequencer_with_indexer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - start_sequencer_with_indexer(world, step, &sequencer_alias).await -} - -async fn start_sequencer_with_indexer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, -) -> StepResult { - start_named_sequencer(world, step, sequencer_alias, None, DriveMode::passive()).await?; - initialize_zone_indexer(world, step, sequencer_alias) -} - -#[when( - expr = "I start zone sequencer {string} pinning then withdrawing observed deposits with outputs {string}" -)] -async fn step_start_zone_sequencer_deposit_reaction( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - outputs: String, -) -> StepResult { - let withdraw_outputs = outputs - .split(',') - .map(|amount| { - amount - .trim() - .parse::() - .map_err(|error| StepError::InvalidArgument { - message: format!("invalid withdraw output amount '{amount}': {error}"), - }) - }) - .collect::, _>>()?; - start_deposit_reaction_sequencer(world, step, &sequencer_alias, withdraw_outputs).await -} - -#[cucumber::when( - expr = "I start multi-sig lifecycle zone sequencers with withdraw outputs {string}:" -)] -async fn step_start_multisig_lifecycle_sequencers( - world: &mut ZoneWorld, - step: &Step, - outputs: String, -) -> StepResult { - let withdraw_outputs = outputs - .split(',') - .map(|amount| { - amount - .trim() - .parse::() - .map_err(|error| StepError::InvalidArgument { - message: format!("invalid withdraw output amount '{amount}': {error}"), - }) - }) - .collect::, _>>()?; - let aliases = single_column_table(step, "alias", "zone sequencer aliases")?; - // Shared gossip stand-in: `bus` holds signatures, `announce` fans out bundles. - let bus = MultiSigBus::default(); - let (announce, _): (BundleAnnounce, _) = tokio::sync::broadcast::channel(256); - // Readiness barrier: each start awaits the SDK's ready signal before the - // next alias begins, and the policy takes its `announce` subscription - // synchronously before its task is spawned. So when the loop exits, every - // peer is ready and subscribed — no announcement can be broadcast into an - // empty channel. - for alias in &aliases { - start_multisig_lifecycle_sequencer( - world, - step, - alias, - withdraw_outputs.clone(), - std::sync::Arc::clone(&bus), - announce.clone(), - ) - .await?; - } - // Make the barrier checkable: one live subscriber per spawned policy. - // (The channel's initial receiver was dropped above, so the count is - // exactly the policies'.) - let subscribed = announce.receiver_count(); - if subscribed != aliases.len() { - return Err(StepError::LogicalError { - message: format!( - "expected {} multi-sig policies subscribed to the announce channel, found \ - {subscribed}", - aliases.len() - ), - }); - } - Ok(()) -} - -#[when( - expr = "I start zone sequencer {string} withdrawing observed deposit of {int} with outputs {string}" -)] -async fn step_start_zone_sequencer_deposit_withdraw( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - target_amount: u64, - outputs: String, -) -> StepResult { - let withdraw_outputs = outputs - .split(',') - .map(|amount| { - amount - .trim() - .parse::() - .map_err(|error| StepError::InvalidArgument { - message: format!("invalid withdraw output amount '{amount}': {error}"), - }) - }) - .collect::, _>>()?; - start_deposit_withdraw_sequencer( - world, - step, - &sequencer_alias, - target_amount, - withdraw_outputs, - ) - .await -} - -#[when("I start zone sequencers:")] -async fn step_start_zone_sequencers(world: &mut ZoneWorld, step: &Step) -> StepResult { - for row in zone_sequencer_start_rows(step)? { - let alias = row.alias; - let startup = ZoneSequencerStartup { - pending_submit_depth: parse_optional_submit_depth(step, &row.pending_submit_depth)?, - passive_republish_orphans: row.passive_republish_orphans, - }; - world.zone.set_sequencer_startup(&alias, startup); - - start_named_sequencer_with_startup(world, step, &alias, None, startup).await?; - - if row.indexer { - initialize_zone_indexer(world, step, &alias)?; - } - } - - Ok(()) -} - -#[when(expr = "I stop zone sequencer {string}")] -fn step_stop_zone_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - let _ = step; - stop_zone_sequencer(world, sequencer_alias) -} - -#[cucumber::when(expr = "the zone LIB advances in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_lib_advances( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let client = log_step_error(step, world.zone_node_http_client())?; - let initial_lib_slot = client - .consensus_info() - .await - .map_err(|error| StepError::LogicalError { - message: format!("Failed to fetch zone consensus info: {error}"), - })? - .cryptarchia_info - .lib_slot; - - wait_for_lib_advance( - &client, - initial_lib_slot, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/mod.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/mod.rs deleted file mode 100644 index 6d9c416..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/mod.rs +++ /dev/null @@ -1,127 +0,0 @@ -use std::{ - collections::{HashMap, HashSet, VecDeque}, - sync::Arc, - time::Duration, -}; - -use cucumber::{gherkin::Step, given, when}; -use lb_core::mantle::ops::channel::inscribe::Inscription; -use logos_blockchain_tests::{ - common::mantle_inscription::make_inscription, - cucumber::{ - error::{StepError, StepResult}, - steps::parse_steps::single_column_table, - }, -}; - -use super::{ - BundleAnnounce, CustomRepublishDeps, MultiSigBus, PublishDeadline, - actions::{ - DriveMode, initialize_zone_indexer, prepare_zone_channel_config, - publish_atomic_zone_withdraw_transaction, publish_zone_messages, - publish_zone_messages_concurrently, register_zone_sequencers_with_shared_key, - remember_published_zone_message, save_zone_checkpoint, sign_prepared_zone_channel_config, - start_deposit_reaction_sequencer, start_deposit_withdraw_sequencer, - start_multisig_lifecycle_sequencer, start_named_sequencer, - start_named_sequencer_with_pending_submit_depth, start_nodes_with_zone_resources, - stop_zone_sequencer, submit_atomic_zone_deposit_transaction, - submit_prepared_zone_channel_config, submit_zone_channel_config, - submit_zone_channel_split_transaction, submit_zone_deposit_transaction, - submit_zone_multi_deposit_transaction, submit_zone_withdraw_transaction, - }, - assertions::{ - assert_sorted_outcome, scan_indexer_for_payloads, wait_for_indexer_unordered, - wait_until_sorted_conflict_settles, - }, - balance_update_payload, collect_indexed_messages, collect_indexed_messages_exactly_once, - ensure_zone_message_included, ensure_zone_transactions_included, - errors::{log_step_error, zone_step_error}, - parse_balance_payload, pin_payload, publish_message_with_retry, replay_finalized_history, - tables::{ - ConcurrentZoneMessageRow, GeneratedZoneMessageBatch, concurrent_zone_message_rows, - custom_tx_rows, generated_zone_message_batches, generated_zone_message_sequencers, - group_zone_messages_by_sequencer, zone_account_balances, zone_atomic_withdraw_rows, - zone_balance_rows, zone_config_row, zone_message_rows, zone_node_resource_rows, - zone_sequencer_start_rows, zone_sequencing_state_row, - }, - wait_for_channel_transfer_input_count, wait_for_channel_view, wait_for_channel_wallet_counts, - wait_for_channel_wallet_note, wait_for_deposit, wait_for_exact_indexed_payload_count, - wait_for_finalized_deposit_via_sequencer, wait_for_finalized_withdraw_via_sequencer, - wait_for_lib_advance, wait_for_transactions_finalized, wait_for_turn_to_write, - wait_for_withdraw, withdraw_payload, -}; -use crate::cucumber::world::{ZoneSequencerStartup, ZoneWorld}; - -pub(super) const DEFAULT_ZONE_SEQUENCER: &str = "SEQ_A"; - -fn parse_submit_depth(step: &Step, value: &str) -> Result { - let value = value.trim(); - if matches!(value.to_lowercase().as_str(), "unlimited" | "none") { - return Ok(usize::MAX); - } - - let inner = value - .strip_prefix("Some(") - .and_then(|rest| rest.strip_suffix(')')) - .unwrap_or(value); - - let limit = inner - .parse::() - .map_err(|error| StepError::LogicalError { - message: format!( - "Invalid pending submit depth '{value}' in step '{}': {error}", - step.value - ), - })?; - - Ok(limit) -} - -fn parse_optional_submit_depth(step: &Step, value: &str) -> Result, StepError> { - let value = value.trim(); - if value.eq_ignore_ascii_case("default") { - return Ok(None); - } - - parse_submit_depth(step, value).map(Some) -} - -const fn passive_mode_for_startup(startup: ZoneSequencerStartup) -> DriveMode { - if startup.passive_republish_orphans { - DriveMode::passive_republish_orphans() - } else { - DriveMode::passive() - } -} - -async fn start_named_sequencer_with_startup( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: &str, - checkpoint: Option, - startup: ZoneSequencerStartup, -) -> StepResult { - let mode = passive_mode_for_startup(startup); - if let Some(submit_depth) = startup.pending_submit_depth { - start_named_sequencer_with_pending_submit_depth( - world, - step, - sequencer_alias, - checkpoint, - mode, - submit_depth, - ) - .await - } else { - start_named_sequencer(world, step, sequencer_alias, checkpoint, mode).await - } -} - -const CONCURRENT_DUPLICATE_SETTLE_SECS: u64 = 30; - -mod assertions; -mod channel; -mod lifecycle; -mod policies; -mod publishing; -mod sequencing; diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/policies.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/policies.rs deleted file mode 100644 index d62bee6..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/policies.rs +++ /dev/null @@ -1,260 +0,0 @@ -use super::{ - Arc, ConcurrentZoneMessageRow, DEFAULT_ZONE_SEQUENCER, DriveMode, GeneratedZoneMessageBatch, - HashMap, HashSet, Inscription, Step, StepResult, ZoneWorld, balance_update_payload, - concurrent_zone_message_rows, generated_zone_message_batches, - generated_zone_message_sequencers, group_zone_messages_by_sequencer, initialize_zone_indexer, - make_inscription, publish_zone_messages_concurrently, start_named_sequencer, - start_named_sequencer_with_pending_submit_depth, when, zone_balance_rows, -}; - -#[when("the following zone messages are published concurrently with republish policy:")] -async fn step_publish_zone_messages_concurrently_with_republish_policy( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let rows = concurrent_zone_message_rows(step)?; - publish_zone_messages_with_republish_policy(world, step, rows).await -} - -#[when( - expr = "each listed zone sequencer publishes {int} generated zone messages concurrently with republish policy:" -)] -async fn step_publish_generated_zone_messages_concurrently_with_republish_policy( - world: &mut ZoneWorld, - step: &Step, - messages_per_sequencer: usize, -) -> StepResult { - let rows = build_generated_zone_message_rows( - generated_zone_message_batches(step)?, - messages_per_sequencer, - ); - - publish_zone_messages_with_republish_policy(world, step, rows).await -} - -#[when( - expr = "each listed zone sequencer publishes {int} copies of zone message {string} concurrently with republish policy:" -)] -async fn step_publish_repeated_zone_messages_concurrently_with_republish_policy( - world: &mut ZoneWorld, - step: &Step, - copies_per_sequencer: usize, - payload: String, -) -> StepResult { - let rows = build_repeated_zone_message_rows( - generated_zone_message_sequencers(step)?, - copies_per_sequencer, - &payload, - ); - - publish_zone_messages_with_republish_policy(world, step, rows).await -} - -fn build_generated_zone_message_rows( - batches: Vec, - messages_per_sequencer: usize, -) -> Vec { - let mut builder = GeneratedZoneMessages::default(); - - for batch in batches { - builder.append_numbered_payloads(batch, messages_per_sequencer); - } - - builder.finish() -} - -fn build_repeated_zone_message_rows( - sequencer_aliases: Vec, - copies_per_sequencer: usize, - payload: &str, -) -> Vec { - let mut builder = GeneratedZoneMessages::default(); - let payload = make_inscription(payload); - - for sequencer_alias in sequencer_aliases { - builder.append_repeated_payloads(sequencer_alias, copies_per_sequencer, &payload); - } - - builder.finish() -} - -#[derive(Default)] -struct GeneratedZoneMessages { - next_message_number: usize, - rows: Vec, -} - -impl GeneratedZoneMessages { - fn append_numbered_payloads(&mut self, batch: GeneratedZoneMessageBatch, count: usize) { - let GeneratedZoneMessageBatch { - sequencer_alias, - data_prefix, - } = batch; - - for payload_number in 1..=count { - self.push( - sequencer_alias.clone(), - make_inscription(&format!("{data_prefix}{payload_number}")), - ); - } - } - - fn append_repeated_payloads( - &mut self, - sequencer_alias: String, - count: usize, - payload: &Inscription, - ) { - for _ in 1..count { - self.push(sequencer_alias.clone(), payload.clone()); - } - - if count > 0 { - self.push(sequencer_alias, payload.clone()); - } - } - - fn push(&mut self, sequencer_alias: String, payload: Inscription) { - self.next_message_number += 1; - - self.rows.push(ConcurrentZoneMessageRow { - sequencer_alias, - message_alias: format!("MSG_{}", self.next_message_number), - payload, - }); - } - - fn finish(self) -> Vec { - self.rows - } -} - -async fn publish_zone_messages_with_republish_policy( - world: &mut ZoneWorld, - step: &Step, - rows: Vec, -) -> StepResult { - let grouped = group_zone_messages_by_sequencer(&rows); - - for (sequencer_alias, messages) in &grouped { - let planned = messages - .iter() - .map(|message| message.payload.clone()) - .collect(); - start_named_sequencer_with_pending_submit_depth( - world, - step, - sequencer_alias, - None, - DriveMode::RepublishLineage { planned }, - usize::MAX, - ) - .await?; - } - - // Each sequencer's lineage policy owns publishing its own copies; the step - // only records the messages so the indexer assertions can find them. - for row in rows { - world - .zone - .remember_zone_message(row.message_alias, row.payload, None, None, None, None); - } - if world.zone.indexer().is_err() { - initialize_zone_indexer(world, step, DEFAULT_ZONE_SEQUENCER)?; - } - Ok(()) -} - -#[when("the following zone messages are published concurrently with sorted conflict policy:")] -async fn step_publish_zone_messages_concurrently_with_sorted_policy( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let rows = concurrent_zone_message_rows(step)?; - let grouped = group_zone_messages_by_sequencer(&rows); - let discarded = Arc::new(tokio::sync::Mutex::new(HashSet::new())); - - for sequencer_alias in grouped.keys() { - start_named_sequencer( - world, - step, - sequencer_alias, - None, - DriveMode::Sorted { - discarded: Arc::clone(&discarded), - }, - ) - .await?; - } - - world.zone.set_sorted_total_payloads(rows.len()); - world.zone.set_sorted_expected_by_sequencer( - grouped - .iter() - .map(|(sequencer_alias, messages)| { - ( - sequencer_alias.clone(), - messages - .iter() - .map(|message| message.payload.clone()) - .collect(), - ) - }) - .collect(), - ); - - publish_zone_messages_concurrently(world, step, rows).await -} - -#[when("the following zone balance updates are published concurrently with balance-aware policy:")] -async fn step_publish_zone_balance_updates_with_balance_policy( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let rows = zone_balance_rows(step)?; - let initial_balances = world.zone.zone_account_balances()?; - let grouped = rows.iter().fold( - HashMap::>::new(), - |mut grouped, row| { - let payload = balance_update_payload(&row.message_alias, &row.account, row.delta); - grouped - .entry(row.sequencer_alias.clone()) - .or_default() - .push((row.message_alias.clone(), payload)); - grouped - }, - ); - - for (sequencer_alias, planned) in &grouped { - start_named_sequencer( - world, - step, - sequencer_alias, - None, - DriveMode::BalanceAware { - initial_balances: initial_balances.clone(), - planned_payloads: planned.iter().map(|(_, payload)| payload.clone()).collect(), - }, - ) - .await?; - } - - for messages in grouped.values() { - for (message_alias, payload) in messages { - world.zone.remember_zone_message( - message_alias.clone(), - payload.clone(), - None, - None, - None, - None, - ); - } - } - - if world.zone.indexer().is_err() { - initialize_zone_indexer(world, step, DEFAULT_ZONE_SEQUENCER)?; - } - - Ok(()) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/publishing.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/publishing.rs deleted file mode 100644 index a8b8dd4..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/publishing.rs +++ /dev/null @@ -1,308 +0,0 @@ -use super::{ - CustomRepublishDeps, DEFAULT_ZONE_SEQUENCER, DriveMode, Duration, HashSet, Inscription, - PublishDeadline, Step, StepError, StepResult, VecDeque, ZoneWorld, custom_tx_rows, - log_step_error, make_inscription, publish_message_with_retry, publish_zone_messages, - remember_published_zone_message, start_named_sequencer, wait_for_channel_view, - wait_for_indexer_unordered, when, zone_message_rows, zone_step_error, -}; - -#[when("I publish the following zone messages:")] -async fn step_publish_zone_messages(world: &mut ZoneWorld, step: &Step) -> StepResult { - publish_zone_messages( - world, - step, - DEFAULT_ZONE_SEQUENCER, - zone_message_rows(step)?, - ) - .await -} - -#[when(expr = "sequencer {string} publishes the following zone messages:")] -async fn step_publish_zone_messages_for_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - publish_zone_messages(world, step, sequencer_alias, zone_message_rows(step)?).await -} - -/// Publishing while the sequencer's node is down must be rejected: with -/// funding configured, building a transaction needs the node's wallet, so -/// the sequencer fails fast with `Unavailable` once the stream drop is -/// noticed (or surfaces the funding error in the brief window before). A -/// fresh `Ready` event fires once the node is back and a live block -/// confirms the reconnect. -#[cucumber::then( - expr = "publishing zone message with data {string} via sequencer {string} fails while the node is down" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require the world as the first `&mut` argument" -)] -async fn step_publish_fails_while_node_down( - world: &mut ZoneWorld, - step: &Step, - data: String, - sequencer_alias: String, -) -> StepResult { - let _ = step; - let payload = make_inscription(&data); - let client = world.zone.sequencer_client(&sequencer_alias)?.clone(); - match client.publish(payload).await { - Ok(_) => Err(StepError::LogicalError { - message: format!( - "Zone publish unexpectedly succeeded for sequencer '{sequencer_alias}' while its node is down" - ), - }), - Err(_expected) => Ok(()), - } -} - -#[when( - expr = "I submit zone message {string} to sequencer {string} with data {string} immediately" -)] -async fn step_publish_single_zone_message_for_sequencer( - world: &mut ZoneWorld, - step: &Step, - message_alias: String, - sequencer_alias: String, - data: String, -) -> StepResult { - let _ = step; - let payload = make_inscription(&data); - let handle = world.zone.sequencer_client(&sequencer_alias)?.clone(); - - let (published, _checkpoint) = handle - .publish(payload.clone()) - .await - .map_err(|error| StepError::LogicalError { - message: format!( - "Zone publish failed for sequencer '{sequencer_alias}' and message '{message_alias}': {error}" - ), - })?; - - remember_published_zone_message(world, &sequencer_alias, message_alias, payload, &published); - - Ok(()) -} - -#[when( - expr = "sequencer {string} submits zone message {string} with data {string} to queue immediately" -)] -async fn step_publish_single_zone_message_to_queue_for_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - message_alias: String, - data: String, -) -> StepResult { - step_publish_single_zone_message_for_sequencer( - world, - step, - message_alias, - sequencer_alias, - data, - ) - .await -} - -#[when( - "the following custom transactions are published concurrently with custom republish policy:" -)] -async fn step_publish_custom_txs_concurrently_with_policy( - world: &mut ZoneWorld, - step: &Step, -) -> StepResult { - let rows = custom_tx_rows(step)?; - let mut expected_payloads = Vec::new(); - - for row in &rows { - let batches: VecDeque> = (0..row.transactions) - .map(|tx_index| { - (0..row.inscriptions) - .map(|entry_index| { - make_inscription(&format!( - "custom-{}-{tx_index}-{entry_index}", - row.sequencer_alias - )) - }) - .collect() - }) - .collect(); - expected_payloads.extend(batches.iter().flatten().cloned()); - - let node_client = log_step_error( - step, - world.zone_node_http_client_for_sequencer(&row.sequencer_alias), - )?; - let node_name = world - .zone - .sequencer_node_name(&row.sequencer_alias)? - .to_owned(); - let funding_pk = world.node.funding_wallet(&node_name)?.public_key()?; - let deps = CustomRepublishDeps { - node_client, - channel_id: world.zone.sequencer_channel_id(&row.sequencer_alias)?, - signing_key: world - .zone - .sequencer_signing_key(&row.sequencer_alias)? - .clone(), - funding_pk, - batches, - }; - - start_named_sequencer( - world, - step, - &row.sequencer_alias, - None, - DriveMode::CustomRepublish { - deps: Box::new(deps), - }, - ) - .await?; - } - - world - .zone - .remember_expected_custom_payloads(expected_payloads); - Ok(()) -} - -#[cucumber::then(expr = "the zone indexer returns all custom payloads in {int} seconds")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_zone_indexer_returns_all_custom_payloads( - world: &mut ZoneWorld, - step: &Step, - timeout_seconds: u64, -) -> StepResult { - let expected: HashSet = world - .zone - .expected_custom_payloads() - .iter() - .cloned() - .collect(); - if expected.is_empty() { - return Err(StepError::LogicalError { - message: "no custom transactions were planned".to_owned(), - }); - } - let indexer = log_step_error(step, world.zone.indexer())?; - - wait_for_indexer_unordered(indexer, &expected, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error))?; - Ok(()) -} - -#[when( - expr = "I submit zone message {string} to sequencer {string} with data {string} on its turn" -)] -async fn step_publish_single_zone_message_for_sequencer_on_turn( - world: &mut ZoneWorld, - step: &Step, - message_alias: String, - sequencer_alias: String, - data: String, -) -> StepResult { - let payload = make_inscription(&data); - let handle = world.zone.sequencer_client(&sequencer_alias)?.clone(); - let mut view_rx = world.zone.sequencer_channel_view_rx(&sequencer_alias)?; - - wait_for_channel_view(&mut view_rx, Duration::from_mins(3), |view| { - view.our_turn_to_write - && view.authorized_key_index.is_some() - && view.authorized_key_index == view.own_key_index - }) - .await - .map_err(|error| zone_step_error(step, &error))?; - - let published = publish_message_with_retry( - &handle, - &payload, - PublishDeadline::from_now(Duration::from_mins(3)), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - remember_published_zone_message(world, &sequencer_alias, message_alias, payload, &published); - - Ok(()) -} - -#[when(expr = "sequencer {string} submits the following zone messages to queue immediately:")] -async fn step_publish_zone_messages_to_queue_for_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - let rows = zone_message_rows(step)?; - let handle = world.zone.sequencer_client(&sequencer_alias)?.clone(); - - for (message_alias, payload) in rows { - let (published, _checkpoint) = handle - .publish(payload.clone()) - .await - .map_err(|error| StepError::LogicalError { - message: format!( - "Zone publish failed for sequencer '{sequencer_alias}' and message '{message_alias}': {error}" - ), - })?; - - remember_published_zone_message( - world, - &sequencer_alias, - message_alias, - payload, - &published, - ); - } - - Ok(()) -} - -/// Publish via [`SequencerClient`] and record each inscription id, without -/// waiting for on-chain inclusion. -/// -/// Unlike `publishes the following zone messages` (which polls the node to -/// confirm inclusion), this performs no node HTTP calls, so it can be issued -/// while the node is down: each publish is accepted locally and posted on -/// reconnect. Recording the ids (the publish returns them locally) lets later -/// `... are finalized` / indexer assertions track the messages once the node is -/// back. -#[when( - expr = "sequencer {string} submits the following zone messages without waiting for inclusion:" -)] -async fn step_publish_zone_messages_without_inclusion_for_sequencer( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - let rows = zone_message_rows(step)?; - let handle = world.zone.sequencer_client(&sequencer_alias)?.clone(); - - for (message_alias, payload) in rows { - // Accepted locally is all this step asks for; a publish issued while - // the sequencer is still reconnecting to a restarted node fails fast - // and is retried until it is taken. - let published = publish_message_with_retry( - &handle, - &payload, - PublishDeadline::from_now(Duration::from_mins(3)), - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - remember_published_zone_message( - world, - &sequencer_alias, - message_alias, - payload, - &published, - ); - } - - Ok(()) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/steps/sequencing.rs b/tests/zone-sdk/src/cucumber/steps/zone/steps/sequencing.rs deleted file mode 100644 index 828ccf1..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/steps/sequencing.rs +++ /dev/null @@ -1,234 +0,0 @@ -use super::{ - Duration, Step, StepResult, ZoneWorld, ensure_zone_transactions_included, log_step_error, - single_column_table, wait_for_channel_view, wait_for_turn_to_write, zone_sequencing_state_row, - zone_step_error, -}; - -#[cucumber::then( - expr = "sequencer {string} reaches sequencing state OWN_KEY_INDEX {int} NOT_OUR_TURN with {int} pending publish txs in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_sequencer_reaches_sequencing_state_not_our_turn( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - own_key_index: usize, - pending_publish_txs: usize, - timeout_seconds: u64, -) -> StepResult { - wait_for_sequencing_state( - world, - step, - &sequencer_alias, - own_key_index, - false, - pending_publish_txs, - timeout_seconds, - ) - .await -} - -#[cucumber::then( - expr = "sequencer {string} reaches sequencing state OWN_KEY_INDEX {int} OUR_TURN with {int} pending publish txs in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_sequencer_reaches_sequencing_state_our_turn( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - own_key_index: usize, - pending_publish_txs: usize, - timeout_seconds: u64, -) -> StepResult { - wait_for_sequencing_state( - world, - step, - &sequencer_alias, - own_key_index, - true, - pending_publish_txs, - timeout_seconds, - ) - .await -} - -#[cucumber::then(expr = "sequencer {string} reaches sequencing state:")] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_sequencer_reaches_sequencing_state_from_table( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, -) -> StepResult { - let row = zone_sequencing_state_row(step)?; - - wait_for_sequencing_state( - world, - step, - &sequencer_alias, - row.own_key_index, - row.is_our_turn, - row.pending_transactions, - row.timeout_seconds, - ) - .await -} - -async fn wait_for_sequencing_state( - world: &ZoneWorld, - step: &Step, - sequencer_alias: &str, - own_key_index: usize, - is_our_turn: bool, - pending_publish_txs: usize, - timeout_seconds: u64, -) -> StepResult { - let _handle = log_step_error(step, world.zone.sequencer_client(sequencer_alias))?.clone(); - let mut view_rx = log_step_error(step, world.zone.sequencer_channel_view_rx(sequencer_alias))?; - - wait_for_channel_view( - &mut view_rx, - Duration::from_secs(timeout_seconds), - move |view| { - view.own_key_index == Some(own_key_index as u16) - && view.authorized_key_index.is_some() - && view.our_turn_to_write == is_our_turn - && (is_our_turn || view.authorized_key_index != view.own_key_index) - && (!is_our_turn || view.authorized_key_index == Some(own_key_index as u16)) - && view.pending_publish_txs == pending_publish_txs - }, - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - Ok(()) -} - -#[cucumber::then( - expr = "sequencer {string} is notified it is their turn to write in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_sequencer_notified_turn_to_write( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - timeout_seconds: u64, -) -> StepResult { - let mut turn_rx = log_step_error( - step, - world.zone.sequencer_turn_to_write_rx(&sequencer_alias), - )?; - wait_for_turn_to_write(&mut turn_rx, Duration::from_secs(timeout_seconds)) - .await - .map_err(|error| zone_step_error(step, &error))?; - - Ok(()) -} - -#[cucumber::then( - expr = "zone messages queued by sequencer {string} are included after its turn in {int} seconds:" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_queued_zone_messages_are_included_after_turn( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - timeout_seconds: u64, -) -> StepResult { - let aliases = single_column_table(step, "alias", "zone message aliases")?; - let tx_hashes = log_step_error(step, world.zone.message_tx_hashes_for_aliases(&aliases))?; - let mut view_rx = log_step_error(step, world.zone.sequencer_channel_view_rx(&sequencer_alias))?; - wait_for_channel_view(&mut view_rx, Duration::from_secs(timeout_seconds), |view| { - view.our_turn_to_write - }) - .await - .map_err(|error| zone_step_error(step, &error))?; - - // The messages were remembered when queued; the turn posts them, so wait - // until the node has them on chain (mined, not yet finalized). - let node_client = log_step_error( - step, - world.zone_node_http_client_for_sequencer(&sequencer_alias), - )?; - ensure_zone_transactions_included( - &node_client, - &tx_hashes, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} - -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -#[cucumber::then(expr = "sequencer {string} has {int} pending publish txs in {int} seconds")] -async fn step_sequencer_has_pending_publish_txs( - world: &mut ZoneWorld, - step: &Step, - sequencer_alias: String, - pending_publish_txs: usize, - timeout_seconds: u64, -) -> StepResult { - let mut view_rx = log_step_error(step, world.zone.sequencer_channel_view_rx(&sequencer_alias))?; - - wait_for_channel_view( - &mut view_rx, - Duration::from_secs(timeout_seconds), - move |view| view.pending_publish_txs == pending_publish_txs, - ) - .await - .map_err(|error| zone_step_error(step, &error))?; - - Ok(()) -} - -#[cucumber::then( - expr = "zone message {string} published by sequencer {string} is included while in turn in {int} seconds" -)] -#[expect( - clippy::needless_pass_by_ref_mut, - reason = "Cucumber step functions require `&mut World` as the first parameter" -)] -async fn step_published_zone_message_is_included_while_in_turn( - world: &mut ZoneWorld, - step: &Step, - message_alias: String, - sequencer_alias: String, - timeout_seconds: u64, -) -> StepResult { - // The message was remembered when submitted; wait until the node has it - // on chain (mined, not yet finalized). - let tx_hashes = log_step_error( - step, - world - .zone - .message_tx_hashes_for_aliases(std::slice::from_ref(&message_alias)), - )?; - let node_client = log_step_error( - step, - world.zone_node_http_client_for_sequencer(&sequencer_alias), - )?; - ensure_zone_transactions_included( - &node_client, - &tx_hashes, - Duration::from_secs(timeout_seconds), - ) - .await - .map_err(|error| zone_step_error(step, &error)) -} diff --git a/tests/zone-sdk/src/cucumber/steps/zone/tables.rs b/tests/zone-sdk/src/cucumber/steps/zone/tables.rs deleted file mode 100644 index 6c1c7af..0000000 --- a/tests/zone-sdk/src/cucumber/steps/zone/tables.rs +++ /dev/null @@ -1,472 +0,0 @@ -use std::collections::{HashMap, HashSet}; - -use cucumber::gherkin::Step; -use lb_core::mantle::ops::channel::inscribe::Inscription; -use logos_blockchain_tests::{ - common::mantle_inscription::make_inscription, - cucumber::{ - error::StepError, - steps::parse_steps::{ - comma_separated_aliases, invalid_table_row, optional_string_cell, parse_list_cell, - parse_required_bool_cell, parse_required_string_cell, parse_single_table_row, - parse_table_rows, parse_u32_cell, parse_u64_cell, parse_usize_cell, - }, - }, -}; - -#[derive(Clone)] -pub(super) struct ConcurrentZoneMessageRow { - pub sequencer_alias: String, - pub message_alias: String, - pub payload: Inscription, -} - -#[derive(Clone)] -pub(super) struct ZoneBalanceRow { - pub sequencer_alias: String, - pub message_alias: String, - pub account: String, - pub delta: i64, -} - -pub(super) struct ZoneAccountBalance { - pub account: String, - pub balance: i64, -} - -pub(super) struct GeneratedZoneMessageBatch { - pub sequencer_alias: String, - pub data_prefix: String, -} - -pub(super) struct ZoneSequencerStartRow { - pub alias: String, - pub indexer: bool, - pub pending_submit_depth: String, - pub passive_republish_orphans: bool, -} - -pub(super) struct ZoneSequencingStateRow { - pub own_key_index: usize, - pub is_our_turn: bool, - pub pending_transactions: usize, - pub timeout_seconds: u64, -} - -pub(super) struct ZoneConfigRow { - pub config_name: String, - pub posting_timeframe: u32, - pub posting_timeout: u32, - pub authorized_sequencers: Vec, -} - -pub(super) struct ZoneNodeResourcesRow { - pub node_name: String, - pub account_index: usize, - pub wallet_name: String, - pub connected_to: Option, - pub sequencers: Vec, -} - -pub(super) fn zone_node_resource_rows(step: &Step) -> Result, StepError> { - let rows = parse_table_rows( - step, - &[ - "node_name", - "account_index", - "wallet_name", - "connected_to", - "sequencers", - ], - "Zone node", - parse_zone_node_resource_row, - )?; - - ensure_unique_zone_node_resources(&rows)?; - - Ok(rows) -} - -fn parse_zone_node_resource_row(row: &[String]) -> Result { - match row { - [ - node_name, - account_index, - wallet_name, - connected_to, - sequencers, - ] => { - let node_name = parse_required_string_cell(node_name, "node_name", "Zone node")?; - let account_index = - account_index - .trim() - .parse() - .map_err(|error| StepError::InvalidArgument { - message: format!("Invalid zone account index '{account_index}': {error}"), - })?; - let wallet_name = parse_required_string_cell(wallet_name, "wallet_name", "Zone node")?; - let connected_to = optional_string_cell(connected_to); - let sequencers = comma_separated_aliases(sequencers, "sequencers", "Zone node")?; - - Ok(ZoneNodeResourcesRow { - node_name, - account_index, - wallet_name, - connected_to, - sequencers, - }) - } - _ => invalid_table_row( - "Zone node", - &[ - "node_name", - "account_index", - "wallet_name", - "connected_to", - "sequencers", - ], - row.len(), - ), - } -} - -fn ensure_unique_zone_node_resources(rows: &[ZoneNodeResourcesRow]) -> Result<(), StepError> { - let mut wallets = HashSet::new(); - let mut sequencers = HashSet::new(); - - for row in rows { - if !wallets.insert(row.wallet_name.clone()) { - return Err(StepError::InvalidArgument { - message: format!("Duplicate zone wallet '{}'", row.wallet_name), - }); - } - - for sequencer in &row.sequencers { - if !sequencers.insert(sequencer.clone()) { - return Err(StepError::InvalidArgument { - message: format!("Duplicate zone sequencer '{sequencer}'"), - }); - } - } - } - - Ok(()) -} - -pub(super) fn zone_message_rows(step: &Step) -> Result, StepError> { - parse_table_rows(step, &["alias", "data"], "Zone message", |row| match row { - [alias, data] => Ok((alias.clone(), make_inscription(data))), - _ => invalid_table_row("Zone message", &["alias", "data"], row.len()), - }) -} - -pub(super) fn concurrent_zone_message_rows( - step: &Step, -) -> Result, StepError> { - parse_table_rows( - step, - &["sequencer", "alias", "data"], - "Concurrent zone message", - |row| match row { - [sequencer_alias, message_alias, data] => Ok(ConcurrentZoneMessageRow { - sequencer_alias: sequencer_alias.clone(), - message_alias: message_alias.clone(), - payload: make_inscription(data), - }), - _ => invalid_table_row( - "Concurrent zone message", - &["sequencer", "alias", "data"], - row.len(), - ), - }, - ) -} - -#[derive(Clone)] -pub(super) struct CustomTxRow { - pub sequencer_alias: String, - pub transactions: u32, - pub inscriptions: u32, -} - -pub(super) fn custom_tx_rows(step: &Step) -> Result, StepError> { - parse_table_rows( - step, - &["sequencer", "transactions", "inscriptions"], - "Concurrent custom transaction", - |row| match row { - [sequencer_alias, transactions, inscriptions] => Ok(CustomTxRow { - sequencer_alias: sequencer_alias.clone(), - transactions: parse_u32_cell(transactions, "transactions")?, - inscriptions: parse_u32_cell(inscriptions, "inscriptions")?, - }), - _ => invalid_table_row( - "Concurrent custom transaction", - &["sequencer", "transactions", "inscriptions"], - row.len(), - ), - }, - ) -} - -pub(super) fn group_zone_messages_by_sequencer( - rows: &[ConcurrentZoneMessageRow], -) -> HashMap> { - let mut grouped = HashMap::new(); - - for row in rows { - grouped - .entry(row.sequencer_alias.clone()) - .or_insert_with(Vec::new) - .push(row.clone()); - } - - grouped -} - -pub(super) fn generated_zone_message_batches( - step: &Step, -) -> Result, StepError> { - parse_table_rows( - step, - &["sequencer", "data_prefix"], - "Generated zone message batch", - |row| match row { - [sequencer_alias, data_prefix] => Ok(GeneratedZoneMessageBatch { - sequencer_alias: sequencer_alias.clone(), - data_prefix: data_prefix.clone(), - }), - _ => invalid_table_row( - "Generated zone message batch", - &["sequencer", "data_prefix"], - row.len(), - ), - }, - ) -} - -pub(super) fn generated_zone_message_sequencers(step: &Step) -> Result, StepError> { - parse_table_rows( - step, - &["sequencer"], - "Generated zone message sequencer", - |row| match row { - [sequencer_alias] => Ok(sequencer_alias.clone()), - _ => invalid_table_row( - "Generated zone message sequencer", - &["sequencer"], - row.len(), - ), - }, - ) -} - -pub(super) fn zone_sequencer_start_rows( - step: &Step, -) -> Result, StepError> { - parse_table_rows( - step, - &[ - "alias", - "indexer", - "pending_submit_depth", - "passive_republish_orphans", - ], - "Zone sequencer startup", - |row| match row { - [ - alias, - indexer, - pending_submit_depth, - passive_republish_orphans, - ] => Ok(ZoneSequencerStartRow { - alias: alias.clone(), - indexer: parse_required_bool_cell(indexer, "indexer", "Zone sequencer startup")?, - pending_submit_depth: pending_submit_depth.clone(), - passive_republish_orphans: parse_required_bool_cell( - passive_republish_orphans, - "passive_republish_orphans", - "Zone sequencer startup", - )?, - }), - _ => invalid_table_row( - "Zone sequencer startup", - &[ - "alias", - "indexer", - "pending_submit_depth", - "passive_republish_orphans", - ], - row.len(), - ), - }, - ) -} - -pub(super) fn zone_sequencing_state_row(step: &Step) -> Result { - parse_single_table_row( - step, - &[ - "own_key_index", - "turn_to_write", - "pending_transactions", - "time_out", - ], - "Zone sequencing state", - |row| match row { - [ - own_key_index, - turn_to_write, - pending_transactions, - timeout_seconds, - ] => Ok(ZoneSequencingStateRow { - own_key_index: parse_usize_cell(own_key_index, "own_key_index")?, - is_our_turn: parse_turn_cell(turn_to_write)?, - pending_transactions: parse_usize_cell( - pending_transactions, - "pending_transactions", - )?, - timeout_seconds: parse_u64_cell(timeout_seconds, "time_out")?, - }), - _ => invalid_table_row( - "Zone sequencing state", - &[ - "own_key_index", - "turn_to_write", - "pending_transactions", - "time_out", - ], - row.len(), - ), - }, - ) -} - -pub(super) fn zone_config_row(step: &Step) -> Result { - parse_single_table_row( - step, - &[ - "config_name", - "posting_timeframe", - "posting_timeout", - "authorized_sequencers", - ], - "Zone config transaction", - |row| match row { - [ - config_name, - posting_timeframe, - posting_timeout, - authorized_sequencers, - ] => Ok(ZoneConfigRow { - config_name: config_name.clone(), - posting_timeframe: parse_u32_cell(posting_timeframe, "posting_timeframe")?, - posting_timeout: parse_u32_cell(posting_timeout, "posting_timeout")?, - authorized_sequencers: parse_list_cell( - authorized_sequencers, - "authorized_sequencers", - "Zone config transaction", - )?, - }), - _ => invalid_table_row( - "Zone config transaction", - &[ - "config_name", - "posting_timeframe", - "posting_timeout", - "authorized_sequencers", - ], - row.len(), - ), - }, - ) -} - -pub(super) fn zone_account_balances(step: &Step) -> Result, StepError> { - parse_table_rows( - step, - &["account", "balance"], - "Zone account balance", - |row| match row { - [account, balance] => Ok(ZoneAccountBalance { - account: account.clone(), - balance: balance - .parse() - .map_err(|error| StepError::InvalidArgument { - message: format!("Invalid zone account balance '{balance}': {error}"), - })?, - }), - _ => invalid_table_row("Zone account balance", &["account", "balance"], row.len()), - }, - ) -} - -pub(super) fn zone_balance_rows(step: &Step) -> Result, StepError> { - parse_table_rows( - step, - &["sequencer", "alias", "account", "delta"], - "Zone balance update", - |row| match row { - [sequencer_alias, message_alias, account, delta] => Ok(ZoneBalanceRow { - sequencer_alias: sequencer_alias.clone(), - message_alias: message_alias.clone(), - account: account.clone(), - delta: delta.parse().map_err(|error| StepError::InvalidArgument { - message: format!("Invalid zone balance delta '{delta}': {error}"), - })?, - }), - _ => invalid_table_row( - "Zone balance update", - &["sequencer", "alias", "account", "delta"], - row.len(), - ), - }, - ) -} - -/// Parses an atomic-withdraw table row of `(alias, outputs)` where `outputs` -/// is a comma-separated list of note values. Each row becomes one -/// `WithdrawArg`; the comma list lets a single arg carry multiple output -/// notes so the SDK's `publish_atomic_withdraw(inscribe, vec![WithdrawArg])` -/// signature is exercised at full width (multi-arg + multi-output-per-arg). -pub(super) fn zone_atomic_withdraw_rows(step: &Step) -> Result)>, StepError> { - parse_table_rows( - step, - &["withdraw", "outputs"], - "Atomic withdraw", - |row| match row { - [alias, outputs] => { - let amounts = outputs - .split(',') - .map(|s| { - s.trim() - .parse::() - .map_err(|error| StepError::InvalidArgument { - message: format!("Invalid withdraw output amount '{s}': {error}"), - }) - }) - .collect::, _>>()?; - if amounts.is_empty() { - return Err(StepError::InvalidArgument { - message: format!( - "Atomic withdraw '{alias}' must list at least one output amount" - ), - }); - } - Ok((alias.clone(), amounts)) - } - _ => invalid_table_row("Atomic withdraw", &["withdraw", "outputs"], row.len()), - }, - ) -} -fn parse_turn_cell(value: &str) -> Result { - match value.to_uppercase().trim() { - "OUR_TURN" => Ok(true), - "NOT_OUR_TURN" => Ok(false), - _ => Err(StepError::InvalidArgument { - message: format!( - "Zone sequencing state `turn_to_write` must be `OUR_TURN` or `NOT_OUR_TURN`, got `{value}`" - ), - }), - } -} diff --git a/tests/zone-sdk/src/cucumber/world.rs b/tests/zone-sdk/src/cucumber/world.rs deleted file mode 100644 index 1f4b0ed..0000000 --- a/tests/zone-sdk/src/cucumber/world.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::fmt; - -use cucumber::World; -use lb_testing_framework::NodeHttpClient; -use logos_blockchain_tests::cucumber::{ - error::{StepError, StepResult}, - runner::ScenarioCleanup, - steps::nodes::stop_all_nodes, - world::CucumberWorld, -}; -use reqwest::Url; - -use super::zone::ZoneState; -pub use super::zone::{ - ZoneDiscardedPayloads, ZoneReaderConfig, ZoneSequencerStartup, ZoneViewViolation, -}; - -#[derive(Default, World)] -pub struct ZoneWorld { - pub node: CucumberWorld, - pub zone: ZoneState, -} - -impl AsMut for ZoneWorld { - fn as_mut(&mut self) -> &mut CucumberWorld { - &mut self.node - } -} - -impl ScenarioCleanup for ZoneWorld { - async fn cleanup(&mut self) -> StepResult { - self.zone.clear(); - - // Setup can fail before a cluster exists. - if self.node.cluster.local_app.is_none() && self.node.cluster.k8s_manual_cluster.is_none() { - return self.node.cleanup().await; - } - - stop_all_nodes(&mut self.node).await - } -} - -impl Drop for ZoneWorld { - fn drop(&mut self) { - // Stop clients before the node world releases their cluster and files. - self.zone.clear(); - } -} - -impl fmt::Debug for ZoneWorld { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter - .debug_struct("ZoneWorld") - .field("node", &self.node) - .field("zone", &self.zone.debug_summary()) - .finish() - } -} - -impl ZoneWorld { - pub fn full_debug_info_string(&self) -> String { - format!( - "{}\n\nZoneSDK state:\n{}", - self.node.full_debug_info_string(), - self.zone.debug_summary(), - ) - } - - pub fn zone_node_http_client(&self) -> Result { - self.node.resolve_node_http_client(self.zone.node_name()?) - } - - pub fn zone_node_url(&self) -> Result { - Ok(self.zone_node_http_client()?.base_url().clone()) - } - - pub fn zone_node_http_client_for_sequencer( - &self, - alias: &str, - ) -> Result { - self.node - .resolve_node_http_client(self.zone.sequencer_node_name(alias)?) - } - - pub fn zone_node_url_for_sequencer(&self, alias: &str) -> Result { - Ok(self - .zone_node_http_client_for_sequencer(alias)? - .base_url() - .clone()) - } -} diff --git a/tests/zone-sdk/src/cucumber/zone.rs b/tests/zone-sdk/src/cucumber/zone.rs deleted file mode 100644 index 4a2d269..0000000 --- a/tests/zone-sdk/src/cucumber/zone.rs +++ /dev/null @@ -1,729 +0,0 @@ -use std::{ - collections::{HashMap, HashSet}, - sync::{Arc, Mutex}, -}; - -use lb_core::{ - mantle::{ - Utxo, Value, - ops::channel::{ - ChannelId, MsgId, deposit::DepositOp, inscribe::Inscription, - withdraw::ChannelWithdrawOp, - }, - }, - proofs::channel_multi_sig_proof::IndexedSignatures, -}; -use lb_key_management_system_service::keys::{Ed25519Key, Ed25519PublicKey}; -use logos_blockchain_tests::cucumber::error::StepError; -use reqwest::Url; -use tokio::task::JoinHandle; - -use super::steps::zone::runner::{ - Event, IndexedSignature, InscriptionId, PreparedChannelConfig, SequencerCheckpoint, - SequencerClient, -}; - -pub struct ZonePublishedMessage { - pub payload: Inscription, - pub inscription_id: Option, - /// The channel message id, stable across a stale-refund rebuild. - pub msg_id: Option, -} - -pub type ZoneDiscardedPayloads = Arc>>; -/// The first channel-view contract violation a sequencer's drive loop -/// recorded, shared with the checker that writes it. -pub type ZoneViewViolation = Arc>>; - -pub struct ZoneSequencerIdentity { - signing_key: Ed25519Key, - channel_id: ChannelId, - node_name: Option, - default_wallet_name: Option, -} - -pub struct ZoneSequencerRuntime { - client: SequencerClient, - task: JoinHandle<()>, - events: tokio::sync::broadcast::Receiver, - checkpoint_rx: tokio::sync::watch::Receiver>, - channel_view_rx: tokio::sync::watch::Receiver, - turn_to_write_rx: tokio::sync::watch::Receiver, - discarded_payloads: Option, -} - -impl ZoneSequencerRuntime { - fn abort_tasks(&self) { - self.task.abort(); - } -} - -#[derive(Clone, Copy, Default)] -pub struct ZoneSequencerStartup { - pub pending_submit_depth: Option, - pub passive_republish_orphans: bool, -} - -/// Connection info for the read-only channel observer of the "zone indexer" -/// steps. -/// -/// Each assertion cold-starts a fresh `ZoneSequencer` from this config with a -/// random signing key that is not part of the channel rotation — such a -/// sequencer can never publish or repost (inscription posting is turn-gated), -/// it only replays and observes finalized history. -#[derive(Clone)] -pub struct ZoneReaderConfig { - pub channel_id: ChannelId, - pub node_url: Url, -} - -#[derive(Default)] -pub struct ZoneState { - node_name: Option, - indexer: Option, - sequencers: HashMap, - runtimes: HashMap, - /// One per run of the sequencer alias, so a violation survives a restart. - view_violations: HashMap>, - default_sequencer_alias: Option, - published_messages: HashMap, - submitted_deposits: HashMap, - /// The channel notes each deposit re-created, keyed by deposit alias — so a - /// later channel split transfer can spend them without waiting on the - /// indexer. - deposit_channel_notes: HashMap>, - submitted_withdraws: HashMap, - account_balances: HashMap, - published_order: Vec, - saved_checkpoints: HashMap, - latest_checkpoints: HashMap, - prepared_configs: HashMap, - prepared_config_signatures: HashMap, - sequencer_startups: HashMap, - sorted_total_payloads: Option, - sorted_expected_by_sequencer: Option>>, - expected_custom_payloads: Vec, -} - -impl ZoneState { - pub fn clear(&mut self) { - self.reset_zone_state(); - } - - pub fn node_name(&self) -> Result<&str, StepError> { - self.node_name.as_deref().ok_or(StepError::LogicalError { - message: "Zone cluster is not initialized".to_owned(), - }) - } - - pub fn register_sequencer(&mut self, alias: String, signing_key: Ed25519Key) -> ChannelId { - let channel_id = self.channel_for_new_sequencer(&signing_key); - let existing_resources = self - .sequencers - .get(&alias) - .map(|sequencer| { - ( - sequencer.node_name.clone(), - sequencer.default_wallet_name.clone(), - ) - }) - .unwrap_or_default(); - - self.sequencers.insert( - alias.clone(), - ZoneSequencerIdentity { - signing_key, - channel_id, - node_name: existing_resources.0, - default_wallet_name: existing_resources.1, - }, - ); - - if self.default_sequencer_alias.is_none() { - self.default_sequencer_alias = Some(alias); - } - - channel_id - } - - fn channel_for_new_sequencer(&self, signing_key: &Ed25519Key) -> ChannelId { - self.default_sequencer_alias - .as_ref() - .and_then(|alias| self.sequencers.get(alias)) - .map_or_else( - || ChannelId::from(signing_key.public_key().to_bytes()), - |sequencer| sequencer.channel_id, - ) - } - - pub fn default_sequencer_alias(&self) -> Result<&str, StepError> { - self.default_sequencer_alias - .as_deref() - .ok_or(StepError::LogicalError { - message: "No zone sequencer is registered".to_owned(), - }) - } - - pub fn sequencer_signing_key(&self, alias: &str) -> Result<&Ed25519Key, StepError> { - self.sequencers - .get(alias) - .map(|sequencer| &sequencer.signing_key) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not registered"), - }) - } - - /// A registered sequencer's public key, without touching its private key. - pub fn sequencer_public_key(&self, alias: &str) -> Result { - self.sequencer_signing_key(alias) - .map(Ed25519Key::public_key) - } - - pub fn sequencer_channel_id(&self, alias: &str) -> Result { - self.sequencers - .get(alias) - .map(|sequencer| sequencer.channel_id) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not registered"), - }) - } - - #[must_use] - pub fn has_sequencer(&self, alias: &str) -> bool { - self.sequencers.contains_key(alias) - } - - pub fn attach_sequencer_resources( - &mut self, - alias: &str, - node_name: String, - wallet_name: String, - ) -> Result<(), StepError> { - if self.node_name.is_none() { - self.node_name = Some(node_name.clone()); - } - - let sequencer = self - .sequencers - .get_mut(alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not registered"), - })?; - - sequencer.node_name = Some(node_name); - sequencer.default_wallet_name = Some(wallet_name); - - Ok(()) - } - - pub fn sequencer_node_name(&self, alias: &str) -> Result<&str, StepError> { - let sequencer = self - .sequencers - .get(alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not registered"), - })?; - - sequencer - .node_name - .as_deref() - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not attached to a node"), - }) - } - - pub fn sequencer_default_wallet_name(&self, alias: &str) -> Result<&str, StepError> { - self.sequencers - .get(alias) - .and_then(|sequencer| sequencer.default_wallet_name.as_deref()) - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone sequencer '{alias}' does not have a default wallet"), - }) - } - - pub fn remember_zone_message( - &mut self, - alias: String, - payload: Inscription, - inscription_id: Option, - msg_id: Option, - sequencer_alias: Option<&str>, - checkpoint: Option, - ) { - self.published_order.push(alias.clone()); - self.published_messages.insert( - alias, - ZonePublishedMessage { - payload, - inscription_id, - msg_id, - }, - ); - - if let (Some(sequencer_alias), Some(checkpoint)) = (sequencer_alias, checkpoint) { - self.latest_checkpoints - .insert(sequencer_alias.to_owned(), checkpoint); - } - } - - pub fn remember_submitted_deposit(&mut self, alias: String, deposit: DepositOp, amount: Value) { - self.submitted_deposits.insert(alias, (deposit, amount)); - } - - pub fn remember_deposit_channel_notes(&mut self, alias: String, channel_notes: Vec) { - self.deposit_channel_notes.insert(alias, channel_notes); - } - - pub fn resolve_deposit_channel_notes( - &self, - alias: impl AsRef, - ) -> Result<&[Utxo], StepError> { - let alias = alias.as_ref(); - - self.deposit_channel_notes - .get(alias) - .map(Vec::as_slice) - .ok_or(StepError::LogicalError { - message: format!("Zone deposit channel notes for alias '{alias}' not found"), - }) - } - - pub fn resolve_submitted_deposit( - &self, - alias: impl AsRef, - ) -> Result<&(DepositOp, Value), StepError> { - let alias = alias.as_ref(); - - self.submitted_deposits - .get(alias) - .ok_or(StepError::LogicalError { - message: format!("Zone deposit alias '{alias}' not found"), - }) - } - - pub fn remember_submitted_withdraw(&mut self, alias: String, withdraw: ChannelWithdrawOp) { - self.submitted_withdraws.insert(alias, withdraw); - } - - pub fn resolve_submitted_withdraw( - &self, - alias: impl AsRef, - ) -> Result<&ChannelWithdrawOp, StepError> { - let alias = alias.as_ref(); - - self.submitted_withdraws - .get(alias) - .ok_or(StepError::LogicalError { - message: format!("Zone withdraw alias '{alias}' not found"), - }) - } - - pub fn set_zone_account_balances(&mut self, balances: HashMap) { - self.account_balances = balances; - } - - pub fn zone_account_balances(&self) -> Result, StepError> { - if self.account_balances.is_empty() { - return Err(StepError::LogicalError { - message: "Zone account balances are not initialized".to_owned(), - }); - } - - Ok(self.account_balances.clone()) - } - - pub fn ordered_inscription_ids(&self) -> Result, StepError> { - self.published_order - .iter() - .map(|alias| { - self.published_messages - .get(alias) - .and_then(|message| message.inscription_id) - .ok_or(StepError::LogicalError { - message: format!( - "Zone message alias '{alias}' does not have a tracked inscription id" - ), - }) - }) - .collect() - } - - pub fn message_payloads_for_aliases( - &self, - aliases: &[String], - ) -> Result, StepError> { - aliases - .iter() - .map(|alias| { - self.published_messages - .get(alias) - .map(|message| message.payload.clone()) - .ok_or(StepError::LogicalError { - message: format!("Zone message alias '{alias}' not found"), - }) - }) - .collect() - } - - pub fn published_message(&self, alias: &str) -> Result<&ZonePublishedMessage, StepError> { - self.published_messages - .get(alias) - .ok_or_else(|| StepError::LogicalError { - message: format!("Zone message alias '{alias}' is not tracked"), - }) - } - - pub fn message_tx_hashes_for_aliases( - &self, - aliases: &[String], - ) -> Result, StepError> { - aliases - .iter() - .map(|alias| { - self.published_messages - .get(alias) - .and_then(|message| message.inscription_id) - .ok_or(StepError::LogicalError { - message: format!( - "Zone message alias '{alias}' does not have a tracked tx hash" - ), - }) - }) - .collect() - } - - pub fn published_message_payloads(&self) -> Result, StepError> { - self.message_payloads_for_aliases(&self.published_order) - } - - #[must_use] - pub const fn has_published_messages(&self) -> bool { - !self.published_order.is_empty() - } - - pub fn remember_checkpoint(&mut self, alias: String, checkpoint: SequencerCheckpoint) { - self.saved_checkpoints.insert(alias, checkpoint); - } - - pub fn remember_prepared_config(&mut self, alias: String, prepared: PreparedChannelConfig) { - self.prepared_configs.insert(alias, prepared); - } - - pub fn prepared_config(&self, alias: &str) -> Result<&PreparedChannelConfig, StepError> { - self.prepared_configs - .get(alias) - .ok_or(StepError::LogicalError { - message: format!("No prepared zone config transaction '{alias}'"), - }) - } - - pub fn add_prepared_config_signature( - &mut self, - alias: String, - IndexedSignature { - channel_key_index, - signature, - }: IndexedSignature, - ) { - self.prepared_config_signatures - .entry(alias) - .or_default() - .try_insert(channel_key_index, signature) - .unwrap(); - } - - #[must_use] - pub fn prepared_config_signatures(&self, alias: &str) -> IndexedSignatures { - self.prepared_config_signatures - .get(alias) - .cloned() - .unwrap_or_default() - } - - pub fn set_latest_checkpoint_for( - &mut self, - sequencer_alias: &str, - checkpoint: SequencerCheckpoint, - ) { - self.latest_checkpoints - .insert(sequencer_alias.to_owned(), checkpoint); - } - - pub fn set_sequencer_startup( - &mut self, - sequencer_alias: impl AsRef, - startup: ZoneSequencerStartup, - ) { - self.sequencer_startups - .insert(sequencer_alias.as_ref().to_owned(), startup); - } - - pub fn sequencer_startup_for(&self, sequencer_alias: impl AsRef) -> ZoneSequencerStartup { - self.sequencer_startups - .get(sequencer_alias.as_ref()) - .copied() - .unwrap_or_default() - } - - pub fn current_checkpoint_for( - &self, - sequencer_alias: &str, - ) -> Result { - if let Some(checkpoint) = self - .runtimes - .get(sequencer_alias) - .and_then(|runtime| runtime.checkpoint_rx.borrow().clone()) - { - return Ok(checkpoint); - } - - self.latest_checkpoints - .get(sequencer_alias) - .cloned() - .ok_or(StepError::LogicalError { - message: format!( - "Zone sequencer '{sequencer_alias}' has not produced a checkpoint yet" - ), - }) - } - - #[must_use] - pub fn checkpoint_receiver( - &self, - sequencer_alias: &str, - ) -> Option>> { - self.runtimes - .get(sequencer_alias) - .map(|runtime| runtime.checkpoint_rx.clone()) - } - - pub fn resolve_checkpoint( - &self, - alias: impl AsRef, - ) -> Result { - let alias = alias.as_ref(); - - self.saved_checkpoints - .get(alias) - .cloned() - .ok_or(StepError::LogicalError { - message: format!("Zone checkpoint alias '{alias}' not found"), - }) - } - - #[expect( - clippy::too_many_arguments, - reason = "test-world runtime bundles many sequencer-owned receivers + state; \ - introducing a wrapper type would just move the same fields around" - )] - pub fn set_sequencer_runtime( - &mut self, - alias: String, - sequencer_client: SequencerClient, - sequencer_task: JoinHandle<()>, - sequencer_events: tokio::sync::broadcast::Receiver, - checkpoint_rx: tokio::sync::watch::Receiver>, - channel_view_rx: tokio::sync::watch::Receiver, - turn_to_write_rx: tokio::sync::watch::Receiver, - discarded_payloads: Option, - view_violation: ZoneViewViolation, - ) { - if let Some(runtime) = self.runtimes.remove(&alias) { - runtime.abort_tasks(); - } - self.view_violations - .entry(alias.clone()) - .or_default() - .push(view_violation); - - self.runtimes.insert( - alias, - ZoneSequencerRuntime { - client: sequencer_client, - task: sequencer_task, - events: sequencer_events, - checkpoint_rx, - channel_view_rx, - turn_to_write_rx, - discarded_payloads, - }, - ); - } - - pub fn sequencer_channel_view_rx( - &self, - alias: &str, - ) -> Result, StepError> - { - self.runtimes - .get(alias) - .map(|runtime| runtime.channel_view_rx.clone()) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not running"), - }) - } - - pub fn sequencer_turn_to_write_rx( - &self, - alias: &str, - ) -> Result, StepError> - { - self.runtimes - .get(alias) - .map(|runtime| runtime.turn_to_write_rx.clone()) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not running"), - }) - } - - /// Channel-view contract violations recorded by any run of any sequencer, - /// as `(alias, message)`. - #[must_use] - pub fn view_violations(&self) -> Vec<(String, String)> { - let mut violations: Vec<(String, String)> = self - .view_violations - .iter() - .flat_map(|(alias, runs)| { - runs.iter().filter_map(|run| { - run.lock() - .ok() - .and_then(|violation| violation.clone()) - .map(|message| (alias.clone(), message)) - }) - }) - .collect(); - violations.sort(); - violations - } - - pub fn stop_sequencer(&mut self, alias: &str) -> Result<(), StepError> { - let runtime = self.runtimes.remove(alias).ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not running"), - })?; - - runtime.abort_tasks(); - - Ok(()) - } - - pub fn sequencer_client(&self, alias: &str) -> Result<&SequencerClient, StepError> { - self.runtimes - .get(alias) - .map(|runtime| &runtime.client) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not running"), - }) - } - - pub fn sequencer_events_mut( - &mut self, - alias: &str, - ) -> Result<&mut tokio::sync::broadcast::Receiver, StepError> { - self.runtimes - .get_mut(alias) - .map(|runtime| &mut runtime.events) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' is not running"), - }) - } - - pub fn discarded_payloads(&self, alias: &str) -> Result { - self.runtimes - .get(alias) - .and_then(|runtime| runtime.discarded_payloads.clone()) - .ok_or(StepError::LogicalError { - message: format!("Zone sequencer '{alias}' does not track discarded payloads"), - }) - } - - pub const fn set_sorted_total_payloads(&mut self, total: usize) { - self.sorted_total_payloads = Some(total); - } - - pub fn set_sorted_expected_by_sequencer( - &mut self, - expected_by_sequencer: HashMap>, - ) { - self.sorted_expected_by_sequencer = Some(expected_by_sequencer); - } - - pub fn sorted_total_payloads(&self) -> Result { - self.sorted_total_payloads.ok_or(StepError::LogicalError { - message: "Zone sorted conflict expectations are not initialized".to_owned(), - }) - } - - pub fn sorted_expected_by_sequencer( - &self, - ) -> Result>, StepError> { - self.sorted_expected_by_sequencer - .clone() - .ok_or(StepError::LogicalError { - message: "Zone sorted conflict payload order is not initialized".to_owned(), - }) - } - - pub fn set_indexer(&mut self, indexer: ZoneReaderConfig) { - self.indexer = Some(indexer); - } - - pub fn indexer(&self) -> Result<&ZoneReaderConfig, StepError> { - self.indexer.as_ref().ok_or(StepError::LogicalError { - message: "Zone indexer is not initialized".to_owned(), - }) - } - - #[must_use] - pub fn debug_summary(&self) -> String { - let node_name = self.node_name.as_deref().unwrap_or(""); - let sequencers = self.sequencers.len(); - let running = self.runtimes.len(); - let published = self.published_messages.len(); - let deposits = self.submitted_deposits.len(); - let withdraws = self.submitted_withdraws.len(); - let checkpoints = self.saved_checkpoints.len(); - - format!( - "node={node_name}, sequencers={sequencers}, running={running}, published={published}, deposits={deposits}, withdraws={withdraws}, checkpoints={checkpoints}" - ) - } - - fn abort_all_runtimes(&mut self) { - for (_, runtime) in self.runtimes.drain() { - runtime.abort_tasks(); - } - } - - fn reset_zone_state(&mut self) { - self.abort_all_runtimes(); - - self.node_name = None; - self.indexer = None; - self.default_sequencer_alias = None; - self.sorted_total_payloads = None; - self.sorted_expected_by_sequencer = None; - - self.sequencers.clear(); - self.published_messages.clear(); - self.submitted_deposits.clear(); - self.deposit_channel_notes.clear(); - self.submitted_withdraws.clear(); - self.account_balances.clear(); - self.published_order.clear(); - self.saved_checkpoints.clear(); - self.latest_checkpoints.clear(); - self.prepared_configs.clear(); - self.prepared_config_signatures.clear(); - self.expected_custom_payloads.clear(); - self.view_violations.clear(); - } - - pub fn remember_expected_custom_payloads(&mut self, payloads: Vec) { - self.expected_custom_payloads = payloads; - } - - #[must_use] - pub fn expected_custom_payloads(&self) -> &[Inscription] { - &self.expected_custom_payloads - } -} diff --git a/tests/zone-sdk/src/lib.rs b/tests/zone-sdk/src/lib.rs deleted file mode 100644 index d721eb6..0000000 --- a/tests/zone-sdk/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod cucumber; diff --git a/tests/zone-sdk/tests/cucumber.rs b/tests/zone-sdk/tests/cucumber.rs index 02da9db..00c8bb2 100644 --- a/tests/zone-sdk/tests/cucumber.rs +++ b/tests/zone-sdk/tests/cucumber.rs @@ -1,8 +1,246 @@ -use std::process::ExitCode; +//! Runs the Cucumber suites of the node's test harness at the pinned node +//! release. The harness is built against the `ZoneSDK` and Logos SQL of this +//! repository, see the `[patch]` section of the workspace manifest. The +//! features come from the harness itself. +use std::{ + collections::HashMap, + fs::OpenOptions, + io, + path::{Path, PathBuf}, + sync::{Arc, Mutex}, + time::{SystemTime, UNIX_EPOCH}, +}; -use logos_blockchain_zone_tests::cucumber::run; +use cucumber::{ + StatsWriter as _, World as _, WriterExt as _, event::ScenarioFinished, writer, + writer::Verbosity, +}; +use lb_testing_framework::{ + hash_str, is_truthy_env, record_system_monitor_event, register_system_monitor_output_file, + release_reserved_port_block, resolve_automatic_genesis_time, + unregister_system_monitor_output_file, +}; +use logos_blockchain_tests::cucumber::{ + defaults::{ + ARTEFACTS, CUCUMBER_REMOVE_ARTEFACTS_IF_SUCCESSFUL, MAX_CUCUMBER_CONCURRENT_SCENARIOS, + create_scenario_output_dir, get_feature_path, get_retries, init_logging_defaults, + init_tracing, + }, + world::{CucumberWorld, DeployerKind}, +}; + +type ScenarioAttempts = Arc>>; + +// Get the maximum number of concurrent scenarios from env var, defaults to 1 +fn get_max_concurrent_scenarios() -> usize { + std::env::var(MAX_CUCUMBER_CONCURRENT_SCENARIOS) + .ok() + .and_then(|val| val.parse().ok()) + .unwrap_or(1) +} + +// Increment and return the attempt count for the given scenario. Counts +// are tracked per-scenario, and keyed by a combination of feature and +// scenario name. +#[expect(clippy::significant_drop_tightening, reason = "Compiler weirdness")] +fn increment_attempts( + scenario_attempts: &ScenarioAttempts, + feature: &str, + scenario: &str, +) -> String { + let mut guard = scenario_attempts + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let key = format!("{feature}::{scenario}"); + let entry = guard.entry(key).or_insert(0); + *entry = entry.wrapping_add(1); + format!("attempt_{}", *entry) +} #[tokio::main] -async fn main() -> ExitCode { - run().await +async fn main() { + println!("args: {:?}", std::env::args()); + + let deployer = DeployerKind::Local; + + init_logging_defaults(); + init_tracing(); + + let scenario_attempts: ScenarioAttempts = Arc::new(Mutex::new(HashMap::new())); + + let output_dir = create_scenario_output_dir(); + let junit_xml_file = OpenOptions::new() + .append(true) + .create(true) + .open(output_dir.join("cucumber-output-junit.xml")) + .inspect_err(|err| println!("Failed to open output file: {err}")) + .expect("should create or open output file"); + let mut world = CucumberWorld::cucumber() + // Re-outputs Failed steps for easier navigation. + .repeat_failed() + // .fail_fast() // Remove comment to enable fail-fast behavior for development + .max_concurrent_scenarios(get_max_concurrent_scenarios()) + // Replaces Writer. + .with_writer( + writer::Summarize::new(writer::Basic::new( + io::stdout(), + // With `writer::Coloring::Auto`, cucumber treats the output as a TTY and using the + // underlying termcolor/console behaviour that can rewrite/clear lines when + // printing step statuses (✔ ...). That can visually clobber the + // immediately adjacent tracing line, especially the one emitted + // right as the step transitions from “running” to “passed”. + writer::Coloring::Never, + Verbosity::ShowWorldAndDocString, + )) + .tee::(writer::JUnit::for_tee(junit_xml_file, 0)) + .normalized(), + ) + // Ensure that all the steps were covered. Keep this after `with_writer`, which replaces + // the runner's writer and would otherwise discard the skipped-step failure wrapper. + .fail_on_skipped() + // Sets a hook, executed on each Scenario before running all its Steps, including Background + // ones. + .before(move |feature, _rule, scenario, world| { + Box::pin({ + let output_dir_clone = output_dir.clone(); + let scenario_attempts_clone = ScenarioAttempts::clone(&scenario_attempts); + async move { + println!( + "\nStarting - {}: {} ({}: {})\n", + scenario.keyword, scenario.name, feature.keyword, feature.name, + ); + prepare_world_for_scenario( + world, + deployer, + &output_dir_clone, + &scenario_attempts_clone, + &feature.name, + &scenario.name, + ); + } + }) + }); + + if let Some(retries) = get_retries() + .inspect_err(|e| println!("{e}")) + .expect("should parse retries") + { + // Makes failed Scenarios being retried the specified number of times. + world = world.retries(retries); + } + + let failed = world + .after(|feature, _rule, scenario, scenario_finished, world| { + Box::pin(async move { + // Runs after the scenario has completed; useful for capturing final state/logs. + println!( + "\nFinished - {}: {} ({}: {})\n", + scenario.keyword, scenario.name, feature.keyword, feature.name, + ); + + if let Some(world) = world { + if let Err(error) = world.stop_background_activity().await { + println!("Scenario background cleanup failed: {error}"); + } + + let path = world + .lifecycle + .scenario_base_dir + .join("debug_dump_file.log"); + if let Some(parent) = path.parent() { + let _unused = std::fs::create_dir_all(parent); + } + let _unused = std::fs::write(&path, world.full_debug_info_string()); + + if matches!(scenario_finished, ScenarioFinished::StepPassed) + && is_truthy_env(CUCUMBER_REMOVE_ARTEFACTS_IF_SUCCESSFUL) + { + println!( + "Env var '{CUCUMBER_REMOVE_ARTEFACTS_IF_SUCCESSFUL}' set, removing all \ + artefacts\n" + ); + if let Err(e) = world.clear_scenario_artifacts() { + println!("{e}"); + } + } + + unregister_system_monitor_output_file( + &world + .lifecycle + .scenario_base_dir + .join("system_stats.ndjson"), + ); + } + }) + }) + // Runs Cucumber. Features sourced from a Parser are fed to a Runner, which + // produces events handled by a Writer. + .run(get_feature_path()) + .await; + + // Release this process's reserved port block before exiting. + release_reserved_port_block(); + + if failed.execution_has_failed() { + std::process::exit(1); + } +} + +fn prepare_world_for_scenario( + world: &mut CucumberWorld, + deployer: DeployerKind, + output_dir: &Path, + scenario_attempts: &ScenarioAttempts, + feature_name: &str, + scenario_name: &str, +) { + world.set_deployer(deployer); + world.set_genesis_time(resolve_automatic_genesis_time()); + + if let Err(err) = world.preflight(deployer) { + println!("Preflight failed for scenario '{scenario_name}': {err}"); + } + + let scenario_dir = + scenario_output_dir(output_dir, scenario_attempts, feature_name, scenario_name); + + if let Err(err) = std::fs::create_dir_all(&scenario_dir) { + println!( + "Failed to create scenario artifact directory '{}': {err}", + scenario_dir.display() + ); + } + + register_system_monitor_output_file(&scenario_dir.join("system_stats.ndjson")); + record_system_monitor_event( + "cucumber_scenario_prepared", + scenario_dir.display().to_string(), + ); + + world.set_scenario_base_dir(&scenario_dir, &deployer); + world.set_scenario_name(scenario_name); + world.apply_deployment_config_override_path(); + + let started_at_ns = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos(); + + let raw_context = format!("{}::{started_at_ns}", scenario_dir.display()); + world.set_test_context(hash_str(&raw_context)); +} + +fn scenario_output_dir( + output_dir: &Path, + scenario_attempts: &ScenarioAttempts, + feature_name: &str, + scenario_name: &str, +) -> PathBuf { + let run_attempt = increment_attempts(scenario_attempts, feature_name, scenario_name); + + output_dir + .join(ARTEFACTS) + .join(feature_name) + .join(scenario_name.trim().replace(' ', "_")) + .join(run_attempt) } diff --git a/zone-sdk/Cargo.toml b/zone-sdk/Cargo.toml index 8db7d87..1eb88f3 100644 --- a/zone-sdk/Cargo.toml +++ b/zone-sdk/Cargo.toml @@ -28,7 +28,6 @@ lb-http-api-common = { workspace = true } lb-key-management-system-service = { workspace = true } lb-log-targets = { workspace = true } lb-testing-framework = { optional = true, workspace = true } -lb-time = { workspace = true } rand = { workspace = true } reqwest = { workspace = true } rpds = { workspace = true } @@ -36,7 +35,6 @@ serde = { features = ["derive"], workspace = true } testing-framework-app = { optional = true, workspace = true } testing-framework-core = { optional = true, workspace = true } thiserror = { workspace = true } -time = { features = ["std"], workspace = true } tokio = { features = ["macros", "rt", "sync", "time"], workspace = true } tracing = { workspace = true } diff --git a/zone-sdk/src/adapter.rs b/zone-sdk/src/adapter.rs index ff4aed0..72bed1e 100644 --- a/zone-sdk/src/adapter.rs +++ b/zone-sdk/src/adapter.rs @@ -22,10 +22,7 @@ use lb_core::{ }, }, }; -use lb_http_api_common::bodies::{ - mantle::LimitsResponseBody, - wallet::fund::{WalletFundRequestBody, WalletFundResponseBody}, -}; +use lb_http_api_common::bodies::wallet::fund::{WalletFundRequestBody, WalletFundResponseBody}; use lb_log_targets::zone_sdk; use reqwest::Url; use tracing::error; @@ -62,8 +59,6 @@ pub trait Node { async fn time_info(&self) -> Result; - async fn limits(&self) -> Result; - async fn channel_state(&self, channel_id: ChannelId) -> Result, Error>; async fn block_stream(&self) -> Result, Error>; @@ -166,10 +161,6 @@ impl Node for NodeHttpClient { self.client.time_info(self.base_url.clone()).await } - async fn limits(&self) -> Result { - self.client.limits(self.base_url.clone()).await - } - async fn channel_state(&self, channel_id: ChannelId) -> Result, Error> { self.client .channel_state(self.base_url.clone(), channel_id) @@ -459,12 +450,8 @@ mod tests { // tx_a mixes three ops so a walker that reused the wrong op's id // (or the wrong channel's) would surface here. - let our_deposit = deposit_op(channel_id, 1, Metadata::new_unchecked(b"to Alice".to_vec())); - let foreign_deposit = deposit_op( - other_channel, - 2, - Metadata::new_unchecked(b"to Bob".to_vec()), - ); + let our_deposit = deposit_op(channel_id, 1, b"to Alice".into()); + let foreign_deposit = deposit_op(other_channel, 2, b"to Bob".into()); let our_withdraw = withdraw_op(channel_id, 42); let tx_a = unverified_tx_with_ops(vec![ Op::ChannelDeposit(our_deposit.clone()), @@ -578,7 +565,7 @@ mod tests { let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(deposit_op( channel_id, 1, - Metadata::new_unchecked(b"to Alice".to_vec()), + b"to Alice".into(), ))]); drop(block_to_messages(vec![tx], channel_id, &HashMap::new())); @@ -598,10 +585,6 @@ mod tests { self.0.time_info().await } - async fn limits(&self) -> Result { - self.0.limits().await - } - async fn channel_state( &self, channel_id: ChannelId, @@ -664,7 +647,7 @@ mod tests { #[tokio::test] async fn zone_messages_in_blocks_report_a_deposit_with_its_event() { let channel_id = ChannelId::from([0; 32]); - let op = deposit_op(channel_id, 1, Metadata::new_unchecked(b"to Alice".to_vec())); + let op = deposit_op(channel_id, 1, b"to Alice".into()); let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(op.clone())]); let events = deposit_event(&tx, &op, 1234, Vec::new()); let node = node_with_block(api_block(1, 0, 1, vec![tx]), Some(events)); @@ -691,7 +674,7 @@ mod tests { #[tokio::test] async fn zone_messages_in_blocks_fail_without_the_block_events() { let channel_id = ChannelId::from([0; 32]); - let op = deposit_op(channel_id, 1, Metadata::new_unchecked(b"to Alice".to_vec())); + let op = deposit_op(channel_id, 1, b"to Alice".into()); let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(op)]); let node = node_with_block(api_block(1, 0, 1, vec![tx]), None); @@ -705,8 +688,8 @@ mod tests { #[tokio::test] async fn zone_messages_in_block_fail_without_an_event_of_each_deposit() { let channel_id = ChannelId::from([0; 32]); - let first = deposit_op(channel_id, 1, Metadata::new_unchecked(b"to Alice".to_vec())); - let second = deposit_op(channel_id, 2, Metadata::new_unchecked(b"to Bob".to_vec())); + let first = deposit_op(channel_id, 1, b"to Alice".into()); + let second = deposit_op(channel_id, 2, b"to Bob".into()); let tx = unverified_tx_with_ops(vec![ Op::ChannelDeposit(first.clone()), Op::ChannelDeposit(second), diff --git a/zone-sdk/src/lib.rs b/zone-sdk/src/lib.rs index 1c9a8db..815bfe1 100644 --- a/zone-sdk/src/lib.rs +++ b/zone-sdk/src/lib.rs @@ -116,9 +116,8 @@ pub mod node_types { transactions::states::Unverified, }, }; - pub use lb_http_api_common::bodies::{ - mantle::LimitsResponseBody, - wallet::fund::{WalletFundRequestBody, WalletFundResponseBody}, + pub use lb_http_api_common::bodies::wallet::fund::{ + WalletFundRequestBody, WalletFundResponseBody, }; } diff --git a/zone-sdk/src/sequencer/actor/tests.rs b/zone-sdk/src/sequencer/actor/tests.rs index 188f388..8edcc9a 100644 --- a/zone-sdk/src/sequencer/actor/tests.rs +++ b/zone-sdk/src/sequencer/actor/tests.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, num::NonZero, sync::Arc}; +use std::{collections::HashSet, sync::Arc}; use lb_core::{ header::HeaderId, @@ -11,13 +11,13 @@ use lb_core::{ channel::{ MsgId, UnverifiedChannelKeys, VerifiedChannelKeys, config::ChannelConfigOp, - deposit::{DepositOp, Metadata}, + deposit::DepositOp, inscribe::{Inscription, InscriptionOp}, withdraw::ChannelWithdrawOp, }, }, traits::Hashable as _, - transactions::{OpProofs, Ops, TxList, states::Unverified}, + transactions::{OpProofs, Ops, states::Unverified}, }, }; use lb_key_management_system_service::keys::{Ed25519Key, ZkKey}; @@ -36,8 +36,8 @@ use super::{ *, }; use crate::test_support::{ - FORK_DIGEST, MockNode, StreamEnd, StreamScript, api_block, funding_config, header_id, - inscribe_op, live_event, scripts, single_key_channel_state, unverified_tx_with_ops, + MockNode, StreamEnd, StreamScript, api_block, funding_config, header_id, inscribe_op, + live_event, scripts, single_key_channel_state, unverified_tx_with_ops, }; #[must_use] @@ -75,15 +75,15 @@ async fn prepare_submit_deposit_and_inscription() { let deposit_op = DepositOp { channel_id, inputs: BoundedInputs::from(utxo.id()).into(), - metadata: Metadata::new_unchecked(b"to Alice".to_vec()), + metadata: b"to Alice".into(), }; // Build a `MantleTx` via the handle let (tx, msg_id, inscription_sig) = sequencer .handle() .prepare_tx( - Ops::new(FORK_DIGEST, [Op::ChannelDeposit(deposit_op.clone())]), - Inscription::new_unchecked(b"Mint 10 to Alice".to_vec()), + [Op::ChannelDeposit(deposit_op.clone())].into(), + b"Mint 10 to Alice".into(), ) .unwrap(); assert_eq!(tx.inner().len(), 2); @@ -138,10 +138,7 @@ async fn submit_signed_tx_refuses_a_parent_with_a_pending_child() { let (tx, msg_id, inscription_sig) = sequencer .handle() - .prepare_tx( - Ops::new(FORK_DIGEST, TxList::new_unchecked(Vec::new())), - Inscription::new_unchecked(b"prepared first".to_vec()), - ) + .prepare_tx(Ops::new_unchecked(Vec::new()), b"prepared first".into()) .unwrap(); let signed_tx = SignedOps::from_parts(tx, OpProofs::from([OpProof::Ed25519Sig(inscription_sig)])) @@ -149,7 +146,7 @@ async fn submit_signed_tx_refuses_a_parent_with_a_pending_child() { sequencer .handle() - .publish(Inscription::new_unchecked(b"published in between".to_vec())) + .publish(b"published in between".into()) .await .unwrap(); @@ -226,17 +223,14 @@ async fn submit_signed_tx_refuses_an_unknown_config_parent() { let channel_id = ChannelId::from([0; 32]); let key = Ed25519Key::from_bytes(&[0; 32]); let mut sequencer = ready_sequencer_with_channel(None, key.clone()).await; - let config_on = |parent: MsgId| { - ChannelConfigOp::new( - channel_id, - parent, - VerifiedChannelKeys::try_from(vec![key.public_key()]).unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap() + let config_on = |parent: MsgId| ChannelConfigOp { + channel: channel_id, + parent, + keys: VerifiedChannelKeys::try_from(vec![key.public_key()]).unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, }; let tip = sequencer.compute_publish_parent(); @@ -301,7 +295,7 @@ async fn gap_deposits_surface_after_a_failed_backfill_and_a_fork_switch() { let op = DepositOp { channel_id, inputs: BoundedInputs::from(NoteId::from(Fr::from(n))).into(), - metadata: Metadata::new_unchecked(vec![u8::try_from(n).unwrap()]), + metadata: Metadata::try_from(vec![u8::try_from(n).unwrap()]).unwrap(), }; let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(op.clone())]); let note = DepositNote { @@ -458,7 +452,7 @@ async fn drive_stale_publish( break; } } - let publish = client.publish(Inscription::new_unchecked(b"stale".to_vec())); + let publish = client.publish(b"stale".into()); tokio::select! { result = publish => drop(result.expect("publish is accepted after Ready")), () = async { loop { drop(sequencer.next_event().await); } } => unreachable!(), @@ -530,7 +524,7 @@ async fn funding_record_survives_a_checkpoint_round_trip() { break; } } - let publish = client.publish(Inscription::new_unchecked(b"restored".to_vec())); + let publish = client.publish(b"restored".into()); let (result, checkpoint) = tokio::select! { result = publish => result.expect("publish is accepted after Ready"), () = async { loop { drop(sequencer.next_event().await); } } => unreachable!(), @@ -788,7 +782,7 @@ async fn client_publish_fails_fast_during_reconnect_and_recovers() { // `wait_reconnect_delay` while the node is still down. With the old // behavior the request would never be drained during reconnect and this // would hang (caught by the timeout). - let publish = client.publish(Inscription::new_unchecked(b"during-reconnect".to_vec())); + let publish = client.publish(b"during-reconnect".into()); let result = tokio::time::timeout(std::time::Duration::from_secs(5), async { tokio::select! { result = publish => result, @@ -815,7 +809,7 @@ async fn client_publish_fails_fast_during_reconnect_and_recovers() { .expect("Ready should be re-announced after reconnect"); // Publishing works again and the inscription is posted. - let publish = client.publish(Inscription::new_unchecked(b"after-reconnect".to_vec())); + let publish = client.publish(b"after-reconnect".into()); tokio::time::timeout(std::time::Duration::from_secs(5), async { tokio::select! { result = publish => result, @@ -967,11 +961,11 @@ async fn assert_turns_alternate_without_blocks(posting_timeframe: u32, posting_t channel.posting_timeout = posting_timeout.into(); let node = MockNode { channel_state: Some(channel), - slot_duration: core::time::Duration::from_millis(100), + slot_duration_ms: 100, ..MockNode::default() }; let config = SequencerConfig { - resubmit_interval: core::time::Duration::from_secs(600), + resubmit_interval: std::time::Duration::from_secs(600), ..SequencerConfig::new(funding_config()) }; let mut sequencer = @@ -1014,7 +1008,7 @@ async fn turn_notification_closes_with_the_publish_margin() { channel.posting_timeframe = 4u32.into(); let node = MockNode { channel_state: Some(channel), - slot_duration: core::time::Duration::from_millis(300), + slot_duration_ms: 300, ..MockNode::default() }; let config = SequencerConfig { @@ -1089,20 +1083,19 @@ async fn publish_after_bundle_chains_on_the_bundle_inscription_tip() { let inscribe = InscriptionOp { channel_id, - inscription: Inscription::new_unchecked(b"bundle".to_vec()), + inscription: b"bundle".to_vec().try_into().unwrap(), parent: MsgId::root(), signer: sequencer_key.public_key().into_unverified(), }; - let config = ChannelConfigOp::new( - channel_id, - MsgId::root(), - VerifiedChannelKeys::try_from(vec![sequencer_key.public_key()]).unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap(); + let config = ChannelConfigOp { + channel: channel_id, + parent: MsgId::root(), + keys: VerifiedChannelKeys::try_from(vec![sequencer_key.public_key()]).unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + }; let inscribe_msg = inscribe.id(); let bundle = unverified_tx_with_ops(vec![ Op::ChannelInscribe(inscribe), @@ -1121,7 +1114,7 @@ async fn publish_after_bundle_chains_on_the_bundle_inscription_tip() { let (published, _cp) = sequencer .handle() - .publish(Inscription::new_unchecked(b"after-bundle".to_vec())) + .publish(b"after-bundle".into()) .await .expect("publish after bundle should be accepted"); assert_eq!( @@ -1136,16 +1129,16 @@ async fn config_only_block_orphans_pending_inscription_but_keeps_message_tip() { let channel_id = ChannelId::from([0; 32]); let sequencer_key = Ed25519Key::from_bytes(&[0; 32]); - let config_op = ChannelConfigOp::new( - channel_id, - MsgId::root(), - VerifiedChannelKeys::try_from(vec![Ed25519Key::from_bytes(&[0; 32]).public_key()]).unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap(); + let config_op = ChannelConfigOp { + channel: channel_id, + parent: MsgId::root(), + keys: VerifiedChannelKeys::try_from(vec![Ed25519Key::from_bytes(&[0; 32]).public_key()]) + .unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + }; let config_tx = unverified_tx_with_ops(vec![Op::ChannelConfig(config_op)]); let config_hash = config_tx.hash(); let config_block = api_block(2, 1, 2, vec![config_tx]); @@ -1182,7 +1175,7 @@ async fn config_only_block_orphans_pending_inscription_but_keeps_message_tip() { } } - let publish = client.publish(Inscription::new_unchecked(b"survives-config".to_vec())); + let publish = client.publish(b"survives-config".into()); let (result, _checkpoint) = tokio::time::timeout(std::time::Duration::from_secs(5), async { tokio::select! { result = publish => result, @@ -1261,19 +1254,16 @@ fn track_pending_tx_classifies_atomic_bundle_with_withdraws() { }; let inscribe_op = InscriptionOp { channel_id, - inscription: Inscription::new_unchecked(b"hello".to_vec()), + inscription: Inscription::try_from(b"hello".to_vec()).unwrap(), parent: MsgId::root(), signer: Ed25519Key::from_bytes(&[0; 32]) .public_key() .into_unverified(), }; - let mantle_tx = Ops::new( - FORK_DIGEST, - [ - Op::ChannelWithdraw(withdraw_op.clone()), - Op::ChannelInscribe(inscribe_op), - ], - ); + let mantle_tx = Ops::from([ + Op::ChannelWithdraw(withdraw_op.clone()), + Op::ChannelInscribe(inscribe_op), + ]); let tx_hash = mantle_tx.hash(); let signed_tx = SignedOps::from_ops_with_sample_proofs(mantle_tx); @@ -1300,13 +1290,13 @@ fn track_pending_tx_classifies_plain_inscription_with_none_withdraws() { let channel_id = ChannelId::from([2u8; 32]); let inscribe_op = InscriptionOp { channel_id, - inscription: Inscription::new_unchecked(b"hello".to_vec()), + inscription: Inscription::try_from(b"hello".to_vec()).unwrap(), parent: MsgId::root(), signer: Ed25519Key::from_bytes(&[0; 32]) .public_key() .into_unverified(), }; - let mantle_tx = Ops::new(FORK_DIGEST, [Op::ChannelInscribe(inscribe_op)]); + let mantle_tx = Ops::from([Op::ChannelInscribe(inscribe_op)]); let tx_hash = mantle_tx.hash(); let signed_tx = SignedOps::from_ops_with_sample_proofs(mantle_tx); @@ -1327,13 +1317,13 @@ fn track_pending_tx_falls_back_to_other_when_no_inscribe_for_channel() { let other_channel = ChannelId::from([4u8; 32]); let inscribe_op = InscriptionOp { channel_id: other_channel, - inscription: Inscription::new_unchecked(b"hello".to_vec()), + inscription: Inscription::try_from(b"hello".to_vec()).unwrap(), parent: MsgId::root(), signer: Ed25519Key::from_bytes(&[0; 32]) .public_key() .into_unverified(), }; - let mantle_tx = Ops::new(FORK_DIGEST, [Op::ChannelInscribe(inscribe_op)]); + let mantle_tx = Ops::from([Op::ChannelInscribe(inscribe_op)]); let tx_hash = mantle_tx.hash(); let signed_tx = SignedOps::from_ops_with_sample_proofs(mantle_tx); @@ -1387,7 +1377,7 @@ async fn cold_start_backfills_genesis_slot() { let deposit = DepositOp { channel_id, inputs: BoundedInputs::from(NoteId::from(Fr::from(1u64))).into(), - metadata: Metadata::new_unchecked(vec![1u8]), + metadata: Metadata::try_from(vec![1u8]).unwrap(), }; let deposit_tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(deposit.clone())]); let note_id = NoteId::from(Fr::from(1001u64)); @@ -1474,17 +1464,14 @@ async fn cold_start_backfill_finalizes_configs_with_their_keys() { let channel_id = ChannelId::from([7; 32]); let sequencer_key = Ed25519Key::from_bytes(&[0; 32]); let peer_key = Ed25519Key::from_bytes(&[1; 32]); - let config = |parent: MsgId, keys: Vec<_>| { - ChannelConfigOp::new( - channel_id, - parent, - VerifiedChannelKeys::try_from(keys).unwrap(), - SlotTimeframe::from(3u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap() + let config = |parent: MsgId, keys: Vec<_>| ChannelConfigOp { + channel: channel_id, + parent, + keys: VerifiedChannelKeys::try_from(keys).unwrap(), + posting_timeframe: SlotTimeframe::from(3u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, }; let accredit = config( MsgId::root(), @@ -1547,7 +1534,7 @@ async fn cold_start_backfill_finalizes_configs_with_their_keys() { configs[0].config, accredit, "the mined config, keys included" ); - assert_eq!(configs[0].config.keys().len(), 2); + assert_eq!(configs[0].config.keys.len(), 2); assert_eq!(configs[1].tx_hash, remove_tx.hash()); assert_eq!(configs[1].parent_msg, accredit.id()); assert_eq!(configs[1].this_msg, remove.id()); @@ -1555,7 +1542,7 @@ async fn cold_start_backfill_finalizes_configs_with_their_keys() { configs[1].config, remove, "the removal, with the one key left" ); - assert_eq!(configs[1].config.keys().len(), 1); + assert_eq!(configs[1].config.keys.len(), 1); } /// Cold start over finalized history that is empty but for a block at @@ -1730,16 +1717,16 @@ async fn resume_backfill_refines_finalized_config_from_a_replayed_config() { let sequencer_key = Ed25519Key::from_bytes(&[0; 32]); // A pure config sits in the finalized history the backfill replays. - let config_op = ChannelConfigOp::new( - channel_id, - MsgId::root(), - VerifiedChannelKeys::try_from(vec![Ed25519Key::from_bytes(&[0; 32]).public_key()]).unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap(); + let config_op = ChannelConfigOp { + channel: channel_id, + parent: MsgId::root(), + keys: VerifiedChannelKeys::try_from(vec![Ed25519Key::from_bytes(&[0; 32]).public_key()]) + .unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + }; let config_id = config_op.id(); let config_tx = unverified_tx_with_ops(vec![Op::ChannelConfig(config_op)]); // The config finalized during downtime at slot 1 — above the checkpoint @@ -2341,7 +2328,7 @@ async fn channel_config_signs_with_own_current_accredited_index() { .expect("config update from an accredited non-leading key must build"); assert_eq!( - config_op_of(&signed_ops).parent(), + config_op_of(&signed_ops).parent, MsgId::root(), "parent must equal the channel's config tip" ); @@ -2393,7 +2380,7 @@ async fn channel_config_on_unclaimed_channel_carries_empty_proof() { }; assert!(proof.signatures().is_empty()); assert_eq!( - config_op_of(&signed_ops).parent(), + config_op_of(&signed_ops).parent, MsgId::root(), "claiming an unclaimed channel must be rooted at ZERO" ); @@ -2425,36 +2412,6 @@ async fn channel_config_fails_when_not_accredited() { ); } -/// A config the ledger would reject (a zero threshold, or a configuration -/// threshold above the key count) fails locally, before it is funded. -#[tokio::test] -async fn channel_config_rejects_an_ill_formed_config() { - let own_key = Ed25519Key::from_bytes(&[7; 32]); - let mut sequencer = ready_sequencer_with_channel(None, own_key.clone()).await; - - for (configuration_threshold, transfer_threshold, reason) in [ - (2, 1, "exceeds the 1 accredited keys"), - (0, 1, "configuration_threshold must be at least 1"), - (1, 0, "transfer_threshold must be at least 1"), - ] { - let error = sequencer - .handle() - .channel_config( - VerifiedChannelKeys::new_unchecked(vec![own_key.public_key()]), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - configuration_threshold, - transfer_threshold, - ) - .await - .expect_err("an ill-formed config must fail locally"); - assert!( - error.to_string().contains(reason), - "unexpected error: {error}" - ); - } -} - /// `configuration_threshold > 1` needs signatures the sequencer cannot /// collect; reject early with a clear error. #[tokio::test] @@ -2522,7 +2479,7 @@ async fn submit_atomic_bundle_rejects_a_bundle_prepared_by_another_sequencer() { let sign_payload = vec![0xab; 32]; let bundle_by = |preparer: &Ed25519Key| PreparedAtomicBundle { - tx: Ops::new(FORK_DIGEST, TxList::new_unchecked(Vec::new())), + tx: Ops::new_unchecked(Vec::new()), transfer_proof: None, pre_fund: MantleTxBuilder::new(), inscribe_sig: preparer.sign_payload(&sign_payload), @@ -2614,7 +2571,7 @@ async fn a_second_config_on_a_pending_config_position_is_refused() { .await; assert_eq!( - config_op_of(&first_tx).parent(), + config_op_of(&first_tx).parent, MsgId::root(), "the config claiming an unclaimed channel must be rooted at ZERO" ); diff --git a/zone-sdk/src/sequencer/backfill.rs b/zone-sdk/src/sequencer/backfill.rs index 306b6b5..09a72ee 100644 --- a/zone-sdk/src/sequencer/backfill.rs +++ b/zone-sdk/src/sequencer/backfill.rs @@ -3,11 +3,10 @@ reason = "`ZoneSequencer` impl is split across actor.rs / backfill.rs / zone_sequencer.rs by concern for navigability." )] -use std::num::NonZero; +use std::time::{Duration, SystemTime}; -use lb_common_http_client::{ChainServiceInfo, EraTiming as ReportedEraTiming, Slot, TimeInfo}; +use lb_common_http_client::{ChainServiceInfo, Slot, TimeInfo}; use lb_core::mantle::ops::channel::MsgId; -use lb_time::era::{EraEntriesAfterGenesis, EraTiming, EraTimingSchedule}; use tracing::{debug, error, info, warn}; use super::{ @@ -170,16 +169,15 @@ where /// sequencer is considered connected. /// /// `current_tip` stays None so the first live block event emits everything - /// from LIB up to the new tip as `adopted`. On reconnect, once both `state` - /// and `slot_clock` are initialized, only the era schedule is taken again - /// (see [`Self::refresh_era_schedule`]). + /// from LIB up to the new tip as `adopted`. On reconnect this is a no-op + /// once both `state` and `slot_clock` are initialized. #[expect( clippy::cognitive_complexity, reason = "TODO: address this in a dedicated refactor" )] async fn init_state_if_needed(&mut self) -> bool { if self.state.is_some() && self.slot_clock.is_some() { - return self.refresh_era_schedule().await; + return true; } let timing_info = match self.node.time_info().await { Ok(info) => info, @@ -226,35 +224,32 @@ where } } - /// Takes the node's era schedule again into the slot clock, which keeps the - /// slot it last observed. The node may have been upgraded with another - /// schedule while the sequencer was disconnected, an era postponed for - /// example. - async fn refresh_era_schedule(&mut self) -> bool { - let eras = match self.node.time_info().await { - Ok(timing_info) => era_schedule(&timing_info), - Err(err) => Err(Error::Network(format!("failed to fetch time info: {err}"))), - }; - match eras { - Ok(eras) => { - if let Some(slot_clock) = self.slot_clock.as_mut() { - slot_clock.set_era_schedule(eras); - } - true - } - Err(err) => { - warn!(target: TARGET, "Failed to refresh the era schedule: {err}"); - self.wait_reconnect_delay().await; - false - } - } - } - fn build_initial_slot_clock( observed_slot: Slot, timing_info: &TimeInfo, ) -> Result { - let mut slot_clock = SlotClock::from_era_schedule(era_schedule(timing_info)?); + let slot_duration = Duration::from_millis(timing_info.slot_duration_ms); + if slot_duration.is_zero() { + return Err(Error::Network( + "node reported slot_duration_ms=0 for time info".to_owned(), + )); + } + + let genesis_ms = u64::try_from(timing_info.genesis_time_unix_ms).map_err(|_| { + Error::Network(format!( + "node reported negative genesis_time_unix_ms: {}", + timing_info.genesis_time_unix_ms + )) + })?; + let chain_start_time = SystemTime::UNIX_EPOCH + .checked_add(Duration::from_millis(genesis_ms)) + .ok_or_else(|| { + Error::Network(format!( + "node reported out-of-range genesis_time_unix_ms: {genesis_ms}" + )) + })?; + + let mut slot_clock = SlotClock::from_chain_start_time(chain_start_time, slot_duration); slot_clock.observe_slot(observed_slot); Ok(slot_clock) } @@ -318,45 +313,3 @@ where true } } - -/// The chain's eras as the node reports them, resolved as the node resolves -/// them. -fn era_schedule(timing_info: &TimeInfo) -> Result { - let map_invalid_error_with_reason = - |reason: &str| Error::Network(format!("node reported invalid eras: {reason}")); - let map_reported_era_timing = |era: &ReportedEraTiming| { - if era.slot_duration.is_zero() { - return Err(map_invalid_error_with_reason("a slot duration of 0")); - } - Ok(EraTiming { - slot_duration: era.slot_duration, - epoch_length: era.slots_per_epoch, - }) - }; - - let mut eras = timing_info.era_timings.iter(); - let (&genesis_epoch, genesis) = eras - .next() - .ok_or_else(|| map_invalid_error_with_reason("empty"))?; - if genesis_epoch != 0 { - return Err(map_invalid_error_with_reason( - "the first era does not start at genesis", - )); - } - let after_genesis = eras - .map(|(&first_epoch, era)| { - let first_epoch = NonZero::new(first_epoch) - .expect("eras are keyed by unique epochs, and only the first is at epoch 0"); - Ok((first_epoch, map_reported_era_timing(era)?)) - }) - .collect::, Error>>()?; - let after_genesis = EraEntriesAfterGenesis::try_from_iter(after_genesis) - .map_err(|error| map_invalid_error_with_reason(&error.to_string()))?; - - EraTimingSchedule::new( - timing_info.genesis_time.into(), - map_reported_era_timing(genesis)?, - after_genesis, - ) - .map_err(|error| map_invalid_error_with_reason(&error.to_string())) -} diff --git a/zone-sdk/src/sequencer/block_fetch/mod.rs b/zone-sdk/src/sequencer/block_fetch/mod.rs index e635e81..5bd54b5 100644 --- a/zone-sdk/src/sequencer/block_fetch/mod.rs +++ b/zone-sdk/src/sequencer/block_fetch/mod.rs @@ -527,15 +527,13 @@ pub fn channel_configs( let tx_hash = tx.op_refs().hash(); tx.op_refs_iter() .filter_map(|op| match op { - OpRef::ChannelConfig(config) if config.channel() == channel_id => { - Some(InscriptionInfo { - tx_hash, - parent_msg: config.parent(), - this_msg: config.id(), - payload: Inscription::new_unchecked(Vec::new()), - signer: None, - }) - } + OpRef::ChannelConfig(config) if config.channel == channel_id => Some(InscriptionInfo { + tx_hash, + parent_msg: config.parent, + this_msg: config.id(), + payload: Inscription::new_unchecked(Vec::new()), + signer: None, + }), _ => None, }) .collect() @@ -764,10 +762,10 @@ fn extract_finalized_items( }; ops.push(FinalizedOp::Inscription(info)); } - OpRef::ChannelConfig(config) if config.channel() == channel_id => { + OpRef::ChannelConfig(config) if config.channel == channel_id => { ops.push(FinalizedOp::Config(ConfigInfo { tx_hash, - parent_msg: config.parent(), + parent_msg: config.parent, this_msg: config.id(), config: config.clone(), })); @@ -1023,7 +1021,7 @@ pub(super) fn classify_channel_tx( }); *block_tip = Some(this_msg); } - OpRef::ChannelConfig(config) if config.channel() == channel_id => { + OpRef::ChannelConfig(config) if config.channel == channel_id => { configs += 1; // Configs sit on the separate config lineage — `this_msg` is a // config id, `parent_msg` its config parent, payload empty. @@ -1032,9 +1030,9 @@ pub(super) fn classify_channel_tx( // node's single tip. config_entries.push(InscriptionInfo { tx_hash, - parent_msg: config.parent(), + parent_msg: config.parent, this_msg: config.id(), - payload: Inscription::empty(), + payload: [].into(), signer: None, }); } @@ -1113,7 +1111,7 @@ pub(super) fn is_pure_config( let mut transfers = 0usize; for op in tx.op_refs_iter() { match op { - OpRef::ChannelConfig(config) if config.channel() == channel_id => configs += 1, + OpRef::ChannelConfig(config) if config.channel == channel_id => configs += 1, OpRef::ChannelInscribe(inscribe) if inscribe.channel_id == channel_id => return false, OpRef::ChannelWithdraw(withdraw) if withdraw.channel_id == channel_id => return false, OpRef::Transfer(_) => transfers += 1, @@ -1154,7 +1152,7 @@ fn touches_channel_tip( ) -> bool { tx.op_refs().iter().any(|op| match op { OpRef::ChannelInscribe(inscribe) => inscribe.channel_id == channel_id, - OpRef::ChannelConfig(set_keys) => set_keys.channel() == channel_id, + OpRef::ChannelConfig(set_keys) => set_keys.channel == channel_id, _ => false, }) } diff --git a/zone-sdk/src/sequencer/block_fetch/tests.rs b/zone-sdk/src/sequencer/block_fetch/tests.rs index 37e0455..ef13a89 100644 --- a/zone-sdk/src/sequencer/block_fetch/tests.rs +++ b/zone-sdk/src/sequencer/block_fetch/tests.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, num::NonZero}; +use std::collections::HashMap; use lb_core::{ crypto::Hash, @@ -27,7 +27,7 @@ use super::*; use crate::{ adapter::DepositEvent, test_support::{ - FORK_DIGEST, MockNode, api_block, deposit_event, header_id, inscribe_op, live_event, + MockNode, api_block, deposit_event, header_id, inscribe_op, live_event, unverified_tx_with_ops, }, }; @@ -76,12 +76,8 @@ fn extract_deposits_returns_matching_amount() { let channel_id = ChannelId::from([0; 32]); let other_channel = ChannelId::from([1; 32]); - let deposit_for_us = deposit_op(channel_id, 1, Metadata::new_unchecked(b"to Alice".to_vec())); - let deposit_other_channel = deposit_op( - other_channel, - 2, - Metadata::new_unchecked(b"to Bob".to_vec()), - ); + let deposit_for_us = deposit_op(channel_id, 1, b"to Alice".into()); + let deposit_other_channel = deposit_op(other_channel, 2, b"to Bob".into()); let our_op_id = deposit_for_us.op_id(); let tx = unverified_tx_with_ops(vec![ @@ -103,7 +99,7 @@ fn extract_deposits_returns_matching_amount() { assert_eq!(d.tx_hash, tx_hash); assert_eq!(d.op_id, our_op_id); assert_eq!(d.amount, 1234); - assert_eq!(d.metadata, Metadata::new_unchecked(b"to Alice".to_vec())); + assert_eq!(d.metadata, b"to Alice".into()); assert_eq!(d.inputs, deposit_for_us.inputs); } @@ -117,7 +113,7 @@ fn extract_finalized_items_panics_if_deposit_events_incomplete() { // gap. A panic here surfaces the bug immediately if a future caller // violates that invariant — silent skip would drop a real deposit. let channel_id = ChannelId::from([0; 32]); - let op = deposit_op(channel_id, 1, Metadata::new_unchecked(b"to Alice".to_vec())); + let op = deposit_op(channel_id, 1, b"to Alice".into()); let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(op)]); drop(extract_finalized_items( std::slice::from_ref(&tx), @@ -130,9 +126,9 @@ fn extract_finalized_items_panics_if_deposit_events_incomplete() { #[test] fn extract_deposits_preserves_tx_and_op_order() { let channel_id = ChannelId::from([0; 32]); - let d1 = deposit_op(channel_id, 1, Metadata::new_unchecked(b"first".to_vec())); - let d2 = deposit_op(channel_id, 2, Metadata::new_unchecked(b"second".to_vec())); - let d3 = deposit_op(channel_id, 3, Metadata::new_unchecked(b"third".to_vec())); + let d1 = deposit_op(channel_id, 1, b"first".into()); + let d2 = deposit_op(channel_id, 2, b"second".into()); + let d3 = deposit_op(channel_id, 3, b"third".into()); let id1 = d1.op_id(); let id2 = d2.op_id(); let id3 = d3.op_id(); @@ -166,11 +162,7 @@ fn extract_finalized_items_interleaves_deposit_then_inscription_in_same_tx() { // (e.g. LEZ) can validate references from the inscription back to // the just-finalized deposit. let channel_id = ChannelId::from([0; 32]); - let dep = deposit_op( - channel_id, - 1, - Metadata::new_unchecked(b"deposit-meta".to_vec()), - ); + let dep = deposit_op(channel_id, 1, b"deposit-meta".into()); let dep_op_id = dep.op_id(); let inscribe = InscriptionOp { channel_id, @@ -220,11 +212,7 @@ fn deposit_plus_inscription_is_adopted_as_custom() { // race and invalidate the author's deposit), and it is not mirrored // for retry — its author recovers it. let channel_id = ChannelId::from([0; 32]); - let dep = deposit_op( - channel_id, - 1, - Metadata::new_unchecked(b"deposit-meta".to_vec()), - ); + let dep = deposit_op(channel_id, 1, b"deposit-meta".into()); let inscribe = inscribe_op(channel_id, MsgId::root(), b"bridge"); let msg_id = inscribe.id(); let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(dep), Op::ChannelInscribe(inscribe)]); @@ -657,39 +645,27 @@ fn extract_finalized_items_surfaces_standalone_withdraw() { } fn channel_config(channel_id: ChannelId, parent: MsgId) -> ChannelConfigOp { - channel_config_with_posting_timeframe(channel_id, parent, SlotTimeframe::from(0u32)) -} - -fn channel_config_with_posting_timeframe( - channel_id: ChannelId, - parent: MsgId, - posting_timeframe: SlotTimeframe, -) -> ChannelConfigOp { let signer = Ed25519Key::from_bytes(&[0u8; 32]).public_key(); - ChannelConfigOp::new( - channel_id, + ChannelConfigOp { + channel: channel_id, parent, - VerifiedChannelKeys::try_from(vec![signer]).unwrap(), - posting_timeframe, - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap() + keys: VerifiedChannelKeys::try_from(vec![signer]).unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + } } fn dummy_pending_tx(seed: u8) -> SignedOps { - let mantle_tx = Ops::new( - FORK_DIGEST, - [Op::ChannelInscribe(InscriptionOp { - channel_id: [0u8; 32].into(), - inscription: Inscription::new_unchecked(vec![seed]), - parent: MsgId::root(), - signer: Ed25519Key::from_bytes(&[seed; 32]) - .public_key() - .into_unverified(), - })], - ); + let mantle_tx = Ops::from([Op::ChannelInscribe(InscriptionOp { + channel_id: [0u8; 32].into(), + inscription: Inscription::new_unchecked(vec![seed]), + parent: MsgId::root(), + signer: Ed25519Key::from_bytes(&[seed; 32]) + .public_key() + .into_unverified(), + })]); let op_proofs = OpProofs::from([OpProof::Ed25519Sig(Ed25519Signature::zero())]); SignedOps::from_parts(mantle_tx, op_proofs) .expect("Should generate a valid transaction with valid matching proofs.") @@ -813,8 +789,8 @@ fn extract_finalized_items_surfaces_configs_with_their_own_parent() { assert_eq!(info.parent_msg, parent); assert_eq!(info.this_msg, config_id); assert_eq!(info.config.id(), config_id); - assert_eq!(info.config.keys().len(), 1, "the mined keys travel with it"); - assert_eq!(info.config.transfer_threshold(), NonZero::::MIN); + assert_eq!(info.config.keys.len(), 1, "the mined keys travel with it"); + assert_eq!(info.config.transfer_threshold, 1); } other => panic!("expected Config, got {other:?}"), } @@ -829,7 +805,7 @@ async fn canonical_backfill_gap_deposits_are_observed() { let pk = lb_key_management_system_service::keys::ZkPublicKey::from(Fr::from(7u64)); let deposit = |n: u32| { let tag = u8::try_from(n).unwrap(); - let op = deposit_op(ch, n, Metadata::new_unchecked(vec![tag])); + let op = deposit_op(ch, n, Metadata::try_from(vec![tag]).unwrap()); let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(op.clone())]); let note = DepositNote { note_id: NoteId::from(Fr::from(1000 + u64::from(n))), @@ -868,7 +844,7 @@ async fn deposit_events_failure_on_a_later_gap_block_applies_nothing() { let pk = lb_key_management_system_service::keys::ZkPublicKey::from(Fr::from(7u64)); let deposit = |n: u32| { let tag = u8::try_from(n).unwrap(); - let op = deposit_op(ch, n, Metadata::new_unchecked(vec![tag])); + let op = deposit_op(ch, n, Metadata::try_from(vec![tag]).unwrap()); let tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(op.clone())]); let note = DepositNote { note_id: NoteId::from(Fr::from(1000 + u64::from(n))), @@ -1306,7 +1282,7 @@ async fn fork_block_deposit_is_reported_when_its_branch_turns_canonical() { // F <- E: F wins; B2 <- B3: F loses; E <- E2: F wins again. let ch = ChannelId::from([0u8; 32]); let pk = lb_key_management_system_service::keys::ZkPublicKey::from(Fr::from(7u64)); - let dep = deposit_op(ch, 1, Metadata::new_unchecked(b"d".to_vec())); + let dep = deposit_op(ch, 1, Metadata::try_from(b"d".to_vec()).unwrap()); let d = dep.op_id(); let dep_tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(dep.clone())]); let note = DepositNote { @@ -1490,7 +1466,7 @@ struct PinFixture { fn pin_fixture(ch: ChannelId) -> PinFixture { let recreated = NoteId::from(Fr::from(1010u64)); let pk = lb_key_management_system_service::keys::ZkPublicKey::from(Fr::from(7u64)); - let dep = deposit_op(ch, 1, Metadata::new_unchecked(b"d".to_vec())); + let dep = deposit_op(ch, 1, Metadata::try_from(b"d".to_vec()).unwrap()); let dep_tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(dep.clone())]); let pin_op = inscribe_op(ch, MsgId::root(), b"pin"); let pin_id = pin_op.id(); @@ -1634,8 +1610,8 @@ async fn foreign_config_is_adopted_kept_through_un_mine_and_orphaned_by_a_rival( // Z <- R(cfg') tip: cfg' takes the config slot cfg chained on. let ch = ChannelId::from([0u8; 32]); let cfg = unverified_tx_with_ops(vec![Op::ChannelConfig(channel_config(ch, MsgId::root()))]); - let rival_op = - channel_config_with_posting_timeframe(ch, MsgId::root(), SlotTimeframe::from(1u32)); + let mut rival_op = channel_config(ch, MsgId::root()); + rival_op.posting_timeframe = SlotTimeframe::from(1u32); let rival = unverified_tx_with_ops(vec![Op::ChannelConfig(rival_op)]); let (cfg_hash, rival_hash) = (cfg.hash(), rival.hash()); let ba = api_block(1, 0, 1, vec![cfg]); @@ -2629,7 +2605,7 @@ async fn wallet_tracks_deposit_and_transfer_through_finalization() { let recreated = NoteId::from(Fr::from(1010u64)); let out_pk = lb_key_management_system_service::keys::ZkPublicKey::from(Fr::from(7u64)); - let dep = deposit_op(channel_id, 1, Metadata::new_unchecked(b"d".to_vec())); + let dep = deposit_op(channel_id, 1, Metadata::try_from(b"d".to_vec()).unwrap()); let dep_tx = unverified_tx_with_ops(vec![Op::ChannelDeposit(dep.clone())]); let b1 = api_block(1, 0, 1, vec![dep_tx.clone()]); diff --git a/zone-sdk/src/sequencer/block_states.rs b/zone-sdk/src/sequencer/block_states.rs index 2bf5486..7cd18aa 100644 --- a/zone-sdk/src/sequencer/block_states.rs +++ b/zone-sdk/src/sequencer/block_states.rs @@ -600,11 +600,7 @@ impl BlockStates { #[cfg(test)] mod tests { use lb_common_http_client::Slot; - use lb_core::mantle::{ - Op, - ops::channel::{ChannelId, inscribe::Inscription}, - traits::Hashable as _, - }; + use lb_core::mantle::{Op, ops::channel::ChannelId, traits::Hashable as _}; use lb_groth16::Fr; use super::*; @@ -645,7 +641,7 @@ mod tests { tx_hash: TxHash([this; 32]), parent_msg: msg_id(parent), this_msg: msg_id(this), - payload: Inscription::empty(), + payload: [].into(), signer: None, } } diff --git a/zone-sdk/src/sequencer/channel_wallet.rs b/zone-sdk/src/sequencer/channel_wallet.rs index 13a0ce7..e28f8c9 100644 --- a/zone-sdk/src/sequencer/channel_wallet.rs +++ b/zone-sdk/src/sequencer/channel_wallet.rs @@ -266,7 +266,7 @@ mod tests { DepositOp { channel_id, inputs: BoundedInputs::from(note_id(input_seed)).into(), - metadata: Metadata::new_unchecked(b"m".to_vec()), + metadata: Metadata::try_from(b"m".to_vec()).unwrap(), } } @@ -345,7 +345,7 @@ mod tests { inputs: BoundedInputs::try_from_iter([note_id(1), note_id(2)]) .unwrap() .into(), - metadata: Metadata::new_unchecked(b"m".to_vec()), + metadata: Metadata::try_from(b"m".to_vec()).unwrap(), }; let tx = crate::test_support::unverified_tx_with_ops(vec![Op::ChannelDeposit(op.clone())]); let events = deposit_events_for(&tx, &op, 70, vec![dep_note(10, 30), dep_note(11, 40)]); diff --git a/zone-sdk/src/sequencer/slot_clock.rs b/zone-sdk/src/sequencer/slot_clock.rs index 351a449..4935b54 100644 --- a/zone-sdk/src/sequencer/slot_clock.rs +++ b/zone-sdk/src/sequencer/slot_clock.rs @@ -1,8 +1,6 @@ use std::time::{Duration, Instant, SystemTime}; use lb_common_http_client::Slot; -use lb_time::era::EraTimingSchedule; -use time::OffsetDateTime; /// Slack after a slot boundary so a wake-up lands inside the new slot; tokio /// timers tick at millisecond granularity. @@ -10,45 +8,50 @@ const BOUNDARY_GRACE: Duration = Duration::from_millis(10); #[derive(Clone, Debug)] pub(super) struct SlotClock { - eras: EraTimingSchedule, + slot_duration: Duration, + chain_start_time: SystemTime, last_observed_slot: Slot, last_observed_at: Instant, } impl SlotClock { - pub(super) fn from_era_schedule(eras: EraTimingSchedule) -> Self { - let current_slot = slot_at(&eras, OffsetDateTime::now_utc()); + pub(super) fn from_chain_start_time( + chain_start_time: SystemTime, + slot_duration: Duration, + ) -> Self { + let current_slot = slot_from_u64( + SystemTime::now() + .duration_since(chain_start_time) + .ok() + .map_or(0, |elapsed| slots_from_duration(elapsed, slot_duration)), + ); Self { - eras, + slot_duration, + chain_start_time, last_observed_slot: current_slot, last_observed_at: Instant::now(), } } - /// Moves the clock onto `eras`, keeping the slot it last observed and when. - pub(super) fn set_era_schedule(&mut self, eras: EraTimingSchedule) { - self.eras = eras; - } - pub(super) fn observe_slot(&mut self, observed_slot: Slot) { self.last_observed_slot = observed_slot; self.last_observed_at = Instant::now(); } pub(super) fn current_slot(&self) -> Slot { - let from_chain_start = slot_at(&self.eras, OffsetDateTime::now_utc()); - // The observed slot is taken to have started when it was observed. - let from_anchor = self - .eras - .checked_time_of(self.last_observed_slot) - .and_then(|observed| { - observed - .checked_add(time::Duration::try_from(self.last_observed_at.elapsed()).ok()?) - }) - .map_or(self.last_observed_slot, |now| slot_at(&self.eras, now)); - - from_chain_start.max(from_anchor) + let from_chain_start = SystemTime::now() + .duration_since(self.chain_start_time) + .ok() + .map_or(0, |elapsed| { + slots_from_duration(elapsed, self.slot_duration) + }); + let from_anchor = slot_to_u64(self.last_observed_slot).saturating_add(slots_from_duration( + self.last_observed_at.elapsed(), + self.slot_duration, + )); + + slot_from_u64(from_chain_start.max(from_anchor)) } /// Sleep until [`Self::current_slot`] has reached `slot`; `None` if the @@ -66,18 +69,17 @@ impl SlotClock { /// if it already has, `None` if it lies beyond what the clock can hold. fn instant_of(&self, slot: Slot) -> Option { let now = Instant::now(); - let start = self.eras.checked_time_of(slot)?; + let target = slot_to_u64(slot); + let slots_from_anchor = target.saturating_sub(slot_to_u64(self.last_observed_slot)); let from_anchor = self - .eras - .checked_time_of(self.last_observed_slot) - .and_then(|observed| { - // A slot before the observed one is reached at the anchor. - let since_observed = Duration::try_from(start - observed).unwrap_or(Duration::ZERO); - self.last_observed_at.checked_add(since_observed) - }); + .last_observed_at + .checked_add(duration_for_slots(slots_from_anchor, self.slot_duration)); - let from_chain_start = Some(system_time_to_instant(SystemTime::from(start), now)); + let from_chain_start = self + .chain_start_time + .checked_add(duration_for_slots(target, self.slot_duration)) + .map(|at| system_time_to_instant(at, now)); // `current_slot` takes the later of its two slot estimates, so the // slot is reached at the earlier of the two instants. @@ -89,50 +91,45 @@ impl SlotClock { } } -/// The slot in progress at `time`: the genesis slot before genesis. -fn slot_at(eras: &EraTimingSchedule, time: OffsetDateTime) -> Slot { - eras.slot_at(time).unwrap_or(Slot::genesis()) -} - fn system_time_to_instant(at: SystemTime, now: Instant) -> Instant { at.duration_since(SystemTime::now()) .map_or(now, |until| now + until) } +fn duration_for_slots(slots: u64, slot_duration: Duration) -> Duration { + let nanos = u128::from(slots) * slot_duration.as_nanos(); + u64::try_from(nanos).map_or(Duration::MAX, Duration::from_nanos) +} + +const fn slots_from_duration(elapsed: Duration, slot_duration: Duration) -> u64 { + let divisor = slot_duration.as_nanos(); + if divisor == 0 { + return 0; + } + let slots = elapsed.as_nanos() / divisor; + if slots > u64::MAX as u128 { + u64::MAX + } else { + slots as u64 + } +} + pub(super) const fn slot_to_u64(slot: Slot) -> u64 { slot.into_inner() } +fn slot_from_u64(value: u64) -> Slot { + Slot::from(value) +} + #[cfg(test)] mod tests { - use std::num::NonZero; - - use lb_time::era::{EraEntriesAfterGenesis, EraTiming}; - use super::*; - /// Eras starting at genesis `genesis`, slots of `slot_duration` in epochs - /// of 2 slots, then from `next` the slot duration paired with it. - fn eras( - genesis: OffsetDateTime, - slot_duration: Duration, - next: Option<(u32, Duration)>, - ) -> EraTimingSchedule { - let timing = |slot_duration| EraTiming { - slot_duration, - epoch_length: NonZero::new(2).unwrap(), - }; - let after_genesis = next.map_or_else(EraEntriesAfterGenesis::empty, |(epoch, duration)| { - EraEntriesAfterGenesis::from((NonZero::new(epoch).unwrap(), timing(duration))) - }); - EraTimingSchedule::new(genesis, timing(slot_duration), after_genesis).unwrap() - } - #[test] fn instant_of_uses_the_earlier_of_anchor_and_chain_start() { let slot_duration = Duration::from_millis(100); - let mut clock = - SlotClock::from_era_schedule(eras(OffsetDateTime::now_utc(), slot_duration, None)); + let mut clock = SlotClock::from_chain_start_time(SystemTime::now(), slot_duration); clock.observe_slot(Slot::from(10)); let anchor = clock.last_observed_at; @@ -143,28 +140,10 @@ mod tests { assert!(clock.instant_of(Slot::from(5)).unwrap() <= Instant::now()); } - #[test] - fn instant_of_follows_the_slot_duration_of_each_era() { - // Slots of 100 ms until epoch 3, that is slot 6, then slots of 300 ms. - let mut clock = SlotClock::from_era_schedule(eras( - OffsetDateTime::now_utc(), - Duration::from_millis(100), - Some((3, Duration::from_millis(300))), - )); - clock.observe_slot(Slot::from(5)); - let anchor = clock.last_observed_at; - - // One slot of 100 ms, then two of 300 ms. - let at = clock.instant_of(Slot::from(8)).unwrap(); - assert!(at >= anchor + Duration::from_millis(700)); - assert!(at < anchor + Duration::from_millis(800)); - } - #[tokio::test] async fn sleep_until_a_reached_slot_does_not_wait() { let slot_duration = Duration::from_millis(100); - let mut clock = - SlotClock::from_era_schedule(eras(OffsetDateTime::now_utc(), slot_duration, None)); + let mut clock = SlotClock::from_chain_start_time(SystemTime::now(), slot_duration); clock.observe_slot(Slot::from(10)); let anchor = clock.last_observed_at; diff --git a/zone-sdk/src/sequencer/state.rs b/zone-sdk/src/sequencer/state.rs index cf9c7ca..112612f 100644 --- a/zone-sdk/src/sequencer/state.rs +++ b/zone-sdk/src/sequencer/state.rs @@ -117,9 +117,9 @@ fn opaque_lineage( } last_msg = Some(inscribe.id()); } - OpRef::ChannelConfig(config) if config.channel() == channel_id => { + OpRef::ChannelConfig(config) if config.channel == channel_id => { if last_config.is_none() { - config_parent = Some(config.parent()); + config_parent = Some(config.parent); } last_config = Some(config.id()); } @@ -1768,7 +1768,7 @@ mod tests { use lb_key_management_system_service::keys::Ed25519PublicKey; use super::*; - use crate::test_support::{FORK_DIGEST, header_id}; + use crate::test_support::header_id; /// A mined channel tx as the block store holds it. fn stored(tx: &SignedOps, kind: ChannelTxKind) -> BlockChannelTx { @@ -1780,15 +1780,12 @@ mod tests { } fn make_dummy_tx_on(parent: MsgId, data: u8) -> SignedOps { - let mantle_tx = Ops::new( - FORK_DIGEST, - [Op::ChannelInscribe(InscriptionOp { - channel_id: [0u8; 32].into(), - inscription: Inscription::new_unchecked(vec![data]), - parent, - signer: UnverifiedEd25519PublicKey::from_bytes(&[0u8; 32]).unwrap(), - })], - ); + let mantle_tx = Ops::from([Op::ChannelInscribe(InscriptionOp { + channel_id: [0u8; 32].into(), + inscription: [data].into(), + parent, + signer: UnverifiedEd25519PublicKey::from_bytes(&[0u8; 32]).unwrap(), + })]); SignedOps::from_ops_with_sample_proofs(mantle_tx) } @@ -2008,45 +2005,48 @@ mod tests { config_parent: MsgId, data: u8, ) -> (SignedOps, MsgId, MsgId) { - bundle_tx_with(parent, config_parent, data, FORK_DIGEST) + bundle_tx_with(parent, config_parent, data, []) } - /// [`bundle_tx_on`] under `fork_digest`: the same ops, another hash. - fn bundle_tx_with( + /// [`bundle_tx_on`] with `extra` ops appended: the same channel + /// messages, another hash. + fn bundle_tx_with( parent: MsgId, config_parent: MsgId, data: u8, - fork_digest: lb_core::era::ForkDigest, + extra: [Op; N], ) -> (SignedOps, MsgId, MsgId) { - use std::num::NonZero; - use lb_core::mantle::{ channel::{SlotTimeframe, SlotTimeout}, ops::channel::{VerifiedChannelKeys, config::ChannelConfigOp}, }; let inscribe = InscriptionOp { channel_id: [0u8; 32].into(), - inscription: Inscription::new_unchecked(vec![data]), + inscription: [data].into(), parent, signer: UnverifiedEd25519PublicKey::from_bytes(&[0u8; 32]).unwrap(), }; - let config = ChannelConfigOp::new( - [0u8; 32].into(), - config_parent, - VerifiedChannelKeys::try_from(vec![Ed25519PublicKey::from_bytes(&[1u8; 32]).unwrap()]) - .unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap(); + let config = ChannelConfigOp { + channel: [0u8; 32].into(), + parent: config_parent, + keys: VerifiedChannelKeys::try_from(vec![ + Ed25519PublicKey::from_bytes(&[1u8; 32]).unwrap(), + ]) + .unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + }; let inscribe_msg = inscribe.id(); let config_msg = config.id(); - let ops = Ops::new( - fork_digest, - [Op::ChannelInscribe(inscribe), Op::ChannelConfig(config)], - ); + let ops = Ops::try_from( + [Op::ChannelInscribe(inscribe), Op::ChannelConfig(config)] + .into_iter() + .chain(extra) + .collect::>(), + ) + .unwrap(); let tx = SignedOps::from_ops_with_sample_proofs(ops); (tx, inscribe_msg, config_msg) } @@ -2141,11 +2141,18 @@ mod tests { state.submit_other(pending, channel_id).unwrap(); let before = state.channel_lineage(b1); + // An extra inscription on another channel changes the tx hash but + // not this channel's messages. let (mined, _, _) = bundle_tx_with( MsgId::root(), MsgId::root(), 1, - lb_core::era::ForkDigest::new([0x22; 32]), + [Op::ChannelInscribe(InscriptionOp { + channel_id: [1u8; 32].into(), + inscription: [1].into(), + parent: MsgId::root(), + signer: UnverifiedEd25519PublicKey::from_bytes(&[0u8; 32]).unwrap(), + })], ); let mined_hash = mined.hash(); assert_ne!( @@ -2308,22 +2315,12 @@ mod tests { state.process_block(tip, genesis, genesis, vec![], vec![], Vec::new(), vec![]); state - .submit_inscription( - make_dummy_tx(1), - MsgId::root(), - msg_id(10), - Inscription::new_unchecked(vec![1]), - ) + .submit_inscription(make_dummy_tx(1), MsgId::root(), msg_id(10), [1].into()) .unwrap(); let (bundle, inscribe_msg, _config_msg) = bundle_tx(msg_id(10), 2); state.submit_other(bundle, channel_id).unwrap(); state - .submit_inscription( - make_dummy_tx(3), - inscribe_msg, - msg_id(30), - Inscription::new_unchecked(vec![3]), - ) + .submit_inscription(make_dummy_tx(3), inscribe_msg, msg_id(30), [3].into()) .unwrap(); assert_eq!(state.publish_parent(tip), msg_id(30)); @@ -2340,12 +2337,7 @@ mod tests { let first = make_dummy_tx(1); let first_hash = first.hash(); state - .submit_inscription( - first, - MsgId::root(), - msg_id(10), - Inscription::new_unchecked(vec![1]), - ) + .submit_inscription(first, MsgId::root(), msg_id(10), [1].into()) .unwrap(); let (bundle, _i, _c) = bundle_tx(MsgId::root(), 2); assert_eq!( @@ -2363,8 +2355,6 @@ mod tests { /// chaining on the existing pending chain meanwhile. #[test] fn publish_parent_ignores_pending_pure_config_cut() { - use std::num::NonZero; - use lb_core::mantle::{ channel::{SlotTimeframe, SlotTimeout}, ops::channel::{VerifiedChannelKeys, config::ChannelConfigOp}, @@ -2376,26 +2366,22 @@ mod tests { state.process_block(tip, genesis, genesis, vec![], vec![], Vec::new(), vec![]); state - .submit_inscription( - make_dummy_tx(1), - MsgId::root(), - msg_id(10), - Inscription::new_unchecked(vec![1]), - ) + .submit_inscription(make_dummy_tx(1), MsgId::root(), msg_id(10), [1].into()) .unwrap(); - let config = ChannelConfigOp::new( - [0u8; 32].into(), - MsgId::root(), - VerifiedChannelKeys::try_from(vec![Ed25519PublicKey::from_bytes(&[1u8; 32]).unwrap()]) - .unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap(); - let ops = Ops::new(FORK_DIGEST, [Op::ChannelConfig(config)]); + let config = ChannelConfigOp { + channel: [0u8; 32].into(), + parent: MsgId::root(), + keys: VerifiedChannelKeys::try_from(vec![ + Ed25519PublicKey::from_bytes(&[1u8; 32]).unwrap(), + ]) + .unwrap(), + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + }; + let ops = Ops::from([Op::ChannelConfig(config)]); let config_tx = SignedOps::from_ops_with_sample_proofs(ops); state.submit_other(config_tx, channel_id).unwrap(); @@ -2447,7 +2433,7 @@ mod tests { let hash = tx.hash(); let msg = dummy_tx_msg(&tx); state - .submit_inscription(tx, parent, msg, Inscription::new_unchecked(vec![data])) + .submit_inscription(tx, parent, msg, [data].into()) .unwrap(); (hash, msg) } @@ -2465,7 +2451,7 @@ mod tests { Arc::clone(&stored), MsgId::root(), msg, - Inscription::new_unchecked(vec![1]), + [1].into(), PendingBundle::Plain, ); @@ -2887,10 +2873,7 @@ mod tests { ) -> (SignedOps, MsgId) { let config = config_op(config_parent, seed); let id = config.id(); - let tx = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [Op::ChannelConfig(config)], - )); + let tx = SignedOps::from_ops_with_sample_proofs(Ops::from([Op::ChannelConfig(config)])); (tx, id) } @@ -2899,26 +2882,23 @@ mod tests { config_parent: MsgId, seed: u8, ) -> lb_core::mantle::ops::channel::config::ChannelConfigOp { - use std::num::NonZero; - use lb_core::mantle::{ channel::{SlotTimeframe, SlotTimeout}, ops::channel::{VerifiedChannelKeys, config::ChannelConfigOp}, }; - ChannelConfigOp::new( - [0u8; 32].into(), - config_parent, - VerifiedChannelKeys::try_from(vec![ + ChannelConfigOp { + channel: [0u8; 32].into(), + parent: config_parent, + keys: VerifiedChannelKeys::try_from(vec![ lb_key_management_system_service::keys::Ed25519Key::from_bytes(&[seed; 32]) .public_key(), ]) .unwrap(), - SlotTimeframe::from(0u32), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap() + posting_timeframe: SlotTimeframe::from(0u32), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + } } /// A whole expired chain is reported parent first whatever the hash @@ -3032,23 +3012,19 @@ mod tests { let mut state = TxState::new(genesis, MsgId::root()); state.process_block(tip, genesis, genesis, vec![], vec![], Vec::new(), vec![]); let again_op = config_op(MsgId::root(), 4); - let again = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [Op::ChannelConfig(again_op.clone())], - )); + let again = SignedOps::from_ops_with_sample_proofs(Ops::from([Op::ChannelConfig( + again_op.clone(), + )])); let again_hash = again.hash(); state.submit_other(again, channel_id).unwrap(); - let same_config_other_tx = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [ - Op::ChannelInscribe(crate::test_support::inscribe_op( - channel_id, - MsgId::root(), - b"beside the config", - )), - Op::ChannelConfig(again_op), - ], - )); + let same_config_other_tx = SignedOps::from_ops_with_sample_proofs(Ops::from([ + Op::ChannelInscribe(crate::test_support::inscribe_op( + channel_id, + MsgId::root(), + b"beside the config", + )), + Op::ChannelConfig(again_op), + ])); assert_ne!(same_config_other_tx.hash(), again_hash); state.observe_other_tx(same_config_other_tx, channel_id); @@ -3075,7 +3051,7 @@ mod tests { original, MsgId::root(), msg, - Inscription::new_unchecked(vec![1]), + [1].into(), PendingBundle::Plain, ); state.process_block( @@ -3310,10 +3286,10 @@ mod tests { inputs: lb_core::mantle::ledger::BoundedInputs::from(input).into(), outputs: Outputs::new([Note::new(5, pk)]), }; - let tx = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [Op::ChannelTransfer(transfer), Op::ChannelInscribe(inscribe)], - )); + let tx = SignedOps::from_ops_with_sample_proofs(Ops::from([ + Op::ChannelTransfer(transfer), + Op::ChannelInscribe(inscribe), + ])); (tx, msg) }; let (live, live_msg) = bundle(MsgId::root(), present, 1); @@ -3397,12 +3373,7 @@ mod tests { let tx = make_dummy_tx(data); let hash = tx.hash(); state - .submit_inscription( - tx, - parent_msg, - this_msg, - Inscription::new_unchecked(vec![data]), - ) + .submit_inscription(tx, parent_msg, this_msg, [data].into()) .unwrap(); hash } @@ -3410,28 +3381,24 @@ mod tests { /// Build a pure `[config]` tx for the zero channel; `data` varies the /// payload so ids differ. fn config_tx(parent: MsgId, data: u32) -> (SignedOps, MsgId) { - use std::num::NonZero; - use lb_core::mantle::{ channel::{SlotTimeframe, SlotTimeout}, ops::channel::{VerifiedChannelKeys, config::ChannelConfigOp}, }; - let config = ChannelConfigOp::new( - [0u8; 32].into(), + let config = ChannelConfigOp { + channel: [0u8; 32].into(), parent, - VerifiedChannelKeys::try_from(vec![Ed25519PublicKey::from_bytes(&[1u8; 32]).unwrap()]) - .unwrap(), - SlotTimeframe::from(data), - SlotTimeout::from(0u32), - NonZero::::MIN, - NonZero::::MIN, - ) - .unwrap(); + keys: VerifiedChannelKeys::try_from(vec![ + Ed25519PublicKey::from_bytes(&[1u8; 32]).unwrap(), + ]) + .unwrap(), + posting_timeframe: SlotTimeframe::from(data), + posting_timeout: SlotTimeout::from(0u32), + configuration_threshold: 1, + transfer_threshold: 1, + }; let config_msg = config.id(); - let tx = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [Op::ChannelConfig(config)], - )); + let tx = SignedOps::from_ops_with_sample_proofs(Ops::from([Op::ChannelConfig(config)])); (tx, config_msg) } @@ -3450,7 +3417,7 @@ mod tests { tx_hash: tx.hash(), parent_msg: parent, this_msg, - payload: Inscription::empty(), + payload: [].into(), signer: None, }), ) @@ -3572,7 +3539,7 @@ mod tests { tx_hash: tx.hash(), parent_msg: parent, this_msg, - payload: Inscription::empty(), + payload: [].into(), signer: None, }; let custom = stored( @@ -3782,7 +3749,7 @@ mod tests { tx_hash: p1, parent_msg: MsgId::root(), this_msg: msg_id(1), - payload: Inscription::new_unchecked(vec![1]), + payload: [1].into(), signer: Some(test_signer()), }; state.process_block( @@ -3857,10 +3824,10 @@ mod tests { parent: first.id(), signer, }; - let custom = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [Op::ChannelInscribe(first), Op::ChannelInscribe(second)], - )); + let custom = SignedOps::from_ops_with_sample_proofs(Ops::from([ + Op::ChannelInscribe(first), + Op::ChannelInscribe(second), + ])); let custom_hash = custom.hash(); state.submit_other(custom, channel_id).unwrap(); assert_eq!( @@ -3926,10 +3893,10 @@ mod tests { signer, }; let last_msg = second.id(); - let custom = SignedOps::from_ops_with_sample_proofs(Ops::new( - FORK_DIGEST, - [Op::ChannelInscribe(first), Op::ChannelInscribe(second)], - )); + let custom = SignedOps::from_ops_with_sample_proofs(Ops::from([ + Op::ChannelInscribe(first), + Op::ChannelInscribe(second), + ])); let custom_hash = custom.hash(); state.submit_other(custom, channel_id).unwrap(); let p1 = submit_fake_inscription(&mut state, 1, last_msg, msg_id(1)); @@ -3983,7 +3950,7 @@ mod tests { tx_hash: make_dummy_tx(1).hash(), parent_msg: MsgId::root(), this_msg: msg_id(1), - payload: Inscription::new_unchecked(vec![1]), + payload: [1].into(), signer: Some(test_signer()), }; state.process_block( @@ -4050,7 +4017,7 @@ mod tests { tx_hash: c1_tx_hash, parent_msg: MsgId::root(), this_msg: c1_msg, - payload: Inscription::new_unchecked(vec![99]), + payload: [99].into(), signer: Some(test_signer()), }; // Mirror the observed inscription into pending before the safe-set @@ -4060,7 +4027,7 @@ mod tests { c1_tx, MsgId::root(), c1_msg, - Inscription::new_unchecked(vec![99]), + [99].into(), PendingBundle::Plain, ); state.process_block( @@ -4124,12 +4091,8 @@ mod tests { let b1_msg = msg_id(10); let b1_hash = submit_fake_inscription(&mut state, 1, MsgId::root(), b1_msg); - let refused = state.submit_inscription( - make_dummy_tx(4), - MsgId::root(), - msg_id(30), - Inscription::new_unchecked(vec![4]), - ); + let refused = + state.submit_inscription(make_dummy_tx(4), MsgId::root(), msg_id(30), [4].into()); assert_eq!( refused, Err(ParentTaken { @@ -4179,7 +4142,7 @@ mod tests { tx_hash: make_dummy_tx(99).hash(), parent_msg: MsgId::root(), this_msg: c1_msg, - payload: Inscription::new_unchecked(vec![99]), + payload: [99].into(), signer: Some(test_signer()), }; state.process_block( diff --git a/zone-sdk/src/sequencer/tx_builder.rs b/zone-sdk/src/sequencer/tx_builder.rs index 7f9c54b..f5da55e 100644 --- a/zone-sdk/src/sequencer/tx_builder.rs +++ b/zone-sdk/src/sequencer/tx_builder.rs @@ -1,5 +1,3 @@ -use std::num::NonZero; - use lb_core::{ mantle::{ SignedOps, TxHash, @@ -374,25 +372,15 @@ pub(super) async fn build_and_fund_config( where Node: adapter::Node + Sync, { - // Reject a config the ledger would reject before funding it. - let key_count = keys.len(); - let non_zero = |threshold: u16, name: &str| { - NonZero::new(threshold).ok_or_else(|| Error::Network(format!("{name} must be at least 1"))) - }; - let config_op = ChannelConfigOp::new( - channel_id, + let config_op = ChannelConfigOp { + channel: channel_id, parent, keys, posting_timeframe, posting_timeout, - non_zero(configuration_threshold, "configuration_threshold")?, - non_zero(transfer_threshold, "transfer_threshold")?, - ) - .map_err(|_| { - Error::Network(format!( - "configuration_threshold {configuration_threshold} exceeds the {key_count} accredited keys" - )) - })?; + configuration_threshold, + transfer_threshold, + }; let (config_tx, transfer_proof, _) = fund_ops(node, funding, vec![Op::ChannelConfig(config_op)]).await?; @@ -504,13 +492,12 @@ mod tests { use lb_core::mantle::{ ledger::{BoundedInputs, NoteId}, ops::{OpProofRef, channel::withdraw::ChannelWithdrawOp}, - transactions::TxList, }; use lb_groth16::Fr; use tokio::sync::mpsc; use super::*; - use crate::test_support::{FORK_DIGEST, MockNode, funding_config, inscribe_op}; + use crate::test_support::{MockNode, funding_config, inscribe_op}; #[tokio::test] async fn funding_path_passes_priority_fee_as_a_percentage() { @@ -713,9 +700,7 @@ mod tests { inputs: BoundedInputs::from(NoteId::from(Fr::from(1u64))).into(), }), ]; - TxList::try_from(ops) - .map(|ops| Ops::new(FORK_DIGEST, ops)) - .expect("ops fit") + Ops::try_from(ops).expect("ops fit") } #[test] diff --git a/zone-sdk/src/sequencer/types.rs b/zone-sdk/src/sequencer/types.rs index 8bbf5fa..bbb201c 100644 --- a/zone-sdk/src/sequencer/types.rs +++ b/zone-sdk/src/sequencer/types.rs @@ -909,8 +909,6 @@ pub enum FinalizedOp { #[cfg(test)] mod tests { - use std::num::NonZero; - use lb_core::mantle::{ Op, channel::{SlotTimeframe, SlotTimeout}, @@ -920,13 +918,12 @@ mod tests { withdraw::ChannelWithdrawOp, }, traits::Hashable as _, - transactions::{Ops, TxList}, + transactions::Ops, }; use lb_groth16::Fr; use lb_key_management_system_service::keys::{Ed25519Key, Ed25519PublicKey}; use super::{Error, PreparedChannelConfig, sign_prepared}; - use crate::test_support::FORK_DIGEST; /// The signing payload the SDK derives for `ops`. fn payload_of(ops: &Ops) -> Vec { @@ -934,16 +931,15 @@ mod tests { } fn config_op(keys: Vec) -> ChannelConfigOp { - ChannelConfigOp::new( - ChannelId::from([7; 32]), - MsgId::root(), - VerifiedChannelKeys::new_unchecked(keys), - SlotTimeframe::from(15), - SlotTimeout::from(3), - NonZero::::MIN, - NonZero::new(2).unwrap(), - ) - .unwrap() + ChannelConfigOp { + channel: ChannelId::from([7; 32]), + parent: MsgId::root(), + keys: VerifiedChannelKeys::new_unchecked(keys), + posting_timeframe: SlotTimeframe::from(15), + posting_timeout: SlotTimeout::from(3), + configuration_threshold: 2, + transfer_threshold: 2, + } } #[test] @@ -954,10 +950,7 @@ mod tests { .collect(); let op = config_op(new_keys); let prepared = PreparedChannelConfig { - tx: Ops::new( - FORK_DIGEST, - TxList::new_unchecked(vec![Op::ChannelConfig(op.clone())]), - ), + tx: Ops::new_unchecked(vec![Op::ChannelConfig(op.clone())]), transfer_proof: None, sign_payload: vec![0xab; 32], accredited_keys: Vec::new(), @@ -979,10 +972,7 @@ mod tests { .iter() .map(|k| k.into_unverified()) .collect::>(); - let tx = Ops::new( - FORK_DIGEST, - TxList::new_unchecked(vec![Op::ChannelConfig(config_op(accredited))]), - ); + let tx = Ops::new_unchecked(vec![Op::ChannelConfig(config_op(accredited))]); let prepared = PreparedChannelConfig { sign_payload: payload_of(&tx), tx, @@ -1014,10 +1004,7 @@ mod tests { channel_id: ChannelId::from([7; 32]), inputs: BoundedInputs::from(NoteId::from(Fr::from(1u64))).into(), }); - let tx = Ops::new( - FORK_DIGEST, - TxList::new_unchecked(vec![config.clone(), smuggled.clone()]), - ); + let tx = Ops::new_unchecked(vec![config.clone(), smuggled.clone()]); let prepared = PreparedChannelConfig { sign_payload: payload_of(&tx), tx, @@ -1040,10 +1027,7 @@ mod tests { let accredited = vec![signer.public_key()]; let accredited_as_unverified_keys = accredited.iter().map(|k| k.into_unverified()).collect(); - let tx = Ops::new( - FORK_DIGEST, - TxList::new_unchecked(vec![Op::ChannelConfig(config_op(accredited))]), - ); + let tx = Ops::new_unchecked(vec![Op::ChannelConfig(config_op(accredited))]); // Honest-looking ops, but the payload belongs to some other tx. let prepared = PreparedChannelConfig { tx, diff --git a/zone-sdk/src/sequencer/zone_sequencer.rs b/zone-sdk/src/sequencer/zone_sequencer.rs index 4b2a299..fac750f 100644 --- a/zone-sdk/src/sequencer/zone_sequencer.rs +++ b/zone-sdk/src/sequencer/zone_sequencer.rs @@ -376,20 +376,6 @@ where SequencerHandle::new(self) } - /// The most bytes an inscription may take in the era of the node's - /// current slot: what a published inscription must fit. - pub fn max_inscription_size( - &self, - ) -> impl Future> + Send + use { - let node = self.node.clone(); - async move { - node.limits() - .await - .map(|limits| limits.max_op_data_size) - .map_err(|err| Error::Network(format!("failed to fetch the node's limits: {err}"))) - } - } - /// Obtain a cheap-to-clone client for driving the sequencer from tasks /// outside the drive loop. /// diff --git a/zone-sdk/src/test_support.rs b/zone-sdk/src/test_support.rs index 418512b..3e014d9 100644 --- a/zone-sdk/src/test_support.rs +++ b/zone-sdk/src/test_support.rs @@ -1,8 +1,7 @@ //! Configurable [`adapter::Node`] mock and shared builders for unit tests. use std::{ - collections::{BTreeMap, HashMap, VecDeque}, - num::NonZero, + collections::{HashMap, VecDeque}, sync::{ Arc, Mutex, atomic::{AtomicUsize, Ordering}, @@ -12,11 +11,10 @@ use std::{ use async_trait::async_trait; use futures::StreamExt as _; use lb_common_http_client::{ - ApiBlock, ApiHeader, BlockInfo, ChainServiceInfo, CryptarchiaInfo, EraTiming, Events, PhaseTag, + ApiBlock, ApiHeader, BlockInfo, ChainServiceInfo, CryptarchiaInfo, Events, PhaseTag, ProcessedBlockEvent, Slot, State, TimeInfo, }; use lb_core::{ - era::ForkDigest, events::{DepositNote, Event as ChainEvent, TxEvent, TxEventPayload}, header::{ContentId, HeaderId}, mantle::{ @@ -33,15 +31,12 @@ use lb_core::{ }, }, traits::Hashable as _, - transactions::{Ops, TxList, states::Unverified}, + transactions::{Ops, states::Unverified}, }, proofs::leader_proof::Groth16LeaderProof, }; use lb_groth16::Fr; -use lb_http_api_common::bodies::{ - mantle::LimitsResponseBody, - wallet::fund::{WalletFundRequestBody, WalletFundResponseBody}, -}; +use lb_http_api_common::bodies::wallet::fund::{WalletFundRequestBody, WalletFundResponseBody}; use lb_key_management_system_service::keys::Ed25519Key; use tokio::sync::{mpsc, watch}; @@ -51,9 +46,6 @@ use crate::{ sequencer::FundingConfig, }; -/// The fork digest the transactions of the tests carry. -pub const FORK_DIGEST: ForkDigest = ForkDigest::new([0x11; 32]); - /// One scripted `block_stream` connection: serve `events`, then `then`. #[derive(Clone)] pub struct StreamScript { @@ -116,11 +108,10 @@ pub struct MockNode { pub events: HashMap, /// Receives the priority-fee percentages from funding requests. pub funding_priority_fees: Option>, - /// Served by `time_info()`: a single era from `genesis_time`, with slots - /// of `slot_duration` in epochs of `slots_per_epoch` slots. - pub slot_duration: core::time::Duration, - pub slots_per_epoch: NonZero, - pub genesis_time: time::UtcDateTime, + /// Served by `time_info()`. + pub slot_duration_ms: u64, + pub slots_per_epoch: u64, + pub genesis_time_unix_ms: i64, } impl Default for MockNode { @@ -146,9 +137,9 @@ impl Default for MockNode { posted: None, events: HashMap::new(), funding_priority_fees: None, - slot_duration: core::time::Duration::from_secs(1), - slots_per_epoch: NonZero::new(1_000).unwrap(), - genesis_time: time::UtcDateTime::UNIX_EPOCH, + slot_duration_ms: 1_000, + slots_per_epoch: 1_000, + genesis_time_unix_ms: 0, } } } @@ -195,24 +186,13 @@ impl adapter::Node for MockNode { }) } - async fn limits(&self) -> Result { - Ok(LimitsResponseBody { - max_op_data_size: u32::MAX, - }) - } - async fn time_info(&self) -> Result { Ok(TimeInfo { - genesis_time: self.genesis_time, + slot_duration_ms: self.slot_duration_ms, + genesis_time_unix_ms: self.genesis_time_unix_ms, current_slot: 0, current_epoch: 0, - era_timings: BTreeMap::from([( - 0, - EraTiming { - slot_duration: self.slot_duration, - slots_per_epoch: self.slots_per_epoch, - }, - )]), + slots_per_epoch: self.slots_per_epoch, }) } @@ -370,7 +350,7 @@ impl adapter::Node for MockNode { // node would at zero gas price. Ok(WalletFundResponseBody { tip: header_id(0), - funded_tx: request.tx_builder.build(FORK_DIGEST).map_err(|e| { + funded_tx: request.tx_builder.build().map_err(|e| { lb_common_http_client::Error::Server(format!("mock funding failed: {e:?}")) })?, transfer_proof: None, @@ -452,9 +432,7 @@ pub fn live_event(block: &ApiBlock) -> ProcessedBlockEvent { /// Build a `SignedOps` carrying the given ops, with sample proofs. /// Suitable for tests that only care about op extraction, not verification. pub fn unverified_tx_with_ops(ops: Vec) -> SignedOps { - let ops = TxList::try_from(ops) - .map(|ops| Ops::new(FORK_DIGEST, ops)) - .expect("ops fit"); + let ops = Ops::try_from(ops).expect("ops fit"); SignedOps::from_ops_with_sample_proofs(ops) }