module Network.Bitcoin.Mining ( Auth(..)
, getGenerate
, setGenerate
, getHashesPerSec
, MiningInfo(..)
, getMiningInfo
, HashData(..)
, getWork
, solveBlock
, Transaction(..)
, CoinBaseAux(..)
, BlockTemplate(..)
, getBlockTemplate
, submitBlock
) where
import Data.Aeson as A
import Control.Applicative
import Control.Monad
import Network.Bitcoin.Internal
getGenerate :: Auth
-> IO Bool
getGenerate auth = callApi auth "getgenerate" []
setGenerate :: Auth
-> Bool
-> Maybe Int
-> IO ()
setGenerate auth onOff Nothing =
unNil <$> callApi auth "setgenerate" [ tj onOff ]
setGenerate auth onOff (Just limit) =
unNil <$> callApi auth "setgenerate" [ tj onOff, tj limit ]
getHashesPerSec :: Auth -> IO Integer
getHashesPerSec auth = callApi auth "gethashespersec" []
data MiningInfo =
MiningInfo {
nBlocks :: Integer
, currentBlockSize :: Integer
, currentBlockTransaction :: Integer
, difficulty :: Double
, miningErrors :: Text
, isGenerating :: Bool
, generationProcessorLimit :: Integer
, hashesPerSecond :: Integer
, pooledTransactions :: Integer
, miningOnTestNetwork :: Bool
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON MiningInfo where
parseJSON (Object o) = MiningInfo <$> o .: "blocks"
<*> o .: "currentblocksize"
<*> o .: "currentblocktx"
<*> o .: "difficulty"
<*> o .: "errors"
<*> o .: "generate"
<*> o .: "genproclimit"
<*> o .: "hashespersec"
<*> o .: "pooledtx"
<*> o .: "testnet"
parseJSON _ = mzero
getMiningInfo :: Auth -> IO MiningInfo
getMiningInfo auth = callApi auth "getmininginfo" []
data HashData =
HashData { blockData :: HexString
, hdTarget :: HexString
, hash1 :: HexString
, midstate :: HexString
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON HashData where
parseJSON (Object o) = HashData <$> o .: "data"
<*> o .: "target"
<*> o .: "hash1"
<*> o .: "midstate"
parseJSON _ = mzero
instance ToJSON HashData where
toJSON (HashData dat tar has mid) = object ["data" .= dat, "target" .= tar, "hash1" .= has, "midstate" .= mid]
getWork :: Auth -> IO HashData
getWork auth = callApi auth "getwork" []
solveBlock :: Auth -> HexString -> IO Bool
solveBlock auth data_ = callApi auth "getwork" [ tj data_ ]
data Transaction =
Transaction { txnData :: HexString
, txnHash :: HexString
, depends :: Vector Integer
, txnFee :: Maybe Integer
, sigOps :: Integer
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON Transaction where
parseJSON (Object o) = Transaction <$> o .: "data"
<*> o .: "hash"
<*> o .: "depends"
<*> o .:? "fee"
<*> o .: "sigops"
parseJSON _ = mzero
data CoinBaseAux = CoinBaseAux { cbFlags :: HexString
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON CoinBaseAux where
parseJSON (Object o) = CoinBaseAux <$> o .: "flags"
parseJSON _ = mzero
data BlockTemplate =
BlockTemplate { blockVersion :: Integer
, previousBlockHash :: HexString
, transactionsToInclude :: Vector Transaction
, coinBaseAux :: CoinBaseAux
, coinBaseValue :: Integer
, btTarget :: HexString
, minTime :: Integer
, nonceRange :: HexString
, sigopLimit :: Integer
, sizeLimit :: Integer
, curTime :: Integer
, btBits :: HexString
, btHeight :: Integer
}
deriving ( Show, Read, Ord, Eq )
instance FromJSON BlockTemplate where
parseJSON (Object o) = BlockTemplate <$> o .: "version"
<*> o .: "previousblockhash"
<*> o .: "transactions"
<*> o .: "coinbaseaux"
<*> o .: "coinbasevalue"
<*> o .: "target"
<*> o .: "mintime"
<*> o .: "noncerange"
<*> o .: "sigoplimit"
<*> o .: "sizelimit"
<*> o .: "curtime"
<*> o .: "bits"
<*> o .: "height"
parseJSON _ = mzero
getBlockTemplate :: Auth -> IO BlockTemplate
getBlockTemplate auth = callApi auth "getblocktemplate" []
data StupidReturnValue = SRV { unStupid :: Bool }
instance FromJSON StupidReturnValue where
parseJSON Null = return $ SRV True
parseJSON _ = return $ SRV False
submitBlock :: Auth
-> HexString
-> IO Bool
submitBlock auth block = unStupid <$> callApi auth "submitblock" [ tj block ]