Contact Us

Contact Us



Pleas confirm by checkbox


Uncategorized

EOS Demux using PostgreSQL

Author_img
By Hemant Sachdeva September 11, 2018

I have been looking for EOS demux implementation using PostgreSQL but found none. So I thought of writing this short tutorial explaining how we can implement EOS demux using PostgreSQL. Those who don’t know about EOS Demux please have a look at the article explaining the same in a very intuitive way.  https://github.com/EOSIO/demux-js.

 

A short explanation of EOS Demux: Demux architecture allows application developers to use traditional Mongo or Postgres SQL databases in a way that means the data stored in them is still verifiable by the blockchain. This enables the best of both worlds: the flexibility and speed of traditional databases, coupled with the trust and immutable properties of a blockchain. The overall flow of an application looks as follows:

 

To use PostgreSQL as a data store we need to use massive.js. So go ahead and install massive.js using the following command

> npm i massive --save
To demonstrate the working of PostgreSQL based demux implementation we are going to use a very basic contract. The contract is about a blog, using which user can do CRUD operation on the blog. As blog data is big we are going to store the blog data into PostgreSQL and metadata related to the blog like user, email etc. will be a part of the blockchain. Blockchain maintains all the information which
are required to make blog entry verifiable and secure. Following is the contract.
#include <eosiolib/eosio.hpp>

#include <eosiolib/multi_index.hpp>

#include <eosiolib/types.hpp>

#include <eosiolib/print.hpp>

#include <eosiolib/system.h>

//using namespace eosio;


class blog_contract : public eosio::contract{

    public:


    blog_contract(account_name self) : eosio::contract(self),

          bloges(self, self){}


        /// @abi action

        void create(account_name author, uint64_t id, std::string email, const std::string& data){

            require_auth(author);

            blogs.emplace(author, [&](auto& new_blog) {

                new_blog.id = id;

                new_blog.author = author;

            });

        }

        //.. destroy, show and update actions .. 


    private :

        /// @abi table blog i64

        struct blog{

            uint64_t id;

            uint64_t author;

            uint64_t primary_key() const {

                return id;

            }

            EOSLIB_SERIALIZE(blog,(id)(author));

        };

        typedef eosio::multi_index<N(blog), blog> blog_table;

        blog_table blogs;

};


EOSIO_ABI( blog_contract, (create)(destroy)(show)(update) )
If you look closely at ‘create’ function of the contract, you would notice that the function’s signature contains blog data as one of the parameters but we are not storing the blog data as part of EOS’ database. The purpose of having blog data as part of the function signature is that it gets available to be stored in PostgreSQL. Whatever we mentioned as function parameters, those parameter’s data will be available to the action handler. Looking at the diagram ‘Action watcher’ keeps looking for any change in blockchain via ‘Action Reader’. Whenever there is any action happened in the Blockchain, Action watcher delegate that action to ‘Action Handler’. Then ‘Action Handler’ further calls ‘updaters’ and ‘effects’ which actually execute necessary action code. Based upon the discussion the configuration looks as follows:
//First get the postgreSQL instance via massive as follows:
const massive = require('massive');

let db;

massive({
 host: host,
 port: port,
 database: database, 
 user: user,
 password: password 
}).then(instance => {
 db = instance;
 return Promise.resolve(db);
}).catch(e => {
 console.log(e)
 console.log('error while getting massive instance')
 });
Let’s define Updaters and Effects
//Updaters
function createBlog(db, payload, blockInfo, context){
 db.blog_data.insert({
 id : payload.data.id,
 author : payload.data.author,
 data : payload.data.data, //Storing blog data
 email : payload.data.email
 }).then(new_blog => {
 console.log('blog created')
 }).catch(err => {
 console.log('error while inserting data')
 })
}

//Register blockchain Actions to which this update will be called.

const updaters = [{
 actionType: "blogger.p::create",
 updater: createProfile}
]

module.exports = updaters
//Effects
function logUpdate(state, payload, blockInfo, context) {
 console.info(“blog created\n")
}
const effects = [
{
 actionType: "blogger.p::create",
 effect: logUpdate,
}
]

module.exports = effects
Now create MassiveActionHandler, Action Reader, and Action Watcher as follows:
const {
 readers: {
 eos: { NodeosActionReader }
 },
 handlers :{
 postgres : { MassiveActionHandler }
 },
 watchers: { BaseActionWatcher }
 } = require('demux-js')

const actionHandler = new MassiveActionHandler(
 updaters,
 effects,
 db
)

const actionReader = new NodeosActionReader(
 httpEndpoint,
 0, // Start at most recent blocks
)


const actionWatcher = new BaseActionWatcher(
 actionReader,
 actionHandler,
500,
)
actionWatcher.watch()
You also need to make sure the following table exists in PostgreSQL, which keeps track of block number up to which action has already been taken.
CREATE TABLE _index_state (
 id serial PRIMARY KEY,
 block_number integer NOT NULL,
 block_hash text NOT NULL,
 is_replay boolean NOT NULL
);
Related posts
VPC Sharing Using AWS RAM (Resource Access Manager)
Uncategorized

VPC Sharing Using AWS RAM (Resource Access Manager)

By shekhar.wagh June 18, 2019
Reduce App size with On Demand Resources
Uncategorized

Reduce App size with On Demand Resources

By shekhar.wagh May 29, 2019
iMessage Stickers and Apps
Uncategorized

iMessage Stickers and Apps

By shekhar.wagh May 27, 2019
What is UX Writing?
Uncategorized

What is UX Writing?

By shekhar.wagh April 08, 2019
AWS ECS (Amazon Elastic Container Service )
Uncategorized

AWS ECS (Amazon Elastic Container Service )

By shekhar.wagh March 26, 2019
Scala code analysis and coverage report on Sonarqube using SBT
Uncategorized

Scala code analysis and coverage report on Sonarqube using SBT

By shekhar.wagh March 19, 2019
Introduction to Akka Streams
Uncategorized

Introduction to Akka Streams

By shekhar.wagh March 08, 2019
Decentralized Applications - Utilizing the Power of Blockchain Technology
Uncategorized

Decentralized Applications - Utilizing the Power of Blockchain Technology

By shekhar.wagh March 05, 2019
App Store Connect API To Automate TestFlight Workflow
Uncategorized

App Store Connect API To Automate TestFlight Workflow

By shekhar.wagh February 28, 2019
Using Custom Metrics for CloudWatch Monitoring
Uncategorized

Using Custom Metrics for CloudWatch Monitoring

By shekhar.wagh February 06, 2019

Stay updated

Get the latest creative news from Fubiz about art, design and pop-culture.