Skip to content

Commit c55b22d

Browse files
committed
Introduce HasAccumulatorElement class
Fix
1 parent ec23009 commit c55b22d

File tree

28 files changed

+193
-157
lines changed

28 files changed

+193
-157
lines changed

hydra-cluster/test/Test/BlockfrostChainSpec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Hydra.Options (
4444
ChainConfig (..),
4545
defaultBlockfrostOptions,
4646
)
47+
import Hydra.Tx.Accumulator (getAccumulatorHash, makeHeadAccumulator)
4748
import Hydra.Tx.BlueprintTx (CommitBlueprintTx (..))
4849
import Hydra.Tx.Crypto (aggregate, sign)
4950
import Hydra.Tx.HeadParameters (HeadParameters (..))
@@ -108,6 +109,7 @@ spec = around (onlyWithBlockfrostProjectFile . showLogsOnFailure "BlockfrostChai
108109
{ headId
109110
, number = 1
110111
, utxo = someUTxO
112+
, utxoHash = getAccumulatorHash (makeHeadAccumulator someUTxO)
111113
, confirmed = []
112114
, utxoToCommit = Nothing
113115
, utxoToDecommit = Nothing

hydra-cluster/test/Test/DirectChainSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, readConfigFile
6464
import Hydra.Ledger.Cardano (Tx)
6565
import Hydra.Logging (Tracer, nullTracer, showLogsOnFailure)
6666
import Hydra.Options (CardanoChainConfig (..), ChainBackendOptions (..), ChainConfig (..), DirectOptions (..), toArgNetworkId)
67+
import Hydra.Tx.Accumulator (getAccumulatorHash, makeHeadAccumulator)
6768
import Hydra.Tx.BlueprintTx (CommitBlueprintTx (..))
6869
import Hydra.Tx.Crypto (aggregate, sign)
6970
import Hydra.Tx.HeadId (HeadId, HeadSeed (..))
@@ -317,6 +318,7 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
317318
{ headId
318319
, number = 1
319320
, utxo = someUTxO
321+
, utxoHash = getAccumulatorHash (makeHeadAccumulator someUTxO)
320322
, confirmed = []
321323
, utxoToCommit = Just someUTxOToCommit
322324
, utxoToDecommit = Nothing
@@ -464,6 +466,7 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
464466
{ headId
465467
, number = 1
466468
, utxo = inHead
469+
, utxoHash = getAccumulatorHash (makeHeadAccumulator inHead)
467470
, confirmed = []
468471
, utxoToCommit = Nothing
469472
, utxoToDecommit = Just toDecommit
@@ -488,6 +491,7 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
488491
{ headId
489492
, number = 2
490493
, utxo = inHead
494+
, utxoHash = getAccumulatorHash (makeHeadAccumulator inHead)
491495
, confirmed = []
492496
, utxoToCommit = Nothing
493497
, utxoToDecommit = Just toDecommit

hydra-node/src/Hydra/API/ClientInput.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Hydra.API.ClientInput where
55
import Hydra.Prelude
66

77
import Hydra.Tx (ConfirmedSnapshot, IsTx (..), TxIdType)
8+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
89
import Test.QuickCheck.Arbitrary.ADT (ToADTArbitrary)
910

1011
data ClientInput tx
@@ -24,7 +25,7 @@ deriving stock instance IsTx tx => Show (ClientInput tx)
2425
deriving anyclass instance IsTx tx => ToJSON (ClientInput tx)
2526
deriving anyclass instance IsTx tx => FromJSON (ClientInput tx)
2627

27-
instance (Arbitrary tx, Arbitrary (TxIdType tx), Arbitrary (UTxOType tx), IsTx tx) => Arbitrary (ClientInput tx) where
28+
instance (Arbitrary tx, Arbitrary (TxIdType tx), Arbitrary (UTxOType tx), HasAccumulatorElement tx) => Arbitrary (ClientInput tx) where
2829
arbitrary = genericArbitrary
2930

3031
-- NOTE: Somehow, can't use 'genericShrink' here as GHC is complaining about
@@ -41,4 +42,4 @@ instance (Arbitrary tx, Arbitrary (TxIdType tx), Arbitrary (UTxOType tx), IsTx t
4142
Fanout -> []
4243
SideLoadSnapshot sn -> SideLoadSnapshot <$> shrink sn
4344

44-
instance (Arbitrary tx, Arbitrary (TxIdType tx), Arbitrary (UTxOType tx), IsTx tx) => ToADTArbitrary (ClientInput tx)
45+
instance (Arbitrary tx, Arbitrary (TxIdType tx), Arbitrary (UTxOType tx), HasAccumulatorElement tx) => ToADTArbitrary (ClientInput tx)

hydra-node/src/Hydra/API/HTTPServer.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import Hydra.Node.DepositPeriod (toNominalDiffTime)
4040
import Hydra.Node.Environment (Environment (..))
4141
import Hydra.Node.State (NodeState (..))
4242
import Hydra.Tx (CommitBlueprintTx (..), ConfirmedSnapshot, IsTx (..), Snapshot (..), UTxOType)
43+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
4344
import Network.HTTP.Types (ResponseHeaders, hContentType, status200, status202, status400, status404, status500)
4445
import Network.Wai (Application, Request (pathInfo, requestMethod), Response, consumeRequestBodyStrict, rawPathInfo, responseLBS)
4546
import System.Directory (doesFileExist)
@@ -145,7 +146,7 @@ newtype SideLoadSnapshotRequest tx = SideLoadSnapshotRequest
145146
deriving newtype (Eq, Show, Generic)
146147
deriving newtype (ToJSON, FromJSON)
147148

148-
instance (Arbitrary tx, Arbitrary (UTxOType tx), IsTx tx) => Arbitrary (SideLoadSnapshotRequest tx) where
149+
instance (Arbitrary tx, Arbitrary (UTxOType tx), HasAccumulatorElement tx) => Arbitrary (SideLoadSnapshotRequest tx) where
149150
arbitrary = genericArbitrary
150151

151152
shrink = \case
@@ -207,7 +208,7 @@ jsonContent = [(hContentType, "application/json")]
207208
-- | Hydra HTTP server
208209
httpApp ::
209210
forall tx.
210-
IsChainState tx =>
211+
(IsChainState tx, HasAccumulatorElement tx) =>
211212
Tracer IO APIServerLog ->
212213
Chain tx IO ->
213214
Environment ->

hydra-node/src/Hydra/API/Server.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import Hydra.Node.ApiTransactionTimeout (ApiTransactionTimeout)
4747
import Hydra.Node.Environment (Environment)
4848
import Hydra.Node.State (Deposit (..), NodeState (..), initNodeState)
4949
import Hydra.Tx (IsTx (..), Party, txId)
50+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
5051
import Network.HTTP.Types (status500)
5152
import Network.Wai (responseLBS)
5253
import Network.Wai.Handler.Warp (
@@ -81,7 +82,7 @@ data APIServerConfig = APIServerConfig
8182

8283
withAPIServer ::
8384
forall tx.
84-
IsChainState tx =>
85+
(IsChainState tx, HasAccumulatorElement tx) =>
8586
APIServerConfig ->
8687
Environment ->
8788
FilePath ->

hydra-node/src/Hydra/API/ServerOutput.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Hydra.Node.State (NodeState)
2121
import Hydra.Prelude hiding (seq)
2222
import Hydra.Tx (HeadId, Party, Snapshot, SnapshotNumber, getSnapshot)
2323
import Hydra.Tx qualified as Tx
24+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
2425
import Hydra.Tx.ContestationPeriod (ContestationPeriod)
2526
import Hydra.Tx.Crypto (MultiSignature)
2627
import Hydra.Tx.IsTx (ArbitraryIsTx, IsTx (..))
@@ -90,7 +91,7 @@ instance IsChainState tx => FromJSON (ClientMessage tx) where
9091
{ omitNothingFields = True
9192
}
9293

93-
instance (IsChainState tx, ArbitraryIsTx tx) => Arbitrary (ClientMessage tx) where
94+
instance (IsChainState tx, ArbitraryIsTx tx, HasAccumulatorElement tx) => Arbitrary (ClientMessage tx) where
9495
arbitrary = genericArbitrary
9596

9697
-- | A friendly welcome message which tells a client something about the
@@ -222,11 +223,11 @@ deriving stock instance IsChainState tx => Show (ServerOutput tx)
222223
deriving anyclass instance IsChainState tx => FromJSON (ServerOutput tx)
223224
deriving anyclass instance IsChainState tx => ToJSON (ServerOutput tx)
224225

225-
instance (ArbitraryIsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (ServerOutput tx) where
226+
instance (ArbitraryIsTx tx, HasAccumulatorElement tx, Arbitrary (ChainStateType tx)) => Arbitrary (ServerOutput tx) where
226227
arbitrary = genericArbitrary
227228
shrink = recursivelyShrink
228229

229-
instance (ArbitraryIsTx tx, IsChainState tx) => ToADTArbitrary (ServerOutput tx)
230+
instance (ArbitraryIsTx tx, HasAccumulatorElement tx, IsChainState tx) => ToADTArbitrary (ServerOutput tx)
230231

231232
-- | Whether or not to include full UTxO in server outputs.
232233
data WithUTxO = WithUTxO | WithoutUTxO
@@ -331,7 +332,7 @@ instance Arbitrary NetworkInfo where
331332
arbitrary = genericArbitrary
332333

333334
-- | Get latest confirmed snapshot UTxO from 'HeadState'.
334-
getSnapshotUtxo :: IsTx tx => HeadState tx -> Maybe (UTxOType tx)
335+
getSnapshotUtxo :: HasAccumulatorElement tx => HeadState tx -> Maybe (UTxOType tx)
335336
getSnapshotUtxo = \case
336337
HeadState.Idle{} ->
337338
Nothing
@@ -346,7 +347,7 @@ getSnapshotUtxo = \case
346347
in Just $ Tx.utxo snapshot <> fromMaybe mempty (Tx.utxoToCommit snapshot)
347348

348349
-- | Get latest seen snapshot from 'HeadState'.
349-
getSeenSnapshot :: IsTx tx => HeadState tx -> HeadState.SeenSnapshot tx
350+
getSeenSnapshot :: HasAccumulatorElement tx => HeadState tx -> HeadState.SeenSnapshot tx
350351
getSeenSnapshot = \case
351352
HeadState.Idle{} ->
352353
NoSeenSnapshot

hydra-node/src/Hydra/API/WSServer.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import Hydra.NetworkVersions qualified as NetworkVersions
4848
import Hydra.Node.Environment (Environment (..))
4949
import Hydra.Node.State (NodeState (..))
5050
import Hydra.Tx (HeadId, Party)
51+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
5152
import Network.WebSockets (
5253
PendingConnection (pendingRequest),
5354
RequestHead (..),
@@ -61,7 +62,7 @@ import Text.URI.QQ (queryKey, queryValue)
6162

6263
wsApp ::
6364
forall tx.
64-
IsChainState tx =>
65+
(IsChainState tx, HasAccumulatorElement tx) =>
6566
Environment ->
6667
Party ->
6768
Tracer IO APIServerLog ->

hydra-node/src/Hydra/Chain.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Hydra.Tx (
3737
SnapshotVersion,
3838
UTxOType,
3939
)
40+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
4041
import Hydra.Tx.IsTx (ArbitraryIsTx)
4142
import Hydra.Tx.OnChainId (OnChainId)
4243
import Test.Cardano.Ledger.Core.Arbitrary ()
@@ -98,7 +99,7 @@ deriving stock instance IsTx tx => Show (PostChainTx tx)
9899
deriving anyclass instance IsTx tx => ToJSON (PostChainTx tx)
99100
deriving anyclass instance IsTx tx => FromJSON (PostChainTx tx)
100101

101-
instance ArbitraryIsTx tx => Arbitrary (PostChainTx tx) where
102+
instance (ArbitraryIsTx tx, HasAccumulatorElement tx) => Arbitrary (PostChainTx tx) where
102103
arbitrary = genericArbitrary
103104
shrink = \case
104105
InitTx{participants, headParameters} -> InitTx <$> shrink participants <*> shrink headParameters
@@ -219,7 +220,7 @@ deriving anyclass instance IsChainState tx => FromJSON (PostTxError tx)
219220

220221
instance IsChainState tx => Exception (PostTxError tx)
221222

222-
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (PostTxError tx) where
223+
instance (ArbitraryIsTx tx, HasAccumulatorElement tx, IsChainState tx) => Arbitrary (PostTxError tx) where
223224
arbitrary = genericArbitrary
224225

225226
-- | A non empty sequence of chain states that can be rolled back.
@@ -324,7 +325,7 @@ deriving stock instance (IsTx tx, IsChainState tx) => Show (ChainEvent tx)
324325
deriving anyclass instance (IsTx tx, IsChainState tx) => ToJSON (ChainEvent tx)
325326
deriving anyclass instance (IsTx tx, IsChainState tx) => FromJSON (ChainEvent tx)
326327

327-
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (ChainEvent tx) where
328+
instance (ArbitraryIsTx tx, HasAccumulatorElement tx, IsChainState tx) => Arbitrary (ChainEvent tx) where
328329
arbitrary = genericArbitrary
329330

330331
-- | A callback indicating a 'ChainEvent tx' happened. Most importantly the

hydra-node/src/Hydra/HeadLogic.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import Hydra.Tx (
9090
utxoFromTx,
9191
withoutUTxO,
9292
)
93+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
9394
import Hydra.Tx.Accumulator qualified as Accumulator
9495
import Hydra.Tx.Crypto (
9596
Signature,
@@ -296,7 +297,7 @@ onOpenClientNewTx tx =
296297
--
297298
-- __Transition__: 'OpenState' → 'OpenState'
298299
onOpenNetworkReqTx ::
299-
IsTx tx =>
300+
HasAccumulatorElement tx =>
300301
Environment ->
301302
Ledger tx ->
302303
ChainSlot ->
@@ -387,7 +388,7 @@ onOpenNetworkReqTx env ledger currentSlot st ttl tx =
387388
--
388389
-- __Transition__: 'OpenState' → 'OpenState'
389390
onOpenNetworkReqSn ::
390-
IsTx tx =>
391+
HasAccumulatorElement tx =>
391392
Environment ->
392393
Ledger tx ->
393394
PendingDeposits tx ->
@@ -839,7 +840,7 @@ onOpenClientDecommit headId ledger currentSlot coordinatedHeadState decommitTx =
839840
-- be taken out of a Head.
840841
-- - Check if we are the leader
841842
onOpenNetworkReqDec ::
842-
IsTx tx =>
843+
HasAccumulatorElement tx =>
843844
Environment ->
844845
Ledger tx ->
845846
TTL ->
@@ -972,7 +973,7 @@ onChainTick env pendingDeposits chainTime =
972973
--
973974
-- This is primarily used to track deposits and either drop them or request
974975
-- snapshots for inclusion.
975-
onOpenChainTick :: IsTx tx => Environment -> UTCTime -> PendingDeposits tx -> OpenState tx -> Outcome tx
976+
onOpenChainTick :: HasAccumulatorElement tx => Environment -> UTCTime -> PendingDeposits tx -> OpenState tx -> Outcome tx
976977
onOpenChainTick env chainTime pendingDeposits st =
977978
-- Determine new active and new expired
978979
let nextDeposits = determineNextDepositStatus env pendingDeposits chainTime
@@ -1119,7 +1120,7 @@ onOpenClientClose st =
11191120
--
11201121
-- __Transition__: 'OpenState' → 'ClosedState'
11211122
onOpenChainCloseTx ::
1122-
IsTx tx =>
1123+
HasAccumulatorElement tx =>
11231124
OpenState tx ->
11241125
-- | New chain state.
11251126
ChainStateType tx ->
@@ -1172,7 +1173,7 @@ onOpenChainCloseTx openState newChainState closedSnapshotNumber contestationDead
11721173
-- Besides the above, it is expected to work very much like the confirmed snapshot.
11731174
--
11741175
-- __Transition__: 'OpenState' → 'OpenState'
1175-
onOpenClientSideLoadSnapshot :: IsTx tx => OpenState tx -> ConfirmedSnapshot tx -> Outcome tx
1176+
onOpenClientSideLoadSnapshot :: HasAccumulatorElement tx => OpenState tx -> ConfirmedSnapshot tx -> Outcome tx
11761177
onOpenClientSideLoadSnapshot openState requestedConfirmedSnapshot =
11771178
case requestedConfirmedSnapshot of
11781179
InitialSnapshot{} ->
@@ -1242,7 +1243,7 @@ onOpenClientSideLoadSnapshot openState requestedConfirmedSnapshot =
12421243
--
12431244
-- __Transition__: 'ClosedState' → 'ClosedState'
12441245
onClosedChainContestTx ::
1245-
IsTx tx =>
1246+
HasAccumulatorElement tx =>
12461247
ClosedState tx ->
12471248
-- | New chain state.
12481249
ChainStateType tx ->
@@ -1287,7 +1288,7 @@ onClosedChainContestTx closedState newChainState snapshotNumber contestationDead
12871288
--
12881289
-- __Transition__: 'ClosedState' → 'ClosedState'
12891290
onClosedClientFanout ::
1290-
IsTx tx =>
1291+
HasAccumulatorElement tx =>
12911292
ClosedState tx ->
12921293
Outcome tx
12931294
onClosedClientFanout closedState =
@@ -1339,7 +1340,7 @@ onClosedChainFanoutTx closedState newChainState fanoutUTxO =
13391340
-- 'Effect's, in case it is processed successfully. Later, the Node will
13401341
-- 'aggregate' the events, resulting in a new 'HeadState'.
13411342
update ::
1342-
IsChainState tx =>
1343+
(IsChainState tx, HasAccumulatorElement tx) =>
13431344
Environment ->
13441345
Ledger tx ->
13451346
-- | Current NodeState to validate the command against.

hydra-node/src/Hydra/HeadLogic/Input.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Hydra.API.ClientInput (ClientInput)
88
import Hydra.Chain (ChainEvent)
99
import Hydra.Chain.ChainState (IsChainState)
1010
import Hydra.Network.Message (Message, NetworkEvent)
11+
import Hydra.Tx.Accumulator (HasAccumulatorElement)
1112
import Hydra.Tx.IsTx (ArbitraryIsTx)
1213

1314
type TTL = Natural
@@ -32,6 +33,6 @@ deriving stock instance IsChainState tx => Show (Input tx)
3233
deriving anyclass instance IsChainState tx => ToJSON (Input tx)
3334
deriving anyclass instance IsChainState tx => FromJSON (Input tx)
3435

35-
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (Input tx) where
36+
instance (ArbitraryIsTx tx, HasAccumulatorElement tx, IsChainState tx) => Arbitrary (Input tx) where
3637
arbitrary = genericArbitrary
3738
shrink = genericShrink

0 commit comments

Comments
 (0)