Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize over what it means to be an Asset (Coin v.s. Value) #1815

Closed
wants to merge 3 commits into from

Conversation

TimSheard
Copy link
Contributor

This PR parameterizes over what kind of assets a transaction can produce. In the old style we used a Coin, and as an alternative to that we introduce the type Value (which is a kind of multi-asset). Both these types are instances of the Val class, so the code has been made parametric over objects in the Val class.

sgActiveSlotCoeff =
mkActiveSlotCoeff
. unitIntervalFromRational
. sgActiveSlotsCoeff

instance Crypto crypto => ToJSON (ShelleyGenesis crypto) where
-- TODO JSON for v?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this TODO

txsize = fromIntegral . BSL.length . txFullBytes

-- | Convenience Function to bound the txsize function.
-- | It can be helpful for coin selection.
txsizeBound :: forall crypto. (Crypto crypto) => Tx crypto -> Integer
txsizeBound :: forall crypto v. (CV crypto v) => Tx crypto v -> Integer
txsizeBound tx = numInputs * inputSize + numOutputs * outputSize + rest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might be able to get rid of this function altogether. which would mean we could also clean up the transaction type (it stores the input and output bytes so that txsizeBound can remove them). we can do this in a separate PR.

Coin
keyRefunds pp tx = (_keyDeposit pp) * (fromIntegral $ length deregistrations)
where
deregistrations = filter isDeRegKey (toList $ _certs tx)

-- TODO add forge
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to add all these TODOs in the code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a github issue would be more helpful

@@ -159,20 +159,20 @@ instance
PostCondition
"Deposit pot must not be negative (post)"
(\_ st' -> _deposited st' >= 0),
let utxoBalance us = _deposited us + _fees us + balance (_utxo us)
let utxoBalance us = vplus (vinject $ _deposited us + _fees us) (balance (_utxo us))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use <> to make it a bit more like the original?

@@ -301,11 +304,18 @@ utxoInductive = do
-- process Protocol Parameter Update Proposals
ppup' <- trans @(PPUP crypto) $ TRC (PPUPEnv slot pp genDelegs, ppup, txup tx)

let outputs = Set.toList (eval (rng (txouts txb)))
-- TODO check voper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove this? or do we need to check something?

-- TODO add forge errors
-- let (Value vls) = _forge txb
-- let cids = Map.keys vls
-- all (adaID /=) cids ?! (ForgingAda (Value vls))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this and make an issue instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more to do with actually adding forging functionality than just doing the parametrization. Can definitely be removed from here.


deriving via
AllowThunksIn
'[ "txWitsBytes"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we still want the annotations to be thunks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We couldnt get deriving via to work with AllowThunksIn because of lack of ability to derive Generic for everything in Val. Is this essential?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the field is lazy but the instance doesn't mark it as allowed, this will trip thunk detection.

If the field is made strict, this will trigger serialization every time a witness set is modified.

One solution would be to figure out a way to get a generic instance for the val. Another would be to write a manual instance.

CV crypto v =>
{-# UNPACK #-} !BSS.ShortByteString ->
v ->
TxOut crypto v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems we can no longer unpack the Word64 from the coin if we want this to be parametric, so the UTxO memory size will be a bit bigger (one word per TxOut?). we can maybe make an issue to measure the difference.

instance Crypto c => HashAnnotated (TxBody c) c
instance CV c v => HashAnnotated (TxBody c v) c

-- TODO no thunks in bodyBytes is ok?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a good point, I think we want the annotations to be thunks

case decompactAddr bs of
Just (_ :: Addr crypto) -> pure $ TxOutCompact bs coin
Nothing -> cborError $ DecoderErrorCustom "TxOut" "invalid address"
addr <- fromCBOR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need to check that the address parses with decompactAddr

balance :: UTxO crypto -> Coin
balance (UTxO utxo) = fromIntegral $ Map.foldl' addCoins 0 utxo
balance :: CV crypto v => UTxO crypto v -> v
balance (UTxO utxo) = foldr addCoins vzero utxo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foldl'?

where
addCoins !b (TxOutCompact _ a) = a + b
addCoins (TxOut _ a) b = vplus a b
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still want the real constructor TxOutCompact. and maybe <> would be more clear

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, please don't undo #1771.

(Maybe you started working on this with an out-of-date checkout, one that didn't include that PR, and this change got lost while merging/rebasing? If so, we should be careful that no other such changes are in this PR.)

[cert | cert@(DCertPool (RegPool pparams)) <- toList (_certs txb), rewardAcntUsesScript (_poolRAcnt pparams)]
mirCertsUsingScripts =
[cert | cert@(DCertMir mir) <- toList (_certs txb), any credentialUsesScript (Map.keys (mirRewards mir))]
-}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

maxReserves = 10000000,
genTxRetries = 6,
genTxBudget = 10000
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not indent so that we can see exactly what changed

@@ -58,7 +58,8 @@ import Cardano.Crypto.DSIGN.Class (DSIGNAlgorithm (..))
import Cardano.Crypto.VRF (evalCertified)
import qualified Cardano.Crypto.Hash as Hash
import Control.Iterate.SetAlgebra (eval, (∪), (⋪))
import Control.Monad (replicateM)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mystery line! :)

