Skip to content

Commit e00641b

Browse files
updates generate command
restores compatibility with older nodes
1 parent 67b27fa commit e00641b

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.9.1
2+
3+
* tries to call 'generate' before falling back to 'generatetoaddress'
4+
15
1.9.0
26

37
* removes 'generate' rpc call

network-bitcoin.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name: network-bitcoin
2-
Version: 1.9.0
2+
Version: 1.9.1
33
Synopsis: An interface to bitcoind.
44
Description:
55
This can be used to send Bitcoins, query balances, etc. It

src/Network/Bitcoin/Mining.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ module Network.Bitcoin.Mining ( Client
3131
, submitBlock
3232
) where
3333

34+
import Control.Exception (catch, throw)
3435
import Control.Monad
3536
import Data.Aeson as A
3637
import Network.Bitcoin.Internal
3738
import Network.Bitcoin.Wallet (getNewAddress)
39+
import Network.HTTP.Client (HttpException (..),
40+
HttpExceptionContent (..),
41+
responseStatus)
42+
import Network.HTTP.Types (status404)
3843

3944
-- | Returns whether or not bitcoind is generating bitcoins.
4045
getGenerate :: Client -- ^ bitcoind RPC client
@@ -72,7 +77,13 @@ generate :: Client
7277
-- hashes of the generated blocks
7378
-- (may be empty if used with generate 0)
7479
generate client blocks maxTries =
75-
getNewAddress client Nothing >>= flip (generateToAddress client blocks) maxTries
80+
callApi client "generate" args `catch` onFail
81+
where
82+
args = tj blocks : maybe [] (pure . tj) maxTries
83+
onFail (HttpExceptionRequest _ (StatusCodeException rsp _))
84+
| responseStatus rsp == status404
85+
= getNewAddress client Nothing >>= flip (generateToAddress client blocks) maxTries
86+
onFail e = throw e
7687

7788
-- | The generatetoaddress RPC mines blocks immediately to a specified address.
7889
-- See https://bitcoin.org/en/developer-reference#generatetoaddress for more details.

src/Network/Bitcoin/RawTransaction.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type RawTransaction = HexString
5151
getRawTransaction :: Client -> TransactionID -> IO RawTransaction
5252
getRawTransaction client txid =
5353
callApi client "getrawtransaction" [ tj txid, tj verbose ]
54-
where verbose = 0 :: Int
54+
where verbose = False
5555

5656
-- | A transaction into an account. This can either be a coinbase transaction,
5757
-- or a standard transaction with another account.

0 commit comments

Comments
 (0)