Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ADR 18 #579

Merged
merged 56 commits into from
Nov 2, 2022
Merged

Implement ADR 18 #579

merged 56 commits into from
Nov 2, 2022

Conversation

ch1bo
Copy link
Member

@ch1bo ch1bo commented Oct 25, 2022

🍁 Based on previous work done with @ffakenz, @v0d1ch and @pgrange

🍁 Changed interface of Hydra.Chain by adding a

  • ChainStateType tx and IsChainState to modify chainState in a headState abstractly
  • ChainCallback is taking a continuation now which will yield an event

🍁 Removed chain-local persistence and in-memory TVar.

🍁 Rollbacks are now fully handled in the HeadLogic. The rollback event will contain a rollback ChainSlot and we can get the chainStateSlot via IsChainState to unwind the HeadState tx accordingly.

🍁 Tying the knot between chain state updates & providing the callback with the latest state in the hydra-node Main module now (see TODO below)

🍁 Needed to update the simulatedChainAndNetwork quite a bit. Not fully satisfied with the result, especially as chain states are something node-specific and they are shared between all test HydraNodes in the simulation. This might be even problematic for the Model tests should they ever use chainState.

🍁 Rewritten DirectChainSpec test driver. It needs to keep & update chainState now.

🍁 Lowered scope of the HandlersSpec tests on rollbacks as they cannot cover full rollback behavior anymore. Only correct yielding of Rollback events is in scope now.

🍁 Clear chain state persistence in some E2E tests now as it would be loaded consistently with the heads state now.

🍁 Moved state & transaction generators from Hydra.Chain.Direct.Context into Hydra.Chain.Direct.State to avoid orphan instances and additional modules to break cyclic dependencies (the IsChainState is annoying).

🍁 Consolidated ReceivedTxs into RolledForward log entry.

🍁 Renamed the HydraHead handle to NodeState

TODO

  • Figure out simulatedChainAndNetwork for Model/ModelSpec
  • Update api & log schemas
  • Remove chainState from ChainEvent, ChainEffect & also not thread it through HeadLogic
    • couldn't get this to work and it would complicate BehaviorSpec even more..
  • Update ADR18 and set as accepted
  • Clean up and explain things
  • Update changelog

@ch1bo ch1bo force-pushed the ch1bo/adr-18-for-real branch 3 times, most recently from a0a2154 to 128c156 Compare October 25, 2022 11:05
deriving instance IsTx tx => Eq (PostTxError tx)
deriving instance IsTx tx => Show (PostTxError tx)
-- TODO: document
advanceSlot :: a -> a
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could/should be moved to a test-specific type class for chain states as it is only required in the simulatedChainAndNetwork of the BehaviorSpec (Alternative: make that SimpleTx specific.)

@@ -362,7 +362,7 @@ estimateScriptsCost pparams systemStart epochInfo utxo tx = do
Left translationError ->
Left $ ErrTranslationError translationError
Right units ->
Map.traverseWithKey (\ptr -> left $ ErrScriptExecutionFailed . (ptr,)) units
traverse (left ErrScriptExecutionFailed) units
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahah.. @ffakenz, I happened to remove it here as well 😅. I should re-add the redeemer pointer.

@ch1bo ch1bo force-pushed the ch1bo/adr-18-for-real branch from 0317dc2 to 1ccbe50 Compare October 25, 2022 17:41
@ch1bo ch1bo marked this pull request as ready for review October 25, 2022 17:42
@ch1bo ch1bo requested review from a user and KtorZ October 25, 2022 17:42
@github-actions
Copy link

github-actions bot commented Oct 25, 2022

Transactions Costs

Sizes and execution budgets for Hydra protocol transactions. Note that unlisted parameters are currently using arbitrary values and results are not fully deterministic and comparable to previous runs.

Metadata
Generated at 2022-11-02 09:33:42.781330531 UTC
Max. memory units 14000000
Max. CPU units 10000000000
Max. tx size (kB) 16384

