Comments (3)
Michal has agreed to describe the approach to this.
from casper-contract-schema.
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.
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 eitherPrimitive
,Sequence
,FixedSequence
,Tuple
,Enum
,Struct
. Enum
is an object and contains arrayitems
that describes the variantname
is the stringified name of the variantdiscriminant
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.
added in the latest version
from casper-contract-schema.
Related Issues (6)
- Payable HOT 2
- Prevent plain CLTypes to be events. HOT 1
- Storing events HOT 1
- Storage layout HOT 2
- WIT as interface definiton. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from casper-contract-schema.