Seed - Development - Modules, Smart Contracts & The SVM

in #utopian-io3 years ago (edited)

SeedModulesAndSmartContracts

Repository

https://github.com/Cajarty/Seed


Overview

As per the "Module, Smart Contracts & The SVM" design article, this contribution includes the implementation for Smart Contracts (Modules), the Seed Virtual Machine (SVM), and the virtualized ledger, as well as a basic module unit testing framework and a basic Seed module cryptocurrency implementation.

The relevant pull request can be found here.


Modules (Smart Contracts)

The base Module class can be found in the module.js file, with seed.js being a solid example implementation showcasing how to use it.

Modules contain local state data, state data for each user, a name, and invokable functions. These functions can be either state-modifying functions or getter functions.

To create a module, a developer would require the module.js file and invoke the export function. They would pass the require information for creation into the constructor under the "info" parameter. Below is a simple example of creating a module, who's state is one variable anyone can increment.

let module = require("./seedSrc/module.js").createModule({
    module : "ExampleModule",
    initialData : { 
        stateVariable : 0
    }
    functions : {
        incrementCounter : function(container, changeContext) {
            changeContext.add(1, { key : "stateVariable" });
            return changeContext;
        },
        getCounters : function(container) {
            return container.getModuleData("stateVariable");
        }
    }
});

This module has one local variable, "stateVariable", which is incremented any time any user invokes the "incrementCounter" function. "getCounters" is a getter function which returns the variable.


Seed Virtual Machine (SVM)

The SVM is a pseudo-VirtualMachine used to manage the modules and leger. The SVM can be found in the virtualMachine.js file.

To add a module to the virtualMachine, you simple require the file, call them "getVirtualMachine" exported function, and then call the "addModule" function.


Container

The Container class is the "read-only" connection between a modules function and the world state. This container is the first parameter in any state-modifying or getter functions added to a module. The container allows users to read any modules data (even other modules), any user's data (for any module), as well as get deterministic pseudo-random integer/floats, access any modules getter functions, and even access a local-modules state-modifying function (passing it a changeContext, which it will modify and return back the results).


ChangeContext

The ChangeContext class is the "write-only" connection between a modules function and the world state. Any function which intends on modifying the world state (therefore, non-getter functions) requires a ChangeContext parameter as the second parameter.

The ChangeContext has functions that wrap modifying the world state, such as "add"/"subtract" and "set" methods.


Ledger

The Ledger is a virtualized ledger class that mimics what the state of the world currently is for the SVM. The ledger can have a ChangeContext applied to it, which modifies the current state by whatever the ChangeContext intends on changing. The only real logic in it is the "applyChanges" function, which takes a ChangeContext object and modifies its state by the values in the ChangeContext.


The Seed Module

The Seed module is an ERC-20 implementation of a cryptocurrency ported over to the Seed ecosystem. This standard was copied due to its proven logic and being easily testable.

This interface includes methods for transfering SEED, giving allowances, transfering on other users behalfs, getting the symbol/total supply/decimals,etc. The full ERC-20 compliant standard.

A full testing scenario with the Seed module can be found in the scenarioTest.js file.


ModuleTester

The ModuleTester is a class to help with scenario testing/unit testing modules. It's a simple class which just helps with simulating functions being invoked, modifying the ledger accordingly, and asserting what the values should be. The scenarioTest.js file runs through scenarios which utilize the tester.


GitHub

https://github.com/CarsonRoscoe

Pull Request

https://github.com/CarsonRoscoe/Seed/pull/2

Sort:  

I didn't dig too deep in the project but purely from a developer point of view the quality of your code is really great especially regarding the comments. They have a real value and help understand your code and how to use it.

However a few things are kind of weird.
Why did you commit your node_modules folder ? You shouldn't do that.
The files .DS_Store should also be excluded. If I'm not mistaken they only exist for Apple devices right ?
Add a .gitignore file to help you with all that.

Your package.json files are weird too.
This one for instance https://github.com/CarsonRoscoe/Seed/blob/develop/seedSrc/package.json the dependencies don't match the content of the node_modules folder. The entry point seed.js doesn't exist either. The scripts only has a test command.

Good luck for your project!

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @gregory.latinier
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thank you for the kind words!

This is my first project being created using NodeJS, and I realize now I messed up with setting up the modules and such. I'll fix up the gitignore for the .DS_Store and node_modules folder, since users should be able to simply install all the modules themselves upon cloning.

As for the package.json file, that was a mistake upon the initial project creation. The client is launched from the /clientSrc directory using "npm start", however I see now that I most likely did an improper npm init in different places when first building the project, which is the package.json file you're referring to.

Really, there should be two npm packages, one for /clientSrc for the Electron client, and one for /seedSrc to become a publicly available npm package.

I'll make sure to have that all cleaned up for the next contribution, as well as add a "How To Run" into the README to clarify users should launch from the /clientSrc folder in order to launch the client.

Thank you again for the kind words and the helpful advice! Have a wonderful day,

~ Carson Roscoe

Hey @carsonroscoe
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Loading...

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by carsonroscoe from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Fantastic work with this one, kudos.

Any function which intends on modifying the world state requires a ChangeContext parameter as the second parameter

Error is just as important a condition of life as truth. Carl Gustav Jung

Modules contain local state data, state data for each user, a name, and invokable functions

I’ve failed over and over and over again in my life and that is why I succeed. Michael Jordan

Ru
Приветствую. Представляю студию блокчейн-копирайтинга. Предлагаю наши услуги по написанию WhitePaper, созданию видеороликов , написанию уникальных статей и обзоров (в том числе для steemit), переводов. Огромный опыт и большой штат сотрудников. Списко наших услуг и портфолио в нашем телеграм канале или на сайте.
En
Hello everyone! I present to you our blockchain copywriting studio. We propose to you next services: writing WhitePaper, creating videos, , translations, writing unique articles and reviews (Including for steemit). Extensive experience and highly qualified team. List of services and portforlio in our telegram channel or on our website.

Congratulations @carsonroscoe! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Round of 16 - Day 4


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!