packages feed

bitcoin-block 0.12.0 → 0.13.0

raw patch · 3 files changed

+60/−59 lines, 3 filesdep +lensPVP ok

version bump matches the API change (PVP)

Dependencies added: lens

API changes (from Hackage documentation)

- Data.Bitcoin.Block: bhNonce :: BlockHeader -> Word32
- Data.Bitcoin.Block: blockBits :: BlockHeader -> Word32
- Data.Bitcoin.Block: blockCoinbaseTx :: Block -> Coinbase
- Data.Bitcoin.Block: blockHeader :: Block -> BlockHeader
- Data.Bitcoin.Block: blockTimestamp :: BlockHeader -> Word32
- Data.Bitcoin.Block: blockTxns :: Block -> [Transaction]
- Data.Bitcoin.Block: blockVersion :: BlockHeader -> Word32
- Data.Bitcoin.Block: merkleRoot :: BlockHeader -> Word256
- Data.Bitcoin.Block: prevBlock :: BlockHeader -> BlockHash
+ Data.Bitcoin.Block: _bhNonce :: BlockHeader -> Word32
+ Data.Bitcoin.Block: _blockBits :: BlockHeader -> Word32
+ Data.Bitcoin.Block: _blockCoinbaseTx :: Block -> Coinbase
+ Data.Bitcoin.Block: _blockHeader :: Block -> BlockHeader
+ Data.Bitcoin.Block: _blockTimestamp :: BlockHeader -> Word32
+ Data.Bitcoin.Block: _blockTxns :: Block -> [Transaction]
+ Data.Bitcoin.Block: _blockVersion :: BlockHeader -> Word32
+ Data.Bitcoin.Block: _merkleRoot :: BlockHeader -> Word256
+ Data.Bitcoin.Block: _prevBlock :: BlockHeader -> BlockHash

Files

bitcoin-block.cabal view
@@ -1,6 +1,6 @@ name: bitcoin-block
 category: Network, Finance
-version: 0.12.0
+version: 0.13.0
 license: MIT
 license-file: LICENSE
 copyright: (c) 2015 Leon Mergen
@@ -34,6 +34,7 @@                      , hexstring
                      , cryptohash
                      , largeword
+                     , lens
                      
                      , bitcoin-tx
                      , bitcoin-types
src/Data/Bitcoin/Block.hs view
@@ -4,13 +4,14 @@                           , Block (..)
                           , BlockHeader (..) ) where
 
-import qualified Data.Binary                    as B (encode)
+import           Control.Lens             ((^.))
+import qualified Data.Binary              as B (encode)
 
-import qualified Data.ByteString                as BS (reverse)
-import qualified Data.ByteString.Lazy           as BSL (toStrict)
+import qualified Data.ByteString          as BS (reverse)
+import qualified Data.ByteString.Lazy     as BSL (toStrict)
 
-import qualified Crypto.Hash.SHA256             as Sha256
-import qualified Data.HexString                 as HS
+import qualified Crypto.Hash.SHA256       as Sha256
+import qualified Data.HexString           as HS
 
 import           Data.Bitcoin.Block.Types
 
@@ -32,4 +33,4 @@   let sha256d = Sha256.hash . Sha256.hash
       bytes   = BSL.toStrict . B.encode
 
