-
Notifications
You must be signed in to change notification settings - Fork 214
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
Improve balanceTx error if UTxOAssumptions are broken #3994
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -49,14 +49,14 @@ import Cardano.Api | |||
, InAnyCardanoEra (..) | ||||
, IsCardanoEra (..) | ||||
, IsShelleyBasedEra (..) | ||||
, PaymentCredential (PaymentCredentialByKey) | ||||
, ShelleyBasedEra (..) | ||||
) | ||||
import Cardano.Api.Gen | ||||
( genAddressByron | ||||
, genAddressInEra | ||||
, genEncodingBoundaryLovelace | ||||
, genNetworkId | ||||
, genPaymentCredential | ||||
, genSignedValue | ||||
, genStakeAddressReference | ||||
, genTx | ||||
|
@@ -67,6 +67,7 @@ import Cardano.Api.Gen | |||
, genTxOutDatum | ||||
, genTxOutValue | ||||
, genValueForTxOut | ||||
, genVerificationKeyHash | ||||
, genWitnesses | ||||
) | ||||
import Cardano.Binary | ||||
|
@@ -488,6 +489,8 @@ import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut.Gen as TxOutGen | |||
import qualified Cardano.Wallet.Shelley.Compatibility as Compatibility | ||||
import qualified Cardano.Wallet.Write.ProtocolParameters as Write | ||||
import qualified Cardano.Wallet.Write.Tx as Write | ||||
import Cardano.Wallet.Write.UTxOAssumptions | ||||
( UTxOAssumptionViolation (UTxOAssumptionViolation) ) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong section |
||||
import qualified Codec.CBOR.Encoding as CBOR | ||||
import qualified Codec.CBOR.Write as CBOR | ||||
import qualified Data.ByteArray as BA | ||||
|
@@ -2238,6 +2241,12 @@ instance Show AnyChangeAddressGenWithState where | |||
show (AnyChangeAddressGenWithState (ChangeAddressGen gen _) s) = | ||||
show $ toLedger $ fst $ gen s | ||||
|
||||
instance Arbitrary AnyChangeAddressGenWithState where | ||||
arbitrary = elements | ||||
[ dummyByronChangeAddressGen | ||||
, dummyShelleyChangeAddressGen | ||||
] | ||||
|
||||
balanceTransactionSpec :: Spec | ||||
balanceTransactionSpec = describe "balanceTransaction" $ do | ||||
-- TODO: Create a test to show that datums are passed through... | ||||
|
@@ -2506,6 +2515,39 @@ balanceTransactionSpec = describe "balanceTransaction" $ do | |||
`shouldBe` | ||||
Left (ErrBalanceTxAssignRedeemers | ||||
(ErrAssignRedeemersTargetNotFound faultyRedeemer)) | ||||
|
||||
describe "when selected UTxOs violate the UTxOAssumptions" $ do | ||||
it "fails with ErrBalanceTxInternalError" $ property $ \changeAddrGen seed -> do | ||||
|
||||
let partialTx :: PartialTx Cardano.BabbageEra | ||||
partialTx = paymentPartialTx | ||||
[ TxOut dummyAddr | ||||
(TokenBundle.fromCoin (Coin 1_000_000)) | ||||
] | ||||
|
||||
let illconfiguredWallet = | ||||
Wallet' | ||||
AllKeyPaymentCredentials | ||||
(byronUtxo (replicate 3 (Coin 1_000_000))) | ||||
changeAddrGen | ||||
|
||||
|
||||
let res = | ||||
balanceTx | ||||
illconfiguredWallet | ||||
mockPParamsForBalancing | ||||
(dummyTimeTranslationWithHorizon horizon) | ||||
seed | ||||
partialTx | ||||
|
||||
res === Left | ||||
( ErrBalanceTxInternalError | ||||
( ErrUTxOViolatesAssumptions | ||||
( UTxOAssumptionViolation | ||||
(toLedger dummyBootstrapAddr) | ||||
"AllKeyPaymentCredentials"))) | ||||
|
||||
|
||||
where | ||||
mapFirst f (x:xs) = f x : xs | ||||
mapFirst _ [] = error "mapFirst: empty list" | ||||
|
@@ -2515,6 +2557,7 @@ balanceTransactionSpec = describe "balanceTransaction" $ do | |||
|
||||
wallet = mkTestWallet (utxo [Coin 5_000_000]) | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
-- Wallet with only small utxos, and enough of them to fill a tx in the | ||||
-- tests below. | ||||
dustWallet = mkTestWallet dustUTxO | ||||
|
@@ -2537,6 +2580,17 @@ balanceTransactionSpec = describe "balanceTransaction" $ do | |||
outs = map (TxOut dummyAddr . TokenBundle.fromCoin) coins | ||||
dummyHash = Hash $ B8.replicate 32 '0' | ||||
|
||||
dummyBootstrapAddr = case dummyByronChangeAddressGen of | ||||
AnyChangeAddressGenWithState g s0 -> | ||||
fst $ getChangeAddressGen g s0 | ||||
|
||||
byronUtxo coins = UTxO $ Map.fromList $ zip ins outs | ||||
where | ||||
ins = map (TxIn dummyHash) [0..] | ||||
outs = map (TxOut dummyBootstrapAddr . TokenBundle.fromCoin) coins | ||||
dummyHash = Hash $ B8.replicate 32 '0' | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
dummyAddr = Address $ unsafeFromHex | ||||
"60b1e5e0fb74c86c801f646841e07cdb42df8b82ef3ce4e57cb5412e77" | ||||
|
||||
|
@@ -3022,8 +3076,12 @@ instance Buildable Wallet' where | |||
nameF "Wallet" $ mconcat | ||||
[ nameF "assumptions" $ build assumptions | ||||
, nameF "changeAddressGen" $ build changeAddressGen | ||||
, nameF "utxo" $ pretty utxo | ||||
, nameF "utxo" $ build $ show $ fromWalletUTxO utxo | ||||
] | ||||
where | ||||
fromWalletUTxO (UTxO m) = | ||||
Map.mapKeys toLedger | ||||
$ Map.map toBabbageTxOut m | ||||
|
||||
instance Arbitrary Wallet' where | ||||
arbitrary = oneof | ||||
|
@@ -3041,7 +3099,8 @@ instance Arbitrary Wallet' where | |||
genShelleyVkAddr = Cardano.shelleyAddressInEra | ||||
<$> (Cardano.makeShelleyAddress | ||||
<$> genNetworkId | ||||
<*> genPaymentCredential -- only vk credentials | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was also generating script credentials |
||||
<*> (PaymentCredentialByKey | ||||
<$> genVerificationKeyHash Cardano.AsPaymentKey) | ||||
<*> genStakeAddressReference) | ||||
|
||||
genByronVkAddr :: Gen (Cardano.AddressInEra Cardano.BabbageEra) | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not actually an internal balanceTx error. In the HTTP API it is an internal wallet error, but not here, only looking at balanceTx. Will move.