7

Looking through posts here most refer to the doc saying that smart contracts can't access event data.

The Log and its event data is not accessible from within contracts (not even from the contract that created them).

But I didn't find a mention to why is that,if it's cheaper to store data in event logs why aren't contracts able to read it,is it a choice made by the developers or is there something that I missed about events which makes them unusable for smart contracts,I think being able to interact with event logs, even if it's read only, would be pretty useful.

3 Answers 3

5

This is second-hand information and speculation. I tried to reach out to people who did the original doc.

  • Logs were intended to be used as a proof for third party systems that something happened in a blockchain. Now logs are used by bridges between different blockchains for this purpose.

  • Logs were intended to be purgeable i.e. full nodes have logged only for X years - this is why the gas cost is lower.

  • Logs are indexed so they are easy to search on the client-side (e.g. all transactions to this address). However, such queries would be too expensive in smart contracts.

  • Because logs may or may not be there, they cannot be accessed from the state that needs to be deterministic

Note that some other blockchains do not have the log concept on the full node level. For EOS, you need to have a third-party application to index the blockchain events to have useful output from it for applications like explorers, dashboards and such.

For example, free CloudFlare Ethereum node stores events only worth of 128 blocks.

2

But I didn't find a mention to why is that,

because first: you don't need that in a contract, and event is a message from the inside to the outside, why would a contract need to access events of other contracts? Only for some weird use case. Second, you would spend a lot of Gas and your transaction wouldn't fit inside a block.

if it's cheaper to store data in event logs why aren't contracts able to read it,

they way to stored data is by SSTORE instruction. Please use the system as it was conceptually designed.

that I missed about events which makes them unusable for smart contracts,

contracts don't need to read events

read only, would be pretty useful.

not useful at all, the universe of a contract is too small to handle the information of the entire blockchain

4
  • Well let's say you need to know what the state of a certain variable was at a certain point in time, won't using an event save lots of gas ? But with your reply I think the answer to my original questions would be that it was a design choice ? and not like someone else suggested that events weren't meant to be permanentely stored on the blockchain. I still feel like I can see a lot of places where it would be useful to use events since (i think) they save up gas costs ( but maybe that's the reason you can't access them from smart contracts so people don't start using them as memory) Jul 7, 2021 at 11:20
  • 2
    events are cheaper because the developers set such costs, not because it is cheaper to write events to the database. costs of instructions are constantly being updated to find the best cost-benefit for the user/miner. We could set gas limit to infinite and have transactions running for free, so the matter of costs is political , not technical
    – Nulik
    Jul 7, 2021 at 12:27
  • Thanks,this clears up some of the questions i've had,but even if costs are set like for political reasons I think there is still some technical reasoning behind it,I read on this post ethereum.stackexchange.com/questions/73777/… , that writing to events is indeed cheaper than using the sstore instruction, by cheaper here I mean in terms of like computational power and stuff like that not just gas cost Jul 7, 2021 at 14:22
  • I think i've found the answer to my questions here as @Lauri Peltonen wrote " Emitting events does not modify any state (at least not any state which could influence contracts)" This would suggests that events are cheaper because they don't influence contracts,which explains why we can't access them inside of smart contracts because if we would than they will start being able to influence the contracts and would become more expensive (at least that's what I understood i am not very sure about what i am saying) Jul 7, 2021 at 14:25
2

Events use one of the LOG operations. The output of LOG operations is not accessible from smart contracts by design - it allows clients to avoid storing event/log data as an optimization and as a result the LOG operation has a low gas cost reflecting the reduced hardware cost. In other words, SSTORE is expensive because it provides a guarantee that smart contracts can access the data later on, LOG is cheap because the client can "forget" about the data after it has processed the event.

Your Answer

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

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