Update tests.

This commit is contained in:
Kevin Gorham 2019-06-14 19:28:07 -04:00 committed by Kevin Gorham
parent 94e2af7287
commit 305390b439
6 changed files with 165 additions and 12 deletions

View File

@ -1,10 +1,14 @@
package cash.z.wallet.sdk.db
import android.text.format.DateUtils
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.block.CompactBlockProcessor
import cash.z.wallet.sdk.block.ProcessorConfig
import cash.z.wallet.sdk.data.*
import cash.z.wallet.sdk.jni.RustBackend
import cash.z.wallet.sdk.secure.Wallet
import cash.z.wallet.sdk.service.LightWalletGrpcService
import kotlinx.coroutines.runBlocking
import org.junit.AfterClass
import org.junit.Before
@ -21,13 +25,14 @@ class IntegrationTest {
private val cacheDdName = "IntegrationCache41.db"
private val context = InstrumentationRegistry.getInstrumentation().context
private lateinit var downloader: CompactBlockStream
private lateinit var downloader: CompactBlockDownloader
private lateinit var processor: CompactBlockProcessor
private lateinit var wallet: Wallet
@Before
fun setup() {
deleteDbs()
Twig.plant(TroubleshootingTwig())
}
private fun deleteDbs() {
@ -38,14 +43,22 @@ class IntegrationTest {
}
}
@Test(timeout = 1L * DateUtils.MINUTE_IN_MILLIS/10)
@Test(timeout = 120_000L)
fun testSync() = runBlocking<Unit> {
val rustBackend = RustBackend()
rustBackend.initLogs()
val logger = TroubleshootingTwig()
val config = ProcessorConfig(
cacheDbPath = context.getDatabasePath(cacheDdName).absolutePath,
dataDbPath = context.getDatabasePath(dataDbName).absolutePath,
downloadBatchSize = 2000,
blockPollFrequencyMillis = 10_000L
)
downloader = CompactBlockStream("10.0.2.2", 9067, logger)
processor = CompactBlockProcessor(context, rustBackend, cacheDdName, dataDbName, logger = logger)
val lightwalletService = LightWalletGrpcService("192.168.1.134")
val compactBlockStore = CompactBlockDbStore(context, config.cacheDbPath)
downloader = CompactBlockDownloader(lightwalletService, compactBlockStore)
processor = CompactBlockProcessor(config, downloader, repository, rustBackend)
repository = PollingTransactionRepository(context, dataDbName, 10_000L)
wallet = Wallet(
context,
@ -59,16 +72,15 @@ class IntegrationTest {
// repository.start(this)
synchronizer = SdkSynchronizer(
downloader,
processor,
repository,
ActiveTransactionManager(repository, downloader.connection, wallet, logger),
ActiveTransactionManager(repository, lightwalletService, wallet),
wallet,
1000
).start(this)
for(i in synchronizer.progress()) {
logger.twig("made progress: $i")
twig("made progress: $i")
}
}

View File

@ -0,0 +1,79 @@
package cash.z.wallet.sdk.db
import androidx.test.platform.app.InstrumentationRegistry
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.jni.RustBackend
import cash.z.wallet.sdk.secure.Wallet
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
import kotlin.properties.ReadWriteProperty
@ExperimentalCoroutinesApi
class AddressGeneratorUtil {
private val dataDbName = "AddressUtilData.db"
private val context = InstrumentationRegistry.getInstrumentation().context
private val rustBackend = RustBackend()
private lateinit var wallet: Wallet
@Before
fun setup() {
Twig.plant(TroubleshootingTwig())
rustBackend.initLogs()
}
private fun deleteDb() {
context.getDatabasePath(dataDbName).absoluteFile.delete()
}
@Test
fun generateAddresses() = runBlocking {
readLines().collect { seed ->
val keyStore = initWallet(seed)
val address = wallet.getAddress()
val pk by keyStore
println("xrxrx2\t$seed\t$address\t$pk")
}
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): ReadWriteProperty<Any?, String> {
deleteDb()
val spendingKeyProvider = Delegates.notNull<String>()
wallet = Wallet(
context,
rustBackend,
context.getDatabasePath(dataDbName).absolutePath,
context.cacheDir.absolutePath,
arrayOf(0),
SampleSeedProvider(seed),
spendingKeyProvider
)
wallet.initialize()
return spendingKeyProvider
}
}

View File

@ -0,0 +1,3 @@
seed-1
seed-2
seed-3

View File

@ -11,7 +11,7 @@ import java.util.*
//TODO: provide a dynamic way to configure this globally for the SDK
// For now, just make these vars so at least they could be modified in one place
object Conversions {
var ONE_ZEC_IN_ZATOSHI = BigDecimal(100_000_000.0, MathContext.DECIMAL128)
var ONE_ZEC_IN_ZATOSHI = BigDecimal(ZATOSHI, MathContext.DECIMAL128)
var ZEC_FORMATTER = NumberFormat.getInstance(Locale.getDefault()).apply {
roundingMode = RoundingMode.HALF_EVEN
maximumFractionDigits = 6

View File

@ -0,0 +1,59 @@
package cash.z.wallet.sdk.ext
//
// Constants
//
/**
* Miner's fee in zatoshi.
*/
const val MINERS_FEE_ZATOSHI = 10_000L
/**
* The number of zatoshi that equal 1 ZEC.
*/
const val ZATOSHI = 100_000_000L
/**
* The height of the first sapling block. When it comes to shielded transactions, we do not need to consider any blocks
* prior to this height, at all.
*/
const val SAPLING_ACTIVATION_HEIGHT = 280_000
/**
* The theoretical maximum number of blocks in a reorg, due to other bottlenecks in the protocol design.
*/
const val MAX_REORG_SIZE = 100
//
// Defaults
//
/**
* Default size of batches of blocks to request from the compact block service.
*/
const val DEFAULT_BATCH_SIZE = 100
/**
* Default amount of time, in milliseconds, to poll for new blocks. Typically, this should be about half the average
* block time.
*/
const val DEFAULT_POLL_INTERVAL = 75_000L
/**
* Default attempts at retrying.
*/
const val DEFAULT_RETRIES = 5
/**
* Default number of blocks to rewind when a chain reorg is detected. This should be large enough to recover from the
* reorg but smaller than the theoretical max reorg size of 100.
*/
const val DEFAULT_REWIND_DISTANCE = 10
/**
* The number of blocks to allow before considering our data to be stale. This usually helps with what to do when
* returning from the background and is exposed via the Synchronizer's isStale function.
*/
const val DEFAULT_STALE_TOLERANCE = 10

View File

@ -141,7 +141,7 @@ internal class MockSynchronizerTest {
@Test
fun `balance matches transactions without sends`() = runBlocking {
val balances = fastSynchronizer.start(fastSynchronizer).balance()
val balances = fastSynchronizer.start(fastSynchronizer).balances()
var transactions = listOf<WalletTransaction>()
while (transactions.count() < 10) {
transactions = fastSynchronizer.allTransactions().receive()
@ -153,7 +153,7 @@ internal class MockSynchronizerTest {
@Test
fun `balance matches transactions with sends`() = runBlocking {
var transactions = listOf<WalletTransaction>()
val balances = fastSynchronizer.start(fastSynchronizer).balance()
val balances = fastSynchronizer.start(fastSynchronizer).balances()
val transactionChannel = fastSynchronizer.allTransactions()
while (transactions.count() < 10) {
fastSynchronizer.sendToAddress(Random.nextLong(1L..10_000_000_000), validAddress)