-
Notifications
You must be signed in to change notification settings - Fork 156
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 TxSeq
decoders
#4904
Open
teodanciu
wants to merge
3
commits into
master
Choose a base branch
from
td/improve-txseq-decoders
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−61
Open
Improve TxSeq
decoders
#4904
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,8 +18,7 @@ | |||||
|
||||||
module Cardano.Ledger.Shelley.BlockChain ( | ||||||
ShelleyTxSeq (ShelleyTxSeq, txSeqTxns', TxSeq'), | ||||||
constructMetadata, | ||||||
indexLookupSeq, | ||||||
auxDataSeqDecoder, | ||||||
txSeqTxns, | ||||||
bbHash, | ||||||
bBodySize, | ||||||
|
@@ -61,7 +60,9 @@ import Control.Monad (unless) | |||||
import Data.ByteString (ByteString) | ||||||
import qualified Data.ByteString.Lazy as BSL | ||||||
import Data.Coerce (coerce) | ||||||
import Data.Map.Strict (Map) | ||||||
import Data.Functor.Identity (Identity (..)) | ||||||
import Data.IntMap (IntMap) | ||||||
import qualified Data.IntMap as IntMap | ||||||
import qualified Data.Map.Strict as Map | ||||||
import Data.Monoid (All (..)) | ||||||
import Data.Sequence (Seq) | ||||||
|
@@ -191,18 +192,6 @@ bbHash (TxSeq' _ bodies wits md) = | |||||
hashStrict = Hash.hashWith id | ||||||
hashPart = Hash.hashToBytes . hashStrict . BSL.toStrict | ||||||
|
||||||
-- | Given a size and a mapping from indices to maybe metadata, | ||||||
-- return a sequence whose size is the size parameter and | ||||||
-- whose non-Nothing values correspond to the values in the mapping. | ||||||
constructMetadata :: | ||||||
Int -> | ||||||
Map Int (Annotator (TxAuxData era)) -> | ||||||
Seq (Maybe (Annotator (TxAuxData era))) | ||||||
constructMetadata = indexLookupSeq | ||||||
|
||||||
indexLookupSeq :: Int -> Map Int a -> Seq (Maybe a) | ||||||
indexLookupSeq n md = fmap (`Map.lookup` md) (Seq.fromList [0 .. n - 1]) | ||||||
|
||||||
-- | The parts of the Tx in Blocks that have to have DecCBOR(Annotator x) instances. | ||||||
-- These are exactly the parts that are SafeToHash. | ||||||
-- | Decode a TxSeq, used in decoding a Block. | ||||||
|
@@ -215,24 +204,19 @@ txSeqDecoder :: | |||||
txSeqDecoder lax = do | ||||||
(bodies, bodiesAnn) <- withSlice decCBOR | ||||||
(wits, witsAnn) <- withSlice decCBOR | ||||||
let b = length bodies | ||||||
inRange x = (0 <= x) && (x <= (b - 1)) | ||||||
w = length wits | ||||||
(metadata, metadataAnn) <- withSlice $ | ||||||
do | ||||||
m <- decCBOR | ||||||
unless -- TODO this PR introduces this new test, That didn't used to run in the Shelley | ||||||
(lax || all inRange (Map.keysSet m)) -- Era, Is it possible there might be some blocks, that should have been caught on the chain? | ||||||
(fail ("Some Auxiliarydata index is not in the range: 0 .. " ++ show (b - 1))) | ||||||
pure (constructMetadata @era b m) | ||||||
let bodiesLength = length bodies | ||||||
witsLength = length wits | ||||||
(metadata, metadataAnn) <- withSlice $ do | ||||||
auxDataMap <- decCBOR | ||||||
auxDataSeqDecoder bodiesLength auxDataMap lax | ||||||
|
||||||
unless | ||||||
(lax || b == w) | ||||||
(lax || bodiesLength == witsLength) | ||||||
( fail $ | ||||||
"different number of transaction bodies (" | ||||||
<> show b | ||||||
<> show bodiesLength | ||||||
<> ") and witness sets (" | ||||||
<> show w | ||||||
<> show witsLength | ||||||
<> ")" | ||||||
) | ||||||
|
||||||
|
@@ -242,6 +226,21 @@ txSeqDecoder lax = do | |||||
Seq.zipWith3 segWitAnnTx bodies wits metadata | ||||||
pure $ TxSeq' <$> txns <*> bodiesAnn <*> witsAnn <*> metadataAnn | ||||||
|
||||||
auxDataSeqDecoder :: | ||||||
Int -> IntMap (m (TxAuxData era)) -> Bool -> Decoder s (Seq (Maybe (m (TxAuxData era)))) | ||||||
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. This will allow you to avoid using
Suggested change
|
||||||
auxDataSeqDecoder bodiesLength auxDataMap lax = do | ||||||
unless | ||||||
(lax || getAll (IntMap.foldMapWithKey (\k _ -> All (inRange k)) auxDataMap)) | ||||||
(fail ("Some Auxiliarydata index is not in the range: 0 .. " ++ show (bodiesLength - 1))) | ||||||
pure (indexLookupSeq bodiesLength auxDataMap) | ||||||
where | ||||||
inRange x = (0 <= x) && (x <= (bodiesLength - 1)) | ||||||
-- Given a size and a mapping from indices to maybe values, | ||||||
-- return a sequence whose size is the size parameter and | ||||||
-- whose non-Nothing values correspond to the values in the mapping. | ||||||
indexLookupSeq :: Int -> IntMap a -> Seq (Maybe a) | ||||||
indexLookupSeq n ixMap = Seq.fromList [IntMap.lookup ix ixMap | ix <- [0 .. n - 1]] | ||||||
|
||||||
instance EraTx era => DecCBOR (Annotator (ShelleyTxSeq era)) where | ||||||
decCBOR = txSeqDecoder False | ||||||
|
||||||
|
@@ -256,13 +255,12 @@ instance | |||||
decCBOR = do | ||||||
Annotated bodies bodiesBytes <- decodeAnnotated decCBOR | ||||||
Annotated wits witsBytes <- decodeAnnotated decCBOR | ||||||
Annotated auxDataMap auxDataBytes <- decodeAnnotated decCBOR | ||||||
Annotated (auxDataMap :: IntMap (TxAuxData era)) auxDataBytes <- decodeAnnotated decCBOR | ||||||
let bodiesLength = length bodies | ||||||
let inRange x = (0 <= x) && (x <= (bodiesLength - 1)) | ||||||
unless | ||||||
(getAll (Map.foldMapWithKey (\k _ -> All (inRange k)) auxDataMap)) | ||||||
(fail ("Some Auxiliarydata index is not in the range: 0 .. " ++ show (bodiesLength - 1))) | ||||||
let auxData = indexLookupSeq bodiesLength auxDataMap | ||||||
auxData <- | ||||||
fmap (fmap runIdentity) | ||||||
<$> auxDataSeqDecoder bodiesLength (fmap pure auxDataMap) False | ||||||
|
||||||
let witsLength = length wits | ||||||
unless | ||||||
(bodiesLength == witsLength) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is no need to complicate the normal decoder