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

Commit c0febb7

Browse files
authored
Merge pull request #22 from airvin/iterator-methods
Iterator methods
2 parents e736957 + f5b854e commit c0febb7

File tree

8 files changed

+548
-549
lines changed

8 files changed

+548
-549
lines changed

brittany.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
conf_layout:
2+
lconfig_reformatModulePreamble: true
3+
lconfig_altChooser:
4+
tag: AltChooserBoundedSearch
5+
contents: 3
6+
lconfig_allowSingleLineExportList: false
7+
lconfig_importColumn: 50
8+
lconfig_hangingTypeSignature: false
9+
lconfig_importAsColumn: 50
10+
lconfig_alignmentLimit: 30
11+
lconfig_indentListSpecial: true
12+
lconfig_indentAmount: 4
13+
lconfig_alignmentBreakOnMultiline: true
14+
lconfig_cols: 120
15+
lconfig_indentPolicy: IndentPolicyFree
16+
lconfig_indentWhereSpecial: true
17+
lconfig_columnAlignMode:
18+
tag: ColumnAlignModeMajority
19+
contents: 0.7

examples/Marbles.hs

Lines changed: 182 additions & 184 deletions
Large diffs are not rendered by default.

examples/Sacc.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Peer.ProposalResponse as Pb
1515

1616
import Data.Text ( Text )
1717
import Data.Text.Encoding ( encodeUtf8 )
18-
import Data.ByteString.UTF8 as BSU
18+
import Data.ByteString.UTF8 as BSU ( ByteString, toString )
1919
import Debug.Trace
2020

2121
main :: IO ()

src/Helper.hs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{-# LANGUAGE OverloadedStrings #-}
2-
2+
33
module Helper where
44

5-
import Data.Bifunctor ( first )
5+
import Data.Bifunctor ( first )
66
import Proto3.Suite as Suite
77
import Common.Common as Pb
88
import Peer.ChaincodeShim as Pb
@@ -11,33 +11,29 @@ import Peer.Proposal as Pb
1111
import Peer.ProposalResponse as Pb
1212
import Types ( Error(..)
1313
, ChaincodeStub(..)
14-
, MapTextBytes)
15-
14+
, MapTextBytes
15+
)
16+
1617

1718
-- These are some helper functions to process the unmarshalling of different types
1819
-- from the chaincode message in order to populate the stub
1920
getChaincodeInput :: ChaincodeMessage -> Either Error Pb.ChaincodeInput
2021
getChaincodeInput mes = first DecodeError $ Suite.fromByteString (chaincodeMessagePayload mes)
2122

2223
getProposal :: Pb.SignedProposal -> Either Error Pb.Proposal
23-
getProposal signedProposal =
24-
first DecodeError $ Suite.fromByteString (signedProposalProposalBytes signedProposal)
24+
getProposal signedProposal = first DecodeError $ Suite.fromByteString (signedProposalProposalBytes signedProposal)
2525

2626
getHeader :: Pb.Proposal -> Either Error Pb.Header
27-
getHeader proposal =
28-
first DecodeError $ Suite.fromByteString (proposalHeader proposal)
27+
getHeader proposal = first DecodeError $ Suite.fromByteString (proposalHeader proposal)
2928

3029
getChannelHeader :: Pb.Header -> Either Error Pb.ChannelHeader
31-
getChannelHeader header =
32-
first DecodeError $ Suite.fromByteString (headerChannelHeader header)
30+
getChannelHeader header = first DecodeError $ Suite.fromByteString (headerChannelHeader header)
3331

3432
getChaincodeProposalPayload :: Pb.Proposal -> Either Error Pb.ChaincodeProposalPayload
35-
getChaincodeProposalPayload proposal =
36-
first DecodeError $ Suite.fromByteString (proposalPayload proposal)
33+
getChaincodeProposalPayload proposal = first DecodeError $ Suite.fromByteString (proposalPayload proposal)
3734

3835
getSignatureHeader :: Pb.Header -> Either Error Pb.SignatureHeader
39-
getSignatureHeader header =
40-
first DecodeError $ Suite.fromByteString (headerSignatureHeader header)
36+
getSignatureHeader header = first DecodeError $ Suite.fromByteString (headerSignatureHeader header)
4137

4238
-- -- TODO: Use ChannelHeader and SignatureHeader to implement getBinding
4339
createBinding :: Pb.Proposal -> Maybe MapTextBytes

src/Interfaces.hs

Lines changed: 320 additions & 330 deletions
Large diffs are not rendered by default.

src/Shim.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ biDiRequestFn chaincodeStub _call _mmap recv send _done = do
8585
chatWithPeer recv send chaincodeStub
8686

