Skip to content

Commit

Permalink
Merge pull request #49 from input-output-hk/jonathan/wrapping
Browse files Browse the repository at this point in the history
Apply miscellaneous coding style fixes.
  • Loading branch information
jonathanknowles authored Mar 12, 2019
2 parents 0722b47 + aa28cd1 commit 1a920d7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 57 deletions.
20 changes: 13 additions & 7 deletions src/Cardano/Wallet/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ import qualified Data.Set as Set

decodeAddress :: CBOR.Decoder s Address
decodeAddress = do
_ <- CBOR.decodeListLenCanonicalOf 2 -- CRC Protection Wrapper
tag <- CBOR.decodeTag -- Mysterious hard-coded tag cardano-sl seems to so much like
bytes <- CBOR.decodeBytes -- Addr Root + Attributes + Type
_ <- CBOR.decodeListLenCanonicalOf 2
-- CRC Protection Wrapper
tag <- CBOR.decodeTag
-- Mysterious hard-coded tag cardano-sl seems to so much like
bytes <- CBOR.decodeBytes
-- Addr Root + Attributes + Type
crc <- CBOR.decodeWord32 -- CRC
-- NOTE 1:
-- Treating addresses as a blob here, so we just re-encode them as such
Expand Down Expand Up @@ -145,9 +148,11 @@ decodeBlockHeader = do
CBOR.decodeListLenCanonicalOf 2
t <- CBOR.decodeWordCanonical
case t of
0 -> decodeGenesisBlockHeader
1 -> decodeMainBlockHeader
_ -> fail $ "decodeBlockHeader: unknown block header constructor: " <> show t
0 -> decodeGenesisBlockHeader
1 -> decodeMainBlockHeader
_ ->
fail $ "decodeBlockHeader: unknown block header constructor: " <>
show t

decodeBlockVersion :: CBOR.Decoder s ()
decodeBlockVersion = do
Expand Down Expand Up @@ -421,7 +426,8 @@ decodeTxWitness = do
0 -> CBOR.decodeTag *> CBOR.decodeBytes -- PKWitness
1 -> CBOR.decodeTag *> CBOR.decodeBytes -- Script Witness
2 -> CBOR.decodeTag *> CBOR.decodeBytes -- Redeem Witness
_ -> fail $ "decodeTxWitness: unknown tx witness constructor: " <> show t
_ -> fail
$ "decodeTxWitness: unknown tx witness constructor: " <> show t

decodeUpdateProof :: CBOR.Decoder s ()
decodeUpdateProof = do
Expand Down
3 changes: 2 additions & 1 deletion src/Cardano/Wallet/Binary/Packfile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import Data.Int
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL

-- | Things related to the file format that can go wrong when decoding a pack file.
-- | Things related to the file format that can go wrong when decoding a pack
-- file.
data PackfileError
= MissingMagicError
| WrongFileTypeError
Expand Down
5 changes: 3 additions & 2 deletions src/Cardano/Wallet/BlockSyncer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ tickingFunction getNextBlocks action tickTime = go
-> IO ()
go (BlockHeadersConsumed headersConsumed) = do
blocksDownloaded <- getNextBlocks
let blocksToProcess =
filter (checkIfAlreadyConsumed headersConsumed) (L.nub blocksDownloaded)
let blocksToProcess = filter
(checkIfAlreadyConsumed headersConsumed)
(L.nub blocksDownloaded)
mapM_ action blocksToProcess
threadDelay $ (fromIntegral . toMicroseconds) tickTime
go $ BlockHeadersConsumed
Expand Down
8 changes: 6 additions & 2 deletions src/Cardano/Wallet/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ txOutsOurs txs =
updatePending :: Block -> Set Tx -> Set Tx
updatePending b =
let
isStillPending ins = Set.null . Set.intersection ins . Set.fromList . inputs
isStillPending ins =
Set.null . Set.intersection ins . Set.fromList . inputs
in
Set.filter (isStillPending (txIns $ transactions b))

Expand Down Expand Up @@ -241,7 +242,10 @@ instance Dom UTxO where

balance :: UTxO -> Integer
balance =
Map.foldl' (\total out -> total + fromIntegral (getCoin (coin out))) 0 . getUTxO
Map.foldl' fn 0 . getUTxO
where
fn :: Integer -> TxOut -> Integer
fn total out = total + fromIntegral (getCoin (coin out))

-- ins⋪ u
excluding :: UTxO -> Set TxIn -> UTxO
Expand Down
77 changes: 42 additions & 35 deletions test/unit/Cardano/Wallet/BinarySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ spec :: Spec
spec = do
describe "Decoding blocks" $ do
it "should decode a block header" $ do
bs <- L8.readFile "test/data/Cardano/Wallet/BinarySpec-block-header-1"
bs <- L8.readFile
"test/data/Cardano/Wallet/BinarySpec-block-header-1"
let decoded = unsafeDeserialiseFromBytes decodeBlockHeader bs
decoded `shouldBe` blockHeader1

Expand Down Expand Up @@ -98,72 +99,77 @@ block1 = Block
{ header = BlockHeader
{ epochIndex = 105
, slotNumber = 9519
, prevBlockHash = hash16 "4d97da40fb62bec847d6123762e82f9325f11d0c8e89deee0c7dbb598ed5f0cf"
, prevBlockHash = prevBlockHash0
}
, transactions = mempty
}
where
prevBlockHash0 = hash16
"4d97da40fb62bec847d6123762e82f9325f11d0c8e89deee0c7dbb598ed5f0cf"

-- A mainnet block with a transaction
block2 :: Block
block2 = Block
{ header = BlockHeader
{ epochIndex = 105
, slotNumber = 9876
, prevBlockHash = hash16 "da73001193ab3e6a43921385941c5f96b8f56de3908e78fae06f038b91dadd9d"
, prevBlockHash = prevBlockHash0
}
, transactions = Set.fromList
[ Tx
{ inputs =
[ TxIn
{ inputId = hash16 "60dbb2679ee920540c18195a3d92ee9be50aee6ed5f891d92d51db8a76b02cd2"
, inputIx = 3
}
]
[ TxIn { inputId = inputId0, inputIx = 3 } ]
, outputs =
[ TxOut
{ address = addr58 "DdzFFzCqrhsug8jKBMV5Cr94hKY4DrbJtkUpqptoGEkovR2QSkcAcRgjnUyegE689qBX6b2kyxyNvCL6mfqiarzRB9TRq8zwJphR31pr"
, coin = Coin 285000000
}
, TxOut
{ address = addr58 "DdzFFzCqrhsmxmuQpgjUrvRwF5ZKnyQ7pGrS4q53u5B516wcc26maHz9M4myYAkQVc5m9E4DKJjRDjPxuDdK3ZsHb1Dnqf3XorZ1PnzX"
, coin = Coin 1810771919
}
]
[ TxOut { address = address0, coin = Coin 285000000 }
, TxOut { address = address1, coin = Coin 1810771919 } ]
}
]
}
where
prevBlockHash0 = hash16
"da73001193ab3e6a43921385941c5f96b8f56de3908e78fae06f038b91dadd9d"
inputId0 = hash16
"60dbb2679ee920540c18195a3d92ee9be50aee6ed5f891d92d51db8a76b02cd2"
address0 = addr58
"DdzFFzCqrhsug8jKBMV5Cr94hKY4DrbJtkUpqptoGEkovR2QSkcA\
\cRgjnUyegE689qBX6b2kyxyNvCL6mfqiarzRB9TRq8zwJphR31pr"
address1 = addr58
"DdzFFzCqrhsmxmuQpgjUrvRwF5ZKnyQ7pGrS4q53u5B516wcc26m\
\aHz9M4myYAkQVc5m9E4DKJjRDjPxuDdK3ZsHb1Dnqf3XorZ1PnzX"


-- A testnet block with a transaction
block3 :: Block
block3 = Block
{ header = BlockHeader
{ epochIndex = 30
, slotNumber = 9278
, prevBlockHash = hash16 "b065b5fe97bec5fd130e7a639189499c9d0b1fcf9348c5c19f7a22700da7a35e"
, prevBlockHash = prevBlockHash0
}
, transactions = Set.fromList
[ Tx
{ inputs =
[ TxIn
{ inputId = hash16 "6967e2b5c3ad5ae07a9bd8d888f1836195a04f7a1cb4b6d083261870068fab1b"
, inputIx = 1}
, TxIn
{ inputId = hash16 "7064addc0968bccd7d57d2e7aa1e9c2f666d8387042483fc1e87200cfb96c8f1"
, inputIx = 0} ]
[ TxIn { inputId = inputId0, inputIx = 1 }
, TxIn { inputId = inputId1, inputIx = 0 } ]
, outputs =
[ TxOut
{ address = addr58 "37btjrVyb4KBsw2f3V76ntfwqDPgyf3QmmdsrTSmCnuTGYtS9JgVXzxeQEsKjgWurKoyw9BDNEtLxWtU9znK49SC8bLTirk6YqcAESFxXJkSyXhQKL"
, coin = Coin 1404176490
}
, TxOut
{ address = addr58 "37btjrVyb4KD5Ne4yvGAHGbQuHUYQX1VPsXh85rBh3UrGSMWdRSFxBYQ9RQRHCMezN6AMLd3uYTC5hbeVTUiPzfQUTCEogg2HrSJKQUjAgsoYZHwT3"
, coin = Coin 1004099328
}
]
[ TxOut { address = address0, coin = Coin 1404176490 }
, TxOut { address = address1, coin = Coin 1004099328 } ]
}
]
}

where
prevBlockHash0 = hash16
"b065b5fe97bec5fd130e7a639189499c9d0b1fcf9348c5c19f7a22700da7a35e"
inputId0 = hash16
"6967e2b5c3ad5ae07a9bd8d888f1836195a04f7a1cb4b6d083261870068fab1b"
inputId1 = hash16
"7064addc0968bccd7d57d2e7aa1e9c2f666d8387042483fc1e87200cfb96c8f1"
address0 = addr58
"37btjrVyb4KBsw2f3V76ntfwqDPgyf3QmmdsrTSmCnuTGYtS9JgVXzxeQ\
\EsKjgWurKoyw9BDNEtLxWtU9znK49SC8bLTirk6YqcAESFxXJkSyXhQKL"
address1 = addr58
"37btjrVyb4KD5Ne4yvGAHGbQuHUYQX1VPsXh85rBh3UrGSMWdRSFxBYQ9\
\RQRHCMezN6AMLd3uYTC5hbeVTUiPzfQUTCEogg2HrSJKQUjAgsoYZHwT3"

-- * Helpers

Expand All @@ -175,7 +181,8 @@ hash16 = either bomb Hash . convertFromBase Base16

-- | Make an Address from a Base58 encoded string, without error handling.
addr58 :: ByteString -> Address
addr58 = maybe (error "addr58: Could not decode") Address . decodeBase58 bitcoinAlphabet
addr58 = maybe (error "addr58: Could not decode") Address
. decodeBase58 bitcoinAlphabet

-- | CBOR deserialise without error handling - handy for prototypes or testing.
unsafeDeserialiseFromBytes :: (forall s. CBOR.Decoder s a) -> BL.ByteString -> a
Expand Down
23 changes: 13 additions & 10 deletions test/unit/Cardano/Wallet/BlockSyncerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ spec = do
tickingFunctionTest
:: (TickingTime, Blocks)
-> Property
tickingFunctionTest (TickingTime tickTime, Blocks blocks) = monadicIO $ liftIO $ do
(readerChan, reader) <- mkReader
(writerChan, writer) <- mkWriter blocks
waitFor writerChan $ tickingFunction writer reader tickTime (BlockHeadersConsumed [])
takeMVar readerChan `shouldReturn` L.nub (reverse $ mconcat blocks)
tickingFunctionTest (TickingTime tickTime, Blocks blocks) =
monadicIO $ liftIO $ do
(readerChan, reader) <- mkReader
(writerChan, writer) <- mkWriter blocks
waitFor writerChan $
tickingFunction writer reader tickTime (BlockHeadersConsumed [])
takeMVar readerChan `shouldReturn` L.nub (reverse $ mconcat blocks)

waitFor
:: MVar ()
Expand Down Expand Up @@ -133,11 +135,12 @@ instance Arbitrary Blocks where
blockHeaderHash =
Hash . CBOR.toStrictByteString . encodeBlockHeader
where
encodeBlockHeader (BlockHeader (EpochId epoch) (SlotId slot) prev) = mempty
<> CBOR.encodeListLen 3
<> CBOR.encodeWord64 epoch
<> CBOR.encodeWord16 slot
<> CBOR.encodeBytes (getHash prev)
encodeBlockHeader (BlockHeader (EpochId epoch) (SlotId slot) prev) =
mempty
<> CBOR.encodeListLen 3
<> CBOR.encodeWord64 epoch
<> CBOR.encodeWord16 slot
<> CBOR.encodeBytes (getHash prev)


-- | Construct arbitrary groups of elements from a given list.
Expand Down

0 comments on commit 1a920d7

Please sign in to comment.