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

0

Well, how about save data directly to disk and not using RAM, we can do this?

  • 1
    Check this out: steemit.com/eos/@eosio/… Unfortunately, SSDs are about 2500x slower than RAM and forcing the operating system to “swap” to SSD can absolutely kill performance as many Steem witnesses recently discovered when Steem upgraded to chainbase. This is also a good read: medium.com/@bytemaster/… – Nat Nov 29 '18 at 5:14
  • Thank you very much, I know SSDs are slower than RAM but because price of RAM is very expensive, so I want to using SSD intead of RAM. Or any solutions can be swap out RAM. – user2644 Nov 29 '18 at 6:20
  • @Nat, I see overhead_per_account_ram_bytes in EOS's code, It's not necessary so why EOS does not discard this? – user2644 Nov 29 '18 at 6:29
  • That I'm not sure of. You can ask @tbfleming in t.me/joinchat/Esi1OkPktgcFeJ3Lmlcrqg though – Nat Nov 29 '18 at 7:05
  • If I do not care performance, Can I do not install chain_plugin into nodes and I use mongodb which can store user state – user2644 Nov 30 '18 at 4:07

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.