Cost of Init Transaction

Parties Tx size % max Mem % max CPU Min fee ₳
1 4833 11.50 4.58 0.49
2 5036 11.67 4.59 0.50
3 5241 14.09 5.53 0.53
5 5651 19.72 7.75 0.61
10 6681 28.02 10.90 0.75
45 13853 99.91 38.67 1.85

Cost of Commit Transaction

Currently only one UTxO per commit allowed (this is about to change soon)

UTxO Tx size % max Mem % max CPU Min fee ₳
1 5771 19.92 8.05 0.62

Cost of CollectCom Transaction

Parties Tx size % max Mem % max CPU Min fee ₳
1 13057 20.60 8.25 0.95
2 13451 37.36 15.13 1.15
3 13781 55.82 22.79 1.37
4 14101 77.92 31.95 1.63

Cost of Close Transaction

Parties Tx size % max Mem % max CPU Min fee ₳
1 9327 8.19 3.36 0.65
2 9492 8.97 3.81 0.67
3 9725 10.58 4.59 0.70
5 9689 9.87 3.85 0.68
10 10879 15.99 7.73 0.81
30 14222 32.63 17.16 1.16
43 16367 42.58 22.96 1.38

Cost of Contest Transaction

Parties Tx size % max Mem % max CPU Min fee ₳
1 9362 8.56 3.49 0.66
2 9493 8.94 3.79 0.67
3 9660 9.72 4.24 0.68
5 9988 11.28 5.15 0.72
10 10838 15.57 7.56 0.81
30 14186 31.98 16.90 1.15
43 16370 42.64 22.97 1.38

Cost of Abort Transaction

Some variation because of random mixture of still initial and already committed outputs.

Parties Tx size % max Mem % max CPU Min fee ₳
1 13776 26.66 11.38 1.05
2 13891 39.42 16.83 1.20
3 13999 54.10 23.16 1.37
4 14820 94.96 44.20 1.89
5 14504 99.21 43.44 1.90

Cost of FanOut Transaction

Involves spending head output and burning head tokens. Uses ada-only UTxO for better comparability.

UTxO Tx size % max Mem % max CPU Min fee ₳
1 13485 10.26 4.46 0.86
2 13456 11.40 5.19 0.87
3 13622 13.68 6.37 0.91
5 13629 16.34 7.98 0.94
10 13744 23.57 12.23 1.03
50 15249 84.88 47.60 1.85
59 15567 98.85 55.63 2.03

@ch1bo ch1bo force-pushed the ch1bo/adr-18-for-real branch from 1ccbe50 to 1027391 Compare October 27, 2022 15:06
@ch1bo ch1bo linked an issue Oct 27, 2022 that may be closed by this pull request
2 tasks
hydra-node/src/Hydra/Chain.hs Outdated Show resolved Hide resolved
}
readTVarIO headState >>= save
mapM_ (callback . Observation) onChainTxs
forM_ receivedTxs $ \tx ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: previously these have been reversed

