Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken unit tests and warnings #609

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.horizontalsystems.bitcoincash.blocks

import io.horizontalsystems.bitcoincore.blocks.BlockMedianTimeHelper
import io.horizontalsystems.bitcoincore.core.IStorage
import io.horizontalsystems.bitcoincore.managers.BlockValidatorHelper
import io.horizontalsystems.bitcoincore.models.Block

class BitcoinCashBlockValidatorHelper(storage: IStorage) : BlockValidatorHelper(storage) {
private val blockMedianTimeHelper: BlockMedianTimeHelper = BlockMedianTimeHelper(storage)

// Get median of last 3 blocks based on timestamp
fun getSuitableBlock(blocks: MutableList<Block>): Block {
Expand All @@ -29,4 +31,8 @@ class BitcoinCashBlockValidatorHelper(storage: IStorage) : BlockValidatorHelper(
this[index1] = this[index2]
this[index2] = tmp
}

fun medianTimePast(block: Block): Long {
return blockMedianTimeHelper.medianTimePast(block) ?: block.height.toLong()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package io.horizontalsystems.bitcoincore.managers

import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonValue
import io.horizontalsystems.bitcoincore.utils.NetworkUtils
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.BufferedOutputStream
import java.io.BufferedWriter
import java.io.IOException
import java.io.FileNotFoundException
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.net.URL
Expand Down Expand Up @@ -35,7 +35,7 @@ class ApiManager(private val host: String) {
Json.parse(it.bufferedReader())
}
} catch (exception: IOException) {
throw Exception("${exception.javaClass.simpleName}: $host")
throw FileNotFoundException("${exception.javaClass.simpleName}: $host")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ object BlockSyncerTest : Spek({
}

describe("#init") {
beforeEach {
beforeEachTest {
reset(storage)
}

context("when there are some saved blocks") {
beforeEach {
beforeEachTest {
whenever(storage.blocksCount()).thenReturn(1)
whenever(storage.lastBlock()).thenReturn(checkpointBlock)

Expand Down Expand Up @@ -90,7 +90,7 @@ object BlockSyncerTest : Spek({
val blockHash = BlockHash("abc".hexToByteArray(), 1)

context("when no BlockHashes") {
beforeEach {
beforeEachTest {
whenever(storage.getBlockchainBlockHashes()).thenReturn(listOf())
whenever(storage.blocksCount(listOf())).thenReturn(0)
}
Expand All @@ -111,7 +111,7 @@ object BlockSyncerTest : Spek({
}

context("when there are some BlockHashes which haven't downloaded blocks") {
beforeEach {
beforeEachTest {
whenever(storage.getBlockchainBlockHashes()).thenReturn(listOf(blockHash))
whenever(storage.blocksCount(listOf(blockHash.headerHash))).thenReturn(0)
}
Expand All @@ -125,7 +125,7 @@ object BlockSyncerTest : Spek({
}

context("when there are some BlockHashes which have downloaded blocks") {
beforeEach {
beforeEachTest {
whenever(storage.getBlockchainBlockHashes()).thenReturn(listOf(blockHash))
whenever(storage.blocksCount(listOf(blockHash.headerHash))).thenReturn(1)
}
Expand All @@ -143,7 +143,7 @@ object BlockSyncerTest : Spek({
val emptyBlocks = mock<List<Block>>()
val blocksHashes = listOf(byteArrayOf(1))

beforeEach {
beforeEachTest {
whenever(storage.getBlocks(any(), any())).thenReturn(emptyBlocks)
whenever(storage.getBlockHashHeaderHashes(listOf(checkpointBlock.headerHash))).thenReturn(blocksHashes)

Expand Down Expand Up @@ -209,7 +209,7 @@ object BlockSyncerTest : Spek({
val emptyBlocks = mock<List<Block>>()
val blocksHashes = listOf(byteArrayOf(1))

beforeEach {
beforeEachTest {
whenever(storage.getBlocks(any(), any())).thenReturn(emptyBlocks)
whenever(storage.getBlockHashHeaderHashes(listOf(checkpointBlock.headerHash))).thenReturn(blocksHashes)

Expand Down Expand Up @@ -250,7 +250,7 @@ object BlockSyncerTest : Spek({
describe("#getBlockLocatorHashes") {
val peerLastBlockHeight = 99

beforeEach {
beforeEachTest {
whenever(storage.getLastBlockchainBlockHash()).thenReturn(null)
whenever(storage.getBlocks(checkpointBlock.height, "height", 10)).thenReturn(listOf())
whenever(storage.getBlock(peerLastBlockHeight)).thenReturn(null)
Expand All @@ -265,7 +265,7 @@ object BlockSyncerTest : Spek({
context("when there's blockchain block hashes") {
val blockHash = BlockHash("cba".hexToByteArray(), 1)

beforeEach {
beforeEachTest {
whenever(storage.getLastBlockchainBlockHash()).thenReturn(blockHash)
}

Expand All @@ -279,7 +279,7 @@ object BlockSyncerTest : Spek({
val block2 = mock(Block::class.java)
val blocks = listOf(block1, block2)

beforeEach {
beforeEachTest {
whenever(storage.getBlocks(heightGreaterThan = checkpointBlock.height, sortedBy = "height", limit = 10)).thenReturn(blocks)
}

Expand All @@ -299,7 +299,7 @@ object BlockSyncerTest : Spek({
val headerHash = byteArrayOf(1, 2, 3)
val blockHash = BlockHash(headerHash, 1)

beforeEach {
beforeEachTest {
whenever(peerBlock.headerHash).thenReturn(headerHash)

whenever(storage.getLastBlockchainBlockHash()).thenReturn(blockHash)
Expand All @@ -321,14 +321,14 @@ object BlockSyncerTest : Spek({
byteArrayOf(4, 5, 6),
byteArrayOf(5, 6, 7))

beforeEach {
beforeEachTest {
whenever(storage.getBlockHashHeaderHashes()).thenReturn(existingHashes)
}

context("when there's some block hashes") {
val blockHash = BlockHash(byteArrayOf(1), 99, sequence = 99)

beforeEach {
beforeEachTest {
whenever(storage.getLastBlockHash()).thenReturn(blockHash)
}

Expand All @@ -344,7 +344,7 @@ object BlockSyncerTest : Spek({
}

context("when there's no block hashes") {
beforeEach {
beforeEachTest {
whenever(storage.getLastBlockHash()).thenReturn(null)
}

Expand All @@ -366,7 +366,7 @@ object BlockSyncerTest : Spek({
val merkleBlock = mock(MerkleBlock::class.java)
val merkleHeight = 1

beforeEach {
beforeEachTest {
whenever(merkleBlock.height).thenReturn(null)
whenever(merkleBlock.associatedTransactions).thenReturn(mutableListOf())
whenever(block.height).thenReturn(merkleHeight)
Expand All @@ -377,7 +377,7 @@ object BlockSyncerTest : Spek({
whenever(blockchain.forceAdd(merkleBlock, merkleHeight)).thenReturn(block)
}

afterEach {
afterEachTest {
reset(merkleBlock)
}

Expand All @@ -390,7 +390,7 @@ object BlockSyncerTest : Spek({

context("when merkle block height it not null") {

beforeEach {
beforeEachTest {
whenever(merkleBlock.height).thenReturn(merkleHeight)
}

Expand All @@ -402,7 +402,7 @@ object BlockSyncerTest : Spek({
}

context("when bloom filter expired while processing transaction") {
beforeEach {
beforeEachTest {
whenever(transactionProcessor.processReceived(merkleBlock.associatedTransactions, block, state.iterationHasPartialBlocks))
.thenThrow(BloomFilterManager.BloomFilterExpired)
}
Expand All @@ -415,7 +415,7 @@ object BlockSyncerTest : Spek({
}

context("when iteration not have partial blocks") {
beforeEach {
beforeEachTest {
whenever(state.iterationHasPartialBlocks).thenReturn(false)
}

Expand Down Expand Up @@ -445,7 +445,7 @@ object BlockSyncerTest : Spek({
val lastCheckpointBlock = mock<Block>()
val lastBlockInDB = mock<Block>()

beforeEach {
beforeEachTest {
whenever(network.bip44Checkpoint).thenReturn(bip44Checkpoint)
whenever(bip44Checkpoint.block).thenReturn(bip44CheckpointBlock)
whenever(bip44Checkpoint.additionalBlocks).thenReturn(listOf())
Expand All @@ -470,7 +470,7 @@ object BlockSyncerTest : Spek({
val syncMode = BitcoinCore.SyncMode.Api()

context("when last block in DB earlier than checkpoint block") {
beforeEach {
beforeEachTest {
whenever(lastBlockInDB.height).thenReturn(100)
whenever(lastCheckpointBlock.height).thenReturn(200)
}
Expand All @@ -482,7 +482,7 @@ object BlockSyncerTest : Spek({
}

context("when last block in DB later than checkpoint block") {
beforeEach {
beforeEachTest {
whenever(storage.lastBlock()).thenReturn(lastBlockInDB)
whenever(lastBlockInDB.height).thenReturn(200)
whenever(lastCheckpointBlock.height).thenReturn(100)
Expand All @@ -496,7 +496,7 @@ object BlockSyncerTest : Spek({
}

context("when DB has no block") {
beforeEach {
beforeEachTest {
whenever(storage.lastBlock()).thenReturn(null)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ object BlockchainTest : Spek({
}

describe("#connect") {
beforeEach {
beforeEachTest {
whenever(merkleBlock.blockHash).thenReturn(byteArrayOf(1, 2, 3))
whenever(merkleBlock.header).thenReturn(blockHeader)
}

context("when block exists") {
beforeEach {
beforeEachTest {
whenever(storage.getBlock(merkleBlock.blockHash)).thenReturn(block)
}

Expand All @@ -67,12 +67,12 @@ object BlockchainTest : Spek({
}

context("when block doesn't exist") {
beforeEach {
beforeEachTest {
whenever(storage.getBlock(merkleBlock.blockHash)).thenReturn(null)
}

context("when block is not in chain") {
beforeEach {
beforeEachTest {
whenever(storage.getBlock(merkleBlock.header.previousBlockHeaderHash)).thenReturn(null)
}

Expand All @@ -88,7 +88,7 @@ object BlockchainTest : Spek({
}

context("when block is in chain") {
beforeEach {
beforeEachTest {
whenever(storage.getBlock(merkleBlock.header.previousBlockHeaderHash)).thenReturn(block)
}

Expand Down Expand Up @@ -128,7 +128,7 @@ object BlockchainTest : Spek({

val height = 1

beforeEach {
beforeEachTest {
connectedBlock = blockchain.forceAdd(merkleBlock, height)
}

Expand All @@ -148,7 +148,7 @@ object BlockchainTest : Spek({
val blocksInChain = sortedMapOf(1 to "InChain1", 2 to "InChain2", 3 to "InChain3")
val newBlocks = sortedMapOf(4 to "NewBlock4", 5 to "NewBlock5", 6 to "NewBlock6")

beforeEach {
beforeEachTest {
mockedBlocks = MockedBlocks(storage, blockHeader).create(blocksInChain, newBlocks)
}

Expand All @@ -163,7 +163,7 @@ object BlockchainTest : Spek({
val blocksInChain = sortedMapOf(1 to "InChain1", 2 to "InChain2", 3 to "InChain3")
val newBlocks = sortedMapOf(2 to "NewBlock2", 3 to "NewBlock3", 4 to "NewBlock4")

beforeEach {
beforeEachTest {
mockedBlocks = MockedBlocks(storage, blockHeader).create(blocksInChain, newBlocks)
}

Expand All @@ -186,7 +186,7 @@ object BlockchainTest : Spek({
val blocksInChain = sortedMapOf(1 to "InChain1", 2 to "InChain2", 3 to "InChain3", 4 to "InChain4")
val newBlocks = sortedMapOf(2 to "NewBlock2", 3 to "NewBlock3")

beforeEach {
beforeEachTest {
mockedBlocks = MockedBlocks(storage, blockHeader).create(blocksInChain, newBlocks)
}

Expand All @@ -203,7 +203,7 @@ object BlockchainTest : Spek({
val blocksInChain = sortedMapOf(1 to "InChain1", 2 to "InChain2", 3 to "InChain3")
val newBlocks = sortedMapOf(2 to "NewBlock2", 3 to "NewBlock3")

beforeEach {
beforeEachTest {
mockedBlocks = MockedBlocks(storage, blockHeader).create(blocksInChain, newBlocks)
}

Expand All @@ -220,7 +220,7 @@ object BlockchainTest : Spek({
val blocksInChain = sortedMapOf(1 to "InChain1", 2 to "InChain2", 3 to "InChain3")
val newBlocks = sortedMapOf<Int, String>()

beforeEach {
beforeEachTest {
MockedBlocks(storage, blockHeader).create(blocksInChain, newBlocks)
}

Expand All @@ -237,7 +237,7 @@ object BlockchainTest : Spek({
val blocksInChain = sortedMapOf<Int, String>()
val newBlocks = sortedMapOf(2 to "NewBlock2", 3 to "NewBlock3", 4 to "NewBlock4")

beforeEach {
beforeEachTest {
mockedBlocks = MockedBlocks(storage, blockHeader).create(blocksInChain, newBlocks)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ object DataProviderTest : Spek({
context("when transactions exist with given hash and timestamp") {
val fromTransaction = mock<Transaction>()

beforeEach {
beforeEachTest {
whenever(storage.getValidOrInvalidTransaction(fromUid)).thenReturn(fromTransaction)
}

it("starts loading transactions from that transaction") {
dataProvider.transactions(fromUid, limit).test().assertOf {
dataProvider.transactions(fromUid, null, limit).test().assertOf {
verify(storage).getValidOrInvalidTransaction(fromUid)

verify(storage).getFullTransactionInfo(fromTransaction, limit)
verify(storage).getFullTransactionInfo(fromTransaction, null, limit)
}
}
}

context("when transactions does not exist with given hash and timestamp") {
beforeEach {
beforeEachTest {
whenever(storage.getValidOrInvalidTransaction(fromUid)).thenReturn(null)
}

it("do not fetch transactions with `fromHash` and `fromTimestamp`") {
dataProvider.transactions(fromUid, limit).test().assertOf {
dataProvider.transactions(fromUid, null, limit).test().assertOf {
verify(storage).getValidOrInvalidTransaction(fromUid)
verify(storage, never()).getFullTransactionInfo(null, limit)
verify(storage, never()).getFullTransactionInfo(null, null, limit)
}
}
}
Expand All @@ -64,7 +64,7 @@ object DataProviderTest : Spek({
dataProvider.transactions(null, null).test().assertOf {
verify(storage, never()).getTransaction(any())

verify(storage).getFullTransactionInfo(null, null)
verify(storage).getFullTransactionInfo(null, null, null)
}
}
}
Expand Down
Loading