on master help sign in
switch from cryptonite to lighter-weight cryptohash-sha256 which also offers the benefit of compiling on MacOS 10.7
commit0704e54fe796c3099647fc69b64d99759fd59f72 authorAgentM <agentm@themactionfaction.com> dateMon 12 Jun 2017, 12:52 (9 years ago) parentfd9b9b5
4 files changed+16−26
project-m36.cabal+1−1 src/lib/ProjectM36/Client.hs+2−15 src/lib/ProjectM36/TransactionGraph/Persist.hs+11−8 test/TransactionGraph/Persist.hs+2−2
project-m36.cabal+1−1
@@ -67,7 +67,7 @@ Library
6767 ,optparse-applicative
6868 ,Glob
6969--used for hashing the transaction graph file to check for differences
70− ,cryptonite >= 0.21
70+ ,cryptohash-sha256
7171 Exposed-Modules: ProjectM36.Error,
7272 ProjectM36.Transaction,
7373 ProjectM36.TransactionGraph,
src/lib/ProjectM36/Client.hs+2−15
@@ -136,7 +136,7 @@ import Data.Binary (Binary)
136136import GHC.Generics (Generic)
137137import Control.DeepSeq (force)
138138import System.IO
139−import Crypto.Hash
139+import qualified Crypto.Hash.SHA256 as SHA256
140140
141141type Hostname = String
142142
@@ -199,7 +199,6 @@ defaultRemoteConnectionInfo = RemoteProcessConnectionInfo defaultDatabaseName (c
199199type ClientNodes = STMSet.Set ProcessId
200200
201201type TransactionGraphLockHandle = Handle
202−type LockFileHash = Digest SHA256
203202
204203-- internal structure specific to in-process connections
205204data InProcessConnectionConf = InProcessConnectionConf {
@@ -727,23 +726,11 @@ processTransactionGraphPersistence NoPersistence _ = pure ()
727726processTransactionGraphPersistence (MinimalPersistence dbdir) graph = transactionGraphPersist NoDiskSync dbdir graph >> pure ()
728727processTransactionGraphPersistence (CrashSafePersistence dbdir) graph = transactionGraphPersist FsyncDiskSync dbdir graph >> pure ()
729728
730−readGraphTransactionIdDigest :: PersistenceStrategy -> IO (Digest SHA256)
729+readGraphTransactionIdDigest :: PersistenceStrategy -> IO (LockFileHash)
731730readGraphTransactionIdDigest NoPersistence = error "attempt to read digest from transaction log without persistence enabled"
732731readGraphTransactionIdDigest (MinimalPersistence dbdir) = readGraphTransactionIdFileDigest dbdir
733732readGraphTransactionIdDigest (CrashSafePersistence dbdir) = readGraphTransactionIdFileDigest dbdir
734733
735−{-
736−sameGraphTransactionIdDigest :: PersistenceStrategy -> Digest SHA256 -> IO (Bool)
737−sameGraphTransactionIdDigest NoPersistence _ = pure True
738−sameGraphTransactionIdDigest persist lastDigest = do
739− let dbdir = case persist of
740− MinimalPersistence x -> x
741− CrashSafePersistence x -> x
742− newDigest <- readGraphTransactionIdDigest persist
743− pure (lastDigest == newDigest)
744−
745−-}
746−
747734-- | Return a relation whose type would match that of the relational expression if it were executed. This is useful for checking types and validating a relational expression's types.
748735typeForRelationalExpr :: SessionId -> Connection -> RelationalExpr -> IO (Either RelationalError Relation)
749736typeForRelationalExpr sessionId conn@(InProcessConnection _) relExpr = atomically $ typeForRelationalExprSTM sessionId conn relExpr
src/lib/ProjectM36/TransactionGraph/Persist.hs+11−8
@@ -20,9 +20,12 @@ import Control.Monad (foldM)
2020import Data.Either (isRight)
2121import Data.Maybe (catMaybes)
2222import Control.Exception.Base
23−import Crypto.Hash
2423import qualified Data.Text.IO as TIO
24+import Data.ByteString (ByteString)
2525import Data.Monoid
26+import qualified Crypto.Hash.SHA256 as SHA256
27+
28+type LockFileHash = ByteString
2629
2730{-
2831The "m36v1" file at the top-level of the destination directory contains the the transaction graph as a set of transaction ids referencing their parents (1 or more)
@@ -46,7 +49,7 @@ verify that the database directory is valid or bootstrap it
4649- return error or lock file handle which is already locked with a read lock
4750-}
4851
49−setupDatabaseDir :: DiskSync -> FilePath -> TransactionGraph -> IO (Either PersistenceError (Handle, Digest SHA256))
52+setupDatabaseDir :: DiskSync -> FilePath -> TransactionGraph -> IO (Either PersistenceError (Handle, LockFileHash))
5053setupDatabaseDir sync dbdir bootstrapGraph = do
5154 dbdirExists <- doesDirectoryExist dbdir
5255 m36exists <- doesFileExist (transactionLogPath dbdir)
@@ -63,7 +66,7 @@ setupDatabaseDir sync dbdir bootstrapGraph = do
6366{-
6467initialize a database directory with the graph from which to bootstrap- return lock file handle
6568-}
66−bootstrapDatabaseDir :: DiskSync -> FilePath -> TransactionGraph -> IO (Handle, Digest SHA256)
69+bootstrapDatabaseDir :: DiskSync -> FilePath -> TransactionGraph -> IO (Handle, LockFileHash)
6770bootstrapDatabaseDir sync dbdir bootstrapGraph = do
6871 createDirectory dbdir
6972 lockFileH <- openLockFile dbdir
@@ -82,7 +85,7 @@ incrementally updates an existing database directory
8285-assume that all non-head transactions have already been written because this is an incremental (and concurrent!) write method
8386--store the head names with a symlink to the transaction under "heads"
8487-}
85−transactionGraphPersist :: DiskSync -> FilePath -> TransactionGraph -> IO (Digest SHA256)
88+transactionGraphPersist :: DiskSync -> FilePath -> TransactionGraph -> IO LockFileHash
8689transactionGraphPersist sync destDirectory graph = do
8790 transactionHeadTransactionsPersist sync destDirectory graph
8891 --write graph file
@@ -155,19 +158,19 @@ readTransactionIfNecessary dbdir transId mScriptSession graphIn = do
155158 Left err -> return $ Left err
156159 Right trans' -> return $ Right $ TransactionGraph (transactionHeadsForGraph graphIn) (S.insert trans' (transactionsForGraph graphIn))
157160
158−writeGraphTransactionIdFile :: DiskSync -> FilePath -> TransactionGraph -> IO (Digest SHA256)
161+writeGraphTransactionIdFile :: DiskSync -> FilePath -> TransactionGraph -> IO LockFileHash
159162writeGraphTransactionIdFile sync destDirectory (TransactionGraph _ transSet) = writeFileSync sync graphFile uuidInfo >> pure digest
160163 where
161164 graphFile = destDirectory </> "m36v1"
162165 uuidInfo = T.intercalate "\n" graphLines
163− digest = hashWith SHA256 (encodeUtf8 uuidInfo)
166+ digest = SHA256.hash (encodeUtf8 uuidInfo)
164167 graphLines = S.toList $ S.map graphLine transSet
165168 graphLine trans = U.toText (transactionId trans) <> " " <> T.intercalate " " (S.toList (S.map U.toText $ transactionParentIds trans))
166169
167−readGraphTransactionIdFileDigest :: FilePath -> IO (Digest SHA256)
170+readGraphTransactionIdFileDigest :: FilePath -> IO LockFileHash
168171readGraphTransactionIdFileDigest dbdir = do
169172 graphTransactionIdData <- TIO.readFile (transactionLogPath dbdir)
170− pure (hashWith SHA256 (encodeUtf8 graphTransactionIdData))
173+ pure (SHA256.hash (encodeUtf8 graphTransactionIdData))
171174
172175readGraphTransactionIdFile :: FilePath -> IO (Either PersistenceError [(TransactionId, [TransactionId])])
173176readGraphTransactionIdFile dbdir = do
test/TransactionGraph/Persist.hs+2−2
@@ -42,7 +42,7 @@ testDBSimplePersistence = TestCase $ withSystemTempDirectory "m36testdb" $ \temp
4242 let dbdir = tempdir </> "dbdir"
4343 freshId <- nextRandom
4444 let graph = bootstrapTransactionGraph freshId dateExamples
45− bootstrapDatabaseDir NoDiskSync dbdir graph
45+ _ <- bootstrapDatabaseDir NoDiskSync dbdir graph
4646 case transactionForHead "master" graph of
4747 Nothing -> assertFailure "Failed to retrieve head transaction for master branch."
4848 Just headTrans -> do
@@ -57,7 +57,7 @@ testDBSimplePersistence = TestCase $ withSystemTempDirectory "m36testdb" $ \temp
5757 Left err -> assertFailure (show err)
5858 Right (_, graph') -> do
5959 --persist the new graph
60− transactionGraphPersist NoDiskSync dbdir graph'
60+ _ <- transactionGraphPersist NoDiskSync dbdir graph'
6161 --reload the graph from the filesystem and confirm that the transaction is present
6262 graphErr <- transactionGraphLoad dbdir emptyTransactionGraph Nothing
6363 let mapEq graphArg = S.map transactionId (transactionsForGraph graphArg)