Skip to content

Commit

Permalink
better address types for symmetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Corduan committed Apr 14, 2020
1 parent d7fd4f7 commit b48002c
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 134 deletions.
Expand Up @@ -19,7 +19,8 @@ import Shelley.Spec.Ledger.Crypto
import Shelley.Spec.Ledger.Keys (KeyDiscriminator (..), KeyPair, hashKey, vKey)
import Shelley.Spec.Ledger.Scripts
import Shelley.Spec.Ledger.Tx (hashScript)
import Shelley.Spec.Ledger.TxData (Addr (..), Credential (..), RewardAcnt (..))
import Shelley.Spec.Ledger.TxData (Addr (..), Credential (..), RewardAcnt (..),
StakeReference (..))

mkVKeyRwdAcnt
:: Crypto crypto
Expand All @@ -37,7 +38,7 @@ toAddr
:: Crypto crypto
=> (KeyPair 'Regular crypto, KeyPair 'Regular crypto)
-> Addr crypto
toAddr (payKey, stakeKey) = AddrBase (toCred payKey) (toCred stakeKey)
toAddr (payKey, stakeKey) = Addr (toCred payKey) (StakeRefBase $ toCred stakeKey)

toCred
:: Crypto crypto
Expand All @@ -53,7 +54,7 @@ scriptToCred = ScriptHashObj . hashScript
-- | Create a base address from a pair of multi-sig scripts (pay and stake)
scriptsToAddr :: Crypto crypto => (MultiSig crypto, MultiSig crypto) -> Addr crypto
scriptsToAddr (payScript, stakeScript) =
AddrBase (scriptToCred payScript) (scriptToCred stakeScript)
Addr (scriptToCred payScript) (StakeRefBase $ scriptToCred stakeScript)

-- | Serialise an address to the external format.
--
Expand All @@ -75,4 +76,3 @@ serialiseAddr = serialize'
--
deserialiseAddr :: Crypto crypto => ByteString -> Maybe (Addr crypto)
deserialiseAddr = either (const Nothing) id . decodeFull'

Expand Up @@ -37,7 +37,7 @@ import Shelley.Spec.Ledger.Keys (KeyHash)
import Shelley.Spec.Ledger.PParams (PParams, _a0, _nOpt)
import Shelley.Spec.Ledger.Slot (SlotNo, (-*))
import Shelley.Spec.Ledger.TxData (Addr (..), Credential, PoolParams, Ptr, RewardAcnt,
TxOut (..), getRwdCred)
StakeReference (..), TxOut (..), getRwdCred)
import Shelley.Spec.Ledger.UTxO (UTxO (..))

import Cardano.Binary (FromCBOR (..), ToCBOR (..), encodeListLen, enforceSize)
Expand Down Expand Up @@ -73,8 +73,8 @@ newtype Stake crypto

-- | Extract hash of staking key from base address.
getStakeHK :: Addr crypto -> Maybe (Credential crypto)
getStakeHK (AddrBase _ hk) = Just hk
getStakeHK _ = Nothing
getStakeHK (Addr _ (StakeRefBase hk)) = Just hk
getStakeHK _ = Nothing

aggregateOuts :: UTxO crypto -> Map (Addr crypto) Coin
aggregateOuts (UTxO u) =
Expand All @@ -95,7 +95,7 @@ baseStake vals =

-- | Extract pointer from pointer address.
getStakePtr :: Addr crypto -> Maybe Ptr
getStakePtr (AddrPtr _ ptr) = Just ptr
getStakePtr (Addr _ (StakeRefPtr ptr)) = Just ptr
getStakePtr _ = Nothing

-- | Calculate stake of pointer addresses in TxOut set.
Expand Down
Expand Up @@ -813,8 +813,8 @@ witsVKeyNeeded utxo' tx@(Tx txbody _ _ _) _genDelegs =
inputAuthors = undiscriminateKeyHash `Set.map` Set.foldr insertHK Set.empty (_inputs txbody)
insertHK txin hkeys =
case txinLookup txin utxo' of
Just (TxOut (AddrBase (KeyHashObj pay) _) _) -> Set.insert pay hkeys
_ -> hkeys
Just (TxOut (Addr (KeyHashObj pay) _) _) -> Set.insert pay hkeys
_ -> hkeys

wdrlAuthors =
Set.fromList $ extractKeyHash $ map getRwdCred (Map.keys (unWdrl $ _wdrls txbody))
Expand Down
Expand Up @@ -110,12 +110,20 @@ instance Eq (GenesisCredential crypto)

instance NoUnexpectedThunks (Credential crypto)

type PaymentCredential crypto = Credential crypto
type StakeCredential crypto = Credential crypto

data StakeReference crypto
= StakeRefBase !(StakeCredential crypto)
| StakeRefPtr !Ptr
| StakeRefNull
deriving (Show, Eq, Ord, Generic)

instance NoUnexpectedThunks (StakeReference crypto)

-- |An address for UTxO.
data Addr crypto
= AddrBase !(Credential crypto) !(Credential crypto)
| AddrEnterprise !(Credential crypto)
| AddrPtr !(Credential crypto) !Ptr
= Addr !(PaymentCredential crypto) !(StakeReference crypto)
| AddrBootstrap !(KeyHash crypto)
deriving (Show, Eq, Ord, Generic)
deriving (ToCBOR, FromCBOR) via (CBORGroup (Addr crypto))
Expand Down Expand Up @@ -495,26 +503,26 @@ instance
(Typeable crypto, Crypto crypto)
=> ToCBORGroup (Addr crypto)
where
listLen (AddrBase _ _) = 3
listLen (AddrPtr _ pointer) = 2 + listLen pointer
listLen (AddrEnterprise _) = 2
listLen (Addr _ (StakeRefBase _)) = 3
listLen (Addr _ (StakeRefPtr p)) = 2 + listLen p
listLen (Addr _ (StakeRefNull)) = 2
listLen (AddrBootstrap _) = 2

