Skip to content

Commit

Permalink
Merge #1849 #1887
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
-->


1887: Rename our SlotNo to SlotInEpoch r=Anviking a=Anviking


# Issue Number

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


# Overview

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

- [x] Renamed `SlotNo` to `SlotInEpoch` (for consistency with other components)


# Comments

- `W.SlotNo` == `Cardano.SlotInEpoch` /= `Cardano.SlotNo`

```
Epoch       0   1   2   3   4
SlotNo*     0 1 2 3 4 5 6 7 8 9
SlotInEpoch 0 1 0 1 0 1 0 1 0 1
```

*) I.e. the one defined in cardano-base and used in ourobouros-consensus.


<!-- 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]>
Co-authored-by: Johannes Lund <[email protected]>
  • Loading branch information
5 people authored Jul 10, 2020
3 parents 210f392 + 3abc351 + 8a85564 commit f4a4b54
Show file tree
Hide file tree
Showing 26 changed files with 2,641 additions and 167 deletions.
8 changes: 4 additions & 4 deletions lib/byron/src/Cardano/Wallet/Byron/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Cardano.Wallet.Byron.Compatibility
, toByronHash
, toGenTx
, toPoint
, toSlotNo
, toSlotInEpoch

, fromBlockNo
, fromByronBlock
Expand Down Expand Up @@ -296,10 +296,10 @@ toPoint
-> Point ByronBlock
toPoint genesisH epLength (W.BlockHeader sid _ h _)
| h == (coerce genesisH) = O.GenesisPoint
| otherwise = O.Point $ Point.block (toSlotNo epLength sid) (toByronHash h)
| otherwise = O.Point $ Point.block (toSlotInEpoch epLength sid) (toByronHash h)

toSlotNo :: W.EpochLength -> W.SlotId -> SlotNo
toSlotNo epLength =
toSlotInEpoch :: W.EpochLength -> W.SlotId -> SlotNo
toSlotInEpoch epLength =
SlotNo . W.flatSlot epLength

-- | SealedTx are the result of rightfully constructed byron transactions so, it
Expand Down
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
9 changes: 5 additions & 4 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 All @@ -66,7 +67,7 @@ import Cardano.Wallet.Primitive.Types
, Hash (..)
, ProtocolMagic (..)
, SlotId (..)
, SlotNo (..)
, SlotInEpoch (..)
, TxIn (..)
, TxOut (..)
, unsafeEpochNo
Expand Down Expand Up @@ -316,8 +317,8 @@ decodeEpochNo :: HasCallStack => CBOR.Decoder s EpochNo
decodeEpochNo =
unsafeEpochNo . fromIntegral @Word64 @Word32 <$> CBOR.decodeWord64

decodeSlotNo :: CBOR.Decoder s SlotNo
decodeSlotNo = SlotNo . fromIntegral <$> CBOR.decodeWord16
decodeSlotInEpoch :: CBOR.Decoder s SlotInEpoch
decodeSlotInEpoch = SlotInEpoch . fromIntegral <$> CBOR.decodeWord16

decodeGenesisBlockHeader :: CBOR.Decoder s BlockHeader
decodeGenesisBlockHeader = do
Expand Down Expand Up @@ -491,7 +492,7 @@ decodeSlotId :: CBOR.Decoder s SlotId
decodeSlotId = do
_ <- CBOR.decodeListLenCanonicalOf 2
epoch <- decodeEpochNo
SlotId epoch <$> decodeSlotNo
SlotId epoch <$> decodeSlotInEpoch

decodeSoftwareVersion :: CBOR.Decoder s ()
decodeSoftwareVersion = do
Expand Down
14 changes: 7 additions & 7 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ import Cardano.Wallet.Primitive.Types
, NetworkParameters (..)
, PoolId (..)
, ShowFmt (..)
, SlotInEpoch (..)
, SlotLength (..)
, SlotNo (..)
, StakePoolMetadata
, StartTime (..)
, TxIn (..)
Expand Down Expand Up @@ -554,13 +554,13 @@ data ApiTimeReference = ApiTimeReference

