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

Support decoding metadata v15 #89

Merged
merged 3 commits into from
May 24, 2024
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
// App version
versionName = '2.0.2'
versionName = '2.0.3'
versionCode = 1

// SDK and tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io.novasama.substrate_sdk_android.runtime.metadata

import io.emeraldpay.polkaj.scale.ScaleCodecReader
import io.novasama.substrate_sdk_android.extensions.fromHex
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PostV14MetadataSchema
import io.novasama.substrate_sdk_android.runtime.metadata.v14.RuntimeMetadataSchemaV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.RuntimeMetadataSchemaV15
import io.novasama.substrate_sdk_android.scale.EncodableStruct
import io.novasama.substrate_sdk_android.scale.Schema
import io.novasama.substrate_sdk_android.scale.uint32
Expand All @@ -18,21 +20,56 @@ class RuntimeMetadataReader private constructor(
val metadata: EncodableStruct<*>
) {

@Suppress("UNCHECKED_CAST")
val metadataPostV14: EncodableStruct<PostV14MetadataSchema<*>>
get() {
require(metadata.schema is PostV14MetadataSchema<*>) {
"Metadata is pre v14"
}

return metadata as EncodableStruct<PostV14MetadataSchema<*>>
}

companion object {

@OptIn(ExperimentalUnsignedTypes::class)
fun read(metadaScale: String): RuntimeMetadataReader {
return read(metadaScale.fromHex())
}

val scaleCoderReader = ScaleCodecReader(metadaScale.fromHex())
/**
* Can be used to read Option<OpaqueMetadata>, which is the response of
* runtime call metadata_versionedMetadata()
*/
fun readOpaque(opaqueBytes: ByteArray): RuntimeMetadataReader {
val scaleCoderReader = ScaleCodecReader(opaqueBytes)
val exists = scaleCoderReader.readBoolean()
require(exists) {
"Non existent metadata"
}
// Skip opaque vec length
scaleCoderReader.readCompactInt()

val runtimeVersion = Magic.read(scaleCoderReader)[Magic.runtimeVersion].toInt()
return read(scaleCoderReader)
}

fun read(metadataBytes: ByteArray): RuntimeMetadataReader {
val scaleCoderReader = ScaleCodecReader(metadataBytes)
return read(scaleCoderReader)
}

@OptIn(ExperimentalUnsignedTypes::class)
private fun read(reader: ScaleCodecReader): RuntimeMetadataReader {
val runtimeVersion = Magic.read(reader)[Magic.runtimeVersion].toInt()

val metadata = when {
runtimeVersion < 14 -> {
RuntimeMetadataSchema.read(scaleCoderReader)
RuntimeMetadataSchema.read(reader)
}
runtimeVersion == 14 -> {
RuntimeMetadataSchemaV14.read(reader)
}
else -> {
RuntimeMetadataSchemaV14.read(scaleCoderReader)
RuntimeMetadataSchemaV15.read(reader)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ import io.novasama.substrate_sdk_android.runtime.metadata.module.Module
import io.novasama.substrate_sdk_android.runtime.metadata.module.Storage
import io.novasama.substrate_sdk_android.runtime.metadata.module.StorageEntry
import io.novasama.substrate_sdk_android.runtime.metadata.module.StorageEntryType
import io.novasama.substrate_sdk_android.runtime.metadata.v14.ExtrinsicMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.MapTypeV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PalletCallMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PalletConstantMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PalletErrorMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PalletEventMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PalletMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.RuntimeMetadataSchemaV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PostV14ExtrinsicMetadataSchema
import io.novasama.substrate_sdk_android.runtime.metadata.v14.PostV14PalletMetadataSchema
import io.novasama.substrate_sdk_android.runtime.metadata.v14.SignedExtensionMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.StorageEntryMetadataV14
import io.novasama.substrate_sdk_android.runtime.metadata.v14.StorageMetadataV14
import io.novasama.substrate_sdk_android.scale.EncodableStruct
import java.math.BigInteger

@OptIn(ExperimentalUnsignedTypes::class)
object V14RuntimeBuilder : RuntimeBuilder {
object PostV14RuntimeBuilder : RuntimeBuilder {

override fun buildMetadata(
reader: RuntimeMetadataReader,
Expand All @@ -46,20 +45,20 @@ object V14RuntimeBuilder : RuntimeBuilder {
): RuntimeMetadata {
val metadataStruct = reader.metadata

require(metadataStruct.schema is RuntimeMetadataSchemaV14)
val schema = reader.metadataPostV14.schema

return RuntimeMetadata(
extrinsic = buildExtrinsic(
metadataStruct[RuntimeMetadataSchemaV14.extrinsic],
metadataStruct[schema.extrinsic],
typeRegistry
),
modules = buildModules(metadataStruct[RuntimeMetadataSchemaV14.pallets], typeRegistry),
modules = buildModules(metadataStruct[schema.pallets], typeRegistry),
runtimeVersion = reader.metadataVersion.toBigInteger()
)
}

private fun buildModules(
modulesRaw: List<EncodableStruct<PalletMetadataV14>>,
modulesRaw: List<EncodableStruct<PostV14PalletMetadataSchema<*>>>,
typeRegistry: TypeRegistry
): Map<String, Module> {
return modulesRaw.map {
Expand All @@ -69,25 +68,26 @@ object V14RuntimeBuilder : RuntimeBuilder {

private fun buildModule(
typeRegistry: TypeRegistry,
struct: EncodableStruct<PalletMetadataV14>,
struct: EncodableStruct<PostV14PalletMetadataSchema<*>>,
): Module {
val moduleName = struct[PalletMetadataV14.name]
val moduleIndex = struct[PalletMetadataV14.index].toInt()
val schema = struct.schema
val moduleName = struct[schema.name]
val moduleIndex = struct[schema.index].toInt()

return Module(
name = moduleName,
index = moduleIndex.toBigInteger(),
storage = struct[PalletMetadataV14.storage]?.let {
storage = struct[schema.storage]?.let {
buildStorage(typeRegistry, it, moduleName)
},
calls = struct[PalletMetadataV14.calls]?.let {
calls = struct[schema.calls]?.let {
buildCalls(typeRegistry, it, moduleIndex)
},
events = struct[PalletMetadataV14.events]?.let {
events = struct[schema.events]?.let {
buildEvents(typeRegistry, it, moduleIndex)
},
constants = buildConstants(typeRegistry, struct[PalletMetadataV14.constants]),
errors = struct[PalletMetadataV14.errors]?.let {
constants = buildConstants(typeRegistry, struct[schema.constants]),
errors = struct[schema.errors]?.let {
buildErrors(typeRegistry, it)
} ?: emptyMap()
)
Expand Down Expand Up @@ -251,18 +251,22 @@ object V14RuntimeBuilder : RuntimeBuilder {
}

private fun buildExtrinsic(
struct: EncodableStruct<ExtrinsicMetadataV14>,
struct: EncodableStruct<PostV14ExtrinsicMetadataSchema<*>>,
typeRegistry: TypeRegistry,
) = ExtrinsicMetadata(
version = struct[ExtrinsicMetadataV14.version].toInt().toBigInteger(),
signedExtensions = struct[ExtrinsicMetadataV14.signedExtensions].map {
SignedExtensionMetadata(
id = it[SignedExtensionMetadataV14.identifier],
type = typeRegistry[it[SignedExtensionMetadataV14.type].toString()],
additionalSigned = typeRegistry[it[SignedExtensionMetadataV14.additionalSigned].toString()]
)
}
)
): ExtrinsicMetadata {
val schema = struct.schema

return ExtrinsicMetadata(
version = struct[schema.version].toInt().toBigInteger(),
signedExtensions = struct[schema.signedExtensions].map {
SignedExtensionMetadata(
id = it[SignedExtensionMetadataV14.identifier],
type = typeRegistry[it[SignedExtensionMetadataV14.type].toString()],
additionalSigned = typeRegistry[it[SignedExtensionMetadataV14.additionalSigned].toString()]
)
}
)
}

private fun cannotConstructStorageEntry(from: Any?): Nothing {
throw IllegalArgumentException("Cannot construct StorageEntryType from $from")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object VersionedRuntimeBuilder : RuntimeBuilder {
knownSignedExtensions: List<SignedExtensionMetadata>,
): RuntimeMetadata {
return when (reader.metadataVersion) {
14 -> V14RuntimeBuilder.buildMetadata(reader, typeRegistry, knownSignedExtensions)
14, 15 -> PostV14RuntimeBuilder.buildMetadata(reader, typeRegistry, knownSignedExtensions)
else -> V13RuntimeBuilder.buildMetadata(reader, typeRegistry, knownSignedExtensions)
}
}
Expand Down

This file was deleted.

Loading
Loading