gofractally / contract-lab Goto Github PK
View Code? Open in Web Editor NEWCreate, Test, and Debug Mandel Contracts
Home Page: https://gofractally.github.io/contract-lab/book/
License: MIT License
Create, Test, and Debug Mandel Contracts
Home Page: https://gofractally.github.io/contract-lab/book/
License: MIT License
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.
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:
Add an object with custom validation logic:
struct uid {
uid();
uid(const uint32_t account_id);
void validate();
uint32_t id;
};
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"));
}
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);
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:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.