Skip to content

Commit

Permalink
Feat/rpc engine
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamol committed Jan 29, 2024
1 parent f390d0f commit f1dd86b
Show file tree
Hide file tree
Showing 18 changed files with 471 additions and 423 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.11"
const val version = "0.1.12-beta01"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package acurast.rpc

import acurast.codec.extensions.*
import acurast.codec.extensions.blake2b
import acurast.codec.extensions.hexToBa
import acurast.codec.extensions.toU8a
import acurast.codec.extensions.xxH128
import acurast.codec.type.ProcessorVersion
import acurast.codec.type.acurast.JobEnvironment
import acurast.codec.type.acurast.JobIdentifier
import acurast.codec.type.acurast.JobRegistration
import acurast.codec.type.manager.ProcessorUpdateInfo
import acurast.codec.type.marketplace.JobAssignment
import acurast.rpc.http.HttpHeader
import acurast.rpc.http.IHttpClientProvider
import acurast.rpc.http.KtorHttpClientProvider
import acurast.rpc.http.KtorLogger
import acurast.rpc.engine.RpcEngine
import acurast.rpc.pallet.Author
import acurast.rpc.pallet.Chain
import acurast.rpc.pallet.State
Expand All @@ -20,39 +20,30 @@ import acurast.rpc.type.readAccountInfo
import acurast.rpc.type.readPalletAssetsAssetAccount
import java.nio.ByteBuffer

