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

Refactor witnesses indexing functions to have the indexing logic in one place #697

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 34 additions & 65 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,13 @@ substituteExecutionUnits
redeemer
exunits

adjustScriptWitness
:: (ScriptWitness witctx era -> Either (TxBodyErrorAutoBalance era) (ScriptWitness witctx era))
-> Witness witctx era
-> Either (TxBodyErrorAutoBalance era) (Witness witctx era)
adjustScriptWitness _ (KeyWitness ctx) = Right $ KeyWitness ctx
adjustScriptWitness g (ScriptWitness ctx witness') = ScriptWitness ctx <$> g witness'

mapScriptWitnessesTxIns
:: [(TxIn, BuildTxWith BuildTx (Witness WitCtxTxIn era))]
-> Either (TxBodyErrorAutoBalance era) [(TxIn, BuildTxWith BuildTx (Witness WitCtxTxIn era))]
Expand All @@ -1461,27 +1468,18 @@ substituteExecutionUnits
]
mappedScriptWitnesses =
[ (txin, BuildTxWith <$> wit')
| -- The tx ins are indexed in the map order by txid
(ix, (txin, BuildTxWith wit)) <- zip [0 ..] (orderTxIns txins)
, let wit' = case wit of
KeyWitness{} -> Right wit
ScriptWitness ctx witness -> ScriptWitness ctx <$> witness'
where
witness' = substituteExecUnits (ScriptWitnessIndexTxIn ix) witness
| (ix, txin, wit) <- indexTxIns txins
, let wit' = adjustScriptWitness (substituteExecUnits ix) wit
]
in traverse
( \(txIn, eWitness) ->
case eWitness of
Left e -> Left e
Right wit -> Right (txIn, wit)
)
(\(txIn, eWitness) -> (txIn,) <$> eWitness)
mappedScriptWitnesses

mapScriptWitnessesWithdrawals
:: TxWithdrawals BuildTx era
-> Either (TxBodyErrorAutoBalance era) (TxWithdrawals BuildTx era)
mapScriptWitnessesWithdrawals TxWithdrawalsNone = Right TxWithdrawalsNone
mapScriptWitnessesWithdrawals (TxWithdrawals supported withdrawals) =
mapScriptWitnessesWithdrawals txWithdrawals'@(TxWithdrawals supported _) =
let mappedWithdrawals
:: [ ( StakeAddress
, L.Coin
Expand All @@ -1490,55 +1488,30 @@ substituteExecutionUnits
]
mappedWithdrawals =
[ (addr, withdrawal, BuildTxWith <$> mappedWitness)
| -- The withdrawals are indexed in the map order by stake credential
(ix, (addr, withdrawal, BuildTxWith wit)) <- zip [0 ..] (orderStakeAddrs withdrawals)
, let mappedWitness = adjustWitness (substituteExecUnits (ScriptWitnessIndexWithdrawal ix)) wit
| (ix, addr, withdrawal, wit) <- indexTxWithdrawals txWithdrawals'
, let mappedWitness = adjustScriptWitness (substituteExecUnits ix) wit
]
in TxWithdrawals supported
<$> traverse
( \(sAddr, ll, eWitness) ->
case eWitness of
Left e -> Left e
Right wit -> Right (sAddr, ll, wit)
)
(\(sAddr, ll, eWitness) -> (sAddr,ll,) <$> eWitness)
mappedWithdrawals
where
adjustWitness
:: (ScriptWitness witctx era -> Either (TxBodyErrorAutoBalance era) (ScriptWitness witctx era))
-> Witness witctx era
-> Either (TxBodyErrorAutoBalance era) (Witness witctx era)
adjustWitness _ (KeyWitness ctx) = Right $ KeyWitness ctx
adjustWitness g (ScriptWitness ctx witness') = ScriptWitness ctx <$> g witness'

mapScriptWitnessesCertificates
:: TxCertificates BuildTx era
-> Either (TxBodyErrorAutoBalance era) (TxCertificates BuildTx era)
mapScriptWitnessesCertificates TxCertificatesNone = Right TxCertificatesNone
mapScriptWitnessesCertificates
( TxCertificates
supported
certs
(BuildTxWith witnesses)
) =
let mappedScriptWitnesses
:: [(StakeCredential, Either (TxBodyErrorAutoBalance era) (Witness WitCtxStake era))]
mappedScriptWitnesses =
[ (stakecred, ScriptWitness ctx <$> witness')
| -- The certs are indexed in list order
(ix, cert) <- zip [0 ..] certs
, stakecred <- maybeToList (selectStakeCredentialWitness cert)
, ScriptWitness ctx witness <-
maybeToList (List.lookup stakecred witnesses)
, let witness' = substituteExecUnits (ScriptWitnessIndexCertificate ix) witness
]
in TxCertificates supported certs . BuildTxWith
<$> traverse
( \(sCred, eScriptWitness) ->
case eScriptWitness of
Left e -> Left e
Right wit -> Right (sCred, wit)
)
mappedScriptWitnesses
mapScriptWitnessesCertificates txCertificates'@(TxCertificates supported certs _) =
let mappedScriptWitnesses
:: [(StakeCredential, Either (TxBodyErrorAutoBalance era) (Witness WitCtxStake era))]
mappedScriptWitnesses =
[ (stakeCred, witness')
| (ix, _, stakeCred, witness) <- indexTxCertificates txCertificates'
, let witness' = adjustScriptWitness (substituteExecUnits ix) witness
]
in TxCertificates supported certs . BuildTxWith
<$> traverse
(\(sCred, eScriptWitness) -> (sCred,) <$> eScriptWitness)
mappedScriptWitnesses

mapScriptWitnessesVotes
:: Maybe (Featured ConwayEraOnwards era (TxVotingProcedures build era))
Expand All @@ -1548,13 +1521,11 @@ substituteExecutionUnits
mapScriptWitnessesVotes Nothing = return Nothing
mapScriptWitnessesVotes (Just (Featured _ TxVotingProceduresNone)) = return Nothing
mapScriptWitnessesVotes (Just (Featured _ (TxVotingProcedures _ ViewTx))) = return Nothing
mapScriptWitnessesVotes (Just (Featured era (TxVotingProcedures vProcedures (BuildTxWith sWitMap)))) = do
mapScriptWitnessesVotes (Just (Featured era txVotingProcedures'@(TxVotingProcedures vProcedures (BuildTxWith _)))) = do
let eSubstitutedExecutionUnits =
[ (vote, updatedWitness)
| let allVoteMap = L.unVotingProcedures vProcedures
, (vote, scriptWitness) <- toList sWitMap
, index <- maybeToList $ Map.lookupIndex vote allVoteMap
, let updatedWitness = substituteExecUnits (ScriptWitnessIndexVoting $ fromIntegral index) scriptWitness
| (ix, vote, witness) <- indexTxVotingProcedures txVotingProcedures'
, let updatedWitness = substituteExecUnits ix witness
]

substitutedExecutionUnits <- traverseScriptWitnesses eSubstitutedExecutionUnits
Expand All @@ -1571,13 +1542,11 @@ substituteExecutionUnits
mapScriptWitnessesProposals Nothing = return Nothing
mapScriptWitnessesProposals (Just (Featured _ TxProposalProceduresNone)) = return Nothing
mapScriptWitnessesProposals (Just (Featured _ (TxProposalProcedures _ ViewTx))) = return Nothing
mapScriptWitnessesProposals (Just (Featured era txpp@(TxProposalProcedures osetProposalProcedures (BuildTxWith sWitMap)))) = do
let allProposalsList = toList $ convProposalProcedures txpp
eSubstitutedExecutionUnits =
mapScriptWitnessesProposals (Just (Featured era txpp@(TxProposalProcedures osetProposalProcedures (BuildTxWith _)))) = do
let eSubstitutedExecutionUnits =
[ (proposal, updatedWitness)
| (proposal, scriptWitness) <- toList sWitMap
, index <- maybeToList $ List.elemIndex proposal allProposalsList
, let updatedWitness = substituteExecUnits (ScriptWitnessIndexProposing $ fromIntegral index) scriptWitness
| (ix, proposal, scriptWitness) <- indexTxProposalProcedures txpp
, let updatedWitness = substituteExecUnits ix scriptWitness
]

substitutedExecutionUnits <- traverseScriptWitnesses eSubstitutedExecutionUnits
Expand All @@ -1596,7 +1565,7 @@ substituteExecutionUnits
mapScriptWitnessesMinting txMintValue'@(TxMintValue w _) = do
let mappedScriptWitnesses =
[ (policyId, pure . (assetName',quantity,) <$> substitutedWitness)
| (ix, policyId, assetName', quantity, BuildTxWith witness) <- txMintValueToIndexed txMintValue'
| (ix, policyId, assetName', quantity, BuildTxWith witness) <- indexTxMintValue txMintValue'
, let substitutedWitness = BuildTxWith <$> substituteExecUnits ix witness
]
final <- Map.fromListWith (<>) <$> traverseScriptWitnesses mappedScriptWitnesses
Expand Down
34 changes: 17 additions & 17 deletions cardano-api/internal/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,23 @@ data SimpleScriptOrReferenceInput lang
| SReferenceScript TxIn
deriving (Eq, Show)

-- ----------------------------------------------------------------------------
-- The kind of witness to use, key (signature) or script
--

data Witness witctx era where
KeyWitness
:: KeyWitnessInCtx witctx
-> Witness witctx era
ScriptWitness
:: ScriptWitnessInCtx witctx
-> ScriptWitness witctx era
-> Witness witctx era

deriving instance Eq (Witness witctx era)

deriving instance Show (Witness witctx era)

-- | A /use/ of a script within a transaction body to witness that something is
-- being used in an authorised manner. That can be
--
Expand Down Expand Up @@ -797,23 +814,6 @@ getScriptWitnessReferenceInputOrScript = \case
PlutusScriptWitness _ _ (PReferenceScript txIn) _ _ _ ->
Right txIn

-- ----------------------------------------------------------------------------
-- The kind of witness to use, key (signature) or script
--

data Witness witctx era where
KeyWitness
:: KeyWitnessInCtx witctx
-> Witness witctx era
ScriptWitness
:: ScriptWitnessInCtx witctx
-> ScriptWitness witctx era
-> Witness witctx era

deriving instance Eq (Witness witctx era)

deriving instance Show (Witness witctx era)

data KeyWitnessInCtx witctx where
KeyWitnessForSpending :: KeyWitnessInCtx WitCtxTxIn
KeyWitnessForStakeAddr :: KeyWitnessInCtx WitCtxStake
Expand Down
Loading
Loading