Skip to content

Commit 62ab499

Browse files
committed
fix test suites
1 parent 2566085 commit 62ab499

25 files changed

+438
-60
lines changed

bitcoin.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.34.4.
3+
-- This file has been generated from package.yaml by hpack version 0.35.0.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -134,6 +134,7 @@ test-suite spec
134134
Bitcoin.Keys.MnemonicSpec
135135
Bitcoin.KeysSpec
136136
Bitcoin.NetworkSpec
137+
Bitcoin.Orphans
137138
Bitcoin.ScriptSpec
138139
Bitcoin.Transaction.PartialSpec
139140
Bitcoin.Transaction.TaprootSpec

hie.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cradle:
2-
stack:
3-
- path: "./src"
4-
component: "bitcoin:lib"
2+
cabal:
3+
- path: "src"
4+
component: "lib:bitcoin"
55

6-
- path: "./test"
6+
- path: "test"
77
component: "bitcoin:test:spec"

src/Bitcoin/Block/Headers.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module Bitcoin.Block.Headers (
4545
bip34,
4646
validVersion,
4747
lastNoMinDiff,
48-
computeAsertBits,
48+
computeAssertBits,
4949
nextPowWorkRequired,
5050
calcNextWork,
5151
isValidPOW,
@@ -677,13 +677,13 @@ maxTarget :: Integer
677677
maxTarget = fst $ decodeCompact maxBits
678678

679679

680-
computeAsertBits ::
680+
computeAssertBits ::
681681
Integer ->
682682
Word32 ->
683683
Integer ->
684684
Integer ->
685685
Word32
686-
computeAsertBits halflife anchor_bits time_diff height_diff =
686+
computeAssertBits halflife anchor_bits time_diff height_diff =
687687
if e2 >= 0 && e2 < 65536
688688
then
689689
if g4 == 0

test/Bitcoin/AddressSpec.hs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import Bitcoin.Data
99
import Bitcoin.Keys
1010
import Bitcoin.Util
1111
import Bitcoin.Util.Arbitrary
12-
import Bitcoin.UtilSpec
12+
import Bitcoin.UtilSpec hiding (spec)
1313
import Data.Aeson
14+
import Data.Aeson.Encoding
1415
import Data.Aeson.Types (Parser)
1516
import Data.ByteString (ByteString)
1617
import qualified Data.ByteString as BS (append, empty, pack)
@@ -27,6 +28,24 @@ import Test.Hspec.QuickCheck
2728
import Test.QuickCheck
2829

2930

31+
addrToJSON :: Network -> Address -> Value
32+
addrToJSON net a = toJSON (addrToText net a)
33+
34+
35+
addrToEncoding :: Network -> Address -> Encoding
36+
addrToEncoding net = maybe null_ text . addrToText net
37+
38+
39+
-- | JSON parsing for Bitcoin addresses. Works with 'Base58', and
40+
-- 'Bech32'.
41+
addrFromJSON :: Network -> Value -> Parser Address
42+
addrFromJSON net =
43+
withText "address" $ \t ->
44+
case textToAddr net t of
45+
Nothing -> fail "could not decode address"
46+
Just x -> return x
47+
48+
3049
serialVals :: [SerialBox]
3150
serialVals = [SerialBox arbitraryAddressAll]
3251

@@ -40,7 +59,6 @@ netVals =
4059
[NetBox (addrToJSON, addrToEncoding, addrFromJSON, arbitraryNetAddress)]
4160

4261

43-
4462
spec :: Spec
4563
spec = do
4664
testIdentity serialVals readVals [] netVals

test/Bitcoin/BlockSpec.hs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ module Bitcoin.BlockSpec (
77
import Bitcoin.Block
88
import Bitcoin.Constants
99
import Bitcoin.Data
10+
import Bitcoin.Orphans ()
1011
import Bitcoin.Transaction
12+
import Bitcoin.Util
1113
import Bitcoin.Util.Arbitrary
1214
import Bitcoin.UtilSpec hiding (spec)
15+
import Control.Monad (MonadPlus (..), forM_, unless, (<=<))
1316
import Control.Monad.Trans.State.Strict
17+
import Data.Aeson
18+
import Data.Aeson.Encoding
19+
import qualified Data.ByteString.Lazy as BL
20+
import Data.Bytes.Get
21+
import Data.Bytes.Put (runPutL, runPutS)
22+
import Data.Bytes.Serial
1423
import Data.Either (fromRight)
1524
import Data.Maybe (fromJust)
1625
import Data.String (fromString)
@@ -96,11 +105,11 @@ spec = do
96105
describe "compact number" $ do
97106
it "compact number local vectors" testCompact
98107
it "compact number imported vectors" testCompactBitcoinCore
99-
describe "asert" $
108+
describe "assert" $
100109
mapM_
101110
( \x ->
102-
asertTests $
103-
"test_vectors_aserti3-2d_run" ++ printf "%02d" x ++ ".txt"
111+
assertTests $
112+
"test_vectors_asserti3-2d_run" ++ printf "%02d" x ++ ".txt"
104113
)
105114
[(1 :: Int) .. 12]
106115
describe "helper functions" $ do
@@ -378,42 +387,42 @@ testSubsidy net = go (2 * 50 * 100 * 1000 * 1000) 0
378387
go subsidy (halvings + 1)
379388

380389

381-
data AsertBlock = AsertBlock Int Integer Integer Word32
390+
data AssertBlock = AssertBlock Int Integer Integer Word32
382391

383392

384-
data AsertVector = AsertVector String Integer Integer Word32 [AsertBlock]
393+
data AssertVector = AssertVector String Integer Integer Word32 [AssertBlock]
385394

386395

387-
readAsertVector :: FilePath -> IO AsertVector
388-
readAsertVector p = do
396+
readAssertVector :: FilePath -> IO AssertVector
397+
readAssertVector p = do
389398
(d : ah : apt : ab : _ : _ : _ : _ : xs) <- lines <$> readFile ("data/" ++ p)
390399
let desc = drop 16 d
391400
anchor_height = read (words ah !! 3)
392401
anchor_parent_time = read (words apt !! 4)
393402
anchor_nbits = read (words ab !! 3)
394403
blocks = map (f . words) (init xs)
395404
return $
396-
AsertVector
405+
AssertVector
397406
desc
398407
anchor_height
399408
anchor_parent_time
400409
anchor_nbits
401410
blocks
402411
where
403-
f [i, h, t, g] = AsertBlock (read i) (read h) (read t) (read g)
412+
f [i, h, t, g] = AssertBlock (read i) (read h) (read t) (read g)
404413
f _ = undefined
405414

406415

407-
asertTests :: FilePath -> SpecWith ()
408-
asertTests file = do
409-
v@(AsertVector d _ _ _ _) <- runIO $ readAsertVector file
410-
it d $ testAsertBits v
416+
assertTests :: FilePath -> SpecWith ()
417+
assertTests file = do
418+
v@(AssertVector d _ _ _ _) <- runIO $ readAssertVector file
419+
it d $ testAssertBits v
411420

412421

413-
testAsertBits :: AsertVector -> Assertion
414-
testAsertBits (AsertVector _ anchor_height anchor_parent_time anchor_bits blocks) =
415-
forM_ blocks $ \(AsertBlock _ h t g) ->
416-
computeAsertBits
422+
testAssertBits :: AssertVector -> Assertion
423+
testAssertBits (AssertVector _ anchor_height anchor_parent_time anchor_bits blocks) =
424+
forM_ blocks $ \(AssertBlock _ h t g) ->
425+
computeAssertBits
417426
(2 * 24 * 60 * 60)
418427
anchor_bits
419428
(t - anchor_parent_time)

test/Bitcoin/Keys/ExtendedSpec.hs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33

44
module Bitcoin.Keys.ExtendedSpec (spec) where
55

6+
import Bitcoin.Address
7+
import Bitcoin.Constants
8+
import Bitcoin.Keys
9+
import Bitcoin.Orphans ()
10+
import Bitcoin.Util
11+
import Bitcoin.Util.Arbitrary
12+
import Bitcoin.UtilSpec (JsonBox (..), NetBox (..), ReadBox (..), SerialBox (..), customCerealID, testIdentity)
613
import Control.Monad (forM_)
714
import Data.Aeson as A
15+
import Data.Aeson.Encoding
16+
import Data.Aeson.Types
817
import Data.Bits ((.&.))
918
import qualified Data.ByteString.Lazy.Char8 as B8
1019
import Data.Bytes.Get
@@ -16,12 +25,6 @@ import Data.String (fromString)
1625
import Data.String.Conversions (cs)
1726
import Data.Text (Text)
1827
import Data.Word (Word32)
19-
import Bitcoin.Address
20-
import Bitcoin.Constants
21-
import Bitcoin.Keys
22-
import Bitcoin.Util
23-
import Bitcoin.Util.Arbitrary
24-
import Bitcoin.UtilSpec (customCerealID)
2528
import Test.HUnit (Assertion, assertBool, assertEqual)
2629
import Test.Hspec
2730
import Test.Hspec.QuickCheck
@@ -72,6 +75,27 @@ netVals =
7275
, genNetData (snd <$> arbitraryXPubKey)
7376
)
7477
]
78+
where
79+
xPrvToJSON :: Network -> XPrvKey -> Value
80+
xPrvToJSON net = A.String . xPrvExport net
81+
xPrvToEncoding :: Network -> XPrvKey -> Encoding
82+
xPrvToEncoding net = text . xPrvExport net
83+
xPrvFromJSON :: Network -> Value -> Parser XPrvKey
84+
xPrvFromJSON net =
85+
withText "xprv" $ \t ->
86+
case xPrvImport net t of
87+
Nothing -> fail "could not read xprv"
88+
Just x -> return x
89+
xPubFromJSON :: Network -> Value -> Parser XPubKey
90+
xPubFromJSON net =
91+
withText "xpub" $ \t ->
92+
case xPubImport net t of
93+
Nothing -> fail "could not read xpub"
94+
Just x -> return x
95+
xPubToJSON :: Network -> XPubKey -> Value
96+
xPubToJSON net = A.String . xPubExport net
97+
xPubToEncoding :: Network -> XPubKey -> Encoding
98+
xPubToEncoding net = text . xPubExport net
7599

