Home > Blockchain >  Caching data if connection is down
Caching data if connection is down

Time:02-15

I've got a server that I'm relaying every action that I want to do with a smart contract, but let's say the server can't connect to the smart contract (the connection is down). How would I cache such actions and execute them once the connection is up.

CodePudding user response:

I don't know much about smart contracts and the blockchain, but it sounds like you need some kind of persistent data store. Like a relational database. Perhaps you could use something like node-postgres and some kind of task scheduler, such as node-cron, to query for and execute the cached actions every so often.

As I said, I know next to nothing about smart contracts, so maybe this kind of caching is contradicting the decentralization principles of the blockchain and such.

Edit: If the volume is low enough you may simply be able to cache in-memory using a Map or Object, and then still probably use some kind of task scheduling utility.

CodePudding user response:

Interaction with smart contract (SC) has 2 types:

  • Call to update status (write data)
  • Call to view function (read data)

In order to do the write data, we need:

  • SC address
  • Method name
  • Data option
  • Parameters
  • Sender

So we can organize into a JS object

{
 'address': 0x..,
 'method': 'transfer',
 'dataOption': { 
    'from': '0x...',
    'value': 100 (wei)
  },
 'params': ['param1', 'param2', ...]
}

Then you can determine where should we use it depends on the use cases:

  • Can be store at server using: SQL, MongoDB, json file...
  • Can be store inside the front-end using: storage
  • Related