toCBORGroup (AddrBase (KeyHashObj a) (KeyHashObj b)) =
toCBORGroup (Addr (KeyHashObj a) (StakeRefBase (KeyHashObj b))) =
toCBOR (0 :: Word8) <> toCBOR a <> toCBOR b
toCBORGroup (AddrBase (KeyHashObj a) (ScriptHashObj b)) =
toCBORGroup (Addr (KeyHashObj a) (StakeRefBase (ScriptHashObj b))) =
toCBOR (1 :: Word8) <> toCBOR a <> toCBOR b
toCBORGroup (AddrBase (ScriptHashObj a) (KeyHashObj b)) =
toCBORGroup (Addr (ScriptHashObj a) (StakeRefBase (KeyHashObj b))) =
toCBOR (2 :: Word8) <> toCBOR a <> toCBOR b
toCBORGroup (AddrBase (ScriptHashObj a) (ScriptHashObj b)) =
toCBORGroup (Addr (ScriptHashObj a) (StakeRefBase (ScriptHashObj b))) =
toCBOR (3 :: Word8) <> toCBOR a <> toCBOR b
toCBORGroup (AddrPtr (KeyHashObj a) pointer) =
toCBORGroup (Addr (KeyHashObj a) (StakeRefPtr pointer)) =
toCBOR (4 :: Word8) <> toCBOR a <> toCBORGroup pointer
toCBORGroup (AddrPtr (ScriptHashObj a) pointer) =
toCBORGroup (Addr (ScriptHashObj a) (StakeRefPtr pointer)) =
toCBOR (5 :: Word8) <> toCBOR a <> toCBORGroup pointer
toCBORGroup (AddrEnterprise (KeyHashObj a)) =
toCBORGroup (Addr (KeyHashObj a) StakeRefNull) =
toCBOR (6 :: Word8) <> toCBOR a
toCBORGroup (AddrEnterprise (ScriptHashObj a)) =
toCBORGroup (Addr (ScriptHashObj a) StakeRefNull) =
toCBOR (7 :: Word8) <> toCBOR a
toCBORGroup (AddrBootstrap a) =
toCBOR (8 :: Word8) <> toCBOR a
Expand All @@ -526,37 +534,37 @@ instance (Crypto crypto) =>
0 -> do
a <- fromCBOR
b <- fromCBOR
pure $ AddrBase (KeyHashObj a) (KeyHashObj b)
pure $ Addr (KeyHashObj a) (StakeRefBase (KeyHashObj b))
1 -> do
a <- fromCBOR
b <- fromCBOR
pure $ AddrBase (KeyHashObj a) (ScriptHashObj b)
pure $ Addr (KeyHashObj a) (StakeRefBase (ScriptHashObj b))
2 -> do
a <- fromCBOR
b <- fromCBOR
pure $ AddrBase (ScriptHashObj a) (KeyHashObj b)
pure $ Addr (ScriptHashObj a) (StakeRefBase (KeyHashObj b))
3 -> do
a <- fromCBOR
b <- fromCBOR
pure $ AddrBase (ScriptHashObj a) (ScriptHashObj b)
pure $ Addr (ScriptHashObj a) (StakeRefBase (ScriptHashObj b))
4 -> do
a <- fromCBOR
x <- fromCBOR
y <- fromCBOR
z <- fromCBOR
pure $ AddrPtr (KeyHashObj a) (Ptr x y z)
pure $ Addr (KeyHashObj a) (StakeRefPtr (Ptr x y z))
5 -> do
a <- fromCBOR
x <- fromCBOR
y <- fromCBOR
z <- fromCBOR
pure $ AddrPtr (ScriptHashObj a) (Ptr x y z)
pure $ Addr (ScriptHashObj a) (StakeRefPtr (Ptr x y z))
6 -> do
a <- fromCBOR
pure $ AddrEnterprise (KeyHashObj a)
pure $ Addr (KeyHashObj a) StakeRefNull
7 -> do
a <- fromCBOR
pure $ AddrEnterprise (ScriptHashObj a)
pure $ Addr (ScriptHashObj a) StakeRefNull
8 -> do
a <- fromCBOR
pure $ AddrBootstrap (KeyHash a)
Expand Down
Expand Up @@ -195,10 +195,8 @@ txup (Tx txbody _ _ _) = strictMaybeToMaybe (_txUpdate txbody)

-- | Extract script hash from value address with script.
getScriptHash :: Addr crypto -> Maybe (ScriptHash crypto)
getScriptHash (AddrBase (ScriptHashObj hs) _) = Just hs
getScriptHash (AddrPtr (ScriptHashObj hs) _) = Just hs
getScriptHash (AddrEnterprise (ScriptHashObj hs)) = Just hs
getScriptHash _ = Nothing
getScriptHash (Addr (ScriptHashObj hs) _) = Just hs
getScriptHash _ = Nothing

scriptStakeCred
:: DCert crypto
Expand Down Expand Up @@ -243,7 +241,5 @@ txinsScript txInps (UTxO u) =
txInps `Set.intersection`
Map.keysSet (Map.filter (\(TxOut a _) ->
case a of
AddrBase (ScriptHashObj _) _ -> True
AddrEnterprise (ScriptHashObj _) -> True
AddrPtr (ScriptHashObj _) _ -> True
Addr (ScriptHashObj _) _ -> True
_ -> False) u)
Expand Up @@ -10,8 +10,8 @@ import Cardano.Crypto.Hash (ShortHash)
import Cardano.Crypto.KES (MockKES)
import Test.Cardano.Crypto.VRF.Fake (FakeVRF)

import Shelley.Spec.Ledger.Crypto
import qualified Shelley.Spec.Ledger.BlockChain as BlockChain
import Shelley.Spec.Ledger.Crypto
import qualified Shelley.Spec.Ledger.Delegation.Certificates as Delegation.Certificates
import qualified Shelley.Spec.Ledger.EpochBoundary as EpochBoundary
import qualified Shelley.Spec.Ledger.Keys as Keys
Expand Down Expand Up @@ -95,6 +95,8 @@ type PState = LedgerState.PState ConcreteCrypto

