Skip to content

Commit 8efd769

Browse files
committed
Cleaned up TxBuilder service
1 parent 5ddc05d commit 8efd769

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Data.Text qualified as Text
3333
import GHC.Exts (fromString)
3434
import Ledger (PaymentPubKeyHash (PaymentPubKeyHash), TxId)
3535
import Proto.Plutus_Fields (cborBase16)
36-
import Proto.TxBuilderService_Fields (alreadyPublished, gcFsTx, info, mintFsSuccess, mintFsTx, msg, otherErr, submitter, success)
36+
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)
3939
import System.FilePath ((</>))
@@ -79,11 +79,12 @@ txBuilderService opts = do
7979
(runMintFsTxOnReq req)
8080
either
8181
(\err -> return $ defMessage & Proto.TxBuilderService.error . otherErr . msg .~ err)
82-
( \(mayTxId, alreadyPublished') -> do
82+
( \(mayTxId, info') -> do
8383
maybe
8484
( return $
8585
defMessage
86-
& mintFsSuccess . alreadyPublished .~ alreadyPublished'
86+
& Proto.TxBuilderService.error . otherErr . msg .~ "Failed creating mint-fact-statement-tx"
87+
& info .~ info'
8788
)
8889
( \txId -> do
8990
mayRawTx <- readSignedTx pabConf txId
@@ -92,12 +93,13 @@ txBuilderService opts = do
9293
return $
9394
defMessage
9495
& Proto.TxBuilderService.error . otherErr . msg .~ ("Failed creating mint-fact-statement-tx: " <> err)
96+
& info .~ info'
9597
)
9698
( \rawTx ->
9799
return $
98100
defMessage
99-
& mintFsSuccess . alreadyPublished .~ alreadyPublished'
100-
& mintFsSuccess . mintFsTx . cborBase16 .~ rawTx
101+
& success . mintFsTx . cborBase16 .~ rawTx
102+
& info .~ info'
101103
)
102104
mayRawTx
103105
)

coop-pab/coop-pab.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ library
113113
, plutus-tx
114114
, process
115115
, proto-lens
116-
, safe
117116
, stm
118117
, text
119118
, uuid

coop-pab/src/Coop/Pab.hs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Data.Map qualified as Map
3131
import Data.Maybe (catMaybes)
3232
import Data.ProtoLens (Message (defMessage))
3333
import Data.Text (Text)
34+
import Data.Text qualified as Text
3435
import Data.Traversable (for)
3536
import Data.Void (Void)
3637
import Ledger (Language (PlutusV2), POSIXTime (POSIXTime), PaymentPubKeyHash (PaymentPubKeyHash, unPaymentPubKeyHash), TxId, TxOutRef, Versioned (Versioned), after, applyArguments, ciTxOutValue, interval)
@@ -53,17 +54,16 @@ import Plutus.V2.Ledger.Api (
5354
MintingPolicy (MintingPolicy),
5455
POSIXTimeRange,
5556
TokenName (TokenName),
56-
UpperBound (UpperBound),
57+
UpperBound,
5758
Validator (Validator),
5859
Value,
5960
toBuiltin,
6061
toData,
6162
)
6263
import PlutusTx (toBuiltinData)
6364
import PlutusTx.Prelude (Group (inv), fromBuiltin)
64-
import Proto.TxBuilderService (CreateGcFsTxReq, CreateMintFsTxReq, GcFsInfo, MintFsSuccess'FsIdAndTxOutRef)
65-
import Proto.TxBuilderService_Fields (factStatementId, factStatementUtxo, factStatements, fs, fsId, fsIds, gcAfter, notFoundFsIds, obsoleteFsIds, submitter, validFsIds)
66-
import Safe.Foldable (maximumMay)
65+
import Proto.TxBuilderService (CreateGcFsTxReq, CreateMintFsTxReq, GcFsInfo, MintFsInfo)
66+
import Proto.TxBuilderService_Fields (alreadyPublished, factStatements, fs, fsId, fsIds, fsUtxo, gcAfter, notFoundFsIds, obsoleteFsIds, publishedFsIds, submitter, validFsIds)
6767
import Test.Plutip.Internal.BotPlutusInterface.Setup ()
6868
import Test.Plutip.Internal.LocalCluster ()
6969
import Text.Printf (printf)
@@ -451,16 +451,16 @@ findOnchainState coopDeployment authenticators =
451451
toCardanoC :: ProtoCardano (Either Text) proto cardano => proto -> Contract w s Text cardano
452452
toCardanoC proto = let x = toCardano @(Either Text) proto in either throwError return x
453453

454-
mkPublishingSpec :: CoopDeployment -> [PaymentPubKeyHash] -> CreateMintFsTxReq -> Contract w s Text (Maybe PublishingSpec, [MintFsSuccess'FsIdAndTxOutRef])
454+
mkPublishingSpec :: CoopDeployment -> [PaymentPubKeyHash] -> CreateMintFsTxReq -> Contract w s Text (Maybe PublishingSpec, MintFsInfo)
455455
mkPublishingSpec coopDeployment authenticators req = do
456456
when (null $ req ^. factStatements) (throwError "Must have at least one Fact Statement to process")
457457

458458
CoopOnchainState authBatches published <- findOnchainState coopDeployment authenticators
459459
submitterPPkh <- PaymentPubKeyHash <$> toCardanoC (req ^. submitter)
460460
let alreadyPublished' =
461461
[ defMessage
462-
& factStatementId .~ fsInfo ^. fsId
463-
& factStatementUtxo .~ fsOref'
462+
& fsId .~ fsInfo ^. fsId
463+
& fsUtxo .~ fsOref'
464464
| fsInfo <- req ^. factStatements
465465
, fsOref <- maybe [] return $ Map.lookup (LedgerBytes . toBuiltin $ fsInfo ^. fsId) published
466466
, fsOref' <- fromCardano fsOref
@@ -470,24 +470,26 @@ mkPublishingSpec coopDeployment authenticators req = do
470470
| fsInfo <- req ^. factStatements
471471
, not $ Map.member (LedgerBytes . toBuiltin $ fsInfo ^. fsId) published
472472
]
473+
fsIdsToPublish = (^. fsId) <$> fsInfosToPublish
474+
info :: MintFsInfo =
475+
defMessage
476+
& alreadyPublished .~ alreadyPublished'
477+
& publishedFsIds .~ fsIdsToPublish
473478

474479
if null fsInfosToPublish
475480
then
476481
( do
477482
logInfo @String "Nothing new to publish"
478-
return (Nothing, alreadyPublished')
483+
return (Nothing, info)
479484
)
480485
else do
481486
let stillValidAuthPairs' = stillValidAuthPairs authBatches
482487
authInfos = take (length fsInfosToPublish) stillValidAuthPairs'
488+
howManyValidAuthOuts = length stillValidAuthPairs'
483489

484-
when (length fsInfosToPublish > length stillValidAuthPairs') $ throwError "Must have enough valid authentication pairs (cert+auth) to authenticate Fact Statements"
485-
486-
txValidUntil <-
487-
maybe
488-
(throwError "Must have sufficient $AUTH inputs")
489-
return
490-
(maximumMay $ (\(_, _, _, UpperBound ext _) -> ext) <$> authInfos)
490+
when (length fsInfosToPublish > howManyValidAuthOuts) $
491+
throwError $
492+
"Must have enough valid Authenticator UTxOs holding valid $AUTH tokens, wanted" <> (Text.pack . show . length $ fsInfosToPublish) <> " , got " <> (Text.pack . show $ howManyValidAuthOuts)
491493

492494
fsDatums <-
493495
for
@@ -507,9 +509,7 @@ mkPublishingSpec coopDeployment authenticators req = do
507509
)
508510
authInfos
509511
fsDatums
510-
if length fsInfosWithAuth /= length fsInfosToPublish
511-
then throwError "Must have enough $AUTH outputs to authenticate each Fact Statement"
512-
else return (Just $ PublishingSpec submitterPPkh fsInfosWithAuth txValidUntil, alreadyPublished')
512+
return (Just $ PublishingSpec submitterPPkh fsInfosWithAuth, info)
513513
where
514514
stillValidAuthPairs :: Map LedgerBytes CoopAuthBatch -> [(CoopAuthBatch, (TxOutRef, ChainIndexTxOut), PaymentPubKeyHash, UpperBound POSIXTime)]
515515
stillValidAuthPairs authBatches = do
@@ -519,11 +519,11 @@ mkPublishingSpec coopDeployment authenticators req = do
519519
authOut <- Map.toList authOuts
520520
return (batch, authOut, authWallet, ivTo . cert'validity . auth'certDatum $ batch)
521521

522-
runMintFsTx :: CoopDeployment -> [PaymentPubKeyHash] -> (Value, PaymentPubKeyHash) -> (Bool, Integer) -> CreateMintFsTxReq -> Contract w s Text (Maybe TxId, [MintFsSuccess'FsIdAndTxOutRef])
522+
runMintFsTx :: CoopDeployment -> [PaymentPubKeyHash] -> (Value, PaymentPubKeyHash) -> (Bool, Integer) -> CreateMintFsTxReq -> Contract w s Text (Maybe TxId, MintFsInfo)
523523
runMintFsTx coopDeployment authenticators feeSpec (submit, minutes) req = do
524-
(mayPublishingSpec, alreadyPublished') <- mkPublishingSpec coopDeployment authenticators req
524+
(mayPublishingSpec, info) <- mkPublishingSpec coopDeployment authenticators req
525525
maybe
526-
(return (Nothing, alreadyPublished'))
526+
(return (Nothing, info))
527527
( \publishingSpec -> do
528528
(now, _) <- currentNodeClientTimeRange
529529
let mintFsTrx =
@@ -537,14 +537,13 @@ runMintFsTx coopDeployment authenticators feeSpec (submit, minutes) req = do
537537
if submit
538538
then submitTrx @Void mintFsTrx
539539
else submitTrx' @Void mintFsTrx
540-
return (Just txId, alreadyPublished')
540+
return (Just txId, info)
541541
)
542542
mayPublishingSpec
543543

544544
data PublishingSpec = PublishingSpec
545545
{ ps'submitter :: PaymentPubKeyHash
546546
, ps'factStatementSpecs :: [FactStatementSpec]
547-
, _ps'validUntil :: Extended POSIXTime
548547
}
549548

550549
data FactStatementSpec = FactStatementSpec

coop-proto/tx-builder-service.proto

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,18 @@ message CreateMintFsTxResp {
3232
message MintFsSuccess {
3333
// Fact Statement Minting transaction (abbr. mint-fact-statement-tx) signed by Authenticators
3434
plutus.Transaction mint_fs_tx = 1;
35-
// A list of already published Fact Statements (Map FactStatementId TxOutRef)
36-
message FsIdAndTxOutRef {
37-
bytes fact_statement_id = 1;
38-
plutus.TxOutRef fact_statement_utxo = 2;
39-
}
40-
repeated FsIdAndTxOutRef already_published = 2;
4135
}
4236

4337
// Information message associated with CreateMintFsTxResp
44-
message MintFsSuccess {
45-
// A list of already published Fact Statements (Map FactStatementId TxOutRef)
38+
message MintFsInfo {
4639
message FsIdAndTxOutRef {
47-
bytes fact_statement_id = 1;
48-
plutus.TxOutRef fact_statement_utxo = 2;
40+
bytes fs_id = 1;
41+
plutus.TxOutRef fs_utxo = 2;
4942
}
43+
// A list of already published Fact Statements (Map FactStatementId TxOutRef)
5044
repeated FsIdAndTxOutRef already_published = 1;
45+
// Fact Statements ID that will be published with the mint-fact-statement-tx
46+
repeated bytes published_fs_ids = 2;
5147
}
5248

5349
message FactStatementInfo {

0 commit comments

Comments
 (0)