Skip to content

Commit

Permalink
[Plugin] Warn about missing builtins (#5702)
Browse files Browse the repository at this point in the history
* [Plugin] Warn about missing builtins

* Add missing builtins

* Fix naming for consistency

* Fix test

---------

Co-authored-by: Michael Peyton Jones <michael.peyton-jones@iohk.io>
  • Loading branch information
effectfully and michaelpj authored Jan 11, 2024
1 parent e2f555f commit 745f54e
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 202 deletions.
8 changes: 4 additions & 4 deletions plutus-core/plutus-core/src/PlutusCore/Builtin/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ integerToByteStringWrapper ::
integerToByteStringWrapper endiannessArg lengthArg input
-- Check that we are within the Int range on the non-negative side.
| lengthArg < 0 || lengthArg >= 536870912 = do
emit "builtinIntegerToByteString: inappropriate length argument"
emit "integerToByteString: inappropriate length argument"
emit $ "Length requested: " <> (pack . show $ input)
pure EvaluationFailure
-- As this builtin hasn't been costed yet, we have to impose a temporary limit of 10KiB on requested
Expand All @@ -40,7 +40,7 @@ integerToByteStringWrapper endiannessArg lengthArg input
--
-- TODO: Cost this builtin.
| lengthArg > 10240 = do
emit "builtinIntegerToByteString: padding argument too large"
emit "integerToByteString: padding argument too large"
emit "If you are seeing this, it is a bug: please report this!"
pure EvaluationFailure
| otherwise = let endianness = endiannessArgToByteOrder endiannessArg in
Expand All @@ -50,13 +50,13 @@ integerToByteStringWrapper endiannessArg lengthArg input
case integerToByteString endianness (fromIntegral lengthArg) input of
Left err -> case err of
NegativeInput -> do
emit "builtinIntegerToByteString: cannot convert negative Integer"
emit "integerToByteString: cannot convert negative Integer"
-- This does work proportional to the size of input. However, we're in a failing case
-- anyway, and the user's paid for work proportional to this size in any case.
emit $ "Input: " <> (pack . show $ input)
pure EvaluationFailure
NotEnoughDigits -> do
emit "builtinIntegerToByteString: cannot represent Integer in given number of bytes"
emit "integerToByteString: cannot represent Integer in given number of bytes"
-- This does work proportional to the size of input. However, we're in a failing case
-- anyway, and the user's paid for work proportional to this size in any case.
emit $ "Input: " <> (pack . show $ input)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -832,33 +832,33 @@ test_Conversion =
adjustOption (\x -> max x . HedgehogTestLimit . Just $ 8000) .
testGroup "Integer <-> ByteString conversions" $ [
testGroup "Integer -> ByteString" [
--- lengthOfByteString (builtinIntegerToByteString e d 0) = d
--- lengthOfByteString (integerToByteString e d 0) = d
testPropertyNamed "property 1" "i2b_prop1" . property $ Conversion.i2bProperty1,
-- indexByteString (builtinIntegerToByteString e k 0) j = 0
-- indexByteString (integerToByteString e k 0) j = 0
testPropertyNamed "property 2" "i2b_prop2" . property $ Conversion.i2bProperty2,
-- lengthOfByteString (builtinIntegerToByteString e 0 p) > 0
-- lengthOfByteString (integerToByteString e 0 p) > 0
testPropertyNamed "property 3" "i2b_prop3" . property $ Conversion.i2bProperty3,
-- builtinIntegerToByteString False 0 (multiplyInteger p 256) = consByteString
-- 0 (builtinIntegerToByteString False 0 p)
-- integerToByteString False 0 (multiplyInteger p 256) = consByteString
-- 0 (integerToByteString False 0 p)
testPropertyNamed "property 4" "i2b_prop4" . property $ Conversion.i2bProperty4,
-- builtinIntegerToByteString True 0 (multiplyInteger p 256) = appendByteString
-- (builtinIntegerToByteString True 0 p) (singleton 0)
-- integerToByteString True 0 (multiplyInteger p 256) = appendByteString
-- (integerToByteString True 0 p) (singleton 0)
testPropertyNamed "property 5" "i2b_prop5" . property $ Conversion.i2bProperty5,
-- builtinIntegerToByteString False 0 (plusInteger (multiplyInteger q 256) r) =
-- appendByteString (builtinIntegerToByteString False 0 r) (builtinIntegerToByteString False 0 q)
-- integerToByteString False 0 (plusInteger (multiplyInteger q 256) r) =
-- appendByteString (integerToByteString False 0 r) (integerToByteString False 0 q)
testPropertyNamed "property 6" "i2b_prop6" . property $ Conversion.i2bProperty6,
-- builtinIntegerToByteString True 0 (plusInteger (multiplyInteger q 256) r) =
-- appendByteString (builtinIntegerToByteString False 0 q)
-- (builtinIntegerToByteString False 0 r)
-- integerToByteString True 0 (plusInteger (multiplyInteger q 256) r) =
-- appendByteString (integerToByteString False 0 q)
-- (integerToByteString False 0 r)
testPropertyNamed "property 7" "i2b_prop7" . property $ Conversion.i2bProperty7,
testGroup "CIP-0087 examples" Conversion.i2bCipExamples
],
testGroup "ByteString -> Integer" [
-- builtinByteStringToInteger b (builtinIntegerToByteString b d q) = q
-- byteStringToInteger b (integerToByteString b d q) = q
testPropertyNamed "property 1" "b2i_prop1" . property $ Conversion.b2iProperty1,
-- builtinByteStringToInteger b (consByteString w8 emptyByteString) = w8
-- byteStringToInteger b (consByteString w8 emptyByteString) = w8
testPropertyNamed "property 2" "b2i_prop2" . property $ Conversion.b2iProperty2,
-- builtinIntegerToByteString b (lengthOfByteString bs) (builtinByteStringToInteger b bs) = bs
-- integerToByteString b (lengthOfByteString bs) (byteStringToInteger b bs) = bs
testPropertyNamed "property 3" "b2i_prop3" . property $ Conversion.b2iProperty3,
testGroup "CIP-0087 examples" Conversion.b2iCipExamples
]
Expand Down
12 changes: 6 additions & 6 deletions plutus-ledger-api/test-plugin/Spec/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ patternOptions =
, [(1,9), (2,2), (6,10), (2,3), (1,0), (4,10), (3,5), (5,0), (3,6), (2,4), (1,1), (2,7), (4,8)]
]

{-# INLINEABLE integerToByteString #-}
integerToByteString :: Integer -> BuiltinByteString
integerToByteString n =
{-# INLINEABLE i2Bs #-}
i2Bs :: Integer -> BuiltinByteString
i2Bs n =
if n < 0
then "-" `appendByteString` integerToByteString (negate n)
then "-" `appendByteString` i2Bs (negate n)
-- @48@ is the ASCII code of @0@.
else ListTx.foldr (consByteString . (48 +)) emptyByteString $ toDigits n

{-# INLINEABLE replicateToByteString #-}
-- | Like 'integerToByteString' but generates longer bytestrings, so that repeated recalculations of
-- | Like 'i2Bs but generates longer bytestrings, so that repeated recalculations of
-- currency/token name comparisons get reflected in the budget tests in a visible manner.
replicateToByteString :: Integer -> BuiltinByteString
replicateToByteString i =
ListTx.foldr id emptyByteString $
ListTx.replicate iTo6 (appendByteString $ integerToByteString i)
ListTx.replicate iTo6 (appendByteString $ i2Bs i)
where
iTo2 = i * i
iTo4 = iTo2 * iTo2
Expand Down
Loading

2 comments on commit 745f54e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 745f54e Previous: e2f555f Ratio
validation-decode-multisig-sm-7 608.3 μs 558.7 μs 1.09
validation-decode-prism-2 536.5 μs 504.4 μs 1.06

This comment was automatically generated by workflow using github-action-benchmark.

CC: @input-output-hk/plutus-core

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 745f54e Previous: 5102f54 Ratio
validation-decode-future-increase-margin-2 331.1 μs 309 μs 1.07
validation-decode-future-pay-out-2 326.1 μs 308.7 μs 1.06
validation-decode-future-pay-out-3 331.9 μs 310 μs 1.07
validation-decode-future-settle-early-2 329.4 μs 310.4 μs 1.06
validation-decode-multisig-sm-3 583.7 μs 550.4 μs 1.06
validation-decode-multisig-sm-7 585.7 μs 550.9 μs 1.06
validation-decode-multisig-sm-8 577.1 μs 548.6 μs 1.05
validation-decode-multisig-sm-9 583.9 μs 552.3 μs 1.06
validation-decode-prism-2 532.3 μs 496.5 μs 1.07

This comment was automatically generated by workflow using github-action-benchmark.

CC: @input-output-hk/plutus-core

Please sign in to comment.