Added more fixes and examples for Zcon1.

Including unit tests that are more like utilities for doing interesting things.
This commit is contained in:
Kevin Gorham 2019-06-20 06:10:39 -04:00 committed by Kevin Gorham
parent ad3a080466
commit 1de3ee9ed5
4 changed files with 156 additions and 16 deletions

View File

@ -10,7 +10,7 @@ buildscript {
'room': '2.1.0'
],
'grpc':'1.21.0',
'kotlin': '1.3.21',
'kotlin': '1.3.31',
'coroutines': '1.3.0-M1',
'junitJupiter': '5.5.0-M1'
]
@ -22,7 +22,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-beta03'
classpath 'com.android.tools.build:gradle:3.5.0-beta04'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath "org.jetbrains.kotlin:kotlin-allopen:${versions.kotlin}"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.18"

View File

@ -61,13 +61,11 @@ class IntegrationTest {
processor = CompactBlockProcessor(config, downloader, repository, rustBackend)
repository = PollingTransactionRepository(context, dataDbName, rustBackend, 10_000L)
wallet = Wallet(
context,
rustBackend,
context.getDatabasePath(dataDbName).absolutePath,
context.cacheDir.absolutePath,
arrayOf(0),
SampleSeedProvider("dummyseed"),
SampleSpendingKeyProvider("dummyseed")
context = context,
rustBackend = rustBackend,
dataDbName = dataDbName,
seedProvider = SampleSeedProvider("dummyseed"),
spendingKeyProvider = SampleSpendingKeyProvider("dummyseed")
)
// repository.start(this)

View File

@ -65,13 +65,11 @@ class AddressGeneratorUtil {
deleteDb()
val spendingKeyProvider = Delegates.notNull<String>()
wallet = Wallet(
context,
rustBackend,
context.getDatabasePath(dataDbName).absolutePath,
context.cacheDir.absolutePath,
arrayOf(0),
SampleSeedProvider(seed),
spendingKeyProvider
context = context,
rustBackend = rustBackend,
dataDbName = dataDbName,
seedProvider = SampleSeedProvider(seed),
spendingKeyProvider = spendingKeyProvider
)
wallet.initialize()
return spendingKeyProvider

View File

@ -0,0 +1,144 @@
package cash.z.wallet.sdk.db
import androidx.test.platform.app.InstrumentationRegistry
import cash.z.wallet.sdk.block.CompactBlockDbStore
import cash.z.wallet.sdk.block.CompactBlockDownloader
import cash.z.wallet.sdk.data.SampleSeedProvider
import cash.z.wallet.sdk.data.TroubleshootingTwig
import cash.z.wallet.sdk.data.Twig
import cash.z.wallet.sdk.data.twig
import cash.z.wallet.sdk.jni.RustBackend
import cash.z.wallet.sdk.secure.Wallet
import cash.z.wallet.sdk.service.LightWalletGrpcService
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import okio.Okio
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import java.io.IOException
import kotlin.properties.Delegates
@ExperimentalCoroutinesApi
class BalancePrinterUtil {
private val host = "34.65.230.46"
private val downloadBatchSize = 9_000
private val context = InstrumentationRegistry.getInstrumentation().context
private val cacheDbName = "BalanceUtilCache.db"
private val dataDbName = "BalanceUtilData.db"
private val cacheDbPath = context.getDatabasePath("BalanceUtilCache.db").absolutePath
private val dataDbPath = context.getDatabasePath("BalanceUtilData.db").absolutePath
private val rustBackend = RustBackend()
private val birthday = 523240
private val downloader = CompactBlockDownloader(
LightWalletGrpcService(context, host),
CompactBlockDbStore(context, cacheDbName)
)
@Before
fun setup() {
Twig.plant(TroubleshootingTwig())
rustBackend.initLogs()
cacheBlocks()
}
private fun cacheBlocks() = runBlocking {
twig("downloading compact blocks...")
val latestBlockHeight = downloader.getLatestBlockHeight()
val lastDownloaded = downloader.getLastDownloadedHeight()
val blockRange = (Math.max(birthday, lastDownloaded))..latestBlockHeight
downloadNewBlocks(blockRange)
val error = validateNewBlocks(blockRange)
twig("validation completed with result $error")
assertEquals(-1, error)
}
private fun deleteDb() {
context.getDatabasePath(dataDbName).absoluteFile.delete()
}
@Test
fun printBalances() = runBlocking {
readLines().collect { seed ->
deleteDb()
initWallet(seed)
twig("scanning blocks for seed <$seed>")
rustBackend.scanBlocks(cacheDbPath, dataDbPath)
twig("done scanning blocks for seed $seed")
val total = rustBackend.getBalance(dataDbPath, 0)
twig("found total: $total")
val available = rustBackend.getVerifiedBalance(dataDbPath, 0)
twig("found available: $available")
twig("xrxrx2\t$seed\t$total\t$available")
println("xrxrx2\t$seed\t$total\t$available")
}
Thread.sleep(5000)
assertEquals("foo", "bar")
}
@Throws(IOException::class)
fun readLines() = flow<String> {
val seedFile = javaClass.getResourceAsStream("/utils/seeds.txt")
Okio.buffer(Okio.source(seedFile)).use { source ->
var line: String? = source.readUtf8Line()
while (line != null) {
emit(line)
line = source.readUtf8Line()
}
}
}
private fun initWallet(seed: String): Wallet {
val spendingKeyProvider = Delegates.notNull<String>()
return Wallet(
context = context,
birthday = Wallet.loadBirthdayFromAssets(context, birthday),
rustBackend = rustBackend,
dataDbName = dataDbName,
seedProvider = SampleSeedProvider(seed),
spendingKeyProvider = spendingKeyProvider
).apply {
runCatching {
initialize()
}
}
}
private fun downloadNewBlocks(range: IntRange) = runBlocking {
Twig.sprout("downloading")
twig("downloading blocks in range $range")
var downloadedBlockHeight = range.start
val count = range.last - range.first + 1
val batches = (count / downloadBatchSize + (if (count.rem(downloadBatchSize) == 0) 0 else 1))
twig("found $count missing blocks, downloading in $batches batches of $downloadBatchSize...")
for (i in 1..batches) {
val end = Math.min(range.first + (i * downloadBatchSize), range.last + 1)
val batchRange = downloadedBlockHeight until end
twig("downloaded $batchRange (batch $i of $batches)") {
downloader.downloadBlockRange(batchRange)
}
downloadedBlockHeight = end
}
Twig.clip("downloading")
}
private fun validateNewBlocks(range: IntRange?): Int {
val dummyWallet = initWallet("dummySeed")
Twig.sprout("validating")
twig("validating blocks in range $range")
val result = rustBackend.validateCombinedChain(cacheDbPath, dataDbPath)
Twig.clip("validating")
return result
}
}