2

My Event outputs an indexed parameter uint id.

Can I query the logs for the max value of id, without returning and searching through all log records?

I'm guessing not, since indexed values are stored as a hash so value comparisons would be impossible, but maybe I'm missing something.

How else could a single uint value be "cached" to the logs without having to retrieve all logs of that type?

1 Answer 1

0

Unless you store the value in the contract, you will have to loop over all events. That being said you would have to have a pretty full node to keep a log of all events ever (normally logs are pruned from nodes when they get too old).

What might be a simpler solution (gas costs not withstanding) would be to store the max value in your contract.

// store the max val
uint maxVal = 0;

// an event to notify that val has changed
event ValChange(uint _val);

function storeValue(uint _val) {
  // store biggest _val
  if (_val > maxVal) {
     maxVal = _val;
  }
  // trigger event
  ValChange(_val);
}
1
  • 1
    Looks like I'll stick with the contract cache then. Was trying to avoid the gas cost. Commented Dec 17, 2017 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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