From b3c77451bfb7486a241273d5a37e58c0f529dd06 Mon Sep 17 00:00:00 2001 From: Bogdan Mingela Date: Fri, 26 Jan 2024 09:27:58 +0300 Subject: [PATCH] Fixed RegistrableBox metadata merging for test genesis blocks (#403) Signed-off-by: Mingela --- .../kotlin/jp/co/soramitsu/iroha2/Genesis.kt | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Genesis.kt b/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Genesis.kt index 2370c1f5..5976712d 100644 --- a/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Genesis.kt +++ b/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Genesis.kt @@ -4,6 +4,7 @@ import jp.co.soramitsu.iroha2.generated.ExecutorMode import jp.co.soramitsu.iroha2.generated.Expression import jp.co.soramitsu.iroha2.generated.IdentifiableBox import jp.co.soramitsu.iroha2.generated.InstructionExpr +import jp.co.soramitsu.iroha2.generated.InstructionExpr.Register import jp.co.soramitsu.iroha2.generated.Metadata import jp.co.soramitsu.iroha2.generated.NewAccount import jp.co.soramitsu.iroha2.generated.NewAssetDefinition @@ -59,19 +60,20 @@ open class Genesis(open val block: RawGenesisBlock) { } private fun MutableSet.mergeMetadata(): List { + // entity id to its metadata val metadataMap = mutableMapOf() // only for InstructionExpr.Register this.extractIdentifiableBoxes().forEach { idBox -> metadataMap.putMergedMetadata(idBox) } - this.findIsiToReplace(metadataMap).forEach { (metadata, toReplace) -> + this.findIsiToReplace(metadataMap).forEach { (idToMetadata, toReplace) -> toReplace.forEach { isi -> this.remove(isi) } val idBox = toReplace.first().extractIdentifiableBox() - val registrableBox = idBox?.toRegisterBox(metadata) + val registrableBox = idBox?.toRegisterBox(idToMetadata.second) ?: throw RuntimeException("IdentifiableBox shouldn't be null") - this.add(InstructionExpr.Register(RegisterExpr(registrableBox.evaluatesTo()))) + this.add(Register(RegisterExpr(registrableBox.evaluatesTo()))) } return this.sorted() @@ -80,7 +82,7 @@ open class Genesis(open val block: RawGenesisBlock) { private fun MutableSet.sorted() = this.sortedWith( compareByDescending { instruction -> when (instruction) { - is InstructionExpr.Register -> when (instruction.extractIdentifiableBox()) { + is Register -> when (instruction.extractIdentifiableBox()) { is IdentifiableBox.NewDomain -> 5 is IdentifiableBox.NewAccount -> 4 is IdentifiableBox.NewAssetDefinition -> 3 @@ -144,26 +146,37 @@ open class Genesis(open val block: RawGenesisBlock) { private fun MutableSet.findIsiToReplace( metadata: Map, - ): MutableMap> { - val isiToReplace = mutableMapOf>() + ): MutableMap, MutableList> { + val isiToReplace = + mutableMapOf, MutableList>() this.forEach { instruction -> runCatching { - instruction.cast() + instruction.cast() .registerExpr.`object`.expression .cast().value .cast().identifiableBox }.onSuccess { idBox -> when (idBox) { - is IdentifiableBox.NewAccount -> metadata[idBox.newAccount.id] - is IdentifiableBox.NewDomain -> metadata[idBox.newDomain.id] - is IdentifiableBox.NewAssetDefinition -> metadata[idBox.newAssetDefinition.id] + is IdentifiableBox.NewAccount -> { + val id = idBox.newAccount.id + id to metadata[id]!! + } + is IdentifiableBox.NewDomain -> { + val id = idBox.newDomain.id + id to metadata[id]!! + } + is IdentifiableBox.NewAssetDefinition -> { + val id = idBox.newAssetDefinition.id + id to metadata[id]!! + } else -> null - }?.takeIf { it.map.isNotEmpty() }?.let { metadata -> - isiToReplace.merge(metadata, mutableListOf(instruction.cast())) { old, new -> - old.plus(new).toMutableList() + }?.takeIf { it.second.map.isNotEmpty() } + ?.let { + isiToReplace.merge(it, mutableListOf(instruction.cast())) { old, new -> + old.plus(new).toMutableList() + } } - } } } return isiToReplace