Skip to content

Commit

Permalink
deserializers fix (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
arndey authored Jun 25, 2024
1 parent 541e68b commit 558784f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
25 changes: 23 additions & 2 deletions modules/block/src/main/kotlin/jp/co/soramitsu/iroha2/Serde.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down Expand Up @@ -260,6 +262,16 @@ object StringWithJsonDeserializer : JsonDeserializer<StringWithJson>() {
}
}

object FilterOptOfAccountIdDeserializer : JsonDeserializer<FilterOptOfAccountId>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): FilterOptOfAccountId {
val node = p.readValueAsTree<JsonNode>()
return when (val text = node.textValue()) {
FilterOptOfAccountId.AcceptAll::class.simpleName -> FilterOptOfAccountId.AcceptAll()
else -> FilterOptOfAccountId.BySome(text.asAccountId())
}
}
}

object TriggerOfTriggeringFilterBoxDeserializer : JsonDeserializer<TriggerOfTriggeringFilterBox>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): TriggerOfTriggeringFilterBox {
val node = p.readValueAsTree<JsonNode>()
Expand Down Expand Up @@ -643,6 +655,15 @@ object ScheduleSerializer : JsonSerializer<Schedule>() {
}
}

/**
* Serializer for [FilterOptOfAccountId]
*/
object FilterOptOfAccountIdSerializer : JsonSerializer<FilterOptOfAccountId>() {
override fun serialize(value: FilterOptOfAccountId, gen: JsonGenerator, serializers: SerializerProvider) {
gen.writeString(value.asString())
}
}

/**
* Serializer for [Name] as key
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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`() {
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Empty file.
33 changes: 33 additions & 0 deletions modules/test-tools/src/main/resources/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down

0 comments on commit 558784f

Please sign in to comment.