cadence icon indicating copy to clipboard operation
cadence copied to clipboard

Expose functions/properties to query current gaz data

Open hmatthieu opened this issue 3 years ago • 0 comments

Issue To Be Solved

Expose functions/properties to query current gaz data in cadence.

For now, it's impossible to throttle a computation based on remaining gaz for this computation. For example, let's say I have a collection, and a contract exposes a function to iterate over this collection to render it. The render function could paginate its result based on how much gaz is available.

SomeContract.render(myCollection): {
  // Classic pagination data
  pagination: {
     offset: UInt64,
     limit: UInt64,
     total: UInt64
  },
  // Rendered items
  items: [String] 
};

Small implementation example :

contract SomeContract {
  pub fun render(collection: Collection, _offset: UInt64?): PaginatedResult {

    let items: [String] = [];
    let offset = _offset ?? 0;
    var i = offset;
    let gazToKeep = 100;
    let loopCost = 10;

    while(i < collection.length && getGazLeft() - loopCost > gazToKeep) {
      items.push(renderItem(collection[i]));
      i++;
    }

    return {
      pagination: {
        offset: offset,
        limit: i - offset,
        total: collection.length
      },
      items: items
    }
  }
}

Suggest A Solution

I was first thinking about some base property for transaction, something like

transaction() {
  execute {
    log(self.gaz.limit);
    log(self.gaz.consumed);
  }
}

But I think something more global like a built-in function or built-in property could be better to access this property from anywhere.

FYI, Solidity uses block.gazlimit and gasleft() to access gaz data (I'm not saying that it should be implemented that way, it's just interresting to check existing solutions). https://docs.soliditylang.org/en/develop/units-and-global-variables.html#block-and-transaction-properties

hmatthieu avatar Mar 24 '21 09:03 hmatthieu