File tree 3 files changed +10
-12
lines changed
3 files changed +10
-12
lines changed Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ mineBlock stringData = do
80
80
replaceChain :: MonadIO m => IORef [Block ] -> [Block ] -> m ()
81
81
replaceChain chainRef newChain = do
82
82
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)
84
84
then liftDebug $ " chain is not valid for updating!: " ++ show newChain
85
85
else do
86
86
setChain <- liftIO $ atomicModifyIORef' chainRef $ const (newChain, newChain)
@@ -122,8 +122,7 @@ main = do
122
122
_ <- initLogger $ p2pPort args
123
123
debugM " legion" " starting"
124
124
(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)
127
126
spockCfg <- defaultSpockCfg EmptySession PCNoDatabase (BlockChainState ref localNode procId)
128
127
_ <- async $ runSpock (read (httpPort args) :: Int ) (spock spockCfg Main. app)
129
128
-- wait for messages to come in from the p2p network and respond to them
Original file line number Diff line number Diff line change @@ -46,11 +46,10 @@ addHashToBlock block = block { blockHash = calculateBlockHash block }
46
46
-- a hardcoded initial block, we need this to make sure all
47
47
-- nodes have the same starting point, so we have a hard coded
48
48
-- frame of reference to detect validity
49
- initialBlock :: IO Block
49
+ initialBlock :: Block
50
50
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 }
54
53
55
54
-- a new block is valid if its index is 1 higher, its
56
55
-- previous hash points to our last block, and its hash is computed
@@ -64,13 +63,13 @@ isValidNewBlock prev next
64
63
65
64
-- a chain is valid if it starts with our hardcoded initial
66
65
-- 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
69
68
[] -> True
70
- [x] -> x == initial
69
+ [x] -> x == initialBlock
71
70
(x: xs) ->
72
71
let blockPairs = zip chain xs in
73
- x == initial &&
72
+ x == initialBlock &&
74
73
all (uncurry isValidNewBlock) blockPairs
75
74
76
75
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ unitTests =
15
15
testValidChain :: IO ()
16
16
testValidChain = do
17
17
print " running test!"
18
- b <- initialBlock
18
+ let b = initialBlock
19
19
assertBool " block eq" $ b == b
20
20
assertBool " empty chains are valid" $ isValidChain b []
21
21
assertBool " base chain is valid" $ isValidChain b [b]
You can’t perform that action at this time.
0 commit comments