-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from horizontalsystems/master
Merge master into production
- Loading branch information
Showing
6 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...shkit/src/main/kotlin/io/horizontalsystems/bitcoincash/blocks/validators/ForkValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.horizontalsystems.bitcoincash.blocks.validators | ||
|
||
import io.horizontalsystems.bitcoincore.blocks.validators.BlockValidatorException | ||
import io.horizontalsystems.bitcoincore.blocks.validators.IBlockValidator | ||
import io.horizontalsystems.bitcoincore.models.Block | ||
|
||
class ForkValidator(private val forkHeight: Int, private val expectedBlockHash: ByteArray, private val concreteBlockValidator: IBlockValidator) : IBlockValidator { | ||
|
||
override fun isBlockValidatable(block: Block, previousBlock: Block): Boolean { | ||
return block.height == forkHeight | ||
} | ||
|
||
override fun validate(block: Block, previousBlock: Block) { | ||
if (!block.headerHash.contentEquals(expectedBlockHash)) { | ||
throw BlockValidatorException.WrongBlockHash(expectedBlockHash, block.headerHash) | ||
} | ||
|
||
concreteBlockValidator.validate(block, previousBlock) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...t/src/test/kotlin/io/horizontalsystems/bitcoincash/blocks/validators/ForkValidatorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.horizontalsystems.bitcoincash.blocks.validators | ||
|
||
import com.nhaarman.mockito_kotlin.mock | ||
import com.nhaarman.mockito_kotlin.whenever | ||
import io.horizontalsystems.bitcoincore.blocks.validators.BlockValidatorException | ||
import io.horizontalsystems.bitcoincore.blocks.validators.IBlockValidator | ||
import io.horizontalsystems.bitcoincore.models.Block | ||
import org.junit.Assert | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.assertThrows | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.specification.describe | ||
|
||
object ForkValidatorTest : Spek({ | ||
val forkHeight = 100 | ||
val expectedBlockHash = byteArrayOf(1, 2, 3) | ||
val concreteBlockValidator = mock<IBlockValidator>() | ||
val validator by memoized { ForkValidator(forkHeight, expectedBlockHash, concreteBlockValidator) } | ||
|
||
describe("#isBlockValidatable") { | ||
val block = mock<Block>() | ||
|
||
it("is true when block height is equal to fork height") { | ||
whenever(block.height).thenReturn(forkHeight) | ||
Assert.assertTrue(validator.isBlockValidatable(block, mock())) | ||
} | ||
|
||
it("is false when block height is not equal to fork height") { | ||
whenever(block.height).thenReturn(104) | ||
Assert.assertFalse(validator.isBlockValidatable(block, mock())) | ||
} | ||
|
||
} | ||
|
||
describe("#validate") { | ||
val block = mock<Block>() | ||
|
||
it("validates without any error when block hash is equal to expected block hash") { | ||
whenever(block.headerHash).thenReturn(expectedBlockHash) | ||
Assertions.assertDoesNotThrow { | ||
validator.validate(block, mock()) | ||
} | ||
} | ||
|
||
it("throws exception when block hash is not equal to expected block hash") { | ||
whenever(block.headerHash).thenReturn(byteArrayOf(3, 2, 1)) | ||
assertThrows<BlockValidatorException.WrongBlockHash> { | ||
validator.validate(block, mock()) | ||
} | ||
} | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters