Giter Site home page Giter Site logo

Enums about casper-contract-schema HOT 3 CLOSED

zie1ony avatar zie1ony commented on July 19, 2024
Enums

from casper-contract-schema.

Comments (3)

melpadden avatar melpadden commented on July 19, 2024

Michal has agreed to describe the approach to this.

from casper-contract-schema.

mpapierski avatar mpapierski commented on July 19, 2024

In current VM2 prototype the schema recursively defines all the types involved in the smart contract interaction. Here's an example of a schema for a CEP18 contract ported over to the new VM prototype.

https://github.com/mpapierski/casper-node/blob/f265d3013401dbfcd2bce14d975fdf0e556510f7/smart_contracts/sdk-codegen/tests/fixtures/cep18_schema.json#L5-L245

  • definitions is a map where a key is a textual representation of a given type encountered at the time a schema is populated. I.e. Cep18Error
  • The value of given key in definitions can be either Primitive, Sequence, FixedSequence, Tuple, Enum, Struct.
  • Enum is an object and contains array items that describes the variant
    • name is the stringified name of the variant
    • discriminant preserves the value at compile time (i.e. enum Condition { A = 1, B = 2, C }). If code does not specify a discriminant, it will be autogenerated in increasing order starting from zero.
    • decl specifies declaration of a variant fields (depends on named/unnamed fields: either a tuple, or a struct will be generated).

Enum

"vm2_cep18::error::Cep18Error": {
      "Enum": {
        "items": [
          {
            "name": "InvalidContext",
            "discriminant": 0,
            "decl": "()"
          },
          {
            "name": "InsufficientBalance",
            "discriminant": 1,
            "decl": "()"
          },
          {
            "name": "InsufficientAllowance",
            "discriminant": 2,
            "decl": "()"
          },
          {
            "name": "Overflow",
            "discriminant": 3,
            "decl": "()"
          },
          {
            "name": "PackageHashMissing",
            "discriminant": 4,
            "decl": "()"
          },
          {
            "name": "PackageHashNotPackage",
            "discriminant": 5,
            "decl": "()"
          },
          {
            "name": "InvalidEventsMode",
            "discriminant": 6,
            "decl": "()"
          },
          {
            "name": "MissingEventsMode",
            "discriminant": 7,
            "decl": "()"
          },
          {
            "name": "Phantom",
            "discriminant": 8,
            "decl": "()"
          },
          {
            "name": "FailedToGetArgBytes",
            "discriminant": 9,
            "decl": "()"
          },
          {
            "name": "InsufficientRights",
            "discriminant": 10,
            "decl": "()"
          },
          {
            "name": "InvalidAdminList",
            "discriminant": 11,
            "decl": "()"
          },
          {
            "name": "InvalidMinterList",
            "discriminant": 12,
            "decl": "()"
          },
          {
            "name": "InvalidNoneList",
            "discriminant": 13,
            "decl": "()"
          },
          {
            "name": "InvalidEnableMBFlag",
            "discriminant": 14,
            "decl": "()"
          },
          {
            "name": "AlreadyInitialized",
            "discriminant": 15,
            "decl": "()"
          },
          {
            "name": "MintBurnDisabled",
            "discriminant": 16,
            "decl": "()"
          },
          {
            "name": "CannotTargetSelfUser",
            "discriminant": 17,
            "decl": "()"
          },
          {
            "name": "InvalidBurnTarget",
            "discriminant": 18,
            "decl": "()"
          }
        ]
      }
    },

It is a representation of a following rust enum:

pub enum Cep18Error {
    /// CEP-18 contract called from within an invalid context.
    InvalidContext,
    /// Spender does not have enough balance.
    InsufficientBalance,
    /// Spender does not have enough allowance approved.
    InsufficientAllowance,
    /// Operation would cause an integer overflow.
    Overflow,
    /// A required package hash was not specified.
    PackageHashMissing,
    /// The package hash specified does not represent a package.
    PackageHashNotPackage,
    /// An invalid event mode was specified.
    InvalidEventsMode,
    /// The event mode required was not specified.
    MissingEventsMode,
    /// An unknown error occurred.
    Phantom,
    /// Failed to read the runtime arguments provided.
    FailedToGetArgBytes,
    /// The caller does not have sufficient security access.
    InsufficientRights,
    /// The list of Admin accounts provided is invalid.
    InvalidAdminList,
    /// The list of accounts that can mint tokens is invalid.
    InvalidMinterList,
    /// The list of accounts with no access rights is invalid.
    InvalidNoneList,
    /// The flag to enable the mint and burn mode is invalid.
    InvalidEnableMBFlag,
    /// This contract instance cannot be initialized again.
    AlreadyInitialized,
    ///  The mint and burn mode is disabled.
    MintBurnDisabled,
    CannotTargetSelfUser,
    InvalidBurnTarget,
}

This spec supports algebraic data types to support languages that support this feature natively (i.e. Rust). This means that for variants that contain information (i.e. enum ComplexEnum { Foo, Bar(u64) } the variant with data will be also preserved.

Serialization for enums is defined here https://borsh.io/ (same as casper's bytesrepr)

  • Each variant has it's own u8 tag
  • If enum variant contains fields, then all the fields are serialized after the tag

from casper-contract-schema.

zie1ony avatar zie1ony commented on July 19, 2024

added in the latest version

from casper-contract-schema.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.