Skip to content

Commit

Permalink
add 'txMaxSize' to known blockchain parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ authored and paweljakubas committed Jul 15, 2019
1 parent 103604c commit 489f5b8
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 32 deletions.
26 changes: 15 additions & 11 deletions exe/wallet/http-bridge/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,20 @@ import Cardano.Wallet.DaedalusIPC
import Cardano.Wallet.DB
( DBLayer )
import Cardano.Wallet.HttpBridge.Compatibility
( HttpBridge, Network (..), byronFeePolicy, byronSlotLength )
( HttpBridge
, Network (..)
, byronFeePolicy
, byronSlotLength
, byronTxMaxSize
)
import Cardano.Wallet.HttpBridge.Environment
( KnownNetwork (..) )
import Cardano.Wallet.HttpBridge.Primitive.Types
( Tx )
import Cardano.Wallet.Network
( ErrNetworkTip, NetworkLayer, defaultRetryPolicy, waitForConnection )
import Cardano.Wallet.Primitive.AddressDerivation
( KeyToAddress )
import Cardano.Wallet.Primitive.AddressDiscovery
( SeqState )
import Cardano.Wallet.Primitive.Fee
( FeePolicy )
import Cardano.Wallet.Primitive.Types
( Block )
import Cardano.Wallet.Version
( showVersion, version )
import Control.Applicative
Expand Down Expand Up @@ -277,19 +276,24 @@ cmdServe = command "serve" $ info (helper <*> cmd) $ mempty
-> DBLayer IO s t
-> IO (WalletLayer s t)
newWalletLayer (sb, tracer) db = do
(nl, block0, feePolicy) <- newNetworkLayer (sb, tracer)
(nl, bp) <- newNetworkLayer (sb, tracer)
let tl = HttpBridge.newTransactionLayer @n
let bp = BlockchainParameters block0 feePolicy byronSlotLength
Wallet.newWalletLayer tracer bp db nl tl

newNetworkLayer
:: (Switchboard Text, Trace IO Text)
-> IO (NetworkLayer t IO, Block Tx, FeePolicy)
-> IO (NetworkLayer t IO, BlockchainParameters t)
newNetworkLayer (sb, tracer) = do
nl <- HttpBridge.newNetworkLayer @n (getPort nodePort)
waitForService @ErrNetworkTip "http-bridge" (sb, tracer) nodePort $
waitForConnection nl defaultRetryPolicy
return (nl, HttpBridge.block0, byronFeePolicy)
let bp = BlockchainParameters
{ getGenesisBlock = HttpBridge.block0
, getFeePolicy = byronFeePolicy
, getSlotLength = byronSlotLength
, getTxMaxSize = byronTxMaxSize
}
return (nl, bp)

withDBLayer
:: CM.Configuration
Expand Down
11 changes: 7 additions & 4 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ data BlockchainParameters t = BlockchainParameters
{ getGenesisBlock :: Block (Tx t)
-- ^ Very first block
, getFeePolicy :: FeePolicy
-- ^ Policy regarding transcation fee
, getSlotLength :: SlotLength
-- ^ Length, in seconds, of a slot
, getTxMaxSize :: Quantity "byte" Word16
-- ^ Maximum size of a transaction (soft or hard limit)
}

-- | Create a new instance of the wallet layer.
Expand All @@ -379,10 +383,7 @@ newWalletLayer
-> NetworkLayer t IO
-> TransactionLayer t
-> IO (WalletLayer s t)
newWalletLayer
tracer
(BlockchainParameters block0 feePolicy (SlotLength slotLength))
db nw tl = do
newWalletLayer tracer bp db nw tl = do
logDebugT $ "Wallet layer starting with: "
<> "block0: "+| block0 |+ ", "
<> "fee policy: "+|| feePolicy ||+""
Expand All @@ -404,6 +405,8 @@ newWalletLayer
, listTransactions = _listTransactions
}
where
BlockchainParameters block0 feePolicy (SlotLength slotLength) txMaxSize = bp

logDebugT :: MonadIO m => Text -> m ()
logDebugT = liftIO . logDebug tracer

