Skip to content

Commit 15bb965

Browse files
committed
Implemented Publisher.createMintFsTx method
1 parent cd06fea commit 15bb965

File tree

21 files changed

+583
-313
lines changed

21 files changed

+583
-313
lines changed
Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
{-# LANGUAGE BlockArguments #-}
2+
{-# OPTIONS_GHC -Wno-missing-import-lists #-}
23

34
module FactStatementStoreGrpc (factStatementStoreService, FactStatementStoreGrpcOpts (FactStatementStoreGrpcOpts)) where
45

56
import BeamConfig (FactStatementT (_factStatementId, _json), FsStore (fsTbl), fsStoreSettings)
67
import Cardano.Proto.Aux ()
7-
import Control.Lens (makeLenses, (&), (.~), (^.), (^..))
8+
import Control.Lens (makeLenses, (&), (.~), (^.))
89
import Data.Aeson (json)
910
import Data.Aeson.Parser (decodeStrictWith)
1011
import Data.Functor.Identity (Identity)
1112
import Data.List (nub)
1213
import Data.ProtoLens (Message (defMessage))
1314
import Data.String (IsString (fromString))
15+
import Data.Text (Text)
16+
import Data.Text qualified as Text
1417
import Data.Traversable (for)
1518
import Database.Beam (SqlValable (val_), runSelectReturningOne)
1619
import Database.Beam.Query (SqlEq ((==.)), all_, filter_, select)
@@ -31,10 +34,9 @@ import Network.Wai.Handler.Warp qualified as Warp
3134
import Network.Wai.Handler.WarpTLS (tlsSettings)
3235
import PlutusJson (jsonToPlutusData)
3336
import PlutusTx (fromData)
34-
import Proto.Coop_Fields (value)
35-
import Proto.FactStatementStoreService (FactStatementStore, GetFactStatementRequest, GetFactStatementResponse, GetFactStatementResponse'FsIdAndPlutus)
36-
import Proto.FactStatementStoreService_Fields (fsId, fsIds, fsIdsWithPlutus, msg, plutusData)
37-
import Proto.FactStatementStoreService_Fields qualified as Proto
37+
import Proto.FactStatementStoreService (FactStatementStore, GetFactStatementRequest, GetFactStatementResponse, Success'FsIdAndPlutus)
38+
import Proto.FactStatementStoreService_Fields (error, fsId, fsIds, fsIdsWithPlutus, msg, otherErr, plutusData, success)
39+
import Prelude hiding (error, succ)
3840

3941
data FactStatementStoreGrpcOpts = FactStatementStoreGrpcOpts
4042
{ _db :: FilePath
@@ -72,62 +74,41 @@ runServer routes (h, p) (certFile, keyFile) = do
7274
, Encoding.gzip
7375
]
7476

75-
type Fs = FactStatementT Identity
77+
type FsT = FactStatementT Identity
7678

7779
handleReq :: FilePath -> Server.UnaryHandler IO GetFactStatementRequest GetFactStatementResponse
7880
handleReq dbPath _ req = do
7981
putStrLn $ "Establishing the database connection to: " <> dbPath
8082
fsDb <- open dbPath
8183
let fsTbl' = fsTbl fsStoreSettings
82-
ids = nub $ req ^. fsIds ^.. traverse . value
84+
ids = nub $ req ^. fsIds
8385

84-
mayIdsWithPs <-
85-
sequence
86-
<$> for
87-
ids
88-
( \i -> do
89-
(mayFsWithId :: Maybe Fs) <- runBeamSqliteDebug Prelude.putStrLn fsDb $ runSelectReturningOne (select $ filter_ (\fs -> _factStatementId fs ==. val_ i) (all_ fsTbl'))
90-
maybe
91-
(putStrLn ("Not found requested fact statement with id " <> show i) >> return Nothing)
92-
(return . Just)
93-
mayFsWithId
94-
)
86+
idsWithRes :: [Either Text Success'FsIdAndPlutus] <-
87+
for
88+
ids
89+
( \i -> do
90+
(mayFsT :: Maybe FsT) <- runBeamSqliteDebug Prelude.putStrLn fsDb $ runSelectReturningOne (select $ filter_ (\fs -> _factStatementId fs ==. val_ i) (all_ fsTbl'))
91+
maybe
92+
(return (Left $ Text.pack "Not found requested Fact Statement with ID " <> (Text.pack . show $ i)))
93+
( \fs -> do
94+
let maySucc :: Maybe Success'FsIdAndPlutus = do
95+
decoded <- decodeStrictWith json return (_json @Identity $ fs)
96+
let plData = jsonToPlutusData decoded
97+
prData <- fromData plData
98+
return $
99+
defMessage
100+
& fsId .~ _factStatementId @Identity fs
101+
& plutusData .~ prData
95102

96-
idsWithPs <-
97-
maybe
98-
(putStrLn "There were errors processing the requests" >> return [])
99-
return
100-
mayIdsWithPs
103+
maybe (return (Left $ Text.pack "Failed formatting to PlutusData for Fact Statement with ID: " <> (Text.pack . show $ i))) (return . Right) maySucc
104+
)
105+
mayFsT
106+
)
101107

102-
mayIdsWithPs' <-
103-
sequence
104-
<$> for
105-
idsWithPs
106-
( \idWithP -> do
107-
let fsIdAndP :: Maybe GetFactStatementResponse'FsIdAndPlutus = do
108-
decoded <- decodeStrictWith json return (_json @Identity $ idWithP)
109-
let plData = jsonToPlutusData decoded
110-
prData <- fromData plData
111-
return $
112-
defMessage
113-
& fsId .~ _factStatementId @Identity idWithP
114-
& plutusData .~ prData
115-
maybe
116-
( do
117-
putStrLn $ "Failed formatting a fact statement with id" <> show (_factStatementId @Identity idWithP)
118-
return Nothing
119-
)
120-
(return . Just)
121-
fsIdAndP
122-
)
123-
maybe
124-
( return $
125-
defMessage
126-
& Proto.error . msg .~ "Failed processing request"
127-
)
128-
( \idsWithPs' ->
129-
return $
130-
defMessage
131-
& Proto.success . fsIdsWithPlutus .~ idsWithPs'
132-
)
133-
mayIdsWithPs'
108+
-- If any contains an error report that
109+
let allSuccOrErr = sequence idsWithRes
110+
return $
111+
either
112+
(\err -> defMessage & error . otherErr . msg .~ err)
113+
(\succ -> defMessage & success . fsIdsWithPlutus .~ succ)
114+
allSuccOrErr

coop-pab/app/Coop/Cli/TxBuilderGrpc.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import Data.Text (Text, unpack)
3232
import Data.Text qualified as Text
3333
import GHC.Exts (fromString)
3434
import Ledger (PaymentPubKeyHash (PaymentPubKeyHash), TxId)
35-
import Proto.Plutus_Fields (cborBase16)
35+
import Proto.Cardano_Fields (cborBase16)
3636
import Proto.TxBuilderService_Fields (gcFsTx, info, mintFsTx, msg, otherErr, submitter, success)
3737
import Proto.TxBuilderService_Fields qualified as Proto.TxBuilderService
3838
import System.Directory (doesFileExist, makeAbsolute)

coop-pab/aux.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function coop-run-tx-builder-grpc {
6161

6262
function coop-garbage-collect {
6363
make-exports
64-
cabal run coop-pab-cli -- garbage-collect --cert-rdmr-wallet $CERT-RDMR
64+
cabal run coop-pab-cli -- garbage-collect --cert-rdmr-wallet $CERT_RDMR_WALLET
6565
}
6666

6767
function coop-get-state {

coop-proto/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ grpcui:
44
protogen:
55
protoc -I . --plugin=protoc-gen-haskell=`which proto-lens-protoc` \
66
--haskell_out proto_out \
7-
fact-statement-store-service.proto coop.proto plutus.proto tx-builder-service.proto
7+
fact-statement-store-service.proto cardano.proto tx-builder-service.proto publisher-service.proto

coop-proto/cardano-proto-extras/src/Cardano/Proto/Aux.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import Ledger qualified
1616
import Plutus.V1.Ledger.Api (BuiltinData (BuiltinData), ToData (toBuiltinData), fromBuiltin, toBuiltin)
1717
import PlutusTx (FromData (fromBuiltinData), builtinDataToData, dataToBuiltinData)
1818
import PlutusTx qualified
19-
import Proto.Plutus qualified as Proto
20-
import Proto.Plutus_Fields (base16, elements, extended, fields, finiteLedgerTime, idx, index, key, kvs, maybe'plutusData, pdbytes, pdconstr, pdint, pdlist, pdmap, transactionHash, value)
21-
import Proto.Plutus_Fields qualified as PPlutus
19+
import Proto.Cardano qualified as Proto
20+
import Proto.Cardano_Fields (base16, elements, extended, fields, finiteLedgerTime, idx, index, key, kvs, maybe'plutusData, pdbytes, pdconstr, pdint, pdlist, pdmap, transactionHash, value)
21+
import Proto.Cardano_Fields qualified as PCardano
2222

2323
class (MonadFail m) => ProtoCardano m proto cardano where
2424
toCardano :: proto -> m cardano
@@ -51,14 +51,14 @@ instance MonadFail m => ProtoCardano m Proto.ExtendedLedgerTime (Ledger.Extended
5151

5252
instance (MonadFail m) => ProtoCardano m Proto.TxOutRef Ledger.TxOutRef where
5353
toCardano ptxOutRef = do
54-
txId <- toCardano (ptxOutRef ^. PPlutus.txId)
54+
txId <- toCardano (ptxOutRef ^. PCardano.txId)
5555
return $ Ledger.TxOutRef txId (toInteger $ ptxOutRef ^. idx)
5656

5757
fromCardano (Ledger.TxOutRef txId ix) = do
5858
txId' <- fromCardano txId
5959
return $
6060
defMessage
61-
& PPlutus.txId .~ txId'
61+
& PCardano.txId .~ txId'
6262
& idx .~ fromInteger ix
6363

6464
instance (MonadFail m) => ProtoCardano m Proto.TxId Ledger.TxId where

coop-proto/plutus.proto renamed to coop-proto/cardano.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto3";
22

3-
package plutus;
3+
package cardano;
44

55
message Transaction {
66
// CBOR+Base16 (hex) encoded transaction

coop-proto/coop.proto

Lines changed: 0 additions & 7 deletions
This file was deleted.

coop-proto/fact-statement-store-service.proto

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
syntax = "proto3";
22

3-
import "plutus.proto";
4-
import "coop.proto";
3+
import "cardano.proto";
54

6-
package coop;
5+
package coop.fact_statement_store;
76

87
service FactStatementStore {
98
// Fetch a PlutusData encoded Fact Statement with a given ID
109
rpc getFactStatement(GetFactStatementRequest) returns (GetFactStatementResponse) {}
1110
}
1211

1312
message GetFactStatementRequest {
14-
repeated coop.FactStatementId fs_ids = 1;
13+
repeated bytes fs_ids = 1;
1514
}
1615

17-
message GetFactStatementResponse {
16+
message Success {
1817
message FsIdAndPlutus {
1918
bytes fs_id = 1;
20-
plutus.PlutusData plutus_data = 2;
21-
}
22-
message Success {
23-
repeated FsIdAndPlutus fs_ids_with_plutus = 1;
19+
cardano.PlutusData plutus_data = 2;
2420
}
25-
message Error {
21+
repeated FsIdAndPlutus fs_ids_with_plutus = 1;
22+
}
23+
24+
// Error associated with above response messages
25+
// TODO: Should catch and properly report classes of errors users would expect to trigger.
26+
// For example:
27+
// - Fact statement ID not found
28+
message Error {
29+
message OtherError {
30+
// Some other error message
2631
string msg = 1;
2732
}
33+
oneof someError {
34+
// Some other error
35+
OtherError other_err = 1;
36+
}
37+
}
38+
39+
message GetFactStatementResponse {
2840
oneof factStatementsOrErr {
2941
// Error encountered when servicing the request
3042
Error error = 1;

coop-proto/publisher-service.proto

Lines changed: 43 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,69 @@
11
syntax = "proto3";
22

3-
import "google/protobuf/empty.proto";
3+
import "cardano.proto";
4+
import "tx-builder-service.proto";
5+
import "fact-statement-store-service.proto";
46

5-
package coop;
7+
package coop.publisher;
68

79
service Publisher {
8-
// Create a Fact Statement minting transaction (FSMintTx)
9-
rpc createFsMintTransaction(CreateFsMintTransactionRequest) returns (CreateFsMintTransactionResponse) {}
10-
// Create a Fact Statement burning transaction (FSBurnTx)
11-
rpc createFsBurnTransaction(CreateFsBurnTransactionRequest) returns (CreateFsBurnTransactionResponse) {}
10+
// Create a Fact Statement minting transaction (mint-fact-statement-tx)
11+
rpc createMintFsTx(CreateMintFsTxRequest) returns (CreateMintFsTxResponse) {}
12+
// Create a Fact Statement garbage collection transaction (gc-fact-statement-tx)
13+
rpc createGcFsTx(CreateGcFsTxRequest) returns (CreateGcFsTxResponse) {}
1214
}
1315

14-
service Collector {
15-
// Fetch a Fact Statement with a given ID
16-
rpc getFactStatement(GetFactStatementRequest) returns (GetFactStatementResponse) {}
17-
}
16+
message CreateMintFsTxRequest {
17+
message FactStatementInfo {
18+
// Fact Statement identifier known by the Oracle's Fact Store
19+
bytes fs_id = 1;
20+
// Extended ledger time after which the created Fact Statement UTxO at @FsV can be spent by the Submitter
21+
cardano.ExtendedLedgerTime gcAfter = 2;
22+
}
1823

19-
message GetFactStatementRequest {
20-
repeated FactStatementId fs_ids = 1;
24+
// A list of Fact Statement information containing the ID and time-to-live
25+
repeated FactStatementInfo fs_infos = 1;
26+
// The PubKeyHash of the user that will submit the transaction
27+
cardano.PubKeyHash submitter = 2;
2128
}
2229

23-
message GetFactStatementResponse {
24-
message FSById {
25-
map<FactStatementId, FactStatement> value = 1;
26-
}
27-
oneof factStatementsOrErr {
28-
// Fetched Fact Statements
29-
FSById fs_by_id = 1;
30+
message CreateMintFsTxResponse {
31+
oneof transactionOrErr {
3032
// Error encountered when servicing the request
31-
Error error = 2;
33+
Error error = 1;
34+
// Fact Statement Minting transaction (mint-fact-statement-tx) that must be signed by the Submitter and submitted
35+
cardano.Transaction mint_fs_tx = 2;
36+
}
37+
message Info {
38+
coop.tx_builder.MintFsInfo tx_builder_info = 1;
3239
}
40+
Info info = 3;
3341
}
3442

35-
message CreateFsMintTransactionRequest {
36-
message FsParams {
37-
// Fact statement identifier
38-
FactStatementId fs_id = 1;
39-
// Extended ledger time after which the created FSUTxO can be spent by the submitter
40-
ExtendedLedgerTime gcAfter = 2;
41-
}
43+
message CreateGcFsTxRequest {
44+
// Fact Statement IDs to garbage collect
45+
repeated bytes fs_ids = 1;
46+
// The PubKeyHash of the user that submitted the FSMintTx and will also submit the FSBurnTx
47+
cardano.PubKeyHash submitter = 2;
48+
}
4249

43-
// A list of fact statements to mint along with their ttl
44-
repeated FsParams fs_params = 1;
45-
// The PubKeyHash of the user that will submit the transaction
46-
PubKeyHash submitter = 2;
47-
// Submitter owned UTxO to use as a collateral UTxO in the returned FSMintTx
48-
UTxORef collateral = 3;
49-
// Submitter owned UTxO that contains the $FEE tokens requested by the publisher
50-
UTxORef fee = 4;
50+
message CreateGcFsTxResponse {
51+
oneof transactionOrErr {
52+
// Fact Statement garbage collection transaction (gc-fact-statement-tx) to sign and submit
53+
cardano.Transaction gc_fs_tx = 1;
54+
// Error encountered when servicing the request
55+
Error error = 2;
56+
}
5157
}
5258

5359
message Error {
54-
message NotFoundError {
55-
// Fact Statement ID that wasn't found
56-
FactStatementId fs_id = 1;
57-
}
58-
message TimeoutError {
59-
// Timeout error message
60-
string msg = 1;
61-
}
6260
message OtherError {
6361
// Some other error message
6462
string msg = 1;
6563
}
6664
oneof someError {
67-
// Fact Statement ID wasn't found
68-
NotFoundError not_found_err = 1;
69-
// Timeout error
70-
TimeoutError timeout_err = 2;
71-
// Some other error
65+
coop.fact_statement_store.Error fs_store_err = 1;
66+
coop.tx_builder.Error tx_builder_err = 2;
7267
OtherError other_err = 3;
7368
}
7469
}
75-
76-
message CreateFsMintTransactionResponse {
77-
oneof transactionOrErr {
78-
// Fact Statement Minting transaction (abbr. FSMintTx) to sign and submit
79-
Transaction fs_mint_tx = 1;
80-
// Error encountered when servicing the request
81-
Error error = 2;
82-
}
83-
}
84-
85-
message CreateFsBurnTransactionRequest {
86-
// User provided FSUTxOs to spend
87-
repeated UTxORef fs = 1;
88-
// The PubKeyHash of the user that submitted the FSMintTx and will also submit the FSBurnTx
89-
PubKeyHash submitter = 2;
90-
// Submitter owned UTxO to use as a collateral UTxO in the returned FSBurnTx
91-
UTxORef collateral = 3;
92-
}
93-
94-
message CreateFsBurnTransactionResponse {
95-
oneof transactionOrErr {
96-
// Fact Statement Burning transaction (abbr. FSBurnTx) to sign and submit
97-
Transaction fs_burn_tx = 1;
98-
// Error encountered when servicing the request
99-
Error error = 2;
100-
}
101-
}

0 commit comments

Comments
 (0)