From 1982c524fd4a38253614b44f124e4c91b18c3675 Mon Sep 17 00:00:00 2001 From: valentunn <70131744+valentunn@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:33:41 +0300 Subject: [PATCH] Force batch support (#76) * Force batch support * Code style --- build.gradle | 2 +- .../runtime/extrinsic/BatchMode.kt | 19 +++++++++++++++++ .../runtime/extrinsic/ExtrinsicBuilder.kt | 21 ++++++++++++------- .../runtime/extrinsic/ExtrinsicBuilderTest.kt | 4 ++-- 4 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/BatchMode.kt diff --git a/build.gradle b/build.gradle index 71ae3d39..5cd6754e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ buildscript { ext { // App version - versionName = '1.11.1' + versionName = '1.11.2' versionCode = 1 // SDK and tools diff --git a/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/BatchMode.kt b/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/BatchMode.kt new file mode 100644 index 00000000..eef98ba7 --- /dev/null +++ b/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/BatchMode.kt @@ -0,0 +1,19 @@ +package jp.co.soramitsu.fearless_utils.runtime.extrinsic + +enum class BatchMode { + + /** + * Execute all calls until finding an uncessffull one + */ + BATCH, + + /** + * Either execute all calls succeffuly or revert whole batch if at least one call was uncescessfull + */ + BATCH_ALL, + + /** + * Execute all successfull calls even if there was some unsuccessfull ones in between them + */ + FORCE_BATCH +} diff --git a/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilder.kt b/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilder.kt index 952343a4..1fb02f57 100644 --- a/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilder.kt +++ b/fearless-utils/src/main/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilder.kt @@ -99,9 +99,9 @@ class ExtrinsicBuilder( } suspend fun build( - useBatchAll: Boolean = false + batchMode: BatchMode = BatchMode.BATCH ): String { - val call = maybeWrapInBatch(useBatchAll) + val call = maybeWrapInBatch(batchMode) return build(CallRepresentation.Instance(call)) } @@ -115,9 +115,9 @@ class ExtrinsicBuilder( } suspend fun buildSignature( - useBatchAll: Boolean = false + batchMode: BatchMode = BatchMode.BATCH ): String { - val call = maybeWrapInBatch(useBatchAll) + val call = maybeWrapInBatch(batchMode) return buildSignature(CallRepresentation.Instance(call)) } @@ -164,11 +164,11 @@ class ExtrinsicBuilder( return signatureType.toHexUntyped(runtime, multiSignature) } - private fun maybeWrapInBatch(useBatchAll: Boolean): GenericCall.Instance { + private fun maybeWrapInBatch(batchMode: BatchMode): GenericCall.Instance { return if (calls.size == 1) { calls.first() } else { - wrapInBatch(useBatchAll) + wrapInBatch(batchMode) } } @@ -204,9 +204,14 @@ class ExtrinsicBuilder( return default + custom } - private fun wrapInBatch(useBatchAll: Boolean): GenericCall.Instance { + private fun wrapInBatch(batchMode: BatchMode): GenericCall.Instance { val batchModule = runtime.metadata.module("Utility") - val batchFunctionName = if (useBatchAll) "batch_all" else "batch" + + val batchFunctionName = when (batchMode) { + BatchMode.BATCH -> "batch" + BatchMode.BATCH_ALL -> "batch_all" + BatchMode.FORCE_BATCH -> "force_batch" + } val batchFunction = batchModule.call(batchFunctionName) return GenericCall.Instance( diff --git a/fearless-utils/src/test/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilderTest.kt b/fearless-utils/src/test/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilderTest.kt index 0c966d32..02731186 100644 --- a/fearless-utils/src/test/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilderTest.kt +++ b/fearless-utils/src/test/java/jp/co/soramitsu/fearless_utils/runtime/extrinsic/ExtrinsicBuilderTest.kt @@ -128,7 +128,7 @@ class ExtrinsicBuilderTest { ) } - val encoded = extrinsicBuilder.build(useBatchAll = true) + val encoded = extrinsicBuilder.build(batchMode = BatchMode.BATCH_ALL) val decoded = Extrinsic.fromHex(runtime, encoded) assertEquals(decoded.call.function.name, "batch_all") @@ -145,7 +145,7 @@ class ExtrinsicBuilderTest { ) } - val encoded = extrinsicBuilder.build(useBatchAll = true) + val encoded = extrinsicBuilder.build(batchMode = BatchMode.BATCH_ALL) assertEquals(BIG_TRANSACTION, encoded) }