Skip to content

Commit

Permalink
fix: DPP 0.24 encode protocol as VarInt (#37)
Browse files Browse the repository at this point in the history
* chore: update to dashj 19.0-SNAPSHOT

* fix: use VarInt for protocol version encoding/decoding

* chore: update dashjbls shared library to v1.0.0 (linux)

* style: update format and remove commented code
  • Loading branch information
HashEngineering authored Jun 6, 2023
1 parent bceba12 commit 5ca581f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext.kotlin_version = '1.4.32'
ext.dashj_version = '0.18.3-CJ-SNAPSHOT'
ext.dashj_version = '19.0-SNAPSHOT'
ext.dpp_version = '0.24-SNAPSHOT'
repositories {
mavenCentral()
Expand Down
Binary file modified src/main/jniLibs/libdashjbls.so
Binary file not shown.
10 changes: 6 additions & 4 deletions src/main/kotlin/org/dashj/platform/dpp/BaseObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package org.dashj.platform.dpp

import org.bitcoinj.core.Sha256Hash
import org.bitcoinj.core.Utils
import org.bitcoinj.core.VarInt
import org.dashj.platform.dpp.util.Cbor

/**
Expand Down Expand Up @@ -41,9 +41,11 @@ abstract class BaseObject(var protocolVersion: Int = ProtocolVersion.latestVersi

fun encodeProtocolEntity(payload: Map<String, Any?>): ByteArray {
val encoded = Cbor.encode(payload)
val buffer = ByteArray(encoded.size + ProtocolVersion.SIZE)
Utils.uint32ToByteArrayLE(protocolVersion.toLong(), buffer, 0)
System.arraycopy(encoded, 0, buffer, ProtocolVersion.SIZE, encoded.size)
val protocolVersionCompact = VarInt(protocolVersion.toLong())
val buffer = ByteArray(encoded.size + protocolVersionCompact.sizeInBytes)

System.arraycopy(protocolVersionCompact.encode(), 0, buffer, 0, protocolVersionCompact.sizeInBytes)
System.arraycopy(encoded, 0, buffer, protocolVersionCompact.sizeInBytes, encoded.size)
return buffer
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/org/dashj/platform/dpp/Factory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package org.dashj.platform.dpp

import org.bitcoinj.core.Utils
import org.bitcoinj.core.VarInt
import org.dashj.platform.dpp.util.Cbor

open class Factory(val dpp: DashPlatformProtocol, val stateRepository: StateRepository) {
Expand All @@ -19,10 +19,11 @@ open class Factory(val dpp: DashPlatformProtocol, val stateRepository: StateRepo
companion object {
@JvmStatic
fun decodeProtocolEntity(buffer: ByteArray): Pair<Int, MutableMap<String, Any?>> {
val protocolVersion = Utils.readUint32(buffer, 0).toInt()
val rawEntity = buffer.copyOfRange(ProtocolVersion.SIZE, buffer.size)
val protocolVersionCompact = VarInt(buffer, 0)
val protocolVersion = protocolVersionCompact.value
val rawEntity = buffer.copyOfRange(protocolVersionCompact.sizeInBytes, buffer.size)
val rawObject = Cbor.decode(rawEntity)
return Pair(protocolVersion, rawObject.toMutableMap())
return Pair(protocolVersion.toInt(), rawObject.toMutableMap())
}
}
}

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions src/test/kotlin/org/dashj/platform/dpp/identity/IdentitySpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package org.dashj.platform.dpp.identity

import org.bitcoinj.core.Sha256Hash
import org.bitcoinj.core.Utils
import org.bitcoinj.core.VarInt
import org.dashj.platform.dpp.Metadata
import org.dashj.platform.dpp.ProtocolVersion
import org.dashj.platform.dpp.toBase64
Expand Down Expand Up @@ -98,9 +98,11 @@ class IdentitySpec {
val identityDataToEncode = identity.toObject().toMutableMap()
identityDataToEncode.remove("protocolVersion")
val encoded = Cbor.encode(identityDataToEncode)
val buffer = ByteArray(encoded.size + ProtocolVersion.SIZE)
Utils.uint32ToByteArrayLE(identity.protocolVersion.toLong(), buffer, 0)
System.arraycopy(encoded, 0, buffer, ProtocolVersion.SIZE, encoded.size)

val protocolVersionCompact = VarInt(identity.protocolVersion.toLong())
val buffer = ByteArray(encoded.size + protocolVersionCompact.sizeInBytes)
System.arraycopy(protocolVersionCompact.encode(), 0, buffer, 0, protocolVersionCompact.sizeInBytes)
System.arraycopy(encoded, 0, buffer, protocolVersionCompact.sizeInBytes, encoded.size)

assertArrayEquals(result, buffer)
}
Expand All @@ -112,10 +114,11 @@ class IdentitySpec {
val identityDataToEncode = identity.toObject().toMutableMap()
identityDataToEncode.remove("protocolVersion")
val encoded = Cbor.encode(identityDataToEncode)
val buffer = ByteArray(encoded.size + ProtocolVersion.SIZE)
Utils.uint32ToByteArrayLE(identity.protocolVersion.toLong(), buffer, 0)
System.arraycopy(encoded, 0, buffer, ProtocolVersion.SIZE, encoded.size)

val protocolVersionCompact = VarInt(identity.protocolVersion.toLong())
val buffer = ByteArray(encoded.size + protocolVersionCompact.sizeInBytes)
System.arraycopy(protocolVersionCompact.encode(), 0, buffer, 0, protocolVersionCompact.sizeInBytes)
System.arraycopy(encoded, 0, buffer, protocolVersionCompact.sizeInBytes, encoded.size)
assertArrayEquals(result, Sha256Hash.hashTwice(buffer))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ class IdentityTest {

val hash = stateTransition.hash()

assertEquals("60efcd3cdc3676ce9221f4e66435a2a4dce6d4d8e9bfe2817e073aa13bfdcf34", hash.toHex())
assertEquals("bdf9cee9cd70f527e0b73ee0b4e7b00b090d4861266bf38e29273e51ce8760e0", hash.toHex())
assertEquals(
"01000000a3647479706500697369676e6174757265f6747369676e61747572655075626c69634b65794964f6",
"01a3647479706500697369676e6174757265f6747369676e61747572655075626c69634b65794964f6",
serializedDataBytes.toHex()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ class StateTransitionIdentitySignedSpec {
fun `#hash should return serialized hash`() {
val hash = stateTransition.hash()

assertEquals("60efcd3cdc3676ce9221f4e66435a2a4dce6d4d8e9bfe2817e073aa13bfdcf34", hash.toHex())
assertEquals("bdf9cee9cd70f527e0b73ee0b4e7b00b090d4861266bf38e29273e51ce8760e0", hash.toHex())
}

@Test
fun `#toBuffer should return serialized data`() {
val serializedData = stateTransition.toBuffer()
assertEquals(
"01000000a3647479706500697369676e6174757265f6747369676e61747572655075626c69634b65794964f6",
"01a3647479706500697369676e6174757265f6747369676e61747572655075626c69634b65794964f6",
serializedData.toHex()
)
}

@Test
fun `#toBuffer should return serialized data without signature data`() {
val serializedData = stateTransition.toBuffer(true)
assertEquals("01000000a1647479706500", serializedData.toHex())
assertEquals("01a1647479706500", serializedData.toHex())
}

@Test
Expand Down

0 comments on commit 5ca581f

Please sign in to comment.