Skip to content

Commit

Permalink
Update GitHub pages documentation site.
Browse files Browse the repository at this point in the history
commit 840f39f
Merge: c258c31 524e423
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Thu Jul 25 21:20:09 2024 -0400

    Merge pull request #3 from loopedresolve/main

    Refactor chain specification implementation and update tests

commit 524e423
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Thu Jul 25 14:17:28 2024 -0400

    Update README.md

commit 0fac2b7
Author: loopedresolve <steve@loopedresolve.io>
Date:   Thu Jul 25 14:14:36 2024 -0400

    Update README.md

commit 7f562a8
Author: loopedresolve <steve@loopedresolve.io>
Date:   Thu Jul 25 14:09:14 2024 -0400

    Update README.md

commit 142685c
Author: loopedresolve <steve@loopedresolve.io>
Date:   Tue Jul 23 23:32:35 2024 -0400

    Update json-rpc2 package dependency

commit e08b8a5
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sun Jul 21 22:28:00 2024 -0400

    Update error handling

commit 1419940
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sun Jul 21 01:31:30 2024 -0400

    Update Chain+Specification.swift

commit 142f4b4
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sun Jul 21 00:21:51 2024 -0400

    Remove bundled resources from package

commit 54ef58b
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sun Jul 21 00:03:23 2024 -0400

    Update Chain.swift

commit 52a9837
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sat Jul 20 23:16:58 2024 -0400

    Update SmoldotSwiftTests.swift

commit 24061fd
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sat Jul 20 22:01:06 2024 -0400

    Add chain specification files to test project

commit 3d0ba96
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sat Jul 20 22:00:28 2024 -0400

    Move chain specification back into Chain.swift

commit 773bbbd
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sat Jul 20 14:56:52 2024 -0400

    Add todo item

commit be25a76
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sat Jul 20 14:56:28 2024 -0400

    Expose Chain initialization from a specification file.

commit cc948d9
Author: loopedresolve <steve@loopedresolve.io>
Date:   Sat Jul 20 14:46:47 2024 -0400

    Move Chain Specification definition to its own file.

commit c258c31
Author: finsig <steve@finsig.io>
Date:   Sun Jul 7 12:46:29 2024 -0400

    Set specific nightly release for Rust toolchain

commit ce4a154
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Sun Jun 30 20:33:21 2024 -0400

    Update README.md

commit 1c65e91
Author: finsig <steve@finsig.io>
Date:   Sun Jun 30 14:01:55 2024 -0400

    Update Client.swift documentation

commit 371c3ac
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Sat Jun 29 21:00:40 2024 -0400

    Update README.md

commit 9b9a35c
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Sat Jun 29 20:39:38 2024 -0400

    Update README.md

commit 7fe337b
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 11:48:26 2024 -0400

    Clean up

commit 6b222a4
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 11:47:20 2024 -0400

    Remove comments

commit 0f8e36b
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 11:43:51 2024 -0400

    Update scripts

commit 46cf8c6
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 11:21:26 2024 -0400

    Update scripts

commit 8aacc81
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 11:21:15 2024 -0400

    Add dev script

commit 163eea2
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 10:58:28 2024 -0400

    Fix typo

commit a6e66a7
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 10:37:54 2024 -0400

    Remove whitespace

commit 01d1da8
Author: finsig <steve@finsig.io>
Date:   Sat Jun 29 10:36:35 2024 -0400

    Add shell script functions to modify Package.swift

commit 47bd2e0
Author: finsig <steve@finsig.io>
Date:   Fri Jun 28 22:15:10 2024 -0400

    Use checkouts directory

commit eadcf2a
Author: finsig <steve@finsig.io>
Date:   Fri Jun 28 20:20:13 2024 -0400

    Update functions script

    Copy xcframework to package

commit 520b258
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Fri Jun 28 08:58:09 2024 -0400

    Update swift.yml

commit ee68c98
Merge: e21255f 56ce5bd
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Fri Jun 28 00:45:15 2024 -0400

    Merge pull request #1 from finsig/loopedresolve-patch-1

    Create swift.yml

commit 56ce5bd
Author: loopedresolve <99568419+loopedresolve@users.noreply.github.com>
Date:   Fri Jun 28 00:42:27 2024 -0400

    Create swift.yml
  • Loading branch information
