11{-# LANGUAGE OverloadedStrings #-}
22{-# LANGUAGE DeriveGeneric #-}
33
4- -- Example invocations:
5- -- peer chaincode invoke -n mycc -c '{"Args":["initMarble","marble1","red","large","Al"]}' -C myc
6- -- peer chaincode invoke -n mycc -c '{"Args":["initMarble","marble2","blue","large","Nick"]}' -C myc
7- -- peer chaincode invoke -n mycc -c '{"Args":["readMarble","marble1"]}' -C myc
8- -- peer chaincode invoke -n mycc -c '{"Args":["deleteMarble","marble1"]}' -C myc
9- -- peer chaincode invoke -n mycc -c '{"Args":["transferMarble","marble1", "Nick"]}' -C myc
10- -- peer chaincode invoke -n mycc -c '{"Args":["getMarblesByRange","marble1", "marble3"]}' -C myc
11-
124module Marbles where
135
146import GHC.Generics
@@ -18,17 +10,24 @@ import Shim ( start
1810 , ChaincodeStub (.. )
1911 , ChaincodeStubInterface (.. )
2012 , DefaultChaincodeStub
13+ , StateQueryIterator (.. )
14+ , StateQueryIteratorInterface (.. )
15+ , Error (.. )
2116 )
2217
2318import Peer.ProposalResponse as Pb
19+ import Ledger.Queryresult.KvQueryResult as Pb
2420
2521import Data.Text ( Text
2622 , unpack
2723 , pack
24+ , append
2825 )
26+ import qualified Data.Text.Encoding as TSE
2927import qualified Data.ByteString as BS
3028import qualified Data.ByteString.UTF8 as BSU
3129import qualified Data.ByteString.Lazy as LBS
30+ import qualified Data.Text.Lazy as TL
3231
3332import Data.Aeson ( ToJSON
3433 , FromJSON
@@ -38,6 +37,7 @@ import Data.Aeson ( ToJSON
3837 , encode
3938 , decode
4039 )
40+
4141import Debug.Trace
4242
4343main :: IO ()
@@ -60,8 +60,14 @@ instance ToJSON Marble where
6060instance FromJSON Marble
6161
6262initFunc :: DefaultChaincodeStub -> IO Pb. Response
63- initFunc _ = pure $ successPayload Nothing
64-
63+ initFunc s =
64+ let e = getFunctionAndParameters s
65+ in
66+ case e of
67+ Left _ -> pure $ errorPayload " "
68+ Right (" initMarble" , parameters) -> initMarble s parameters
69+ Right (fn , _ ) -> pure
70+ $ errorPayload (pack (" Invoke did not find function: " ++ unpack fn))
6571
6672invokeFunc :: DefaultChaincodeStub -> IO Pb. Response
6773invokeFunc s =
@@ -81,8 +87,8 @@ invokeFunc s =
8187 -- Right ("getHistoryForMarble", parameters) ->
8288 -- getHistoryForMarble s parameters
8389 Right (" getMarblesByRange" , parameters) -> getMarblesByRange s parameters
84- -- Right ("getMarblesByRangeWithPagination", parameters) ->
85- -- getMarblesByRangeWithPagination s parameters
90+ Right (" getMarblesByRangeWithPagination" , parameters) ->
91+ getMarblesByRangeWithPagination s parameters
8692 -- Right ("queryMarblesWithPagination", parameters) ->
8793 -- queryMarblesWithPagination s parameters
8894 Right (fn , _ ) -> pure
@@ -166,9 +172,36 @@ getMarblesByRange s params = if Prelude.length params == 2
166172 e <- getStateByRange s (params !! 0 ) (params !! 1 )
167173 case e of
168174 Left _ -> pure $ errorPayload " Failed to get marbles"
169- Right a -> trace (show a) (pure $ successPayload Nothing )
170- else pure $ errorPayload
171- " Incorrect arguments. Need a start key and an end key"
175+ Right sqi -> do
176+ resultBytes <- generateResultBytes sqi " "
177+ trace (show resultBytes) (pure $ successPayload Nothing )
178+ else pure $ errorPayload " Incorrect arguments. Need a start key and an end key"
179+
180+ getMarblesByRangeWithPagination :: DefaultChaincodeStub -> [Text ] -> IO Pb. Response
181+ getMarblesByRangeWithPagination s params = if Prelude. length params == 4
182+ then do
183+ e <- getStateByRangeWithPagination s (params !! 0 ) (params !! 1 ) (read (unpack $ params !! 2 ) :: Int ) (params !! 3 )
184+ case e of
185+ Left _ -> pure $ errorPayload " Failed to get marbles"
186+ Right _ -> pure $ successPayload $ Just " The payload"
187+ else pure $ errorPayload " Incorrect arguments. Need start key, end key, pageSize and bookmark"
188+
189+ generateResultBytes :: StateQueryIterator -> Text -> IO (Either Error BSU. ByteString )
190+ generateResultBytes sqi text = do
191+ hasNextBool <- hasNext sqi
192+ if hasNextBool then do
193+ eeKV <- next sqi
194+ -- TODO: We need to check that the Either Error KV returned from next
195+ -- is correct and append the showable version of KVs instead of "abc".
196+ case eeKV of
197+ Left e -> pure $ Left e
198+ Right kv ->
199+ let
200+ makeKVString :: Pb. KV -> Text
201+ makeKVString kv_ = pack " Key: " <> TL. toStrict (Pb. kvKey kv_) <> pack " , Value: " <> TSE. decodeUtf8 (kvValue kv_)
202+ in
203+ generateResultBytes sqi (append text (makeKVString kv))
204+ else pure $ Right $ TSE. encodeUtf8 text
172205
173206parseMarble :: [Text ] -> Marble
174207parseMarble params = Marble { objectType = " marble"
0 commit comments