1

I found that storing data in EOS RAM is sometimes expensive in turn makes it impractical. Can I use MongoDB for storing such data locally instead of storing in RAM?

2

The Mongo_DB_plugin can be configured to listen to actions on chain from a locally hosted node. This can be resource intensive in processing large blocks, you don't have the option to listen through an API, and the options for storing data are limited to 5 options:

--mongodb-store-blocks                Enables storing blocks in mongodb.
                                        Defaults to true.
--mongodb-store-block-states          Enables storing block state in mongodb.
                                        Defaults to true.
--mongodb-store-transactions          Enables storing transactions in mongodb.
                                        Defaults to true.
--mongodb-store-transaction-traces    Enables storing transaction traces in                                             mongodb.
                                        Defaults to true.
--mongodb-store-action-traces         Enables storing action traces in mongodb.
                                        Defaults to true.

If you want to use MongoDB to store and handle logic that pertains to your smart contract, then I would recommend using the demux-js library. The advantages of using demux-js over the plugin is that demux-js allows you to pull the block data and extract only the information you want so you can store it the way you want as well as allowing you to store the data in any DB you like.

You accomplish this by listening to the chain (either through an API like me or by hosting a node locally).

This architecture essentially only uses the require_auth() C++ API to authorize the transaction by ensuring the person sending the action owns the private key. With that, you would also move your DB logic from the smart contract to your back end for a more traditional setup, removing the need to interact with the Multi Index container altogether.

So for example, in my simple chess application, I listen for a setmove action from the account that hosts my contract on chain and then pull the account_name and the status: win or loss from the actions so that I can update my database.

Contract:

void Chess::setstat(account_name account, string status) {
        require_auth(account);

        // print to node
        print( name{account}, " ", status); // "win" or "loss"
    }

Demux:

const updaters = [
    {
      actionType: "chesschessch::setstat", // contract_name::action_name
      updater: updateMatchStatus,
    },
  ]

App.js:

// DEMUX SETUP - setting up API to query for new actions
const actionReader = new NodeosActionReader(
    "https://poll.troonblockchain.io:9999", // Locally hosted node needed for reasonable indexing speed
    22238967, // First actions relevant to this dapp happens at this block
)

const actionHandler = new MyActionHandler(
    updaters,
    effects,
)

const actionWatcher = new BaseActionWatcher(
    actionReader,
    actionHandler,
    250, // Frequency to ping API for new actions in milliseconds, so twice the chains speed (half second blocks)
)

actionWatcher.watch() // Start watch loop

Demux architecture photo from repo:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.