Skip to content

Commit

Permalink
change Ptr Ix to Word64
Browse files Browse the repository at this point in the history
  • Loading branch information
goolord committed Jun 8, 2021
1 parent 452ec4e commit 272db94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
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 . fromIntegral $ 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 @@ -407,30 +407,35 @@ putWord7s [] = pure ()
putWord7s [Word7 x] = B.putWord8 x
putWord7s (Word7 x : xs) = B.putWord8 (x .|. 0x80) >> putWord7s xs

-- limited to 8 bytes
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
getWord7s = go 0
where
go :: Int -> Get [Word7]
go 8 = pure []
go n = do
next <- B.getWord8
case next .&. 0x80 of
0x80 -> (:) (toWord7 next) <$> go (n + 1)
_ -> pure [Word7 next]

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

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

word7sToNat :: [Word7] -> Natural
word7sToNat = foldl' f 0
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
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 @@ -187,14 +186,14 @@ getWord7s = do
-- 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
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 @@ -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 @@ -198,7 +197,7 @@ genDCerts ::
Core.PParams era ->
DPState (Crypto era) ->
SlotNo ->
Natural ->
Ix ->
AccountState ->
Gen
( StrictSeq (DCert (Crypto era)),
Expand Down

0 comments on commit 272db94

Please sign in to comment.