Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit 26f9598

Browse files
author
Nick Waywood
authored
Merge pull request #14 from nwaywood/iterator-methods
Iterator methods
2 parents 7fa0ed7 + b96437b commit 26f9598

File tree

3 files changed

+117
-98
lines changed

3 files changed

+117
-98
lines changed

examples/Marbles.hs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-- peer chaincode invoke -n mycc -c '{"Args":["deleteMarble","marble1"]}' -C myc
1010
-- peer chaincode invoke -n mycc -c '{"Args":["transferMarble","marble1", "Nick"]}' -C myc
1111
-- peer chaincode invoke -n mycc -c '{"Args":["getMarblesByRange","marble1", "marble3"]}' -C myc
12+
-- peer chaincode invoke -n mycc -c '{"Args":["getMarblesByRangeWithPagination","marble1", "marble3", "2", ""]}' -C myc
1213

1314
module Marbles where
1415

@@ -25,6 +26,7 @@ import Shim ( start
2526
)
2627

2728
import Peer.ProposalResponse as Pb
29+
import Ledger.Queryresult.KvQueryResult as Pb
2830

2931
import Data.Text ( Text
3032
, unpack
@@ -35,6 +37,7 @@ import qualified Data.Text.Encoding as TSE
3537
import qualified Data.ByteString as BS
3638
import qualified Data.ByteString.UTF8 as BSU
3739
import qualified Data.ByteString.Lazy as LBS
40+
import qualified Data.Text.Lazy as TL
3841

3942
import Data.Aeson ( ToJSON
4043
, FromJSON
@@ -93,8 +96,8 @@ invokeFunc s =
9396
-- Right ("getHistoryForMarble", parameters) ->
9497
-- getHistoryForMarble s parameters
9598
Right ("getMarblesByRange", parameters) -> getMarblesByRange s parameters
96-
-- Right ("getMarblesByRangeWithPagination", parameters) ->
97-
-- getMarblesByRangeWithPagination s parameters
99+
Right ("getMarblesByRangeWithPagination", parameters) ->
100+
getMarblesByRangeWithPagination s parameters
98101
-- Right ("queryMarblesWithPagination", parameters) ->
99102
-- queryMarblesWithPagination s parameters
100103
Right (fn , _ ) -> pure
@@ -183,14 +186,30 @@ getMarblesByRange s params = if Prelude.length params == 2
183186
trace (show resultBytes) (pure $ successPayload Nothing)
184187
else pure $ errorPayload "Incorrect arguments. Need a start key and an end key"
185188

189+
getMarblesByRangeWithPagination :: DefaultChaincodeStub -> [Text] -> IO Pb.Response
190+
getMarblesByRangeWithPagination s params = if Prelude.length params == 4
191+
then do
192+
e <- getStateByRangeWithPagination s (params !! 0) (params !! 1) (read (unpack $ params !! 2) :: Int) (params !! 3)
193+
case e of
194+
Left _ -> pure $ errorPayload "Failed to get marbles"
195+
Right _ -> pure $ successPayload $ Just "The payload"
196+
else pure $ errorPayload "Incorrect arguments. Need start key, end key, pageSize and bookmark"
197+
186198
generateResultBytes :: StateQueryIterator -> Text -> IO (Either Error BSU.ByteString)
187199
generateResultBytes sqi text = do
188200
hasNextBool <- hasNext sqi
189201
if hasNextBool then do
190202
eeKV <- next sqi
191203
-- TODO: We need to check that the Either Error KV returned from next
192204
-- is correct and append the showable version of KVs instead of "abc".
193-
generateResultBytes sqi (append text "abc")
205+
case eeKV of
206+
Left e -> pure $ Left e
207+
Right kv ->
208+
let
209+
makeKVString :: Pb.KV -> Text
210+
makeKVString kv_ = pack "Key: " <> TL.toStrict (Pb.kvKey kv_) <> pack ", Value: " <> TSE.decodeUtf8 (kvValue kv_)
211+
in
212+
generateResultBytes sqi (append text (makeKVString kv))
194213
else pure $ Right $ TSE.encodeUtf8 text
195214

196215
parseMarble :: [Text] -> Marble

src/Interfaces.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified Google.Protobuf.Timestamp as GooglePb
1414
import qualified Peer.Proposal as Pb
1515
import qualified Peer.ProposalResponse as Pb
1616
import qualified Peer.Chaincode as Pb
17+
import qualified Peer.ChaincodeShim as Pb
1718

1819

1920
import Types
@@ -37,9 +38,7 @@ class ChaincodeStubInterface ccs where
3738
-- setStateValidationParameter :: ccs -> String -> [ByteString] -> Maybe Error
3839
-- getStateValiationParameter :: ccs -> String -> Either Error [ByteString]
3940
getStateByRange :: ccs -> Text -> Text -> IO (Either Error StateQueryIterator)
40-
41-
-- TODO: We need to implement this so we can test the fetchNextQueryResult functionality
42-
-- getStateByRangeWithPagination :: ccs -> String -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
41+
getStateByRangeWithPagination :: ccs -> Text -> Text -> Int -> Text -> IO (Either Error (StateQueryIterator, Pb.QueryResponseMetadata))
4342

4443
-- getStateByPartialCompositeKey :: ccs -> String -> [String] -> Either Error StateQueryIterator
4544
-- getStateByPartialCompositeKeyWithPagination :: ccs -> String -> [String] -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)

src/Stub.hs

Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -127,34 +127,37 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
127127
getStateByRange ccs startKey endKey =
128128
let payload = getStateByRangePayload startKey endKey
129129
message = buildChaincodeMessage GET_STATE_BY_RANGE payload (txId ccs) (channelId ccs)
130-
-- We have listenForResponse a :: IO (Either Error ByteString)
131-
-- and the function bsToSqi :: ByteString -> IO (Either Error StateQueryIterator)
132-
-- And want IO (Either Error StateQueryIterator)
133130
-- ExceptT is a monad transformer that allows us to compose these by binding over IO Either
134131
bsToSqi :: ByteString -> ExceptT Error IO StateQueryIterator
135-
bsToSqi bs = let eeaQueryResponse = parse (decodeMessage (FieldNumber 1)) bs :: Either ParseError Pb.QueryResponse in
136-
case eeaQueryResponse of
137-
-- TODO: refactor out pattern matching, e.g. using >>= or <*>
138-
Left err -> ExceptT $ pure $ Left $ DecodeError err
139-
Right queryResponse -> ExceptT $ do
140-
-- queryResponse and currentLoc are IORefs as they need to be mutated
141-
-- as a part of the next() function
142-
queryResponseIORef <- newIORef queryResponse
143-
currentLocIORef <- newIORef 0
144-
pure $ Right StateQueryIterator
145-
{ sqiChaincodeStub = ccs
146-
, sqiChannelId = getChannelId ccs
147-
, sqiTxId = getTxId ccs
148-
, sqiResponse = queryResponseIORef
149-
, sqiCurrentLoc = currentLocIORef
150-
}
151-
in do
132+
bsToSqi bs =
133+
let eeaQueryResponse = parse (decodeMessage (FieldNumber 1)) bs :: Either ParseError Pb.QueryResponse
134+
in
135+
case eeaQueryResponse of
136+
-- TODO: refactor out pattern matching, e.g. using >>= or <*>
137+
Left err -> ExceptT $ pure $ Left $ DecodeError err
138+
Right queryResponse -> ExceptT $ do
139+
-- queryResponse and currentLoc are IORefs as they need to be mutated
140+
-- as a part of the next() function
141+
queryResponseIORef <- newIORef queryResponse
142+
currentLocIORef <- newIORef 0
143+
pure $ Right StateQueryIterator {
144+
sqiChaincodeStub = ccs
145+
, sqiChannelId = getChannelId ccs
146+
, sqiTxId = getTxId ccs
147+
, sqiResponse = queryResponseIORef
148+
, sqiCurrentLoc = currentLocIORef
149+
}
150+
in do
152151
e <- (sendStream ccs) message
153152
case e of
154153
Left err -> error ("Error while streaming: " ++ show err)
155154
Right _ -> pure ()
156155
runExceptT $ ExceptT (listenForResponse (recvStream ccs)) >>= bsToSqi
157156

157+
-- TODO: We need to implement this so we can test the fetchNextQueryResult functionality
158+
-- getStateByRangeWithPagination :: ccs -> String -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
159+
getStateByRangeWithPagination ccs startKey endKey pageSize bookmark = pure $ Left $ Error "Not implemented"
160+
158161
-- TODO : implement all these interface functions
159162
instance StateQueryIteratorInterface StateQueryIterator where
160163
-- hasNext :: sqi -> IO Bool
@@ -221,75 +224,73 @@ fetchNextQueryResult sqi = do
221224
Right _ -> pure ()
222225
runExceptT $ ExceptT (listenForResponse (recvStream $ sqiChaincodeStub sqi)) >>= bsToQueryResponse
223226

224-
--
225-
-- -- getStateByRangeWithPagination :: ccs -> String -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
226-
-- getStateByRangeWithPagination ccs startKey endKey pageSize bookmark = Left notImplemented
227-
--
228-
-- -- getStateByPartialCompositeKey :: ccs -> String -> [String] -> Either Error StateQueryIterator
229-
-- getStateByPartialCompositeKey ccs objectType keys = Left notImplemented
230-
--
231-
-- --getStateByPartialCompositeKeyWithPagination :: ccs -> String -> [String] -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
232-
-- getStateByPartialCompositeKeyWithPagination ccs objectType keys pageSize bookmark = Left notImplemented
233-
--
234-
-- --createCompositeKey :: ccs -> String -> [String] -> Either Error String
235-
-- createCompositeKey ccs objectType keys = Left notImplemented
236-
--
237-
-- --splitCompositeKey :: ccs -> String -> Either Error (String, [String])
238-
-- splitCompositeKey ccs key = Left notImplemented
239-
--
240-
-- --getQueryResult :: ccs -> String -> Either Error StateQueryIterator
241-
-- getQueryResult ccs query = Left notImplemented
242-
--
243-
-- --getQueryResultWithPagination :: ccs -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
244-
-- getQueryResultWithPagination ccs key pageSize bookmark = Left notImplemented
245-
--
246-
-- --getHistoryForKey :: ccs -> String -> Either Error HistoryQueryIterator
247-
-- getHistoryForKey ccs key = Left notImplemented
248-
--
249-
-- --getPrivateData :: ccs -> String -> String -> Either Error ByteString
250-
-- getPrivateData ccs collection key = Left notImplemented
251-
--
252-
-- --getPrivateDataHash :: ccs -> String -> String -> Either Error ByteString
253-
-- getPrivateDataHash ccs collection key = Left notImplemented
254-
--
255-
-- --putPrivateData :: ccs -> String -> String -> ByteString -> Maybe Error
256-
-- putPrivateData ccs collection string value = Right notImplemented
257-
--
258-
-- --delPrivateData :: ccs -> String -> String -> Maybe Error
259-
-- delPrivateData ccs collection key = Right notImplemented
260-
--
261-
-- --setPrivateDataValidationParameter :: ccs -> String -> String -> ByteArray -> Maybe Error
262-
-- setPrivateDataValidationParameter ccs collection key params = Right notImplemented
263-
--
264-
-- --getPrivateDataValidationParameter :: ccs -> String -> String -> Either Error ByteString
265-
-- getPrivateDataValidationParameter ccs collection key = Left notImplemented
266-
--
267-
-- --getPrivateDataByRange :: ccs -> String -> String -> String -> Either Error StateQueryIterator
268-
-- getPrivateDataByRange ccs collection startKey endKey = Left notImplemented
269-
--
270-
-- --getPrivateDataByPartialCompositeKey :: ccs -> String -> String -> [String] -> Either Error StateQueryIterator
271-
-- getPrivateDataByPartialCompositeKey ccs collection objectType keys = Left notImplemented
272-
--
273-
-- -- getPrivateDataQueryResult :: ccs -> String -> String -> Either Error StateQueryIterator
274-
-- getPrivateDataQueryResult ccs collection query = Left notImplemented
275-
--
276-
-- -- getCreator :: ccs -> Either Error ByteArray
277-
-- getCreator ccs = Right creator
278-
--
279-
-- -- getTransient :: ccs -> Either Error MapStringBytes
280-
-- getTransient ccs = Right transient
281-
--
282-
-- -- getBinding :: ccs -> Either Error MapStringBytes
283-
-- getBinding ccs = Right binding
284-
--
285-
-- -- getDecorations :: ccs -> MapStringBytes
286-
-- getDecorations ccs = Right decorations
287-
--
288-
-- -- getSignedProposal :: ccs -> Either Error Pb.SignedProposal
289-
-- getSignedProposal ccs = Right signedProposal
290-
--
291-
-- -- getTxTimestamp :: ccs -> Either Error Pb.Timestamp
292-
-- getTxTimestamp ccs = Right txTimestamp
293-
--
294-
-- -- setEvent :: ccs -> String -> ByteArray -> Maybe Error
295-
-- setEvent ccs = Right notImplemented
227+
228+
--
229+
-- -- getStateByPartialCompositeKey :: ccs -> String -> [String] -> Either Error StateQueryIterator
230+
-- getStateByPartialCompositeKey ccs objectType keys = Left notImplemented
231+
--
232+
-- --getStateByPartialCompositeKeyWithPagination :: ccs -> String -> [String] -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
233+
-- getStateByPartialCompositeKeyWithPagination ccs objectType keys pageSize bookmark = Left notImplemented
234+
--
235+
-- --createCompositeKey :: ccs -> String -> [String] -> Either Error String
236+
-- createCompositeKey ccs objectType keys = Left notImplemented
237+
--
238+
-- --splitCompositeKey :: ccs -> String -> Either Error (String, [String])
239+
-- splitCompositeKey ccs key = Left notImplemented
240+
--
241+
-- --getQueryResult :: ccs -> String -> Either Error StateQueryIterator
242+
-- getQueryResult ccs query = Left notImplemented
243+
--
244+
-- --getQueryResultWithPagination :: ccs -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
245+
-- getQueryResultWithPagination ccs key pageSize bookmark = Left notImplemented
246+
--
247+
-- --getHistoryForKey :: ccs -> String -> Either Error HistoryQueryIterator
248+
-- getHistoryForKey ccs key = Left notImplemented
249+
--
250+
-- --getPrivateData :: ccs -> String -> String -> Either Error ByteString
251+
-- getPrivateData ccs collection key = Left notImplemented
252+
--
253+
-- --getPrivateDataHash :: ccs -> String -> String -> Either Error ByteString
254+
-- getPrivateDataHash ccs collection key = Left notImplemented
255+
--
256+
-- --putPrivateData :: ccs -> String -> String -> ByteString -> Maybe Error
257+
-- putPrivateData ccs collection string value = Right notImplemented
258+
--
259+
-- --delPrivateData :: ccs -> String -> String -> Maybe Error
260+
-- delPrivateData ccs collection key = Right notImplemented
261+
--
262+
-- --setPrivateDataValidationParameter :: ccs -> String -> String -> ByteArray -> Maybe Error
263+
-- setPrivateDataValidationParameter ccs collection key params = Right notImplemented
264+
--
265+
-- --getPrivateDataValidationParameter :: ccs -> String -> String -> Either Error ByteString
266+
-- getPrivateDataValidationParameter ccs collection key = Left notImplemented
267+
--
268+
-- --getPrivateDataByRange :: ccs -> String -> String -> String -> Either Error StateQueryIterator
269+
-- getPrivateDataByRange ccs collection startKey endKey = Left notImplemented
270+
--
271+
-- --getPrivateDataByPartialCompositeKey :: ccs -> String -> String -> [String] -> Either Error StateQueryIterator
272+
-- getPrivateDataByPartialCompositeKey ccs collection objectType keys = Left notImplemented
273+
--
274+
-- -- getPrivateDataQueryResult :: ccs -> String -> String -> Either Error StateQueryIterator
275+
-- getPrivateDataQueryResult ccs collection query = Left notImplemented
276+
--
277+
-- -- getCreator :: ccs -> Either Error ByteArray
278+
-- getCreator ccs = Right creator
279+
--
280+
-- -- getTransient :: ccs -> Either Error MapStringBytes
281+
-- getTransient ccs = Right transient
282+
--
283+
-- -- getBinding :: ccs -> Either Error MapStringBytes
284+
-- getBinding ccs = Right binding
285+
--
286+
-- -- getDecorations :: ccs -> MapStringBytes
287+
-- getDecorations ccs = Right decorations
288+
--
289+
-- -- getSignedProposal :: ccs -> Either Error Pb.SignedProposal
290+
-- getSignedProposal ccs = Right signedProposal
291+
--
292+
-- -- getTxTimestamp :: ccs -> Either Error Pb.Timestamp
293+
-- getTxTimestamp ccs = Right txTimestamp
294+
--
295+
-- -- setEvent :: ccs -> String -> ByteArray -> Maybe Error
296+
-- setEvent ccs = Right notImplemented

0 commit comments

Comments
 (0)