Skip to content

Commit

Permalink
Merge pull request #2321 from input-output-hk/ptr-word64
Browse files Browse the repository at this point in the history
 change Ptr Ix to Word64
  • Loading branch information
Jared Corduan committed Jun 16, 2021
2 parents 8e8e8c8 + fc00acc commit 6474f68
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ module Shelley.Spec.Ledger.Address
getPtr,
getRewardAcnt,
getScriptHash,
getVariableLengthNat,
getVariableLengthWord64,
payCredIsScript,
putAddr,
putCredential,
putPtr,
putRewardAcnt,
putVariableLengthNat,
putVariableLengthWord64,
-- TODO: these should live somewhere else
natToWord7s,
word7sToNat,
word64ToWord7s,
word7sToWord64,
Word7 (..),
toWord7,
)
Expand Down Expand Up @@ -94,9 +94,9 @@ import Data.Maybe (fromMaybe)
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Numeric.Natural (Natural)
import Quiet
import Shelley.Spec.Ledger.Credential
( Credential (..),
Expand Down Expand Up @@ -385,16 +385,16 @@ isBootstrapRedeemer _ = False
putPtr :: Ptr -> Put
putPtr (Ptr slot txIx certIx) = do
putSlot slot
putVariableLengthNat txIx
putVariableLengthNat certIx
putVariableLengthWord64 txIx
putVariableLengthWord64 certIx
where
putSlot (SlotNo n) = putVariableLengthNat . fromIntegral $ n
putSlot (SlotNo n) = putVariableLengthWord64 n

getPtr :: Get Ptr
getPtr =
Ptr <$> (SlotNo . fromIntegral <$> getVariableLengthNat)
<*> getVariableLengthNat
<*> getVariableLengthNat
Ptr <$> (SlotNo <$> getVariableLengthWord64)
<*> getVariableLengthWord64
<*> getVariableLengthWord64

newtype Word7 = Word7 Word8
deriving (Eq, Show)
Expand All @@ -410,27 +410,32 @@ putWord7s (Word7 x : xs) = B.putWord8 (x .|. 0x80) >> putWord7s xs
getWord7s :: Get [Word7]
getWord7s = do
next <- B.getWord8
case next .&. 0x80 of
0x80 -> (:) (toWord7 next) <$> getWord7s
_ -> pure [Word7 next]

natToWord7s :: Natural -> [Word7]
natToWord7s = reverse . go
-- is the high bit set?
if testBit next 7
then -- if so, grab more words
(:) (toWord7 next) <$> getWord7s
else -- otherwise, this is the last one
pure [Word7 next]

word64ToWord7s :: Word64 -> [Word7]
word64ToWord7s = reverse . go
where
go :: Word64 -> [Word7]
go n
| n <= 0x7F = [Word7 . fromIntegral $ n]
| otherwise = (toWord7 . fromIntegral) n : go (shiftR n 7)
| n > 0x7F = (toWord7 . fromIntegral) n : go (shiftR n 7)
| otherwise = [Word7 . fromIntegral $ n]

putVariableLengthNat :: Natural -> Put
putVariableLengthNat = putWord7s . natToWord7s
putVariableLengthWord64 :: Word64 -> Put
putVariableLengthWord64 = putWord7s . word64ToWord7s

word7sToNat :: [Word7] -> Natural
word7sToNat = foldl' f 0
-- invariant: length [Word7] < 8
word7sToWord64 :: [Word7] -> Word64
word7sToWord64 = foldl' f 0
where
f n (Word7 r) = shiftL n 7 .|. (fromIntegral r)

getVariableLengthNat :: Get Natural
getVariableLengthNat = word7sToNat <$> getWord7s
getVariableLengthWord64 :: Get Word64
getVariableLengthWord64 = word7sToWord64 <$> getWord7s

decoderFromGet :: Text -> Get a -> Decoder s a
decoderFromGet name get = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ import Data.ByteString.Short as SBS
import Data.ByteString.Short.Internal (ShortByteString (SBS))
import Data.Maybe (fromMaybe)
import qualified Data.Primitive.ByteArray as BA
import Data.Word (Word8)
import Numeric.Natural (Natural)
import Shelley.Spec.Ledger.Address (Addr (..), BootstrapAddress (..), Word7 (..), byron, isEnterpriseAddr, notBaseAddr, payCredIsScript, serialiseAddr, stakeCredIsScript, toWord7, word7sToNat)
import Data.Word (Word64, Word8)
import Shelley.Spec.Ledger.Address (Addr (..), BootstrapAddress (..), Word7 (..), byron, isEnterpriseAddr, notBaseAddr, payCredIsScript, serialiseAddr, stakeCredIsScript, toWord7, word7sToWord64)
import Shelley.Spec.Ledger.Credential
( Credential (KeyHashObj, ScriptHashObj),
PaymentCredential,
Expand Down Expand Up @@ -180,21 +179,21 @@ skip n = GetShort $ \i sbs ->
getWord7s :: GetShort [Word7]
getWord7s = do
next <- getWord
case next .&. 0x80 of -- 0x80 ~ 0b10000000
-- is the high bit set?
-- if so, grab more words
0x80 -> (:) (toWord7 next) <$> getWord7s
-- otherwise, this is the last one
_ -> pure [Word7 next]
if testBit next 7
then -- if so, grab more words
(:) (toWord7 next) <$> getWord7s
else -- otherwise, this is the last one
pure [Word7 next]

getVariableLengthNat :: GetShort Natural
getVariableLengthNat = word7sToNat <$> getWord7s
getVariableLengthWord64 :: GetShort Word64
getVariableLengthWord64 = word7sToWord64 <$> getWord7s

getPtr :: GetShort Ptr
getPtr =
Ptr <$> (SlotNo . fromIntegral <$> getVariableLengthNat)
<*> getVariableLengthNat
<*> getVariableLengthNat
Ptr <$> (SlotNo <$> getVariableLengthWord64)
<*> getVariableLengthWord64
<*> getVariableLengthWord64

getKeyHash :: CC.Crypto crypto => GetShort (Credential kr crypto)
getKeyHash = KeyHashObj . KeyHash <$> getHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ import Data.Aeson (FromJSON (..), FromJSONKey, ToJSON (..), ToJSONKey, (.:), (.=
import qualified Data.Aeson as Aeson
import Data.Foldable (asum)
import Data.Typeable (Typeable)
import Data.Word (Word8)
import Data.Word (Word64, Word8)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Numeric.Natural (Natural)
import Quiet
import Shelley.Spec.Ledger.Orphans ()
import Shelley.Spec.Ledger.Scripts (ScriptHash)
Expand Down Expand Up @@ -100,7 +99,7 @@ data StakeReference crypto

instance NoThunks (StakeReference crypto)

type Ix = Natural
type Ix = Word64

-- | Pointer to a slot, transaction index and index in certificate list.
data Ptr
Expand Down Expand Up @@ -133,8 +132,8 @@ instance
instance ToCBORGroup Ptr where
toCBORGroup (Ptr sl txIx certIx) =
toCBOR sl
<> toCBOR (fromInteger (toInteger txIx) :: Word)
<> toCBOR (fromInteger (toInteger certIx) :: Word)
<> toCBOR txIx
<> toCBOR certIx
encodedGroupSizeExpr size_ proxy =
encodedSizeExpr size_ (getSlotNo <$> proxy)
+ encodedSizeExpr size_ (getIx1 <$> proxy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import Data.Sequence.Strict (StrictSeq)
import qualified Data.Sequence.Strict as StrictSeq
import GHC.Generics (Generic)
import GHC.Stack (HasCallStack)
import Numeric.Natural (Natural)
import Shelley.Spec.Ledger.API
( AccountState,
DCert,
Expand Down Expand Up @@ -199,7 +198,7 @@ genDCerts ::
Core.PParams era ->
DPState (Crypto era) ->
SlotNo ->
Natural ->
Ix ->
AccountState ->
Gen
( StrictSeq (DCert (Crypto era)),
Expand Down

0 comments on commit 6474f68

Please sign in to comment.