SPB Git

Repository Public

MultichainWalletSyncHarnessTests.swift 7.6 KB 135 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : security/tests/Tests/FuzzReplayTests/MultichainWalletSyncHarnessTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0512// Last Modified    : 2026-12-0513//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import AuditSupport42import Foundation43import QyvoraCore44import QyvoraEthereum45import QyvoraMath46import QyvoraSolana47import QyvoraTron48import Testing4950@testable import FuzzTargets51@testable import QyvoraWallet5253// The multi-chain modes of the `wallet-sync` fuzz target are only worth running if, with every answer left at its54// default, the wallet really synchronises each family and reaches the payment builder: otherwise the mutations55// would only ever exercise the first refusal. This test runs the harness's own fixture with healthy answers and56// then with one hostile answer per family, and states what each does.5758@Suite(.serialized) struct MultichainWalletSyncHarnessTests {59    @Test func healthyAnswersSynchroniseEveryFamilyAndReachThePaymentBuilder() async throws {60        let fixture = try #require(WalletFixture.make("harness-test"), "the wallet fixture of the fuzz target could not be created")61        let service = fixture.service62        var cases = 0, failed = 063        func check(_ ok: Bool, _ what: String) {64            cases += 165            if !ok {66                failed += 167                Issue.record("wallet-sync harness: \(what)")68            }69        }70        fixture.router.install([])71        let active = await service.activeChains().map(\.id)72        check(Set([ChainID.solanaDevnet, .tronNile, .litecoinTestnet, .baseSepolia]).isSubset(of: Set(active)), "the four multi-chain networks are active: \(active)")7374        // healthy answers: every family synchronises75        for chain in [ChainID.solanaDevnet, .tronNile, .litecoinTestnet, .baseSepolia] {76            do {77                try await service.synchronize(chain: chain)78                let data = await service.chainSnapshot(chain)?.data.value79                check(data != nil && data?.isFromPreviousSession == false, "\(chain): no data after a synchronisation")80            } catch {81                check(false, "\(chain): a healthy synchronisation failed: \(error)")82            }83        }84        let solana = await service.chainSnapshot(.solanaDevnet)?.data.value85        check(solana?.native.confirmed == UInt256(2_000_000_000), "Solana balance read: \(String(describing: solana?.native.confirmed))")86        let tron = await service.chainSnapshot(.tronNile)?.data.value87        check(tron?.native.confirmed == UInt256(50_000_000), "TRON balance read: \(String(describing: tron?.native.confirmed))")88        let base = await service.chainSnapshot(.baseSepolia)?.data.value89        check(base?.native.confirmed == UInt256(1_000_000_000_000_000_000), "Base Sepolia balance read: \(String(describing: base?.native.confirmed))")9091        // and a payment of the native coin is built on the three account families (the Litecoin wallet is empty)92        let solanaRecipient = try SolanaKeys.publicKey(privateSeed: Bytes(repeating: 9, count: 32)).base5893        let tronRecipient = try #require(TronAddress(hash: Bytes(repeating: 0x33, count: 20))).base5894        for (chain, recipient) in [(ChainID.solanaDevnet, solanaRecipient), (.tronNile, tronRecipient), (.baseSepolia, sampleAddress)] {95            do {96                let review = try await service.prepareSend(chain: chain, asset: .native, to: recipient, amount: UInt256(1_000))97                check(review.chain.id == chain && review.amount == UInt256(1_000) && !review.isMainnet, "\(chain): the review is not the payment asked")98                if case .evm(let inner) = review.body {99                    // the default oracle answer is 8 gwei: 10 gwei with the margin, inside the maximum fee100                    check(inner.l1DataFee == UInt256(10_000_000_000) && inner.transaction.chainID == 84_532, "Base Sepolia: L1 data fee \(inner.l1DataFee)")101                    check(EVMFeeRules.maximumFee(gasLimit: inner.transaction.gasLimit, maxFeePerGas: inner.transaction.maxFeePerGas, l1DataFee: inner.l1DataFee) == inner.maximumFee, "Base Sepolia: maximum fee")102                }103            } catch {104                check(false, "\(chain): a payment could not be prepared with healthy answers: \(error)")105            }106        }107108        // one hostile answer per family: refused, or kept inside the bounds — never a trap (the harness's own invariants)109        let hostile: [(Int, [Int: String])] = [110            (4, [16: "getBalance={\"context\":{\"slot\":5000},\"value\":18446744073709551615}"]), (4, [16: "getGenesisHash=\"\(SolanaNetwork.mainnetBeta.genesisHash)\""]),111            (5, [17: "/wallet/getaccount={\"address\":\"41\",\"balance\":9223372036854775808}"]), (5, [17: "/wallet/getchainparameters={\"chainParameter\":[{\"key\":\"getEnergyFee\",\"value\":18446744073709551615}]}"]),112            (6, [18: "/blocks/tip/height=18446744073709551615"]), (6, [18: "/fee-estimates={\"1\":1e308,\"2\":-1}"]),113            (7, [19: "\"0x\""]), (7, [19: "\"0x" + String(repeating: "f", count: 64) + "\""]), (7, [20: "{\"number\":\"0x10\",\"baseFeePerGas\":\"0x9184e72a000\"}"]),114        ]115        for (mode, sections) in hostile {116            let last = sections.keys.max() ?? 0117            fixture.router.install((0...last).map { sections[$0] ?? "" })118            await multiChainSync(service, mode: mode)119            cases += 1120        }121        // the oracle that answers nothing usable: no payment is prepared (never a fee of zero)122        fixture.router.install((0...19).map { $0 == 19 ? "\"0x\"" : "" })123        do {124            _ = try await service.prepareSend(chain: .baseSepolia, asset: .native, to: sampleAddress, amount: UInt256(1_000))125            check(false, "a payment was prepared on Base Sepolia without an L1 data fee")126        } catch {127            check(true, "")128        }129        fixture.router.install([])130        AuditRecorder.record(area: "fuzz", name: "wallet-sync-multichain-harness", algorithm: "wallet-sync fuzz harness, multi-chain modes",131                             parameterSet: "healthy answers synchronise Solana devnet, TRON Nile, Litecoin testnet and Base Sepolia and reach the payment builder; one hostile answer per family", cases: cases, failed: failed)132        #expect(failed == 0 && cases > 0)133    }134}135
Files main
Menu

Theme