Skip to content

Commit

Permalink
add v15 runtime parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ERussel committed May 21, 2024
1 parent 1e7d37a commit b8d5525
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Example/SubstrateSdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0CE840E92BFBFB60003C2B7A /* polkadot-v15 in Resources */ = {isa = PBXBuildFile; fileRef = 0CE840E82BFBFB60003C2B7A /* polkadot-v15 */; };
0CE840FA2BFCA0F3003C2B7A /* RuntimeMetadataV15Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CE840F92BFCA0F3003C2B7A /* RuntimeMetadataV15Tests.swift */; };
0CE92A6F2B2C5906009A21E9 /* kusama-v14-metadata-latest in Resources */ = {isa = PBXBuildFile; fileRef = 0CE92A6E2B2C5906009A21E9 /* kusama-v14-metadata-latest */; };
6976AE87036D532DDD11B3D9 /* Pods_SubstrateSdkExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 00060C8DD6432907D485E325 /* Pods_SubstrateSdkExample.framework */; };
841D528A25C4A11900DFA15B /* RegexParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841D528925C4A11900DFA15B /* RegexParserTests.swift */; };
Expand Down Expand Up @@ -139,6 +140,7 @@
/* Begin PBXFileReference section */
00060C8DD6432907D485E325 /* Pods_SubstrateSdkExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SubstrateSdkExample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
0CE840E82BFBFB60003C2B7A /* polkadot-v15 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "polkadot-v15"; sourceTree = "<group>"; };
0CE840F92BFCA0F3003C2B7A /* RuntimeMetadataV15Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuntimeMetadataV15Tests.swift; sourceTree = "<group>"; };
0CE92A6E2B2C5906009A21E9 /* kusama-v14-metadata-latest */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "kusama-v14-metadata-latest"; sourceTree = "<group>"; };
27A1BEB05AA8335CF4053A81 /* Pods_SubstrateSdkTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SubstrateSdkTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2E305CB3E5589CF7DC3BD962 /* Pods-SubstrateSdkTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SubstrateSdkTests.debug.xcconfig"; path = "Target Support Files/Pods-SubstrateSdkTests/Pods-SubstrateSdkTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -363,6 +365,7 @@
84F439BB25DBF8D600AEDA56 /* TypeRegistryNetworkValidationTests.swift */,
84F43A2325DC124600AEDA56 /* TypeRegistryCatalogTests.swift */,
84AF376C271A3AE5007408D6 /* RuntimeMetadataV14Tests.swift */,
0CE840F92BFCA0F3003C2B7A /* RuntimeMetadataV15Tests.swift */,
84AF3779271DA9A2007408D6 /* ScaleRegistryTests.swift */,
);
path = Runtime;
Expand Down Expand Up @@ -914,6 +917,7 @@
849E0AA825C96AD600B33506 /* CompactNodeFactoryTests.swift in Sources */,
849E0B3025CBD1CC00B33506 /* TypeRegistryCatalogValidationTests.swift in Sources */,
84DA3B4324C8CE5A00B5E27F /* ScaleStringTests.swift in Sources */,
0CE840FA2BFCA0F3003C2B7A /* RuntimeMetadataV15Tests.swift in Sources */,
84AF376D271A3AE5007408D6 /* RuntimeMetadataV14Tests.swift in Sources */,
84DA3B4224C8CE5A00B5E27F /* ScaleUInt64Tests.swift in Sources */,
849E0A7A25C94E3F00B33506 /* EnumMappingFactoryTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation
import BigInt

public struct RuntimeMetadataContainer {
public enum OneOfRuntimeMetadata {
case v13(_ metadata: RuntimeMetadata)
case v14(_ metadata: RuntimeMetadataV14)
case v15(_ metadata: RuntimeMetadataV15)
}

public let metaReserved: UInt32
Expand All @@ -27,6 +29,8 @@ extension RuntimeMetadataContainer: ScaleCodable {
try metadata.encode(scaleEncoder: scaleEncoder)
case .v14(let metadata):
try metadata.encode(scaleEncoder: scaleEncoder)
case let .v15(metadata):
try metadata.encode(scaleEncoder: scaleEncoder)
}
}

Expand All @@ -37,9 +41,42 @@ extension RuntimeMetadataContainer: ScaleCodable {
if runtimeMetadataVersion < 14 {
let metadata = try RuntimeMetadata(scaleDecoder: scaleDecoder)
runtimeMetadata = .v13(metadata)
} else {
} else if runtimeMetadataVersion == 14 {
let metadata = try RuntimeMetadataV14(scaleDecoder: scaleDecoder)
runtimeMetadata = .v14(metadata)
} else {
let metadata = try RuntimeMetadataV15(scaleDecoder: scaleDecoder)
runtimeMetadata = .v15(metadata)
}
}
}

public enum RuntimeMetadataContainerError: Error {
case nonExistentMetadata
case invalidMetadataLength
}

extension RuntimeMetadataContainer {
/**
* Can be used to read Option<OpaqueMetadata>, which is the response of
* runtime call metadata_versionedMetadata()
*/
public static func createFromOpaque(data: Data) throws -> RuntimeMetadataContainer {
let scaleDecoder = try ScaleDecoder(data: data)

let exists = try Bool(scaleDecoder: scaleDecoder)

guard exists else {
throw RuntimeMetadataContainerError.nonExistentMetadata
}

// skip metadata len
let metadataLength = try BigUInt(scaleDecoder: scaleDecoder)

guard metadataLength <= scaleDecoder.remained else {
throw RuntimeMetadataContainerError.invalidMetadataLength
}

return try RuntimeMetadataContainer(scaleDecoder: scaleDecoder)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Foundation
import BigInt

struct RuntimeMetadataV15 {
public struct RuntimeMetadataV15 {
public let types: RuntimeTypesLookup
public let pallets: [PalletMetadataV15]
public let extrinsic: ExtrinsicMetadataV15
public let runtimeType: SiLookupId

public init(
types: RuntimeTypesLookup,
pallets: [PalletMetadataV15],
Expand All @@ -24,7 +24,7 @@ extension RuntimeMetadataV15: PostV14RuntimeMetadataProtocol {
public var postV14Pallets: [PostV14PalletMetadataProtocol] {
pallets
}

public var postV14Extrinsic: PostV14ExtrinsicMetadataProtocol {
extrinsic
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Helpers/RuntimeHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class RuntimeHelper {
switch container.runtimeMetadata {
case .v13(let metadata):
return metadata
case .v14:
case .v14, .v15:
throw RuntimeHelperError.unexpectedMetadata
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Helpers/ScaleInfoHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ScaleInfoHelper {
switch container.runtimeMetadata {
case .v14(let metadata):
return metadata
case .v13:
case .v13, .v15:
throw RuntimeHelperError.unexpectedMetadata
}
}
Expand Down
39 changes: 39 additions & 0 deletions Tests/Runtime/RuntimeMetadataV15Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import XCTest
import SubstrateSdk

final class RuntimeMetadataV15Tests: XCTestCase {
func testPolkadotV15MetadataParsing() {
performOpaqueV15MetadataTest(filename: "polkadot-v15")
}

private func performOpaqueV15MetadataTest(filename: String) {
do {
guard let url = Bundle(for: type(of: self))
.url(forResource: filename, withExtension: "") else {
XCTFail("Can't find metadata file")
return
}

let hex = try String(contentsOf: url)
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let expectedData = try Data(hexString: hex)

let runtimeMetadataContainer = try RuntimeMetadataContainer.createFromOpaque(data: expectedData)

guard case .v15 = runtimeMetadataContainer.runtimeMetadata else {
XCTFail("unexpected metadata")
return
}

let encoder = ScaleEncoder()
try runtimeMetadataContainer.encode(scaleEncoder: encoder)
let resultData = encoder.encode()

XCTAssertEqual(Data(expectedData.suffix(resultData.count)), resultData)

UIPasteboard.general.string = resultData.toHex()
} catch {
XCTFail("Unexpected error: \(error)")
}
}
}

0 comments on commit b8d5525

Please sign in to comment.