Skip to content

Commit

Permalink
Revert using SerialiseAsCBOR instead of ToCBOR/FromCBOR
Browse files Browse the repository at this point in the history
  This is too big of a change at the moment, with debatable benefits. We'll resort to the same trick as before with an orphan instance on Tx
  • Loading branch information
KtorZ committed Nov 22, 2021
1 parent 5b0fbb7 commit 8f9cbef
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
1 change: 1 addition & 0 deletions hydra-node/hydra-node.cabal
Expand Up @@ -130,6 +130,7 @@ library
, contra-tracer
, data-default
, filepath
, formatting
, gitrev
, hedgehog-quickcheck
, hydra-plutus
Expand Down
5 changes: 2 additions & 3 deletions hydra-node/src/Hydra/Ledger.hs
Expand Up @@ -5,13 +5,12 @@ module Hydra.Ledger where

import Hydra.Prelude

import Cardano.Api

class
( Eq tx
, Show tx
, Typeable tx
, SerialiseAsCBOR tx
, FromCBOR tx
, ToCBOR tx
, FromJSON tx
, ToJSON tx
, --
Expand Down
33 changes: 25 additions & 8 deletions hydra-node/src/Hydra/Ledger/Cardano.hs
Expand Up @@ -14,6 +14,7 @@ import Hydra.Prelude hiding (id)
import Cardano.Api
import Cardano.Api.Byron
import Cardano.Api.Shelley
import Cardano.Binary (decodeAnnotator, serialize')
import qualified Cardano.Crypto.DSIGN as CC
import qualified Cardano.Crypto.Hash.Class as CC
import qualified Cardano.Ledger.Address as Ledger
Expand All @@ -37,6 +38,8 @@ import qualified Cardano.Ledger.Slot as Ledger
import qualified Cardano.Ledger.TxIn as Ledger
import qualified Cardano.Slotting.EpochInfo as Slotting
import qualified Cardano.Slotting.Time as Slotting
import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Encoding as CBOR
import Control.Monad (foldM)
import qualified Control.State.Transition as Ledger
import Data.Default (Default, def)
Expand All @@ -45,7 +48,9 @@ import Data.Maybe (fromJust)
import Data.Maybe.Strict (StrictMaybe (..), maybeToStrictMaybe, strictMaybeToMaybe)
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Text.Lazy.Builder (toLazyText)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Formatting.Buildable (build)
import Hydra.Ledger (IsTx (..), Ledger (..), ValidationError (..))
import Hydra.Ledger.Cardano.Orphans ()
import Test.Cardano.Ledger.Alonzo.AlonzoEraGen ()
Expand Down Expand Up @@ -185,27 +190,39 @@ utxoFromTx (Tx body@(ShelleyTxBody _ ledgerBody _ _ _ _) _) =
-- Tx
--

instance IsTx (Tx Era) where
type TxIdType (Tx Era) = TxId
type UtxoType (Tx Era) = Utxo
type ValueType (Tx Era) = Value
instance IsTx (CardanoTx) where
type TxIdType (CardanoTx) = TxId
type UtxoType (CardanoTx) = Utxo
type ValueType (CardanoTx) = Value

txId = getTxId . getTxBody
balance (Utxo u) =
let aggregate (Ledger.Alonzo.TxOut _ value _) = (<>) (fromMaryValue value)
in Map.foldr aggregate mempty u

instance ToJSON (Tx Era) where
instance ToCBOR (CardanoTx) where
toCBOR = CBOR.encodeBytes . serialize' . toLedgerTx

instance FromCBOR (CardanoTx) where
fromCBOR = do
bs <- CBOR.decodeBytes
decodeAnnotator "CardanoTx" fromCBOR (fromStrict bs)
& either
(fail . toString . toLazyText . build)
(pure . fromLedgerTx)

instance ToJSON (CardanoTx) where
toJSON = toJSON . toLedgerTx

instance FromJSON (Tx Era) where
instance FromJSON (CardanoTx) where
parseJSON = fmap fromLedgerTx . parseJSON

instance Arbitrary (Tx Era) where
instance Arbitrary (CardanoTx) where
-- TODO: shrinker!
arbitrary = genUtxo >>= genTx

-- | Convert an existing @cardano-api@'s 'Tx' to a @cardano-ledger-specs@ 'Tx'
toLedgerTx :: Tx Era -> Ledger.Tx LedgerEra
toLedgerTx :: CardanoTx -> Ledger.Tx LedgerEra
toLedgerTx = \case
Tx (ShelleyTxBody _era body scripts scriptsData auxData validity) vkWits ->
let (datums, redeemers) =
Expand Down
29 changes: 10 additions & 19 deletions hydra-node/src/Hydra/Ledger/Simple.hs
Expand Up @@ -11,8 +11,6 @@ module Hydra.Ledger.Simple where

import Hydra.Prelude

import Cardano.Api
import Cardano.Binary (decodeFullDecoder, serializeEncoding')
import Data.Aeson (
object,
withObject,
Expand Down Expand Up @@ -66,23 +64,16 @@ instance FromJSON SimpleTx where
<*> (obj .: "inputs")
<*> (obj .: "outputs")

instance SerialiseAsCBOR SimpleTx where
serialiseToCBOR (SimpleTx txid inputs outputs) =
serializeEncoding' (toCBOR txid <> toCBOR inputs <> toCBOR outputs)
deserialiseFromCBOR _ =
decodeFullDecoder
"SimpleTx"
( SimpleTx
<$> fromCBOR
<*> fromCBOR
<*> fromCBOR
)
. fromStrict

data AsSimpleTx
instance HasTypeProxy SimpleTx where
data AsType SimpleTx = AsSimpleTx
proxyToAsType _ = AsSimpleTx
instance ToCBOR SimpleTx where
toCBOR (SimpleTx txid inputs outputs) =
toCBOR txid <> toCBOR inputs <> toCBOR outputs

instance FromCBOR SimpleTx where
fromCBOR =
SimpleTx
<$> fromCBOR
<*> fromCBOR
<*> fromCBOR

--
-- MockTxIn
Expand Down
6 changes: 3 additions & 3 deletions hydra-node/src/Hydra/Snapshot.hs
Expand Up @@ -46,16 +46,16 @@ instance IsTx tx => ToJSON (Snapshot tx) where
, "confirmedTransactions" .= confirmed s
]

instance IsTx tx => FromJSON (Snapshot tx) where
instance (IsTx tx, FromCBOR tx) => FromJSON (Snapshot tx) where
parseJSON = withObject "Snapshot" $ \obj ->
Snapshot
<$> (obj .: "snapshotNumber")
<*> (obj .: "utxo")
<*> (obj .: "confirmedTransactions")

instance IsTx tx => ToCBOR (Snapshot tx) where
instance (ToCBOR tx, ToCBOR (UtxoType tx)) => ToCBOR (Snapshot tx) where
toCBOR Snapshot{number, utxo, confirmed} =
toCBOR number <> toCBOR utxo <> toCBOR confirmed

instance IsTx tx => FromCBOR (Snapshot tx) where
instance (FromCBOR tx, FromCBOR (UtxoType tx)) => FromCBOR (Snapshot tx) where
fromCBOR = Snapshot <$> fromCBOR <*> fromCBOR <*> fromCBOR

0 comments on commit 8f9cbef

Please sign in to comment.