When I look at it in general, everywhere it says calldata is used only with external visibility, but I can also use it with public while writing a contract? Because when I use public, there is no problem.
In general, I see less use of calldata in contracts. Not used much ?
Is calldata our abi coded information that specifies the function and parameters to be called to the contract?
Based on my third question, do msg.data and calldata have the same meanings?
calldatacan typically only be used with functions that have
externalvisibility. However, Solidity will let you declare any function with
calldataas long as you call it with
calldataarguments. This means that the source of these arguments needs to come from message calldata, directly or indirectly (i.e. by passing forward
calldatacan be useful! It helps lower gas consumption by skipping a copy of message calldata into memory.
- Precisely! :)
msg.datais the Solidity equivalent to the calldata concept in the EVM.
The Solidity keyword
calldata tells the compiler that you want to use values directly from message calldata (i.e.
msg.data) and not have it copy to memory (or worse, storage) and cost additional gas.
The concept of calldata extends much beyond Solidity as an EVM term.
Here is a useful excerpt from an OpenZeppelin blog post:
The calldata is a read-only byte-addressable space where the data parameter of a transaction or call is held. Unlike the stack, to use this data you have to specify an exact byte offset and number of bytes you want to read. The opcodes provided by the EVM to operate with the calldata include:
- CALLDATASIZE tells the size of the transaction data.
- CALLDATALOAD loads 32 bytes of the transaction data onto the stack.
- CALLDATACOPY copies a number of bytes of the transaction data to memory.