-
Notifications
You must be signed in to change notification settings - Fork 25
cardano-rpc | Add UTxO RPC: submitTx method #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package utxorpc.v1alpha.submit; | ||
|
|
||
| // Represents a transaction from any supported blockchain. | ||
| message AnyChainTx { | ||
| oneof type { | ||
| bytes raw = 1; // Raw transaction data. | ||
| } | ||
| } | ||
|
|
||
| // Request to submit transactions to the blockchain. | ||
| message SubmitTxRequest { | ||
| repeated AnyChainTx tx = 1; // List of transactions to submit. | ||
| } | ||
|
|
||
| // TODO u5c: new type | ||
| message TxSubmitResult { | ||
| oneof result { | ||
| bytes ref = 1; // Transaction references. | ||
| string error_message = 2; // The error message | ||
| } | ||
| } | ||
|
|
||
| // Response containing references to the submitted transactions. | ||
| // TODO u5c: changed type | ||
| message SubmitTxResponse { | ||
| repeated TxSubmitResult results = 1; // List of either transaction references or error messages. | ||
| } | ||
|
|
||
| // Service definition for submitting transactions and checking their status. | ||
| service SubmitService { | ||
| rpc SubmitTx(SubmitTxRequest) returns (SubmitTxResponse); // Submit transactions to the blockchain. | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| {-# LANGUAGE TypeFamilies #-} | ||
| {-# OPTIONS_GHC -Wno-orphans #-} | ||
|
|
||
| module Cardano.Rpc.Proto.Api.UtxoRpc.Submit | ||
| ( module Proto.Utxorpc.V1alpha.Submit.Submit | ||
| , module Proto.Utxorpc.V1alpha.Submit.Submit_Fields | ||
| ) | ||
| where | ||
|
|
||
| import Network.GRPC.Common | ||
| import Network.GRPC.Common.Protobuf | ||
|
|
||
| import Proto.Utxorpc.V1alpha.Submit.Submit | ||
| import Proto.Utxorpc.V1alpha.Submit.Submit_Fields | ||
|
|
||
| type instance RequestMetadata (Protobuf SubmitService meth) = NoMetadata | ||
|
|
||
| type instance ResponseInitialMetadata (Protobuf SubmitService meth) = NoMetadata | ||
|
|
||
| type instance ResponseTrailingMetadata (Protobuf SubmitService meth) = NoMetadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Submit.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| {-# LANGUAGE ConstraintKinds #-} | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DerivingVia #-} | ||
| {-# LANGUAGE FlexibleContexts #-} | ||
| {-# LANGUAGE GADTs #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
| {-# LANGUAGE OverloadedLabels #-} | ||
|
||
| {-# LANGUAGE QuantifiedConstraints #-} | ||
| {-# LANGUAGE RankNTypes #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| module Cardano.Rpc.Server.Internal.UtxoRpc.Submit | ||
| ( submitTxMethod | ||
| ) | ||
| where | ||
|
|
||
| import Cardano.Api | ||
| import Cardano.Api.Network.IPC qualified as Net.Tx | ||
| import Cardano.Rpc.Proto.Api.UtxoRpc.Submit qualified as UtxoRpc | ||
| import Cardano.Rpc.Server.Internal.Error | ||
| import Cardano.Rpc.Server.Internal.Monad | ||
| import Cardano.Rpc.Server.Internal.Orphans () | ||
|
|
||
| import RIO hiding (toList) | ||
|
|
||
| import Data.Default | ||
| import Network.GRPC.Spec | ||
|
|
||
| -- | Submit a CBOR-serialised list of transactions to the node | ||
| submitTxMethod | ||
| :: MonadRpc e m | ||
| => Proto UtxoRpc.SubmitTxRequest | ||
| -> m (Proto UtxoRpc.SubmitTxResponse) | ||
| -- ^ A list of succeeded transaction ids or errors for failed ones | ||
| submitTxMethod req = do | ||
| -- index transactions in the request | ||
| let serialisedTxs = zip @Int [0 ..] $ req ^.. #tx . traverse . #raw | ||
|
|
||
| nodeConnInfo <- grab | ||
| AnyCardanoEra era <- liftIO . throwExceptT $ determineEra nodeConnInfo | ||
| eon <- forEraInEon era (error "Minimum Shelley era required") pure | ||
|
|
||
| -- try to submit each one consecutively | ||
| submitResults <- forM serialisedTxs $ \(i, txBytes) -> do | ||
| let eTx = | ||
| first (("Failed to decode transaction with index " <> show i <> ": ") <>) $ | ||
| deserialiseTx eon txBytes | ||
|
|
||
| eTxId <- | ||
| fmap join . forM eTx $ | ||
| fmap | ||
| ( first | ||
| ( ("Failed to submit transaction with index " <> show i <> ": ") | ||
| <> | ||
| ) | ||
| ) | ||
| . submitTx eon | ||
|
|
||
| pure $ case eTxId of | ||
| Left err -> def & #errorMessage .~ fromString err | ||
| Right txId' -> def & #ref .~ serialiseToRawBytes txId' | ||
|
|
||
| pure $ def & #results .~ submitResults | ||
| where | ||
| deserialiseTx :: ShelleyBasedEra era -> ByteString -> Either String (Tx era) | ||
| deserialiseTx sbe = shelleyBasedEraConstraints sbe $ first show . deserialiseFromCBOR asType | ||
|
|
||
| submitTx | ||
| :: MonadRpc e m | ||
| => ShelleyBasedEra era | ||
| -> Tx era | ||
| -> m (Either String TxId) | ||
| submitTx sbe tx = do | ||
| nodeConnInfo <- grab | ||
| eRes <- | ||
| tryAny $ | ||
| submitTxToNodeLocal nodeConnInfo (TxInMode sbe tx) >>= \case | ||
| Net.Tx.SubmitFail reason -> pure . Left $ show reason | ||
| Net.Tx.SubmitSuccess -> pure $ Right . getTxId $ getTxBody tx | ||
| case eRes of | ||
| Left err -> do | ||
| let errString = displayException err | ||
| putTrace $ "N2C connection error while trying to submit a transaction: " <> errString | ||
| pure $ Left errString | ||
| Right res -> pure res | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you implemented something that deviates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is a new "sum type" isomorphic to
Either.Original proto file had just successful txIds without errors. https://github.com/utxorpc/spec/blob/main/proto/utxorpc/v1alpha/submit/submit.proto#L38