8787
-- main loop listening for messages from the peer
88+
chatWithPeer :: IO (Either GRPCIOError (Maybe ChaincodeMessage)) -> StreamSend ChaincodeMessage -> ChaincodeStub -> IO b
8889
chatWithPeer recv send chaincodeStub = do
8990
res <- recv
9091
case res of

src/Stub.hs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,39 +102,34 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
102102
Nothing -> Left $ Error "ChannelHeader doesn't have a timestamp"
103103
Just timestamp -> Right timestamp
104104
Nothing -> Left $ Error "Chaincode stub doesn't has a proposal to get the timestamp from"
105-
106-
-- invokeChaincode :: ccs -> String -> [ByteString] -> String -> Pb.Response
107-
-- invokeChaincode ccs cc params = Pb.Response{ responseStatus = 500, responseMessage = message(notImplemented), responsePayload = Nothing }
108-
--
109-
-- getState :: ccs -> Text -> IO (Either Error ByteString)
105+
106+
-- getState :: ccs -> Text -> ExceptT Error IO ByteString
110107
getState ccs key =
111108
let payload = getStatePayload key
112109
message =
113110
buildChaincodeMessage GET_STATE payload (txId ccs) (channelId ccs)
114-
in do
111+
in ExceptT $ do
115112
e <- (sendStream ccs) message
116113
case e of
117-
Left err -> error ("Error while streaming: " ++ show err)
118-
Right _ -> pure ()
119-
listenForResponse (recvStream ccs)
114+
Left err -> pure $ Left $ Error $ "Error while streaming: " ++ show err
115+
Right _ -> listenForResponse (recvStream ccs)
120116

121-
-- putState :: ccs -> Text -> ByteString -> Maybe Error
117+
-- putState :: ccs -> Text -> ByteString -> ExceptT Error IO ByteString
122118
putState ccs key value =
123119
let payload = putStatePayload key value
124120
message =
125121
buildChaincodeMessage PUT_STATE payload (txId ccs) (channelId ccs)
126-
in do
122+
in ExceptT $ do
127123
e <- (sendStream ccs) message
128124
case e of
129-
Left err -> error ("Error while streaming: " ++ show err)
130-
Right _ -> pure ()
131-
listenForResponse (recvStream ccs)
132-
125+
Left err -> pure $ Left $ Error $ "Error while streaming: " ++ show err
126+
Right _ -> listenForResponse (recvStream ccs)
127+
133128
-- delState :: ccs -> Text -> IO (Maybe Error)
134129
delState ccs key =
135130
let payload = delStatePayload key
136131
message = buildChaincodeMessage DEL_STATE payload (txId ccs) (channelId ccs)
137-
in do
132+
in ExceptT $ do
138133
e <- (sendStream ccs) message
139134
case e of
140135
Left err -> error ("Error while streaming: " ++ show err)
@@ -154,7 +149,7 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
154149
getStateByRange ccs startKey endKey =
155150
let payload = getStateByRangePayload startKey endKey Nothing
156151
message = buildChaincodeMessage GET_STATE_BY_RANGE payload (txId ccs) (channelId ccs)
157-
in do
152+
in ExceptT $ do
158153
e <- (sendStream ccs) message
159154
case e of
160155
Left err -> error ("Error while streaming: " ++ show err)
@@ -170,7 +165,7 @@ instance ChaincodeStubInterface DefaultChaincodeStub where
170165
}
171166
payload = (trace "Building getStateByRangeWithPagination payload") getStateByRangePayload startKey endKey $ Just metadata
172167
message = buildChaincodeMessage GET_STATE_BY_RANGE payload (txId ccs) (channelId ccs)
173-
in do
168+
in ExceptT $ do
174169
e <- (sendStream ccs) message
175170
case e of
176171
Left err -> error ("Error while streaming: " ++ show err)
@@ -190,7 +185,7 @@ instance StateQueryIteratorInterface StateQueryIterator where
190185
-- close :: sqi -> IO (Maybe Error)
191186
close _ = pure Nothing
192187
-- next :: sqi -> IO (Either Error Pb.KV)
193-
next sqi = do
188+
next sqi = ExceptT $ do
194189
eeQueryResultBytes <- nextResult sqi
195190
case eeQueryResultBytes of
196191
Left _ -> pure $ Left $ Error "Error getting next queryResultBytes"

src/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Network.GRPC.HighLevel
1717
import Peer.ChaincodeShim as Pb
1818
import Google.Protobuf.Timestamp as Pb
1919
import Peer.Proposal as Pb
20-
import Peer.ProposalResponse as Pb
20+
import Peer.ProposalResponse as Pb ( Response )
2121

2222
data Error = GRPCError GRPCIOError
2323
| InvalidArgs

0 commit comments

Comments
 (0)