Skip to content

Commit

Permalink
API churn
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Sep 16, 2020
1 parent c969483 commit 810c5dc
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 106 deletions.
8 changes: 4 additions & 4 deletions lib/shelley/src/Cardano/Wallet/Shelley.hs
Expand Up @@ -121,7 +121,7 @@ import Cardano.Wallet.Registry
import Cardano.Wallet.Shelley.Api.Server
( server )
import Cardano.Wallet.Shelley.Compatibility
( CardanoBlock, TPraosStandardCrypto, fromCardanoBlock )
( CardanoBlock, StandardCrypto, fromCardanoBlock )
import Cardano.Wallet.Shelley.Network
( NetworkLayerLog, withNetworkLayer )
import Cardano.Wallet.Shelley.Pools
Expand Down Expand Up @@ -338,7 +338,7 @@ serveWallet
withPoolsMonitoring
:: Maybe FilePath
-> GenesisParameters
-> NetworkLayer IO t (CardanoBlock TPraosStandardCrypto)
-> NetworkLayer IO t (CardanoBlock StandardCrypto)
-> (StakePoolLayer -> IO a)
-> IO a
withPoolsMonitoring dir gp nl action =
Expand Down Expand Up @@ -371,7 +371,7 @@ serveWallet
, WalletKey k
)
=> TransactionLayer t k
-> NetworkLayer IO t (CardanoBlock TPraosStandardCrypto)
-> NetworkLayer IO t (CardanoBlock StandardCrypto)
-> (WorkerCtx (ApiLayer s t k) -> WalletId -> IO ())
-> IO (ApiLayer s t k)
apiLayer tl nl coworker = do
Expand Down Expand Up @@ -468,7 +468,7 @@ data Tracers' f = Tracers
, poolsEngineTracer :: f (WorkerLog Text StakePoolLog)
, poolsDbTracer :: f PoolDbLog
, ntpClientTracer :: f NtpTrace
, networkTracer :: f (NetworkLayerLog TPraosStandardCrypto)
, networkTracer :: f (NetworkLayerLog StandardCrypto)
}

-- | All of the Shelley 'Tracer's.
Expand Down
68 changes: 36 additions & 32 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Expand Up @@ -27,7 +27,8 @@ module Cardano.Wallet.Shelley.Compatibility
, CardanoBlock

, NodeVersionData
, TPraosStandardCrypto
, StandardCrypto
, StandardShelley

-- * Chain Parameters
, mainnetVersionData
Expand Down Expand Up @@ -104,6 +105,8 @@ import Cardano.Binary
( fromCBOR, serialize' )
import Cardano.Crypto.Hash.Class
( Hash (UnsafeHash), hashToBytes )
import Cardano.Ledger.Era
( Era (..) )
import Cardano.Slotting.Slot
( EpochNo (..), EpochSize (..) )
import Cardano.Wallet.Api.Types
Expand Down Expand Up @@ -184,11 +187,11 @@ import Ouroboros.Consensus.HardFork.Combinator.AcrossEras
import Ouroboros.Consensus.HardFork.History.Summary
( Bound (..) )
import Ouroboros.Consensus.Shelley.Ledger
( Crypto, ShelleyHash (..) )
( ShelleyHash (..) )
import Ouroboros.Consensus.Shelley.Ledger.Block
( ShelleyBlock (..) )
import Ouroboros.Consensus.Shelley.Protocol.Crypto
( TPraosStandardCrypto )
( StandardCrypto, StandardShelley )
import Ouroboros.Network.Block
( BlockNo (..)
, ChainHash
Expand Down Expand Up @@ -217,6 +220,7 @@ import Type.Reflection
import qualified Cardano.Api.Typed as Cardano
import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Ledger.Crypto as SL
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.Binary.Bech32.TH as Bech32
Expand Down Expand Up @@ -341,9 +345,9 @@ toPoint genesisH (W.BlockHeader sl _ (W.Hash h) _)
| otherwise = O.BlockPoint sl (OneEraHash $ toShort h)

toCardanoBlockHeader
:: O.Crypto sc
:: forall c. (SL.Crypto c, Era (SL.Shelley c))
=> W.GenesisParameters
-> CardanoBlock sc
-> CardanoBlock c
-> W.BlockHeader
toCardanoBlockHeader gp = \case
BlockByron blk ->
Expand All @@ -352,9 +356,9 @@ toCardanoBlockHeader gp = \case
toShelleyBlockHeader (W.getGenesisBlockHash gp) blk

toShelleyBlockHeader
:: O.Crypto sc
:: Era e
=> W.Hash "Genesis"
-> ShelleyBlock sc
-> ShelleyBlock e
-> W.BlockHeader
toShelleyBlockHeader genesisHash blk =
let
Expand All @@ -372,14 +376,14 @@ toShelleyBlockHeader genesisHash blk =
SL.bheaderPrev header
}

getProducer :: O.Crypto sc => ShelleyBlock sc -> W.PoolId
getProducer :: Era e => ShelleyBlock e -> W.PoolId
getProducer (ShelleyBlock (SL.Block (SL.BHeader header _) _) _) =
fromPoolKeyHash $ SL.hashKey (SL.bheaderVk header)

fromCardanoBlock
:: O.Crypto sc
:: Era (SL.Shelley c)
=> W.GenesisParameters
-> CardanoBlock sc
-> CardanoBlock c
-> W.Block
fromCardanoBlock gp = \case
BlockByron blk ->
Expand All @@ -398,8 +402,8 @@ fromCardanoBlock gp = \case
}

