Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 5f38668

Browse files
mhueschMichael Hueschen
authored and
Michael Hueschen
committed
[CO-354] Fix wallet-new "example1" test
This test was failing, apparently due to the increased sizes of `AddrAttributes` in the NMJust case. I added 4 bytes to the maximum byte limit, which is the amount by which `AddrAttributes` grows when the `NetworkMagic` value is added.
1 parent 16bb450 commit 5f38668

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

wallet-new/test/unit/Test/Infrastructure/Generator.hs

+19-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import UTxO.Generator
2323
import Wallet.Inductive
2424
import Wallet.Inductive.Generator
2525

26-
import Pos.Core ( TxSizeLinear, calculateTxSizeLinear )
26+
import Pos.Core (TxSizeLinear, calculateTxSizeLinear)
27+
import Pos.Crypto (RequiresNetworkMagic (..))
2728
import Serokell.Data.Memory.Units (Byte, fromBytes)
2829

2930
{-------------------------------------------------------------------------------
@@ -51,20 +52,21 @@ data GeneratorModel h a = GeneratorModel {
5152
, gmMaxNumOurs :: Int
5253

5354
-- | Estimate fees
54-
, gmEstimateFee :: Int -> [Value] -> Value
55+
, gmEstimateFee :: RequiresNetworkMagic -> Int -> [Value] -> Value
5556
}
5657

57-
genChainUsingModel :: (Hash h a, Ord a) => GeneratorModel h a -> Gen (Chain h a)
58-
genChainUsingModel GeneratorModel{..} =
58+
genChainUsingModel :: (Hash h a, Ord a)
59+
=> RequiresNetworkMagic -> GeneratorModel h a -> Gen (Chain h a)
60+
genChainUsingModel rnm GeneratorModel{..} =
5961
evalStateT (genChain params) initState
6062
where
61-
params = defChainParams gmEstimateFee gmAllAddresses
63+
params = defChainParams (gmEstimateFee rnm) gmAllAddresses
6264
initUtxo = utxoRestrictToAddr (`elem` gmAllAddresses) $ trUtxo gmBoot
6365
initState = initTrState initUtxo 1
6466

65-
genInductiveUsingModel :: (Hash h a, Ord a)
66-
=> GeneratorModel h a -> Gen (Inductive h a)
67-
genInductiveUsingModel GeneratorModel{..} = do
67+
genInductiveUsingModel :: (Hash h a, Ord a) => RequiresNetworkMagic
68+
-> GeneratorModel h a -> Gen (Inductive h a)
69+
genInductiveUsingModel rnm GeneratorModel{..} = do
6870
numOurs <- choose (1, min (length potentialOurs) gmMaxNumOurs)
6971
addrs' <- shuffle potentialOurs
7072
let ours = Set.fromList (take numOurs addrs')
@@ -76,7 +78,8 @@ genInductiveUsingModel GeneratorModel{..} = do
7678
}
7779
where
7880
potentialOurs = filter gmPotentialOurs gmAllAddresses
79-
params ours = defEventsParams gmEstimateFee gmAllAddresses ours initUtxo
81+
params ours =
82+
defEventsParams (gmEstimateFee rnm) gmAllAddresses ours initUtxo
8083
initUtxo = utxoRestrictToAddr (`elem` gmAllAddresses) $ trUtxo gmBoot
8184
initState = initEventsGlobalState 1
8285

@@ -91,7 +94,7 @@ simpleModel :: GeneratorModel GivenHash Char
9194
simpleModel = GeneratorModel {
9295
gmAllAddresses = addrs
9396
, gmPotentialOurs = \_ -> True
94-
, gmEstimateFee = \_ _ -> 0
97+
, gmEstimateFee = \_ _ _ -> 0
9598
, gmMaxNumOurs = 3
9699
, gmBoot = Transaction {
97100
trFresh = fromIntegral (length addrs) * initBal
@@ -176,9 +179,12 @@ estimateSize saa sta ins outs
176179
-- NOTE: The average size of @Attributes AddrAttributes@ and
177180
-- the transaction attributes @Attributes ()@ are both hard-coded
178181
-- here with some (hopefully) realistic values.
179-
estimateCardanoFee :: TxSizeLinear -> Int -> [Value] -> Value
180-
estimateCardanoFee linearFeePolicy ins outs
181-
= round (calculateTxSizeLinear linearFeePolicy (estimateSize 128 16 ins outs))
182+
estimateCardanoFee :: TxSizeLinear -> RequiresNetworkMagic -> Int -> [Value] -> Value
183+
estimateCardanoFee linearFeePolicy rnm ins outs
184+
= round (calculateTxSizeLinear linearFeePolicy (estimateSize addrAttrSize 16 ins outs))
185+
where
186+
addrAttrSize = 128 + (case rnm of NMMustBeNothing -> 0
187+
NMMustBeJust -> 4)
182188

183189
{-------------------------------------------------------------------------------
184190
Auxiliary

wallet-new/test/unit/Test/Infrastructure/Genesis.hs

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import UTxO.Context
1111
import UTxO.DSL
1212

1313
import Pos.Core (TxSizeLinear)
14+
import Pos.Crypto (RequiresNetworkMagic)
1415
import Test.Infrastructure.Generator (estimateCardanoFee)
1516

1617
{-------------------------------------------------------------------------------
@@ -35,12 +36,13 @@ data GenesisValues h = GenesisValues {
3536
, hashBoot :: h (Transaction h Addr)
3637

3738
-- | Fee policy
38-
, txFee :: Int -> [Value] -> Value
39+
, txFee :: Int -> [Value] -> Value
3940
}
4041

4142
-- | Compute genesis values from the bootstrap transaction
42-
genesisValues :: (Hash h Addr) => TxSizeLinear -> Transaction h Addr -> GenesisValues h
43-
genesisValues txSizeLinear boot@Transaction{..} = GenesisValues{..}
43+
genesisValues :: (Hash h Addr) => TxSizeLinear
44+
-> RequiresNetworkMagic -> Transaction h Addr -> GenesisValues h
45+
genesisValues txSizeLinear rnm boot@Transaction{..} = GenesisValues{..}
4446
where
4547
initR0 = unsafeHead [val | Output a val <- trOuts, a == r0]
4648

@@ -52,7 +54,7 @@ genesisValues txSizeLinear boot@Transaction{..} = GenesisValues{..}
5254

5355
hashBoot = hash boot
5456

55-
txFee = estimateCardanoFee txSizeLinear
57+
txFee = estimateCardanoFee txSizeLinear rnm
5658

5759
{-------------------------------------------------------------------------------
5860
Auxiliary

wallet-new/test/unit/Test/Spec/Kernel.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ runWithMagic rnm = do
4343
specBody :: ProtocolMagic -> Spec
4444
specBody pm =
4545
it "Compare wallet kernel to pure model" $
46-
forAll (genInductiveUsingModel model) $ \ind -> do
46+
let rnm = getRequiresNetworkMagic pm in
47+
forAll (genInductiveUsingModel rnm model) $ \ind -> do
4748
-- TODO: remove once we have support for rollback in the kernel
4849
let indDontRoll = uptoFirstRollback ind
4950
bracketActiveWallet pm $ \activeWallet -> do

wallet-new/test/unit/Test/Spec/Models.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ runWithMagic rnm = do
4545
specBody :: ProtocolMagic -> Spec
4646
specBody pm =
4747
describe "Test pure wallets" $ do
48+
let rnm = getRequiresNetworkMagic pm
4849
it "Using simple model" $
49-
forAll (genInductiveUsingModel simpleModel) $ testPureWalletWith
50+
forAll (genInductiveUsingModel rnm simpleModel) $ testPureWalletWith
5051
it "Using Cardano model" $
51-
forAll (genInductiveUsingModel (cardanoModel linearFeePolicy boot)) $ testPureWalletWith
52+
forAll (genInductiveUsingModel rnm (cardanoModel linearFeePolicy boot)) $ testPureWalletWith
5253
where
5354
transCtxt = runTranslateNoErrors pm ask
5455
boot = bootstrapTransaction transCtxt

wallet-new/test/unit/Test/Spec/Translation.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ specBody pm = do
6262

6363
describe "Translation QuickCheck tests" $ do
6464
prop "can translate randomly generated chains" $
65+
let rnm = getRequiresNetworkMagic pm in
6566
forAll
66-
(intAndVerifyGen pm (genChainUsingModel . cardanoModel linearFeePolicy))
67+
(intAndVerifyGen pm (genChainUsingModel rnm . cardanoModel linearFeePolicy))
6768
expectValid
6869

6970
where
@@ -188,7 +189,9 @@ intAndVerifyPure :: ProtocolMagic
188189
-> (GenesisValues GivenHash -> Chain GivenHash Addr)
189190
-> ValidationResult GivenHash Addr
190191
intAndVerifyPure pm txSizeLinear pc = runIdentity $
191-
intAndVerify pm (Identity . pc . genesisValues txSizeLinear)
192+
intAndVerify pm (Identity . pc . genesisValues txSizeLinear rnm)
193+
where
194+
rnm = getRequiresNetworkMagic pm
192195

193196
-- | Specialization of 'intAndVerify' to 'Gen'
194197
intAndVerifyGen :: ProtocolMagic -> (Transaction GivenHash Addr

0 commit comments

Comments
 (0)