Skip to content

Commit

Permalink
Revert "next-node is the new merge (ends PLT-558) (#745)" (#746)
Browse files Browse the repository at this point in the history
This reverts commit 465e6ee.
  • Loading branch information
berewt authored Oct 11, 2022
1 parent 465e6ee commit 5ab8b28
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 82 deletions.
15 changes: 9 additions & 6 deletions cardano-streaming/cardano-streaming.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ library
-- Other IOG dependencies
--------------------------
build-depends:
, cardano-api >=1.35
, cardano-api
, ouroboros-network

------------------------
Expand All @@ -62,14 +62,15 @@ executable cardano-streaming-example-1
--------------------------
-- Other IOG dependencies
--------------------------
build-depends: cardano-api >=1.35
build-depends: cardano-api

------------------------
-- Non-IOG dependencies
------------------------
build-depends:
, aeson
, base >=4.9 && <5
, base16-bytestring
, bytestring
, optparse-applicative
, streaming
Expand All @@ -88,19 +89,20 @@ executable cardano-streaming-example-2
--------------------
build-depends:
, cardano-streaming
, plutus-ledger >=1.0.0
, plutus-ledger

--------------------------
-- Other IOG dependencies
--------------------------
build-depends: cardano-api >=1.35
build-depends: cardano-api

------------------------
-- Non-IOG dependencies
------------------------
build-depends:
, aeson
, base >=4.9 && <5
, base16-bytestring
, bytestring
, optparse-applicative
, streaming
Expand All @@ -118,19 +120,20 @@ executable cardano-streaming-example-3
--------------------
build-depends:
, cardano-streaming
, plutus-chain-index-core >=1.0.0
, plutus-chain-index-core

--------------------------
-- Other IOG dependencies
--------------------------
build-depends: cardano-api >=1.35
build-depends: cardano-api

------------------------
-- Non-IOG dependencies
------------------------
build-depends:
, aeson
, base >=4.9 && <5
, base16-bytestring
, bytestring
, optparse-applicative
, streaming
1 change: 0 additions & 1 deletion cardano-streaming/examples/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,4 @@ workaround k Cardano.Api.ShelleyEraInCardanoMode = k Cardano.Api.ShelleyEraInCar
workaround k Cardano.Api.AllegraEraInCardanoMode = k Cardano.Api.AllegraEraInCardanoMode
workaround k Cardano.Api.MaryEraInCardanoMode = k Cardano.Api.MaryEraInCardanoMode
workaround k Cardano.Api.AlonzoEraInCardanoMode = k Cardano.Api.AlonzoEraInCardanoMode
workaround k Cardano.Api.BabbageEraInCardanoMode = k Cardano.Api.BabbageEraInCardanoMode

18 changes: 10 additions & 8 deletions cardano-streaming/examples/Example3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ utxoState =
S.scanned step initial projection
where
step index (RollForward block _) =
let txs = CI.fromCardanoBlock block
tip = CI.tipFromCardanoBlock block
balance = TxUtxoBalance.fromBlock tip txs
in case UtxoState.insert balance index of
Left err ->
error (show err)
Right (UtxoState.InsertUtxoSuccess newIndex _insertPosition) ->
newIndex
case CI.fromCardanoBlock block of
Left err -> error ("FromCardanoError: " <> show err)
Right txs ->
let tip = CI.tipFromCardanoBlock block
balance = TxUtxoBalance.fromBlock tip txs
in case UtxoState.insert balance index of
Left err ->
error (show err)
Right (UtxoState.InsertUtxoSuccess newIndex _insertPosition) ->
newIndex
step index (RollBackward cardanoPoint _) =
let point = CI.fromCardanoPoint cardanoPoint
in case TxUtxoBalance.rollback point index of
Expand Down
20 changes: 19 additions & 1 deletion cardano-streaming/examples/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@

module Orphans where

import Cardano.Api (BlockHeader (BlockHeader), BlockNo, ChainPoint (ChainPoint, ChainPointAtGenesis), ToJSON)
import Cardano.Api (BlockHeader (BlockHeader), BlockNo, ChainPoint (ChainPoint, ChainPointAtGenesis),
HasTypeProxy (proxyToAsType), Hash, SerialiseAsRawBytes (deserialiseFromRawBytes), ToJSON)
import Cardano.Streaming (ChainSyncEvent)
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Char8 qualified as C8
import Data.Proxy (Proxy (Proxy))
import Data.String (IsString (fromString))
import GHC.Generics (Generic)

-- https://github.com/input-output-hk/cardano-node/pull/3608
instance IsString (Hash BlockHeader) where
fromString = either error id . deserialiseFromRawBytesBase16 . C8.pack
where
deserialiseFromRawBytesBase16 str =
case Base16.decode str of
Right raw -> case deserialiseFromRawBytes ttoken raw of
Just x -> Right x
Nothing -> Left ("cannot deserialise " ++ show str)
Left msg -> Left ("invalid hex " ++ show str ++ ", " ++ msg)
where
ttoken = proxyToAsType (Proxy :: Proxy a)

deriving instance Generic ChainPoint

instance ToJSON ChainPoint
Expand Down
7 changes: 3 additions & 4 deletions cardano-streaming/src/Cardano/Streaming.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ where
import Cardano.Api (BlockInMode, CardanoMode, ChainPoint, ChainSyncClient (ChainSyncClient), ChainTip,
ConsensusModeParams (CardanoModeParams), EpochSlots (EpochSlots),
LocalChainSyncClient (LocalChainSyncClient),
LocalNodeClientProtocols (LocalNodeClientProtocols, localChainSyncClient, localStateQueryClient, localTxMonitoringClient, localTxSubmissionClient),
LocalNodeClientProtocols (LocalNodeClientProtocols, localChainSyncClient, localStateQueryClient, localTxSubmissionClient),
LocalNodeConnectInfo (LocalNodeConnectInfo, localConsensusModeParams, localNodeNetworkId, localNodeSocketPath),
NetworkId, connectToLocalNode)
import Cardano.Api.ChainSync.Client (ClientStIdle (SendMsgFindIntersect, SendMsgRequestNext),
Expand Down Expand Up @@ -69,9 +69,8 @@ withChainSyncEventStream socketPath networkId point consumer = do
localNodeClientProtocols =
LocalNodeClientProtocols
{ localChainSyncClient = LocalChainSyncClient client,
localStateQueryClient = Nothing,
localTxMonitoringClient = Nothing,
localTxSubmissionClient = Nothing
localTxSubmissionClient = Nothing,
localStateQueryClient = Nothing
}

connectInfo =
Expand Down
5 changes: 1 addition & 4 deletions marconi/src/Marconi/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ chainPointParser =
)
where
maybeParseHashBlockHeader :: String -> Maybe (C.Hash C.BlockHeader)
maybeParseHashBlockHeader =
either (const Nothing) Just
. C.deserialiseFromRawBytesHex (C.proxyToAsType Proxy)
. C8.pack
maybeParseHashBlockHeader = C.deserialiseFromRawBytesHex (C.proxyToAsType Proxy) . C8.pack
2 changes: 1 addition & 1 deletion marconi/src/Marconi/Index/Datum.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Database.SQLite.Simple.FromField (FromField (fromField), ResultError (Con
import Database.SQLite.Simple.ToField (ToField (toField))

import Cardano.Api (SlotNo (SlotNo))
import Ledger.Scripts (Datum, DatumHash)
import Plutus.V1.Ledger.Api (Datum, DatumHash)
import RewindableIndex.Index.VSqlite (SqliteIndex)
import RewindableIndex.Index.VSqlite qualified as Ix

Expand Down
16 changes: 2 additions & 14 deletions marconi/src/Marconi/Index/ScriptTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,10 @@ fromShelleyBasedScript era script =
Shelley.ScriptInEra Shelley.PlutusScriptV1InAlonzo $
Shelley.PlutusScript Shelley.PlutusScriptV1 $
Shelley.PlutusScriptSerialised s
Alonzo.PlutusScript Alonzo.PlutusV2 _ ->
error "fromShelleyBasedScript: PlutusV2 not supported in Alonzo era"
Shelley.ShelleyBasedEraBabbage ->
case script of
Alonzo.TimelockScript s ->
Shelley.ScriptInEra Shelley.SimpleScriptV2InBabbage $
Shelley.SimpleScript Shelley.SimpleScriptV2 $
fromAllegraTimelock Shelley.TimeLocksInSimpleScriptV2 s
Alonzo.PlutusScript Alonzo.PlutusV1 s ->
Shelley.ScriptInEra Shelley.PlutusScriptV1InBabbage $
Shelley.PlutusScript Shelley.PlutusScriptV1 $
Shelley.PlutusScriptSerialised s
Alonzo.PlutusScript Alonzo.PlutusV2 s ->
Shelley.ScriptInEra Shelley.PlutusScriptV2InBabbage $
Shelley.ScriptInEra Shelley.PlutusScriptV2InAlonzo $
Shelley.PlutusScript Shelley.PlutusScriptV2 $
Shelley.PlutusScriptSerialised s
Shelley.PlutusScriptSerialised s

where
fromAllegraTimelock :: Shelley.TimeLocksSupported lang
Expand Down
27 changes: 12 additions & 15 deletions marconi/src/Marconi/Indexers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import Streaming.Prelude qualified as S
import Cardano.Api (Block (Block), BlockHeader (BlockHeader), BlockInMode (BlockInMode), CardanoMode, SlotNo, Tx (Tx),
chainPointToSlotNo)
import Cardano.Api qualified as C
import Cardano.Streaming (ChainSyncEvent (RollBackward, RollForward))
-- TODO Remove the following dependencies from cardano-ledger, and
-- then also the package dependency from this package's cabal
-- file. Tracked with: https://input-output.atlassian.net/browse/PLT-777
import Ledger (TxIn (TxIn), TxOut (TxOut), TxOutRef (TxOutRef, txOutRefId, txOutRefIdx), txInRef)
import Cardano.Streaming (ChainSyncEvent (RollBackward, RollForward))
import Ledger (TxIn (TxIn), TxOut, TxOutRef (TxOutRef, txOutRefId, txOutRefIdx), txInRef)
import Ledger.Scripts (Datum, DatumHash)
import Ledger.Tx.CardanoAPI (fromCardanoTxId, fromCardanoTxIn, fromTxScriptValidity, scriptDataFromCardanoTxBody,
withIsCardanoEra)
import Ledger.Tx.CardanoAPI (fromCardanoTxId, fromCardanoTxIn, fromCardanoTxOut, fromTxScriptValidity,
scriptDataFromCardanoTxBody, withIsCardanoEra)
import Marconi.Index.Datum (DatumIndex)
import Marconi.Index.Datum qualified as Datum
import Marconi.Index.ScriptTx qualified as ScriptTx
Expand All @@ -48,25 +48,23 @@ getDatums (BlockInMode (Block (BlockHeader slotNo _ _) txs) _) = concatMap extra
in map (slotNo,) hashes

isTargetTxOut :: TargetAddresses -> C.TxOut C.CtxTx era -> Bool
isTargetTxOut targetAddresses (C.TxOut address _ _ _) = case address of
isTargetTxOut targetAddresses (C.TxOut address _ _) = case address of
(C.AddressInEra (C.ShelleyAddressInEra _) addr) -> addr `elem` targetAddresses
_ -> False

-- UtxoIndexer
type TargetAddresses = NonEmpty.NonEmpty (C.Address C.ShelleyAddr )

getOutputs
:: C.IsCardanoEra era
=> Maybe TargetAddresses
:: Maybe TargetAddresses
-> C.Tx era
-> Maybe [(TxOut, TxOutRef)]
getOutputs maybeTargetAddresses (C.Tx txBody@(C.TxBody C.TxBodyContent{C.txOuts}) _) = do
outs <-
either (const Nothing) (Just . map TxOut)
. traverse (C.eraCast C.BabbageEra)
$ case maybeTargetAddresses of
Just targetAddresses -> filter (isTargetTxOut targetAddresses) txOuts
Nothing -> txOuts
outs <- case maybeTargetAddresses of
Just targetAddresses ->
either (const Nothing) Just $ traverse fromCardanoTxOut . filter (isTargetTxOut targetAddresses) $ txOuts
Nothing ->
either (const Nothing) Just $ traverse fromCardanoTxOut txOuts
pure $ outs & zip ([0..] :: [Integer])
<&> (\(ix, out) -> (out, TxOutRef { txOutRefId = fromCardanoTxId (C.getTxId txBody)
, txOutRefIdx = ix
Expand All @@ -85,8 +83,7 @@ getInputs (C.Tx (C.TxBody C.TxBodyContent{C.txIns, C.txScriptValidity, C.txInsCo
in Set.fromList $ fmap (txInRef . (`TxIn` Nothing) . fromCardanoTxIn) inputs

getUtxoUpdate
:: C.IsCardanoEra era
=> SlotNo
:: SlotNo
-> [C.Tx era]
-> Maybe TargetAddresses
-> UtxoUpdate
Expand Down
29 changes: 11 additions & 18 deletions marconi/test/Integration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Hedgehog.Extras.Stock.IO.Network.Sprocket qualified as IO
import Hedgehog.Extras.Test qualified as HE
import Hedgehog.Extras.Test.Base qualified as H
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Hedgehog (testPropertyNamed)
import Test.Tasty.Hedgehog (testProperty)

import Cardano.Api qualified as C
import Cardano.Api.Shelley qualified as C
Expand All @@ -59,8 +59,7 @@ import Marconi.Logging ()

tests :: TestTree
tests = testGroup "Integration"
[ testPropertyNamed "prop_script_hash_in_local_testnet_tx_match" "testIndex" testIndex
]
[ testProperty "prop_script_hash_in_local_testnet_tx_match" testIndex ]

{- | We test the script transaction indexer by setting up a testnet,
adding a script to it and then spending it, and then see if the
Expand Down Expand Up @@ -158,7 +157,7 @@ testIndex = H.integration . HE.runFinallies . workspace "chairman" $ \tempAbsBas
, C.localNodeSocketPath = socketPathAbs
}

(tx1in, C.TxOut _ v _ _) <- do
(tx1in, C.TxOut _ v _) <- do
utxo <- findUTxOByAddress localNodeConnectInfo (C.toAddressAny address)
headM $ Map.toList $ C.unUTxO utxo
let totalLovelace = C.txOutValueToLovelace v
Expand All @@ -182,23 +181,18 @@ testIndex = H.integration . HE.runFinallies . workspace "chairman" $ \tempAbsBas
(C.AddressInEra (C.ShelleyAddressInEra C.ShelleyBasedEraAlonzo) plutusScriptAddr)
(C.TxOutValue C.MultiAssetInAlonzoEra $ C.lovelaceToValue amountPaid)
(C.TxOutDatumHash C.ScriptDataInAlonzoEra scriptDatumHash)
C.ReferenceScriptNone
txOut2 :: C.TxOut ctx C.AlonzoEra
txOut2 =
C.TxOut
(C.AddressInEra (C.ShelleyAddressInEra C.ShelleyBasedEraAlonzo) address)
(C.TxOutValue C.MultiAssetInAlonzoEra $ C.lovelaceToValue amountReturned)
C.TxOutDatumNone
C.ReferenceScriptNone
txBodyContent :: C.TxBodyContent C.BuildTx C.AlonzoEra
txBodyContent =
C.TxBodyContent {
C.txIns = [(tx1in, C.BuildTxWith $ C.KeyWitness C.KeyWitnessForSpending)],
C.txInsCollateral = C.TxInsCollateralNone,
C.txInsReference = C.TxInsReferenceNone,
C.txOuts = [txOut1, txOut2],
C.txTotalCollateral = C.TxTotalCollateralNone,
C.txReturnCollateral = C.TxReturnCollateralNone,
C.txFee = C.TxFeeExplicit C.TxFeesExplicitInAlonzoEra tx1fee,
C.txValidityRange = (C.TxValidityNoLowerBound, C.TxValidityNoUpperBound C.ValidityNoUpperBoundInAlonzoEra),
C.txMetadata = C.TxMetadataNone,
Expand All @@ -225,7 +219,7 @@ testIndex = H.integration . HE.runFinallies . workspace "chairman" $ \tempAbsBas

tx2collateralTxIn <- headM . Map.keys . C.unUTxO =<< findUTxOByAddress localNodeConnectInfo (C.toAddressAny address)

(scriptTxIn, C.TxOut _ valueAtScript _ _) <- do
(scriptTxIn, C.TxOut _ valueAtScript _) <- do
scriptUtxo <- findUTxOByAddress localNodeConnectInfo $ C.toAddressAny plutusScriptAddr
headM $ Map.toList $ C.unUTxO scriptUtxo

Expand All @@ -244,7 +238,7 @@ testIndex = H.integration . HE.runFinallies . workspace "chairman" $ \tempAbsBas

scriptWitness :: C.Witness C.WitCtxTxIn C.AlonzoEra
scriptWitness = C.ScriptWitness C.ScriptWitnessForSpending $
C.PlutusScriptWitness C.PlutusScriptV1InAlonzo C.PlutusScriptV1 (C.PScript plutusScript)
C.PlutusScriptWitness C.PlutusScriptV1InAlonzo C.PlutusScriptV1 plutusScript
(C.ScriptDatumForTxIn scriptDatum) redeemer executionUnits

collateral = C.TxInsCollateral C.CollateralInAlonzoEra [tx2collateralTxIn]
Expand All @@ -256,17 +250,13 @@ testIndex = H.integration . HE.runFinallies . workspace "chairman" $ \tempAbsBas
-- send ADA back to the original genesis address ^
(C.TxOutValue C.MultiAssetInAlonzoEra $ C.lovelaceToValue $ lovelaceAtScript - tx2fee)
C.TxOutDatumNone
C.ReferenceScriptNone

tx2bodyContent :: C.TxBodyContent C.BuildTx C.AlonzoEra
tx2bodyContent =
C.TxBodyContent {
C.txIns = [(scriptTxIn, C.BuildTxWith scriptWitness)],
C.txInsCollateral = collateral,
C.txInsReference = C.TxInsReferenceNone,
C.txOuts = [tx2out],
C.txTotalCollateral = C.TxTotalCollateralNone,
C.txReturnCollateral = C.TxReturnCollateralNone,
C.txFee = C.TxFeeExplicit C.TxFeesExplicitInAlonzoEra tx2fee,
C.txValidityRange = (C.TxValidityNoLowerBound, C.TxValidityNoUpperBound C.ValidityNoUpperBoundInAlonzoEra),
C.txMetadata = C.TxMetadataNone,
Expand Down Expand Up @@ -328,12 +318,15 @@ startTestnet base tempAbsBasePath' = do
Nothing
assert $ tempAbsPath == (tempAbsBasePath' <> "/")
&& tempAbsPath == (tempBaseAbsPath <> "/")
tn <- TN.testnet TN.defaultTestnetOptions conf
let networkId = C.Testnet $ C.NetworkMagic $ fromIntegral (TN.testnetMagic tn)
socketPath <- IO.sprocketArgumentName <$> headM (TN.nodeSprocket <$> TN.bftNodes tn)
TN.TestnetRuntime { TN.bftSprockets, TN.testnetMagic } <- TN.testnet TN.defaultTestnetOptions conf
let networkId = C.Testnet $ C.NetworkMagic $ fromIntegral testnetMagic
socketPath <- IO.sprocketArgumentName <$> headM bftSprockets
socketPathAbs <- H.note =<< (liftIO $ IO.canonicalizePath $ tempAbsPath </> socketPath)
pure (socketPathAbs, networkId, tempAbsPath)

deriving instance Real C.Lovelace
deriving instance Integral C.Lovelace

readAs :: (C.HasTextEnvelope a, MonadIO m, MonadTest m) => C.AsType a -> FilePath -> m a
readAs as path = H.leftFailM . liftIO $ C.readFileTextEnvelope as path

Expand Down
Loading

0 comments on commit 5ab8b28

Please sign in to comment.