data ApiBlockReference = ApiBlockReference
{ epochNumber :: !(ApiT EpochNo)
, slotNumber :: !(ApiT SlotNo)
, slotNumber :: !(ApiT SlotInEpoch)
, height :: !(Quantity "block" Natural)
} deriving (Eq, Generic, Show)

data ApiNetworkTip = ApiNetworkTip
{ epochNumber :: !(ApiT EpochNo)
, slotNumber :: !(ApiT SlotNo)
, slotNumber :: !(ApiT SlotInEpoch)
} deriving (Eq, Generic, Show)

data ApiNetworkInformation = ApiNetworkInformation
Expand Down Expand Up @@ -1124,10 +1124,10 @@ instance FromJSON (ApiT EpochNo) where
instance ToJSON (ApiT EpochNo) where
toJSON (ApiT (EpochNo en)) = toJSON $ fromIntegral @Word31 @Word32 en

instance FromJSON (ApiT SlotNo) where
parseJSON = fmap (ApiT . SlotNo) . parseJSON
instance ToJSON (ApiT SlotNo) where
toJSON (ApiT (SlotNo sn)) = toJSON sn
instance FromJSON (ApiT SlotInEpoch) where
parseJSON = fmap (ApiT . SlotInEpoch) . parseJSON
instance ToJSON (ApiT SlotInEpoch) where
toJSON (ApiT (SlotInEpoch sn)) = toJSON sn

instance FromJSON ApiNetworkTip where
parseJSON = genericParseJSON defaultRecordTypeOptions
Expand Down
10 changes: 5 additions & 5 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Cardano.Wallet.Primitive.Types
, PoolId
, PoolOwner (..)
, SlotId (..)
, SlotNo (..)
, SlotInEpoch (..)
, StakeKeyCertificate (..)
, StakePoolMetadataHash (..)
, StakePoolMetadataUrl (..)
Expand Down Expand Up @@ -307,11 +307,11 @@ instance ToJSON SlotId where
instance FromJSON SlotId where
parseJSON = genericParseJSON defaultOptions

instance ToJSON SlotNo where
toJSON (SlotNo n) = toJSON n
instance ToJSON SlotInEpoch where
toJSON (SlotInEpoch n) = toJSON n

instance FromJSON SlotNo where
parseJSON = fmap SlotNo . parseJSON
instance FromJSON SlotInEpoch where
parseJSON = fmap SlotInEpoch . parseJSON

instance ToJSON EpochNo where
toJSON (EpochNo n) = toJSON (fromIntegral @Word31 @Word32 n)
Expand Down
20 changes: 10 additions & 10 deletions lib/core/src/Cardano/Wallet/Primitive/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module Cardano.Wallet.Primitive.Types
, FeePolicy (..)
, SlotId (..)
, SlotLength (..)
, SlotNo (..)
, SlotInEpoch (..)
, StartTime (..)
, slotParams

Expand Down Expand Up @@ -1461,10 +1461,10 @@ instance Buildable TxParameters where
-- | A slot identifier is the combination of an epoch and slot.
data SlotId = SlotId
{ epochNumber :: !EpochNo
, slotNumber :: !SlotNo
, slotNumber :: !SlotInEpoch
} deriving stock (Show, Read, Eq, Ord, Generic)

newtype SlotNo = SlotNo { unSlotNo :: Word32 }
newtype SlotInEpoch = SlotInEpoch { unSlotInEpoch :: Word32 }
deriving stock (Show, Read, Eq, Ord, Generic)
deriving newtype (Num, Buildable, NFData, Enum)

Expand Down Expand Up @@ -1522,7 +1522,7 @@ epochSucc (EpochNo e)
instance NFData SlotId

