Skip to content

Commit

Permalink
WIP Both tests and benchmarks compile.
Browse files Browse the repository at this point in the history
The Tx generator is generating transactions where ADA is not conserved. We always seem to off by a small number.
If you run this command
cabal test --test-show-details=streaming shelley-spec-ledger-test --test-options '(-p total amount of Ada is preserved)'
You can see the evidence. Lines like   (NOT the Same Coin 399927950 =/= Coin 399927951)  are printed by tracing
the generator. The file where I beleive the problem is:
cardano-ledger-specs/shelley/chain-and-ledger/shelley-spec-ledger-test/src/Test/Shelley/Spec/Ledger/Generator/Utxo.hs
  • Loading branch information
TimSheard committed Sep 16, 2020
1 parent e6867bf commit a97d509
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Expand Up @@ -103,6 +103,7 @@ test-suite shelley-spec-ledger-test
other-modules:
Test.Control.Iterate.SetAlgebra
Test.Shelley.Spec.Ledger.Address.Bootstrap
Test.Shelley.Spec.Ledger.ByronTranslation
Test.Shelley.Spec.Ledger.Examples
Test.Shelley.Spec.Ledger.Examples.Cast
Test.Shelley.Spec.Ledger.Examples.Combinators
Expand All @@ -119,7 +120,6 @@ test-suite shelley-spec-ledger-test
Test.Shelley.Spec.Ledger.NonTraceProperties.Generator
Test.Shelley.Spec.Ledger.NonTraceProperties.Mutator
Test.Shelley.Spec.Ledger.NonTraceProperties.PropertyTests
Test.Shelley.Spec.Ledger.NonTraceProperties.Serialization
Test.Shelley.Spec.Ledger.NonTraceProperties.Validity
Test.Shelley.Spec.Ledger.PropertyTests
Test.Shelley.Spec.Ledger.Rewards
Expand All @@ -137,6 +137,7 @@ test-suite shelley-spec-ledger-test
Test.Shelley.Spec.Ledger.Serialisation.Golden.Address
Test.Shelley.Spec.Ledger.Serialisation.Golden.Encoding
Test.Shelley.Spec.Ledger.Serialisation.Golden.Genesis
Test.Shelley.Spec.Ledger.Serialisation.StakeRef
Test.Shelley.Spec.Ledger.Serialisation.Tripping.CBOR
Test.Shelley.Spec.Ledger.Serialisation.Tripping.JSON
Test.Shelley.Spec.Ledger.STSTests
Expand Down
Expand Up @@ -69,6 +69,10 @@ import Shelley.Spec.Ledger.LedgerState
_dstate,
_ptrs,
_rewards,
_pstate,
_pParams,
consumed,
produced,
)
import Shelley.Spec.Ledger.MetaData (MetaDataHash)
import Shelley.Spec.Ledger.PParams (PParams, PParams' (..))
Expand Down Expand Up @@ -194,8 +198,11 @@ genTx
(remainderCoin,draftOutputs) = calcOutputsFromBalance spendingBalance outputAddrs draftFee
draftTxBody <- genTxBody inputs draftOutputs certs wdrls update draftFee ttl metadataHash
let draftTx = Tx draftTxBody (mkTxWits' draftTxBody) metadata
cs = consumed pparams utxo draftTxBody
ps = produced pparams (_pParams (_pstate dpState)) draftTxBody
-- We add now repeatedly add inputs until the process converges.
draftTx2 <- converge remainderCoin wits scripts keys' scripts' utxo pparams keySpace draftTx
draftTx2 <- converge remainderCoin wits scripts keys' scripts' utxo pparams keySpace
(if cs==ps then draftTx else (trace ("NOT the Same "++show ps++" =/= "++show cs) draftTx))

let fee = _txfee (_body draftTx2)
utxoSize = (Map.size . unUTxO) utxo
Expand Down
Expand Up @@ -133,7 +133,7 @@ initStPoolLifetime = initSt initUTxO
aliceCoinEx1 :: Coin
aliceCoinEx1 =
aliceInitCoin Val.~~ _poolDeposit ppEx
Val.~~ (Val.scale (3 :: Int) $ _keyDeposit ppEx)
Val.~~ (Val.scale (3 :: Integer) $ _keyDeposit ppEx)
Val.~~ Coin 3

carlMIR :: Coin
Expand Down Expand Up @@ -216,7 +216,7 @@ expectedStEx1 :: forall era. (Era era, ExMock (Crypto era)) => ChainState era
expectedStEx1 =
C.evolveNonceUnfrozen (getBlockNonce (blockEx1 @era))
. C.newLab blockEx1
. C.feesAndDeposits feeTx1 (Val.scale (3 :: Int) (_keyDeposit ppEx) <> _poolDeposit ppEx)
. C.feesAndDeposits feeTx1 (Val.scale (3 :: Integer) (_keyDeposit ppEx) <> _poolDeposit ppEx)
. C.newUTxO txbodyEx1
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo 10) 0 0)
. C.newStakeCred Cast.bobSHK (Ptr (SlotNo 10) 0 1)
Expand Down
Expand Up @@ -95,13 +95,20 @@ import Test.Shelley.Spec.Ledger.Generator.Core
( applyTxBody,
genesisCoins,
)
import Test.Shelley.Spec.Ledger.Generator.Utxo (splitCoin)
import Test.Shelley.Spec.Ledger.NonTraceProperties.Mutator
import Test.Shelley.Spec.Ledger.NonTraceProperties.Validity
import Test.Shelley.Spec.Ledger.Orphans ()
import Test.Shelley.Spec.Ledger.Utils
import Unsafe.Coerce

-- ==============================================================

splitCoin :: Coin -> Integer -> (Coin, Coin)
splitCoin (Coin n) 0 = (Coin 0, Coin n)
splitCoin (Coin n) m
| m <= 0 = error "must split coins into positive parts"
| otherwise = (Coin $ n `div` m, Coin $ n `rem` m)

-- | Find first matching key pair for address. Returns the matching key pair
-- where the first element of the pair matched the hash in 'addr'.
findPayKeyPair :: Era era => Addr era -> KeyPairs era -> KeyPair 'Payment era
Expand Down

0 comments on commit a97d509

Please sign in to comment.