Skip to content

Commit

Permalink
hash hygiene (#1602)
Browse files Browse the repository at this point in the history
* move genesisId, genesisCoins, and applyTxBody into tests

* the only CBOR in the mkSeed calculation is from the SlotNo

* removes the duplicate tag applied when hashing a multisig script
  • Loading branch information
redxaxder committed Jul 1, 2020
1 parent 7f9cbdd commit 7aabae6
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 118 deletions.
Expand Up @@ -123,7 +123,6 @@ import Shelley.Spec.Ledger.Keys
decodeVerKeyVRF,
encodeSignedKES,
encodeVerKeyVRF,
hash,
)
import Shelley.Spec.Ledger.OCert (OCert (..))
import Shelley.Spec.Ledger.PParams (ProtVer (..))
Expand Down Expand Up @@ -604,10 +603,14 @@ mkSeed ::
-- | Epoch nonce
Nonce ->
Seed
mkSeed (Nonce uc) slot nonce =
Seed . coerce $ uc `Hash.xor` coerce (hash @SHA256 (slot, nonce))
mkSeed NeutralNonce slot nonce =
Seed . coerce $ hash @SHA256 (slot, nonce)
mkSeed uc slot nonce = Seed . coerce $ case uc of
NeutralNonce -> seed
Nonce ucNonce -> ucNonce `Hash.xor` seed
where
seed = coerce $ Hash.hashRaw @SHA256 id (serialize' slot <> nonceBytes)
nonceBytes = case nonce of
NeutralNonce -> mempty
Nonce n -> Hash.getHash n

-- | Check that the certified input natural is valid for being slot leader. This
-- means we check that
Expand Down
Expand Up @@ -23,43 +23,42 @@
-- as state transformations on a ledger state ('LedgerState'),
-- as specified in /A Simplified Formal Specification of a UTxO Ledger/.
module Shelley.Spec.Ledger.LedgerState
( LedgerState (..),
Ix,
( AccountState (..),
DPState (..),
DState (..),
AccountState (..),
RewardUpdate (..),
RewardAccounts,
InstantaneousRewards (..),
emptyInstantaneousRewards,
totalInstantaneousReservesRewards,
totalInstantaneousTreasuryRewards,
emptyRewardUpdate,
FutureGenDeleg (..),
EpochState (..),
emptyEpochState,
emptyLedgerState,
emptyPPUPState,
emptyUTxOState,
updatePpup,
PState (..),
FutureGenDeleg (..),
InstantaneousRewards (..),
Ix,
KeyPairs,
LedgerState (..),
OBftSlot (..),
PPUPState (..),
pvCanFollow,
PState (..),
RewardAccounts,
RewardUpdate (..),
UTxOState (..),
OBftSlot (..),
depositPoolChange,
emptyAccount,
emptyPState,
emptyDState,
emptyDPState,
emptyDState,
emptyEpochState,
emptyInstantaneousRewards,
emptyLedgerState,
emptyPPUPState,
emptyPState,
emptyRewardUpdate,
emptyUTxOState,
pvCanFollow,
reapRewards,
totalInstantaneousReservesRewards,
totalInstantaneousTreasuryRewards,
updatePpup,

-- * state transitions
emptyDelegation,
applyTxBody,

-- * Genesis State
genesisId,
genesisCoins,
genesisState,

-- * Validation
Expand Down Expand Up @@ -109,7 +108,6 @@ import qualified Data.List.NonEmpty as NonEmpty
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import qualified Data.Sequence.Strict as StrictSeq
import Data.Set (Set)
import qualified Data.Set as Set
import GHC.Generics (Generic)
Expand All @@ -123,7 +121,7 @@ import Shelley.Spec.Ledger.BaseTypes
intervalValue,
)
import Shelley.Spec.Ledger.Coin (Coin (..))
import Shelley.Spec.Ledger.Core (dom, haskey, range, (∪), (∪+), (⋪), (▷), (◁))
import Shelley.Spec.Ledger.Core (dom, haskey, range, (∪), (∪+), (▷), (◁))
import Shelley.Spec.Ledger.Credential (Credential (..))
import Shelley.Spec.Ledger.Crypto (Crypto)
import Shelley.Spec.Ledger.Delegation.Certificates
Expand Down Expand Up @@ -158,7 +156,6 @@ import Shelley.Spec.Ledger.Keys
KeyRole (..),
VKey,
asWitness,
hash,
)
import Shelley.Spec.Ledger.PParams
( PParams,
Expand Down Expand Up @@ -198,8 +195,6 @@ import Shelley.Spec.Ledger.TxData
Ptr (..),
RewardAcnt (..),
TxBody (..),
TxId (..),
TxIn (..),
TxOut (..),
Wdrl (..),
WitVKey (..),
Expand Down Expand Up @@ -689,33 +684,6 @@ instance Crypto crypto => FromCBOR (LedgerState crypto) where
dp <- fromCBOR
pure $ LedgerState u dp

-- | The transaction Id for 'UTxO' included at the beginning of a new ledger.
genesisId ::
(Crypto crypto) =>
TxId crypto
genesisId =
TxId $
hash
( TxBody
Set.empty
StrictSeq.Empty
StrictSeq.Empty
(Wdrl Map.empty)
(Coin 0)
(SlotNo 0)
SNothing
SNothing
)

-- | Creates the UTxO for a new ledger with the specified transaction outputs.
genesisCoins ::
(Crypto crypto) =>
[TxOut crypto] ->
UTxO crypto
genesisCoins outs =
UTxO $
Map.fromList [(TxIn genesisId idx, out) | (idx, out) <- zip [0 ..] outs]

-- | Creates the ledger state for an empty ledger which
-- contains the specified transaction outputs.
genesisState ::
Expand Down Expand Up @@ -917,37 +885,6 @@ depositPoolChange ls pp tx = (currentPool + txDeposits) - txRefunds
totalDeposits pp ((_stPools . _pstate . _delegationState) ls) (toList $ _certs tx)
txRefunds = keyRefunds pp tx

-- | Apply a transaction body as a state transition function on the ledger state.
--
-- TODO this function is only used in testing, and should be moved accordingly.
applyTxBody ::
(Crypto crypto) =>
LedgerState crypto ->
PParams ->
TxBody crypto ->
LedgerState crypto
applyTxBody ls pp tx =
ls
{ _utxoState =
us
{ _utxo = txins tx (_utxo us) txouts tx,
_deposited = depositPoolChange ls pp tx,
_fees = (_txfee tx) + (_fees . _utxoState $ ls)
},
_delegationState =
dels
{ _dstate = dst {_rewards = newAccounts}
}
}
where
dels = _delegationState ls
dst = _dstate dels
us = _utxoState ls
newAccounts =
reapRewards
((_rewards . _dstate . _delegationState) ls)
(unWdrl $ _wdrls tx)

reapRewards ::
RewardAccounts crypto ->
RewardAccounts crypto ->
Expand Down
Expand Up @@ -38,7 +38,6 @@ import Cardano.Binary
encodeListLen,
encodePreEncoded,
encodeWord,
encodeWord8,
matchSize,
serializeEncoding,
)
Expand Down Expand Up @@ -156,20 +155,15 @@ countMSigNodes (RequireAllOf msigs) = 1 + sum (map countMSigNodes msigs)
countMSigNodes (RequireAnyOf msigs) = 1 + sum (map countMSigNodes msigs)
countMSigNodes (RequireMOf _ msigs) = 1 + sum (map countMSigNodes msigs)

-- | Hashes native multi-signature script, appending the 'nativeMultiSigTag' in
-- front and then calling the script CBOR function.
-- | Hashes native multi-signature script. We serialize it as a Script, which
-- includes the type tag.
hashMultiSigScript ::
Crypto crypto =>
MultiSig crypto ->
ScriptHash crypto
hashMultiSigScript msig =
ScriptHash $
hashWithSerialiser
( \x ->
encodeWord8 nativeMultiSigTag
<> toCBOR x
)
(MultiSigScript msig)
hashWithSerialiser toCBOR (MultiSigScript msig)

hashAnyScript ::
Crypto crypto =>
Expand Down
Expand Up @@ -58,7 +58,6 @@ import Shelley.Spec.Ledger.Keys
import Shelley.Spec.Ledger.LedgerState
( PPUPState (..),
UTxOState (..),
genesisId,
)
import Shelley.Spec.Ledger.PParams
( PParams' (..),
Expand Down Expand Up @@ -91,6 +90,7 @@ import Shelley.Spec.Ledger.UTxO
import qualified Test.Cardano.Chain.Common.Gen as Byron
import qualified Test.Cardano.Crypto.Gen as Byron
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (ConcreteCrypto)
import Test.Shelley.Spec.Ledger.Generator.Core (genesisId)
import Test.Shelley.Spec.Ledger.Utils (testSTS)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit
Expand Down
Expand Up @@ -198,8 +198,6 @@ import Shelley.Spec.Ledger.LedgerState
emptyRewardUpdate,
esAccountState,
esLState,
genesisCoins,
genesisId,
nesEs,
nonMyopic,
overlaySchedule,
Expand Down Expand Up @@ -350,6 +348,8 @@ import Test.Shelley.Spec.Ledger.Generator.Core
( AllIssuerKeys (..),
NatNonce (..),
genesisAccountState,
genesisCoins,
genesisId,
mkBlock,
mkOCert,
zero,
Expand Down
Expand Up @@ -35,7 +35,7 @@ import Shelley.Spec.Ledger.Delegation.Certificates
pattern RetirePool,
)
import Shelley.Spec.Ledger.Keys (KeyRole (..), asWitness, hashKey, vKey)
import Shelley.Spec.Ledger.LedgerState (genesisId, txsize)
import Shelley.Spec.Ledger.LedgerState (txsize)
import qualified Shelley.Spec.Ledger.MetaData as MD
import Shelley.Spec.Ledger.Scripts (pattern RequireMOf, pattern RequireSignature)
import Shelley.Spec.Ledger.Slot (EpochNo (..), SlotNo (..))
Expand Down Expand Up @@ -95,6 +95,7 @@ import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes
hashKeyVRF,
pattern KeyPair,
)
import Test.Shelley.Spec.Ledger.Generator.Core (genesisId)
import Test.Shelley.Spec.Ledger.Utils
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, testCase, (@?=))
Expand Down

0 comments on commit 7aabae6

Please sign in to comment.