Skip to content

Commit

Permalink
Avoid KeyWitnessCount overestimation for delegation script credentials
Browse files Browse the repository at this point in the history
Main idea is that we're taking the sum of
1. Key witness count needed for key credentials
2. Key witness count needed for (native) scripts

Unfortunately, this commit also breaks an integration test: #3830 (comment)
as `signTransaction` adds too many witnesses — or at least more than
what is needed and more than what we made room for when balancing. Next
commit will fix this by "making more room" by increasing the estimated
`KeyWitnessCount` in a more proper way.
  • Loading branch information
Anviking authored and paweljakubas committed Apr 14, 2023
1 parent d303c5c commit 4fdd150
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/wallet/src/Cardano/Wallet/Shelley/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,31 +1212,22 @@ estimateKeyWitnessCount utxo txbody@(Cardano.TxBody txbodycontent) =

dummyKeyRole = Payment

apiScriptHashes =
let walletScripts = mapMaybe toTimelockScript scripts
toScriptHash =
Cardano.hashScript
. Cardano.SimpleScript
. toCardanoSimpleScript
toPair s = (s, toScriptHash s)
in toPair <$> walletScripts

estimateWitNumForCred = \case
Cardano.StakeCredentialByKey _ -> 1
Cardano.StakeCredentialByScript scriptHash ->
let pair = filter (\(_,sh) -> sh == scriptHash) apiScriptHashes
in case pair of
[(delScript,_)] ->
estimateMaxWitnessRequiredPerInput delScript
_ -> error "there should be delegation script in the tx body"

estimateDelegSigningKeys :: Cardano.Certificate -> Integer
estimateDelegSigningKeys = \case
Cardano.StakeAddressRegistrationCertificate _ -> 0
Cardano.StakeAddressDeregistrationCertificate cred ->
estimateWitNumForCred cred
Cardano.StakeAddressDelegationCertificate cred _ ->
estimateWitNumForCred cred
_ -> 1
where
-- Does not include the key witness needed for script credentials.
-- They are accounted for separately in @scriptVkWitsUpperBound@.
estimateWitNumForCred = \case
Cardano.StakeCredentialByKey _ -> 1
Cardano.StakeCredentialByScript _ -> 0


toTimelockScript
:: Ledger.Script (Cardano.ShelleyLedgerEra era)
Expand Down

0 comments on commit 4fdd150

Please sign in to comment.