Skip to content

Commit

Permalink
Merge pull request #1341 from input-output-hk/refactor-event
Browse files Browse the repository at this point in the history
Move PostTxError input event into chain event type
  • Loading branch information
v0d1ch authored Mar 11, 2024
2 parents 536f012 + 2346c2b commit cf5223e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 128 deletions.
56 changes: 3 additions & 53 deletions hydra-node/golden/Event SimpleTx.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,10 @@
{
"samples": [
{
"postChainTx": {
"confirmedSnapshot": {
"signatures": {
"multiSignature": [
"7acc594b607e17c7d275b512d2feab0865849f7f2842593a7398f409914a0e91372b3dd8db3ca62c45d1a468330451af82b46873ca98930ff2863faed279da0c",
"63c7f2e54353aacf0e90876be875f68def0c38bf8c3937e0c34560ba035ad9455f630f3a58574c3ab941b44c9ef031e2aeed4ba07f843e5e3d5290f2fd1f0906",
"417c10ea2b723c01e7a0f04a487d8e6484e25d376a05386cff5fa461fe154a128daff256e468dc89e69d0581fd4c5af875eb3cbe0ada2c54ef1cabcc204bf407",
"2756453295179e357d5a533bfd56cbf1bacda088715fea72d3af7566a2b4f24f0878f4d54798e7e15d3d4d94871a5d2fd64c6a0db5e2c6fce275a4401cedc606",
"062e353f2db34f825925f034859a8ee51d1add6311944fef339de85a35fb27d866f4e5aefc4e87a248e0d14f0a5714f5991b97cb86c3886fe893f7e7f91cb00e"
]
},
"snapshot": {
"confirmedTransactions": [],
"headId": "d47ef17a7ccc05bf82f0625c1d931a0e",
"snapshotNumber": 27,
"utxo": [
2,
12
]
},
"tag": "ConfirmedSnapshot"
},
"headId": "c59608c612d55a0c882abaa18a1c250e",
"headParameters": {
"contestationPeriod": 75488,
"parties": [
{
"vkey": "ba2b316673c386efb932066ceafeb187b3c055d8092ed46cc296b56588fb480b"
}
]
},
"tag": "CloseTx"
},
"postTxError": {
"knownUTxO": [
-27,
-23,
-17,
-16,
-13,
-9,
-3,
2,
5,
10,
11,
16,
17,
21,
27
],
"tag": "CannotFindOwnInitial"
"clientInput": {
"tag": "Contest"
},
"tag": "PostTxError"
"tag": "ClientEvent"
},
{
"message": {
Expand Down
53 changes: 18 additions & 35 deletions hydra-node/json-schemas/logs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1588,23 +1588,6 @@ definitions:
enum: ["OnChainEvent"]
chainEvent:
$ref: "logs.yaml#/definitions/OnChainEvent"
- title: PostTxError
type: object
additionalProperties: false
required:
- tag
- postChainTx
- postTxError
description: >-
Event emitted when posting a transaction on the main chain failed.
properties:
tag:
type: string
enum: ["PostTxError"]
postChainTx:
$ref: "api.yaml#/components/schemas/PostChainTx"
postTxError:
$ref: "api.yaml#/components/schemas/PostTxError"

Message:
description: >-
Expand Down Expand Up @@ -1686,24 +1669,6 @@ definitions:
newChainState:
$ref: "api.yaml#/components/schemas/ChainState"

- title: IgnoredInitTx
type: object
additionalProperties: false
required:
- tag
- headId
- participants
properties:
tag:
type: string
enum: ["IgnoredInitTx"]
headId:
$ref: "api.yaml#/components/schemas/HeadId"
participants:
items:
type: string
contentEncoding: base16

- title: Rollback
type: object
additionalProperties: false
Expand Down Expand Up @@ -1735,6 +1700,24 @@ definitions:
chainSlot:
$ref: "api.yaml#/components/schemas/ChainSlot"

- title: PostTxError
type: object
additionalProperties: false
required:
- tag
- postChainTx
- postTxError
description: >-
Event emitted when posting a transaction on the main chain failed.
properties:
tag:
type: string
enum: ["PostTxError"]
postChainTx:
$ref: "api.yaml#/components/schemas/PostChainTx"
postTxError:
$ref: "api.yaml#/components/schemas/PostTxError"

OnChainTx:
description: >-
On-Chain transactions for the Head protocol. These data structures
Expand Down
26 changes: 11 additions & 15 deletions hydra-node/src/Hydra/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Hydra.Cardano.Api (
)
import Hydra.ContestationPeriod (ContestationPeriod)
import Hydra.HeadId (HeadId, HeadSeed)
import Hydra.Ledger (ChainSlot, IsTx, TxIdType, UTxOType)
import Hydra.Ledger (ChainSlot, IsTx, UTxOType)
import Hydra.OnChainId (OnChainId)
import Hydra.Party (Party)
import Hydra.Snapshot (ConfirmedSnapshot, SnapshotNumber)
Expand Down Expand Up @@ -212,12 +212,13 @@ deriving anyclass instance FromJSON (ChainStateType tx) => FromJSON (ChainStateH
instance Arbitrary (ChainStateType tx) => Arbitrary (ChainStateHistory tx) where
arbitrary = genericArbitrary

-- | Interface available from a chain state. Expected to be instantiated by all
-- 'ChainStateType tx'.
-- | Types that can be used on-chain by the Hydra protocol.
-- XXX: Find a better name for this. Maybe IsChainTx or IsL1Tx?
class
( IsTx tx
, Eq (ChainStateType tx)
, Show (ChainStateType tx)
, Arbitrary (ChainStateType tx)
, FromJSON (ChainStateType tx)
, ToJSON (ChainStateType tx)
) =>
Expand Down Expand Up @@ -276,21 +277,16 @@ data ChainEvent tx
{ chainTime :: UTCTime
, chainSlot :: ChainSlot
}
| -- | Event to re-ingest errors from 'postTx' for further processing.
PostTxError {postChainTx :: PostChainTx tx, postTxError :: PostTxError tx}
deriving stock (Generic)

deriving stock instance (IsTx tx, Eq (ChainStateType tx)) => Eq (ChainEvent tx)
deriving stock instance (IsTx tx, Show (ChainStateType tx)) => Show (ChainEvent tx)
deriving anyclass instance (IsTx tx, ToJSON (ChainStateType tx)) => ToJSON (ChainEvent tx)
deriving anyclass instance (IsTx tx, FromJSON (ChainStateType tx)) => FromJSON (ChainEvent tx)
deriving stock instance (IsTx tx, IsChainState tx) => Eq (ChainEvent tx)
deriving stock instance (IsTx tx, IsChainState tx) => Show (ChainEvent tx)
deriving anyclass instance (IsTx tx, IsChainState tx) => ToJSON (ChainEvent tx)
deriving anyclass instance (IsTx tx, IsChainState tx) => FromJSON (ChainEvent tx)

instance
( Arbitrary tx
, Arbitrary (UTxOType tx)
, Arbitrary (TxIdType tx)
, Arbitrary (ChainStateType tx)
) =>
Arbitrary (ChainEvent tx)
where
instance (IsTx tx, IsChainState tx) => Arbitrary (ChainEvent tx) where
arbitrary = genericArbitrary

-- | A callback indicating a 'ChainEvent tx' happened. Most importantly the
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ update env ledger st ev = case (st, ev) of
cause (ClientEffect . ServerOutput.GetUTxOResponse headId $ getField @"utxo" $ getSnapshot confirmedSnapshot)
-- NOTE: If posting the collectCom transaction failed in the open state, then
-- another party likely opened the head before us and it's okay to ignore.
(Open{}, PostTxError{postChainTx = CollectComTx{}}) ->
(Open{}, OnChainEvent PostTxError{postChainTx = CollectComTx{}}) ->
noop
-- Closed
(Closed closedState@ClosedState{headId = ourHeadId}, OnChainEvent Observation{observedTx = OnContestTx{headId, snapshotNumber, contestationDeadline}, newChainState})
Expand All @@ -764,7 +764,7 @@ update env ledger st ev = case (st, ev) of
newState ChainRolledBack{chainState = rolledBackChainState}
(_, OnChainEvent Tick{chainSlot}) ->
newState TickObserved{chainSlot}
(_, PostTxError{postChainTx, postTxError}) ->
(_, OnChainEvent PostTxError{postChainTx, postTxError}) ->
cause . ClientEffect $ ServerOutput.PostTxOnChainFailed{postChainTx, postTxError}
(_, ClientEvent{clientInput}) ->
cause . ClientEffect $ ServerOutput.CommandFailed clientInput st
Expand Down
12 changes: 2 additions & 10 deletions hydra-node/src/Hydra/HeadLogic/Event.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ module Hydra.HeadLogic.Event where
import Hydra.Prelude

import Hydra.API.ClientInput (ClientInput)
import Hydra.Chain (
ChainEvent,
ChainStateType,
IsChainState,
PostChainTx,
PostTxError,
)
import Hydra.Chain (ChainEvent, IsChainState)
import Hydra.Ledger (IsTx)
import Hydra.Network.Message (Message)
import Hydra.Party (Party)
Expand All @@ -33,15 +27,13 @@ data Event tx
NetworkEvent {ttl :: TTL, party :: Party, message :: Message tx}
| -- | Event received from the chain via a "Hydra.Chain".
OnChainEvent {chainEvent :: ChainEvent tx}
| -- | Event to re-ingest errors from 'postTx' for further processing.
PostTxError {postChainTx :: PostChainTx tx, postTxError :: PostTxError tx}
deriving stock (Generic)

deriving stock instance IsChainState tx => Eq (Event tx)
deriving stock instance IsChainState tx => Show (Event tx)
deriving anyclass instance IsChainState tx => ToJSON (Event tx)
deriving anyclass instance IsChainState tx => FromJSON (Event tx)

instance (IsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (Event tx) where
instance (IsTx tx, IsChainState tx) => Arbitrary (Event tx) where
arbitrary = genericArbitrary
shrink = genericShrink
9 changes: 2 additions & 7 deletions hydra-node/src/Hydra/HeadLogic/Outcome.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ deriving stock instance IsChainState tx => Show (Effect tx)
deriving anyclass instance IsChainState tx => ToJSON (Effect tx)
deriving anyclass instance IsChainState tx => FromJSON (Effect tx)

instance
( IsTx tx
, Arbitrary (ChainStateType tx)
) =>
Arbitrary (Effect tx)
where
instance IsChainState tx => Arbitrary (Effect tx) where
arbitrary = genericArbitrary

-- | Head state changed event. These events represent all the internal state
Expand Down Expand Up @@ -111,7 +106,7 @@ deriving stock instance IsChainState tx => Show (Outcome tx)
deriving anyclass instance IsChainState tx => ToJSON (Outcome tx)
deriving anyclass instance IsChainState tx => FromJSON (Outcome tx)

instance (IsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (Outcome tx) where
instance IsChainState tx => Arbitrary (Outcome tx) where
arbitrary = genericArbitrary
shrink = genericShrink

Expand Down
7 changes: 4 additions & 3 deletions hydra-node/src/Hydra/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Hydra.API.Server (Server, sendOutput)
import Hydra.Cardano.Api (AsType (AsPaymentKey, AsSigningKey, AsVerificationKey), getVerificationKey)
import Hydra.Chain (
Chain (..),
ChainEvent (..),
ChainStateHistory,
ChainStateType,
HeadParameters (..),
Expand All @@ -45,7 +46,7 @@ import Hydra.HeadLogic (
import Hydra.HeadLogic qualified as Logic
import Hydra.HeadLogic.Outcome (StateChanged (..))
import Hydra.HeadLogic.State (getHeadParameters)
import Hydra.Ledger (IsTx (), Ledger)
import Hydra.Ledger (Ledger)
import Hydra.Logging (Tracer, traceWith)
import Hydra.Network (Network (..))
import Hydra.Network.Message (Message)
Expand Down Expand Up @@ -159,7 +160,7 @@ deriving stock instance IsChainState tx => Show (HydraNodeLog tx)
deriving anyclass instance IsChainState tx => ToJSON (HydraNodeLog tx)
deriving anyclass instance IsChainState tx => FromJSON (HydraNodeLog tx)

instance (IsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (HydraNodeLog tx) where
instance IsChainState tx => Arbitrary (HydraNodeLog tx) where
arbitrary = genericArbitrary
shrink = genericShrink

Expand Down Expand Up @@ -252,7 +253,7 @@ processEffects HydraNode{hn, oc = Chain{postTx}, server, eq, env = Environment{p
OnChainEffect{postChainTx} ->
postTx postChainTx
`catch` \(postTxError :: PostTxError tx) ->
putEvent eq $ PostTxError{postChainTx, postTxError}
putEvent eq . OnChainEvent $ PostTxError{postChainTx, postTxError}
traceWith tracer $ EndEffect party eventId effectId

-- ** Manage state
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Snapshot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ instance IsTx tx => FromJSON (Snapshot tx) where
<*> (obj .: "utxo")
<*> (obj .: "confirmedTransactions")

instance (Arbitrary (TxIdType tx), Arbitrary (UTxOType tx)) => Arbitrary (Snapshot tx) where
instance IsTx tx => Arbitrary (Snapshot tx) where
arbitrary = genericArbitrary

-- NOTE: See note on 'Arbitrary (ClientInput tx)'
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/Chain/Direct/HandlersSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ spec = do
run $ newLocalChainState (initHistory ChainStateAt{spendableUTxO = utxo, recordedAt = Nothing})
timeHandle <- pickBlind arbitrary
let callback = \case
Rollback{} ->
failure "rolled back but expected roll forward."
Rollback{} -> failure "rolled back but expected roll forward."
PostTxError{} -> failure "Unxpected PostTxError event"
Tick{} -> pure ()
Observation{observedTx} -> do
let observedTransition =
Expand Down

0 comments on commit cf5223e

Please sign in to comment.