Skip to content

Commit

Permalink
Remove unnecessary validation of Data
Browse files Browse the repository at this point in the history
Close to the release of Alonzo, we improved the format of `Data` to
allow arbitrary-size integers and bytestrings by requiring them to be
encoded in CBOR using indefinite-length bytestrings. This means that the
client-side validation performed by the API was never necessary, and can
be removed.
  • Loading branch information
michaelpj committed Aug 8, 2022
1 parent 1e9fd19 commit bc666ca
Showing 1 changed file with 8 additions and 37 deletions.
45 changes: 8 additions & 37 deletions cardano-api/src/Cardano/Api/ScriptData.hs
Expand Up @@ -188,16 +188,11 @@ validateScriptData d =
err:_ -> Left err
where
-- collect all errors in a monoidal fold style:
collect (ScriptDataNumber n) =
[ ScriptDataNumberOutOfRange n
| n > fromIntegral (maxBound :: Word64)
|| n < negate (fromIntegral (maxBound :: Word64))
]
collect (ScriptDataBytes bs) =
[ ScriptDataBytesTooLong len
| let len = BS.length bs
, len > scriptDataByteStringMaxLength
]

-- Arbitrary size numbers are fine
collect (ScriptDataNumber _) = []
-- Arbitrary sized bytes are fine
collect (ScriptDataBytes _) = []
collect (ScriptDataList xs) =
foldMap collect xs

Expand All @@ -206,51 +201,27 @@ validateScriptData d =
<> collect v)
kvs

-- Constr tags do need to be less than a Word64
collect (ScriptDataConstructor n xs) =
[ ScriptDataConstructorOutOfRange n
| n > fromIntegral (maxBound :: Word64) || n < 0 ]
<> foldMap collect xs


-- | The maximum length of a script data byte string value.
scriptDataByteStringMaxLength :: Int
scriptDataByteStringMaxLength = 64


-- | An error in script data due to an out-of-range value.
--
data ScriptDataRangeError =
newtype ScriptDataRangeError =

-- | The number is outside the maximum range of @-2^64-1 .. 2^64-1@.
--
ScriptDataNumberOutOfRange !Integer

-- | The number is outside the maximum range of @-2^64-1 .. 2^64-1@.
--
| ScriptDataConstructorOutOfRange !Integer

-- | The length of a byte string metadatum value exceeds the maximum of
-- 64 bytes.
--
| ScriptDataBytesTooLong !Int
ScriptDataConstructorOutOfRange Integer
deriving (Eq, Show)

instance Error ScriptDataRangeError where
displayError (ScriptDataNumberOutOfRange n) =
"Number in script data value "
<> show n
<> " is outside the range -(2^64-1) .. 2^64-1."
displayError (ScriptDataConstructorOutOfRange n) =
"Constructor numbers in script data value "
<> show n
<> " is outside the range 0 .. 2^64-1."
displayError (ScriptDataBytesTooLong actualLen) =
"Byte strings in script data must consist of at most "
<> show scriptDataByteStringMaxLength
<> " bytes, but it consists of "
<> show actualLen
<> " bytes."


-- ----------------------------------------------------------------------------
-- JSON conversion
Expand Down

0 comments on commit bc666ca

Please sign in to comment.