Skip to content

Commit d287e5d

Browse files
committed
Simplify blockfrost setup
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 97b7dbd commit d287e5d

File tree

7 files changed

+87
-124
lines changed

7 files changed

+87
-124
lines changed

hydra-node/hydra-node.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ library
5858
Hydra.Chain
5959
Hydra.Chain.Blockfrost
6060
Hydra.Chain.Blockfrost.Client
61-
Hydra.Chain.Blockfrost.TimeHandle
6261
Hydra.Chain.CardanoClient
6362
Hydra.Chain.Direct
6463
Hydra.Chain.Direct.Handlers
@@ -67,7 +66,6 @@ library
6766
Hydra.Chain.Direct.Wallet
6867
Hydra.Chain.Offline
6968
Hydra.Chain.ScriptRegistry
70-
Hydra.Chain.Wallet
7169
Hydra.Events
7270
Hydra.Events.FileBased
7371
Hydra.HeadLogic

hydra-node/src/Hydra/Chain/Direct.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ import Hydra.Chain.Direct.TimeHandle (queryTimeHandle)
8383
import Hydra.Chain.Direct.Util (
8484
readKeyPair,
8585
)
86-
import Hydra.Chain.Direct.Wallet (newTinyWallet)
87-
import Hydra.Chain.ScriptRegistry (queryScriptRegistry)
88-
import Hydra.Chain.Wallet (
86+
import Hydra.Chain.Direct.Wallet (
8987
TinyWallet (..),
9088
WalletInfoOnChain (..),
89+
newTinyWallet,
9190
)
91+
import Hydra.Chain.ScriptRegistry (queryScriptRegistry)
9292
import Hydra.Logging (Tracer, traceWith)
9393
import Hydra.Node.Util (readKeyPair)
9494
import Hydra.Options (DirectChainConfig (..))

hydra-node/src/Hydra/Chain/Direct/Handlers.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import Hydra.Chain.Direct.State (
5757
recover,
5858
)
5959
import Hydra.Chain.Direct.TimeHandle (TimeHandle (..))
60-
import Hydra.Chain.Wallet (
60+
import Hydra.Chain.Direct.Wallet (
6161
ErrCoverFee (..),
6262
TinyWallet (..),
6363
TinyWalletLog,

hydra-node/src/Hydra/Chain/Direct/Wallet.hs

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ import Cardano.Ledger.Babbage.TxBody qualified as Babbage
5555
import Cardano.Ledger.Babbage.UTxO (getReferenceScripts)
5656
import Cardano.Ledger.BaseTypes qualified as Ledger
5757
import Cardano.Ledger.Coin (Coin (..))
58+
import Cardano.Ledger.Core (
59+
TxUpgradeError,
60+
)
5861
import Cardano.Ledger.Core qualified as Core
5962
import Cardano.Ledger.Core qualified as Ledger
6063
import Cardano.Ledger.Hashes (EraIndependentTxBody, HashAnnotated, hashAnnotated)
@@ -72,9 +75,13 @@ import Data.Ratio ((%))
7275
import Data.Sequence.Strict ((|>))
7376
import Data.Set qualified as Set
7477
import Hydra.Cardano.Api (
78+
BlockHeader,
79+
ChainPoint,
80+
LedgerEra,
7581
NetworkId,
7682
PaymentCredential (PaymentCredentialByKey),
7783
PaymentKey,
84+
ShelleyAddr,
7885
SigningKey,
7986
StakeAddressReference (NoStakeAddress),
8087
VerificationKey,
@@ -91,20 +98,49 @@ import Hydra.Cardano.Api (
9198
)
9299
import Hydra.Cardano.Api qualified as Api
93100
import Hydra.Chain.CardanoClient (QueryPoint (..))
94-
import Hydra.Chain.Wallet (
95-
Address,
96-
ChainQuery,
97-
ChangeError (..),
98-
ErrCoverFee (..),
99-
TinyWallet (..),
100-
TinyWalletLog (..),
101-
TxIn,
102-
TxOut,
103-
WalletInfoOnChain (..),
104-
)
105101
import Hydra.Ledger.Cardano ()
106102
import Hydra.Logging (Tracer, traceWith)
107103

104+
type Address = Ledger.Addr StandardCrypto
105+
type TxIn = Ledger.TxIn StandardCrypto
106+
type TxOut = Ledger.TxOut LedgerEra
107+
108+
-- | A 'TinyWallet' is a small abstraction of a wallet with basic UTXO
109+
-- management. The wallet is assumed to have only one address, and only one UTXO
110+
-- at that address. It can sign transactions and keeps track of its UTXO behind
111+
-- the scene.
112+
--
113+
-- The wallet is connecting to the node initially and when asked to 'reset'.
114+
-- Otherwise it can be fed blocks via 'update' as the chain rolls forward.
115+
data TinyWallet m = TinyWallet
116+
{ getUTxO :: STM m (Map TxIn TxOut)
117+
-- ^ Return all known UTxO addressed to this wallet.
118+
, getSeedInput :: STM m (Maybe Api.TxIn)
119+
-- ^ Returns the /seed input/
120+
-- This is the special input needed by `Direct` chain component to initialise
121+
-- a head
122+
, sign :: Api.Tx -> Api.Tx
123+
, coverFee ::
124+
UTxO ->
125+
Api.Tx ->
126+
m (Either ErrCoverFee Api.Tx)
127+
, reset :: m ()
128+
-- ^ Re-initializ wallet against the latest tip of the node and start to
129+
-- ignore 'update' calls until reaching that tip.
130+
, update :: BlockHeader -> [Api.Tx] -> m ()
131+
-- ^ Update the wallet state given a block and list of txs. May be ignored if
132+
-- wallet is still initializing.
133+
}
134+
135+
data WalletInfoOnChain = WalletInfoOnChain
136+
{ walletUTxO :: Map TxIn TxOut
137+
, systemStart :: SystemStart
138+
, tip :: ChainPoint
139+
-- ^ Latest point on chain the wallet knows of.
140+
}
141+
142+
type ChainQuery m = QueryPoint -> Api.Address ShelleyAddr -> m WalletInfoOnChain
143+
108144
-- | Create a new tiny wallet handle.
109145
newTinyWallet ::
110146
-- | A tracer for logging
@@ -193,6 +229,19 @@ getTxId ::
193229
Ledger.TxId
194230
getTxId tx = Ledger.TxId $ hashAnnotated (body tx)
195231

232+
-- | This are all the error that can happen during coverFee.
233+
data ErrCoverFee
234+
= ErrNotEnoughFunds ChangeError
235+
| ErrNoFuelUTxOFound
236+
| ErrUnknownInput {input :: TxIn}
237+
| ErrScriptExecutionFailed {redeemerPointer :: Text, scriptFailure :: Text}
238+
| ErrTranslationError (ContextError LedgerEra)
239+
| ErrConwayUpgradeError (TxUpgradeError Conway)
240+
deriving stock (Show)
241+
242+
data ChangeError = ChangeError {inputBalance :: Coin, outputBalance :: Coin}
243+
deriving stock (Show)
244+
196245
-- | Cover fee for a transaction body using the given UTXO set. This calculate
197246
-- necessary fees and augments inputs / outputs / collateral accordingly to
198247
-- cover for the transaction cost and get the change back.
@@ -387,3 +436,21 @@ estimateScriptsCost pparams systemStart epochInfo utxo tx = do
387436
{ redeemerPointer = show ptr
388437
, scriptFailure = show failure
389438
}
439+
440+
--
441+
-- Logs
442+
--
443+
444+
data TinyWalletLog
445+
= BeginInitialize
446+
| EndInitialize {initialUTxO :: Api.UTxO, tip :: ChainPoint}
447+
| BeginUpdate {point :: ChainPoint}
448+
| EndUpdate {newUTxO :: Api.UTxO}
449+
| SkipUpdate {point :: ChainPoint}
450+
deriving stock (Eq, Generic, Show)
451+
452+
deriving anyclass instance ToJSON TinyWalletLog
453+
454+
instance Arbitrary TinyWalletLog where
455+
arbitrary = genericArbitrary
456+
shrink = genericShrink

hydra-node/src/Hydra/Chain/Wallet.hs

Lines changed: 0 additions & 100 deletions
This file was deleted.

hydra-node/test/Hydra/Chain/Direct/WalletSpec.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,16 @@ import Hydra.Cardano.Api.Pretty (renderTx)
4343
import Hydra.Cardano.Api.Tx (signTx, toLedgerTx)
4444
import Hydra.Chain.CardanoClient (QueryPoint (..))
4545
import Hydra.Chain.Direct.Wallet (
46-
applyTxs,
47-
coverFee_,
48-
findLargestUTxO,
49-
newTinyWallet,
50-
)
51-
import Hydra.Chain.Wallet (
5246
Address,
5347
ChainQuery,
5448
TinyWallet (..),
5549
TxIn,
5650
TxOut,
5751
WalletInfoOnChain (..),
52+
applyTxs,
53+
coverFee_,
54+
findLargestUTxO,
55+
newTinyWallet,
5856
)
5957
import Test.Hydra.Tx.Fixture qualified as Fixture
6058
import Test.Hydra.Tx.Gen (genKeyPair, genOneUTxOFor, genSigningKey)

hydra-node/test/Hydra/Model/MockChain.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Hydra.Chain.Direct.Handlers (
5555
)
5656
import Hydra.Chain.Direct.State (ChainContext (..), initialChainState)
5757
import Hydra.Chain.Direct.TimeHandle (TimeHandle, mkTimeHandle)
58-
import Hydra.Chain.Wallet (TinyWallet (..))
58+
import Hydra.Chain.Direct.Wallet (TinyWallet (..))
5959
import Hydra.HeadLogic (
6060
ClosedState (..),
6161
HeadState (..),

0 commit comments

Comments
 (0)