forked from cgaebel/network-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
753932d
commit d14e796
Showing
4 changed files
with
64 additions
and
57 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,98 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TupleSections #-} | ||
|
||
|
||
module Main where | ||
|
||
|
||
import Data.Fixed (Fixed (MkFixed)) | ||
import Data.Vector (empty) | ||
import qualified Data.Vector as V | ||
import Data.Either (isRight) | ||
import Data.Text (Text) | ||
import Data.Vector (empty) | ||
import qualified Data.Vector as V | ||
import Network.Bitcoin | ||
import Network.Bitcoin.Internal (callApi, tj) | ||
import Test.QuickCheck | ||
import Test.QuickCheck.Monadic | ||
import Test.Tasty (TestName, TestTree, defaultMain, | ||
testGroup) | ||
import Test.Tasty.QuickCheck | ||
|
||
|
||
main :: IO () | ||
main = mapM_ qcOnce [ canListUnspent | ||
, canGetBlock | ||
, canGetOutputInfo | ||
, canGetRawTransaction | ||
, canGetAddress | ||
, canSendPayment | ||
, canEstimateFees | ||
] | ||
main = defaultMain . testGroup "network-bitcoin tests" $ | ||
[ canListUnspent | ||
, canGetBlock | ||
, canGetOutputInfo | ||
, canGetRawTransaction | ||
, canGetAddress | ||
, canSendPayment | ||
] | ||
|
||
|
||
qcOnce :: Property -> IO () | ||
qcOnce = quickCheckWith stdArgs { maxSuccess = 1 | ||
, maxSize = 1 | ||
, maxDiscardRatio = 1 | ||
} | ||
client :: IO Client | ||
client = getClient "http://127.0.0.1:18444" "bitcoinrpc" "bitcoinrpcpassword" | ||
|
||
|
||
client :: IO Client | ||
client = getClient "http://127.0.0.1:18332" "bitcoinrpc" "bitcoinrpcpassword" | ||
nbTest name = testProperty name . once . monadicIO | ||
|
||
|
||
canListUnspent :: Property | ||
canListUnspent = monadicIO $ do | ||
_ <- run $ (\c -> listUnspent c Nothing Nothing Data.Vector.empty) =<< client | ||
canListUnspent :: TestTree | ||
canListUnspent = nbTest "listUnspent" $ do | ||
_ <- run $ do | ||
c <- client | ||
listUnspent c Nothing Nothing Data.Vector.empty | ||
assert True | ||
|
||
|
||
getTopBlock :: Client -> IO Block | ||
getTopBlock c = getBlockCount c >>= getBlockHash c >>= getBlock c | ||
|
||
|
||
canGetBlock :: Property | ||
canGetBlock = monadicIO $ do | ||
run $ client >>= | ||
getTopBlock >>= | ||
assert True | ||
canGetBlock :: TestTree | ||
canGetBlock = nbTest "getBlockCount / getBlockHash / getBlock" $ do | ||
run $ client >>= getTopBlock | ||
assert True | ||
|
||
|
||
canGetRawTransaction :: Property | ||
canGetRawTransaction = monadicIO $ do | ||
canGetRawTransaction :: TestTree | ||
canGetRawTransaction = nbTest "getRawTransactionInfo" $ do | ||
run $ do | ||
c <- client | ||
b <- getTopBlock c | ||
getRawTransactionInfo c (subTransactions b V.! 0) >>= print | ||
getRawTransactionInfo c (subTransactions b V.! 0) | ||
assert True | ||
|
||
|
||
canGetOutputInfo :: Property | ||
canGetOutputInfo = monadicIO $ do | ||
canGetOutputInfo :: TestTree | ||
canGetOutputInfo = nbTest "getOutputInfo" $ do | ||
run $ do | ||
c <- client | ||
b <- getTopBlock c | ||
getOutputInfo c (subTransactions b V.! 0) 0 >>= print | ||
getOutputInfo c (subTransactions b V.! 0) 0 | ||
assert True | ||
|
||
|
||
canEstimateFees :: Property | ||
canEstimateFees = monadicIO $ do | ||
run $ client >>= \c -> | ||
estimateSmartFee c 10 Nothing >>= print | ||
assert True | ||
|
||
|
||
canGetAddress :: Property | ||
canGetAddress = monadicIO $ do | ||
canGetAddress :: TestTree | ||
canGetAddress = nbTest "getNewAddress" $ do | ||
run $ do | ||
c <- client | ||
getNewAddress c Nothing >>= print | ||
getNewAddress c Nothing | ||
assert True | ||
|
||
|
||
canSendPayment :: Property | ||
canSendPayment = monadicIO $ do | ||
run $ do | ||
c <- client | ||
canSendPayment :: TestTree | ||
canSendPayment = nbTest "send payment" $ do | ||
c <- run client | ||
bal <- run $ getBalance c | ||
amt <- pick . suchThat arbitrary $ \x -> x < bal && x > 0 | ||
|
||
(addr, recv) <- run $ do | ||
addr <- getNewAddress c Nothing | ||
sendToAddress c addr (MkFixed 10000000) Nothing Nothing >>= print | ||
assert True | ||
sendToAddress c addr amt Nothing Nothing | ||
_ :: [Text] <- callApi c "generate" [ tj (2 :: Int) ] | ||
(addr,) <$> listReceivedByAddress c | ||
|
||
assert . V.elem (addr, amt) . fmap f $ recv | ||
where | ||
f = (,) <$> recvAddress <*> recvAmount |