Skip to content

Commit fc01155

Browse files
committed
fixes
1 parent 425288e commit fc01155

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

app/Main.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mineBlock stringData = do
8080
replaceChain :: MonadIO m => IORef [Block] -> [Block] -> m ()
8181
replaceChain chainRef newChain = do
8282
currentChain <- liftIO $ readIORef chainRef
83-
if not $ isValidChain (Prelude.head currentChain) newChain || (length currentChain >= length newChain)
83+
if not $ isValidChain newChain || (length currentChain >= length newChain)
8484
then liftDebug $ "chain is not valid for updating!: " ++ show newChain
8585
else do
8686
setChain <- liftIO $ atomicModifyIORef' chainRef $ const (newChain, newChain)
@@ -122,8 +122,7 @@ main = do
122122
_ <- initLogger $ p2pPort args
123123
debugM "legion" "starting"
124124
(localNode, procId) <- runP2P (p2pPort args) (seedNode args) (return ())
125-
genesis <- initialBlock
126-
ref <- maybe (newIORef [genesis]) (const $ newIORef []) (seedNode args)
125+
ref <- maybe (newIORef [initialBlock]) (const $ newIORef []) (seedNode args)
127126
spockCfg <- defaultSpockCfg EmptySession PCNoDatabase (BlockChainState ref localNode procId)
128127
_ <- async $ runSpock (read (httpPort args) :: Int) (spock spockCfg Main.app)
129128
-- wait for messages to come in from the p2p network and respond to them

src/Lib.hs

+7-8
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ addHashToBlock block = block { blockHash = calculateBlockHash block }
4646
-- a hardcoded initial block, we need this to make sure all
4747
-- nodes have the same starting point, so we have a hard coded
4848
-- frame of reference to detect validity
49-
initialBlock :: IO Block
49+
initialBlock :: Block
5050
initialBlock = do
51-
time <- epoch
52-
let block = Block 0 "0" time "initial data" ""
53-
return $ block { blockHash = calculateBlockHash block }
51+
let block = Block 0 "0" 0 "initial data" ""
52+
block { blockHash = calculateBlockHash block }
5453

5554
-- a new block is valid if its index is 1 higher, its
5655
-- previous hash points to our last block, and its hash is computed
@@ -64,13 +63,13 @@ isValidNewBlock prev next
6463

6564
-- a chain is valid if it starts with our hardcoded initial
6665
-- block and every block is valid with respect to the previous
67-
isValidChain :: Block -> [Block] -> Bool
68-
isValidChain initial chain = case chain of
66+
isValidChain :: [Block] -> Bool
67+
isValidChain chain = case chain of
6968
[] -> True
70-
[x] -> x == initial
69+
[x] -> x == initialBlock
7170
(x:xs) ->
7271
let blockPairs = zip chain xs in
73-
x == initial &&
72+
x == initialBlock &&
7473
all (uncurry isValidNewBlock) blockPairs
7574

7675

test/Spec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ unitTests =
1515
testValidChain :: IO ()
1616
testValidChain = do
1717
print "running test!"
18-
b <- initialBlock
18+
let b = initialBlock
1919
assertBool "block eq" $ b == b
2020
assertBool "empty chains are valid" $ isValidChain b []
2121
assertBool "base chain is valid" $ isValidChain b [b]

0 commit comments

Comments
 (0)