Does emitting an event modify the state?

Hey there, as I was testing events and errors, I thought the following function is a pure function, however, the compiler issues a TypeError that the line emit ConcatStrings(str1, str2, newString); potentially modifies the state. I’m wondering why. Thanks.

// SPDX-License-Identifier: GPL-3.0

pragma solidity 0.8.4;

contract Test {

    event ConcatStrings(string str1, string str2, string newString);
    error EmptyStringsNotAllowed(string str1, string str2);

    function concatStrings(string memory str1, string memory str2) public pure returns(string memory) {
        if (keccak256(abi.encodePacked(str1)) == keccak256(abi.encodePacked("")) ||
            keccak256(abi.encodePacked(str2)) == keccak256(abi.encodePacked(""))) {
            revert EmptyStringsNotAllowed(str1, str2);

        string memory newString = string(abi.encodePacked(str1, str2));
        emit ConcatStrings(str1, str2, newString);
        return newString;

Because this function modifies contract’s state in Ethereum database by emitting ConcatStrings event. Events should be written on disk, and this is an I/O operation, which is not pure.


Hey @rumkin Thanks for the answer. Although an event is not changing state variables, since it involves an on-chain writing operation, it is still considered a state modification, correct? I thought a state is only about state information of accounts, EOA or smart contracts. Other on-chain information, if not directly associated with an account, is not a state variable, for example, gasPrice, chainid, code length, etc. I wish I could find a more formal definition of a state.

The main distinction between view / pure and other functions are that, external calls to such function can be staticcall instead of call. Events in EVM corresponds to log* opcode, and staticcall will revert when a log* opcode is executed (see EIP-214: New opcode STATICCALL). In other words, events modify the blockchain state.