loopedresolve committed Jul 26, 2024
1 parent 1691ca6 commit fcbcfaf
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .scripts/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ env::setup() {
log::info "ROOT_DIRECTORY=$ROOT_DIRECTORY"
export BUILD_DIRECTORY=${BUILD_DIRECTORY:-"$ROOT_DIRECTORY/.build/smoldot-framework"}
log::info "BUILD_DIRECTORY=$BUILD_DIRECTORY"
export RUST_TOOLCHAIN=${RUST_TOOLCHAIN:-'nightly'}
export RUST_TOOLCHAIN=${RUST_TOOLCHAIN:-'nightly-2024-06-30'}
log::info "RUST_TOOLCHAIN=$RUST_TOOLCHAIN"

export CHECKOUTS_DIRECTORY=${CHECKOUTS_DIRECTORY:-"$ROOT_DIRECTORY/.build/checkouts"}
Expand Down
20 changes: 11 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
targets: ["SmoldotSwift"]),
],
dependencies: [
.package(url: "https://github.com/finsig/json-rpc2", from: "0.1.0"),
.package(url: "https://github.com/finsig/json-rpc2", from: "0.1.1")
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
],
targets: [
Expand All @@ -23,13 +23,8 @@ let package = Package(
"CSmoldot",
.product(name: "JSONRPC2", package: "json-rpc2"),
],
path: "Sources/SmoldotSwift",
resources: [
.process("Resources/polkadot.json"),
.process("Resources/kusama.json"),
.process("Resources/rococo.json"),
.process("Resources/westend.json")]
),
path: "Sources/SmoldotSwift"
),
.target(
name: "CSmoldot",
dependencies: ["smoldot"],
Expand All @@ -47,6 +42,13 @@ let package = Package(

.testTarget(
name: "SmoldotSwiftTests",
dependencies: ["SmoldotSwift"]),
dependencies: ["SmoldotSwift"],
resources: [
.process("Resources/polkadot.json"),
.process("Resources/kusama.json"),
.process("Resources/rococo.json"),
.process("Resources/westend.json")]
),

]
)
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ Add the package declaration to your project's manifest dependencies array:

## Usage

Initialize a Chain from a specification. A Chain Specification is a JSON Object that describes a Polkadot-based blockchain network. Chain Specifications for Polkadot, Kusama, Rococo, and Westend are provided.
A Chain Specification file must be provided to initialize a chain. A Chain Specification is a JSON Object that describes a Polkadot-based blockchain network.

