-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from novasamatech/develop
v1.10.0
- Loading branch information
Showing
13 changed files
with
319 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...java/jp/co/soramitsu/fearless_utils/runtime/definitions/types/instances/ExtrinsicTypes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package jp.co.soramitsu.fearless_utils.runtime.definitions.types.instances | ||
|
||
object ExtrinsicTypes { | ||
|
||
const val ADDRESS = "Address" | ||
const val SIGNATURE = "ExtrinsicSignature" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...itsu/fearless_utils/runtime/definitions/v14/typeMapping/AddExtrinsicTypesSiTypeMapping.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jp.co.soramitsu.fearless_utils.runtime.definitions.v14.typeMapping | ||
|
||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.TypePresetBuilder | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.alias | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.Type | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.instances.ExtrinsicTypes | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.PortableType | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.paramType | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.type | ||
import jp.co.soramitsu.fearless_utils.scale.EncodableStruct | ||
|
||
private const val UNCHECKED_EXTRINSIC_TYPE = "sp_runtime.generic.unchecked_extrinsic.UncheckedExtrinsic" | ||
|
||
class AddExtrinsicTypesSiTypeMapping : SiTypeMapping { | ||
|
||
override fun map( | ||
originalDefinition: EncodableStruct<PortableType>, | ||
suggestedTypeName: String, | ||
typesBuilder: TypePresetBuilder | ||
): Type<*>? { | ||
if (suggestedTypeName == UNCHECKED_EXTRINSIC_TYPE) { | ||
addTypeFromTypeParams( | ||
originalDefinition = originalDefinition, | ||
typesBuilder = typesBuilder, | ||
typeParamName = "Address", | ||
newTypeName = ExtrinsicTypes.ADDRESS | ||
) | ||
|
||
addTypeFromTypeParams( | ||
originalDefinition = originalDefinition, | ||
typesBuilder = typesBuilder, | ||
typeParamName = "Signature", | ||
newTypeName = ExtrinsicTypes.SIGNATURE | ||
) | ||
} | ||
|
||
// we don't modify any existing type | ||
return null | ||
} | ||
|
||
private fun addTypeFromTypeParams( | ||
originalDefinition: EncodableStruct<PortableType>, | ||
typesBuilder: TypePresetBuilder, | ||
typeParamName: String, | ||
newTypeName: String | ||
) { | ||
val paramType = originalDefinition.type.paramType(typeParamName) ?: return | ||
|
||
// type with type-id name is present in the registry as alias to fully qualified name | ||
typesBuilder.alias(newTypeName, paramType.toString()) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...fearless_utils/runtime/definitions/v14/typeMapping/AddRuntimeDispatchInfoSiTypeMapping.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package jp.co.soramitsu.fearless_utils.runtime.definitions.v14.typeMapping | ||
|
||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.TypePresetBuilder | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.getOrCreate | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.type | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.Type | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.TypeReference | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.composite.Struct | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.PortableType | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.TypeDefComposite | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.asCompositeOrNull | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.fieldType | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.type | ||
import jp.co.soramitsu.fearless_utils.scale.EncodableStruct | ||
|
||
private const val DISPATCH_INFO_TYPE = "frame_support.dispatch.DispatchInfo" | ||
|
||
class AddRuntimeDispatchInfoSiTypeMapping : SiTypeMapping { | ||
|
||
override fun map( | ||
originalDefinition: EncodableStruct<PortableType>, | ||
suggestedTypeName: String, | ||
typesBuilder: TypePresetBuilder | ||
): Type<*>? { | ||
if (suggestedTypeName == DISPATCH_INFO_TYPE) { | ||
addRuntimeDispatchInfo(originalDefinition, typesBuilder) | ||
} | ||
|
||
// we don't modify any existing type | ||
return null | ||
} | ||
|
||
private fun addRuntimeDispatchInfo( | ||
dispatchInfoType: EncodableStruct<PortableType>, | ||
typesBuilder: TypePresetBuilder | ||
) { | ||
val typeDef = dispatchInfoType.type.asCompositeOrNull() ?: return | ||
|
||
val weightType = typeDef.fieldTypeReference("weight", typesBuilder) ?: return | ||
val dispatchClassType = typeDef.fieldTypeReference("class", typesBuilder) ?: return | ||
val partialFeeType = typesBuilder.getOrCreate("u128") | ||
|
||
val runtimeDispatchInfo = RuntimeDispatchInfo(weightType, dispatchClassType, partialFeeType) | ||
|
||
typesBuilder.type(runtimeDispatchInfo) | ||
} | ||
|
||
private fun EncodableStruct<TypeDefComposite>.fieldTypeReference( | ||
name: String, | ||
typesBuilder: TypePresetBuilder | ||
): TypeReference? { | ||
val fieldType = fieldType(name) ?: return null | ||
|
||
return typesBuilder.getOrCreate(fieldType.toString()) | ||
} | ||
} | ||
|
||
fun RuntimeDispatchInfo( | ||
weightType: TypeReference, | ||
classType: TypeReference, | ||
partialFeeType: TypeReference | ||
) = Struct( | ||
name = "RuntimeDispatchInfo", | ||
mapping = linkedMapOf( | ||
"weight" to weightType, | ||
"class" to classType, | ||
"partialFee" to partialFeeType | ||
) | ||
) |
110 changes: 110 additions & 0 deletions
110
...p/co/soramitsu/fearless_utils/runtime/definitions/v14/typeMapping/PathMatchTypeMapping.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package jp.co.soramitsu.fearless_utils.runtime.definitions.v14.typeMapping | ||
|
||
import jp.co.soramitsu.fearless_utils.extensions.tryFindNonNull | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.TypePresetBuilder | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.registry.getOrCreate | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.RuntimeType | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.Type | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.types.composite.aliasedAs | ||
import jp.co.soramitsu.fearless_utils.runtime.definitions.v14.typeMapping.PathMatchTypeMapping.Matcher | ||
import jp.co.soramitsu.fearless_utils.runtime.metadata.v14.PortableType | ||
import jp.co.soramitsu.fearless_utils.scale.EncodableStruct | ||
|
||
typealias PathMatchTypeMappingReplacement<T> = Pair<T, PathMatchTypeMapping.Replacement> | ||
|
||
class PathMatchTypeMapping( | ||
private val replacements: List<PathMatchTypeMappingReplacement<Matcher>>, | ||
) : SiTypeMapping { | ||
|
||
sealed class Replacement { | ||
|
||
abstract fun create(suggestedName: String, types: TypePresetBuilder): RuntimeType<*, *> | ||
|
||
class AliasTo(private val aliasedName: String) : Replacement() { | ||
|
||
override fun create( | ||
suggestedName: String, | ||
types: TypePresetBuilder | ||
): RuntimeType<*, *> { | ||
return types.getOrCreate(aliasedName).aliasedAs(suggestedName) | ||
} | ||
} | ||
|
||
class CreateType( | ||
private val createExpr: (suggestedName: String, types: TypePresetBuilder) -> RuntimeType<*, *> | ||
) : Replacement() { | ||
|
||
override fun create( | ||
suggestedName: String, | ||
types: TypePresetBuilder | ||
): RuntimeType<*, *> { | ||
return createExpr(suggestedName, types) | ||
} | ||
} | ||
} | ||
|
||
sealed class Matcher { | ||
|
||
abstract fun match(fullPathName: String): Boolean | ||
|
||
class Exact(val value: String) : Matcher() { | ||
override fun match(fullPathName: String): Boolean { | ||
return value == fullPathName | ||
} | ||
} | ||
|
||
class PrefixMatch(val prefix: String) : Matcher() { | ||
override fun match(fullPathName: String): Boolean { | ||
return fullPathName.startsWith(prefix) | ||
} | ||
} | ||
|
||
class SuffixMatch(val suffix: String) : Matcher() { | ||
override fun match(fullPathName: String): Boolean { | ||
return fullPathName.endsWith(suffix) | ||
} | ||
} | ||
} | ||
|
||
override fun map( | ||
originalDefinition: EncodableStruct<PortableType>, | ||
suggestedTypeName: String, | ||
typesBuilder: TypePresetBuilder | ||
): Type<*>? { | ||
return replacements.tryFindNonNull { (matcher, replacement) -> | ||
if (matcher.match(suggestedTypeName)) { | ||
replacement.create(suggestedTypeName, typesBuilder) | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Can be used to pass wildcard strings instead of [Matcher] instances | ||
* | ||
* "full.name" -> Matcher.Exact("full.name") | ||
* "*suffix.name" -> Matcher.Suffix("suffix.name") | ||
* "prefix.name*" -> Matcher.Prefix("prefix.name) | ||
* | ||
* Wildcards are only considered at the beginning or at the end of the string, | ||
* others will be considered as a part of the content | ||
*/ | ||
fun PathMatchTypeMapping( | ||
vararg replacements: PathMatchTypeMappingReplacement<String> | ||
): PathMatchTypeMapping { | ||
val replacementMatchers = replacements.map { (wildCardString, typeCreator) -> | ||
Matcher(wildCardString) to typeCreator | ||
} | ||
|
||
return PathMatchTypeMapping(replacementMatchers) | ||
} | ||
|
||
private fun Matcher(wildCardString: String): Matcher { | ||
return when { | ||
wildCardString.startsWith("*") -> Matcher.SuffixMatch(wildCardString.drop(1)) | ||
wildCardString.endsWith("*") -> Matcher.PrefixMatch(wildCardString.dropLast(1)) | ||
else -> Matcher.Exact(wildCardString) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.