-  in HS.fromBytes . BS.reverse . sha256d . bytes $ blockHeader block
+  in HS.fromBytes . BS.reverse . sha256d . bytes $ (block ^. blockHeader)
src/Data/Bitcoin/Block/Types.hs view
@@ -1,56 +1,24 @@-{-# LANGUAGE FlexibleInstances  #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE TemplateHaskell   #-}
 
 module Data.Bitcoin.Block.Types where
 
-import Data.Binary ( Binary, get, put )
-import Data.Binary.Get ( getByteString
-                       , getWord32le )
-
-import Data.Binary.Put ( putByteString
-                       , putWord32le )
-
-import Control.Monad ( forM_
-                     , replicateM )
-
-import qualified Data.HexString as HS
-import qualified Data.ByteString as BS
-
-import Data.Word ( Word32 )
-import Data.LargeWord ( Word256 )
-
-import Data.Bitcoin.Types ( VarInt (..)
-                          , BlockHash )
-import qualified Data.Bitcoin.Transaction as Btc ( Transaction (..)
-                                                 , Coinbase )
-
-
--- | Data type describing a block in the bitcoin protocol.
-data Block = Block {
-  -- | Header information for this block.
-  blockHeader     :: BlockHeader,
-
-  -- | Coinbase transaction of this block.
-  blockCoinbaseTx :: Btc.Coinbase,
+import           Data.Binary              (Binary, get, put)
+import           Data.Binary.Get          (getByteString, getWord32le)
 
-  -- | List of transactions pertaining to this block.
-  blockTxns       :: [Btc.Transaction]
+import           Data.Binary.Put          (putByteString, putWord32le)
 
-  } deriving (Eq, Show)
+import           Control.Lens.TH          (makeLenses)
+import           Control.Monad            (forM_, replicateM)
 
-instance Binary Block where
+import qualified Data.ByteString          as BS
+import qualified Data.HexString           as HS
 
-    get = do
-        header     <- get
-        (VarInt c) <- get
-        cb         <- get
-        txs        <- replicateM (fromIntegral (c-1)) get
-        return $ Block header cb txs
+import           Data.LargeWord           (Word256)
+import           Data.Word                (Word32)
 
-    put (Block h cb txs) = do
-        put h
-        put $ VarInt $ fromIntegral $ length txs + 1
-        put cb
-        forM_ txs put
+import qualified Data.Bitcoin.Transaction as Btc (Coinbase, Transaction (..))
+import           Data.Bitcoin.Types       (BlockHash, VarInt (..))
 
 -- | Data type recording information on a 'Block'. The hash of a block is
 --   defined as the hash of this data structure. The block mining process
@@ -58,34 +26,35 @@ --   'BlockHeader' and/or additional randomness in the 'Btc.Coinbase' of this
 --   'Block'. Variations in the 'Btc.Coinbase' will result in different merkle
 --   roots in the 'BlockHeader'.
-
 data BlockHeader = BlockHeader {
 
   -- | Block version information, based on the version of the
   --   software creating this block.
-  blockVersion   :: Word32,
+  _blockVersion   :: Word32,
 
   -- | Hash of the previous block (parent) referenced by this
   --   block.
-  prevBlock      :: BlockHash,
+  _prevBlock      :: BlockHash,
 
   -- | Root of the merkle tree of all transactions pertaining
   --   to this block.
-  merkleRoot     :: Word256,
+  _merkleRoot     :: Word256,
 
   -- | Unix timestamp recording when this block was created
-  blockTimestamp :: Word32,
+  _blockTimestamp :: Word32,
 
   -- | The difficulty target being used for this block
-  blockBits      :: Word32,
+  _blockBits      :: Word32,
 
   -- | A random nonce used to generate this block. Additional
   --   randomness is included in the coinbase transaction of
   --   this block.
-  bhNonce        :: Word32
+  _bhNonce        :: Word32
 
   } deriving (Eq, Show)
 
+makeLenses ''BlockHeader
+
 instance Binary BlockHeader where
 
     get = do
@@ -104,3 +73,33 @@       putWord32le   bt
       putWord32le   bb
       putWord32le   n
+
+-- | Data type describing a block in the bitcoin protocol.
+data Block = Block {
+  -- | Header information for this block.
+  _blockHeader     :: BlockHeader,
+
+  -- | Coinbase transaction of this block.
+  _blockCoinbaseTx :: Btc.Coinbase,
+
+  -- | List of transactions pertaining to this block.
+  _blockTxns       :: [Btc.Transaction]
+
+  } deriving (Eq, Show)
+
+makeLenses ''Block
+
+instance Binary Block where
+
+    get = do
+        header     <- get
+        (VarInt c) <- get
+        cb         <- get
+        txs        <- replicateM (fromIntegral (c-1)) get
+        return $ Block header cb txs
+
+    put (Block h cb txs) = do
+        put h
+        put $ VarInt $ fromIntegral $ length txs + 1
+        put cb
+        forM_ txs put