Permalink
Browse files

Merge branch 'master' of github.com:input-output-hk/cardano-chain int…

…o mdimjasevic/304-polymorphic-block
  • Loading branch information...
mdimjasevic committed Feb 11, 2019
2 parents e55c20d + e853bb4 commit 8a69d930be1753503bea1ba0b401a017c7389525
@@ -199,6 +199,7 @@ test-suite cardano-chain-test
build-depends: base
, base16-bytestring
, bytestring
, canonical-json
, cardano-binary
, cardano-binary-test
, cardano-chain
@@ -8,7 +8,7 @@ packages:

extra-deps:
- git: https://github.com/input-output-hk/cardano-prelude
commit: 7f8544ae9e162b6664f07093227edb148f1c8a5b
commit: 7161436bd31195b2ddfa7d359cd99f0fe53c881c
subdirs:
- .
- test
@@ -1,4 +1,5 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE OverloadedStrings #-}

module Test.Cardano.Chain.Common.Gen
@@ -8,6 +9,7 @@ module Test.Cardano.Chain.Common.Gen
, genAddrSpendingData
, genAddrStakeDistribution
, genBlockCount
, genCanonicalTxFeePolicy
, genChainDifficulty
, genLovelace
, genLovelacePortion
@@ -135,6 +137,30 @@ genAddrStakeDistribution = Gen.choice
genBlockCount :: Gen BlockCount
genBlockCount = BlockCount <$> Gen.word64 Range.constantBounded

-- | `TxFeePolicyUnknown` is not needed because this is a generator
-- used to generate `GenesisData`.
genCanonicalTxFeePolicy :: Gen TxFeePolicy
genCanonicalTxFeePolicy = TxFeePolicyTxSizeLinear <$> genCanonicalTxSizeLinear

genCanonicalTxSizeLinear :: Gen TxSizeLinear
genCanonicalTxSizeLinear = TxSizeLinear <$> genLovelace' <*> genLovelace'
where
genLovelace' :: Gen Lovelace
genLovelace' =
mkLovelace
<$> Gen.word64 (Range.constant 0 maxCanonicalLovelaceVal)
>>= \case
Right lovelace -> pure lovelace
Left err -> panic $ sformat
("The impossible happened in genLovelace: " . build)
err
-- | Maximal possible value of `Lovelace` in Canonical JSON (JSNum !Int54)
-- This should be (2^53 - 1) ~ 9e15, however in the Canonical ToJSON instance of
-- `TxFeePolicy` this number is multiplied by 1e9 to keep compatibility with 'Nano'
-- coefficients
maxCanonicalLovelaceVal :: Word64
maxCanonicalLovelaceVal = 9e6

genChainDifficulty :: Gen ChainDifficulty
genChainDifficulty = ChainDifficulty <$> genBlockCount

