Skip to content

Commit

Permalink
Make pointers normalized upon deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Jan 11, 2025
1 parent 622bde1 commit d83dc54
Show file tree
Hide file tree
Showing 27 changed files with 125 additions and 176 deletions.
1 change: 0 additions & 1 deletion eras/conway/impl/cardano-ledger-conway.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ test-suite tests
cardano-ledger-alonzo,
cardano-ledger-alonzo:testlib,
cardano-ledger-babbage,
cardano-ledger-binary,
cardano-ledger-binary:testlib,
cardano-ledger-conway,
cardano-ledger-core,
Expand Down
3 changes: 1 addition & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

module Cardano.Ledger.Conway.TxOut () where

import Cardano.Ledger.Address (addrPtrNormalize)
import Cardano.Ledger.Babbage.Core
import Cardano.Ledger.Babbage.TxOut (
BabbageTxOut (..),
Expand All @@ -37,7 +36,7 @@ instance EraTxOut ConwayEra where
mkBasicTxOut addr vl = BabbageTxOut addr vl NoDatum SNothing

upgradeTxOut (BabbageTxOut addr value d s) =
BabbageTxOut (addrPtrNormalize addr) value (translateDatum d) (upgradeScript <$> s)
BabbageTxOut addr value (translateDatum d) (upgradeScript <$> s)

addrEitherTxOutL = addrEitherBabbageTxOutL
{-# INLINE addrEitherTxOutL #-}
Expand Down
21 changes: 0 additions & 21 deletions eras/conway/impl/test/Test/Cardano/Ledger/Conway/BinarySpec.hs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Conway.BinarySpec (spec) where

import Cardano.Ledger.Babbage
import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Binary
import Cardano.Ledger.Conway
import Cardano.Ledger.Conway.Core
import Cardano.Ledger.Conway.Genesis
import Cardano.Ledger.Conway.Governance
import Cardano.Ledger.Credential
import Cardano.Ledger.Shelley.LedgerState
import Data.Default (def)
import qualified Data.Map.Strict as Map
import Lens.Micro
import Test.Cardano.Ledger.Binary.RoundTrip
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Conway.Arbitrary ()
Expand All @@ -38,15 +29,3 @@ spec = do
roundTripConwayCommonSpec @ConwayEra
-- ConwayGenesis only makes sense in Conway era
roundTripEraSpec @ConwayEra @ConwayGenesis
describe "Regression" $ do
prop "Drop Ptrs from Incrementasl Stake" $ \(ls :: LedgerState BabbageEra) conwayGenesis slotNo testCoin -> do
let
badPtr = Ptr slotNo (TxIx maxBound) (CertIx maxBound)
lsBabbage :: LedgerState BabbageEra
lsBabbage = ls & lsUTxOStateL . utxosStakeDistrL . ptrMapL <>~ Map.singleton badPtr testCoin
lsConway :: LedgerState ConwayEra
lsConway = translateEra' conwayGenesis lsBabbage
v = eraProtVerLow @ConwayEra
expectNoBadPtr :: LedgerState ConwayEra -> LedgerState ConwayEra -> Expectation
expectNoBadPtr x y = x `shouldBe` (y & lsUTxOStateL . utxosStakeDistrL . ptrMapL .~ mempty)
embedTripExpectation v v (mkTrip encCBOR decNoShareCBOR) expectNoBadPtr lsConway
18 changes: 11 additions & 7 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Delegs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ import Cardano.Ledger.BaseTypes (
EpochNo,
Network,
ShelleyBase,
TxIx,
TxIx (..),
invalidKey,
mkCertIxPartial,
networkId,
)
import Cardano.Ledger.Binary (
Expand All @@ -45,7 +44,7 @@ import Cardano.Ledger.Binary (
)
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential, Ptr (..))
import Cardano.Ledger.Credential (Credential, mkPtrNormalized)
import Cardano.Ledger.Rules.ValidationMode (Test)
import Cardano.Ledger.Shelley.Era (ShelleyDELEGS, ShelleyEra)
import Cardano.Ledger.Shelley.LedgerState (
Expand All @@ -71,7 +70,7 @@ import Cardano.Ledger.Shelley.TxBody (
Withdrawals (..),
)
import Cardano.Ledger.Shelley.TxCert (pattern DelegStakeTxCert)
import Cardano.Ledger.Slot (SlotNo)
import Cardano.Ledger.Slot (SlotNo (..))
import Cardano.Ledger.UMap (UMElem (..), UMap (..), UView (..), fromCompact)
import qualified Cardano.Ledger.UMap as UM
import Control.DeepSeq
Expand All @@ -92,7 +91,7 @@ import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Sequence (Seq (..))
import Data.Typeable (Typeable)
import Data.Word (Word8)
import Data.Word (Word16, Word64, Word8)
import GHC.Generics (Generic)
import Lens.Micro ((^.))
import NoThunks.Class (NoThunks (..))
Expand Down Expand Up @@ -237,7 +236,8 @@ delegsTransition ::
) =>
TransitionRule (ShelleyDELEGS era)
delegsTransition = do
TRC (env@(DelegsEnv slot epochNo txIx pp tx acnt), certState, certificates) <- judgmentContext
TRC (env@(DelegsEnv slot@(SlotNo slot64) epochNo (TxIx txIx16) pp tx acnt), certState, certificates) <-
judgmentContext
network <- liftSTS $ asks networkId

case certificates of
Expand All @@ -257,7 +257,11 @@ delegsTransition = do
_ -> pure ()
-- It is impossible to have 65535 number of certificates in a
-- transaction, therefore partial function is justified.
let ptr = Ptr slot txIx (mkCertIxPartial $ toInteger $ length gamma)
let ptr =
mkPtrNormalized
slot64
(fromIntegral @Word16 @Word64 txIx16)
(fromIntegral @Int @Word64 $ length gamma)
trans @(EraRule "DELPL" era) $
TRC (DelplEnv slot epochNo ptr pp acnt, certState', txCert)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import Cardano.Ledger.PoolParams (
PoolMetadata (..),
PoolParams (..),
)
import Cardano.Ledger.Slot (SlotNo (..))
import Cardano.Protocol.Crypto (hashVerKeyVRF)
import Cardano.Protocol.TPraos.OCert (KESPeriod (..))
import qualified Data.ByteString.Char8 as BS (pack)
Expand Down Expand Up @@ -120,7 +119,7 @@ aliceSHK = (KeyHashObj . hashKey . vKey) aliceStake

-- | Alice's base address
alicePtrAddr :: Addr
alicePtrAddr = Addr Testnet alicePHK (StakeRefPtr $ Ptr (SlotNo 10) minBound minBound)
alicePtrAddr = Addr Testnet alicePHK (StakeRefPtr $ Ptr 10 minBound minBound)

-- | Alice's stake pool parameters
alicePoolParams :: PoolParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Test.Cardano.Ledger.Shelley.Generator.Trace.TxCert (
)
where

import Cardano.Ledger.BaseTypes (CertIx, Globals, ShelleyBase, TxIx)
import Cardano.Ledger.BaseTypes (CertIx, Globals, ShelleyBase, SlotNo (..), TxIx)
import Cardano.Ledger.CertState (
CertState (..),
lookupDepositDState,
Expand All @@ -28,6 +28,7 @@ import Cardano.Ledger.CertState (
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Credential (SlotNo32 (..))
import Cardano.Ledger.Keys (HasKeyRole (coerceKeyRole), asWitness)
import Cardano.Ledger.Shelley.API (
AccountState,
Expand All @@ -36,7 +37,6 @@ import Cardano.Ledger.Shelley.API (
ShelleyDELPL,
)
import Cardano.Ledger.Shelley.Rules (ShelleyDelplEvent, ShelleyDelplPredFailure)
import Cardano.Ledger.Slot (SlotNo (..))
import Control.Monad.Trans.Reader (runReaderT)
import Control.State.Transition (
BaseM,
Expand Down Expand Up @@ -123,15 +123,15 @@ certsTransition ::
TransitionRule (CERTS era)
certsTransition = do
TRC
( (slot, txIx, pp, acnt)
( (slot@(SlotNo slot64), txIx, pp, acnt)
, (dpState, nextCertIx)
, c
) <-
judgmentContext

case c of
Just (cert, _wits) -> do
let ptr = Ptr slot txIx nextCertIx
let ptr = Ptr (SlotNo32 (fromIntegral slot64)) txIx nextCertIx
let epoch = epochFromSlotNo slot
dpState' <-
trans @(Core.EraRule "DELPL" era) $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ module Test.Cardano.Ledger.Shelley.Rules.TestChain (
shortChainTrace,
) where

import Cardano.Ledger.BaseTypes (Globals)
import Cardano.Ledger.BaseTypes (Globals, SlotNo (..))
import Cardano.Ledger.Block (
Block (..),
bheader,
neededTxInsForBlock,
)
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Ptr (..))
import Cardano.Ledger.Credential (Ptr (..), SlotNo32 (..))
import Cardano.Ledger.Shelley.API (ApplyBlock, CertState (..), ShelleyDELEG)
import Cardano.Ledger.Shelley.Core
import Cardano.Ledger.Shelley.LedgerState (
Expand Down Expand Up @@ -223,10 +223,10 @@ delegTraceFromBlock chainSt block =
certs = concatMap (reverse . toList . view certsTxBodyL . view bodyTxL)
blockCerts = filter delegCert (certs txs)
delegEnv =
let (LedgerEnv s _ txIx pp reserves _) = ledgerEnv
let (LedgerEnv slot@(SlotNo slot64) _ txIx pp reserves _) = ledgerEnv
dummyCertIx = minBound
ptr = Ptr s txIx dummyCertIx
in DelegEnv s (epochFromSlotNo s) ptr reserves pp
ptr = Ptr (SlotNo32 (fromIntegral slot64)) txIx dummyCertIx
in DelegEnv slot (epochFromSlotNo slot) ptr reserves pp
delegSt0 =
certDState (lsCertState ledgerSt0)
delegCert (RegTxCert _) = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Cardano.Ledger.Credential (
import Cardano.Ledger.Shelley.LedgerState (IncrementalStake (..))
import Cardano.Ledger.TxIn (TxId (..), TxIn (..))
import Cardano.Ledger.UTxO (UTxO (..))
import Cardano.Slotting.Slot (SlotNo (..))
import qualified Data.Map.Strict as Map

-- ====================================================
Expand Down Expand Up @@ -85,7 +84,7 @@ instance Terse (Credential r) where
terse (KeyHashObj (KeyHash hash)) = "Key " ++ show hash

instance Terse Ptr where
terse (Ptr (SlotNo n) i j) = "Ptr " ++ show n ++ " " ++ show i ++ " " ++ show j
terse (Ptr n i j) = "Ptr " ++ show n ++ " " ++ show i ++ " " ++ show j

instance Terse TxId where
terse (TxId safehash) = show (extractHash safehash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
import Cardano.Ledger.BaseTypes (Mismatch (..), Nonce, StrictMaybe (..), mkCertIxPartial)
import Cardano.Ledger.Block (Block, bheader)
import Cardano.Ledger.Coin (Coin (..), toDeltaCoin)
import Cardano.Ledger.Credential (Ptr (..))
import Cardano.Ledger.Credential (Ptr (..), SlotNo32 (..))
import Cardano.Ledger.EpochBoundary (emptySnapShot)
import Cardano.Ledger.Keys (asWitness)
import Cardano.Ledger.Shelley (ShelleyEra)
Expand Down Expand Up @@ -179,7 +179,7 @@ expectedStEx1' txwits pot =
. C.newLab (blockEx1' txwits pot)
. C.feesAndDeposits ppEx feeTx1 [Cast.aliceSHK] []
. C.newUTxO (txbodyEx1 pot)
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 1))
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 1))
. C.mir Cast.aliceSHK pot aliceMIRCoin
$ initStMIR (Coin 1000)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ where

import Cardano.Ledger.BaseTypes (Mismatch (..), ProtVer (..), natVersion)
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..))
import Cardano.Ledger.Credential (SlotNo32 (..))
import Cardano.Ledger.Hashes (GenDelegs (..))
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.API (
Expand Down Expand Up @@ -49,12 +50,13 @@ env pv acnt =
DelegEnv
{ slotNo = slot
, deCurEpochNo = epochFromSlotNo slot
, ptr_ = Ptr slot minBound minBound
, ptr_ = Ptr slot32 minBound minBound
, acnt_ = acnt
, ppDE = emptyPParams & ppProtocolVersionL .~ pv
}
where
slot = SlotNo 50
slot32 = SlotNo32 50

shelleyPV :: ProtVer
shelleyPV = ProtVer (natVersion @2) 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import Cardano.Ledger.Coin (
toDeltaCoin,
)
import Cardano.Ledger.Compactible
import Cardano.Ledger.Credential (Credential, Ptr (..))
import Cardano.Ledger.Credential (Credential, Ptr (..), SlotNo32 (..))
import qualified Cardano.Ledger.EpochBoundary as EB
import Cardano.Ledger.Keys (asWitness, coerceKeyRole)
import Cardano.Ledger.PoolDistr (
Expand Down Expand Up @@ -252,9 +252,9 @@ expectedStEx1 =
. C.newLab blockEx1
. C.feesAndDeposits ppEx feeTx1 [Cast.aliceSHK, Cast.bobSHK, Cast.carlSHK] [Cast.alicePoolParams]
. C.newUTxO txbodyEx1
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 0))
. C.newStakeCred Cast.bobSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 1))
. C.newStakeCred Cast.carlSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 2))
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 0))
. C.newStakeCred Cast.bobSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 1))
. C.newStakeCred Cast.carlSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 2))
. C.newPool Cast.alicePoolParams
. C.mir Cast.carlSHK ReservesMIR carlMIR
. C.mir Cast.dariaSHK ReservesMIR dariaMIR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Cardano.Ledger.Coin (
rationalToCoinViaFloor,
toDeltaCoin,
)
import Cardano.Ledger.Credential (Credential, Ptr (..))
import Cardano.Ledger.Credential (Credential, Ptr (..), SlotNo32 (..))
import qualified Cardano.Ledger.EpochBoundary as EB
import Cardano.Ledger.Keys (asWitness, coerceKeyRole)
import Cardano.Ledger.PoolDistr (
Expand Down Expand Up @@ -242,9 +242,9 @@ expectedStEx1 =
[Cast.aliceSHK, Cast.bobSHK, Cast.carlSHK]
[alicePoolParams', bobPoolParams']
. C.newUTxO txbodyEx1
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 0))
. C.newStakeCred Cast.bobSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 1))
. C.newStakeCred Cast.carlSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 2))
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 0))
. C.newStakeCred Cast.bobSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 1))
. C.newStakeCred Cast.carlSHK (Ptr (SlotNo32 10) minBound (mkCertIxPartial 2))
. C.newPool alicePoolParams'
. C.newPool bobPoolParams'
. C.delegation Cast.aliceSHK (ppId alicePoolParams')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import Cardano.Crypto.Hash (HashAlgorithm (..), hashFromBytes, hashFromTextAsHex
import Cardano.Crypto.Hash.Blake2b (Blake2b_224)
import Cardano.Ledger.Address
import Cardano.Ledger.BaseTypes (Network (..), mkCertIxPartial, mkTxIxPartial)
import Cardano.Ledger.Credential (Credential (..), Ptr (..), StakeReference (..))
import Cardano.Ledger.Credential (Credential (..), Ptr (..), SlotNo32 (..), StakeReference (..))
import Cardano.Ledger.Shelley.Core
import Cardano.Ledger.Slot (SlotNo (..))
import qualified Data.Binary as B
import qualified Data.Binary.Put as B
import qualified Data.ByteString as BS
Expand Down Expand Up @@ -124,7 +123,7 @@ goldenTests_MockCrypto =
ptrHex :: IsString s => s
ptrHex = "81000203"
ptr :: Ptr
ptr = Ptr (SlotNo 128) (mkTxIxPartial 2) (mkCertIxPartial 3)
ptr = Ptr (SlotNo32 128) (mkTxIxPartial 2) (mkCertIxPartial 3)

goldenTests_ShelleyCrypto :: TestTree
goldenTests_ShelleyCrypto =
Expand Down Expand Up @@ -191,7 +190,7 @@ goldenTests_ShelleyCrypto =
stakeKey :: Credential 'Staking
stakeKey = keyBlake2b224 $ B16.encode "1c2c3c4c5c6c7c8c"
ptr :: Ptr
ptr = Ptr (SlotNo 128) (mkTxIxPartial 2) (mkCertIxPartial 3)
ptr = Ptr (SlotNo32 128) (mkTxIxPartial 2) (mkCertIxPartial 3)
-- 32-byte verification key is expected, vk, ie., public key without chain code.
-- The verification key undergoes Blake2b_224 hashing
-- and should be 28-byte in the aftermath
Expand Down
3 changes: 2 additions & 1 deletion libs/cardano-ledger-api/src/Cardano/Ledger/Api/Tx/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ decodeAddrShort sbs = evalStateT (decodeAddrStateT sbs) 0
data DecAddr
= -- | Address was decoded with no problems
DecAddr Addr
| -- | Address was decoded, but it contains an invalid `Cardano.Ledger.Credential.Ptr`
| -- | Address was decoded, but it contains an invalid `Cardano.Ledger.Credential.Ptr`, which
-- means that address will be decoded with Ptr that has all values clamped to zero.
DecAddrBadPtr Addr
| -- | Address was decoded, but not all of input was consumed
DecAddrUnconsumed
Expand Down
Loading

0 comments on commit d83dc54

Please sign in to comment.