Skip to content

Commit

Permalink
Feat/new encryption keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamol authored and godenzim committed Jun 25, 2024
1 parent 54f68a9 commit ab1048b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 26 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Project.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object Project {
const val group = "com.github.acurast"
const val version = "0.1.21"
const val version = "0.1.22-beta01"
}
22 changes: 21 additions & 1 deletion codec/src/main/kotlin/acurast/codec/extrinsic/marketplace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ public sealed interface PublicKey: ToU8a {
Tag.Secp256r1 -> Secp256r1(value)
Tag.Secp256k1 -> Secp256k1(value)
Tag.Ed25519 -> Ed25519(value)
Tag.Secp256r1Encryption -> Secp256r1Encryption(value)
Tag.Secp256k1Encryption -> Secp256k1Encryption(value)
}
}
}

public enum class Tag(public val id: Byte) : ToU8a {
Secp256r1(0),
Secp256k1(1),
Ed25519(2);
Ed25519(2),
Secp256r1Encryption(3),
Secp256k1Encryption(4);

override fun toU8a(): ByteArray = id.toU8a()

Expand Down Expand Up @@ -81,6 +85,22 @@ public sealed interface PublicKey: ToU8a {

override fun toU8a(): ByteArray = tag.toU8a() + bytes.toU8a()
}

public data class Secp256r1Encryption(override val bytes: ByteArray) : PublicKey {
public companion object : Kind {
override val tag: Tag = Tag.Secp256r1Encryption
}

override fun toU8a(): ByteArray = tag.toU8a() + bytes.toU8a()
}

public data class Secp256k1Encryption(override val bytes: ByteArray): PublicKey {
public companion object : Kind {
override val tag: Tag = Tag.Secp256k1Encryption
}

override fun toU8a(): ByteArray = tag.toU8a() + bytes.toU8a()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package acurast.codec.type.acurast

import acurast.codec.extensions.readByte
import acurast.codec.extensions.toU8a
import acurast.codec.extensions.*
import acurast.codec.type.*
import java.io.UnsupportedEncodingException
import java.nio.ByteBuffer
Expand All @@ -11,20 +10,10 @@ import java.nio.ByteBuffer
*/
public data class MarketplaceAdvertisement(
public val pricing: MarketplacePricing,
public val maxMemory: Int,
public val networkRequestQuota: Byte,
public val storageCapacity: Int,
public val allowedConsumers: Option<List<MultiOrigin>>,
public val availableModules: List<JobModule> = emptyList(),
public val restriction: MarketplaceAdvertisementRestriction,
) : ToU8a {
override fun toU8a(): ByteArray {
return pricing.toU8a() +
maxMemory.toU8a() +
networkRequestQuota.toU8a() +
storageCapacity.toU8a() +
allowedConsumers.toU8a() +
availableModules.toU8a(withSize = true)

return pricing.toU8a() + restriction.toU8a()
}
}

Expand All @@ -50,7 +39,7 @@ public enum class JobModule(public val id: Byte) : ToU8a {
/**
* The structure of the pricing accepted by the data processor.
*/
public data class MarketplacePricing constructor(
public data class MarketplacePricing(
public val feePerMillisecond: UInt128,
public val feePerStorageByte: UInt128,
public val baseFeePerExecution: UInt128,
Expand All @@ -62,6 +51,56 @@ public data class MarketplacePricing constructor(
baseFeePerExecution.toU8a() +
schedulingWindow.toU8a()
}

public companion object {
public fun read(buffer: ByteBuffer): MarketplacePricing {
val feePerMillisecond = buffer.readU128()
val feePerStorageByte = buffer.readU128()
val baseFeePerExecution = buffer.readU128()
val schedulingWindow = SchedulingWindow.read(buffer)

return MarketplacePricing(
UInt128(feePerMillisecond),
UInt128(feePerStorageByte),
UInt128(baseFeePerExecution),
schedulingWindow,
)
}
}
}

public data class MarketplaceAdvertisementRestriction(
public val maxMemory: Int,
public val networkRequestQuota: Byte,
public val storageCapacity: Int,
public val allowedConsumers: Option<List<MultiOrigin>>,
public val availableModules: List<JobModule> = emptyList(),
) : ToU8a {
override fun toU8a(): ByteArray {
return maxMemory.toU8a() +
networkRequestQuota.toU8a() +
storageCapacity.toU8a() +
allowedConsumers.toU8a() +
availableModules.toU8a(withSize = true)
}

public companion object {
public fun read(buffer: ByteBuffer): MarketplaceAdvertisementRestriction {
val maxMemory = buffer.readU32()
val networkRequestQuota = buffer.readU8()
val storageCapacity = buffer.readU32()
val allowedConsumers = buffer.readOptional { readList { MultiOrigin.read(this@readList) } }
val availableModules = buffer.readList { JobModule.read(this) }

return MarketplaceAdvertisementRestriction(
maxMemory.toInt(),
networkRequestQuota.toByte(),
storageCapacity.toInt(),
allowedConsumers?.let { Option.some(it) } ?: Option.none(),
availableModules,
)
}
}
}

public enum class SchedulingWindowKind(public val id: Int) {
Expand All @@ -78,4 +117,13 @@ public data class SchedulingWindow(public val kind: Kind, public val time: UInt6
override fun toU8a(): ByteArray {
return kind.id.toByte().toU8a() + time.toU8a()
}

public companion object {
public fun read(buffer: ByteBuffer): SchedulingWindow {
val kind = Kind.entries.first { it.id == buffer.readU8().toInt() }
val time = buffer.readU64()

return SchedulingWindow(kind, UInt64(time.toLong()))
}
}
}
12 changes: 7 additions & 5 deletions codec/src/test/kotlin/acurast/codec/extrinsic/MarketplaceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class MarketplaceTest {
baseFeePerExecution = UInt128(BigInteger.ONE),
schedulingWindow = SchedulingWindow(SchedulingWindow.Kind.End, UInt64(10000))
),
maxMemory = 1,
networkRequestQuota = 2,
storageCapacity = 3,
allowedConsumers = Option.some(listOf(MultiOrigin.Acurast(AccountId32("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d".hexToBa())))),
availableModules = listOf(JobModule.DataEncryption)
restriction = MarketplaceAdvertisementRestriction(
maxMemory = 1,
networkRequestQuota = 2,
storageCapacity = 3,
allowedConsumers = Option.some(listOf(MultiOrigin.Acurast(AccountId32("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d".hexToBa())))),
availableModules = listOf(JobModule.DataEncryption)
),
)
val call = AdvertiseCall(byteArrayOf(0x2b, 0x00), advertisement)
val expected = "2b00010000000000000000000000000000000100000000000000000000000000000001000000000000000000000000000000001027000000000000010000000203000000010400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0400"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package acurast.rpc.versioned.storage.v0

import acurast.codec.extensions.*
import acurast.codec.type.acurast.JobEnvironment
import acurast.codec.type.acurast.JobIdentifier
import acurast.codec.type.acurast.JobRegistration
import acurast.codec.type.acurast.*
import acurast.codec.type.marketplace.JobAssignment
import acurast.codec.type.uniques.PalletUniquesItemDetails
import acurast.rpc.engine.RpcEngine
Expand Down Expand Up @@ -83,6 +81,18 @@ public interface V0AcurastStorage : VersionedAcurastStorage {

public suspend fun getAsset(managerId: Int, blockHash: ByteArray? = null, timeout: Long? = null): PalletUniquesItemDetails?

public suspend fun getMarketplacePricing(
accountId: ByteArray,
blockHash: ByteArray? = null,
timeout: Long? = null,
): MarketplacePricing?

public suspend fun getMarketplaceAdvertisementRestriction(
accountId: ByteArray,
blockHash: ByteArray? = null,
timeout: Long? = null,
): MarketplaceAdvertisementRestriction?

public companion object {
public const val VERSION: UInt = 0u
}
Expand Down Expand Up @@ -329,4 +339,52 @@ internal open class V0AcurastStorageImpl(private val engine: RpcEngine, private

return PalletUniquesItemDetails.read(ByteBuffer.wrap(storage.hexToBa()))
}

override suspend fun getMarketplacePricing(
accountId: ByteArray,
blockHash: ByteArray?,
timeout: Long?,
): MarketplacePricing? {
val key =
"AcurastMarketplace".toByteArray().xxH128() +
"StoredAdvertisementPricing".toByteArray().xxH128() +
accountId.blake2b(128)

val storage = state.getStorage(
storageKey = key,
blockHash,
timeout,
engine,
)

if (storage.isNullOrEmpty()) {
return null
}

return MarketplacePricing.read(ByteBuffer.wrap(storage.hexToBa()))
}

override suspend fun getMarketplaceAdvertisementRestriction(
accountId: ByteArray,
blockHash: ByteArray?,
timeout: Long?,
): MarketplaceAdvertisementRestriction? {
val key =
"AcurastMarketplace".toByteArray().xxH128() +
"StoredAdvertisementRestriction".toByteArray().xxH128() +
accountId.blake2b(128)

val storage = state.getStorage(
storageKey = key,
blockHash,
timeout,
engine,
)

if (storage.isNullOrEmpty()) {
return null
}

return MarketplaceAdvertisementRestriction.read(ByteBuffer.wrap(storage.hexToBa()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import org.json.JSONObject
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertTrue
import java.math.BigInteger
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class V0AcurastStorageTest {
@MockK
Expand Down

0 comments on commit ab1048b

Please sign in to comment.