public class RPC public constructor(
rpc_url: String,
http_client: IHttpClientProvider = KtorHttpClientProvider(object : KtorLogger() {
override fun log(message: String) {
println(message)
}
})
) {
public val author: Author = Author(http_client, rpc_url)
public val chain: Chain = Chain(http_client, rpc_url)
public val state: State = State(http_client, rpc_url)
public class AcurastRpc(override val defaultEngine: RpcEngine) : Rpc {
public val author: Author = Author(defaultEngine)
public val chain: Chain = Chain(defaultEngine)
public val state: State = State(defaultEngine)

/**
* Query account information. (nonce, etc...)
*/
public suspend fun getAccountInfo(
accountId: ByteArray,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): FrameSystemAccountInfo {
val key =
"System".toByteArray().xxH128() +
"Account".toByteArray().xxH128() +
accountId.blake2b(128) + accountId;
accountId.blake2b(128) + accountId

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

if (storage.isNullOrEmpty()) {
Expand All @@ -69,23 +60,21 @@ public class RPC public constructor(
assetId: Int,
accountId: ByteArray,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): PalletAssetsAssetAccount? {
val assetIdBytes = assetId.toU8a();
val key =
"Assets".toByteArray().xxH128() +
"Account".toByteArray().xxH128() +
assetIdBytes.blake2b(128) + assetIdBytes +
accountId.blake2b(128) + accountId;
accountId.blake2b(128) + accountId

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

if (storage.isNullOrEmpty()) {
Expand All @@ -101,9 +90,8 @@ public class RPC public constructor(
public suspend fun getJobRegistration(
jobIdentifier: JobIdentifier,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): JobRegistration? {
val origin = jobIdentifier.origin.toU8a()
val jobId = jobIdentifier.id.toU8a()
Expand All @@ -115,10 +103,9 @@ public class RPC public constructor(

val storage = state.getStorage(
storageKey = indexKey,
blockHash = blockHash,
headers = headers,
requestTimeout = requestTimeout,
connectionTimeout = connectionTimeout
blockHash,
timeout,
engine
)

if (storage.isNullOrEmpty()) {
Expand All @@ -134,31 +121,28 @@ public class RPC public constructor(
public suspend fun getAssignedJobs(
accountId: ByteArray,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): List<JobAssignment> {
val jobs: MutableList<JobAssignment> = mutableListOf()

val indexKey =
"AcurastMarketplace".toByteArray().xxH128() +
"StoredMatches".toByteArray().xxH128() +
accountId.blake2b(128) + accountId;
accountId.blake2b(128) + accountId

val keys = state.getKeys(
indexKey,
blockHash = blockHash,
headers = headers,
requestTimeout = requestTimeout,
connectionTimeout = connectionTimeout
key = indexKey,
blockHash,
timeout,
engine,
)

val result = state.queryStorageAt(
storageKeys = keys,
blockHash = blockHash,
headers = headers,
requestTimeout = requestTimeout,
connectionTimeout = connectionTimeout
blockHash,
timeout,
engine,
)

if (result.isNotEmpty()) {
Expand All @@ -176,9 +160,8 @@ public class RPC public constructor(
public suspend fun isAttested(
accountId: ByteArray,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): Boolean {
val key =
"Acurast".toByteArray().xxH128() +
Expand All @@ -188,10 +171,9 @@ public class RPC public constructor(
return try {
val result = state.getStorage(
storageKey = key,
blockHash = blockHash,
headers = headers,
requestTimeout = requestTimeout,
connectionTimeout = connectionTimeout
blockHash,
timeout,
engine,
)

!result.isNullOrEmpty()
Expand All @@ -204,9 +186,8 @@ public class RPC public constructor(
jobIdentifier: JobIdentifier,
accountId: ByteArray,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): JobEnvironment? {
val jobId = jobIdentifier.origin.toU8a() + jobIdentifier.id.toU8a()

Expand All @@ -218,10 +199,9 @@ public class RPC public constructor(

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

if (storage.isNullOrEmpty()) {
Expand All @@ -234,9 +214,8 @@ public class RPC public constructor(
public suspend fun getUpdateInfo(
accountId: ByteArray,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): ProcessorUpdateInfo? {
val key =
"AcurastProcessorManager".toByteArray().xxH128() +
Expand All @@ -245,10 +224,9 @@ public class RPC public constructor(

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

if (storage.isNullOrEmpty()) {
Expand All @@ -261,9 +239,8 @@ public class RPC public constructor(
public suspend fun getKnownBinaryHash(
version: ProcessorVersion,
blockHash: ByteArray? = null,
headers: List<HttpHeader>? = null,
requestTimeout: Long? = null,
connectionTimeout: Long? = null,
timeout: Long? = null,
engine: RpcEngine = defaultEngine,
): ByteArray? {
val versionBytes = version.toU8a()
val key =
Expand All @@ -273,10 +250,9 @@ public class RPC public constructor(

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

return storage?.takeIf { it.isNotEmpty() }?.hexToBa()
Expand Down
15 changes: 15 additions & 0 deletions rpc/src/main/kotlin/acurast/rpc/Rpc.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package acurast.rpc

import acurast.rpc.engine.RpcEngine

public interface Rpc {
public val defaultEngine: RpcEngine
}

internal object JsonRpc {
object Key {
const val RESULT = "result"
const val ERROR = "error"
const val MESSAGE = "message"
}
}
22 changes: 22 additions & 0 deletions rpc/src/main/kotlin/acurast/rpc/engine/RpcEngine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package acurast.rpc.engine

import acurast.rpc.utils.jsonRpcRequest
import org.json.JSONArray
import org.json.JSONObject
import kotlin.random.Random

public interface RpcEngine {
public val id: String
public suspend fun request(body: JSONObject, timeout: Long? = null): JSONObject
}

public suspend fun RpcEngine.request(
id: UInt = Random.nextLong().toUInt(),
method: String,
params: JSONArray = JSONArray(),
timeout: Long? = null,
): JSONObject {
val body = jsonRpcRequest(id, method, params)

return request(body, timeout)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package acurast.rpc.http;
package acurast.rpc.engine.http;

/**
* A key value HTTP header.
Expand All @@ -15,9 +15,9 @@ public typealias HttpParameter = Pair<String, String?>
*
* Use this interface to register a custom HTTP client implementation.
* See:
* - [KtorHttpClientProvider] for a ready-to-use implementation.
* - [KtorHttpClient] for a ready-to-use implementation.
*/
public interface IHttpClientProvider {
public interface HttpClient {

/**
* Call DELETE HTTP method on specified [baseUrl] with [headers] and [parameters].
Expand Down
Loading

0 comments on commit f1dd86b

Please sign in to comment.