diff --git a/CHANGELOG b/CHANGELOG index 0c16050..7e825f7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +1.9.1 + + * tries to call 'generate' before falling back to 'generatetoaddress' + 1.9.0 * removes 'generate' rpc call diff --git a/network-bitcoin.cabal b/network-bitcoin.cabal index 2e94c9e..fe88817 100644 --- a/network-bitcoin.cabal +++ b/network-bitcoin.cabal @@ -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 diff --git a/src/Network/Bitcoin/Mining.hs b/src/Network/Bitcoin/Mining.hs index dd81949..d121c60 100644 --- a/src/Network/Bitcoin/Mining.hs +++ b/src/Network/Bitcoin/Mining.hs @@ -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 @@ -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. diff --git a/src/Network/Bitcoin/RawTransaction.hs b/src/Network/Bitcoin/RawTransaction.hs index 084999d..c0d32d5 100644 --- a/src/Network/Bitcoin/RawTransaction.hs +++ b/src/Network/Bitcoin/RawTransaction.hs @@ -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.