Skip to content

Commit

Permalink
Merge #1849
Browse files Browse the repository at this point in the history
1849: Diverse witnesses in shelley r=paweljakubas a=paweljakubas

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->
#1844 

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- [x] I have added WitnessTag
- [x] I have added impl of mkTx for Byron/Icarus wtinesses
- [x] I have added unit tests verifying decodeTx for Byron/Icarus witnesses
- [x] I have updated fee estimation
- [x] I have added integration tests illustrating the case 


# Comments

<!-- Additional comments or screenshots to attach if any -->

 
<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Assign the PR to a corresponding milestone
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: Pawel Jakubas <[email protected]>
Co-authored-by: IOHK <[email protected]>
Co-authored-by: KtorZ <[email protected]>
  • Loading branch information
4 people authored Jul 10, 2020
2 parents 210f392 + 3abc351 commit f6e6180
Show file tree
Hide file tree
Showing 9 changed files with 2,561 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module Test.Integration.Scenario.API.Shelley.Transactions
import Prelude

import Cardano.Wallet.Api.Types
( ApiFee
( ApiByronWallet
, ApiFee
, ApiT (..)
, ApiTransaction
, ApiTxId (..)
Expand Down Expand Up @@ -73,6 +74,7 @@ import Test.Integration.Framework.DSL
, expectSuccess
, faucetAmt
, faucetUtxoAmt
, fixtureIcarusWallet
, fixturePassphrase
, fixtureRandomWallet
, fixtureWallet
Expand Down Expand Up @@ -464,6 +466,73 @@ spec = do
(Link.createTransaction @'Shelley w) Default payload
expectResponseCode @IO HTTP.status400 r

describe "TRANS_CREATE_09 - Single Output Transaction with non-Shelley witnesses" $
forM_ [(fixtureRandomWallet, "Byron wallet"), (fixtureIcarusWallet, "Icarus wallet")] $
\(srcFixture,name) -> it name $ \ctx -> do

(wByron, wShelley) <- (,) <$> srcFixture ctx <*> fixtureWallet ctx
addrs <- listAddresses @n ctx wShelley

let amt = 1
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}]
}|]

rFeeEst <- request @ApiFee ctx
(Link.getTransactionFee @'Byron wByron) Default payload
verify rFeeEst
[ expectSuccess
, expectResponseCode HTTP.status202
]
let (Quantity feeEstMin) = getFromResponse #estimatedMin rFeeEst
let (Quantity feeEstMax) = getFromResponse #estimatedMax rFeeEst

r <- postTx ctx
(wByron, Link.createTransaction @'Byron, fixturePassphrase)
wShelley
amt
verify r
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#amount . #getQuantity) $
between (feeEstMin + amt, feeEstMax + amt)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` Pending)
]

ra <- request @ApiByronWallet ctx (Link.getWallet @'Byron wByron) Default Empty
verify ra
[ expectSuccess
, expectField (#balance . #total) $
between
( Quantity (faucetAmt - feeEstMax - amt)
, Quantity (faucetAmt - feeEstMin - amt)
)
, expectField
(#balance . #available)
(.>= Quantity (faucetAmt - faucetUtxoAmt))
]

eventually "wa and wb balances are as expected" $ do
rb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wShelley) Default Empty
expectField
(#balance . #getApiT . #available)
(`shouldBe` Quantity (faucetAmt + amt)) rb

ra2 <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wByron) Default Empty
expectField
(#balance . #available)
(`shouldBe` Quantity (faucetAmt - feeEstMax - amt)) ra2

describe "TRANS_ESTIMATE_08 - Bad payload" $ do
let matrix =
[ ( "empty payload", NonJson "" )
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Byron/Codec/Cbor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Cardano.Byron.Codec.Cbor
, inspectNextToken
, decodeList
, decodeListIndef
, decodeNestedBytes
) where

import Prelude
Expand Down
1 change: 1 addition & 0 deletions lib/shelley/cardano-wallet-shelley.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ library
, cardano-binary
, cardano-crypto
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-ledger
, cardano-slotting
, cardano-wallet-cli
Expand Down
25 changes: 25 additions & 0 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Cardano.Wallet.Shelley.Compatibility
, toStakeCredential
, toShelleyCoin
, fromShelleyCoin
, toHDPayloadAddress

-- ** Stake pools
, fromPoolId
Expand All @@ -73,6 +74,7 @@ module Cardano.Wallet.Shelley.Compatibility
, fromChainHash
, fromGenesisData
, fromNetworkMagic
, toByronNetworkMagic
, fromSlotNo
, fromTip
, fromTip'
Expand Down Expand Up @@ -189,6 +191,7 @@ import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.Binary.Bech32.TH as Bech32
import qualified Codec.CBOR.Decoding as CBOR
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -757,6 +760,13 @@ fromNetworkDiscriminant _ =
Just{} -> SL.Mainnet
Nothing -> SL.Testnet

toByronNetworkMagic :: W.ProtocolMagic -> Byron.NetworkMagic
toByronNetworkMagic pm@(W.ProtocolMagic magic) =
if pm == W.mainnetMagic then
Byron.NetworkMainOrStage
else
Byron.NetworkTestnet (fromIntegral magic)

-- NOTE: Arguably breaks naming conventions. Perhaps fromCardanoSignedTx instead
toSealed :: SL.Tx TPraosStandardCrypto -> (W.Tx, W.SealedTx)
toSealed tx =
Expand Down Expand Up @@ -900,6 +910,21 @@ _decodeAddress serverNetwork text =
Byron.NetworkMainOrStage -> SL.Mainnet
Byron.NetworkTestnet{} -> SL.Testnet

toHDPayloadAddress :: W.Address -> Maybe Byron.HDAddressPayload
toHDPayloadAddress (W.Address addr) = do
payload <- CBOR.deserialiseCbor CBOR.decodeAddressPayload addr
attributes <- CBOR.deserialiseCbor decodeAllAttributes' payload
case filter (\(tag,_) -> tag == 1) attributes of
[(1, bytes)] ->
Byron.HDAddressPayload <$> CBOR.decodeNestedBytes CBOR.decodeBytes bytes
_ ->
Nothing
where
decodeAllAttributes' = do
_ <- CBOR.decodeListLenCanonicalOf 3
_ <- CBOR.decodeBytes
CBOR.decodeAllAttributes

{-------------------------------------------------------------------------------
Logging
-------------------------------------------------------------------------------}
Expand Down
Loading

0 comments on commit f6e6180

Please sign in to comment.