3

I want to calculate the ram usage when I conduct an action in contract, if ram is over the limit, I will let the action fail automatically.

How can I get the current ram usage in contract?

1

From resource_limits.cpp, it seems like in eosio code, they use _db to retrieve the resource consumption, which needs access to the chainbase database running.

As far as I know, this cannot be accessed from smart contract directly, thus, assuming all RAM usage for your contract can be tracked, two suboptimal solutions would be:

  • Calculate the RAM consumption every time by iterating through all the table entries paid for by the account, and estimating the costs based on the data stored in each row (possibly infeasible during one transaction;)
  • Keep the RAM costs in a singleton by updating it for every new entry in the table, so it doesn't have to iterate through all tables every time (for already deployed contracts, it needs an action to calculate existing RAM or just pass it as an inital value based on cleos get account data)

Interested in hearing about better solutions as well.

  • However, when you transfer money to others through your smart contract, you can't know how much ram you paid for others, because they can consume your ram by their code – Jimmy Guo Aug 20 '18 at 2:02
  • @JimmyGuo yeah perhaps you could have the contract not interact directly with any contracts you don't control, though that may be a pain – confused00 Aug 20 '18 at 8:08

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.