instance Buildable SlotId where
build (SlotId (EpochNo e) (SlotNo s)) =
build (SlotId (EpochNo e) (SlotInEpoch s)) =
fromString (show e) <> "." <> fromString (show s)

-- | The essential parameters necessary for performing slot arithmetic.
Expand All @@ -1539,7 +1539,7 @@ data SlotParameters = SlotParameters

-- | Convert a 'SlotId' to the number of slots since genesis.
flatSlot :: EpochLength -> SlotId -> Word64
flatSlot (EpochLength epochLength) (SlotId (EpochNo e) (SlotNo s)) =
flatSlot (EpochLength epochLength) (SlotId (EpochNo e) (SlotInEpoch s)) =
fromIntegral epochLength * fromIntegral e + fromIntegral s

-- | Convert a 'flatSlot' index to 'SlotId'.
Expand All @@ -1565,7 +1565,7 @@ fromFlatSlot el@(EpochLength epochLength) n
e = n `div` fromIntegral epochLength
s = n `mod` fromIntegral epochLength
maxFlatSlot =
flatSlot el (SlotId (EpochNo maxBound) (SlotNo $ epochLength - 1))
flatSlot el (SlotId (EpochNo maxBound) (SlotInEpoch $ epochLength - 1))

-- | @slotDifference a b@ is how many slots @a@ is after @b@. The result is
-- non-negative, and if @b > a@ then this function returns zero.
Expand All @@ -1582,12 +1582,12 @@ slotPred :: SlotParameters -> SlotId -> Maybe SlotId
slotPred (SlotParameters (EpochLength el) _ _ _) (SlotId en sn)
| en == 0 && sn == 0 = Nothing
| sn > 0 = Just $ SlotId en (sn - 1)
| otherwise = Just $ SlotId (en - 1) (SlotNo $ el - 1)
| otherwise = Just $ SlotId (en - 1) (SlotInEpoch $ el - 1)

-- | Return the slot immediately after the given slot.
slotSucc :: SlotParameters -> SlotId -> SlotId
slotSucc (SlotParameters (EpochLength el) _ _ _) (SlotId en (SlotNo sn))
| sn < el - 1 = SlotId en (SlotNo $ sn + 1)
slotSucc (SlotParameters (EpochLength el) _ _ _) (SlotId en (SlotInEpoch sn))
| sn < el - 1 = SlotId en (SlotInEpoch $ sn + 1)
| otherwise = SlotId (en + 1) 0

-- | The time when a slot begins.
Expand Down Expand Up @@ -1628,7 +1628,7 @@ slotAt (SlotParameters (EpochLength el) (SlotLength sl) (StartTime st) _) t
epochNumber = EpochNo $
floor (diff / epochLength)

slotNumber = SlotNo $
slotNumber = SlotInEpoch $
floor ((diff - fromIntegral (unEpochNo epochNumber) * epochLength) / sl)

-- | Transforms the given inclusive time range into an inclusive slot range.
Expand Down
4 changes: 2 additions & 2 deletions lib/core/test/bench/db/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import Cardano.Wallet.Primitive.Types
, Hash (..)
, Range (..)
, SlotId (..)
, SlotNo (unSlotNo)
, SlotInEpoch (unSlotInEpoch)
, SortOrder (..)
, TransactionInfo
, Tx (..)
Expand Down Expand Up @@ -411,7 +411,7 @@ mkTxHistory numTx numInputs numOutputs range =
{ status = [InLedger, Pending] !! (i `mod` 2)
, direction = Incoming
, slotId = sl i
, blockHeight = Quantity $ fromIntegral $ unSlotNo $ slotNumber $ sl i
, blockHeight = Quantity $ fromIntegral $ unSlotInEpoch $ slotNumber $ sl i
, amount = Quantity (fromIntegral numOutputs)
}
)
Expand Down
8 changes: 4 additions & 4 deletions lib/core/test/unit/Cardano/Pool/DB/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Cardano.Wallet.Primitive.Types
, PoolRegistrationCertificate (..)
, PoolRetirementCertificate (..)
, SlotId (..)
, SlotNo (..)
, SlotInEpoch (..)
, SlotParameters (..)
, StakePoolMetadata (..)
, StakePoolMetadataHash (..)
Expand Down Expand Up @@ -103,9 +103,9 @@ instance Arbitrary SlotId where
uncurry SlotId <$> shrink (ep, sl)
arbitrary = applyArbitrary2 SlotId

instance Arbitrary SlotNo where
shrink (SlotNo x) = SlotNo <$> shrink x
arbitrary = SlotNo <$> choose (0, fromIntegral arbitraryChainLength)
instance Arbitrary SlotInEpoch where
shrink (SlotInEpoch x) = SlotInEpoch <$> shrink x
arbitrary = SlotInEpoch <$> choose (0, fromIntegral arbitraryChainLength)

instance Arbitrary EpochNo where
shrink (EpochNo x) = EpochNo <$> shrink x
Expand Down
8 changes: 4 additions & 4 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import Cardano.Wallet.Primitive.Types
, PoolId (..)
, PoolOwner (..)
, SlotId (..)
, SlotNo (..)
, SlotInEpoch (..)
, SortOrder (..)
, StakePoolMetadata (..)
, StakePoolTicker
Expand Down Expand Up @@ -1183,9 +1183,9 @@ instance Arbitrary SlotId where
arbitrary = applyArbitrary2 SlotId
shrink = genericShrink

instance Arbitrary SlotNo where
shrink (SlotNo x) = SlotNo <$> shrink x
arbitrary = SlotNo <$> arbitrary
instance Arbitrary SlotInEpoch where
shrink (SlotInEpoch x) = SlotInEpoch <$> shrink x
arbitrary = SlotInEpoch <$> arbitrary

instance Arbitrary EpochNo where
shrink (EpochNo x) = EpochNo <$> shrink x
Expand Down
14 changes: 7 additions & 7 deletions lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import Cardano.Wallet.Primitive.Types
, Range (..)
, ShowFmt (..)
, SlotId (..)
, SlotNo (..)
, SlotInEpoch (..)
, SlotParameters (..)
, SortOrder (..)
, Tx (..)
Expand Down Expand Up @@ -347,19 +347,19 @@ instance Arbitrary PassphraseScheme where

instance Arbitrary BlockHeader where
arbitrary = do
sid@(SlotId (EpochNo ep) (SlotNo sl)) <- arbitrary
sid@(SlotId (EpochNo ep) (SlotInEpoch sl)) <- arbitrary
let h = fromIntegral sl + fromIntegral ep * arbitraryEpochLength
blockH <- arbitrary
pure $ BlockHeader sid (Quantity h) blockH (coerce blockH)

instance Arbitrary SlotId where
shrink (SlotId (EpochNo ep) (SlotNo sl)) =
uncurry SlotId <$> shrink (EpochNo ep, SlotNo sl)
shrink (SlotId (EpochNo ep) (SlotInEpoch sl)) =
uncurry SlotId <$> shrink (EpochNo ep, SlotInEpoch sl)
arbitrary = applyArbitrary2 SlotId

instance Arbitrary SlotNo where
shrink (SlotNo x) = SlotNo <$> shrink x
arbitrary = SlotNo <$> choose (0, fromIntegral arbitraryChainLength)
instance Arbitrary SlotInEpoch where
shrink (SlotInEpoch x) = SlotInEpoch <$> shrink x
arbitrary = SlotInEpoch <$> choose (0, fromIntegral arbitraryChainLength)

instance Arbitrary EpochNo where
shrink (EpochNo x) = EpochNo <$> shrink x
Expand Down
Loading

0 comments on commit f4a4b54

Please sign in to comment.