Coder Social home page Coder Social logo

gofractally / contract-lab Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 2.0 2.34 MB

Create, Test, and Debug Mandel Contracts

Home Page: https://gofractally.github.io/contract-lab/book/

License: MIT License

CMake 3.15% C++ 91.76% Dockerfile 0.11% C 1.96% TypeScript 2.79% JavaScript 0.05% Shell 0.18%

contract-lab's People

Contributors

brandonfancher avatar cool-ant avatar james-mart avatar sparkplug0025 avatar swatanabe avatar tbfleming avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

contract-lab's Issues

consider: standard action parameter validation

Standard Action Parameter Validation Proposal

Problem

A parameter of a specific type often has the same validation logic in each action in which its included. For example, if there exists a type RegisteredUser that wraps a uint64_t, then every action that takes a RegisteredUser would need to verify that the value passed in matches an actual registered user.

This forces the developer to repeat the same validation logic in many actions, increasing the RAM size of the contract executable. Furthermore, it bloats unit tests, since every test suite of a particular action would need a test case verifying that the action correctly reacts to improper values passed into this action.

Solution Description

A solution should allow a developer to define validation logic on custom types in one place, and anywhere that object is constructed as an action parameter would automatically run the validation logic. This would:

  • Be less error prone, increasing contract security
  • Result in smaller binaries, decreasing developer cost
  • Be easier to write, decreasing development time

Solution Implementation

In contract

Add an object with custom validation logic:

struct uid {
    uid();
    uid(const uint32_t account_id);
 
    void validate();
 
    uint32_t id;
};

In Unit Tests

Because the “validate” function exists, it will automatically run whenever an object of this type is created before passing it into an action handler.

This obviates the need for tests like this:

THEN("Alice may not transfer to a nonexistent recipient")
{
    CHECK(failed_with(alice.trace<eosio_nft::actions::transfer>(nft_id_a, 
          uid("alice"_n), uid{}, "Enjoy"), "Empty UID object"));
}

In Dispatcher

   template<typename T>
   concept has_validate = requires {
      { std::declval<T>().validate() } -> std::same_as<void>;
   };
 
   template<typename T>
   void validate_param(T param) { /* NOP */ }
 
   template<typename T>
   requires has_validate<T>
   void validate_param(T param)
   {
      param.validate();
   }

In execute_action:

    std::tuple<std::decay_t<Args>...> args;
    datastream<const char*> ds((char*)buffer, size);
    ds >> args;

   // Validate parameters that support validation
    std::apply([&](auto&&... param) {
        ( ( validate_param(param) ) , ...);
    }, args);
 
     T inst(self, code, ds);

Other Considerations

Thanks to @tbfleming for the input

In a specific case where the object with custom validation is just a simple wrapper around a primitive type, serializing it as a custom object forces the front-end devs to pass an object as a parameter to a smart contract rather than just the underlying value.

They shouldn’t need to know or care about the fact that there is custom validation going on. The ABI should only tell them about the underlying type and allow them to pass in the underlying type.

To achieve this, we would need:

  • Custom to_json and from_json logic written for all types with custom validation (Maybe able to automate this such that devs only need to inherit from a special type)
    • As seen in asset.hpp in abieos
  • Update to the get_type function in abi_generator.hpp in clsdk/eosiolib/contracts/

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.