Expand Down
13 changes: 8 additions & 5 deletions lib/core/test/unit/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,22 @@ setupFixture (wid, wname, wstate) = do
db <- newDBLayer
let nl = error "NetworkLayer"
let tl = dummyTransactionLayer
let bp = BlockchainParameters block0 dummyPolicy dummySlotLength
let bp = BlockchainParameters block0 policy slotLength txMaxSize
wl <- newWalletLayer @_ @DummyTarget nullTracer bp db nl tl
res <- runExceptT $ createWallet wl wid wname wstate
let wal = case res of
Left _ -> []
Right walletId -> [walletId]
pure $ WalletLayerFixture db wl wal
where
dummyPolicy :: FeePolicy
dummyPolicy = LinearFee (Quantity 14) (Quantity 42)
policy :: FeePolicy
policy = LinearFee (Quantity 14) (Quantity 42)

dummySlotLength :: SlotLength
dummySlotLength = SlotLength $ secondsToDiffTime 1
slotLength :: SlotLength
slotLength = SlotLength $ secondsToDiffTime 1

txMaxSize :: Quantity "byte" Word16
txMaxSize = Quantity 8192

-- | A dummy transaction layer to see the effect of a root private key. It
-- implements a fake signer that still produces sort of witnesses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Cardano.Wallet.HttpBridge.Compatibility
, block0
, byronFeePolicy
, byronSlotLength
, byronTxMaxSize
) where

import Prelude
Expand Down Expand Up @@ -62,6 +63,8 @@ import Data.Text.Class
( TextDecodingError (..) )
import Data.Time.Clock
( secondsToDiffTime )
import Data.Word
( Word16 )

import qualified Cardano.Wallet.HttpBridge.Binary as CBOR
import qualified Cardano.Wallet.HttpBridge.Primitive.Types as W
Expand Down Expand Up @@ -159,7 +162,10 @@ block0 = Block
byronFeePolicy :: FeePolicy
byronFeePolicy = LinearFee (Quantity 155381) (Quantity 43.946)


-- | Hard-coded slot duration
byronSlotLength :: SlotLength
byronSlotLength = SlotLength $ secondsToDiffTime 20

