Skip to content

Commit

Permalink
updates generate command
Browse files Browse the repository at this point in the history
restores compatibility with older nodes
  • Loading branch information
GambolingPangolin committed Aug 26, 2019
1 parent 67b27fa commit e00641b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.9.1

* tries to call 'generate' before falling back to 'generatetoaddress'

1.9.0

* removes 'generate' rpc call
Expand Down
2 changes: 1 addition & 1 deletion network-bitcoin.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: network-bitcoin
Version: 1.9.0
Version: 1.9.1
Synopsis: An interface to bitcoind.
Description:
This can be used to send Bitcoins, query balances, etc. It
Expand Down
13 changes: 12 additions & 1 deletion src/Network/Bitcoin/Mining.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ module Network.Bitcoin.Mining ( Client
, submitBlock
) where

import Control.Exception (catch, throw)
import Control.Monad
import Data.Aeson as A
import Network.Bitcoin.Internal
import Network.Bitcoin.Wallet (getNewAddress)
import Network.HTTP.Client (HttpException (..),
HttpExceptionContent (..),
responseStatus)
import Network.HTTP.Types (status404)

-- | Returns whether or not bitcoind is generating bitcoins.
getGenerate :: Client -- ^ bitcoind RPC client
Expand Down Expand Up @@ -72,7 +77,13 @@ generate :: Client
-- hashes of the generated blocks
-- (may be empty if used with generate 0)
generate client blocks maxTries =
getNewAddress client Nothing >>= flip (generateToAddress client blocks) maxTries
callApi client "generate" args `catch` onFail
where
args = tj blocks : maybe [] (pure . tj) maxTries
onFail (HttpExceptionRequest _ (StatusCodeException rsp _))
| responseStatus rsp == status404
= getNewAddress client Nothing >>= flip (generateToAddress client blocks) maxTries
onFail e = throw e

-- | The generatetoaddress RPC mines blocks immediately to a specified address.
-- See https://bitcoin.org/en/developer-reference#generatetoaddress for more details.
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 @@ -51,7 +51,7 @@ type RawTransaction = HexString
getRawTransaction :: Client -> TransactionID -> IO RawTransaction
getRawTransaction client txid =
callApi client "getrawtransaction" [ tj txid, tj verbose ]
where verbose = 0 :: Int
where verbose = False

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

0 comments on commit e00641b

Please sign in to comment.