76100

77101
spec :: Spec

test/Bitcoin/KeysSpec.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
module Bitcoin.KeysSpec (spec) where
44

5+
import Bitcoin.Address
6+
import Bitcoin.Constants
7+
import Bitcoin.Crypto
8+
import Bitcoin.Keys
9+
import Bitcoin.Orphans ()
10+
import Bitcoin.Script
11+
import Bitcoin.Util
12+
import Bitcoin.Util.Arbitrary
13+
import Bitcoin.UtilSpec hiding (spec)
514
import Control.Lens
615
import Control.Monad
716
import Data.Aeson as A
@@ -16,14 +25,6 @@ import qualified Data.Serialize as S
1625
import Data.String (fromString)
1726
import Data.String.Conversions (cs)
1827
import Data.Text (Text)
19-
import Bitcoin.Address
20-
import Bitcoin.Constants
21-
import Bitcoin.Crypto
22-
import Bitcoin.Keys
23-
import Bitcoin.Script
24-
import Bitcoin.Util
25-
import Bitcoin.Util.Arbitrary
26-
import Bitcoin.UtilSpec (readTestFile)
2728
import Test.HUnit
2829
import Test.Hspec
2930
import Test.Hspec.QuickCheck

test/Bitcoin/NetworkSpec.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import Bitcoin.Network
99
import Bitcoin.Transaction
1010
import Bitcoin.Util
1111
import Bitcoin.Util.Arbitrary
12-
import Bitcoin.UtilSpec
13-
import Bitcoin.UtilSpec (customCerealID)
12+
import Bitcoin.UtilSpec hiding (spec)
1413
import Data.Bytes.Get
1514
import Data.Bytes.Put
1615
import Data.Bytes.Serial

0 commit comments

Comments
 (0)