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

Commit a59b4c2

Browse files
committed
Merged upstream
2 parents 7f1e712 + 26f9598 commit a59b4c2

File tree

3 files changed

+115
-99
lines changed

3 files changed

+115
-99
lines changed

examples/Marbles.hs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Shim ( start
1616
)
1717

1818
import Peer.ProposalResponse as Pb
19+
import Ledger.Queryresult.KvQueryResult as Pb
1920

2021
import Data.Text ( Text
2122
, unpack
@@ -26,6 +27,7 @@ import qualified Data.Text.Encoding as TSE
2627
import qualified Data.ByteString as BS
2728
import qualified Data.ByteString.UTF8 as BSU
2829
import qualified Data.ByteString.Lazy as LBS
30+
import qualified Data.Text.Lazy as TL
2931

3032
import Data.Aeson ( ToJSON
3133
, FromJSON
@@ -85,8 +87,8 @@ invokeFunc s =
8587
-- Right ("getHistoryForMarble", parameters) ->
8688
-- getHistoryForMarble s parameters
8789
Right ("getMarblesByRange", parameters) -> getMarblesByRange s parameters
88-
-- Right ("getMarblesByRangeWithPagination", parameters) ->
89-
-- getMarblesByRangeWithPagination s parameters
90+
Right ("getMarblesByRangeWithPagination", parameters) ->
91+
getMarblesByRangeWithPagination s parameters
9092
-- Right ("queryMarblesWithPagination", parameters) ->
9193
-- queryMarblesWithPagination s parameters
9294
Right (fn , _ ) -> pure
@@ -175,16 +177,30 @@ getMarblesByRange s params = if Prelude.length params == 2
175177
trace (show resultBytes) (pure $ successPayload Nothing)
176178
else pure $ errorPayload "Incorrect arguments. Need a start key and an end key"
177179

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+
178189
generateResultBytes :: StateQueryIterator -> Text -> IO (Either Error BSU.ByteString)
179190
generateResultBytes sqi text = do
180191
hasNextBool <- hasNext sqi
181192
if hasNextBool then do
182193
eeKV <- next sqi
183194
-- TODO: We need to check that the Either Error KV returned from next
184195
-- is correct and append the showable version of KVs instead of "abc".
185-
case eeKV of
186-
Right kv -> generateResultBytes sqi (append text $ pack $ show kv)
196+
case eeKV of
187197
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))
188204
else pure $ Right $ TSE.encodeUtf8 text
189205

190206
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
-- TODO: remove the IO from this function (possibly with the State monad)
@@ -222,75 +225,73 @@ fetchNextQueryResult sqi = do
222225
Right _ -> pure ()
223226
runExceptT $ ExceptT (listenForResponse (recvStream $ sqiChaincodeStub sqi)) >>= bsToQueryResponse
224227

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

0 commit comments

Comments
 (0)