Skip to content

Commit

Permalink
Don't pass a chain point to internal wallet anymore
Browse files Browse the repository at this point in the history
It will always query from the tip now.
  • Loading branch information
ch1bo committed Nov 29, 2022
1 parent d87dc39 commit 54bb8f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
5 changes: 2 additions & 3 deletions hydra-node/src/Hydra/Chain/Direct.hs
Expand Up @@ -44,7 +44,6 @@ import Hydra.Cardano.Api (
NetworkId,
Tx,
TxId,
fromConsensusPointHF,
shelleyBasedEra,
toConsensusPointHF,
toLedgerPParams,
Expand Down Expand Up @@ -189,7 +188,7 @@ withDirectChain tracer config ctx persistedPoint callback action = do
(min <$> startChainFrom <*> persistedPoint)
<|> persistedPoint
<|> startChainFrom
wallet <- newTinyWallet (contramap Wallet tracer) networkId keyPair chainPoint queryWalletInfo
wallet <- newTinyWallet (contramap Wallet tracer) networkId keyPair queryWalletInfo
let chainHandle =
mkChain
tracer
Expand Down Expand Up @@ -353,7 +352,7 @@ chainSyncClient handler wallet startingPoint =
pure clientStIdle
, recvMsgRollBackward = \point _tip -> ChainSyncClient $ do
-- Re-initialize the tiny wallet
reset wallet $ fromConsensusPointHF point
reset wallet
-- Rollback main chain sync handler
onRollBackward handler point
pure clientStIdle
Expand Down
14 changes: 5 additions & 9 deletions hydra-node/src/Hydra/Chain/Direct/Wallet.hs
Expand Up @@ -87,10 +87,9 @@ data TinyWallet m = TinyWallet
getUTxO :: STM m (Map TxIn TxOut)
, sign :: ValidatedTx LedgerEra -> ValidatedTx LedgerEra
, coverFee :: Map TxIn TxOut -> ValidatedTx LedgerEra -> STM m (Either ErrCoverFee (ValidatedTx LedgerEra))
, -- | Reset the wallet state to some point. This will start re-initializing
-- against the latest tip of the node and start to ignore 'update' calls
-- until reaching that tip.
reset :: ChainPoint -> m ()
, -- | Re-initializ wallet against the latest tip of the node and start to
-- ignore 'update' calls until reaching that tip.
reset :: m ()
, -- | Update the wallet state given some 'Block'. May be ignored if wallet is
-- still initializing.
update :: Block -> m ()
Expand Down Expand Up @@ -125,13 +124,11 @@ newTinyWallet ::
NetworkId ->
-- | Credentials of the wallet.
(VerificationKey PaymentKey, SigningKey PaymentKey) ->
-- Starting point on the chain. From this onward we will receive blocks on 'update'.
ChainPoint ->
-- | A function to query UTxO, pparams, system start and epoch info from the
-- node. Initially and on demand later.
ChainQuery IO ->
IO (TinyWallet IO)
newTinyWallet tracer networkId (vk, sk) startPoint queryWalletInfo = do
newTinyWallet tracer networkId (vk, sk) queryWalletInfo = do
walletInfoVar <- newTVarIO =<< initialize
pure
TinyWallet
Expand All @@ -141,8 +138,7 @@ newTinyWallet tracer networkId (vk, sk) startPoint queryWalletInfo = do
-- TODO: We should query pparams and epochInfo here
WalletInfoOnChain{walletUTxO, pparams, systemStart, epochInfo} <- readTVar walletInfoVar
pure $ coverFee_ pparams systemStart epochInfo lookupUTxO walletUTxO partialTx
, reset = \point -> do
initialize >>= atomically . writeTVar walletInfoVar
, reset = initialize >>= atomically . writeTVar walletInfoVar
, update = \block -> do
let point = fromConsensusPointHF $ blockPoint block
walletTip <- atomically $ readTVar walletInfoVar <&> tip
Expand Down
26 changes: 10 additions & 16 deletions hydra-node/test/Hydra/Chain/Direct/WalletSpec.hs
Expand Up @@ -79,25 +79,19 @@ spec = parallel $ do

describe "newTinyWallet" $ do
prop "initialises wallet by querying UTxO" $
forAll genKeyPair $ \(vk, sk) ->
forAll genChainPoint $ \cp -> do
wallet <- newTinyWallet nullTracer Fixture.testNetworkId (vk, sk) cp (mockChainQuery vk)
utxo <- atomically (getUTxO wallet)
utxo `shouldSatisfy` \m -> Map.size m > 0
forAll genKeyPair $ \(vk, sk) -> do
wallet <- newTinyWallet nullTracer Fixture.testNetworkId (vk, sk) (mockChainQuery vk)
utxo <- atomically (getUTxO wallet)
utxo `shouldSatisfy` \m -> Map.size m > 0

-- TODO: This test has become a bit pointless
prop "re-queries UTxO from the tip after reset" $
forAll genKeyPair $ \(vk, sk) ->
let twoDistinctChainPoints = do
cp1 <- genChainPoint
cp2 <- genChainPoint `suchThat` (cp1 /=)
pure (cp1, cp2)
in forAll twoDistinctChainPoints $ \(cp1, cp2) -> do
(queryFn, assertQueryPoint) <- setupQuery vk
wallet <- newTinyWallet nullTracer Fixture.testNetworkId (vk, sk) cp1 queryFn
assertQueryPoint QueryTip
reset wallet cp2
assertQueryPoint QueryTip
forAll genKeyPair $ \(vk, sk) -> do
(queryFn, assertQueryPoint) <- setupQuery vk
wallet <- newTinyWallet nullTracer Fixture.testNetworkId (vk, sk) queryFn
assertQueryPoint QueryTip
reset wallet
assertQueryPoint QueryTip

setupQuery ::
VerificationKey PaymentKey ->
Expand Down

0 comments on commit 54bb8f9

Please sign in to comment.