Skip to content

Commit 7f00cc1

Browse files
authored
Merge pull request #363 from horizontalsystems/master
Merge master into production
2 parents f54ed84 + a9c781f commit 7f00cc1

6 files changed

Lines changed: 89 additions & 5 deletions

File tree

bitcoincashkit/src/main/kotlin/io/horizontalsystems/bitcoincash/BitcoinCashKit.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import android.database.sqlite.SQLiteDatabase
55
import io.horizontalsystems.bitcoincash.blocks.BitcoinCashBlockValidatorHelper
66
import io.horizontalsystems.bitcoincash.blocks.validators.DAAValidator
77
import io.horizontalsystems.bitcoincash.blocks.validators.EDAValidator
8+
import io.horizontalsystems.bitcoincash.blocks.validators.ForkValidator
89
import io.horizontalsystems.bitcoincore.AbstractKit
910
import io.horizontalsystems.bitcoincore.BitcoinCore
1011
import io.horizontalsystems.bitcoincore.BitcoinCoreBuilder
1112
import io.horizontalsystems.bitcoincore.blocks.validators.LegacyDifficultyAdjustmentValidator
13+
import io.horizontalsystems.bitcoincore.extensions.toReversedByteArray
1214
import io.horizontalsystems.bitcoincore.managers.BitcoinCashAddressSelector
1315
import io.horizontalsystems.bitcoincore.managers.InsightApi
1416
import io.horizontalsystems.bitcoincore.models.TransactionInfo
@@ -81,7 +83,12 @@ class BitcoinCashKit : AbstractKit {
8183
if (networkType == NetworkType.MainNet) {
8284
val blockHelper = BitcoinCashBlockValidatorHelper(storage)
8385

84-
bitcoinCore.addBlockValidator(DAAValidator(targetSpacing, blockHelper))
86+
val svForkHeight = 556767
87+
val abcForkBlockHash = "0000000000000000004626ff6e3b936941d341c5932ece4357eeccac44e6d56c".toReversedByteArray()
88+
89+
val daaValidator = DAAValidator(targetSpacing, blockHelper)
90+
bitcoinCore.addBlockValidator(ForkValidator(svForkHeight, abcForkBlockHash, daaValidator))
91+
bitcoinCore.addBlockValidator(daaValidator)
8592
bitcoinCore.addBlockValidator(LegacyDifficultyAdjustmentValidator(blockHelper, heightInterval, targetTimespan, maxTargetBits))
8693
bitcoinCore.addBlockValidator(EDAValidator(maxTargetBits, blockHelper, network.bip44CheckpointBlock.height))
8794
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.horizontalsystems.bitcoincash.blocks.validators
2+
3+
import io.horizontalsystems.bitcoincore.blocks.validators.BlockValidatorException
4+
import io.horizontalsystems.bitcoincore.blocks.validators.IBlockValidator
5+
import io.horizontalsystems.bitcoincore.models.Block
6+
7+
class ForkValidator(private val forkHeight: Int, private val expectedBlockHash: ByteArray, private val concreteBlockValidator: IBlockValidator) : IBlockValidator {
8+
9+
override fun isBlockValidatable(block: Block, previousBlock: Block): Boolean {
10+
return block.height == forkHeight
11+
}
12+
13+
override fun validate(block: Block, previousBlock: Block) {
14+
if (!block.headerHash.contentEquals(expectedBlockHash)) {
15+
throw BlockValidatorException.WrongBlockHash(expectedBlockHash, block.headerHash)
16+
}
17+
18+
concreteBlockValidator.validate(block, previousBlock)
19+
}
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.horizontalsystems.bitcoincash.blocks.validators
2+
3+
import com.nhaarman.mockito_kotlin.mock
4+
import com.nhaarman.mockito_kotlin.whenever
5+
import io.horizontalsystems.bitcoincore.blocks.validators.BlockValidatorException
6+
import io.horizontalsystems.bitcoincore.blocks.validators.IBlockValidator
7+
import io.horizontalsystems.bitcoincore.models.Block
8+
import org.junit.Assert
9+
import org.junit.jupiter.api.Assertions
10+
import org.junit.jupiter.api.assertThrows
11+
import org.spekframework.spek2.Spek
12+
import org.spekframework.spek2.style.specification.describe
13+
14+
object ForkValidatorTest : Spek({
15+
val forkHeight = 100
16+
val expectedBlockHash = byteArrayOf(1, 2, 3)
17+
val concreteBlockValidator = mock<IBlockValidator>()
18+
val validator by memoized { ForkValidator(forkHeight, expectedBlockHash, concreteBlockValidator) }
19+
20+
describe("#isBlockValidatable") {
21+
val block = mock<Block>()
22+
23+
it("is true when block height is equal to fork height") {
24+
whenever(block.height).thenReturn(forkHeight)
25+
Assert.assertTrue(validator.isBlockValidatable(block, mock()))
26+
}
27+
28+
it("is false when block height is not equal to fork height") {
29+
whenever(block.height).thenReturn(104)
30+
Assert.assertFalse(validator.isBlockValidatable(block, mock()))
31+
}
32+
33+
}
34+
35+
describe("#validate") {
36+
val block = mock<Block>()
37+
38+
it("validates without any error when block hash is equal to expected block hash") {
39+
whenever(block.headerHash).thenReturn(expectedBlockHash)
40+
Assertions.assertDoesNotThrow {
41+
validator.validate(block, mock())
42+
}
43+
}
44+
45+
it("throws exception when block hash is not equal to expected block hash") {
46+
whenever(block.headerHash).thenReturn(byteArrayOf(3, 2, 1))
47+
assertThrows<BlockValidatorException.WrongBlockHash> {
48+
validator.validate(block, mock())
49+
}
50+
}
51+
}
52+
53+
})

bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/blocks/InitialBlockDownload.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ class InitialBlockDownload(
167167
syncedPeers.add(peer)
168168

169169
blockSyncer.downloadCompleted()
170-
syncStateListener.onSyncFinish()
171-
peer.sendMempoolMessage()
170+
if (peerManager.isHalfSynced()) {
171+
syncStateListener.onSyncFinish()
172+
}
173+
peer.sendMempoolMessage()
172174
logger.info("Peer synced ${peer.host}")
173175
syncPeer = null
174176
assignNextSyncPeer()

bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/blocks/validators/BlockValidatorException.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.horizontalsystems.bitcoincore.blocks.validators
22

3+
import io.horizontalsystems.bitcoincore.extensions.toReversedHex
4+
35
open class BlockValidatorException(msg: String) : RuntimeException(msg) {
46
class NoHeader : BlockValidatorException("No Header")
57
class NoCheckpointBlock : BlockValidatorException("No Checkpoint Block")
@@ -8,4 +10,5 @@ open class BlockValidatorException(msg: String) : RuntimeException(msg) {
810
class NotEqualBits : BlockValidatorException("Not Equal Bits")
911
class NotDifficultyTransitionEqualBits : BlockValidatorException("Not Difficulty Transition Equal Bits")
1012
class InvalidProofOfWork : BlockValidatorException("Invalid Prove of Work")
13+
class WrongBlockHash(expected: ByteArray, actual: ByteArray) : BlockValidatorException("Wrong Block Hash ${actual.toReversedHex()} vs expected ${expected.toReversedHex()}")
1114
}

bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/core/KitStateProvider.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ class KitStateProvider : ISyncStateListener {
6060
}
6161

6262
if (progress >= 1) {
63-
syncState = KitState.Synced
63+
syncState = KitState.Syncing(1.0)
6464
} else {
6565
syncState = KitState.Syncing(progress)
6666
}
67-
6867
}
6968
}

0 commit comments

Comments
 (0)