haskoin-store-data 0.25.3 → 0.25.4
raw patch · 3 files changed
+124/−97 lines, 3 files
Files
- haskoin-store-data.cabal +2/−2
- src/Haskoin/Store/Data.hs +99/−88
- test/Haskoin/Store/DataSpec.hs +23/−7
haskoin-store-data.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: bdee4b95fe36df338078b745febb903d0e2e19a659216ec224a4371b1a362303+-- hash: 39a15f6c1fa31c97f8734f31c16c78e7b569a0b97cee9f2dabc4a1aee5353ccf name: haskoin-store-data-version: 0.25.3+version: 0.25.4 synopsis: Data for Haskoin Store description: Data and its (de)serialisation functions for Haskoin Store category: Bitcoin, Finance, Network
src/Haskoin/Store/Data.hs view
@@ -15,6 +15,8 @@ -- * Block Data , BlockData(..)+ , blockDataToJSON+ , blockDataToEncoding , confirmed -- * Transactions@@ -391,45 +393,48 @@ -- ^ block subsidy } deriving (Show, Read, Eq, Ord, Generic, Serialize, Hashable, NFData) -instance ToJSON BlockData where- toJSON bv =- object- [ "hash" .= headerHash (blockDataHeader bv)- , "height" .= blockDataHeight bv- , "mainchain" .= blockDataMainChain bv- , "previous" .= prevBlock (blockDataHeader bv)- , "time" .= blockTimestamp (blockDataHeader bv)- , "version" .= blockVersion (blockDataHeader bv)- , "bits" .= blockBits (blockDataHeader bv)- , "nonce" .= bhNonce (blockDataHeader bv)- , "size" .= blockDataSize bv- , "tx" .= blockDataTxs bv- , "merkle" .= TxHash (merkleRoot (blockDataHeader bv))- , "subsidy" .= blockDataSubsidy bv- , "fees" .= blockDataFees bv- , "outputs" .= blockDataOutputs bv- , "work" .= show (blockDataWork bv)- , "weight" .= blockDataWeight bv- ]- toEncoding bv =- pairs- ( "hash" `pair` text (blockHashToHex (headerHash (blockDataHeader bv)))- <> "height" .= blockDataHeight bv- <> "mainchain" .= blockDataMainChain bv- <> "previous" .= prevBlock (blockDataHeader bv)- <> "time" .= blockTimestamp (blockDataHeader bv)- <> "version" .= blockVersion (blockDataHeader bv)- <> "bits" .= blockBits (blockDataHeader bv)- <> "nonce" .= bhNonce (blockDataHeader bv)- <> "size" .= blockDataSize bv- <> "tx" .= blockDataTxs bv- <> "merkle" `pair` text (txHashToHex (TxHash (merkleRoot (blockDataHeader bv))))- <> "subsidy" .= blockDataSubsidy bv- <> "fees" .= blockDataFees bv- <> "outputs" .= blockDataOutputs bv- <> "work" .= blockDataWork bv- <> "weight" .= blockDataWeight bv)+blockDataToJSON :: Network -> BlockData -> Value+blockDataToJSON net bv =+ object $+ [ "hash" .= headerHash (blockDataHeader bv)+ , "height" .= blockDataHeight bv+ , "mainchain" .= blockDataMainChain bv+ , "previous" .= prevBlock (blockDataHeader bv)+ , "time" .= blockTimestamp (blockDataHeader bv)+ , "version" .= blockVersion (blockDataHeader bv)+ , "bits" .= blockBits (blockDataHeader bv)+ , "nonce" .= bhNonce (blockDataHeader bv)+ , "size" .= blockDataSize bv+ , "tx" .= blockDataTxs bv+ , "merkle" .= TxHash (merkleRoot (blockDataHeader bv))+ , "subsidy" .= blockDataSubsidy bv+ , "fees" .= blockDataFees bv+ , "outputs" .= blockDataOutputs bv+ , "work" .= blockDataWork bv+ ] <>+ ["weight" .= blockDataWeight bv | getSegWit net] +blockDataToEncoding :: Network -> BlockData -> Encoding+blockDataToEncoding net bv =+ pairs+ ( "hash" `pair` text (blockHashToHex (headerHash (blockDataHeader bv)))+ <> "height" .= blockDataHeight bv+ <> "mainchain" .= blockDataMainChain bv+ <> "previous" .= prevBlock (blockDataHeader bv)+ <> "time" .= blockTimestamp (blockDataHeader bv)+ <> "version" .= blockVersion (blockDataHeader bv)+ <> "bits" .= blockBits (blockDataHeader bv)+ <> "nonce" .= bhNonce (blockDataHeader bv)+ <> "size" .= blockDataSize bv+ <> "tx" .= blockDataTxs bv+ <> "merkle" `pair` text (txHashToHex (TxHash (merkleRoot (blockDataHeader bv))))+ <> "subsidy" .= blockDataSubsidy bv+ <> "fees" .= blockDataFees bv+ <> "outputs" .= blockDataOutputs bv+ <> "work" .= blockDataWork bv+ <> if getSegWit net then "weight" .= blockDataWeight bv else mempty+ )+ instance FromJSON BlockData where parseJSON = A.withObject "blockdata" $ \o -> do@@ -447,7 +452,7 @@ fees <- o .: "fees" outputs <- o .: "outputs" work <- o .: "work"- weight <- o .: "weight"+ weight <- o .:? "weight" .!= 0 return BlockData { blockDataHeader =@@ -497,33 +502,33 @@ , inputAmount = val , inputWitness = wit } =- object- [ "coinbase" .= False- , "txid" .= oph- , "output" .= opi- , "sigscript" .= String (encodeHex ss)- , "sequence" .= sq- , "pkscript" .= String (encodeHex ps)- , "value" .= val- , "address" .= scriptToAddrJSON net ps- , "witness" .= fmap (map encodeHex) wit- ]-storeInputToJSON _ StoreCoinbase { inputPoint = OutPoint oph opi- , inputSequence = sq- , inputSigScript = ss- , inputWitness = wit- } =- object- [ "coinbase" .= True- , "txid" .= oph- , "output" .= opi- , "sigscript" .= String (encodeHex ss)- , "sequence" .= sq- , "pkscript" .= Null- , "value" .= Null- , "address" .= Null- , "witness" .= fmap (map encodeHex) wit- ]+ object $+ [ "coinbase" .= False+ , "txid" .= oph+ , "output" .= opi+ , "sigscript" .= String (encodeHex ss)+ , "sequence" .= sq+ , "pkscript" .= String (encodeHex ps)+ , "value" .= val+ , "address" .= scriptToAddrJSON net ps+ ] <>+ ["witness" .= fmap (map encodeHex) wit | getSegWit net]+storeInputToJSON net StoreCoinbase { inputPoint = OutPoint oph opi+ , inputSequence = sq+ , inputSigScript = ss+ , inputWitness = wit+ } =+ object $+ [ "coinbase" .= True+ , "txid" .= oph+ , "output" .= opi+ , "sigscript" .= String (encodeHex ss)+ , "sequence" .= sq+ , "pkscript" .= Null+ , "value" .= Null+ , "address" .= Null+ ] <>+ ["witness" .= fmap (map encodeHex) wit | getSegWit net] storeInputToEncoding :: Network -> StoreInput -> Encoding storeInputToEncoding net StoreInput { inputPoint = OutPoint oph opi@@ -542,10 +547,12 @@ <> "pkscript" `pair` text (encodeHex ps) <> "value" .= val <> "address" `pair` scriptToAddrEncoding net ps- <> "witness" .= fmap (map encodeHex) wit+ <> if getSegWit net+ then "witness" .= fmap (map encodeHex) wit+ else mempty ) -storeInputToEncoding _ StoreCoinbase { inputPoint = OutPoint oph opi+storeInputToEncoding net StoreCoinbase { inputPoint = OutPoint oph opi , inputSequence = sq , inputSigScript = ss , inputWitness = wit@@ -559,7 +566,9 @@ <> "pkscript" `pair` null_ <> "value" `pair` null_ <> "address" `pair` null_- <> "witness" .= fmap (map encodeHex) wit+ <> if getSegWit net+ then "witness" .= fmap (map encodeHex) wit+ else mempty ) instance FromJSON StoreInput where@@ -777,30 +786,30 @@ transactionToJSON :: Network -> Transaction -> Value transactionToJSON net dtx =- object- [ "txid" .= txHash (transactionData dtx)- , "size" .= B.length (S.encode (transactionData dtx))- , "version" .= transactionVersion dtx- , "locktime" .= transactionLockTime dtx- , "fee" .=- if any isCoinbase (transactionInputs dtx)- then 0- else inv - outv- , "inputs" .= map (storeInputToJSON net) (transactionInputs dtx)- , "outputs" .= map (storeOutputToJSON net) (transactionOutputs dtx)- , "block" .= transactionBlock dtx- , "deleted" .= transactionDeleted dtx- , "time" .= transactionTime dtx- , "rbf" .= transactionRBF dtx- , "weight" .= w- ]+ object $+ [ "txid" .= txHash (transactionData dtx)+ , "size" .= B.length (S.encode (transactionData dtx))+ , "version" .= transactionVersion dtx+ , "locktime" .= transactionLockTime dtx+ , "fee" .=+ if any isCoinbase (transactionInputs dtx)+ then 0+ else inv - outv+ , "inputs" .= map (storeInputToJSON net) (transactionInputs dtx)+ , "outputs" .= map (storeOutputToJSON net) (transactionOutputs dtx)+ , "block" .= transactionBlock dtx+ , "deleted" .= transactionDeleted dtx+ , "time" .= transactionTime dtx+ , "rbf" .= transactionRBF dtx+ ] <>+ ["weight" .= w | getSegWit net] where inv = sum (map inputAmount (transactionInputs dtx)) outv = sum (map outputAmount (transactionOutputs dtx)) w = let b = B.length $ S.encode (transactionData dtx) {txWitness = []} x = B.length $ S.encode (transactionData dtx)- in b * 3 + x+ in b * 3 + x transactionToEncoding :: Network -> Transaction -> Encoding transactionToEncoding net dtx =@@ -816,7 +825,9 @@ <> "deleted" .= transactionDeleted dtx <> "time" .= transactionTime dtx <> "rbf" .= transactionRBF dtx- <> "weight" .= w+ <> if getSegWit net+ then "weight" .= w+ else mempty ) where inv = sum (map inputAmount (transactionInputs dtx))
test/Haskoin/Store/DataSpec.hs view
@@ -34,7 +34,9 @@ XPubBal (..), XPubSpec (..), XPubSummary (..), XPubUnspent (..), balanceParseJSON, balanceToEncoding,- balanceToJSON, transactionToEncoding,+ balanceToJSON, blockDataToEncoding,+ blockDataToJSON,+ transactionToEncoding, transactionToJSON, unspentToEncoding, unspentToJSON, xPubUnspentToEncoding, xPubUnspentToJSON)@@ -79,14 +81,28 @@ prop "identity for block ref" $ \x -> testJSON (x :: BlockRef) prop "identity for unspent" . forAll arbitraryNetData $ \(net, x) -> testNetJSON parseJSON (unspentToJSON net) (unspentToEncoding net) x- prop "identity for block data" $ \x -> testJSON (x :: BlockData)+ prop "identity for block data" . forAll arbitraryNetData $ \(net, x) ->+ let x' =+ if getSegWit net+ then x+ else x {blockDataWeight = 0}+ in testNetJSON+ parseJSON+ (blockDataToJSON net)+ (blockDataToEncoding net)+ x' prop "identity for spender" $ \x -> testJSON (x :: Spender) prop "identity for transaction" . forAll arbitraryNetData $ \(net, x) ->- testNetJSON- parseJSON- (transactionToJSON net)- (transactionToEncoding net)- x+ let f i = i {inputWitness = Nothing}+ x' =+ if getSegWit net+ then x+ else x {transactionInputs = map f (transactionInputs x)}+ in testNetJSON+ parseJSON+ (transactionToJSON net)+ (transactionToEncoding net)+ x' prop "identity for xpub summary" $ \x -> testJSON (x :: XPubSummary) prop "identity for xpub unspent" . forAll arbitraryNetData $ \(net, x) -> testNetJSON