[ (frequencyTxWithMetaData, genMetaData'),
(100 - frequencyTxWithMetaData, pure (SNothing, SNothing))
]
pure(a,b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this doesn't actually change anything

@@ -150,7 +150,7 @@ genDCerts ::
([KeyPair 'Witness c], [(MultiSig c, MultiSig c)])
)
genDCerts
ge@( GenEnv
ge@( GenEnv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this

pure (Nothing, [])
)
]
pure(a,b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not needed

@@ -57,18 +62,18 @@ import Shelley.Spec.Ledger.LedgerState
DState (..),
KeyPairs,
UTxOState (..),
minfeeBound,
minfee,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@@ -82,6 +87,8 @@ import Shelley.Spec.Ledger.UTxO
makeWitnessesFromScriptKeys,
makeWitnessesVKey,
)
-- import Shelley.Spec.Ledger.Value (Val (..))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove

@@ -173,40 +179,219 @@ genTxRetry
-- SpendingBalance, Output Addresses (including some Pointer addresses)
-- and a Outputs builder that distributes the given balance over addresses.
-------------------------------------------------------------------------
let spendingBalance = spendingBalanceUtxo + (sum (snd <$> wdrls)) - deposits + refunds
let spendingBalance = vplus spendingBalanceUtxo (vinject $ (sum (snd <$> wdrls)) - deposits + refunds)
n = if (Map.size . unUTxO) utxo < 100 then 3 else 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add this 100 to the constants or reuse another constant...

genWrdls (Map.toList wdrls)
)
]
pure ( a, b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another one that didn't change?

MD.I <$> arbitrary,
MD.B <$> arbitrary,
MD.S <$> (T.pack <$> arbitrary)
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just indendation?

RequireAllOf <$> resize maxMultiSigListLens (listOf (sizedMultiSig (n-1))),
RequireAnyOf <$> resize maxMultiSigListLens (listOf (sizedMultiSig (n-1))),
RequireMOf <$> arbitrary <*> resize maxMultiSigListLens (listOf (sizedMultiSig (n-1)))
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just indentation

import Test.Shelley.Spec.Ledger.Utils (mkHash)

genShelleyGenesis :: Crypto c => Gen (ShelleyGenesis c)
-- TODO this is temporary until I look through the Hedgehog docs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this now?

Copy link
Contributor

@JaredCorduan JaredCorduan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this massive undertaking!! All in all it looks good to me, but I left several comments, but it's all small stuff.

…not consistent between different

parts of the generated code. Slight refactoring to make the changes widely useable.
nc6 added a commit that referenced this pull request Sep 2, 2020
This commit only adds the class and an instance for `Coin`. It differs
from #1815 in the following ways:

- Class methods are now intended for qualified import (as per Michael's
comment in #1803)
- Added some additional documentation. Some functions still need
significantly more documentation.
- Use the `Semigroup`, `Monoid` and `Group` superclasses. This removes
the need for various functions defined in this class.
- Removed the instance for `Integer` and define the instance for `Coin`
directly.
nc6 added a commit that referenced this pull request Sep 2, 2020
This commit only adds the class and an instance for `Coin`. It differs
from #1815 in the following ways:

- Class methods are now intended for qualified import (as per Michael's
comment in #1803)
- Added some additional documentation. Some functions still need
significantly more documentation.
- Use the `Semigroup`, `Monoid` and `Group` superclasses. This removes
the need for various functions defined in this class.
- Removed the instance for `Integer` and define the instance for `Coin`
directly.
polinavino added a commit that referenced this pull request Sep 2, 2020
nc6 added a commit that referenced this pull request Sep 3, 2020
This builds upon PR #1830, so the first commit should be reviewed in
that PR.

Adds the `Value` type for Shelley-MA lifted from PR #1815 (and adjusted
following changes in the ts/eras branch).

Some changes are needed:
- The documentation is lifted from Plutus and is incorrect in some
detail; it should be updated.
- Size computation should be checked.
@nc6 nc6 mentioned this pull request Sep 3, 2020
polinavino pushed a commit that referenced this pull request Sep 4, 2020
This builds upon PR #1830, so the first commit should be reviewed in
that PR.

Adds the `Value` type for Shelley-MA lifted from PR #1815 (and adjusted
following changes in the ts/eras branch).

Some changes are needed:
- The documentation is lifted from Plutus and is incorrect in some
detail; it should be updated.
- Size computation should be checked.
@redxaxder redxaxder deleted the fixed-point-gentx branch April 20, 2021 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants