Skip to content

Commit

Permalink
Try #2967:
Browse files Browse the repository at this point in the history
  • Loading branch information
iohk-bors[bot] committed Oct 15, 2021
2 parents b3db604 + fc86190 commit 27c709c
Show file tree
Hide file tree
Showing 33 changed files with 3,947 additions and 3,285 deletions.
1 change: 1 addition & 0 deletions lib/core-integration/cardano-wallet-core-integration.cabal
Expand Up @@ -35,6 +35,7 @@ library
, base
, base58-bytestring
, bech32
, bech32-th
, bytestring
, cardano-api
, cardano-addresses
Expand Down
Expand Up @@ -21,6 +21,8 @@ import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Transaction
( DelegationAction )
import Data.ByteString
( ByteString )
import Data.IORef
( IORef )
import Data.Text
Expand Down Expand Up @@ -51,6 +53,10 @@ data Context = Context
, _faucet
:: Faucet
-- ^ Provides access to funded wallets.
, _moveRewardsToScript
:: (ByteString, Coin)
-> IO ()
-- ^ A function to inject rewards into some stake address.
, _feeEstimator :: TxDescription -> (Natural, Natural)
-- ^ A fee estimator.
, _networkParameters :: NetworkParameters
Expand Down
26 changes: 22 additions & 4 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Expand Up @@ -84,6 +84,7 @@ module Test.Integration.Framework.DSL
, getSharedWalletKey
, postAccountKeyShared
, getAccountKeyShared
, getSomeVerificationKey

