diff --git a/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Serde.kt b/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Serde.kt index 9470b490..a6647f90 100644 --- a/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Serde.kt +++ b/modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Serde.kt @@ -38,6 +38,7 @@ import jp.co.soramitsu.iroha2.generated.ExecuteTriggerEventFilter import jp.co.soramitsu.iroha2.generated.ExecutionTime import jp.co.soramitsu.iroha2.generated.ExecutorMode import jp.co.soramitsu.iroha2.generated.Expression +import jp.co.soramitsu.iroha2.generated.FilterOptOfAccountId import jp.co.soramitsu.iroha2.generated.Fixed import jp.co.soramitsu.iroha2.generated.GrantExpr import jp.co.soramitsu.iroha2.generated.Hash @@ -93,7 +94,7 @@ import kotlin.reflect.full.memberProperties import kotlin.reflect.full.primaryConstructor /** - * This JSON mapper is configured to serialise and deserialise `Genesis block` in a format compatible with Iroha 2 peer + * This JSON mapper is configured to serialise and deserialize `Genesis block` in a format compatible with Iroha 2 peer */ public val JSON_SERDE by lazy { ObjectMapper().also { mapper -> @@ -126,6 +127,7 @@ public val JSON_SERDE by lazy { module.addDeserializer(StringWithJson::class.java, StringWithJsonDeserializer) module.addDeserializer(TriggerId::class.java, TriggerIdDeserializer) module.addDeserializer(TriggerOfTriggeringFilterBox::class.java, TriggerOfTriggeringFilterBoxDeserializer) + module.addDeserializer(FilterOptOfAccountId::class.java, FilterOptOfAccountIdDeserializer) module.addKeyDeserializer(AssetDefinitionId::class.java, AssetDefinitionIdKeyDeserializer) module.addKeyDeserializer(AccountId::class.java, AccountIdKeyDeserializer) module.addKeyDeserializer(AssetId::class.java, AssetIdKeyDeserializer) @@ -157,6 +159,7 @@ public val JSON_SERDE by lazy { module.addSerializer(StringWithJson::class.java, StringWithJsonSerializer) module.addSerializer(TimeEventFilter::class.java, TimeEventFilterSerializer) module.addSerializer(Schedule::class.java, ScheduleSerializer) + module.addSerializer(FilterOptOfAccountId::class.java, FilterOptOfAccountIdSerializer) mapper.registerModule(module) mapper.registerModule( @@ -165,7 +168,6 @@ public val JSON_SERDE by lazy { .configure(KotlinFeature.NullToEmptyMap, true) .build(), ) - mapper.propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE mapper.enable(SerializationFeature.INDENT_OUTPUT) } @@ -260,6 +262,16 @@ object StringWithJsonDeserializer : JsonDeserializer() { } } +object FilterOptOfAccountIdDeserializer : JsonDeserializer() { + override fun deserialize(p: JsonParser, ctxt: DeserializationContext): FilterOptOfAccountId { + val node = p.readValueAsTree() + return when (val text = node.textValue()) { + FilterOptOfAccountId.AcceptAll::class.simpleName -> FilterOptOfAccountId.AcceptAll() + else -> FilterOptOfAccountId.BySome(text.asAccountId()) + } + } +} + object TriggerOfTriggeringFilterBoxDeserializer : JsonDeserializer() { override fun deserialize(p: JsonParser, ctxt: DeserializationContext): TriggerOfTriggeringFilterBox { val node = p.readValueAsTree() @@ -643,6 +655,15 @@ object ScheduleSerializer : JsonSerializer() { } } +/** + * Serializer for [FilterOptOfAccountId] + */ +object FilterOptOfAccountIdSerializer : JsonSerializer() { + override fun serialize(value: FilterOptOfAccountId, gen: JsonGenerator, serializers: SerializerProvider) { + gen.writeString(value.asString()) + } +} + /** * Serializer for [Name] as key */ diff --git a/modules/block/src/test/kotlin/jp/co/soramitsu/iroha2/DeserializerTest.kt b/modules/block/src/test/kotlin/jp/co/soramitsu/iroha2/DeserializerTest.kt index 723fb3d8..0b49320c 100644 --- a/modules/block/src/test/kotlin/jp/co/soramitsu/iroha2/DeserializerTest.kt +++ b/modules/block/src/test/kotlin/jp/co/soramitsu/iroha2/DeserializerTest.kt @@ -1,12 +1,10 @@ package jp.co.soramitsu.iroha2 import jp.co.soramitsu.iroha2.generated.RawGenesisBlock -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import java.io.File import kotlin.test.assertEquals -@Disabled class DeserializerTest { @Test fun `should deserialize genesis block`() { @@ -15,14 +13,14 @@ class DeserializerTest { val block = JSON_SERDE.convertValue(node, RawGenesisBlock::class.java) assert(block.transactions.isNotEmpty()) - // genesis.json has 10 instructions ("isi") + // genesis.json has 12 instructions ("isi") // Register -> NewDomain // Register -> NewAccount (2) // Register -> NewAssetDefinition // Grant -> PermissionToken (2) // Mint -> AssetId (2) - // Register -> Trigger (2) - assert(block.transactions.flatten().size == 10) + // Register -> Trigger (4) + assert(block.transactions.flatten().size == 12) val genesis = Genesis(block) val newJson = removeWhiteSpaceAndReplacePubKey(genesis.asJson()) diff --git a/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/Extensions.kt b/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/Extensions.kt index 0d8dd148..f7105071 100644 --- a/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/Extensions.kt +++ b/modules/client/src/main/kotlin/jp/co/soramitsu/iroha2/Extensions.kt @@ -16,6 +16,7 @@ import jp.co.soramitsu.iroha2.generated.Executable import jp.co.soramitsu.iroha2.generated.ExecutionTime import jp.co.soramitsu.iroha2.generated.Expression import jp.co.soramitsu.iroha2.generated.FilterBox +import jp.co.soramitsu.iroha2.generated.FilterOptOfAccountId import jp.co.soramitsu.iroha2.generated.FindError import jp.co.soramitsu.iroha2.generated.Fixed import jp.co.soramitsu.iroha2.generated.Hash @@ -655,3 +656,13 @@ fun AssetId.asStringWithJson() = this.asJsonString().asStringWithJson() fun AccountId.asStringWithJson() = this.asJsonString().asStringWithJson() fun RoleId.asStringWithJson() = this.asJsonString().asStringWithJson() + +fun FilterOptOfAccountId.asString() = when (this) { + is FilterOptOfAccountId.BySome -> this.accountId.asString() + is FilterOptOfAccountId.AcceptAll -> this::class.simpleName +} + +fun FilterOptOfAccountId.asAccountIdOrNull() = when (this) { + is FilterOptOfAccountId.BySome -> this.accountId + is FilterOptOfAccountId.AcceptAll -> null +} diff --git a/modules/test-tools/src/main/resources/.cleanup.wip-7 b/modules/test-tools/src/main/resources/.cleanup.wip-7 deleted file mode 100644 index e69de29b..00000000 diff --git a/modules/test-tools/src/main/resources/genesis.json b/modules/test-tools/src/main/resources/genesis.json index a4998668..48e26027 100644 --- a/modules/test-tools/src/main/resources/genesis.json +++ b/modules/test-tools/src/main/resources/genesis.json @@ -122,6 +122,39 @@ } } }, + { + "Register" : { + "Trigger" : { + "id" : "name2$wonderland", + "action" : { + "executable" : { + "Instructions" : [ { + "SetKeyValue" : { + "DomainId" : "wonderland", + "key" : { + "Name" : "key" + }, + "value" : { + "String" : "value" + } + } + } ] + }, + "repeats" : { + "Exactly" : 1 + }, + "authority" : "alice@wonderland", + "filter": { + "ExecuteTrigger": { + "trigger_id": "name2$wonderland", + "authority": "AcceptAll" + } + }, + "metadata" : { } + } + } + } + }, { "Register" : { "Trigger" : {