Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ChainIndexError and ChainIndexLog
- Loading branch information
1 parent
386acbb
commit a2a9540
Showing
10 changed files
with
143 additions
and
113 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
55 changes: 55 additions & 0 deletions
55
plutus-chain-index/src/Plutus/ChainIndex/ChainIndexError.hs
This file contains 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,55 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
module Plutus.ChainIndex.ChainIndexError (ChainIndexError(..), InsertUtxoFailed(..), RollbackFailed(..)) where | ||
|
||
import Data.Aeson (FromJSON, ToJSON) | ||
import GHC.Generics (Generic) | ||
import Plutus.ChainIndex.Types (Point (..), Tip (..)) | ||
import Prettyprinter (Pretty (..), colon, (<+>)) | ||
|
||
data ChainIndexError = | ||
InsertionFailed InsertUtxoFailed | ||
| RollbackFailed RollbackFailed | ||
| QueryFailedNoTip -- ^ Query failed because the chain index does not have a tip (not synchronised with node) | ||
deriving stock (Eq, Show, Generic) | ||
deriving anyclass (FromJSON, ToJSON) | ||
|
||
instance Pretty ChainIndexError where | ||
pretty = \case | ||
InsertionFailed err -> "Insertion failed" <> colon <+> pretty err | ||
RollbackFailed err -> "Rollback failed" <> colon <+> pretty err | ||
QueryFailedNoTip -> "Query failed" <> colon <+> "No tip." | ||
|
||
-- | UTXO state could not be inserted into the chain index | ||
data InsertUtxoFailed = | ||
DuplicateBlock Tip -- ^ Insertion failed as there was already a block with the given number | ||
| InsertUtxoNoTip -- ^ The '_usTip' field of the argument was 'Last Nothing' | ||
deriving stock (Eq, Ord, Show, Generic) | ||
deriving anyclass (FromJSON, ToJSON) | ||
|
||
instance Pretty InsertUtxoFailed where | ||
pretty = \case | ||
DuplicateBlock _ -> "UTxO insertion failed - already a block with the given number" | ||
InsertUtxoNoTip -> "UTxO insertion failed - no tip" | ||
|
||
-- | Reason why the 'rollback' operation failed | ||
data RollbackFailed = | ||
RollbackNoTip -- ^ Rollback failed because the utxo index had no tip (not synchronised) | ||
| TipMismatch { foundTip :: Tip, targetPoint :: Point } -- ^ Unable to roll back to 'expectedTip' because the tip at that position was different | ||
| OldPointNotFound Point -- ^ Unable to find the old tip | ||
deriving stock (Eq, Ord, Show, Generic) | ||
deriving anyclass (FromJSON, ToJSON) | ||
|
||
instance Pretty RollbackFailed where | ||
pretty = \case | ||
RollbackNoTip -> "UTxO index had no tip (not synchronised)" | ||
TipMismatch{..} -> | ||
"Unable to rollback to" | ||
<+> pretty targetPoint | ||
<+> "because the tip at that position" | ||
<+> pretty foundTip | ||
<+> "was different" | ||
OldPointNotFound t -> "Unable to find the old tip" <+> pretty t |
This file contains 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,55 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Plutus.ChainIndex.ChainIndexLog (ChainIndexLog(..), InsertUtxoPosition(..)) where | ||
|
||
import Cardano.BM.Data.Tracer (ToObject (..)) | ||
import Data.Aeson (FromJSON, ToJSON) | ||
import GHC.Generics (Generic) | ||
import Ledger (TxId, TxOut, TxOutRef) | ||
import Plutus.ChainIndex.ChainIndexError (ChainIndexError) | ||
import Plutus.ChainIndex.Types (Tip (..)) | ||
import Plutus.Contract.CardanoAPI (FromCardanoError (..)) | ||
import Prettyprinter (Pretty (..), colon, (<+>)) | ||
|
||
data ChainIndexLog = | ||
InsertionSuccess Tip InsertUtxoPosition | ||
| ConversionFailed FromCardanoError | ||
| RollbackSuccess Tip | ||
| Err ChainIndexError | ||
| TxNotFound TxId | ||
| TxOutNotFound TxOutRef | ||
| TipIsGenesis | ||
| NoDatumScriptAddr TxOut | ||
deriving stock (Eq, Show, Generic) | ||
deriving anyclass (FromJSON, ToJSON, ToObject) | ||
|
||
instance Pretty ChainIndexLog where | ||
pretty = \case | ||
InsertionSuccess t p -> | ||
"InsertionSuccess" | ||
<> colon | ||
<+> "New tip is" | ||
<+> pretty t | ||
<> "." | ||
<+> pretty p | ||
RollbackSuccess t -> "RollbackSuccess: New tip is" <+> pretty t | ||
ConversionFailed cvError -> "Conversion failed: " <+> pretty cvError | ||
Err ciError -> "ChainIndexError:" <+> pretty ciError | ||
TxNotFound txid -> "TxNotFound:" <+> pretty txid | ||
TxOutNotFound ref -> "TxOut not found with:" <+> pretty ref | ||
TipIsGenesis -> "TipIsGenesis" | ||
NoDatumScriptAddr txout -> "The following transaction output from a script adress does not have a datum:" <+> pretty txout | ||
|
||
-- | Outcome of inserting a 'UtxoState' into the utxo index | ||
data InsertUtxoPosition = | ||
InsertAtEnd -- ^ The utxo state was added to the end. Returns the new index | ||
| InsertBeforeEnd -- ^ The utxo state was added somewhere before the end. Returns the new index and the tip | ||
deriving stock (Eq, Ord, Show, Generic) | ||
deriving anyclass (FromJSON, ToJSON) | ||
|
||
instance Pretty InsertUtxoPosition where | ||
pretty = \case | ||
InsertAtEnd -> "UTxO state was added to the end." | ||
InsertBeforeEnd -> "UTxO state was added somewhere before the end." |
This file contains 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 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 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 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