*Example Chain Specification JSON files for Polkadot, Kusama, Rococo, and Westend can be copied for use from [/Tests/SmoldotSwiftTests/Resources](https://github.com/loopedresolve/smoldot-swift/tree/main/Tests/SmoldotSwiftTests/Resources).*


Initialize a chain from a specification file:

```swift
var chain = Chain(specification: .polkadot)
var chain = Chain(specificationFile: {Resource file URL})
```

Add the chain to the client to connect to the network:
Expand Down Expand Up @@ -63,7 +67,7 @@ try Client.shared.remove(chain: &chain)
````


For additional information about usage see [documentation](https://finsig.github.io/smoldot-swift/documentation/smoldotswift/).
For additional information about usage see [reference documentation](https://finsig.github.io/smoldot-swift/documentation/smoldotswift/).

## Logging

Expand All @@ -74,7 +78,7 @@ You may enable logging of the smoldot Rust FFI library with an environment varia
There is a build_xcframework.sh script in the repo which can be used to build the XCode Framework target from the smoldot Rust FFI library.

```zsh
> zsh build_xcframework.sh dev
$ zsh build_xcframework.sh dev
```

Omitting the `dev` argument will modify the package settings to use a remote binary target and create a compressed framework file (along with the checksum value).
Expand All @@ -83,3 +87,7 @@ Omitting the `dev` argument will modify the package settings to use a remote bin
## Testing

In addition to unit tests please see this [project](https://github.com/finsig/smoldot-swift-performance) for memory usage profiling.

## Bug Reports

See [GitHub issues](https://github.com/finsig/smoldot-swift/issues) regarding log noise.
48 changes: 0 additions & 48 deletions Sources/SmoldotSwift/Chain+Resources.swift

This file was deleted.

6 changes: 5 additions & 1 deletion Sources/SmoldotSwift/Chain+Specification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import Foundation

extension Chain.Specification {


/// Chain name as defined in the specification.
public var name: String {
precondition(self["name"] != nil, "Chain Specification is missing required key `name`")
return self["name"] as! String
}

/// Chain identifier as defined in the specification.
var id: String {
precondition(self["id"] != nil, "Chain Specification is missing required key `id`")
return self["id"] as! String
}
}
17 changes: 16 additions & 1 deletion Sources/SmoldotSwift/Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,31 @@ public final class Chain: Hashable {
/// connects to, the other nodes that it initially communicates with, and the initial state that nodes
/// must agree on to produce blocks.
///
/// A typelias is used rather than defining an explicit type so that Foundation `JSONSerialization`
/// can be used to convert the JSON into a `Dictionary` type representation of the object with key
/// values of type `Any`.
///
/// - Important:
/// Niether the validity of the Chain Specification JSON nor its conformance to the ChainSpec trait
/// is handled by Swift and will produce fatal error information in the Rust environment logger.
///
public typealias Specification = JSONObject

/// Creates a Chain from the provided Chain Specification.
/// Creates a Chain from the a Chain Specification JSON object.
///
/// See ``Specification`` for more information.
///
public init(specification: Specification) {
self.specification = specification
}

/// Creates a Chain from a Chain Specification JSON file.
public convenience init(specificationFile url: URL) throws {
let data = try Data(contentsOf: url)
let specification = try JSONSerialization.jsonObject(with: data) as! Specification
self.init(specification: specification)
}

public func hash(into hasher: inout Hasher) {
hasher.combine(specification.id)
}
Expand Down
16 changes: 7 additions & 9 deletions Sources/SmoldotSwift/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ public final class Client {
///
public func add(chain: inout Chain) throws {
guard !chain.isValid else {
throw ClientError(message: "Chain has already been added.")
}
guard let data = try? JSONSerialization.data(withJSONObject: chain.specification) else {
throw ClientError(message: "Invalid JSON object.") /// note: checks for validity only, does not check Chain Specification JSON object correctness. intentional.
throw ClientError.chainHasAlreadyBeenAdded
}
let data = try JSONSerialization.data(withJSONObject: chain.specification)
let string = String(data: data, encoding: .utf8)
chain.id = Chain.Id( smoldot_add_chain(string) )
}
Expand All @@ -65,7 +63,7 @@ public final class Client {
///
public func remove(chain: inout Chain) throws {
guard let id = chain.id else {
throw ClientError(message: "Chain not found in client.")
throw ClientError.chainNotFound
}
smoldot_remove_chain(id)
}
Expand All @@ -79,12 +77,12 @@ public final class Client {
///
public func send(request: JSONRPC2Request, to chain: Chain) throws {
guard let id = chain.id else {
throw ClientError(message: "Chain not found in client.")
throw ClientError.chainNotFound
}
let encoder = JSONEncoder()
let data = try encoder.encode(request)
guard let string = String(data: data, encoding: .utf8) else {
throw ClientError(message: "Error encoding request.")
throw ClientError.errorEncodingRequest
}
smoldot_json_rpc_request(id, string)
}
Expand All @@ -104,7 +102,7 @@ public final class Client {
Task.detached {
while (true) {
guard let id = chain.id else {
throw ClientError(message: "Chain not found in client.")
throw ClientError.chainNotFound
}
guard let cString = smoldot_wait_next_json_rpc_response(id) else {
break
Expand All @@ -128,7 +126,7 @@ public final class Client {
///
public func response(from chain: Chain) async throws -> String? {
guard let id = chain.id else {
throw ClientError(message: "Chain not found in client.")
throw ClientError.chainNotFound
}
guard let cString = smoldot_wait_next_json_rpc_response(id) else {
return nil
Expand Down
10 changes: 4 additions & 6 deletions Sources/SmoldotSwift/ClientError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

import Foundation

public struct ClientError: Error, CustomStringConvertible {
let message: String

public var description: String {
return "[error: \(message)]"
}
public enum ClientError: Error {
case chainHasAlreadyBeenAdded
case chainNotFound
case errorEncodingRequest
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fcbcfaf

Please sign in to comment.