Skip to content

Commit

Permalink
Stop reserializing in abstract size
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Corduan committed May 19, 2020
1 parent 584119e commit 92e4d6c
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 135 deletions.
Expand Up @@ -67,9 +67,9 @@ import Cardano.Binary
serialize',
serializeEncoding,
serializeEncoding',
szCases,
withSlice,
withWordSize,
szCases,
)
import Cardano.Crypto.Hash (SHA256)
import qualified Cardano.Crypto.Hash.Class as Hash
Expand All @@ -90,10 +90,10 @@ import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
import Data.Sequence (Seq)
import Data.Word (Word64)
import qualified Data.Sequence as Seq
import Data.Sequence.Strict (StrictSeq)
import qualified Data.Sequence.Strict as StrictSeq
import Data.Word (Word64)
import GHC.Generics (Generic)
import Numeric.Natural (Natural)
import Shelley.Spec.Ledger.BaseTypes
Expand Down Expand Up @@ -201,9 +201,9 @@ instance
encodePreEncoded $ BSL.toStrict $
bodyBytes <> witsBytes <> metadataBytes
encodedGroupSizeExpr size _proxy =
encodedSizeExpr size (Proxy :: Proxy ByteString)
+ encodedSizeExpr size (Proxy :: Proxy ByteString)
+ encodedSizeExpr size (Proxy :: Proxy ByteString)
encodedSizeExpr size (Proxy :: Proxy ByteString)
+ encodedSizeExpr size (Proxy :: Proxy ByteString)
+ encodedSizeExpr size (Proxy :: Proxy ByteString)
listLen _ = 3
listLenBound _ = 3