-- * Wallet helpers
, listFilteredWallets
Expand Down Expand Up @@ -249,6 +250,7 @@ import Cardano.Wallet.Api.Types
, ApiTxId (ApiTxId)
, ApiUtxoStatistics (..)
, ApiVerificationKeyShared
, ApiVerificationKeyShelley (..)
, ApiWallet
, ApiWalletDelegation (..)
, ApiWalletDelegationNext (..)
Expand Down Expand Up @@ -333,6 +335,8 @@ import Control.Retry
( capDelay, constantDelay, retrying )
import Crypto.Hash
( Blake2b_160, Digest, digestFromByteString )
import Crypto.Hash.Utils
( blake2b224 )
import Data.Aeson
( FromJSON, ToJSON, Value, (.=) )
import Data.Aeson.QQ
Expand Down Expand Up @@ -1558,13 +1562,13 @@ getSharedWalletKey
-> DerivationIndex
-> Maybe Bool
-> m (HTTP.Status, Either RequestException ApiVerificationKeyShared)
getSharedWalletKey ctx wal role ix hashed =
getSharedWalletKey ctx wal role ix isHashed =
case wal of
ApiSharedWallet (Left wal') -> r wal'
ApiSharedWallet (Right wal') -> r wal'
where
r :: forall w. HasType (ApiT WalletId) w => w -> m (HTTP.Status, Either RequestException ApiVerificationKeyShared)
r w = request @ApiVerificationKeyShared ctx (Link.getWalletKey @'Shared w role ix hashed) Default Empty
r w = request @ApiVerificationKeyShared ctx (Link.getWalletKey @'Shared w role ix isHashed) Default Empty

postAccountKeyShared
:: forall m.
Expand Down Expand Up @@ -1594,13 +1598,27 @@ getAccountKeyShared
-> ApiSharedWallet
-> Maybe KeyFormat
-> m (HTTP.Status, Either RequestException ApiAccountKeyShared)
getAccountKeyShared ctx wal hashed =
getAccountKeyShared ctx wal isHashed =
case wal of
ApiSharedWallet (Left wal') -> r wal'
ApiSharedWallet (Right wal') -> r wal'
where
r :: forall w. HasType (ApiT WalletId) w => w -> m (HTTP.Status, Either RequestException ApiAccountKeyShared)
r w = request @ApiAccountKeyShared ctx (Link.getAccountKey @'Shared w hashed) Default Empty
r w = request @ApiAccountKeyShared ctx (Link.getAccountKey @'Shared w isHashed) Default Empty

getSomeVerificationKey
:: forall m.
( MonadIO m
, MonadUnliftIO m
)
=> Context
-> ApiWallet
-> m (ApiVerificationKeyShelley, ApiT (Hash "VerificationKey"))
getSomeVerificationKey ctx w = do
let link = Link.getWalletKey @'Shelley w UtxoExternal (DerivationIndex 0) Nothing
(_, vk@(ApiVerificationKeyShelley (bytes, _) _)) <-
unsafeRequest @ApiVerificationKeyShelley ctx link Empty
pure (vk, ApiT $ Hash $ blake2b224 @ByteString bytes)

patchEndpointEnding :: CredentialType -> Text
patchEndpointEnding = \case
Expand Down
240 changes: 235 additions & 5 deletions lib/core-integration/src/Test/Integration/Plutus.hs

Large diffs are not rendered by default.

Expand Up @@ -49,6 +49,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Tx
( SealedTx
, TxStatus (..)
Expand All @@ -57,9 +59,11 @@ import Cardano.Wallet.Primitive.Types.Tx
, sealedTxFromCardanoBody
)
import Cardano.Wallet.Unsafe
( unsafeMkMnemonic )
( unsafeFromHex, unsafeMkMnemonic )
import Control.Arrow
( second )
import Control.Monad
( foldM_, forM_ )
import Control.Monad.IO.Unlift
( MonadIO (..), MonadUnliftIO (..), liftIO )
import Control.Monad.Trans.Resource
Expand Down Expand Up @@ -110,6 +114,7 @@ import Test.Integration.Framework.DSL
, fixtureWalletWith
, fixtureWalletWithMnemonics
, getFromResponse
, getSomeVerificationKey
, json
, listAddresses
, minUTxOValue
Expand All @@ -120,6 +125,7 @@ import Test.Integration.Framework.DSL
, submitTx
, unsafeRequest
, verify
, waitForNextEpoch
, waitForTxImmutability
)
import Test.Integration.Framework.TestData
Expand All @@ -144,6 +150,7 @@ import qualified Data.Aeson as Aeson
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Integration.Plutus as PlutusScenario

Expand Down Expand Up @@ -1197,34 +1204,63 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
]

describe "Plutus scenarios" $ do
-- NOTE: This test scenario is currently unreliable because of the way
-- the redeemer pointers work. Redeemers are identified by pointers into
-- the input set, but that set is an ordered set where the order is
-- determined lexicographically based on the txin's transaction id and
-- index. Thus, adding new inputs during the coin selection may
-- arbitrarily change the order of inputs in the inputs set and thus,
-- render redeemer pointers invalid.
--
-- A solution to this would be to assign pointers only after the
-- transaction has been balanced; to be done properly, this requires an
-- API change so that clients (e.g. the PAB) can give us a little bit
-- more information about the nature of each redeemers in order to
-- connect the dots at the end.
it "ping-pong" $ \ctx -> runResourceT $ do
liftIO $ pendingWith "Need to dynamically assign redeemer pointers in API."
let scenarios =
[ ( "ping-pong"
, \_ _ -> pure
( PlutusScenario.pingPong_1
, [ PlutusScenario.pingPong_2 ]
)
)
, ( "game state-machine"
, \_ _ -> pure
( PlutusScenario.game_1
, [ PlutusScenario.game_2
, PlutusScenario.game_3
]
)
)
, ( "mint-burn"
, \ctx w -> do
(_vk, vkHash) <- getSomeVerificationKey ctx w
let (policy, policyId) = PlutusScenario.mkSignerPolicy [json|{
"vkHash": #{vkHash} }
|]
mint <- PlutusScenario.mintBurn_1 [json|{
"policy": #{policy},
"policyId": #{policyId},
"vkHash": #{vkHash}
}|]
let burn = \_ -> PlutusScenario.mintBurn_2 [json|{
"policy": #{policy},
"policyId": #{policyId},
"vkHash": #{vkHash}
}|]
pure (mint, [burn])
)
, ( "withdrawal"
, \ctx _w -> do
let (script, _scriptHash) = PlutusScenario.alwaysTrueValidator
liftIO $ _moveRewardsToScript ctx
( unsafeFromHex $ T.encodeUtf8 script
, Coin 42000000
)
waitForNextEpoch ctx
withdrawal <- PlutusScenario.withdrawScript_1
pure (withdrawal, [])
)
]

forM_ scenarios $ \(title, setupContract) -> it title $ \ctx -> runResourceT $ do
w <- fixtureWallet ctx
let balanceEndpoint = Link.balanceTransaction @'Shelley w
let signEndpoint = Link.signTransaction @'Shelley w

--
-- Part 1 :: Contract Setup
--
(setup, steps) <- setupContract ctx w

-- Balance
let toBalance = Json PlutusScenario.pingPong_1
let toBalance = Json setup
(_, sealedTx) <- second (view #transaction) <$>
unsafeRequest @(ApiConstructTransaction n) ctx balanceEndpoint toBalance
unsafeRequest @ApiSerialisedTransaction ctx balanceEndpoint toBalance

-- Sign
let toSign = Json [json|
Expand All @@ -1236,30 +1272,28 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do

-- Submit
txid <- submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
waitForTxImmutability ctx

--
-- Part 2 :: Contract Utilization
--
let runStep = \previous step -> do
waitForTxImmutability ctx

-- Balance
partialTx' <- PlutusScenario.pingPong_2 $ Aeson.object
[ "transactionId" .= view #id txid ]
let toBalance' = Json (toJSON partialTx')
-- Balance
partialTx' <- step $ Aeson.object [ "transactionId" .= view #id previous ]
let toBalance' = Json (toJSON partialTx')

-- Sign
(_, sealedTx') <- second (view #transaction) <$>
unsafeRequest @(ApiConstructTransaction n) ctx balanceEndpoint toBalance'
-- Sign
(_, sealedTx') <- second (view #transaction) <$>
unsafeRequest @ApiSerialisedTransaction ctx balanceEndpoint toBalance'
let toSign' = Json [json|
{ "transaction": #{sealedTx'}
, "passphrase": #{fixturePassphrase}
}|]
(_, signedTx') <- second (view #transaction) <$>
unsafeRequest @ApiSerialisedTransaction ctx signEndpoint toSign'

let toSign' = Json [json|
{ "transaction": #{sealedTx'}
, "passphrase": #{fixturePassphrase}
}|]
(_, signedTx') <- second (view #transaction) <$>
unsafeRequest @ApiSerialisedTransaction ctx signEndpoint toSign'
-- Submit
submitTx ctx signedTx' [ expectResponseCode HTTP.status202 ]

-- Submit
void $ submitTx ctx signedTx' [ expectResponseCode HTTP.status202 ]
foldM_ runStep txid steps
where
unsafeGetTx
:: MonadIO m
Expand Down
Binary file not shown.
11 changes: 0 additions & 11 deletions lib/core-integration/test/data/game-sm-success_1-1.json

This file was deleted.

19 changes: 0 additions & 19 deletions lib/core-integration/test/data/game-sm-success_1-2.json

This file was deleted.

1 change: 1 addition & 0 deletions lib/core/cardano-wallet-core.cabal
Expand Up @@ -198,6 +198,7 @@ library
Cardano.Wallet.Primitive.Types.Address
Cardano.Wallet.Primitive.Types.Coin
Cardano.Wallet.Primitive.Types.Hash
Cardano.Wallet.Primitive.Types.Redeemer
Cardano.Wallet.Primitive.Types.RewardAccount
Cardano.Wallet.Primitive.Types.TokenBundle
Cardano.Wallet.Primitive.Types.TokenMap
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet.hs
Expand Up @@ -1763,7 +1763,7 @@ submitExternalTx ctx sealedTx = traceResult trPost $ do
tl = ctx ^. transactionLayer @k
nw = ctx ^. networkLayer
trPost = contramap (MsgSubmitExternalTx (tx ^. #txId)) (ctx ^. logger)
tx = decodeTx tl sealedTx
(tx, _, _) = decodeTx tl sealedTx

-- | Remove a pending or expired transaction from the transaction history. This
-- happens at the request of the user. If the transaction is already on chain,
Expand Down

0 comments on commit 27c709c

Please sign in to comment.