Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump deps (protobufs are deps) #81

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "protobufs"]
path = protobufs
url = https://github.com/hashgraph/hedera-protobufs.git
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/grpc/grpc-swift.git",
"state" : {
"revision" : "f09cfb4d36315e2b48dbba1359179abf3cb2e6ac",
"version" : "1.14.2"
"revision" : "4ab02e1ae5b4dfdd723773e955b62f35ccbaa7c7",
"version" : "1.16.0"
}
},
{
Expand All @@ -50,8 +50,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "ff3d2212b6b093db7f177d0855adbc4ef9c5f036",
"version" : "1.0.3"
"revision" : "6c89474e62719ddcc1e9614989fff2f68208fe10",
"version" : "1.1.0"
}
},
{
Expand Down Expand Up @@ -122,8 +122,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "38feec96bcd929028939107684073554bf01abeb",
"version" : "1.25.2"
"revision" : "6d021a48483dbb273a9be43f65234bdc9185b364",
"version" : "1.26.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/objecthub/swift-numberkit.git", from: "2.4.1"),
.package(url: "https://github.com/thebarndog/swift-dotenv.git", from: "1.0.0"),
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.14.2"),
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.16.0"),
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.6.0"),
.package(url: "https://github.com/vsanthanam/AnyAsyncSequence.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"),
// swift-asn1 wants swift 5.7+ past 0.4
.package(url: "https://github.com/apple/swift-asn1.git", "0.3.0"..<"0.4.0"),
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", .upToNextMajor(from: "0.10.0")),
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,24 @@ protoc-gen-grpc-swift (from https://github.com/grpc/grpc-swift)

### Generate services
```bash
# cwd: `$REPO/sdk/swift`
protoc --swift_opt=Visibility=Public --swift_out=./Sources/HederaProtobufs/Services --proto_path=../../protobufs/services ../../protobufs/services/*
# cwd: `$REPO`
protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --swift_out=./Sources/HederaProtobufs/Services --proto_path=./protobufs/services protobufs/services/**.proto

# generate GRPC (if needed)
protoc --grpc-swift_opt=Visibility=Public,Server=false --grpc-swift_out=./Sources/HederaProtobufs/Services --proto_path=../../protobufs/services ../../protobufs/services/*
protoc --grpc-swift_opt=Visibility=Public,Server=false --grpc-swift_out=./Sources/HederaProtobufs/Services --proto_path=protobufs/services protobufs/services/**.proto
```

### Generate Mirror
```bash
# cwd: `$REPO/sdk/swift`
protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --swift_out=./Sources/HederaProtobufs/Mirror -I=../../protobufs/mirror -I=../../protobufs/services ../../protobufs/mirror/*
protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --swift_out=./Sources/HederaProtobufs/Mirror -I=protobufs/mirror -I=protobufs/services protobufs/mirror/**.proto

# generate GRPC (if needed)
protoc --grpc-swift_opt=Visibility=Public,FileNaming=PathToUnderscores,Server=false --grpc-swift_out=./Sources/HederaProtobufs/Mirror -I=../../protobufs/mirror -I=../../protobufs/services ../../protobufs/mirror/*
protoc --grpc-swift_opt=Visibility=Public,FileNaming=PathToUnderscores,Server=false --grpc-swift_out=./Sources/HederaProtobufs/Mirror -I=protobufs/mirror -I=protobufs/services protobufs/mirror/**.proto
```

### Generate SDK
```bash
# cwd: `$REPO/sdk/swift`
protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --swift_out=./Sources/HederaProtobufs/Sdk -I=../../protobufs/sdk -I=../../protobufs/services ../../protobufs/sdk/*
protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --swift_out=./Sources/HederaProtobufs/Sdk -I=protobufs/sdk -I=protobufs/services protobufs/sdk/**.proto
```
34 changes: 8 additions & 26 deletions Sources/Hedera/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,16 @@ import GRPC
import NIOConcurrencyHelpers
import NIOCore

// safety: Atomics just forgot to put the conformance.
private struct AtomicBool: @unchecked Sendable {
init(_ value: Bool) {
self.inner = .init(value)
}

fileprivate let inner: ManagedAtomic<Bool>
}

// safety: Atomics just forgot to put the conformance.
private struct AtomicInt64: @unchecked Sendable {
init(_ value: Int64) {
self.inner = .init(value)
}

fileprivate let inner: ManagedAtomic<Int64>
}

/// Managed client for use on the Hedera network.
public final class Client: Sendable {
internal let eventLoop: NIOCore.EventLoopGroup

private let mirrorNetwork: MirrorNetwork
internal let network: Network
private let operatorInner: NIOLockedValueBox<Operator?>
private let autoValidateChecksumsInner: AtomicBool
private let regenerateTransactionIdInner: AtomicBool
private let maxTransactionFeeInner: AtomicInt64
private let autoValidateChecksumsInner: ManagedAtomic<Bool>
private let regenerateTransactionIdInner: ManagedAtomic<Bool>
private let maxTransactionFeeInner: ManagedAtomic<Int64>

private init(
network: Network,
Expand Down Expand Up @@ -91,7 +73,7 @@ public final class Client: Sendable {
}

internal var maxTransactionFee: Hbar? {
let value = maxTransactionFeeInner.inner.load(ordering: .relaxed)
let value = maxTransactionFeeInner.load(ordering: .relaxed)

guard value != 0 else {
return nil
Expand Down Expand Up @@ -212,8 +194,8 @@ public final class Client: Sendable {
}

fileprivate var autoValidateChecksums: Bool {
get { self.autoValidateChecksumsInner.inner.load(ordering: .relaxed) }
set(value) { self.autoValidateChecksumsInner.inner.store(value, ordering: .relaxed) }
get { self.autoValidateChecksumsInner.load(ordering: .relaxed) }
set(value) { self.autoValidateChecksumsInner.store(value, ordering: .relaxed) }
}

@discardableResult
Expand All @@ -233,8 +215,8 @@ public final class Client: Sendable {
///
/// >Note: Some operations forcibly disable transaction ID regeneration, such as setting the transaction ID explicitly.
public var defaultRegenerateTransactionId: Bool {
get { self.regenerateTransactionIdInner.inner.load(ordering: .relaxed) }
set(value) { self.regenerateTransactionIdInner.inner.store(value, ordering: .relaxed) }
get { self.regenerateTransactionIdInner.load(ordering: .relaxed) }
set(value) { self.regenerateTransactionIdInner.store(value, ordering: .relaxed) }
}

/// Sets whether or not the transaction ID should be refreshed if a ``Status/transactionExpired`` occurs.
Expand Down
30 changes: 9 additions & 21 deletions Sources/Hedera/Client/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ internal final class ChannelBalancer: GRPCChannel {
}
}

// todo: remove this once `Atomic` 1.1 is released (where they add Sendable declarations to things)
internal struct ManagedAtomicWrapper<Value> where Value: Atomics.AtomicValue {
internal init(_ value: Value) {
self.inner = ManagedAtomic(value)
}

internal let inner: ManagedAtomic<Value>
}

// safety: Atomics just forgot to put the conformance.
extension ManagedAtomicWrapper: @unchecked Sendable where Value: Sendable {}

internal final class Network: Sendable {
internal struct Config {
internal let map: [AccountId: Int]
Expand All @@ -87,8 +75,8 @@ internal final class Network: Sendable {
map: [AccountId: Int],
nodes: [AccountId],
channels: [GRPCChannel],
healthy: [ManagedAtomicWrapper<Int64>],
lastPinged: [ManagedAtomicWrapper<Int64>]
healthy: [ManagedAtomic<Int64>],
lastPinged: [ManagedAtomic<Int64>]
) {
self.map = map
self.nodes = nodes
Expand All @@ -100,8 +88,8 @@ internal final class Network: Sendable {
internal let map: [AccountId: Int]
internal let nodes: [AccountId]
fileprivate let channels: [GRPCChannel]
fileprivate let healthy: [ManagedAtomicWrapper<Int64>]
fileprivate let lastPinged: [ManagedAtomicWrapper<Int64>]
fileprivate let healthy: [ManagedAtomic<Int64>]
fileprivate let lastPinged: [ManagedAtomic<Int64>]

fileprivate convenience init(config: Config, eventLoop: NIOCore.EventLoopGroup) {
// todo: someone verify this code pls.
Expand All @@ -110,8 +98,8 @@ internal final class Network: Sendable {
}

// note: `Array(repeating: <element>, count: Int)` does *not* work the way you'd want with reference types.
let healthy = (0..<config.nodes.count).map { _ in ManagedAtomicWrapper(Int64(0)) }
let lastPinged = (0..<config.nodes.count).map { _ in ManagedAtomicWrapper(Int64(0)) }
let healthy = (0..<config.nodes.count).map { _ in ManagedAtomic(Int64(0)) }
let lastPinged = (0..<config.nodes.count).map { _ in ManagedAtomic(Int64(0)) }

self.init(
map: config.map,
Expand Down Expand Up @@ -162,15 +150,15 @@ internal final class Network: Sendable {
}

internal func markNodeUsed(_ index: Int, now: Timestamp) {
lastPinged[index].inner.store(Int64(now.unixTimestampNanos), ordering: .relaxed)
lastPinged[index].store(Int64(now.unixTimestampNanos), ordering: .relaxed)
}

internal func nodeRecentlyPinged(_ index: Int, now: Timestamp) -> Bool {
lastPinged[index].inner.load(ordering: .relaxed) > Int64((now - .minutes(15)).unixTimestampNanos)
lastPinged[index].load(ordering: .relaxed) > Int64((now - .minutes(15)).unixTimestampNanos)
}

internal func isNodeHealthy(_ index: Int, _ now: Timestamp) -> Bool {
healthy[index].inner.load(ordering: .relaxed) < Int64(now.unixTimestampNanos)
healthy[index].load(ordering: .relaxed) < Int64(now.unixTimestampNanos)
}
}

Expand Down
7 changes: 7 additions & 0 deletions Sources/Hedera/Contract/ContractId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public struct ContractId: EntityId {
}
}

#if compiler(>=5.7)
extension ContractId: Sendable {}
#else
// Swift 5.7 added the conformance to data, despite to the best of my knowledge, not changing anything in the underlying type.
extension ContractId: @unchecked Sendable {}
#endif

extension ContractId: TryProtobufCodable {
internal typealias Protobuf = Proto_ContractID

Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/EntityId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ internal enum PartialEntityId<S: StringProtocol> {
// fixme(sr): How do DRY?

/// The unique identifier for a file on Hedera.
public struct FileId: EntityId, ValidateChecksums {
public struct FileId: EntityId, ValidateChecksums, Sendable {
public init(shard: UInt64 = 0, realm: UInt64 = 0, num: UInt64, checksum: Checksum?) {
self.shard = shard
self.realm = realm
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/Schedule/ScheduleId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
import HederaProtobufs

/// The unique identifier for a schedule on Hedera.
public struct ScheduleId: EntityId, ValidateChecksums {
public struct ScheduleId: EntityId, ValidateChecksums, Sendable {
public init(shard: UInt64 = 0, realm: UInt64 = 0, num: UInt64, checksum: Checksum?) {
self.shard = shard
self.realm = realm
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/Token/TokenId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
import HederaProtobufs

/// The unique identifier for a token on Hedera.
public struct TokenId: EntityId, ValidateChecksums {
public struct TokenId: EntityId, ValidateChecksums, Sendable {
public init(shard: UInt64 = 0, realm: UInt64 = 0, num: UInt64, checksum: Checksum?) {
self.shard = shard
self.realm = realm
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/Topic/TopicId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
import HederaProtobufs

/// The unique identifier for a topic on Hedera.
public struct TopicId: EntityId, ValidateChecksums {
public struct TopicId: EntityId, ValidateChecksums, Sendable {
public init(shard: UInt64 = 0, realm: UInt64 = 0, num: UInt64, checksum: Checksum?) {
self.shard = shard
self.realm = realm
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/Transaction/TransactionSources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private struct OnceRace<T> {

// Note: This is fine: https://github.com/apple/swift-atomics/pull/46/files#diff-a94af62997ccad839d2a78a1fe0e824e43ee0523f01a6034a2e2abc34aa440a7R156
// ideally this wouldn't need to be @unchecked
extension OnceRace: @unchecked Sendable where T: Sendable {}
extension OnceRace: Sendable where T: Sendable {}

internal struct SourceChunk: Sendable {
fileprivate init(map: TransactionSources.Ref, index: Int) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hedera/TransactionReceipt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import HederaProtobufs

// TODO: exchangeRate
/// The summary of a transaction's result so far, if the transaction has reached consensus.
public struct TransactionReceipt {
public struct TransactionReceipt: Sendable {
internal init(
transactionId: TransactionId? = nil,
status: Status,
Expand Down
Loading