-- | Hard-coded max transaction size
byronTxMaxSize :: Quantity "byte" Word16
byronTxMaxSize = Quantity 8192
9 changes: 7 additions & 2 deletions lib/http-bridge/test/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Cardano.Wallet
import Cardano.Wallet.DB.Sqlite
( PersistState )
import Cardano.Wallet.HttpBridge.Compatibility
( HttpBridge, block0, byronFeePolicy, byronSlotLength )
( HttpBridge, block0, byronFeePolicy, byronSlotLength, byronTxMaxSize )
import Cardano.Wallet.HttpBridge.Environment
( KnownNetwork (..), Network (..) )
import Cardano.Wallet.HttpBridge.Network
Expand Down Expand Up @@ -212,7 +212,12 @@ bench_restoration _ (wid, wname, s) = withHttpBridge network $ \port -> do
let tl = newTransactionLayer
BlockHeader sl _ <- unsafeRunExceptT $ networkTip nw
sayErr . fmt $ network ||+ " tip is at " +|| sl ||+ ""
let bp = BlockchainParameters block0 byronFeePolicy byronSlotLength
let bp = BlockchainParameters
{ getGenesisBlock = block0
, getFeePolicy = byronFeePolicy
, getSlotLength = byronSlotLength
, getTxMaxSize = byronTxMaxSize
}
w <- newWalletLayer @_ @t nullTracer bp db nw tl
wallet <- unsafeRunExceptT $ createWallet w wid wname s
unsafeRunExceptT $ restoreWallet w wallet
Expand Down
4 changes: 2 additions & 2 deletions lib/http-bridge/test/integration/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Cardano.Launcher
import Cardano.Wallet
( BlockchainParameters (..), WalletLayer (..), newWalletLayer )
import Cardano.Wallet.HttpBridge.Compatibility
( HttpBridge, block0, byronFeePolicy, byronSlotLength )
( HttpBridge, block0, byronFeePolicy, byronSlotLength, byronTxMaxSize )
import Cardano.Wallet.HttpBridge.Environment
( KnownNetwork (..), Network (..) )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -81,6 +81,6 @@ spec = do
db <- MVar.newDBLayer
nl <- HttpBridge.newNetworkLayer @'Testnet port
let tl = HttpBridge.newTransactionLayer
let bp = BlockchainParameters block0 byronFeePolicy byronSlotLength
let bp = BlockchainParameters block0 byronFeePolicy byronSlotLength byronTxMaxSize
(handle,) <$>
(newWalletLayer @_ @(HttpBridge 'Testnet) nullTracer bp db nl tl)
9 changes: 7 additions & 2 deletions lib/http-bridge/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Cardano.Wallet.Api.Server
import Cardano.Wallet.DB.Sqlite
( SqliteContext )
import Cardano.Wallet.HttpBridge.Compatibility
( HttpBridge, block0, byronFeePolicy, byronSlotLength )
( HttpBridge, block0, byronFeePolicy, byronSlotLength, byronTxMaxSize )
import Cardano.Wallet.HttpBridge.Environment
( Network (..) )
import Cardano.Wallet.Network
Expand Down Expand Up @@ -264,7 +264,12 @@ main = do
mvar <- newEmptyMVar
thread <- forkIO $ do
let tl = HttpBridge.newTransactionLayer
let bp = BlockchainParameters block0 byronFeePolicy byronSlotLength
let bp = BlockchainParameters
{ getGenesisBlock = block0
, getFeePolicy = byronFeePolicy
, getSlotLength = byronSlotLength
, getTxMaxSize = byronTxMaxSize
}
wallet <- newWalletLayer nullTracer bp db nl tl
let listen = fromMaybe (ListenOnPort defaultPort) mlisten
Server.withListeningSocket listen $ \(port, socket) -> do
Expand Down
13 changes: 13 additions & 0 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Cardano.Wallet.Jormungandr.Compatibility
Jormungandr
, Network (..)
, block0
, softTxMaxSize

-- * Node's Configuration
, BaseUrl (..)
Expand Down Expand Up @@ -63,8 +64,12 @@ import Data.Maybe
( fromJust, isJust )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text.Class
( TextDecodingError (..) )
import Data.Word
( Word16 )
import Servant.Client.Core
( BaseUrl (..), Scheme (..) )
import System.FilePath
Expand All @@ -90,6 +95,14 @@ block0 = BlockHeader
, prevBlockHash = Hash (BS.replicate 32 0)
}

-- | Jörmugandr's chain parameter doesn't include a transaction max size. The
-- actual hard-limit for the size is constrained by the binary format and
-- numbers used to represent the number of inputs and outputs (Word8), yet
-- there's also a soft-limit of 8kb which results in much smaller transactions
-- in the end.
softTxMaxSize :: Quantity "byte" Word16
softTxMaxSize = Quantity 8192

instance DefineTx (Jormungandr network) where
type Tx (Jormungandr network) = Tx
inputs = fmap fst . inputs
Expand Down
9 changes: 7 additions & 2 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import Cardano.Wallet.Jormungandr.Api
import Cardano.Wallet.Jormungandr.Binary
( ConfigParam (..), Message (..), coerceBlock )
import Cardano.Wallet.Jormungandr.Compatibility
( Jormungandr )
( Jormungandr, softTxMaxSize )
import Cardano.Wallet.Jormungandr.Primitive.Types
( Tx )
import Cardano.Wallet.Network
Expand Down Expand Up @@ -267,7 +267,12 @@ mkJormungandrLayer mgr baseUrl = JormungandrLayer

case (mpolicy,mduration) of
([policy],[duration]) ->
return $ BlockchainParameters (coerceBlock jblock) policy (SlotLength duration)
return $ BlockchainParameters
{ getGenesisBlock = coerceBlock jblock
, getFeePolicy = policy
, getSlotLength = SlotLength duration
, getTxMaxSize = softTxMaxSize
}
_ ->
throwE $ ErrGetBlockchainParamsNoInitialPolicy params
}
Expand Down
5 changes: 2 additions & 3 deletions lib/jormungandr/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ cardanoWalletServer
cardanoWalletServer mlisten = do
logConfig <- CM.empty
tracer <- initTracer Info "serve"
(nl, bp@(BlockchainParameters _ feePolicy _) ) <-
newNetworkLayer jormungandrUrl block0H
(nl, bp) <- newNetworkLayer jormungandrUrl block0H
(sqlCtx, db) <- Sqlite.newDBLayer @_ @network logConfig tracer Nothing
mvar <- newEmptyMVar
handle <- async $ do
Expand All @@ -192,7 +191,7 @@ cardanoWalletServer mlisten = do
let settings = Warp.defaultSettings
& setBeforeMainLoop (putMVar mvar port)
Server.start settings tracer socket wallet
(handle,,feePolicy,sqlCtx) <$> takeMVar mvar
(handle, , getFeePolicy bp, sqlCtx) <$> takeMVar mvar
where
jormungandrUrl :: BaseUrl
jormungandrUrl = BaseUrl Http "localhost" 8080 "/api"
Expand Down

0 comments on commit 489f5b8

Please sign in to comment.