Just Observation{newChainState} -> atomically $ writeTVar stateVar newChainState
let handler = chainSyncHandler nullTracer callback (pure timeHandle)
-- Simulate some chain following
run $ mapM_ (onRollForward handler) blocks
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could: not test roll forward when testing the roll backward (it's not needed by the implementation)

@github-actions
Copy link

github-actions bot commented Oct 28, 2022

Test Results

267 tests  +2   261 ✔️ +2   16m 6s ⏱️ + 1m 36s
  93 suites  - 1       6 💤 ±0 
    5 files   ±0       0 ±0 

Results for commit 3613cee. ± Comparison against base commit c8f99f9.

This pull request removes 8 and adds 10 tests. Note that renamed tests count towards both.
Hydra.Behavior/rolling back ‑ resets head to just after collect-com
Hydra.Behavior/rolling back ‑ resets head to just after init
Hydra.Chain.Direct.Handlers ‑ can replay chain on (benign) rollback
Hydra.Options/Hydra Node RunOptions ‑ roundtrip options
Test.EndToEnd/End-to-end on Cardano devnet/backup restore of a hydra head ‑ start a hydra node and restart it
Test.EndToEnd/End-to-end on Cardano devnet/contestation scenarios ‑ close of an initial snapshot from restarting node is contested
Test.EndToEnd/End-to-end on Cardano devnet/hydra-node executable ‑ hydra-node logs it's command line arguments
Test.EndToEnd/End-to-end on Cardano devnet/start chain observer from the past ‑ can restart head to point in the past and replay on-chain events
Hydra.Behavior/rolling back & forward ‑ does work for rollbacks past init
Hydra.Behavior/rolling back & forward ‑ does work for rollbacks past open
Hydra.Chain.Direct.Handlers ‑ yields rollback events onRollBackward
Hydra.Options/Hydra Node RunOptions ‑ roundtrip parsing & printing
Hydra.Options/Hydra Node RunOptions/JSON encoding of RunOptions ‑ allows to encode values with aeson and read them back
Hydra.Options/Hydra Node RunOptions/JSON encoding of RunOptions ‑ produces the same JSON as is found in golden/RunOptions.json
Test.EndToEnd/End-to-end on Cardano devnet/hydra-node executable ‑ logs its command line arguments
Test.EndToEnd/End-to-end on Cardano devnet/restarting nodes ‑ can abort head after restart
Test.EndToEnd/End-to-end on Cardano devnet/restarting nodes ‑ can start chain from the past and replay on-chain events
Test.EndToEnd/End-to-end on Cardano devnet/restarting nodes ‑ close of an initial snapshot from re-initialized node is contested

♻️ This comment has been updated with latest results.

Copy link
Contributor

@ffakenz ffakenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌶️

IsChainState tx
where
-- | Types of what to keep as L1 chain state.
type ChainStateType tx = c | c -> tx
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, didn't know one can use functional dependencies here!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered it from having read something about injective type families. My first try was to use a data family but the changes were much more involved and annoying, esp. with parameterised types.

ch1bo and others added 26 commits November 2, 2022 10:14
Only this way we can avoid orphan instances and cyclic module dependencies.
This pairs a ChainState with a slot. We need to know the latter to be
able to rollback correctly.
It's sufficient that we check correct yielding of events now. The actual
rollback logic is in the HeadLogic and tested there.
This is an interesting one. Should we have unit tests for this? i.e.
asserting the chain state used on effects?
The ChainContext (in the chain state) needs to be consistent for
correctly minting and distributing tokens on InitTx.
This is necessary to have the latest chainState when observing multiple
transactions without processing events in HeadLogic, e.g. concurrent
commits.

It will also make chainState update redundant in the HeadLogic, which in
turn increases complexity of "where things are happening".
Also clean up run options logging test
This would be quite awkward to define and means that either the
simulatedChainAndNetwork is not correctly implemented to be used with
real Tx (non-SimpleTx), or that we actually want to have a different
test driver for the model based tests (we will also want to
construct & validate real transactions).
We only use it in simulatedChainAndNetwork, hence moving it there.
Only relying on this modifyHeadState in 'Main' would mean that mocks
like the simulatedChainAndNetwork would become even more complicated.
Instead of an (open) external type family I have turned it into an
injective associated type family, using the TypeFamilyDependencies
option. It seems to me this simplify the definition and the use by
tying both the transactions and the state to the tx type.
@ch1bo ch1bo force-pushed the ch1bo/adr-18-for-real branch from ee45583 to ed3bcdd Compare November 2, 2022 09:15
@ch1bo ch1bo merged commit 86e20c3 into master Nov 2, 2022
@ch1bo ch1bo deleted the ch1bo/adr-18-for-real branch November 2, 2022 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement ADR18 to have a single TVar representing the head state
4 participants