Skip to content
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

Spike: cost of on-chain CBOR encoding #154

Merged
merged 7 commits into from
Jan 5, 2022

Conversation

KtorZ
Copy link
Collaborator

@KtorZ KtorZ commented Jan 4, 2022

  • 📍 Create new 'plutus-cbor' package.

  • 📍 Write simple implementation of an on-chain CBOR encoder for integer.

  • 📍 Compare on-chain CBOR encoder with off-chain oracle ('cborg' library).

  • 📍 fixup create lib.

  • 📍 Define new property for measuring and controlling the execution cost of an encoder.

  • 📍 Write plumbing code for the validators and the calculation of execution units.


Plutus.Codec.CBOR.Encoding
  our CBOR encoding matches oracle's ('cborg' library)
    for all (x :: Integer) w/ 'encodeInteger'
      +++ OK, passed 100 tests.
  (on-chain) execution cost of CBOR encoding is small
    for all (x :: Integer), <0.05%
      +++ OK, passed 100 tests.

Property failing:

  1) Plutus.Codec.CBOR.Encoding, our CBOR encoding matches oracle's ('cborg' library), for all (x :: Integer) w/ 'encodeInteger'
       Falsified (after 6 tests):
         27985
         
         ─── ours: 
         1a6d51
         
         ─── cborg: 
         
         19 6d 51  # integer(27985)
         value: 27985
         "\EMmQ" /= "\SUBmQ"
         
  2) Plutus.Codec.CBOR.Encoding, (on-chain) execution cost of CBOR encoding is small, for all (x :: Integer), <0.01%
       Falsified (after 1 test):
         CPU execution units:    1764531 (0.017645%)
         memory execution units: 4043 (0.040430%)

@KtorZ KtorZ requested review from ch1bo and a user January 4, 2022 18:44
@KtorZ KtorZ self-assigned this Jan 4, 2022
@@ -0,0 +1 @@
# Plutus CBOR
Copy link
Member

Choose a reason for hiding this comment

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

Writing 1 or 2 lines here trivial and helps people a lot. So please do :)

Copy link
Member

Choose a reason for hiding this comment

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

:'(

@@ -0,0 +1,125 @@
cabal-version: 2.2
name: plutus-cbor
version: 0.1.0
Copy link
Member

Choose a reason for hiding this comment

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

Now this gets interesting. So far all the packages were very much Hydra specific in our mono repo and we would tag and release them all with the same version. But this (and the MPT package) are arguably more standalone.

How would we handle this?

Interpret a tag 0.2.0 as everything hydra- prefixed being on that version with additional tags for plutus-cbor-0.1.0, plutus-cbor-0.2.0... on the repo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah! I actually wondered why the MPT was versioned 0.2.0 and it didn't occur to me that this was following the Hydra versioning 🤔 ... Not sure how we want to do with that.

encodeInteger,

-- * Data-structures
) where
Copy link
Member

Choose a reason for hiding this comment

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

Empty section? I'd suggest not maintaining both, export-level and intra-module haddocks.


encodingToBuiltinByteString :: Encoding -> BuiltinByteString
encodingToBuiltinByteString = id
{-# INLINEABLE encodingToBuiltinByteString #-}
Copy link
Member

Choose a reason for hiding this comment

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

What's the point of this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, the fact that the module uses a BuiltinByteString as an intermediary representation of encoding is sort of an implementation detail, so, when considering the library from a user perspective, this should not transpire through the interface IMO.

Copy link
Member

Choose a reason for hiding this comment

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

A newtype with non-exported or marked Unsafe data constructor would hint at that more rigorously IMO

distanceExUnits (ExUnits m0 s0) (ExUnits m1 s1) =
ExUnits
(if m0 > m1 then m0 - m1 else m1 - m0)
(if s0 > s1 then s0 - s1 else s1 - s0)
Copy link
Member

Choose a reason for hiding this comment

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

🎉

encodeIntegerValidator :: Scripts.TypedValidator (EncodeValidator Integer)
encodeIntegerValidator =
Scripts.mkTypedValidator @(EncodeValidator Integer)
$$(Plutus.compile [||\() xs _ctx -> let _bytes = encodeInteger <$> xs in True||])
Copy link
Member

Choose a reason for hiding this comment

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

This didn't work for me..#155 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Aaaand, it doesn't for me either actually 😂

@github-actions
Copy link

github-actions bot commented Jan 5, 2022

Unit Test Results

    5 files  +1    67 suites  +2   3m 24s ⏱️ -11s
190 tests +2  187 ✔️ +2  3 💤 ±0  0 ±0 

Results for commit 53eab8e. ± Comparison against base commit 6cdf0d8.

@KtorZ KtorZ merged commit 5ff29e1 into master Jan 5, 2022
@KtorZ KtorZ deleted the KtorZ/spike-on-chain-cbor-encoding-cost branch January 5, 2022 12:02
ghost pushed a commit that referenced this pull request Jan 5, 2022
This commit only tries to serialise Ada values on-chain. It also
"vendorises" the CBOR encoder from @KtorZ stolen from #154
ghost pushed a commit that referenced this pull request Jan 7, 2022
This commit only tries to serialise Ada values on-chain. It also
"vendorises" the CBOR encoder from @KtorZ stolen from #154
ghost pushed a commit that referenced this pull request Jan 11, 2022
This commit only tries to serialise Ada values on-chain. It also
"vendorises" the CBOR encoder from @KtorZ stolen from #154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants