From fc01155f82b11eb2f772a3a20cbb12e904311a19 Mon Sep 17 00:00:00 2001 From: aviaviavi Date: Thu, 25 May 2017 21:32:41 -0700 Subject: [PATCH] fixes --- app/Main.hs | 5 ++--- src/Lib.hs | 15 +++++++-------- test/Spec.hs | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 7fa3136..e373bce 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -80,7 +80,7 @@ mineBlock stringData = do replaceChain :: MonadIO m => IORef [Block] -> [Block] -> m () replaceChain chainRef newChain = do currentChain <- liftIO $ readIORef chainRef - if not $ isValidChain (Prelude.head currentChain) newChain || (length currentChain >= length newChain) + if not $ isValidChain newChain || (length currentChain >= length newChain) then liftDebug $ "chain is not valid for updating!: " ++ show newChain else do setChain <- liftIO $ atomicModifyIORef' chainRef $ const (newChain, newChain) @@ -122,8 +122,7 @@ main = do _ <- initLogger $ p2pPort args debugM "legion" "starting" (localNode, procId) <- runP2P (p2pPort args) (seedNode args) (return ()) - genesis <- initialBlock - ref <- maybe (newIORef [genesis]) (const $ newIORef []) (seedNode args) + ref <- maybe (newIORef [initialBlock]) (const $ newIORef []) (seedNode args) spockCfg <- defaultSpockCfg EmptySession PCNoDatabase (BlockChainState ref localNode procId) _ <- async $ runSpock (read (httpPort args) :: Int) (spock spockCfg Main.app) -- wait for messages to come in from the p2p network and respond to them diff --git a/src/Lib.hs b/src/Lib.hs index b1a8b1f..8341a7b 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -46,11 +46,10 @@ addHashToBlock block = block { blockHash = calculateBlockHash block } -- a hardcoded initial block, we need this to make sure all -- nodes have the same starting point, so we have a hard coded -- frame of reference to detect validity -initialBlock :: IO Block +initialBlock :: Block initialBlock = do - time <- epoch - let block = Block 0 "0" time "initial data" "" - return $ block { blockHash = calculateBlockHash block } + let block = Block 0 "0" 0 "initial data" "" + block { blockHash = calculateBlockHash block } -- a new block is valid if its index is 1 higher, its -- previous hash points to our last block, and its hash is computed @@ -64,13 +63,13 @@ isValidNewBlock prev next -- a chain is valid if it starts with our hardcoded initial -- block and every block is valid with respect to the previous -isValidChain :: Block -> [Block] -> Bool -isValidChain initial chain = case chain of +isValidChain :: [Block] -> Bool +isValidChain chain = case chain of [] -> True - [x] -> x == initial + [x] -> x == initialBlock (x:xs) -> let blockPairs = zip chain xs in - x == initial && + x == initialBlock && all (uncurry isValidNewBlock) blockPairs diff --git a/test/Spec.hs b/test/Spec.hs index a506102..df75dbc 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -15,7 +15,7 @@ unitTests = testValidChain :: IO () testValidChain = do print "running test!" - b <- initialBlock + let b = initialBlock assertBool "block eq" $ b == b assertBool "empty chains are valid" $ isValidChain b [] assertBool "base chain is valid" $ isValidChain b [b]