2020-06-10 00:08:19 -07:00
|
|
|
package cash.z.ecc.android.sdk.demoapp.demos.getblockrange
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
import android.os.Bundle
|
2019-10-21 03:17:07 -07:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
2020-06-10 00:08:19 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.BaseDemoFragment
|
2020-09-11 00:16:46 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.R
|
2020-06-10 00:08:19 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.databinding.FragmentGetBlockRangeBinding
|
2021-09-04 04:05:41 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.ext.requireApplicationContext
|
2023-01-09 07:42:38 -08:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.util.fromResources
|
2020-09-11 00:16:46 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.util.mainActivity
|
2022-08-02 06:29:09 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.ZcashNetwork
|
2022-07-12 05:40:09 -07:00
|
|
|
import kotlin.math.max
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-02-27 00:25:07 -08:00
|
|
|
/**
|
2023-02-01 02:14:55 -08:00
|
|
|
* Retrieves a range of compact block from the lightwalletd server and displays basic information
|
2020-09-11 00:16:46 -07:00
|
|
|
* about them. This demonstrates the basic ability to connect to the server, request a range of
|
2020-02-27 00:25:07 -08:00
|
|
|
* compact block and parse the response. This could be augmented to display metadata about certain
|
|
|
|
* block ranges for instance, to find the block with the most shielded transactions in a range.
|
|
|
|
*/
|
2019-10-21 03:17:07 -07:00
|
|
|
class GetBlockRangeFragment : BaseDemoFragment<FragmentGetBlockRangeBinding>() {
|
2023-04-13 04:36:24 -07:00
|
|
|
// TODO [#973]: Eliminate old UI demo-app
|
|
|
|
// TODO [#973]: https://github.com/zcash/zcash-android-wallet-sdk/issues/973
|
|
|
|
@Suppress("MaxLineLength", "MagicNumber", "UNUSED_PARAMETER")
|
|
|
|
/*
|
2022-07-12 05:40:09 -07:00
|
|
|
private fun setBlockRange(blockRange: ClosedRange<BlockHeight>) {
|
2020-09-11 00:16:46 -07:00
|
|
|
val start = System.currentTimeMillis()
|
2023-04-13 04:36:24 -07:00
|
|
|
|
|
|
|
val range = BlockHeightUnsafe(blockRange.start.value)..BlockHeightUnsafe(blockRange.endInclusive.value)
|
|
|
|
|
|
|
|
val response = lightWalletClient?.getBlockRange(range)
|
|
|
|
|
|
|
|
val blocks = when (response) {
|
|
|
|
is Response.Success -> {
|
|
|
|
Twig.debug { "Get blocks: ${response.result} for range: $range succeeded." }
|
|
|
|
response.result
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
Twig.debug { "Get blocks for range: $range failed with: $response." }
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
val fetchDelta = System.currentTimeMillis() - start
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
// Note: This is a demo so we won't worry about iterating efficiently over these blocks
|
2022-07-28 05:39:48 -07:00
|
|
|
// Note: Converting the blocks sequence to a list can consume a lot of memory and may
|
|
|
|
// cause OOM.
|
2022-08-17 06:48:02 -07:00
|
|
|
binding.textInfo.text = HtmlCompat.fromHtml(
|
2022-07-28 05:39:48 -07:00
|
|
|
blocks?.toList()?.run {
|
2021-09-25 08:11:33 -07:00
|
|
|
val count = size
|
2023-04-13 04:36:24 -07:00
|
|
|
val processTime = System.currentTimeMillis() - start - fetchDelta
|
|
|
|
var totalSaplingOutputCount: UInt = 0u
|
|
|
|
var totalOrchardOutputCount: UInt = 0u
|
|
|
|
forEach {
|
|
|
|
totalSaplingOutputCount += it.saplingOutputsCount
|
|
|
|
totalOrchardOutputCount += it.orchardOutputsCount
|
2021-09-25 08:11:33 -07:00
|
|
|
}
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2022-08-23 06:49:00 -07:00
|
|
|
@Suppress("MaxLineLength", "MagicNumber")
|
2021-09-25 08:11:33 -07:00
|
|
|
"""
|
2020-09-11 00:16:46 -07:00
|
|
|
<b>total blocks:</b> ${count.withCommas()}
|
2021-09-25 08:11:33 -07:00
|
|
|
<br/><b>fetch time:</b> ${if (fetchDelta > 1000) "%.2f sec".format(fetchDelta / 1000.0) else "%d ms".format(fetchDelta)}
|
|
|
|
<br/><b>process time:</b> ${if (processTime > 1000) "%.2f sec".format(processTime / 1000.0) else "%d ms".format(processTime)}
|
2021-09-04 04:05:41 -07:00
|
|
|
<br/><b>block time range:</b> ${first().time.toRelativeTime(requireApplicationContext())}<br/>   to ${last().time.toRelativeTime(requireApplicationContext())}
|
2023-04-13 04:36:24 -07:00
|
|
|
<br/><b>total sapling outputs:</b> $totalSaplingOutputCount
|
|
|
|
<br/><b>total orchard outputs:</b> $totalOrchardOutputCount
|
2021-09-25 08:11:33 -07:00
|
|
|
""".trimIndent()
|
2022-08-17 06:48:02 -07:00
|
|
|
} ?: "No blocks found in that range.",
|
|
|
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
2021-09-25 08:11:33 -07:00
|
|
|
)
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
2023-04-13 04:36:24 -07:00
|
|
|
*/
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2022-08-23 06:49:00 -07:00
|
|
|
private fun onApply(unused: View) {
|
2022-07-12 05:40:09 -07:00
|
|
|
val network = ZcashNetwork.fromResources(requireApplicationContext())
|
2024-01-04 12:21:32 -08:00
|
|
|
val start =
|
|
|
|
max(
|
|
|
|
binding.textStartHeight.text.toString().toLongOrNull()
|
|
|
|
?: network.saplingActivationHeight.value,
|
|
|
|
network.saplingActivationHeight.value
|
|
|
|
)
|
|
|
|
val end =
|
|
|
|
max(
|
|
|
|
binding.textEndHeight.text.toString().toLongOrNull()
|
|
|
|
?: network.saplingActivationHeight.value,
|
|
|
|
network.saplingActivationHeight.value
|
|
|
|
)
|
2019-10-21 03:17:07 -07:00
|
|
|
if (start <= end) {
|
2022-08-23 06:49:00 -07:00
|
|
|
@Suppress("TooGenericExceptionCaught")
|
2020-09-11 00:16:46 -07:00
|
|
|
try {
|
|
|
|
with(binding.buttonApply) {
|
|
|
|
isEnabled = false
|
|
|
|
setText(R.string.loading)
|
|
|
|
binding.textInfo.setText(R.string.loading)
|
|
|
|
post {
|
2023-04-13 04:36:24 -07:00
|
|
|
// TODO [#973]: Eliminate old UI demo-app
|
|
|
|
// TODO [#973]: https://github.com/zcash/zcash-android-wallet-sdk/issues/973
|
|
|
|
// setBlockRange(
|
|
|
|
// BlockHeight.new(network, start)..BlockHeight.new(
|
|
|
|
// network,
|
|
|
|
// end
|
|
|
|
// )
|
|
|
|
// )
|
2020-09-11 00:16:46 -07:00
|
|
|
isEnabled = true
|
|
|
|
setText(R.string.apply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (t: Throwable) {
|
|
|
|
setError(t.toString())
|
|
|
|
}
|
2019-10-21 03:17:07 -07:00
|
|
|
} else {
|
|
|
|
setError("Invalid range")
|
|
|
|
}
|
2020-09-11 00:16:46 -07:00
|
|
|
mainActivity()?.hideKeyboard()
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun setError(message: String) {
|
|
|
|
binding.textInfo.text = "Error: $message"
|
|
|
|
}
|
2020-09-11 00:16:46 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// Android Lifecycle overrides
|
|
|
|
//
|
|
|
|
|
2024-01-04 12:21:32 -08:00
|
|
|
override fun onViewCreated(
|
|
|
|
view: View,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
) {
|
2020-09-11 00:16:46 -07:00
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
binding.buttonApply.setOnClickListener(::onApply)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Base Fragment overrides
|
|
|
|
//
|
|
|
|
|
|
|
|
override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetBlockRangeBinding =
|
|
|
|
FragmentGetBlockRangeBinding.inflate(layoutInflater)
|
|
|
|
|
2020-09-21 19:43:50 -07:00
|
|
|
override fun onActionButtonClicked() {
|
|
|
|
super.onActionButtonClicked()
|
|
|
|
}
|
2020-06-09 20:28:21 -07:00
|
|
|
}
|