@@ -6,14 +6,14 @@ module Stub where
66
77import qualified Common.Common as Pb
88
9- import Control.Monad.Except ( ExceptT (.. ), runExceptT )
9+ import Control.Monad.Except ( ExceptT (.. ), runExceptT , throwError )
1010
11- -- import Data.Int (fromIntegral)
1211import Data.Bifunctor
1312import Data.ByteString as BS
1413import qualified Data.ByteString.Lazy as LBS
14+ import Data.Char ( chr )
1515import Data.IORef ( modifyIORef , newIORef , readIORef , writeIORef )
16- import Data.Text
16+ import Data.Text as TS
1717import Data.Text.Encoding
1818import Data.Text.Lazy as TL
1919import Data.Vector as Vector ( (!) , Vector , empty , foldr , length , toList )
@@ -137,13 +137,6 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
137137 Right _ -> pure ()
138138 listenForResponse (recvStream ccs)
139139
140- --
141- -- -- setStateValidationParameter :: ccs -> String -> [ByteString] -> Maybe Error
142- -- setStateValidationParameter ccs key parameters = Right notImplemented
143- --
144- -- -- getStateValiationParameter :: ccs -> String -> Either Error [ByteString]
145- -- getStateValiationParameter ccs key = Left notImplemented
146- --
147140 -- TODO: Implement better error handling/checks etc
148141 -- getStateByRange :: ccs -> Text -> Text -> IO (Either Error StateQueryIterator)
149142 getStateByRange ccs startKey endKey =
@@ -173,7 +166,37 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
173166 Right _ -> pure ()
174167 runExceptT $ ExceptT (listenForResponse (recvStream ccs)) >>= (bsToSqiAndMeta ccs)
175168
176- -- TODO : implement all these interface functions
169+ -- TODO: This is the next TODO! Implement these 7 function because they are needed in marbles.hs
170+ -- getStateByPartialCompositeKey :: ccs -> Text -> [Text] -> Either Error StateQueryIterator
171+ getStateByPartialCompositeKey ccs objectType keys = throwError $ Error " not implemented"
172+
173+ -- getStateByPartialCompositeKeyWithPagination :: ccs -> Text -> [Text] -> Int32 -> Text -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
174+ getStateByPartialCompositeKeyWithPagination ccs objectType keys pageSize bookmark =
175+ throwError $ Error " not implemented"
176+
177+ -- createCompositeKey :: ccs -> Text -> [Text] -> Either Error Text
178+ createCompositeKey ccs objectType keys =
179+ let keysString = Prelude. foldr (\ key acc -> acc ++ TS. unpack key ++ nullCodepoint) " " keys
180+ nullCodepoint = [ chr 0 ]
181+ in
182+ -- TODO: Check that objectTypes and keys are all valid utf8 strings
183+ Right $ TS. pack $ " \x00 " ++ TS. unpack objectType ++ nullCodepoint ++ keysString
184+
185+ -- splitCompositeKey :: ccs -> Text -> Either Error (Text, [Text])
186+ splitCompositeKey ccs key =
187+ -- key has the form \x00objectTypeU+0000keyU+0000key etc so we use `tail key` to ignore the \x00 char
188+ -- and then split on the unicode codepoint U+0000 to extract the objectType and keys
189+ let keys = TS. splitOn (TS. singleton $ chr 0 ) (TS. tail key) in Right (Prelude. head keys, Prelude. tail keys)
190+
191+ -- getQueryResult :: ccs -> Text -> Either Error StateQueryIterator
192+ getQueryResult ccs query = throwError $ Error " not implemented"
193+
194+ -- getQueryResultWithPagination :: ccs -> Text -> Int32 -> Text -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
195+ getQueryResultWithPagination ccs key pageSize bookmark = throwError $ Error " not implemented"
196+
197+ -- getHistoryForKey :: ccs -> Text -> Either Error HistoryQueryIterator
198+ getHistoryForKey ccs key = throwError $ Error " not implemented"
199+
177200instance StateQueryIteratorInterface StateQueryIterator where
178201 -- TODO: remove the IO from this function (possibly with the State monad)
179202 -- hasNext :: sqi -> IO Bool
@@ -184,6 +207,7 @@ instance StateQueryIteratorInterface StateQueryIterator where
184207 pure $ (currentLoc < Prelude. length (Pb. queryResponseResults queryResponse))
185208 || (Pb. queryResponseHasMore queryResponse)
186209
210+ -- TODO : implement close function (need to do anything here in haskell?)
187211 -- close :: sqi -> IO (Maybe Error)
188212 close _ = pure Nothing
189213
@@ -296,28 +320,6 @@ fetchNextQueryResult sqi = do
296320 Left err -> error (" Error while streaming: " ++ show err)
297321 Right _ -> pure ()
298322 runExceptT $ ExceptT (listenForResponse (recvStream $ sqiChaincodeStub sqi)) >>= bsToQueryResponse
299- --
300- -- -- getStateByPartialCompositeKey :: ccs -> String -> [String] -> Either Error StateQueryIterator
301- -- getStateByPartialCompositeKey ccs objectType keys = Left notImplemented
302- --
303- -- --getStateByPartialCompositeKeyWithPagination :: ccs -> String -> [String] -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
304- -- getStateByPartialCompositeKeyWithPagination ccs objectType keys pageSize bookmark = Left notImplemented
305- --
306- -- --createCompositeKey :: ccs -> String -> [String] -> Either Error String
307- -- createCompositeKey ccs objectType keys = Left notImplemented
308- --
309- -- --splitCompositeKey :: ccs -> String -> Either Error (String, [String])
310- -- splitCompositeKey ccs key = Left notImplemented
311- --
312- -- --getQueryResult :: ccs -> String -> Either Error StateQueryIterator
313- -- getQueryResult ccs query = Left notImplemented
314- --
315- -- --getQueryResultWithPagination :: ccs -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
316- -- getQueryResultWithPagination ccs key pageSize bookmark = Left notImplemented
317- --
318- -- --getHistoryForKey :: ccs -> String -> Either Error HistoryQueryIterator
319- -- getHistoryForKey ccs key = Left notImplemented
320- --
321323-- --getPrivateData :: ccs -> String -> String -> Either Error ByteString
322324-- getPrivateData ccs collection key = Left notImplemented
323325--
0 commit comments