type DPState = LedgerState.DPState ConcreteCrypto

type StakeReference = TxData.StakeReference ConcreteCrypto

type Addr = TxData.Addr ConcreteCrypto

type Tx = Tx.Tx ConcreteCrypto
Expand Down
Expand Up @@ -152,10 +152,10 @@ import Shelley.Spec.Ledger.STS.Ledgers (pattern LedgerFailure)
import Shelley.Spec.Ledger.STS.Utxow (pattern MIRImpossibleInDecentralizedNetUTXOW,
pattern MIRInsufficientGenesisSigsUTXOW)
import Shelley.Spec.Ledger.Tx (pattern Tx)
import Shelley.Spec.Ledger.TxData (pattern AddrPtr, pattern DCertDeleg,
pattern DCertGenesis, pattern DCertMir, pattern DCertPool, pattern Delegation,
pattern KeyHashObj, PoolMetaData (..), pattern PoolParams, Ptr (..),
pattern RewardAcnt, pattern StakeCreds, pattern StakePools, pattern TxBody,
import Shelley.Spec.Ledger.TxData (pattern Addr, pattern DCertDeleg, pattern DCertGenesis,
pattern DCertMir, pattern DCertPool, pattern Delegation, pattern KeyHashObj,
PoolMetaData (..), pattern PoolParams, Ptr (..), pattern RewardAcnt,
pattern StakeCreds, pattern StakePools, pattern StakeRefPtr, pattern TxBody,
pattern TxIn, pattern TxOut, Url (..), Wdrl (..), addStakeCreds, _poolCost,
_poolMD, _poolMDHash, _poolMDUrl, _poolMargin, _poolOwners, _poolPledge,
_poolPubKey, _poolRAcnt, _poolRelays, _poolVrf)
Expand Down Expand Up @@ -513,7 +513,7 @@ txEx2A = Tx

-- | Pointer address to address of Alice address.
alicePtrAddr :: Addr
alicePtrAddr = AddrPtr (KeyHashObj . hashKey $ vKey alicePay) (Ptr (SlotNo 10) 0 0)
alicePtrAddr = Addr (KeyHashObj . hashKey $ vKey alicePay) (StakeRefPtr $ Ptr (SlotNo 10) 0 0)

utxostEx2A :: UTxOState
utxostEx2A = UTxOState utxoEx2A (Coin 0) (Coin 0) emptyPPPUpdates
Expand Down
Expand Up @@ -32,9 +32,10 @@ import Shelley.Spec.Ledger.Scripts (pattern RequireAllOf, pattern Requ
import Shelley.Spec.Ledger.Slot (SlotNo (..))
import Shelley.Spec.Ledger.STS.Utxo (UtxoEnv (..))
import Shelley.Spec.Ledger.Tx (pattern Tx, hashScript, _body)
import Shelley.Spec.Ledger.TxData (pattern AddrBase, pattern KeyHashObj,
pattern ScriptHashObj, pattern StakeCreds, pattern StakePools, pattern TxBody,
pattern TxIn, pattern TxOut, pattern Wdrl, unWdrl)
import Shelley.Spec.Ledger.TxData (pattern Addr, pattern KeyHashObj,
pattern ScriptHashObj, pattern StakeCreds, pattern StakePools,
pattern StakeRefBase, pattern TxBody, pattern TxIn, pattern TxOut,
pattern Wdrl, unWdrl)
import Shelley.Spec.Ledger.UTxO (makeWitnessesVKey, txid)

import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Addr, KeyPair, LedgerState, MultiSig,
Expand All @@ -47,7 +48,7 @@ import Test.Shelley.Spec.Ledger.Utils

-- Multi-signature scripts
singleKeyOnly :: Addr -> MultiSig
singleKeyOnly (AddrBase (KeyHashObj pk) _ ) = RequireSignature $ undiscriminateKeyHash pk
singleKeyOnly (Addr (KeyHashObj pk) _ ) = RequireSignature $ undiscriminateKeyHash pk
singleKeyOnly _ = error "use VKey address"

aliceOnly :: MultiSig
Expand Down Expand Up @@ -131,9 +132,9 @@ initialUTxOState aliceKeep msigs =
let addresses =
[(aliceAddr, aliceKeep) | aliceKeep > 0] ++
map (\(msig, c) ->
(AddrBase
(Addr
(ScriptHashObj $ hashScript msig)
(ScriptHashObj $ hashScript msig), c)) msigs
(StakeRefBase $ ScriptHashObj $ hashScript msig), c)) msigs
in
let tx = makeTx (initTxBody addresses)
[alicePay, bobPay]
Expand Down
Expand Up @@ -11,9 +11,8 @@ import qualified Data.Maybe as Maybe (fromJust)
import Data.String (fromString)
import qualified Shelley.Spec.Ledger.MetaData as MD

import Cardano.Binary (FromCBOR (..), ToCBOR (..), decodeFullDecoder,
serializeEncoding, serialize', toCBOR, DecoderError, Annotator,
decodeAnnotator)
import Cardano.Binary (Annotator, DecoderError, FromCBOR (..), ToCBOR (..),
decodeAnnotator, decodeFullDecoder, serialize', serializeEncoding, toCBOR)
import Cardano.Crypto.DSIGN (DSIGNAlgorithm (encodeVerKeyDSIGN), encodeSignedDSIGN)
import Cardano.Crypto.Hash (ShortHash, getHash)
import Cardano.Prelude (LByteString)
Expand Down Expand Up @@ -56,13 +55,14 @@ import Shelley.Spec.Ledger.Rewards (emptyNonMyopic)
import Shelley.Spec.Ledger.Serialization (FromCBORGroup (..), ToCBORGroup (..))
import Shelley.Spec.Ledger.Slot (BlockNo (..), EpochNo (..), SlotNo (..))
import Shelley.Spec.Ledger.Tx (Tx (..), hashScript)
import Shelley.Spec.Ledger.TxData (pattern AddrBase, pattern AddrEnterprise,
pattern AddrPtr, Credential (..), pattern DCertDeleg, pattern DCertGenesis,
pattern DCertMir, pattern DCertPool, pattern Delegation, PoolMetaData (..),
pattern PoolParams, Ptr (..), pattern RewardAcnt, pattern TxBody,
pattern TxIn, pattern TxOut, Url (..), Wdrl (..), WitVKey (..), _TxId,
_poolCost, _poolMD, _poolMDHash, _poolMDUrl, _poolMargin, _poolOwners,
_poolPledge, _poolPubKey, _poolRAcnt, _poolRelays, _poolVrf)
import Shelley.Spec.Ledger.TxData (pattern Addr, Credential (..), pattern DCertDeleg,
pattern DCertGenesis, pattern DCertMir, pattern DCertPool, pattern Delegation,
PoolMetaData (..), pattern PoolParams, Ptr (..), pattern RewardAcnt,
pattern StakeRefBase, pattern StakeRefNull, pattern StakeRefNull,
pattern StakeRefPtr, pattern TxBody, pattern TxIn, pattern TxOut, Url (..),
Wdrl (..), WitVKey (..), _TxId, _poolCost, _poolMD, _poolMDHash, _poolMDUrl,
_poolMargin, _poolOwners, _poolPledge, _poolPubKey, _poolRAcnt, _poolRelays,
_poolVrf)

import Shelley.Spec.Ledger.OCert (KESPeriod (..), pattern OCert)
import Shelley.Spec.Ledger.Scripts (pattern RequireSignature, pattern ScriptHash)
Expand Down Expand Up @@ -209,7 +209,7 @@ testKESKeys :: (SKeyES, VKeyES)
testKESKeys = mkKESKeyPair (0, 0, 0, 0, 3)

testAddrE :: Addr
testAddrE = AddrEnterprise (KeyHashObj testKeyHash1)
testAddrE = Addr (KeyHashObj testKeyHash1) StakeRefNull

testScript :: MultiSig
testScript = RequireSignature $ undiscriminateKeyHash testKeyHash1
Expand Down Expand Up @@ -308,59 +308,59 @@ serializationTests = testGroup "Serialization Tests"
(KeyHashObj testKeyHash1)
(T (TkListLen 2 . TkWord 0) <> S testKeyHash1)
, checkEncodingCBOR "base_address_key_key"
(AddrBase (KeyHashObj testKeyHash1) (KeyHashObj testKeyHash2))
(Addr (KeyHashObj testKeyHash1) (StakeRefBase $ KeyHashObj testKeyHash2))
( (T $ TkListLen 3)
<> (T $ TkWord 0)
<> S testKeyHash1
<> S testKeyHash2
)
, checkEncodingCBOR "base_address_key_script"
(AddrBase (KeyHashObj testKeyHash1) (ScriptHashObj testScriptHash))
(Addr (KeyHashObj testKeyHash1) (StakeRefBase $ ScriptHashObj testScriptHash))
( (T $ TkListLen 3)
<> (T $ TkWord 1)
<> S testKeyHash1
<> S testScriptHash
)
, checkEncodingCBOR "base_address_script_key"
(AddrBase (ScriptHashObj testScriptHash) (KeyHashObj testKeyHash2))
(Addr (ScriptHashObj testScriptHash) (StakeRefBase $ KeyHashObj testKeyHash2))
( (T $ TkListLen 3)
<> (T $ TkWord 2)
<> S testScriptHash
<> S testKeyHash2
)
, checkEncodingCBOR "base_address_script_script"
(AddrBase (ScriptHashObj testScriptHash) (ScriptHashObj testScriptHash2))
(Addr (ScriptHashObj testScriptHash) (StakeRefBase $ ScriptHashObj testScriptHash2))
( (T $ TkListLen 3)
<> (T $ TkWord 3)
<> S testScriptHash
<> S testScriptHash2
)
, let ptr = Ptr (SlotNo 12) 0 3 in
checkEncodingCBOR "pointer_address_key"
(AddrPtr (KeyHashObj testKeyHash1) ptr)
(Addr (KeyHashObj testKeyHash1) (StakeRefPtr ptr))
( (T $ TkListLen (2 + fromIntegral (listLen ptr)))
<> T (TkWord 4)
<> S testKeyHash1
<> G ptr
)
, let ptr = Ptr (SlotNo 12) 0 3 in
checkEncodingCBOR "pointer_address_script"
(AddrPtr (ScriptHashObj testScriptHash) ptr)
(Addr (ScriptHashObj testScriptHash) (StakeRefPtr ptr))
( (T $ TkListLen (2 + fromIntegral (listLen ptr)))
<> T (TkWord 5)
<> S testScriptHash
<> G ptr
)
, checkEncodingCBOR "enterprise_address_key"
(AddrEnterprise (KeyHashObj testKeyHash1))
(Addr (KeyHashObj testKeyHash1) StakeRefNull)
(T (TkListLen 2) <> T (TkWord 6) <> S testKeyHash1)
, checkEncodingCBOR "enterprise_address_script"
(AddrEnterprise (ScriptHashObj testScriptHash))
(Addr (ScriptHashObj testScriptHash) StakeRefNull)
(T (TkListLen 2) <> T (TkWord 7) <> S testScriptHash)
, checkEncodingCBOR "txin"
(TxIn genesisId 0 :: TxIn)
(T (TkListLen 2) <> S (genesisId :: TxId) <> T (TkWord64 0))
, let a = AddrEnterprise (KeyHashObj testKeyHash1) in
, let a = Addr (KeyHashObj testKeyHash1) StakeRefNull in
checkEncodingCBOR "txout"
(TxOut a (Coin 2))
(T (TkListLen 3)
Expand Down

0 comments on commit b48002c

Please sign in to comment.