0

I'm confused while understanding the local variable _txId is used in several functions and modifiers, do they all have the same value or are they independent for each call.

I read that local variables are independent but i'm having a hard time understanding that here.

This is a part of my contract for referance.

    modifier notExecuted(uint _txId) {
        require(!transactions[_txId].executed, "tx already executed");
        _;
    }

    constructor(address[] memory _owners, uint _required) {
        require(_owners.length > 0, "owners required");
        require(_required > 0 && _required <= _owners.length,
        "invalid required number of owners"
        );

        // Stating _owners to state variable owner
        for (uint i; i < _owners.length; i++) {
            address owner = _owners[i];
            
            require(owner != address(0), "invalid owner");
            require( !isOwner[owner], "owner is not unique");

            isOwner[owner] = true;
            owners.push(owner);
        }

        required = _required;

    }

    receive() external payable {
        emit Deposit(msg.sender, msg.value);
    }

    // Function to submit transactions for approval
    function submit( address _to, uint _value, bytes calldata _data)
        external
        onlyOwner
        {
            transactions.push(Transaction({
                to: _to,
                value: _value,
                data: _data,
                executed: false
            }));
            emit Submit(transactions.length - 1);
        }

    // Function for each approval
    function approve(uint _txId)
        external
        onlyOwner
        txExists(_txId)
        notApproved(_txId)
        notExecuted(_txId)
        {
            approved[_txId][msg.sender] = true;
            emit Approve(msg.sender, _txId);
        }

    // Counting the number of approvals
    function _getApprovalCount(uint _txId) private view returns (uint count) {
        for (uint i; i < owners.length; i++) {
            if (approved[_txId][owners[i]]) {
                count += 1;
            }        
        }
    }
1
  • There's a scope where a variable is valid. For example the code uses i as counter in the for loops so the variable only exists inside them. Likewise _txId is defined at modifier/function level so it only exists inside that modifier or function. I'll suggest to study the concept en.wikipedia.org/wiki/Scope_(computer_science).
    – Ismael
    Commented Aug 1, 2022 at 13:43

1 Answer 1

0

Actually local variables are declared inside the function, they exist only when the function is called. They are stored in stack, when the function is done the data stored will be gone.

For runtime operations i.e, for storing local variables ,OP codes and such EVM has access to 2 storage locations,

  • Stack
  • Memory

Both of these memory are volatile memory. The persistent memory used by EVM is called as Storage(Account). These are the three main types of storage available for contracts and the EVM.

Long story short this was a stupid question from me.

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.