Skip to content

Commit

Permalink
Delete from pending deposits on successfull recover
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Oct 4, 2024
1 parent 7f9aed0 commit 7a4756a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 26 deletions.
9 changes: 5 additions & 4 deletions docs/docs/how-to/incremental-commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ We can inspect the L1 utxo with:
```shell
cardano-cli query utxo --whole-utxo
```
and
and the state of the faucet public key

```shell
cardano-cli query utxo \
Expand Down Expand Up @@ -80,18 +80,19 @@ cardano-cli query utxo \


Inspect the pending deposits:

```
curl -X GET localhost:4001/commits
["7fa05f9d5269d95452ed86bb7a32f2485245c781b61380673be0e33fb849c919"]
```
and you should see the tx-id of the deposit transaction `["6b51f3787f5482004b258c60fe0c94775164f547d9284b6233bbb4f6f8b9dfa6"]`

To recover, we can use the `/commits` endpoint again using the transaction id of the deposit:

```shell
curl -X DELETE localhost:4001/commits/$(printf "\"7fa05f9d5269d95452ed86bb7a32f2485245c781b61380673be0e33fb849c919"\" | jq -sRr '@uri')
OK
curl -X DELETE localhost:4001/commits/$(printf "\"6b51f3787f5482004b258c60fe0c94775164f547d9284b6233bbb4f6f8b9dfa6"\" | jq -sRr '@uri')
```

If we inspect the faucet funds again we will see that the locked deposit is now recovered

```shell
Expand Down
31 changes: 23 additions & 8 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import Hydra.Cardano.Api (
PaymentKey,
Tx,
TxId,
TxIn,
UTxO,
getTxBody,
getTxId,
Expand Down Expand Up @@ -856,7 +855,7 @@ canSeePendingDeposits tracer workDir node hydraScriptsTxId =
commitUTxO2 <- seedFromFaucet node walletVk 4_000_000 (contramap FromFaucet tracer)
commitUTxO3 <- seedFromFaucet node walletVk 3_000_000 (contramap FromFaucet tracer)

flip evalStateT [] $ forM_ [commitUTxO, commitUTxO2, commitUTxO3] $ \utxo -> do
deposited <- flip execStateT [] $ forM [commitUTxO, commitUTxO2, commitUTxO3] $ \utxo -> do
resp <-
parseUrlThrow ("POST " <> hydraNodeBaseUrl n1 <> "/commit")
<&> setRequestBodyJSON utxo
Expand All @@ -868,20 +867,36 @@ canSeePendingDeposits tracer workDir node hydraScriptsTxId =

liftIO $ submitTx node tx

liftIO $ waitForAllMatch 10 [n1] $ \v -> do
liftIO $ waitForAllMatch 10 [n1] $ \v ->
guard $ v ^? key "tag" == Just "CommitRecorded"

recoverResp <-
pendingDepositReq <-
parseUrlThrow ("GET " <> hydraNodeBaseUrl n1 <> "/commits")
>>= httpJSON

-- the issue doesn't specify the format of the response so we are
-- free to use whatever is convenient for the users ([TxIn]?)
let expectedResponse = fst . List.head . UTxO.pairs $ utxoFromTx tx
let expectedResponse = getTxId (getTxBody tx)
_ <- modify (expectedResponse :)
expected <- get
let expectedResp = getResponseBody recoverResp :: [TxIn]
let expectedResp = getResponseBody pendingDepositReq :: [TxId]
liftIO $ expectedResp `shouldBe` expected

forM_ deposited $ \deposit -> do
let path = BSC.unpack $ urlEncode False $ encodeUtf8 $ T.pack $ show deposit
recoverResp <-
parseUrlThrow ("DELETE " <> hydraNodeBaseUrl n1 <> "/commits/" <> spy path)
>>= httpJSON

(getResponseBody recoverResp :: String) `shouldBe` "OK"

waitForAllMatch 10 [n1] $ \v -> do
guard $ v ^? key "tag" == Just "CommitRecovered"

pendingDepositReq <-
parseUrlThrow ("GET " <> hydraNodeBaseUrl n1 <> "/commits")
>>= httpJSON

let expectedResp = getResponseBody pendingDepositReq :: [TxId]
expectedResp `shouldBe` []
where
RunningNode{networkId, nodeSocket, blockTime} = node

Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/src/HydraNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ withHydraNode' tracer chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNo
}
)
{ std_out = maybe CreatePipe UseHandle mGivenStdOut
, std_err = Inherit
, std_err = CreatePipe
}

traceWith tracer $ HydraNodeCommandSpec $ show $ cmdspec p

withCreateProcess p $ \_stdin mCreatedStdOut mCreatedStdErr processHandle ->
case (mCreatedStdOut <|> mGivenStdOut, mCreatedStdErr) of
(Just out, Nothing) -> action out stderr processHandle
(Just out, Just err) -> action out err processHandle
(Nothing, _) -> error "Should not happen™"
(_, Nothing) -> error "Should not happen™"
where
Expand Down
1 change: 1 addition & 0 deletions hydra-node/src/Hydra/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ data OnChainTx tx
| OnRecoverTx
{ headId :: HeadId
, recoveredUTxO :: UTxOType tx
, recoveredTxId :: TxIdType tx
}
| OnIncrementTx
{ headId :: HeadId
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/Chain/Direct/Handlers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ convertObservation = \case
pure OnCollectComTx{headId}
Deposit DepositObservation{headId, deposited, depositTxId, deadline, depositScriptUTxO} ->
pure $ OnDepositTx{headId, deposited, depositTxId, deadline = posixToUTCTime deadline, depositScriptUTxO}
Recover RecoverObservation{headId, recoveredUTxO} ->
pure OnRecoverTx{headId, recoveredUTxO}
Recover RecoverObservation{headId, recoveredUTxO, recoveredTxId} ->
pure OnRecoverTx{headId, recoveredUTxO, recoveredTxId}
Increment IncrementObservation{headId, newVersion, depositTxId} ->
pure OnIncrementTx{headId, newVersion, depositTxId}
Decrement DecrementObservation{headId, newVersion, distributedOutputs} ->
Expand Down
15 changes: 9 additions & 6 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,10 @@ onOpenChainRecoverTx ::
HeadId ->
OpenState tx ->
UTxOType tx ->
TxIdType tx ->
Outcome tx
onOpenChainRecoverTx headId st recoveredUTxO =
newState CommitRecovered{recoveredUTxO, newLocalUTxO = localUTxO `withoutUTxO` recoveredUTxO}
onOpenChainRecoverTx headId st recoveredUTxO recoveredTxId =
newState CommitRecovered{recoveredUTxO, newLocalUTxO = localUTxO `withoutUTxO` recoveredUTxO, recoveredTxId}
<> cause
( ClientEffect
ServerOutput.CommitRecovered
Expand Down Expand Up @@ -1302,8 +1303,8 @@ update env ledger st ev = case (st, ev) of
| ourHeadId == headId -> onOpenChainDepositTx headId env openState deposited depositTxId deadline depositScriptUTxO
| otherwise ->
Error NotOurHead{ourHeadId, otherHeadId = headId}
(Open openState@OpenState{headId = ourHeadId}, ChainInput Observation{observedTx = OnRecoverTx{headId, recoveredUTxO}})
| ourHeadId == headId -> onOpenChainRecoverTx headId openState recoveredUTxO
(Open openState@OpenState{headId = ourHeadId}, ChainInput Observation{observedTx = OnRecoverTx{headId, recoveredUTxO, recoveredTxId}})
| ourHeadId == headId -> onOpenChainRecoverTx headId openState recoveredUTxO recoveredTxId
| otherwise ->
Error NotOurHead{ourHeadId, otherHeadId = headId}
(Open openState@OpenState{headId = ourHeadId}, ChainInput Observation{observedTx = OnIncrementTx{headId, newVersion, depositTxId}})
Expand Down Expand Up @@ -1421,17 +1422,19 @@ aggregate st = \case
where
CoordinatedHeadState{pendingDeposits = existingDeposits} = coordinatedHeadState
_otherState -> st
CommitRecovered{newLocalUTxO} -> case st of
CommitRecovered{newLocalUTxO, recoveredTxId} -> case st of
Open
os@OpenState{coordinatedHeadState} ->
Open
os
{ coordinatedHeadState =
coordinatedHeadState
{ localUTxO = newLocalUTxO
, pendingDeposits = mempty
, pendingDeposits = Map.delete (spy recoveredTxId) existingDeposits
}
}
where
CoordinatedHeadState{pendingDeposits = existingDeposits} = coordinatedHeadState
_otherState -> st
DecommitRecorded{decommitTx, newLocalUTxO} -> case st of
Open
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/HeadLogic/Outcome.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ data StateChanged tx
, newLocalUTxO :: UTxOType tx
}
| CommitRecorded {pendingDeposits :: PendingDeposits tx, newLocalUTxO :: UTxOType tx}
| CommitRecovered {recoveredUTxO :: UTxOType tx, newLocalUTxO :: UTxOType tx}
| CommitRecovered {recoveredUTxO :: UTxOType tx, newLocalUTxO :: UTxOType tx, recoveredTxId :: TxIdType tx}
| DecommitRecorded {decommitTx :: tx, newLocalUTxO :: UTxOType tx}
| SnapshotRequestDecided {snapshotNumber :: SnapshotNumber}
| -- | A snapshot was requested by some party.
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/BehaviorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@ toOnChainTx now = \case
OnAbortTx{headId = testHeadId}
CollectComTx{headId} ->
OnCollectComTx{headId}
RecoverTx{headId, utxoToDeposit} ->
OnRecoverTx{headId, recoveredUTxO = utxoToDeposit}
RecoverTx{headId, utxoToDeposit, recoverTxId} ->
OnRecoverTx{headId, recoveredUTxO = utxoToDeposit, recoveredTxId = recoverTxId}
IncrementTx{headId, incrementingSnapshot, depositTxId} ->
OnIncrementTx
{ headId
Expand Down
4 changes: 3 additions & 1 deletion hydra-tx/src/Hydra/Tx/Recover.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ recoverTx depositTxId deposited lowerBoundSlot =
data RecoverObservation = RecoverObservation
{ headId :: HeadId
, recoveredUTxO :: UTxO
, recoveredTxId :: TxId
}
deriving stock (Show, Eq, Generic)

Expand All @@ -60,7 +61,7 @@ observeRecoverTx ::
Maybe RecoverObservation
observeRecoverTx networkId utxo tx = do
let inputUTxO = resolveInputsUTxO utxo tx
(_, depositOut) <- findTxOutByScript @PlutusScriptV2 inputUTxO depositScript
(TxIn depositTxId _, depositOut) <- findTxOutByScript @PlutusScriptV2 inputUTxO depositScript
dat <- txOutScriptData $ toTxContext depositOut
Deposit.DepositDatum (headCurrencySymbol, _, onChainDeposits) <- fromScriptData dat
deposits <- do
Expand All @@ -76,6 +77,7 @@ observeRecoverTx networkId utxo tx = do
( RecoverObservation
{ headId
, recoveredUTxO = deposits
, recoveredTxId = depositTxId
}
)
else Nothing
Expand Down

0 comments on commit 7a4756a

Please sign in to comment.