Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aviaviavi committed May 26, 2017
1 parent 425288e commit fc01155
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
5 changes: 2 additions & 3 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit fc01155

Please sign in to comment.