From 9c001629233e9948439a0a31343df8e19a70c255 Mon Sep 17 00:00:00 2001 From: Ben Carman Date: Wed, 8 Jan 2020 16:15:55 -0600 Subject: [PATCH] Increase core test coverage --- .../core/config/NetworkParametersTest.scala | 8 +++++ .../core/currency/CurrencyUnitTest.scala | 15 +++++++- .../bitcoins/core/gcs/FilterTypeTest.scala | 1 + .../core/p2p/ServiceIdentifierTest.scala | 11 ++++++ .../core/p2p/TypeIdentifierTest.scala | 4 +++ .../core/protocol/BitcoinAddressTest.scala | 36 +++++++++++++++++-- .../transaction/TransactionTest.scala | 6 ++-- .../script/RawScriptPubKeyParserTest.scala | 6 +++- .../script/RawScriptSignatureParserTest.scala | 6 +++- .../org/bitcoins/core/protocol/Address.scala | 2 +- .../interpreter/ScriptInterpreter.scala | 3 +- 11 files changed, 88 insertions(+), 10 deletions(-) diff --git a/core-test/src/test/scala/org/bitcoins/core/config/NetworkParametersTest.scala b/core-test/src/test/scala/org/bitcoins/core/config/NetworkParametersTest.scala index 41fee06052..07b27fb185 100644 --- a/core-test/src/test/scala/org/bitcoins/core/config/NetworkParametersTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/config/NetworkParametersTest.scala @@ -21,4 +21,12 @@ class NetworkParametersTest extends BitcoinSUnitTest { it must "create the correct magic network bytes for regtest" in { BitcoinSUtil.encodeHex(RegTest.magicBytes) must be("fabfb5da") } + + it must "get the correct Network from string" in { + assert(Networks.fromString("mainnet").contains(MainNet)) + assert(Networks.fromString("testnet").contains(TestNet3)) + assert(Networks.fromString("regtest").contains(RegTest)) + assert(Networks.fromString("").isEmpty) + assert(Networks.fromString("craig wright is a fraud").isEmpty) + } } diff --git a/core-test/src/test/scala/org/bitcoins/core/currency/CurrencyUnitTest.scala b/core-test/src/test/scala/org/bitcoins/core/currency/CurrencyUnitTest.scala index 0d5a4fe0f0..3bb9bdca3d 100644 --- a/core-test/src/test/scala/org/bitcoins/core/currency/CurrencyUnitTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/currency/CurrencyUnitTest.scala @@ -72,7 +72,7 @@ class CurrencyUnitTest extends BitcoinSUnitTest { } } - it must "subtract satoshis " in { + it must "subtract satoshis" in { forAll(CurrencyUnitGenerator.satoshis, CurrencyUnitGenerator.satoshis) { (num1: Satoshis, num2: Satoshis) => val result: Try[Int64] = Try(Int64(num1.toBigInt - num2.toBigInt)) @@ -150,6 +150,7 @@ class CurrencyUnitTest extends BitcoinSUnitTest { forAll(CurrencyUnitGenerator.satoshis) { satoshis => val b = Bitcoins(satoshis) assert(b.satoshis == satoshis) + assert(CurrencyUnits.toSatoshis(b) == satoshis) } } @@ -164,4 +165,16 @@ class CurrencyUnitTest extends BitcoinSUnitTest { else actual.isFailure && expected.isFailure } } + + it must "correctly compare two currency units" in { + forAll(CurrencyUnitGenerator.satoshis, CurrencyUnitGenerator.satoshis) { + (num1, num2) => + if (num1 > num2) + assert(num1.compare(num2) == 1) + else if (num1 < num2) + assert(num1.compare(num2) == -1) + else + assert(num1.compare(num2) == 0) + } + } } diff --git a/core-test/src/test/scala/org/bitcoins/core/gcs/FilterTypeTest.scala b/core-test/src/test/scala/org/bitcoins/core/gcs/FilterTypeTest.scala index 461b0b516e..11e9d65c86 100644 --- a/core-test/src/test/scala/org/bitcoins/core/gcs/FilterTypeTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/gcs/FilterTypeTest.scala @@ -15,5 +15,6 @@ class FilterTypeTest extends BitcoinSUnitTest { assert(FilterType.getCode(FilterType.Basic) == 0) assert(FilterType.byCode(0) == FilterType.Basic) assertThrows[IllegalArgumentException](FilterType.byCode(1)) + assertThrows[IllegalArgumentException](FilterType.getCode(FilterType.fromHex("ffff"))) } } diff --git a/core-test/src/test/scala/org/bitcoins/core/p2p/ServiceIdentifierTest.scala b/core-test/src/test/scala/org/bitcoins/core/p2p/ServiceIdentifierTest.scala index b40bf62a67..14bfd1b2ae 100644 --- a/core-test/src/test/scala/org/bitcoins/core/p2p/ServiceIdentifierTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/p2p/ServiceIdentifierTest.scala @@ -37,4 +37,15 @@ class ServiceIdentifierTest extends BitcoinSUnitTest { it must "parse NODE_NETWORK_LIMITED" in { assert(ServiceIdentifier.NODE_NETWORK_LIMITED.nodeNetworkLimited) } + + it must "correctly get a ServiceIdentifier from string" in { + assert(ServiceIdentifier.fromString("NETWORK") == ServiceIdentifier.NODE_NETWORK) + assert(ServiceIdentifier.fromString("NETWORK_LIMITED") == ServiceIdentifier.NODE_NETWORK_LIMITED) + assert(ServiceIdentifier.fromString("WITNESS") == ServiceIdentifier.NODE_WITNESS) + assert(ServiceIdentifier.fromString("BLOOM") == ServiceIdentifier.NODE_BLOOM) + assert(ServiceIdentifier.fromString("GETUTXO") == ServiceIdentifier.NODE_GET_UTXO) + assert(ServiceIdentifier.fromString("COMPACT_FILTERS") == ServiceIdentifier.NODE_COMPACT_FILTERS) + assert(ServiceIdentifier.fromString("XTHIN") == ServiceIdentifier.NODE_XTHIN) + assertThrows[IllegalArgumentException](ServiceIdentifier.fromString("this is invalid")) + } } diff --git a/core-test/src/test/scala/org/bitcoins/core/p2p/TypeIdentifierTest.scala b/core-test/src/test/scala/org/bitcoins/core/p2p/TypeIdentifierTest.scala index a54a873ef1..16ab3a4ee4 100644 --- a/core-test/src/test/scala/org/bitcoins/core/p2p/TypeIdentifierTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/p2p/TypeIdentifierTest.scala @@ -5,6 +5,10 @@ import org.bitcoins.testkit.util.BitcoinSUnitTest class TypeIdentifierTest extends BitcoinSUnitTest { + it must "Create a MsgUnassigned" in { + assert(TypeIdentifier(100000).isInstanceOf[MsgUnassigned]) + } + "MsgTx" must "serialize to 01000000" in { MsgTx.hex must be("01000000") } diff --git a/core-test/src/test/scala/org/bitcoins/core/protocol/BitcoinAddressTest.scala b/core-test/src/test/scala/org/bitcoins/core/protocol/BitcoinAddressTest.scala index 486119eafb..720422af4c 100644 --- a/core-test/src/test/scala/org/bitcoins/core/protocol/BitcoinAddressTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/protocol/BitcoinAddressTest.scala @@ -1,8 +1,8 @@ package org.bitcoins.core.protocol import org.bitcoins.core.config.{MainNet, RegTest, TestNet3} -import org.bitcoins.core.crypto.Sha256Hash160Digest -import org.bitcoins.core.protocol.script.ScriptPubKey +import org.bitcoins.core.crypto.{ECPublicKey, Sha256Hash160Digest} +import org.bitcoins.core.protocol.script.{EmptyScriptPubKey, P2PKHScriptPubKey, P2SHScriptPubKey, P2WPKHWitnessSPKV0, ScriptPubKey, WitnessScriptPubKeyV0} import org.bitcoins.testkit.util.BitcoinSUnitTest import scala.util.{Failure, Success, Try} @@ -90,4 +90,36 @@ class BitcoinAddressTest extends BitcoinSUnitTest { val addr = P2SHAddress(scriptPubKey, MainNet) addr must be(BitcoinAddress("3P14159f73E4gFr7JterCCQh9QjiTjiZrG").get) } + + it must "create a bech32 address from a WitnessScriptPubKey" in { + val scriptPubKey = P2WPKHWitnessSPKV0(ECPublicKey.freshPublicKey) + assert(Bech32Address.fromScriptPubKey(scriptPubKey, RegTest).isSuccess) + } + + it must "fail to create a bech32 address from an invalid ScriptPubKey" in { + assert(Bech32Address.fromScriptPubKey(EmptyScriptPubKey, RegTest).isFailure) + } + + it must "create an address from a P2PKHScriptPubKey" in { + val scriptPubKey = P2PKHScriptPubKey(ECPublicKey.freshPublicKey) + assert(P2PKHAddress.fromScriptPubKey(scriptPubKey, RegTest).isSuccess) + } + + it must "fail to create a P2PKHAddress address from an invalid ScriptPubKey" in { + assert(P2PKHAddress.fromScriptPubKey(EmptyScriptPubKey, RegTest).isFailure) + } + + it must "create an address from a P2SHScriptPubKey" in { + val scriptPubKey = P2SHScriptPubKey(EmptyScriptPubKey) + assert(P2SHAddress.fromScriptPubKey(scriptPubKey, RegTest).isSuccess) + } + + it must "fail to create a P2SHScriptPubKey address from an invalid ScriptPubKey" in { + assert(P2SHAddress.fromScriptPubKey(EmptyScriptPubKey, RegTest).isFailure) + } + + it must "create an address from a ScriptPubKey" in { + val scriptPubKey = P2SHScriptPubKey(EmptyScriptPubKey) + assert(Address.fromScriptPubKey(scriptPubKey, RegTest).isSuccess) + } } diff --git a/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionTest.scala b/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionTest.scala index 21fc582678..eca4b0a99c 100644 --- a/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/protocol/transaction/TransactionTest.scala @@ -16,7 +16,6 @@ import org.bitcoins.core.script.result.ScriptOk import org.bitcoins.core.serializers.transaction.RawBaseTransactionParser import org.bitcoins.core.util.{CryptoUtil, TestUtil} import org.bitcoins.testkit.util.BitcoinSUnitTest -import org.slf4j.LoggerFactory import spray.json._ import scodec.bits._ @@ -71,10 +70,11 @@ class TransactionTest extends BitcoinSUnitTest { "0000000000000000000000000000000000000000000000000000000000000000") } - it must "calculate the size of a tranaction correctly" in { + it must "calculate the size of a transaction correctly" in { val rawTx = TestUtil.rawTransaction val tx = Transaction(rawTx) //size is in bytes so divide by 2 + assert(tx.size == tx.totalSize) tx.size must be(rawTx.size / 2) } @@ -135,7 +135,7 @@ class TransactionTest extends BitcoinSUnitTest { tx2.size must be(216) tx2.weight must be(534) tx2.vsize must be(134) - + tx2.baseSize must be(106) } it must "parse a transaction with an OP_PUSHDATA4 op code but not enough data to push" in { diff --git a/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptPubKeyParserTest.scala b/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptPubKeyParserTest.scala index 845049c537..ecf565e1f6 100644 --- a/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptPubKeyParserTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptPubKeyParserTest.scala @@ -1,6 +1,6 @@ package org.bitcoins.core.serializers.script -import org.bitcoins.core.protocol.script.ScriptPubKey +import org.bitcoins.core.protocol.script.{EmptyScriptPubKey, ScriptPubKey} import org.bitcoins.core.script.bitwise.OP_EQUALVERIFY import org.bitcoins.core.script.constant._ import org.bitcoins.core.script.crypto.{OP_CHECKSIG, OP_HASH160} @@ -21,6 +21,10 @@ class RawScriptPubKeyParserTest extends BitcoinSUnitTest { TestUtil.rawScriptPubKey) } + it must "read an EmptyScriptPubKey" in { + assert(RawScriptPubKeyParser.read(ByteVector.empty) == EmptyScriptPubKey) + } + it must "read a raw scriptPubKey and give us the expected asm" in { val scriptPubKey = RawScriptPubKeyParser.read(TestUtil.rawP2PKHScriptPubKey) val expectedAsm: Seq[ScriptToken] = diff --git a/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptSignatureParserTest.scala b/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptSignatureParserTest.scala index a3af9708a9..ab8f7373f6 100644 --- a/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptSignatureParserTest.scala +++ b/core-test/src/test/scala/org/bitcoins/core/serializers/script/RawScriptSignatureParserTest.scala @@ -1,6 +1,6 @@ package org.bitcoins.core.serializers.script -import org.bitcoins.core.protocol.script.ScriptSignature +import org.bitcoins.core.protocol.script.{EmptyScriptSignature, ScriptSignature} import org.bitcoins.core.script.constant._ import org.bitcoins.core.util.{BitcoinSUtil, TestUtil} import org.bitcoins.testkit.util.BitcoinSUnitTest @@ -21,6 +21,10 @@ class RawScriptSignatureParserTest extends BitcoinSUnitTest { encode(RawScriptSignatureParser.write(scriptSig)) must be(rawScriptSig) } + it must "read an EmptyScriptSignature" in { + assert(RawScriptSignatureParser.read(ByteVector.empty) == EmptyScriptSignature) + } + it must "read then write a raw script sig" in { //from this tx //https://tbtc.blockr.io/api/v1/tx/raw/bdc221db675c06dbee2ae75d33e31cad4e2555efea10c337ff32c8cdf97f8e74 diff --git a/core/src/main/scala/org/bitcoins/core/protocol/Address.scala b/core/src/main/scala/org/bitcoins/core/protocol/Address.scala index 8ca5e32042..8abb7fe7ab 100644 --- a/core/src/main/scala/org/bitcoins/core/protocol/Address.scala +++ b/core/src/main/scala/org/bitcoins/core/protocol/Address.scala @@ -190,7 +190,7 @@ object Bech32Address extends AddressFactory[Bech32Address] { np: NetworkParameters): Try[Bech32Address] = spk match { case witSPK: WitnessScriptPubKey => - Bech32Address.fromScriptPubKey(witSPK, np) + Success(Bech32Address(witSPK, np)) case x @ (_: P2PKScriptPubKey | _: P2PKHScriptPubKey | _: P2PKWithTimeoutScriptPubKey | _: MultiSignatureScriptPubKey | _: P2SHScriptPubKey | _: LockTimeScriptPubKey | diff --git a/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala b/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala index 1aae516845..23d216a4fd 100644 --- a/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala +++ b/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala @@ -1078,6 +1078,7 @@ sealed abstract class ScriptInterpreter extends BitcoinSLogger { val inputOutputsNotZero = !(transaction.inputs.isEmpty || transaction.outputs.isEmpty) val txNotLargerThanBlock = transaction.bytes.size < Consensus.maxBlockSize + val txNotHeavierThanBlock = transaction.weight < Consensus.maxBlockWeight val outputsSpendValidAmountsOfMoney = !transaction.outputs.exists(o => o.value < CurrencyUnits.zero || o.value > Consensus.maxMoney) @@ -1094,7 +1095,7 @@ sealed abstract class ScriptInterpreter extends BitcoinSLogger { } else { !transaction.inputs.exists(_.previousOutput == EmptyTransactionOutPoint) } - inputOutputsNotZero && txNotLargerThanBlock && outputsSpendValidAmountsOfMoney && + inputOutputsNotZero && txNotLargerThanBlock && txNotHeavierThanBlock && outputsSpendValidAmountsOfMoney && allOutputsValidMoneyRange && noDuplicateInputs && isValidScriptSigForCoinbaseTx }