From 6b485dcfc2204e90a293834550c3455aebf937d2 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Mon, 7 Oct 2024 13:45:40 +0200 Subject: [PATCH] Add `Read.mockGenesisDataMainnet` --- .../customer-deposit-wallet.cabal | 1 + .../rest/Cardano/Wallet/Deposit/REST.hs | 2 +- .../src/Cardano/Wallet/Deposit/Read.hs | 51 +++++++++++++++++++ .../test/scenario/Test/Scenario/Blockchain.hs | 2 +- .../unit/Cardano/Wallet/Deposit/PureSpec.hs | 2 +- .../unit/Cardano/Wallet/Deposit/RESTSpec.hs | 2 +- 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal index e5a6f866649..a761d4a6bc8 100644 --- a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal +++ b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal @@ -72,6 +72,7 @@ library , mtl , OddWord , text + , time reexported-modules: Cardano.Wallet.Address.BIP32 exposed-modules: diff --git a/lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs b/lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs index 7999e04f97f..52312587546 100644 --- a/lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs +++ b/lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs @@ -240,7 +240,7 @@ findTheDepositWalletOnDisk dir action = do fromXPubAndGenesis identity (fromIntegral @Int users) - (error "FIXME") + Read.mockGenesisDataMainnet store <- newStore writeS store state action $ Right store diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs index 6fdd7570677..4f0eb3fcf1d 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs @@ -1,6 +1,8 @@ {-# LANGUAGE BinaryLiterals #-} +{-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE TypeApplications #-} -- | Indirection module that re-exports types -- used for reading data from the blockchain, @@ -49,6 +51,7 @@ module Cardano.Wallet.Deposit.Read , GenesisData , GenesisHash + , mockGenesisDataMainnet -- * Dummy Values useful for testing , dummyAddress @@ -79,11 +82,17 @@ import Data.Map import Data.Maybe ( fromJust ) +import Data.Time.Clock + ( UTCTime (..) + ) import Data.Word ( Word8 ) +import qualified Cardano.Chain.Common as Byron import qualified Cardano.Chain.Genesis as Byron +import qualified Cardano.Chain.Slotting as Byron +import qualified Cardano.Chain.Update as Byron import qualified Cardano.Wallet.Read as Read import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as B8 @@ -151,3 +160,45 @@ mockNextBlock old txs = -- GenesisData is not part of the ledger specification proper type GenesisData = Byron.GenesisData type GenesisHash = Byron.GenesisHash + +mockGenesisDataMainnet :: GenesisData +mockGenesisDataMainnet = Byron.GenesisData + { Byron.gdGenesisKeyHashes = mempty + , Byron.gdHeavyDelegation = Byron.UnsafeGenesisDelegation mempty + , Byron.gdStartTime = UTCTime (toEnum 0) 0 + , Byron.gdNonAvvmBalances = mempty + , Byron.gdProtocolParameters = protocolParametersMainnet + , gdK = toEnum 2160 + , gdProtocolMagicId = Byron.mainnetProtocolMagicId + , Byron.gdAvvmDistr = Byron.GenesisAvvmBalances mempty + } + +protocolParametersMainnet :: Byron.ProtocolParameters +protocolParametersMainnet = Byron.ProtocolParameters + { ppHeavyDelThd = mkLovelacePortionFromGenesisJSON 300000000000 + , ppMaxBlockSize = 2000000 + , ppMaxHeaderSize = 2000000 + , ppMaxProposalSize = 700 + , ppMaxTxSize = 4096 + , ppMpcThd = mkLovelacePortionFromGenesisJSON 20000000000000 + , ppScriptVersion = 0 + , ppSlotDuration = 20000 + , ppSoftforkRule = Byron.SoftforkRule + { srInitThd = mkLovelacePortionFromGenesisJSON 900000000000000 + , srMinThd = mkLovelacePortionFromGenesisJSON 600000000000000 + , srThdDecrement = mkLovelacePortionFromGenesisJSON 50000000000000 + } + , ppTxFeePolicy = Byron.TxFeePolicyTxSizeLinear + $ Byron.TxSizeLinear + (Byron.mkKnownLovelace @155381000000000) -- don't ask + 43946000000 + ,ppUnlockStakeEpoch = Byron.EpochNumber 18446744073709551615 + , ppUpdateProposalThd = mkLovelacePortionFromGenesisJSON 100000000000000 + , ppUpdateVoteThd = mkLovelacePortionFromGenesisJSON 1000000000000 + , ppUpdateProposalTTL = 1000 -- "updateImplicit" in JSON representation + } + +-- Make a 'LovelacePortion' from the numerical value in the genesis JSON file. +mkLovelacePortionFromGenesisJSON :: Rational -> Byron.LovelacePortion +mkLovelacePortionFromGenesisJSON n = + Byron.rationalToLovelacePortion (n / 10^(15 :: Integer)) diff --git a/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs b/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs index 54ecfc314e8..4a8a3475e47 100644 --- a/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs +++ b/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs @@ -79,7 +79,7 @@ withScenarioEnvMock action = do networkEnv <- mapBlock Read.EraValue <$> newNetworkEnvMock action $ ScenarioEnv - { genesisData = error "TODO: Mock Genesis Data" + { genesisData = Read.mockGenesisDataMainnet , networkEnv , faucet = Faucet{xprv = error "TODO: Faucet xprv"} } diff --git a/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/PureSpec.hs b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/PureSpec.hs index 4cc85afdfbc..503650ad23e 100644 --- a/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/PureSpec.hs +++ b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/PureSpec.hs @@ -115,7 +115,7 @@ testXPub = ------------------------------------------------------------------------------} testGenesis :: Read.GenesisData -testGenesis = undefined +testGenesis = Read.mockGenesisDataMainnet spendOneTxOut :: UTxO.UTxO -> Read.Tx spendOneTxOut utxo = diff --git a/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs index 5b97e7b6945..b82a2f28fb9 100644 --- a/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs +++ b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs @@ -52,7 +52,7 @@ import qualified Cardano.Wallet.Deposit.Read as Read import qualified Data.ByteString.Char8 as B8 fakeBootEnv :: WalletBootEnv IO -fakeBootEnv = WalletBootEnv undefined undefined undefined +fakeBootEnv = WalletBootEnv nullTracer Read.mockGenesisDataMainnet undefined xpub :: XPub xpub =