2.16 Block & Transaction Properties
Solidity
allows accessing various block and transaction properties within smart contracts. These allow developers to perform interesting logic that are dependent on different aspects of the current block or the transaction.
Block
In the case of block
, we have the following members:
blockhash
: gives the hash of the specified block, but only works for the most recent 256 ones, otherwise it returns zero.chain id
: gives the current id of the chain that this is executing on.number
: gives the sequence number of the block within the blockchain.timestamp
: gives the number of seconds since the unix epoch.coinbase
: it is controlled by the miner and gives the beneficiary address where the block rewards and transaction fees go to.difficulty
: block's difficulty related to the proof of work.gaslimit
: Gas limit for the block.
Randomness Source
The block timestamp and block hash that we just discussed are not good sources of randomness, that's because both these values can be influenced by the miners mining the blocks to some degree. The only aspects of timestamps that are guaranteed, is that the current blocks timestamp must be strictly larger than the timestamp of the last block and the other guarantee is that it will be somewhere between the timestamps of two consecutive blocks in the canonical blockchain. Therefore smart contract developers should not rely on either the block timestamp or the block hash as a source of good randomness.
Message and Transaction
There are also fields related to the message (msg
):
value
: represents the amount of Ether that was sent as part of the transaction.data
: gives access to the complete call data sent in this transaction.sender
: gives the sender of the current call or message.signature
: gives the function identifier or the first four bytes of the call data representing the function selector that we talked about earlier.
The thing to be kept in mind is that every external call made, changes the sender
. Every external call made can also change the value
.
So if we have three contracts A
, B
and C
where A
calls B
and B
calls C
, in the context of the contract B
the msg.sender
is A
, but in the context of the contract C
the msg.sender
is contract b
and not a
.
These aspects should be kept in mind when analyzing the security of smart contract because the developers could have made incorrect assumptions about some of these that could result in security issues.
Then there are transaction (tx
) components:
gasprice
: the Gas price used in the transaction. There is an interestingSolidity
native function calledgasleft()
which returns the amount of Gas left in the transaction after all the computation so far.-origin
: gives the sender of the transaction, representing the EOA.
Last updated