diff --git a/shelley/chain-and-ledger/executable-spec/src/Address.hs b/shelley/chain-and-ledger/executable-spec/src/Address.hs index ea8aecbb189..76f7cfcadbf 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Address.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Address.hs @@ -7,8 +7,8 @@ where import Cardano.Crypto.Hash (HashAlgorithm) -import Delegation.PoolParams (RewardAcnt(..)) import Keys +import TxData mkRwdAcnt :: ( HashAlgorithm hashAlgo @@ -16,4 +16,4 @@ mkRwdAcnt ) => KeyPair dsignAlgo -> RewardAcnt hashAlgo dsignAlgo -mkRwdAcnt keys = RewardAcnt $ hashKey $ vKey keys +mkRwdAcnt keys = RewardAcnt $ KeyHashStake (hashKey $ vKey keys) diff --git a/shelley/chain-and-ledger/executable-spec/src/Delegation/Certificates.hs b/shelley/chain-and-ledger/executable-spec/src/Delegation/Certificates.hs index 839b414e1e1..bd719f82adb 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Delegation/Certificates.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Delegation/Certificates.hs @@ -24,8 +24,6 @@ import PParams (PParams (..), keyDecayRate, keyDeposit, keyMinRefund, import Slot (Duration (..)) import TxData -import Delegation.PoolParams - import BaseTypes import NonIntegral (exp') @@ -38,15 +36,13 @@ import Lens.Micro ((^.)) cwitness :: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) => DCert hashAlgo dsignAlgo - -> KeyHash hashAlgo dsignAlgo -cwitness (RegKey (KeyHashStake hk)) = hk -cwitness (DeRegKey (KeyHashStake hk)) = hk -cwitness (RegKey (ScriptHashStake _)) = undefined -cwitness (DeRegKey (ScriptHashStake _)) = undefined -cwitness (RegPool pool) = hashKey $ pool ^. poolPubKey -cwitness (RetirePool k _) = k -cwitness (Delegate delegation) = hashKey $ delegation ^. delegator -cwitness (GenesisDelegate (gk, _)) = hashGenesisKey gk + -> StakeObject hashAlgo dsignAlgo +cwitness (RegKey hk) = hk +cwitness (DeRegKey hk) = hk +cwitness (RegPool pool) = KeyHashStake $ hashKey $ pool ^. poolPubKey +cwitness (RetirePool k _) = KeyHashStake k +cwitness (Delegate delegation) = delegation ^. delegator +cwitness (GenesisDelegate (gk, _)) = KeyHashStake $ hashGenesisKey gk -- |Retrieve the deposit amount for a certificate dvalue :: DCert hashAlgo dsignAlgo -> PParams -> Coin diff --git a/shelley/chain-and-ledger/executable-spec/src/Delegation/PoolParams.hs b/shelley/chain-and-ledger/executable-spec/src/Delegation/PoolParams.hs index f20897bd110..78bf25d74b7 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Delegation/PoolParams.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Delegation/PoolParams.hs @@ -3,86 +3,14 @@ {-# LANGUAGE TemplateHaskell #-} module Delegation.PoolParams - ( PoolParams(..) - , Delegation(..) - , RewardAcnt(..) - -- lenses - , poolPubKey - , poolPledge - , poolPledges - , poolCost - , poolMargin - , poolAltAcnt - , poolSpec - , poolRAcnt - , poolOwners - -- Delegation - , delegator - , delegatee + ( poolSpec ) where -import Data.Map.Strict (Map) -import Data.Set (Set) - import Lens.Micro ((^.)) -import Lens.Micro.TH (makeLenses) - -import Cardano.Binary (ToCBOR(toCBOR), encodeListLen) import BaseTypes import Coin (Coin) -import Keys - --- |An account based address for a rewards -newtype RewardAcnt hashAlgo signAlgo = RewardAcnt - { getRwdHK :: KeyHash hashAlgo signAlgo - } deriving (Show, Eq, Ord, ToCBOR) - - --- |A stake pool. -data PoolParams hashAlgo dsignAlgo = - PoolParams - { _poolPubKey :: VKey dsignAlgo - , _poolPledge :: Coin - , _poolPledges :: Map (VKey dsignAlgo) Coin -- TODO not updated currently - , _poolCost :: Coin - , _poolMargin :: UnitInterval - , _poolAltAcnt :: Maybe (KeyHash hashAlgo dsignAlgo) - , _poolRAcnt :: RewardAcnt hashAlgo dsignAlgo - , _poolOwners :: Set (KeyHash hashAlgo dsignAlgo) - } deriving (Show, Eq, Ord) - -makeLenses ''PoolParams - -instance - (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) - => ToCBOR (PoolParams hashAlgo dsignAlgo) - where - toCBOR poolParams = - encodeListLen 8 - <> toCBOR (_poolPubKey poolParams) - <> toCBOR (_poolPledge poolParams) - <> toCBOR (_poolPledges poolParams) - <> toCBOR (_poolCost poolParams) - <> toCBOR (_poolMargin poolParams) - <> toCBOR (_poolAltAcnt poolParams) - <> toCBOR (_poolRAcnt poolParams) - <> toCBOR (_poolOwners poolParams) - +import TxData poolSpec :: PoolParams hashAlgo dsignAlgo -> (Coin, UnitInterval, Coin) poolSpec pool = (pool ^. poolCost, pool ^. poolMargin, pool ^. poolPledge) - --- |The delegation of one stake key to another. -data Delegation dsignAlgo = Delegation - { _delegator :: VKey dsignAlgo - , _delegatee :: VKey dsignAlgo - } deriving (Show, Eq, Ord) - -instance DSIGNAlgorithm dsignAlgo => ToCBOR (Delegation dsignAlgo) where - toCBOR delegation = - encodeListLen 2 - <> toCBOR (_delegator delegation) - <> toCBOR (_delegatee delegation) - -makeLenses ''Delegation diff --git a/shelley/chain-and-ledger/executable-spec/src/EpochBoundary.hs b/shelley/chain-and-ledger/executable-spec/src/EpochBoundary.hs index 509cf891f74..b803100f9af 100644 --- a/shelley/chain-and-ledger/executable-spec/src/EpochBoundary.hs +++ b/shelley/chain-and-ledger/executable-spec/src/EpochBoundary.hs @@ -33,7 +33,6 @@ module EpochBoundary import Coin import Delegation.Certificates (StakeKeys (..), StakePools (..), decayKey, decayPool, refund) -import Delegation.PoolParams (RewardAcnt (..), PoolParams(..)) import Keys import PParams hiding (a0, nOpt) import Slot @@ -59,12 +58,13 @@ newtype BlocksMade hashAlgo dsignAlgo -- | Type of stake as map from hash key to coins associated. newtype Stake hashAlgo dsignAlgo - = Stake (Map.Map (KeyHash hashAlgo dsignAlgo) Coin) + = Stake (Map.Map (StakeObject hashAlgo dsignAlgo) Coin) deriving (Show, Eq, Ord) -- | Extract hash of staking key from base address. -getStakeHK :: Addr hashAlgo dsignAlgo -> Maybe (KeyHash hashAlgo dsignAlgo) -getStakeHK (AddrVKey _ hk) = Just hk +getStakeHK :: Addr hashAlgo dsignAlgo -> Maybe (StakeObject hashAlgo dsignAlgo) +getStakeHK (AddrVKey _ hk) = Just $ KeyHashStake hk +getStakeHK (AddrScr _ hs) = Just $ ScriptHashStake hs getStakeHK _ = Nothing consolidate :: UTxO hashAlgo dsignAlgo -> Map.Map (Addr hashAlgo dsignAlgo) Coin @@ -78,7 +78,7 @@ baseStake vals = where convert :: (Addr hashAlgo dsignAlgo, Coin) - -> Maybe (KeyHash hashAlgo dsignAlgo, Coin) + -> Maybe (StakeObject hashAlgo dsignAlgo, Coin) convert (a, c) = (,c) <$> getStakeHK a @@ -91,14 +91,14 @@ getStakePtr _ = Nothing ptrStake :: forall hashAlgo dsignAlgo . Map.Map (Addr hashAlgo dsignAlgo) Coin - -> Map.Map Ptr (KeyHash hashAlgo dsignAlgo) + -> Map.Map Ptr (StakeObject hashAlgo dsignAlgo) -> Stake hashAlgo dsignAlgo ptrStake vals pointers = Stake $ Map.fromListWith (+) (mapMaybe convert $ Map.toList vals) where convert :: (Addr hashAlgo dsignAlgo, Coin) - -> Maybe (KeyHash hashAlgo dsignAlgo, Coin) + -> Maybe (StakeObject hashAlgo dsignAlgo, Coin) convert (a, c) = case getStakePtr a of Nothing -> Nothing @@ -117,7 +117,7 @@ rewardStake rewards = -- | Get stake of one pool poolStake :: KeyHash hashAlgo dsignAlgo - -> Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + -> Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) -> Stake hashAlgo dsignAlgo -> Stake hashAlgo dsignAlgo poolStake hk delegs (Stake stake) = @@ -183,15 +183,15 @@ data SnapShots hashAlgo dsignAlgo = SnapShots { _pstakeMark :: ( Stake hashAlgo dsignAlgo - , Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + , Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) ) , _pstakeSet :: ( Stake hashAlgo dsignAlgo - , Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + , Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) ) , _pstakeGo :: ( Stake hashAlgo dsignAlgo - , Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + , Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) ) , _poolsSS :: Map.Map (KeyHash hashAlgo dsignAlgo) (PoolParams hashAlgo dsignAlgo) diff --git a/shelley/chain-and-ledger/executable-spec/src/LedgerState.hs b/shelley/chain-and-ledger/executable-spec/src/LedgerState.hs index 9355903b0e4..94db4ea6bb1 100644 --- a/shelley/chain-and-ledger/executable-spec/src/LedgerState.hs +++ b/shelley/chain-and-ledger/executable-spec/src/LedgerState.hs @@ -133,8 +133,7 @@ import UTxO import Delegation.Certificates (DCert (..), PoolDistr (..), StakeKeys (..), StakePools (..), cwitness, decayKey, refund) -import Delegation.PoolParams (Delegation (..), PoolParams (..), RewardAcnt (..), - poolOwners, poolPledge, poolPubKey, poolRAcnt, poolSpec) +import Delegation.PoolParams import BaseTypes @@ -214,9 +213,9 @@ data DState hashAlgo dsignAlgo = DState -- |The active accounts. , _rewards :: RewardAccounts hashAlgo dsignAlgo -- |The current delegations. - , _delegations :: Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + , _delegations :: Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) -- |The pointed to hash keys. - , _ptrs :: Map.Map Ptr (KeyHash hashAlgo dsignAlgo) + , _ptrs :: Map.Map Ptr (StakeObject hashAlgo dsignAlgo) -- | future genesis key delegations , _fdms :: Map.Map (Slot, VKeyGenesis dsignAlgo) (VKey dsignAlgo) -- |Genesis key delegations @@ -463,7 +462,7 @@ keyRefund -> Coin keyRefund dval dmin lambda (StakeKeys stkeys) slot c = case c of - DeRegKey (KeyHashStake key) -> case Map.lookup key stkeys of -- TODO + DeRegKey key -> case Map.lookup key stkeys of Nothing -> Coin 0 Just s -> refund dval dmin lambda $ slot -* s _ -> Coin 0 @@ -477,7 +476,7 @@ decayedKey -> Coin decayedKey pp stk@(StakeKeys stkeys) cslot cert = case cert of - DeRegKey (KeyHashStake key) -> -- TODO + DeRegKey key -> if Map.notMember key stkeys then 0 else let created' = stkeys Map.! key in @@ -563,10 +562,11 @@ witsNeeded utxo' tx@(Tx txbody _ _) _dms = Just (TxOut (AddrVKey pay _) _) -> Set.insert pay hkeys _ -> hkeys - wdrlAuthors = Set.map getRwdHK (Map.keysSet (txbody ^. wdrls)) + wdrlAuthors = + Set.fromList $ extractKeyHash $ map getRwdHK (Map.keys (txbody ^. wdrls)) owners = foldl Set.union Set.empty [pool ^. poolOwners | RegPool pool <- txbody ^. certs] - certAuthors = Set.fromList (fmap getCertHK (txbody ^. certs)) + certAuthors = Set.fromList $ extractKeyHash (fmap getCertHK (txbody ^. certs)) getCertHK cert = cwitness cert updateKeys = propWits (txup tx) _dms @@ -676,7 +676,7 @@ validKeyRegistration -> Validity validKeyRegistration cert ds = case cert of - RegKey (KeyHashStake key) -> if not $ Map.member key stakeKeys -- TODO + RegKey key -> if not $ Map.member key stakeKeys then Valid else Invalid [StakeKeyAlreadyRegistered] where (StakeKeys stakeKeys) = ds ^. stKeys _ -> Valid @@ -687,20 +687,19 @@ validKeyDeregistration -> Validity validKeyDeregistration cert ds = case cert of - DeRegKey (KeyHashStake key) -> if Map.member key stakeKeys -- TODO + DeRegKey key -> if Map.member key stakeKeys then Valid else Invalid [StakeKeyNotRegistered] where (StakeKeys stakeKeys) = ds ^. stKeys _ -> Valid validStakeDelegation - :: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) - => DCert hashAlgo dsignAlgo + :: DCert hashAlgo dsignAlgo -> DState hashAlgo dsignAlgo -> Validity validStakeDelegation cert ds = case cert of Delegate (Delegation source _) - -> if Map.member (hashKey source) stakeKeys + -> if Map.member source stakeKeys then Valid else Invalid [StakeDelegationImpossible] where (StakeKeys stakeKeys) = ds ^. stKeys _ -> Valid @@ -724,8 +723,7 @@ validStakePoolRetire cert ps = _ -> Valid validDelegation - :: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) - => DCert hashAlgo dsignAlgo + :: DCert hashAlgo dsignAlgo -> DPState hashAlgo dsignAlgo -> Validity validDelegation cert ds = @@ -882,30 +880,27 @@ applyDCert ptr dcert@(Delegate _) ds = ds & dstate %~ (applyDCertDState ptr dcert) applyDCertDState - :: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) - => Ptr + :: Ptr -> DCert hashAlgo dsignAlgo -> DState hashAlgo dsignAlgo -> DState hashAlgo dsignAlgo -applyDCertDState (Ptr slot txIx clx) (DeRegKey (KeyHashStake key)) ds = -- TODO +applyDCertDState (Ptr slot txIx clx) (DeRegKey key) ds = ds & stKeys .~ (StakeKeys $ Map.delete hksk stkeys') & rewards %~ Map.delete (RewardAcnt hksk) & delegations %~ Map.delete hksk & ptrs %~ Map.delete (Ptr slot txIx clx) where hksk = key (StakeKeys stkeys') = ds ^. stKeys -applyDCertDState _ (DeRegKey (ScriptHashStake _)) _ = undefined -applyDCertDState (Ptr slot txIx clx) (RegKey (KeyHashStake key)) ds = -- TODO +applyDCertDState (Ptr slot txIx clx) (RegKey key) ds = ds & stKeys .~ (StakeKeys $ Map.insert hksk slot stkeys') & rewards %~ Map.insert (RewardAcnt hksk) (Coin 0) & ptrs %~ Map.insert (Ptr slot txIx clx) hksk where hksk = key (StakeKeys stkeys') = ds ^. stKeys -applyDCertDState _ (RegKey (ScriptHashStake _)) _ = undefined applyDCertDState _ (Delegate (Delegation source target)) ds = - ds & delegations %~ Map.insert (hashKey source) (hashKey target) + ds & delegations %~ Map.insert source target applyDCertDState _ _ ds = ds @@ -938,7 +933,7 @@ delegatedStake ls@(LedgerState _ ds _) = Map.fromListWith (+) delegatedOutputs where getOutputs (UTxO utxo') = Map.elems utxo' addStake delegs (TxOut (AddrVKey _ hsk) c) = do - pool <- Map.lookup hsk delegs + pool <- Map.lookup (KeyHashStake hsk) delegs return (pool, c) addStake _ (TxOut (AddrScr _ _) _) = undefined -- TODO: script addresses addStake delegs (TxOut (AddrPtr ptr) c) = do @@ -954,7 +949,7 @@ delegatedStake ls@(LedgerState _ ds _) = Map.fromListWith (+) delegatedOutputs -- | Calculate pool reward poolRewards - :: KeyHash hashAlgo dsignAlgo + :: StakeObject hashAlgo dsignAlgo -- TODO check why this paramater is not used -> UnitInterval -> Natural -> Natural @@ -1001,7 +996,7 @@ rewardOnePool -> Coin -> Natural -> Natural - -> KeyHash hashAlgo dsignAlgo + -> StakeObject hashAlgo dsignAlgo -> PoolParams hashAlgo dsignAlgo -> Stake hashAlgo dsignAlgo -> Coin @@ -1039,7 +1034,7 @@ reward -> Set.Set (RewardAcnt hashAlgo dsignAlgo) -> Map.Map (KeyHash hashAlgo dsignAlgo) (PoolParams hashAlgo dsignAlgo) -> Stake hashAlgo dsignAlgo - -> Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + -> Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) -> Map.Map (RewardAcnt hashAlgo dsignAlgo) Coin reward pp (BlocksMade b) r addrsRew poolParams stake@(Stake stake') delegs = rewards' @@ -1055,7 +1050,7 @@ reward pp (BlocksMade b) r addrsRew poolParams stake@(Stake stake') delegs = ] results = [ ( hk - , rewardOnePool pp r n totalBlocks hk pool actgr total addrsRew) + , rewardOnePool pp r n totalBlocks (KeyHashStake hk) pool actgr total addrsRew) | (hk, (pool, n, actgr)) <- pdata ] rewards' = foldl (\m (_, r') -> Map.union m r') Map.empty results @@ -1070,13 +1065,13 @@ stakeDistr stakeDistr u ds ps = Stake $ (Map.keysSet activeDelegs) ◁ stake where DState (StakeKeys stkeys) rewards' delegs ptrs' _ _ = ds - PState (StakePools stpools) _ _ _ = ps + PState (StakePools stpools) _ _ _ = ps outs = consolidate u stake = baseStake' `Map.union` pointerStake `Map.union` rewardStake' Stake baseStake' = baseStake outs Stake pointerStake = ptrStake outs ptrs' Stake rewardStake' = rewardStake rewards' - activeDelegs = (Map.keysSet stpools) ◁ delegs ▷ (Map.keysSet stkeys) + activeDelegs = (Map.keysSet stkeys) ◁ delegs ▷ (Map.keysSet stpools) -- | Pool distribution poolDistr @@ -1084,7 +1079,7 @@ poolDistr -> DState hashAlgo dsignAlgo -> PState hashAlgo dsignAlgo -> ( Stake hashAlgo dsignAlgo - , Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) + , Map.Map (StakeObject hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo) ) poolDistr u ds ps = (stake, delegs) where diff --git a/shelley/chain-and-ledger/executable-spec/src/STS/Deleg.hs b/shelley/chain-and-ledger/executable-spec/src/STS/Deleg.hs index 069736cdd25..ac85eaa8be8 100644 --- a/shelley/chain-and-ledger/executable-spec/src/STS/Deleg.hs +++ b/shelley/chain-and-ledger/executable-spec/src/STS/Deleg.hs @@ -20,7 +20,7 @@ import Control.State.Transition data DELEG hashAlgo dsignAlgo instance - (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) + (DSIGNAlgorithm dsignAlgo) => STS (DELEG hashAlgo dsignAlgo) where type State (DELEG hashAlgo dsignAlgo) = DState hashAlgo dsignAlgo @@ -38,7 +38,7 @@ instance transitionRules = [delegationTransition] delegationTransition - :: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) + :: (DSIGNAlgorithm dsignAlgo) => TransitionRule (DELEG hashAlgo dsignAlgo) delegationTransition = do TRC ((_slot, p), d@(DState _ _ _ _ genMap (Dms _dms)), c) <- judgmentContext diff --git a/shelley/chain-and-ledger/executable-spec/src/STS/Delegs.hs b/shelley/chain-and-ledger/executable-spec/src/STS/Delegs.hs index e266d92cd80..44fe1a3ebfb 100644 --- a/shelley/chain-and-ledger/executable-spec/src/STS/Delegs.hs +++ b/shelley/chain-and-ledger/executable-spec/src/STS/Delegs.hs @@ -12,7 +12,6 @@ where import qualified Data.Map.Strict as Map import Delegation.Certificates -import Delegation.PoolParams import Keys import LedgerState import PParams hiding (d) @@ -76,7 +75,7 @@ delegsTransition = do let isDelegationRegistered = case cert of Delegate deleg -> let StakePools sp = _stPools $ _pstate dpstate in - Map.member (hashKey $ _delegatee deleg) sp + Map.member (_delegatee deleg) sp _ -> True isDelegationRegistered ?! DelegateeNotRegisteredDELEG dpstate' <- diff --git a/shelley/chain-and-ledger/executable-spec/src/Tx.hs b/shelley/chain-and-ledger/executable-spec/src/Tx.hs index d78e486968a..b4ffbbfa797 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Tx.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Tx.hs @@ -27,6 +27,7 @@ module Tx , hashScript , txwitsVKey , txwitsScripts + , extractKeyHash ) where @@ -41,6 +42,7 @@ import Cardano.Crypto.DSIGN (DSIGNAlgorithm) import Data.Word (Word8) +import qualified Data.Maybe as Maybe import qualified Data.Map.Strict as Map import Data.Set (Set) import qualified Data.Set as Set @@ -109,3 +111,9 @@ txwitsScripts :: Tx hashAlgo dsignAlgo -> Map.Map (ScriptHash hashAlgo dsignAlgo) (MultiSig hashAlgo dsignAlgo) txwitsScripts tx = _witnessMSigMap tx + +extractKeyHash :: [StakeObject hashAlgo dsignAlgo] -> [KeyHash hashAlgo dsignAlgo] +extractKeyHash l = + Maybe.catMaybes $ map (\so -> case so of + KeyHashStake hk -> Just hk + _ -> Nothing) l diff --git a/shelley/chain-and-ledger/executable-spec/src/TxData.hs b/shelley/chain-and-ledger/executable-spec/src/TxData.hs index 233f7604f64..c3535d3445f 100644 --- a/shelley/chain-and-ledger/executable-spec/src/TxData.hs +++ b/shelley/chain-and-ledger/executable-spec/src/TxData.hs @@ -16,12 +16,36 @@ import Data.Typeable (Typeable) import Data.Word (Word8) import Numeric.Natural (Natural) +import BaseTypes import Coin -import Delegation.PoolParams import Keys import Slot import Updates +-- |The delegation of one stake key to another. +data Delegation hashAlgo dsignAlgo = Delegation + { _delegator :: StakeObject hashAlgo dsignAlgo + , _delegatee :: KeyHash hashAlgo dsignAlgo + } deriving (Show, Eq, Ord) + +-- |A stake pool. +data PoolParams hashAlgo dsignAlgo = + PoolParams + { _poolPubKey :: VKey dsignAlgo + , _poolPledge :: Coin + , _poolPledges :: Map (VKey dsignAlgo) Coin -- TODO not updated currently + , _poolCost :: Coin + , _poolMargin :: UnitInterval + , _poolAltAcnt :: Maybe (KeyHash hashAlgo dsignAlgo) + , _poolRAcnt :: RewardAcnt hashAlgo dsignAlgo + , _poolOwners :: Set (KeyHash hashAlgo dsignAlgo) + } deriving (Show, Eq, Ord) + +-- |An account based address for a rewards +newtype RewardAcnt hashAlgo signAlgo = RewardAcnt + { getRwdHK :: StakeObject hashAlgo signAlgo + } deriving (Show, Eq, Ord) + -- |An address for UTxO. data Addr hashAlgo dsignAlgo = AddrVKey @@ -80,13 +104,13 @@ data DCert hashAlgo dsignAlgo -- | A stake key registration certificate. = RegKey (StakeObject hashAlgo dsignAlgo) -- | A stake key deregistration certificate. - | DeRegKey (StakeObject hashAlgo dsignAlgo) --TODO this is actually KeyHash on page 13, is that what we want? + | DeRegKey (StakeObject hashAlgo dsignAlgo) -- | A stake pool registration certificate. | RegPool (PoolParams hashAlgo dsignAlgo) -- | A stake pool retirement certificate. | RetirePool (KeyHash hashAlgo dsignAlgo) Epoch -- | A stake delegation certificate. - | Delegate (Delegation dsignAlgo) + | Delegate (Delegation hashAlgo dsignAlgo) -- | Genesis key delegation certificate | GenesisDelegate (VKeyGenesis dsignAlgo, VKey dsignAlgo) deriving (Show, Eq, Ord) @@ -103,8 +127,6 @@ data TxBody hashAlgo dsignAlgo , _txUpdate :: Update dsignAlgo } deriving (Show, Eq, Ord) -makeLenses ''TxBody - -- |Proof/Witness that a transaction is authorized by the given key holder. data WitVKey hashAlgo dsignAlgo = WitVKey (VKey dsignAlgo) !(Sig dsignAlgo (TxBody hashAlgo dsignAlgo)) @@ -119,18 +141,8 @@ data Tx hashAlgo dsignAlgo Map (ScriptHash hashAlgo dsignAlgo) (MultiSig hashAlgo dsignAlgo) } deriving (Show, Eq, Ord) -makeLenses ''Tx - --- newtype StakePools hashAlgo dsignAlgo = --- StakePools (Map (KeyHash hashAlgo dsignAlgo) Slot) --- deriving (Show, Eq) - --- newtype StakeKeys hashAlgo dsignAlgo = --- StakeKeys (Map (StakeObject hashAlgo dsignAlgo) Slot) --- deriving (Show, Eq) - newtype StakeKeys hashAlgo dsignAlgo = - StakeKeys (Map (KeyHash hashAlgo dsignAlgo) Slot) + StakeKeys (Map (StakeObject hashAlgo dsignAlgo) Slot) deriving (Show, Eq) newtype StakePools hashAlgo dsignAlgo = @@ -287,3 +299,43 @@ instance (Typeable dsignAlgo, HashAlgorithm hashAlgo) encodeListLen 2 <> toCBOR (1 :: Word8) <> toCBOR sc + + +instance (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) => + ToCBOR (Delegation hashAlgo dsignAlgo) where + toCBOR delegation = + encodeListLen 2 + <> toCBOR (_delegator delegation) + <> toCBOR (_delegatee delegation) + + +instance + (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) + => ToCBOR (PoolParams hashAlgo dsignAlgo) + where + toCBOR poolParams = + encodeListLen 8 + <> toCBOR (_poolPubKey poolParams) + <> toCBOR (_poolPledge poolParams) + <> toCBOR (_poolPledges poolParams) + <> toCBOR (_poolCost poolParams) + <> toCBOR (_poolMargin poolParams) + <> toCBOR (_poolAltAcnt poolParams) + <> toCBOR (_poolRAcnt poolParams) + <> toCBOR (_poolOwners poolParams) + +instance (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo) + => ToCBOR (RewardAcnt hashAlgo dsignAlgo) where + toCBOR rwdAcnt = + encodeListLen 1 + <> toCBOR (getRwdHK rwdAcnt) + +-- Lenses + +makeLenses ''TxBody + +makeLenses ''Tx + +makeLenses ''Delegation + +makeLenses ''PoolParams diff --git a/shelley/chain-and-ledger/executable-spec/src/UTxO.hs b/shelley/chain-and-ledger/executable-spec/src/UTxO.hs index 43d17604a72..e74b663ca5f 100644 --- a/shelley/chain-and-ledger/executable-spec/src/UTxO.hs +++ b/shelley/chain-and-ledger/executable-spec/src/UTxO.hs @@ -46,9 +46,9 @@ import Keys import PParams (PParams(..)) import Updates (Update) import Tx +import TxData import Delegation.Certificates (StakePools(..), DCert (..), dvalue) -import Delegation.PoolParams (poolPubKey) -- |The unspent transaction outputs. newtype UTxO hashAlgo dsignAlgo