Skip to content

Commit

Permalink
Merge pull request cgaebel#3 from GambolingPangolin/master
Browse files Browse the repository at this point in the history
removes deprecated rpc call 'generate'
  • Loading branch information
wraithm authored Aug 5, 2019
2 parents 4b5f5cb + 67b27fa commit 40057a2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ dist-newstyle/
cabal-dev/
cabal.sandbox.config
.cabal-sandbox/
.stack-work
tags
9 changes: 9 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
1.9.0

* removes 'generate' rpc call
* adds test case for 'generateToAddress' and updates tests

1.8.5

* ...

1.1.0

* Added missing HashData fields
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
network-bitcoin
====

See the [Hackage documentation](http://hackage.haskell.org/package/network-bitcoin).
This library supports [Bitcoin Core][1] `v0.18` only.

See the [Hackage documentation][2].

Testing
----
Expand All @@ -12,3 +14,6 @@ Invoke `bitcoind` with:
```shell
$ bitcoind -regtest -rpcuser=bitcoinrpc -rpcpassword=bitcoinrpcpassword -rpcport=18444
```

[1]: https://github.com/bitcoin/bitcoin
[2]: http://hackage.haskell.org/package/network-bitcoin
4 changes: 2 additions & 2 deletions network-bitcoin.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: network-bitcoin
Version: 1.8.5
Version: 1.9.0
Synopsis: An interface to bitcoind.
Description:
This can be used to send Bitcoins, query balances, etc. It
Expand All @@ -18,7 +18,7 @@ License: BSD3
License-file: LICENSE
Author: Michael Hendricks <[email protected]>
Clark Gaebel <[email protected]>
Maintainer: Matt Wraith <[email protected]>
Maintainer: Matt Wraith <[email protected]>
Homepage: http://github.com/bitnomial/network-bitcoin
Bug-reports: http://github.com/bitnomial/network-bitcoin/issues
Copyright: 2012 Michael Hendricks <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions src/Network/Bitcoin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Network.Bitcoin
, dumpPrivateKey
-- * Mining Operations
, generate
, generateToAddress
, getGenerate
, setGenerate
, getHashesPerSec
Expand Down
12 changes: 5 additions & 7 deletions src/Network/Bitcoin/Mining.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Network.Bitcoin.Mining ( Client
import Control.Monad
import Data.Aeson as A
import Network.Bitcoin.Internal
import Network.Bitcoin.Wallet (getNewAddress)

-- | Returns whether or not bitcoind is generating bitcoins.
getGenerate :: Client -- ^ bitcoind RPC client
Expand Down Expand Up @@ -70,10 +71,8 @@ generate :: Client
-> IO [HexString] -- ^ An array containing the block header
-- hashes of the generated blocks
-- (may be empty if used with generate 0)
generate client blocks Nothing =
callApi client "generate" [ tj blocks ]
generate client blocks (Just maxTries) =
callApi client "generate" [ tj blocks, tj maxTries]
generate client blocks maxTries =
getNewAddress client Nothing >>= flip (generateToAddress client blocks) maxTries

-- | The generatetoaddress RPC mines blocks immediately to a specified address.
-- See https://bitcoin.org/en/developer-reference#generatetoaddress for more details.
Expand Down Expand Up @@ -189,8 +188,7 @@ instance FromJSON Transaction where
<*> o .: "sigops"
parseJSON _ = mzero

data CoinBaseAux = CoinBaseAux { cbFlags :: HexString
}
newtype CoinBaseAux = CoinBaseAux { cbFlags :: HexString }
deriving ( Show, Read, Ord, Eq )

instance FromJSON CoinBaseAux where
Expand Down Expand Up @@ -255,7 +253,7 @@ getBlockTemplate client = callApi client "getblocktemplate" []
-- the string "rejected" on failure.
--
-- We use 'StupidReturnValue' to parse this ridiculous API.
data StupidReturnValue = SRV { unStupid :: Bool }
newtype StupidReturnValue = SRV { unStupid :: Bool }

instance FromJSON StupidReturnValue where
parseJSON Null = return $ SRV True
Expand Down
2 changes: 1 addition & 1 deletion src/Network/Bitcoin/RawTransaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ instance FromJSON RawTransactionInfo where
getRawTransactionInfo :: Client -> TransactionID -> IO RawTransactionInfo
getRawTransactionInfo client txid =
callApi client "getrawtransaction" [ tj txid, tj verbose ]
where verbose = 1 :: Int
where verbose = True

data UnspentTransaction =
UnspentTransaction { unspentTransactionId :: TransactionID
Expand Down
2 changes: 1 addition & 1 deletion src/Network/Bitcoin/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ data ReceivedByAddress =

instance FromJSON ReceivedByAddress where
parseJSON (Object o) = ReceivedByAddress <$> o .: "address"
<*> o .: "account"
<*> o .: "label"
<*> o .: "amount"
<*> o .: "confirmations"
parseJSON _ = mzero
Expand Down
24 changes: 18 additions & 6 deletions src/Test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
module Main where


import Control.Monad (void)
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 qualified Network.Bitcoin.Mining as M
import Test.QuickCheck
import Test.QuickCheck.Monadic
import Test.Tasty (TestName, TestTree, defaultMain,
Expand All @@ -21,7 +23,8 @@ import Test.Tasty.QuickCheck

main :: IO ()
main = defaultMain . testGroup "network-bitcoin tests" $
[ canListUnspent
[ canGenerateBlocks
, canListUnspent
, canGetBlock
, canGetOutputInfo
, canGetRawTransaction
Expand All @@ -37,9 +40,18 @@ client = getClient "http://127.0.0.1:18444" "bitcoinrpc" "bitcoinrpcpassword"
nbTest name = testProperty name . once . monadicIO


canGenerateBlocks :: TestTree
canGenerateBlocks = nbTest "generateToAddress" $ do
void . run $ do
c <- client
rewardAddr <- getNewAddress c Nothing
M.generateToAddress c 101 rewardAddr Nothing
assert True


canListUnspent :: TestTree
canListUnspent = nbTest "listUnspent" $ do
_ <- run $ do
void . run $ do
c <- client
listUnspent c Nothing Nothing Data.Vector.empty
assert True
Expand All @@ -51,8 +63,8 @@ getTopBlock c = getBlockCount c >>= getBlockHash c >>= getBlock c

canGetBlock :: TestTree
canGetBlock = nbTest "getBlockCount / getBlockHash / getBlock" $ do
run $ client >>= getTopBlock
assert True
run $ client >>= getTopBlock
assert True


canGetRawTransaction :: TestTree
Expand Down Expand Up @@ -83,14 +95,14 @@ canGetAddress = nbTest "getNewAddress" $ do

canSendPayment :: TestTree
canSendPayment = nbTest "send payment" $ do
c <- run client
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 amt Nothing Nothing
_ :: [Text] <- callApi c "generate" [ tj (2 :: Int) ]
M.generate c 6 Nothing
(addr,) <$> listReceivedByAddress c

assert . V.elem (addr, amt) . fmap f $ recv
Expand Down

0 comments on commit 40057a2

Please sign in to comment.