~zinigor/cardano-node/trunk

« back to all changes in this revision

Viewing changes to cardano-submit-api/src/Cardano/TxSubmit/ErrorRender.hs

  • Committer: Igor Zinovyev
  • Date: 2021-08-13 19:12:27 UTC
  • Revision ID: zinigor@gmail.com-20210813191227-stlnsj3mc5ypwn0c
Tags: upstream-1.27.0
ImportĀ upstreamĀ versionĀ 1.27.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE NamedFieldPuns #-}
 
2
{-# LANGUAGE OverloadedStrings #-}
 
3
 
 
4
module Cardano.TxSubmit.ErrorRender
 
5
  ( renderApplyMempoolPayloadErr
 
6
  , renderEraMismatch
 
7
  ) where
 
8
 
 
9
-- This file contains error renders. They should have been defined at a lower level, with the error
 
10
-- type definitions, but for some reason have not been.
 
11
-- They will be defined here for now and then moved where they are supposed to be once they
 
12
-- are working.
 
13
 
 
14
import           Cardano.Chain.Byron.API (ApplyMempoolPayloadErr (..))
 
15
import           Cardano.Chain.UTxO.UTxO (UTxOError (..))
 
16
import           Cardano.Chain.UTxO.Validation (TxValidationError (..), UTxOValidationError (..))
 
17
import           Data.Function ((.))
 
18
import           Data.Monoid (Monoid (mconcat), (<>))
 
19
import           Data.Text (Text)
 
20
import           Formatting (build, sformat, stext, (%))
 
21
import           Ouroboros.Consensus.Cardano.Block (EraMismatch (..))
 
22
import           Text.Show (Show (show))
 
23
 
 
24
import qualified Data.Text as T
 
25
 
 
26
renderApplyMempoolPayloadErr :: ApplyMempoolPayloadErr -> Text
 
27
renderApplyMempoolPayloadErr err =
 
28
    case err of
 
29
      MempoolTxErr ve -> renderValidationError ve
 
30
      MempoolDlgErr {} -> "Delegation error"
 
31
      MempoolUpdateProposalErr {} -> "Update proposal error"
 
32
      MempoolUpdateVoteErr {} -> "Update vote error"
 
33
 
 
34
renderValidationError :: UTxOValidationError -> Text
 
35
renderValidationError ve =
 
36
  case ve of
 
37
    UTxOValidationTxValidationError tve -> renderTxValidationError tve
 
38
    UTxOValidationUTxOError ue -> renderUTxOError ue
 
39
 
 
40
renderTxValidationError :: TxValidationError -> Text
 
41
renderTxValidationError tve =
 
42
  "Tx Validation: " <>
 
43
    case tve of
 
44
      TxValidationLovelaceError txt e ->
 
45
        sformat ("Lovelace error "% stext %": "% build) txt e
 
46
      TxValidationFeeTooSmall tx expected actual ->
 
47
        sformat ("Tx "% build %" fee "% build %"too low, expected "% build) tx actual expected
 
48
      TxValidationWitnessWrongSignature wit pmid sig ->
 
49
        sformat ("Bad witness "% build %" for signature "% stext %" protocol magic id "% stext) wit (textShow sig) (textShow pmid)
 
50
      TxValidationWitnessWrongKey wit addr ->
 
51
        sformat ("Bad witness "% build %" for address "% build) wit addr
 
52
      TxValidationMissingInput tx ->
 
53
        sformat ("Validation cannot find input tx "% build) tx
 
54
      -- Fields are <expected> <actual>
 
55
      TxValidationNetworkMagicMismatch expected actual ->
 
56
        mconcat [ "Bad network magic  ", textShow actual, ", expected ", textShow expected ]
 
57
      TxValidationTxTooLarge expected actual ->
 
58
        mconcat [ "Tx is ", textShow actual, " bytes, but expected < ", textShow expected, " bytes" ]
 
59
      TxValidationUnknownAddressAttributes ->
 
60
        "Unknown address attributes"
 
61
      TxValidationUnknownAttributes ->
 
62
        "Unknown attributes"
 
63
 
 
64
renderUTxOError :: UTxOError -> Text
 
65
renderUTxOError ue =
 
66
  "UTxOError: " <>
 
67
    case ue of
 
68
      UTxOMissingInput tx -> sformat ("Lookup of tx "% build %" failed") tx
 
69
      UTxOOverlappingUnion -> "Union or two overlapping UTxO sets"
 
70
 
 
71
renderEraMismatch :: EraMismatch -> Text
 
72
renderEraMismatch EraMismatch{ledgerEraName, otherEraName} =
 
73
  "The era of the node and the tx do not match. " <>
 
74
  "The node is running in the " <> ledgerEraName <>
 
75
  " era, but the transaction is for the " <> otherEraName <> " era."
 
76
 
 
77
textShow :: Show a => a -> Text
 
78
textShow = T.pack . show