Skip to content

Commit

Permalink
Merge pull request #60 from DECENTfoundation/fix/enums-idx-exception
Browse files Browse the repository at this point in the history
fix upper bound for enum values
  • Loading branch information
Marian Vanderka authored May 31, 2019
2 parents 100a7ad + a00d711 commit c9e0c0b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
7 changes: 5 additions & 2 deletions library/src/main/java/ch/decent/sdk/model/FeeSchedule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ch.decent.sdk.model
import com.google.gson.annotations.SerializedName

data class FeeSchedule(
@SerializedName("parameters") val parameters: Map<OperationType, FeeParameter>,
/**
* operation types int keys
*/
@SerializedName("parameters") val parameters: Map<Int, FeeParameter>,
@SerializedName("scale") val scale: Long
)
)
9 changes: 5 additions & 4 deletions library/src/main/java/ch/decent/sdk/model/ObjectType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ enum class ObjectType {
SUBSCRIPTION_OBJECT, //15
SEEDING_STATISTICS_OBJECT,
TRANSACTION_DETAIL_OBJECT,
MESSAGING_OBJECT;
MESSAGING_OBJECT,
UNKNOWN_OBJECT;

val space: Byte
get() = if (ordinal < 10) 1 else 2
get() = if (ordinal < GLOBAL_PROPERTY_OBJECT.ordinal) 1 else 2

val type: Byte
get() = (ordinal - (space - 1) * 10).toByte()
get() = (ordinal - (space - 1) * GLOBAL_PROPERTY_OBJECT.ordinal).toByte()

/**
* This method is used to return the generic object type in the form space.type.0.
Expand All @@ -58,6 +59,6 @@ enum class ObjectType {
get() = ChainObject(this)

companion object {
fun fromSpaceType(space: Int, type: Int) = ObjectType.values()[max(space - 1, 0) * 10 + type]
fun fromSpaceType(space: Int, type: Int) = values().getOrElse(max(space - 1, 0) * GLOBAL_PROPERTY_OBJECT.ordinal + type) { UNKNOWN_OBJECT }
}
}
9 changes: 2 additions & 7 deletions library/src/main/java/ch/decent/sdk/model/OperationType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,5 @@ enum class OperationType(val clazz: Class<*>? = null) {
READY_TO_PUBLISH2_OPERATION,
TRANSFER2_OPERATION(TransferOperation::class.java),
UPDATE_USER_ISSUED_ASSET_ADVANCED,
DISALLOW_AUTOMATIC_RENEWAL_OF_SUBSCRIPTION_OPERATION, // VIRTUAL 41
RETURN_ESCROW_SUBMISSION_OPERATION, // VIRTUAL
RETURN_ESCROW_BUYING_OPERATION, // VIRTUAL
PAY_SEEDER_OPERATION, // VIRTUAL
FINISH_BUYING_OPERATION, // VIRTUAL 45
RENEWAL_OF_SUBSCRIPTION_OPERATION // VIRTUAL
}
UNKNOWN_OPERATION
}
17 changes: 15 additions & 2 deletions library/src/main/java/ch/decent/sdk/model/Operations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package ch.decent.sdk.model

import ch.decent.sdk.crypto.Address
import ch.decent.sdk.crypto.Credentials
import ch.decent.sdk.net.serialization.*
import ch.decent.sdk.net.serialization.ByteSerializable
import ch.decent.sdk.net.serialization.Varint
import ch.decent.sdk.net.serialization.VoteId
import ch.decent.sdk.net.serialization.bytes
import ch.decent.sdk.net.serialization.optionalBytes
import ch.decent.sdk.utils.hex
import ch.decent.sdk.utils.publicElGamal
import ch.decent.sdk.utils.unhex
Expand Down Expand Up @@ -38,6 +42,15 @@ sealed class BaseOperation(
override fun hashCode(): Int = Arrays.hashCode(bytes)
}

class UnknownOperation(val id: Int) : BaseOperation(OperationType.UNKNOWN_OPERATION) {
override val bytes: ByteArray
get() = byteArrayOf()

override fun toString(): String {
return "UnknownOperation(id=$id)"
}
}

class EmptyOperation(type: OperationType) : BaseOperation(type) {
override val bytes: ByteArray
get() = byteArrayOf(0)
Expand Down Expand Up @@ -351,4 +364,4 @@ class SendMessageOperation constructor(
messagePayloadJson: String,
payer: ChainObject,
requiredAuths: List<ChainObject> = listOf(payer)
) : CustomOperation(CustomOperationType.MESSAGING, payer, requiredAuths, messagePayloadJson.toByteArray().hex())
) : CustomOperation(CustomOperationType.MESSAGING, payer, requiredAuths, messagePayloadJson.toByteArray().hex())
10 changes: 6 additions & 4 deletions library/src/main/java/ch/decent/sdk/model/TypeAdapters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ object OperationTypeFactory : TypeAdapterFactory {

override fun read(reader: JsonReader): T? {
val el = Streams.parse(reader)
val op = OperationType.values()[el.asJsonArray[0].asInt]
val idx = el.asJsonArray[0].asInt
val op = OperationType.values().getOrElse(idx) { OperationType.UNKNOWN_OPERATION }
val obj = el.asJsonArray[1].asJsonObject
return op.clazz?.let {
return if (op == OperationType.UNKNOWN_OPERATION) UnknownOperation(idx) as T?
else op.clazz?.let {
val delegate = gson.getDelegateAdapter(this@OperationTypeFactory, TypeToken.get(it))
(delegate.fromJsonTree(obj) as BaseOperation).apply { this.type = op } as T?
} ?: EmptyOperation(op) as T?
Expand Down Expand Up @@ -178,5 +180,5 @@ object OperationTypeAdapter : TypeAdapter<OperationType>() {
out.value(value.ordinal)
}

override fun read(reader: JsonReader): OperationType = OperationType.values()[reader.nextInt()]
}
override fun read(reader: JsonReader): OperationType = OperationType.values().getOrElse(reader.nextInt()) { OperationType.UNKNOWN_OPERATION }
}

0 comments on commit c9e0c0b

Please sign in to comment.