poolCertsFromShelleyBlock
:: O.Crypto sc
=> ShelleyBlock sc
:: Era e
=> ShelleyBlock e
-> (W.SlotNo, [W.PoolCertificate])
poolCertsFromShelleyBlock blk =
let
Expand Down Expand Up @@ -433,7 +437,7 @@ fromChainHash genesisHash = \case

fromShelleyChainHash
:: W.Hash "Genesis"
-> ChainHash (ShelleyBlock sc)
-> ChainHash (ShelleyBlock e)
-> W.Hash "BlockHeader"
fromShelleyChainHash genesisHash = \case
O.GenesisHash -> coerce genesisHash
Expand Down Expand Up @@ -552,9 +556,9 @@ minimumUTxOvalueFromPParams pp = toWalletCoin $ SL._minUTxOValue pp

-- | Convert genesis data into blockchain params and an initial set of UTxO
fromGenesisData
:: forall crypto. (O.Crypto crypto)
=> ShelleyGenesis crypto
-> [(SL.Addr crypto, SL.Coin)]
:: forall e crypto. (Era e, e ~ SL.Shelley crypto)
=> ShelleyGenesis e
-> [(SL.Addr e, SL.Coin)]
-> (W.NetworkParameters, W.Block)
fromGenesisData g initialFunds =
( W.NetworkParameters
Expand Down Expand Up @@ -590,8 +594,7 @@ fromGenesisData g initialFunds =
-- The genesis data on haskell nodes is not a block at all, unlike the
-- block0 on jormungandr. This function is a method to deal with the
-- discrepancy.
genesisBlockFromTxOuts
:: [(SL.Addr crypto, SL.Coin)] -> W.Block
genesisBlockFromTxOuts :: [(SL.Addr e, SL.Coin)] -> W.Block
genesisBlockFromTxOuts outs = W.Block
{ delegations = []
, header = W.BlockHeader
Expand All @@ -615,7 +618,7 @@ fromGenesisData g initialFunds =
Nothing
where
W.TxIn pseudoHash _ = fromShelleyTxIn $
SL.initialFundsPseudoTxIn @crypto addr
SL.initialFundsPseudoTxIn @e addr

fromNetworkMagic :: NetworkMagic -> W.ProtocolMagic
fromNetworkMagic (NetworkMagic magic) =
Expand Down Expand Up @@ -659,18 +662,18 @@ optimumNumberOfPools = unsafeConvert . SL._nOpt
fromShelleyTxId :: SL.TxId crypto -> W.Hash "Tx"
fromShelleyTxId (SL.TxId (UnsafeHash h)) = W.Hash $ fromShort h

fromShelleyTxIn :: Crypto crypto => SL.TxIn crypto -> W.TxIn
fromShelleyTxIn :: Era e => SL.TxIn e -> W.TxIn
fromShelleyTxIn (SL.TxIn txid ix) =
W.TxIn (fromShelleyTxId txid) (unsafeCast ix)
where
unsafeCast :: Natural -> Word32
unsafeCast = fromIntegral

fromShelleyTxOut :: Crypto crypto => SL.TxOut crypto -> W.TxOut
fromShelleyTxOut :: Era e => SL.TxOut e -> W.TxOut
fromShelleyTxOut (SL.TxOut addr amount) =
W.TxOut (fromShelleyAddress addr) (fromShelleyCoin amount)

fromShelleyAddress :: SL.Addr crypto -> W.Address
fromShelleyAddress :: SL.Addr e -> W.Address
fromShelleyAddress = W.Address
. SL.serialiseAddr

Expand All @@ -689,8 +692,8 @@ toShelleyCoin (W.Coin c) = SL.Coin $ safeCast c

-- NOTE: For resolved inputs we have to pass in a dummy value of 0.
fromShelleyTx
:: Crypto crypto
=> SL.Tx crypto
:: Era e
=> SL.Tx e
-> ( W.Tx
, [W.DelegationCertificate]
, [W.PoolCertificate]
Expand Down Expand Up @@ -819,7 +822,7 @@ toByronNetworkMagic pm@(W.ProtocolMagic magic) =
-- | SealedTx are the result of rightfully constructed shelley transactions so, it
-- is relatively safe to unserialize them from CBOR.
unsealShelleyTx
:: (HasCallStack, Crypto c)
:: (HasCallStack, Era (SL.Shelley c))
=> W.SealedTx
-> CardanoGenTx c
unsealShelleyTx = GenTxShelley
Expand Down Expand Up @@ -910,9 +913,9 @@ instance EncodeStakeAddress ('Testnet pm) where
encodeStakeAddress = _encodeStakeAddress SL.Testnet

instance DecodeStakeAddress 'Mainnet where
decodeStakeAddress = _decodeStakeAddress SL.Mainnet
decodeStakeAddress = _decodeStakeAddress @StandardCrypto SL.Mainnet
instance DecodeStakeAddress ('Testnet pm) where
decodeStakeAddress = _decodeStakeAddress SL.Testnet
decodeStakeAddress = _decodeStakeAddress @StandardCrypto SL.Testnet

stakeAddressPrefix :: Word8
stakeAddressPrefix = 0xE0
Expand Down Expand Up @@ -940,13 +943,14 @@ _encodeStakeAddress network (W.ChimericAccount acct) =
putByteString acct

_decodeStakeAddress
:: SL.Network
:: forall c. SL.Crypto c
=> SL.Network
-> Text
-> Either TextDecodingError W.ChimericAccount
_decodeStakeAddress serverNetwork txt = do
(_, dp) <- left (const errBech32) $ Bech32.decodeLenient txt
bytes <- maybe (Left errBech32) Right $ dataPartToBytes dp
rewardAcnt <- runGetOrFail' (SL.getRewardAcnt @TPraosStandardCrypto) bytes
rewardAcnt <- runGetOrFail' (SL.getRewardAcnt @(SL.Shelley c)) bytes

guardNetwork (SL.getRwdNetwork rewardAcnt) serverNetwork

Expand Down Expand Up @@ -1002,7 +1006,7 @@ _decodeAddress
_decodeAddress serverNetwork text =
case tryBase16 <|> tryBech32 <|> tryBase58 of
Just bytes ->
decodeShelleyAddress bytes
decodeShelleyAddress @StandardCrypto bytes
_ ->
Left $ TextDecodingError
"Unrecognized address encoding: must be either bech32, base58 or base16"
Expand All @@ -1023,9 +1027,9 @@ _decodeAddress serverNetwork text =
tryBase16 =
either (const Nothing) Just $ convertFromBase Base16 (T.encodeUtf8 text)

decodeShelleyAddress :: ByteString -> Either TextDecodingError W.Address
decodeShelleyAddress :: forall c. (SL.Crypto c) => ByteString -> Either TextDecodingError W.Address
decodeShelleyAddress bytes = do
case SL.deserialiseAddr @TPraosStandardCrypto bytes of
case SL.deserialiseAddr @(SL.Shelley c) bytes of
Just (SL.Addr addrNetwork _ _) -> do
guardNetwork addrNetwork serverNetwork
pure (W.Address bytes)
Expand Down
6 changes: 2 additions & 4 deletions lib/shelley/src/Cardano/Wallet/Shelley/Launch.hs
Expand Up @@ -90,7 +90,7 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Shelley
( SomeNetworkDiscriminant (..) )
import Cardano.Wallet.Shelley.Compatibility
( NodeVersionData )
( NodeVersionData, StandardShelley )
import Cardano.Wallet.Unsafe
( unsafeFromHex, unsafeRunExceptT )
import Control.Arrow
Expand Down Expand Up @@ -151,8 +151,6 @@ import Options.Applicative
( Parser, flag', help, long, metavar, (<|>) )
import Ouroboros.Consensus.Shelley.Node
( sgNetworkMagic )
import Ouroboros.Consensus.Shelley.Protocol
( TPraosStandardCrypto )
import Ouroboros.Network.Magic
( NetworkMagic (..) )
import Ouroboros.Network.NodeToClient
Expand Down Expand Up @@ -824,7 +822,7 @@ genConfig dir severity systemStart = do
----
-- Parameters
shelleyGenesis <- Yaml.decodeFileThrow
@_ @(ShelleyGenesis TPraosStandardCrypto) shelleyGenesisFile
@_ @(ShelleyGenesis StandardShelley) shelleyGenesisFile
let networkMagic = sgNetworkMagic shelleyGenesis
let shelleyParams = fst $ Shelley.fromGenesisData shelleyGenesis []
let versionData =
Expand Down

0 comments on commit 810c5dc

Please sign in to comment.