Skip to content

Commit

Permalink
better descriptions for Value prints
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jun 22, 2023
1 parent 116204f commit f9bb10a
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Sources/Substrate/Block/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol SomeBlock: Decodable {
public extension SomeBlock {
var hash: THeader.THasher.THash { header.hash }

func anyExtrinsics() throws -> [Extrinsic<AnyCall<RuntimeTypeId>, TExtrinsic.TManager.TSignedExtra>] {
func anyExtrinsics() throws -> [AnyExtrinsic<AnyCall<RuntimeTypeId>, TExtrinsic.TManager>] {
try extrinsics.map { try $0.decode() }
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Substrate/Extrinsic/Call.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public struct AnyCall<C>: Call {
}
}

extension AnyCall: CustomStringConvertible {
public var description: String {
"\(pallet).\(name)(\(params))"
}
}

extension AnyCall: ScaleRuntimeDecodable where C == RuntimeTypeId {
public init(from decoder: ScaleDecoder, runtime: Runtime) throws {
let palletIdx = try decoder.decode(UInt8.self)
Expand Down
49 changes: 38 additions & 11 deletions Sources/Substrate/Extrinsic/Extrinsic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import ScaleCodec
import JsonRPC

public struct Extrinsic<C: Call, Extra> {
public struct Extrinsic<C: Call, Extra>: CustomStringConvertible {
public let call: C
public let extra: Extra
public let isSigned: Bool
Expand All @@ -20,9 +20,13 @@ public struct Extrinsic<C: Call, Extra> {
self.extra = extra
self.isSigned = signed
}

public var description: String {
"\(isSigned ? "SignedExtrinsic" : "UnsignedExtrinsic")(call: \(call), extra: \(extra))"
}
}

public struct ExtrinsicSignPayload<C: Call, Extra> {
public struct ExtrinsicSignPayload<C: Call, Extra>: CustomStringConvertible {
public let call: C
public let extra: Extra

Expand All @@ -31,20 +35,45 @@ public struct ExtrinsicSignPayload<C: Call, Extra> {
self.call = call
self.extra = extra
}

public var description: String {
"ExtrinsicPayload(call: \(call), extra: \(extra))"
}
}

public enum AnyExtrinsicExtra<Signed, Unsigned>: CustomStringConvertible {
case unsigned(Unsigned)
case signed(Signed)

var signed: Signed? {
switch self {
case .signed(let extra): return extra
default: return nil
}
}

public var description: String {
switch self {
case .signed(let extra): return "\(extra)"
case .unsigned(let extra): return "\(extra)"
}
}
}

public typealias AnyExtrinsic<C: Call, M: ExtrinsicManager> =
Extrinsic<C, AnyExtrinsicExtra<M.TSignedExtra, M.TUnsignedExtra>>

public protocol OpaqueExtrinsic<TManager>: Decodable {
associatedtype TManager: ExtrinsicManager

func hash() -> TManager.RT.THasher.THash

func decode<C: Call & ScaleRuntimeDecodable>() throws -> Extrinsic<C, TManager.TSignedExtra>
func decode<C: Call & ScaleRuntimeDecodable>() throws -> AnyExtrinsic<C, TManager>

static var version: UInt8 { get }
}

public enum ExtrinsicCodingError: Error {
case badExtraType(expected: String, got: String)
case badExtrinsicVersion(supported: UInt8, got: UInt8)
case badExtrasCount(expected: Int, got: Int)
case parameterNotFound(extension: ExtrinsicExtensionId, parameter: String)
Expand All @@ -69,9 +98,6 @@ public protocol ExtrinsicManager<RT> {

func unsigned<C: Call>(call: C, params: TUnsignedParams) async throws -> Extrinsic<C, TUnsignedExtra>
func encode<C: Call>(unsigned extrinsic: Extrinsic<C, TUnsignedExtra>, in encoder: ScaleEncoder) throws
func decode<C: Call & ScaleRuntimeDecodable>(
unsigned decoder: ScaleDecoder
) throws -> Extrinsic<C, TUnsignedExtra>

func params<C: Call>(
unsigned extrinsic: Extrinsic<C, TUnsignedExtra>,
Expand All @@ -91,7 +117,8 @@ public protocol ExtrinsicManager<RT> {
address: RT.TAddress,
signature: RT.TSignature) throws -> Extrinsic<C, TSignedExtra>
func encode<C: Call>(signed extrinsic: Extrinsic<C, TSignedExtra>, in encoder: ScaleEncoder) throws
func decode<C: Call & ScaleRuntimeDecodable>(signed decoder: ScaleDecoder) throws -> Extrinsic<C, TSignedExtra>

func decode<C: Call & ScaleRuntimeDecodable>(from decoder: ScaleDecoder) throws -> AnyExtrinsic<C, Self>

mutating func setSubstrate<S: SomeSubstrate<RT>>(substrate: S) throws

Expand All @@ -110,7 +137,7 @@ public extension ExtrinsicManager {
throw ExtrinsicCodingError.unsupportedSubstrate(reason: "Different manager in runtime")
}
return manager
}
}
}

public struct BlockExtrinsic<TManager: ExtrinsicManager>: OpaqueExtrinsic where TManager.RT: Config {
Expand All @@ -129,8 +156,8 @@ public struct BlockExtrinsic<TManager: ExtrinsicManager>: OpaqueExtrinsic where
try! TManager.RT.THasher.THash(runtime.hasher.hash(data: data))
}

public func decode<C: Call & ScaleRuntimeDecodable>() throws -> Extrinsic<C, TManager.TSignedExtra> {
try TManager.get(from: runtime).decode(signed: runtime.decoder(with: data))
public func decode<C: Call & ScaleRuntimeDecodable>() throws -> AnyExtrinsic<C, TManager> {
try TManager.get(from: runtime).decode(from: runtime.decoder(with: data))
}

public static var version: UInt8 { TManager.version }
Expand Down
39 changes: 4 additions & 35 deletions Sources/Substrate/Extrinsic/ExtrinsicV4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,8 @@ public class ExtrinsicV4Manager<RC: Config, SE: SignedExtensionsProvider<RC>>: E
}

public func decode<C: Call & ScaleRuntimeDecodable>(
unsigned decoder: ScaleDecoder
) throws -> Extrinsic<C, TUnsignedExtra> {
let ext = try self.decode(decoder: decoder, call: C.self)
guard !ext.isSigned else {
throw ExtrinsicCodingError.badExtraType(
expected: String(describing: TUnsignedExtra.self),
got: String(describing: TSignedExtra.self)
)
}
return Extrinsic(call: ext.call, extra: (), signed: false)
}

public func decode<C: Call & ScaleRuntimeDecodable>(
signed decoder: ScaleDecoder
) throws -> Extrinsic<C, TSignedExtra> {
let ext = try self.decode(decoder: decoder, call: C.self)
guard ext.isSigned else {
throw ExtrinsicCodingError.badExtraType(
expected: String(describing: TSignedExtra.self),
got: String(describing: TUnsignedExtra.self)
)
}
return Extrinsic(call: ext.call, extra: ext.extra!, signed: true)

}

private func decode<C: Call & ScaleRuntimeDecodable>(
decoder: ScaleDecoder,
call: C.Type
) throws -> Extrinsic<
C,
(address: RC.TAddress, signature: RC.TSignature, extra: SE.TExtra)?>
{
from decoder: ScaleDecoder
) throws -> Extrinsic<C, AnyExtrinsicExtra<TSignedExtra, TUnsignedExtra>> {
let decoder = try runtime.decoder(with: decoder.decode(Data.self))
var version = try decoder.decode(UInt8.self)
let isSigned = version & 0b1000_0000 > 0
Expand All @@ -128,10 +97,10 @@ public class ExtrinsicV4Manager<RC: Config, SE: SignedExtensionsProvider<RC>>: E
let signature = try RC.TSignature(from: decoder, runtime: runtime)
let extra = try extensions.extra(from: decoder)
return Extrinsic(call: try C(from: decoder, runtime: runtime),
extra: (address, signature, extra), signed: true)
extra: .signed((address, signature, extra)), signed: true)
} else {
return Extrinsic(call: try C(from: decoder, runtime: runtime),
extra: nil, signed: false)
extra: .unsigned(()), signed: false)
}
}

Expand Down
28 changes: 24 additions & 4 deletions Sources/Substrate/Metadata/RuntimeTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import Foundation
import ScaleCodec

public struct RuntimeTypeId: ScaleCodable, Hashable, Equatable, ExpressibleByIntegerLiteral, RawRepresentable {
public struct RuntimeTypeId: ScaleCodable, Hashable, Equatable,
ExpressibleByIntegerLiteral, RawRepresentable,
CustomStringConvertible
{
public typealias IntegerLiteralType = UInt32
public typealias RawValue = UInt32

Expand All @@ -35,16 +38,24 @@ public struct RuntimeTypeId: ScaleCodable, Hashable, Equatable, ExpressibleByInt
public func encode(in encoder: ScaleCodec.ScaleEncoder) throws {
try encoder.encode(id, .compact)
}

public var description: String {
"#\(id)"
}
}

public struct RuntimeTypeInfo {
public struct RuntimeTypeInfo: CustomStringConvertible {
public let id: RuntimeTypeId
public let type: RuntimeType

public init(id: RuntimeTypeId, type: RuntimeType) {
self.id = id
self.type = type
}

public var description: String {
"\(type)\(id)"
}
}

extension RuntimeTypeInfo: ScaleCodable {
Expand All @@ -58,7 +69,7 @@ extension RuntimeTypeInfo: ScaleCodable {
}
}

public struct RuntimeType {
public struct RuntimeType: CustomStringConvertible {
public let path: [String]
public let parameters: [RuntimeTypeParameter]
public let definition: RuntimeTypeDefinition
Expand All @@ -75,6 +86,11 @@ public struct RuntimeType {
self.definition = definition
self.docs = docs
}

public var description: String {
let params = parameters.isEmpty ? "" : "<\(parameters.map{$0.description}.joined(separator: ", "))>"
return "\(pathBasedName ?? "_")\(params){\(definition)}"
}
}

extension RuntimeType {
Expand All @@ -97,14 +113,18 @@ extension RuntimeType: ScaleCodable {
}
}

public struct RuntimeTypeParameter {
public struct RuntimeTypeParameter: CustomStringConvertible {
public let name: String
public let type: RuntimeTypeId?

public init(name: String, type: RuntimeTypeId?) {
self.name = name
self.type = type
}

public var description: String {
"\(name)[\(type.map{$0.description} ?? "-")]"
}
}

extension RuntimeTypeParameter: ScaleCodable {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Substrate/Types/AccountId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ public struct AccountId32: StaticAccountId, Hashable, Equatable {
extension AccountId32: ValueRepresentable {
public func asValue() throws -> Value<Void> { .bytes(raw) }
}

extension AccountId32: CustomStringConvertible {
public var description: String { string }
}
12 changes: 12 additions & 0 deletions Sources/Substrate/Types/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ extension MultiAddress: ScaleRuntimeCodable {
}
}
}

extension MultiAddress: CustomStringConvertible {
public var description: String {
switch self {
case .id(let acc): return "\(acc)"
case .index(let index): return "\(index)"
case .raw(let raw): return raw.hex()
case .address20(let raw): return raw.hex()
case .address32(let raw): return raw.hex()
}
}
}
6 changes: 5 additions & 1 deletion Sources/Substrate/Types/BitSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import ScaleCodec

public struct BitSequence: RandomAccessCollection, RangeReplaceableCollection,
MutableCollection, Hashable, Equatable
MutableCollection, Hashable, Equatable, CustomStringConvertible
{
private var storage: [Bool]

Expand Down Expand Up @@ -42,6 +42,10 @@ public struct BitSequence: RandomAccessCollection, RangeReplaceableCollection,
public mutating func reserveCapacity(_ n: Int) {
storage.reserveCapacity(n)
}

public var description: String {
storage.description
}
}

extension BitSequence {
Expand Down
10 changes: 9 additions & 1 deletion Sources/Substrate/Types/CryptoTypeId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@

import Foundation

public enum CryptoTypeId: String, Hashable, Equatable {
public enum CryptoTypeId: String, Hashable, Equatable, CustomStringConvertible {
case ed25519 = "ed25"
case sr25519 = "sr25"
case ecdsa = "ecds"

public var description: String {
switch self {
case .ecdsa: return "ECDSA"
case .ed25519: return "Ed25519"
case .sr25519: return "Sr25519"
}
}
}

public enum CryptoError: Error {
Expand Down
14 changes: 10 additions & 4 deletions Sources/Substrate/Types/Signature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public extension CryptoTypeId {
}
}

public struct EcdsaSignature: ScaleFixedData, Signature, Hashable, Equatable {
public struct EcdsaSignature: ScaleFixedData, Signature, Hashable, Equatable, CustomStringConvertible {
public let signature: Data

public init(raw: Data, algorithm: CryptoTypeId, runtime: any Runtime) throws {
Expand Down Expand Up @@ -62,7 +62,7 @@ public struct EcdsaSignature: ScaleFixedData, Signature, Hashable, Equatable {
public static let fixedBytesCount: Int = CryptoTypeId.ecdsa.signatureBytesCount
}

public struct Ed25519Signature: ScaleFixedData, Signature, Hashable, Equatable {
public struct Ed25519Signature: ScaleFixedData, Signature, Hashable, Equatable, CustomStringConvertible {
public let signature: Data

public init(raw: Data, algorithm: CryptoTypeId, runtime: any Runtime) throws {
Expand Down Expand Up @@ -92,7 +92,7 @@ public struct Ed25519Signature: ScaleFixedData, Signature, Hashable, Equatable {
public static let fixedBytesCount: Int = CryptoTypeId.ed25519.signatureBytesCount
}

public struct Sr25519Signature: ScaleFixedData, Signature, Hashable, Equatable {
public struct Sr25519Signature: ScaleFixedData, Signature, Hashable, Equatable, CustomStringConvertible {
public let signature: Data

public init(raw: Data, algorithm: CryptoTypeId, runtime: any Runtime) throws {
Expand Down Expand Up @@ -122,7 +122,7 @@ public struct Sr25519Signature: ScaleFixedData, Signature, Hashable, Equatable {
public static let fixedBytesCount: Int = CryptoTypeId.sr25519.signatureBytesCount
}

public enum MultiSignature: Hashable, Equatable {
public enum MultiSignature: Hashable, Equatable, CustomStringConvertible {
case ed25519(Ed25519Signature)
case sr25519(Sr25519Signature)
case ecdsa(EcdsaSignature)
Expand Down Expand Up @@ -180,3 +180,9 @@ extension MultiSignature: ScaleCodable {
}

extension MultiSignature: ScaleRuntimeCodable {}

public extension Signature {
var description: String {
"\(algorithm)(\(raw.hex()))"
}
}
1 change: 1 addition & 0 deletions Sources/Substrate/Types/Value/Value+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extension Value: RuntimeDynamicDecodable where C == RuntimeTypeId {


}

extension Value where C == RuntimeTypeId {
public init(from container: inout ValueDecodingContainer,
`as` type: RuntimeTypeId,
Expand Down

0 comments on commit f9bb10a

Please sign in to comment.