Expand Down Expand Up @@ -276,9 +276,9 @@ instance
where
toCBOR (BHeader' _ _ bytes) = encodePreEncoded (BSL.toStrict bytes)
encodedSizeExpr size proxy =
1
+ encodedSizeExpr size (bHeaderBody' <$> proxy)
+ KES.encodedSigKESSizeExpr ((KES.getSig . bHeaderSig') <$> proxy)
1
+ encodedSizeExpr size (bHeaderBody' <$> proxy)
+ KES.encodedSigKESSizeExpr ((KES.getSig . bHeaderSig') <$> proxy)

instance
Crypto crypto =>
Expand All @@ -303,16 +303,22 @@ instance
where
toCBOR GenesisHash = encodeNull
toCBOR (BlockHash h) = toCBOR h
encodedSizeExpr size proxy =
encodedSizeExpr size proxy =
szCases
[ Case "GenesisHash" 1
, Case "BlockHash" (encodedSizeExpr size
((\case
-- we are mapping a 'Proxy', so nothing can
-- go wrong here
GenesisHash -> error "impossible happend"
BlockHash h -> h)
<$> proxy))
[ Case "GenesisHash" 1,
Case
"BlockHash"
( encodedSizeExpr
size
( ( \case
-- we are mapping a 'Proxy', so nothing can
-- go wrong here
GenesisHash -> error "impossible happend"
BlockHash h -> h
)
<$> proxy
)
)
]

instance
Expand Down Expand Up @@ -415,29 +421,28 @@ instance
pv = bprotver bhBody

encodedSizeExpr size proxy =
fromInteger (withWordSize $ 9 + listLenBound oc + listLenBound pv)
+ encodedSizeExpr size (bheaderBlockNo <$> proxy)
+ encodedSizeExpr size (bheaderSlotNo <$> proxy)
+ encodedSizeExpr size (bheaderPrev <$> proxy)
+ encodedSizeExpr size (bheaderVk <$> proxy)
+ VRF.encodedVerKeyVRFSizeExpr (bheaderVrfVk <$> proxy)
+ encodedSizeExpr size (bheaderEta <$> proxy)
+ encodedSizeExpr size (bheaderL <$> proxy)
+ encodedSizeExpr size ((toWord64 . bsize) <$> proxy)
+ encodedSizeExpr size (bhash <$> proxy)
+ encodedSizeExpr size (bheaderOCert <$> proxy)
+ encodedSizeExpr size (bprotver <$> proxy)
fromInteger (withWordSize $ 9 + listLenBound oc + listLenBound pv)
+ encodedSizeExpr size (bheaderBlockNo <$> proxy)
+ encodedSizeExpr size (bheaderSlotNo <$> proxy)
+ encodedSizeExpr size (bheaderPrev <$> proxy)
+ encodedSizeExpr size (bheaderVk <$> proxy)
+ VRF.encodedVerKeyVRFSizeExpr (bheaderVrfVk <$> proxy)
+ encodedSizeExpr size (bheaderEta <$> proxy)
+ encodedSizeExpr size (bheaderL <$> proxy)
+ encodedSizeExpr size ((toWord64 . bsize) <$> proxy)
+ encodedSizeExpr size (bhash <$> proxy)
+ encodedSizeExpr size (bheaderOCert <$> proxy)
+ encodedSizeExpr size (bprotver <$> proxy)
where
oc = bheaderOCert <$> proxy
pv = bprotver <$> proxy

pv = bprotver <$> proxy
toWord64 :: Natural -> Word64
toWord64 = fromIntegral


instance Crypto crypto
=> FromCBOR (BHBody crypto)
where
instance
Crypto crypto =>
FromCBOR (BHBody crypto)
where
fromCBOR = do
n <- decodeListLen
bheaderBlockNo <- fromCBOR
Expand Down
Expand Up @@ -93,13 +93,12 @@ instance ToCBORGroup Ptr where
<> toCBOR (fromInteger (toInteger txIx) :: Word)
<> toCBOR (fromInteger (toInteger certIx) :: Word)
encodedGroupSizeExpr size_ proxy =
encodedSizeExpr size_ (getSlotNo <$> proxy)
+ encodedSizeExpr size_ (getIx1 <$> proxy)
+ encodedSizeExpr size_ (getIx2 <$> proxy)
encodedSizeExpr size_ (getSlotNo <$> proxy)
+ encodedSizeExpr size_ (getIx1 <$> proxy)
+ encodedSizeExpr size_ (getIx2 <$> proxy)
where
getSlotNo :: Ptr -> SlotNo
getSlotNo (Ptr a _ _) = a

getIx1, getIx2 :: Ptr -> Ix
getIx1 (Ptr _ x _) = x
getIx2 (Ptr _ _ x) = x
Expand Down
Expand Up @@ -86,7 +86,6 @@ import Cardano.Binary
encodeNull,
enforceSize,
peekTokenType,
serialize,
)
import Cardano.Crypto.Hash (hashWithSerialiser)
import Cardano.Prelude (NoUnexpectedThunks (..))
Expand Down Expand Up @@ -646,16 +645,7 @@ txsize tx = numInputs * inputSize + numOutputs * outputSize + rest
inputSize = smallArray + uint + hashObj
numOutputs = toInteger . length . _outputs $ txbody
outputSize = smallArray + uint + address
rest =
toInteger . BSL.length . serialize $
tx
{ _body =
txbody
{ _outputs = StrictSeq.empty,
_inputs = Set.empty,
_txfee = 0
}
}
rest = fromIntegral $ BSL.length (txFullBytes tx) - extraSize txbody

-- | Minimum fee calculation
minfee :: forall crypto. (Crypto crypto) => PParams -> Tx crypto -> Coin
Expand Down
Expand Up @@ -17,9 +17,9 @@ module Shelley.Spec.Ledger.OCert
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..), toCBOR)
import Cardano.Prelude (NoUnexpectedThunks (..))
import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.KES as KES
import Cardano.Prelude (NoUnexpectedThunks (..))
import Control.Monad.Trans.Reader (asks)
import Data.Functor ((<&>))
import Data.Map.Strict (Map)
Expand Down Expand Up @@ -98,8 +98,8 @@ instance
<> toCBOR (ocertN ocert)
<> toCBOR (ocertKESPeriod ocert)
<> encodeSignedDSIGN (ocertSigma ocert)
encodedGroupSizeExpr size proxy =
KES.encodedVerKeyKESSizeExpr (ocertVkHot <$> proxy)
encodedGroupSizeExpr size proxy =
KES.encodedVerKeyKESSizeExpr (ocertVkHot <$> proxy)
+ encodedSizeExpr size ((toWord . ocertN) <$> proxy)
+ encodedSizeExpr size ((\(KESPeriod p) -> p) . ocertKESPeriod <$> proxy)
+ DSIGN.encodedSigDSIGNSizeExpr (((\(DSIGN.SignedDSIGN sig) -> sig) . ocertSigma) <$> proxy)
Expand Down
Expand Up @@ -157,7 +157,7 @@ instance NoUnexpectedThunks ProtVer
instance ToCBORGroup ProtVer where
toCBORGroup (ProtVer x y) = toCBOR x <> toCBOR y
encodedGroupSizeExpr size proxy =
encodedSizeExpr size ((\(ProtVer x _) -> toWord x) <$> proxy)
encodedSizeExpr size ((\(ProtVer x _) -> toWord x) <$> proxy)
+ encodedSizeExpr size ((\(ProtVer _ y) -> toWord y) <$> proxy)
where
toWord :: Natural -> Word
Expand Down
Expand Up @@ -15,7 +15,7 @@ module Shelley.Spec.Ledger.STS.Prtcl
PrtclEnv (..),
PrtclState (..),
PredicateFailure (..),
PrtlSeqFailure(..),
PrtlSeqFailure (..),
prtlSeqChecks,
)
where
Expand Down
Expand Up @@ -12,9 +12,9 @@ module Shelley.Spec.Ledger.Serialization
FromCBORGroup (..),
CBORGroup (..),
CborSeq (..),
unwrapCborStrictSeq,
decodeList,
decodeSeq,
decodeStrictSeq,
decodeSet,
decodeMap,
decodeMapContents,
Expand Down Expand Up @@ -49,9 +49,9 @@ import Cardano.Binary
DecoderError (..),
Encoding,
FromCBOR (..),
Size,
ToCBOR (..),
TokenType (TypeNull),
Size,
decodeBreakOr,
decodeListLenOrIndef,
decodeMapLenOrIndef,
Expand Down Expand Up @@ -100,19 +100,22 @@ import Network.Socket (HostAddress6)

class Typeable a => ToCBORGroup a where
toCBORGroup :: a -> Encoding
encodedGroupSizeExpr :: (forall x. ToCBOR x => Proxy x -> Size)
-> Proxy a -> Size
encodedGroupSizeExpr ::
(forall x. ToCBOR x => Proxy x -> Size) ->
Proxy a ->
Size

listLen :: a -> Word

listLen :: a -> Word
-- | an upper bound for 'listLen', used in 'Size' expressions.
listLenBound :: Proxy a -> Word

newtype CBORGroup a = CBORGroup { unCBORGroup :: a }
newtype CBORGroup a = CBORGroup {unCBORGroup :: a}

instance ToCBORGroup a => ToCBOR (CBORGroup a) where
toCBOR (CBORGroup x) = encodeListLen (listLen x) <> toCBORGroup x
encodedSizeExpr size proxy =
fromInteger (withWordSize (listLenBound proxy'))
fromInteger (withWordSize (listLenBound proxy'))
+ encodedGroupSizeExpr size proxy'
where
proxy' = unCBORGroup <$> proxy
Expand Down Expand Up @@ -179,8 +182,8 @@ instance FromCBOR a => FromCBOR (CborSeq a) where
decodeSeq :: Decoder s a -> Decoder s (Seq a)
decodeSeq decoder = Seq.fromList <$> decodeList decoder

unwrapCborStrictSeq :: CborSeq a -> StrictSeq a
unwrapCborStrictSeq = StrictSeq.toStrict . unwrapCborSeq
decodeStrictSeq :: Decoder s a -> Decoder s (StrictSeq a)
decodeStrictSeq decoder = StrictSeq.fromList <$> decodeList decoder

decodeSet :: Ord a => Decoder s a -> Decoder s (Set a)
decodeSet decoder = Set.fromList <$> decodeList decoder
Expand Down

0 comments on commit 92e4d6c

Please sign in to comment.