33module Stub where
44
55
6+ import Data.Bifunctor
67import Data.ByteString as BS
78import Data.Text
89import Data.Text.Encoding
@@ -13,19 +14,20 @@ import Data.Vector as Vector
1314 , foldr
1415 , empty
1516 )
17+ import qualified Data.ByteString.Lazy as LBS
1618
1719import Peer.ChaincodeShim
1820
1921import Network.GRPC.HighLevel
2022import Google.Protobuf.Timestamp as Pb
2123import Peer.Proposal as Pb
2224import Proto3.Suite
25+ import Proto3.Wire.Decode
2326
2427import Interfaces
2528import Messages
2629import Types
2730
28-
2931-- NOTE: When support for concurrency transaction is added, this function will no longer be required
3032-- as the stub function will block and listen for responses over a channel when the code is concurrent
3133listenForResponse :: StreamRecv ChaincodeMessage -> IO (Either Error ByteString )
@@ -66,7 +68,7 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
6668 -- invokeChaincode :: ccs -> String -> [ByteString] -> String -> Pb.Response
6769 -- invokeChaincode ccs cc params = Pb.Response{ responseStatus = 500, responseMessage = message(notImplemented), responsePayload = Nothing }
6870 --
69- getState :: ccs -> Text -> IO (Either Error ByteString )
71+ -- getState :: ccs -> Text -> IO (Either Error ByteString)
7072 getState ccs key =
7173 let payload = getStatePayload key
7274 message =
@@ -78,7 +80,7 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
7880 Right _ -> pure ()
7981 listenForResponse (recvStream ccs)
8082
81- -- -- putState :: ccs -> Text -> ByteString -> Maybe Error
83+ -- putState :: ccs -> Text -> ByteString -> Maybe Error
8284 putState ccs key value =
8385 let payload = putStatePayload key value
8486 message =
@@ -108,16 +110,27 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
108110 -- -- getStateValiationParameter :: ccs -> String -> Either Error [ByteString]
109111 -- getStateValiationParameter ccs key = Left notImplemented
110112 --
111- -- getStateByRange :: ccs -> Text -> Text -> Either Error StateQueryIterator
112- getStateByRange ccs startKey endKey =
113- let payload = getStateByRangePayload startKey endKey
114- message = buildChaincodeMessage GET_STATE_BY_RANGE payload (txId ccs) (channelId ccs)
115- in do
116- e <- (sendStream ccs) message
117- case e of
118- Left err -> error (" Error while streaming: " ++ show err)
119- Right _ -> pure ()
120- listenForResponse (recvStream ccs)
113+ -- getStateByRange :: ccs -> Text -> Text -> IO (Either Error StateQueryIterator)
114+ getStateByRange ccs startKey endKey =
115+ let payload = getStateByRangePayload startKey endKey
116+ message = buildChaincodeMessage GET_STATE_BY_RANGE payload (txId ccs) (channelId ccs)
117+ bsToSqi :: ByteString -> Either Error StateQueryIterator
118+ bsToSqi bs = let eeaQueryResponse = parse (decodeMessage (FieldNumber 1 )) bs :: Either ParseError QueryResponse in
119+ case eeaQueryResponse of
120+ Left _ -> Left ParseError
121+ Right queryResponse -> Right StateQueryIterator {
122+ sqiChannelId = getChannelId ccs
123+ , sqiTxId = getTxId ccs
124+ , sqiResponse = queryResponse
125+ , sqiCurrentLoc = 0
126+ }
127+ in do
128+ e <- (sendStream ccs) message
129+ case e of
130+ Left err -> error (" Error while streaming: " ++ show err)
131+ Right _ -> pure ()
132+ (bsToSqi =<< ) <$> listenForResponse (recvStream ccs)
133+
121134 --
122135 -- -- getStateByRangeWithPagination :: ccs -> String -> String -> Int32 -> String -> Either Error (StateQueryIterator, Pb.QueryResponseMetadata)
123136 -- getStateByRangeWithPagination ccs startKey endKey pageSize bookmark = Left notImplemented
@@ -191,10 +204,10 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
191204 -- -- setEvent :: ccs -> String -> ByteArray -> Maybe Error
192205 -- setEvent ccs = Right notImplemented
193206
194- instance StateQueryIteratorInterface DefaultStateQueryIterator where
207+ instance StateQueryIteratorInterface StateQueryIterator where
195208 -- hasNext :: sqi -> Bool
196209 hasNext sqi = True
197210 -- close :: sqi -> Maybe Error
198211 close _ = Nothing
199212 -- next :: sqi -> Either Error Pb.KV
200- next _ = Left _
213+ next _ = Left $ Error " not implemented "
0 commit comments