Skip to content

Commit

Permalink
[#1327] Ktlint 1.1.0
Browse files Browse the repository at this point in the history
* [#1327] Ktlint 1.1.0

Closes #1327

* [#1327] Autoformatting with Ktlint

* Ktlint formatting warnings fix
  • Loading branch information
HonzaR authored Jan 4, 2024
1 parent e550cd9 commit 73d7afc
Show file tree
Hide file tree
Showing 240 changed files with 4,678 additions and 3,769 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import org.junit.Test
import kotlin.test.assertContentEquals

class RustDerivationToolTest {

companion object {
private const val SEED_PHRASE =
"kitchen renew wide common vague fold vacuum tilt amazing pear square gossip jewel month tree shock scan" +
" alpha just spot fluid toilet view dinner"
}

@Test
fun create_spending_key_does_not_mutate_passed_bytes() = runTest {
val bytesOne = Mnemonics.MnemonicCode(SEED_PHRASE).toEntropy()
val bytesTwo = Mnemonics.MnemonicCode(SEED_PHRASE).toEntropy()
fun create_spending_key_does_not_mutate_passed_bytes() =
runTest {
val bytesOne = Mnemonics.MnemonicCode(SEED_PHRASE).toEntropy()
val bytesTwo = Mnemonics.MnemonicCode(SEED_PHRASE).toEntropy()

RustDerivationTool.new().deriveUnifiedSpendingKey(bytesOne, networkId = 1, accountIndex = 0)
RustDerivationTool.new().deriveUnifiedSpendingKey(bytesOne, networkId = 1, accountIndex = 0)

assertContentEquals(bytesTwo, bytesOne)
}
assertContentEquals(bytesTwo, bytesOne)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import cash.z.ecc.android.sdk.internal.model.JniWalletSummary
*/
@Suppress("TooManyFunctions")
interface Backend {

val networkId: Int

suspend fun initBlockMetaDb(): Int
Expand Down Expand Up @@ -53,7 +52,11 @@ interface Backend {
* @throws RuntimeException as a common indicator of the operation failure
*/
@Throws(RuntimeException::class)
suspend fun createAccount(seed: ByteArray, treeState: ByteArray, recoverUntil: Long?): JniUnifiedSpendingKey
suspend fun createAccount(
seed: ByteArray,
treeState: ByteArray,
recoverUntil: Long?
): JniUnifiedSpendingKey

fun isValidShieldedAddr(addr: String): Boolean

Expand All @@ -75,7 +78,10 @@ interface Backend {
* @throws RuntimeException as a common indicator of the operation failure
*/
@Throws(RuntimeException::class)
suspend fun getMemoAsUtf8(txId: ByteArray, outputIndex: Int): String?
suspend fun getMemoAsUtf8(
txId: ByteArray,
outputIndex: Int
): String?

suspend fun getNearestRewindHeight(height: Long): Long

Expand Down Expand Up @@ -139,7 +145,10 @@ interface Backend {
* @throws RuntimeException as a common indicator of the operation failure
*/
@Throws(RuntimeException::class)
suspend fun scanBlocks(fromHeight: Long, limit: Long)
suspend fun scanBlocks(
fromHeight: Long,
limit: Long
)

/**
* @throws RuntimeException as a common indicator of the operation failure
Expand All @@ -157,6 +166,7 @@ interface Backend {
suspend fun rewindBlockMetadataToHeight(height: Long)

suspend fun getVerifiedTransparentBalance(address: String): Long

suspend fun getTotalTransparentBalance(address: String): Long

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import java.util.concurrent.Executors
internal object SdkExecutors {
/**
* Executor used for database IO that's shared with the Rust native library.
*/
/*
*
* Based on internal discussion, keep the SDK internals confined to a single IO thread.
*
* We don't expect things to break, but we don't have the WAL enabled for SQLite so this
* is a simple solution.
*/
val DATABASE_IO = Executors.newSingleThreadExecutor {
Thread(it, "zc-io").apply { isDaemon = true }
}
val DATABASE_IO =
Executors.newSingleThreadExecutor {
Thread(it, "zc-io").apply { isDaemon = true }
}
}

object SdkDispatchers {
/**
* Dispatcher used for database IO that's shared with the Rust native library.
*/
/*
*
* Based on internal discussion, keep the SDK internals confined to a single IO thread.
*
* We don't expect things to break, but we don't have the WAL enabled for SQLite so this
* is a simple solution.
*
* Don't use `Dispatchers.IO.limitedParallelism(1)`.
* While it executes serially, each dispatch can be on a different thread.
*/
// Don't use `Dispatchers.IO.limitedParallelism(1)`.
// While it executes serially, each dispatch can be on a different thread.
val DATABASE_IO = SdkExecutors.DATABASE_IO.asCoroutineDispatcher()
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal class NativeLibraryLoader(private val libraryName: String) {
}
}

private suspend fun loadLibrarySuspend(libraryName: String) = withContext(Dispatchers.IO) {
System.loadLibrary(libraryName)
}
private suspend fun loadLibrarySuspend(libraryName: String) =
withContext(Dispatchers.IO) {
System.loadLibrary(libraryName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class RustBackend private constructor(
private val saplingSpendFile: File,
private val saplingOutputFile: File,
) : Backend {

/**
* This function deletes the data database file and the cache directory (compact blocks files) if set by input
* parameters.
Expand All @@ -35,7 +34,10 @@ class RustBackend private constructor(
*
* @return false in case of any required and failed deletion, true otherwise.
*/
suspend fun clear(clearCache: Boolean = true, clearDataDb: Boolean = true): Boolean {
suspend fun clear(
clearCache: Boolean = true,
clearDataDb: Boolean = true
): Boolean {
var cacheClearResult = true
var dataClearResult = true
if (clearCache) {
Expand All @@ -55,19 +57,21 @@ class RustBackend private constructor(
// Wrapper Functions
//

override suspend fun initBlockMetaDb() = withContext(SdkDispatchers.DATABASE_IO) {
initBlockMetaDb(
fsBlockDbRoot.absolutePath,
)
}
override suspend fun initBlockMetaDb() =
withContext(SdkDispatchers.DATABASE_IO) {
initBlockMetaDb(
fsBlockDbRoot.absolutePath,
)
}

override suspend fun initDataDb(seed: ByteArray?) = withContext(SdkDispatchers.DATABASE_IO) {
initDataDb(
dataDbFile.absolutePath,
seed,
networkId = networkId
)
}
override suspend fun initDataDb(seed: ByteArray?) =
withContext(SdkDispatchers.DATABASE_IO) {
initDataDb(
dataDbFile.absolutePath,
seed,
networkId = networkId
)
}

override suspend fun createAccount(
seed: ByteArray,
Expand Down Expand Up @@ -108,15 +112,17 @@ class RustBackend private constructor(
}
}

override suspend fun getMemoAsUtf8(txId: ByteArray, outputIndex: Int) =
withContext(SdkDispatchers.DATABASE_IO) {
getMemoAsUtf8(
dataDbFile.absolutePath,
txId,
outputIndex,
networkId = networkId
)
}
override suspend fun getMemoAsUtf8(
txId: ByteArray,
outputIndex: Int
) = withContext(SdkDispatchers.DATABASE_IO) {
getMemoAsUtf8(
dataDbFile.absolutePath,
txId,
outputIndex,
networkId = networkId
)
}

override suspend fun writeBlockMetadata(blockMetadata: List<JniBlockMeta>) =
withContext(SdkDispatchers.DATABASE_IO) {
Expand Down Expand Up @@ -217,10 +223,11 @@ class RustBackend private constructor(

override suspend fun getFullyScannedHeight() =
withContext(SdkDispatchers.DATABASE_IO) {
val height = getFullyScannedHeight(
dataDbFile.absolutePath,
networkId = networkId
)
val height =
getFullyScannedHeight(
dataDbFile.absolutePath,
networkId = networkId
)

if (-1L == height) {
null
Expand All @@ -231,10 +238,11 @@ class RustBackend private constructor(

override suspend fun getMaxScannedHeight() =
withContext(SdkDispatchers.DATABASE_IO) {
val height = getMaxScannedHeight(
dataDbFile.absolutePath,
networkId = networkId
)
val height =
getMaxScannedHeight(
dataDbFile.absolutePath,
networkId = networkId
)

if (-1L == height) {
null
Expand All @@ -260,7 +268,10 @@ class RustBackend private constructor(
}
}

override suspend fun scanBlocks(fromHeight: Long, limit: Long) {
override suspend fun scanBlocks(
fromHeight: Long,
limit: Long
) {
return withContext(SdkDispatchers.DATABASE_IO) {
scanBlocks(
fsBlockDbRoot.absolutePath,
Expand All @@ -287,19 +298,20 @@ class RustBackend private constructor(
to: String,
value: Long,
memo: ByteArray?
): ByteArray = withContext(SdkDispatchers.DATABASE_IO) {
createToAddress(
dataDbFile.absolutePath,
unifiedSpendingKey,
to,
value,
memo ?: ByteArray(0),
spendParamsPath = saplingSpendFile.absolutePath,
outputParamsPath = saplingOutputFile.absolutePath,
networkId = networkId,
useZip317Fees = IS_USE_ZIP_317_FEES
)
}
): ByteArray =
withContext(SdkDispatchers.DATABASE_IO) {
createToAddress(
dataDbFile.absolutePath,
unifiedSpendingKey,
to,
value,
memo ?: ByteArray(0),
spendParamsPath = saplingSpendFile.absolutePath,
outputParamsPath = saplingOutputFile.absolutePath,
networkId = networkId,
useZip317Fees = IS_USE_ZIP_317_FEES
)
}

override suspend fun shieldToAddress(
account: Int,
Expand Down Expand Up @@ -339,17 +351,13 @@ class RustBackend private constructor(
)
}

override fun isValidShieldedAddr(addr: String) =
isValidShieldedAddress(addr, networkId = networkId)
override fun isValidShieldedAddr(addr: String) = isValidShieldedAddress(addr, networkId = networkId)

override fun isValidTransparentAddr(addr: String) =
isValidTransparentAddress(addr, networkId = networkId)
override fun isValidTransparentAddr(addr: String) = isValidTransparentAddress(addr, networkId = networkId)

override fun isValidUnifiedAddr(addr: String) =
isValidUnifiedAddress(addr, networkId = networkId)
override fun isValidUnifiedAddr(addr: String) = isValidUnifiedAddress(addr, networkId = networkId)

override fun getBranchIdForHeight(height: Long): Long =
branchIdForHeight(height, networkId = networkId)
override fun getBranchIdForHeight(height: Long): Long = branchIdForHeight(height, networkId = networkId)

/**
* Exposes all of the librustzcash functions along with helpers for loading the static library.
Expand Down Expand Up @@ -397,7 +405,11 @@ class RustBackend private constructor(
private external fun initBlockMetaDb(fsBlockDbRoot: String): Int

@JvmStatic
private external fun initDataDb(dbDataPath: String, seed: ByteArray?, networkId: Int): Int
private external fun initDataDb(
dbDataPath: String,
seed: ByteArray?,
networkId: Int
): Int

@JvmStatic
private external fun createAccount(
Expand All @@ -422,21 +434,34 @@ class RustBackend private constructor(
private external fun getSaplingReceiverForUnifiedAddress(ua: String): String?

@JvmStatic
private external fun listTransparentReceivers(dbDataPath: String, account: Int, networkId: Int): Array<String>
private external fun listTransparentReceivers(
dbDataPath: String,
account: Int,
networkId: Int
): Array<String>

fun validateUnifiedSpendingKey(bytes: ByteArray) = isValidSpendingKey(bytes)

@JvmStatic
private external fun isValidSpendingKey(bytes: ByteArray): Boolean

@JvmStatic
private external fun isValidShieldedAddress(addr: String, networkId: Int): Boolean
private external fun isValidShieldedAddress(
addr: String,
networkId: Int
): Boolean

@JvmStatic
private external fun isValidTransparentAddress(addr: String, networkId: Int): Boolean
private external fun isValidTransparentAddress(
addr: String,
networkId: Int
): Boolean

@JvmStatic
private external fun isValidUnifiedAddress(addr: String, networkId: Int): Boolean
private external fun isValidUnifiedAddress(
addr: String,
networkId: Int
): Boolean

@JvmStatic
private external fun getMemoAsUtf8(
Expand Down Expand Up @@ -563,7 +588,10 @@ class RustBackend private constructor(
): ByteArray

@JvmStatic
private external fun branchIdForHeight(height: Long, networkId: Int): Long
private external fun branchIdForHeight(
height: Long,
networkId: Int
): Long

@JvmStatic
@Suppress("LongParameterList")
Expand Down
Loading

0 comments on commit 73d7afc

Please sign in to comment.