@@ -1,5 +1,7 @@
module Test.Cardano.Chain.Delegation.Gen
( genCertificate
( genCanonicalCertificate
, genCertificate
, genCanonicalCertificateDistinctList
, genCertificateDistinctList
, genPayload
, genUndo
@@ -13,29 +15,48 @@ import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

import Cardano.Chain.Delegation (Certificate, Payload, Undo(..), unsafePayload)
import Cardano.Crypto (ProtocolMagicId, createPsk)
import Cardano.Chain.Slotting (EpochIndex(..))
import Cardano.Crypto (AProxySecretKey(..), ProtocolMagicId, createPsk)
import Data.List (nub)

import Test.Cardano.Chain.Common.Gen (genStakeholderId)
import Test.Cardano.Chain.Slotting.Gen (genEpochIndex)
import Test.Cardano.Crypto.Gen (genPublicKey, genSafeSigner)


genCanonicalCertificate :: ProtocolMagicId -> Gen Certificate
genCanonicalCertificate pm =
createPsk pm
<$> genSafeSigner
<*> genPublicKey
<*> (EpochIndex <$> Gen.word64 (Range.constant 0 1000000000000000))

genCertificate :: ProtocolMagicId -> Gen Certificate
genCertificate pm =
createPsk pm <$> genSafeSigner <*> genPublicKey <*> genEpochIndex

genCanonicalCertificateDistinctList :: ProtocolMagicId -> Gen [Certificate]
genCanonicalCertificateDistinctList pm = do
let pSKList = Gen.list (Range.linear 0 5) (genCanonicalCertificate pm)
noSelfSigningCerts <$> Gen.filter allDistinct pSKList
where
allDistinct :: Eq a => [a] -> Bool
allDistinct ls = length (nub ls) == length ls

noSelfSigningCerts :: [Certificate] -> [Certificate]
noSelfSigningCerts = filter (\x -> pskIssuerPk x /= pskDelegatePk x)

genCertificateDistinctList :: ProtocolMagicId -> Gen [Certificate]
genCertificateDistinctList pm = do
let
pSKList = Gen.list
(Range.linear 0 5)
(createPsk pm <$> genSafeSigner <*> genPublicKey <*> genEpochIndex)
Gen.filter allDistinct pSKList
let pSKList = Gen.list (Range.linear 0 5) (genCertificate pm)
noSelfSigningCerts <$> Gen.filter allDistinct pSKList
where
allDistinct :: Eq a => [a] -> Bool
allDistinct ls = length (nub ls) == length ls

noSelfSigningCerts :: [Certificate] -> [Certificate]
noSelfSigningCerts = filter (\x -> pskIssuerPk x /= pskDelegatePk x)

genPayload :: ProtocolMagicId -> Gen Payload
genPayload pm =
unsafePayload <$> Gen.list (Range.linear 0 5) (genCertificate pm)
@@ -5,10 +5,11 @@

module Test.Cardano.Chain.Genesis.Example
( exampleGenesisAvvmBalances
, exampleStaticConfig_GCSpec
, exampleStaticConfig_GCSrc
, exampleGenesisData0
, exampleGenesisDelegation
, exampleGenesisInitializer
, exampleStaticConfig_GCSpec
, exampleStaticConfig_GCSrc
)
where

@@ -18,20 +19,26 @@ import qualified Data.ByteString.Base16 as B16
import Data.Either (fromRight)
import qualified Data.Map.Strict as M
import Data.Maybe (fromJust)
import Data.Time (UTCTime(..), Day(..), secondsToDiffTime)

import Cardano.Binary.Class (Raw(..))
import Cardano.Chain.Common
( LovelacePortion(..)
( BlockCount(..)
, LovelacePortion(..)
, mkKnownLovelace
, mkKnownLovelacePortion
, mkStakeholderId
, StakeholderId
)
import Cardano.Chain.Genesis
( FakeAvvmOptions(..)
, GenesisNonAvvmBalances(..)
, GenesisAvvmBalances(..)
, GenesisData(..)
, GenesisDelegation(..)
, GenesisInitializer(..)
, GenesisSpec(..)
, GenesisWStakeholders(..)
, StaticConfig(..)
, TestnetBalanceOptions(..)
)
@@ -50,8 +57,14 @@ import Cardano.Crypto
import Cardano.Crypto.Signing (PublicKey(..))
import qualified Cardano.Crypto.Wallet as CC

import Test.Cardano.Chain.Common.Example
(exampleAddress, exampleAddress1, exampleStakeholderId)
import Test.Cardano.Chain.Update.Example (exampleProtocolParameters)
import Test.Cardano.Crypto.Bi (getBytes)
import Test.Cardano.Crypto.Example (exampleProtocolMagic0)

exampleBlockCount :: BlockCount
exampleBlockCount = BlockCount 12344

exampleStaticConfig_GCSrc :: StaticConfig
exampleStaticConfig_GCSrc =
@@ -78,6 +91,18 @@ exampleGenesisAvvmBalances = GenesisAvvmBalances
exampleRedeemPublicKey' (m, n) =
fromJust (fst <$> redeemDeterministicKeyGen (getBytes m n))

exampleGenesisData0 :: GenesisData
exampleGenesisData0 = GenesisData
{ gdBootStakeholders = exampleGenesisWStakeholders
, gdHeavyDelegation = exampleGenesisDelegation
, gdStartTime = exampleUTCTime0
, gdNonAvvmBalances = exampleGenesisNonAvvmBalances0
, gdProtocolParameters = exampleProtocolParameters
, gdK = exampleBlockCount
, gdProtocolMagic = exampleProtocolMagic0
, gdAvvmDistr = exampleGenesisAvvmBalances
}

exampleGenesisDelegation :: GenesisDelegation
exampleGenesisDelegation = UnsafeGenesisDelegation
(M.fromList
@@ -143,6 +168,34 @@ exampleGenesisInitializer = GenesisInitializer
, giSeed = 0
}

exampleGenesisNonAvvmBalances0 :: GenesisNonAvvmBalances
exampleGenesisNonAvvmBalances0 = GenesisNonAvvmBalances
{ getGenesisNonAvvmBalances = (M.fromList
[ (exampleAddress , coin)
, (exampleAddress1, coin1)
]
)
}
where
coin = mkKnownLovelace @36524597913081152
coin1 = mkKnownLovelace @37343863242999412

exampleGenesisWStakeholders :: GenesisWStakeholders
exampleGenesisWStakeholders =
let
mapSize :: Int
mapSize = 1
stakeholderIds :: [StakeholderId]
stakeholderIds = replicate mapSize exampleStakeholderId
word16s :: [Word16]
word16s = [1337]
in GenesisWStakeholders
{ getGenesisWStakeholders = M.fromList $ zip stakeholderIds word16s
}

exampleUTCTime0 :: UTCTime
exampleUTCTime0 = UTCTime (ModifiedJulianDay 10000) (secondsToDiffTime 82401)

hexToBS :: ByteString -> ByteString
hexToBS ts = case B16.decode ts of
(fullyDecoded, "") -> fullyDecoded
Oops, something went wrong.

0 comments on commit 8a69d93

Please sign in to comment.