gaslimit for a block and other questions related to gas model - ethereum

i know what is a gas, gaslimit and gasprice, but still have confusion even after searching and reading through the Internet.
There is a gaslimit per block, but why many blocks did not reach it? in other words, can a miner send a block to the network without reaching the gaslimit for the block?
Assume the block gaslimit is 4 million and i sent a transaction with 4 million gaslimit. But when the miner executed it (used gas was 1 million). Can the miner add extra transactions to the block to fill the remaining 3 million or not. In another way, does a transaction with a big gaslimit (but uses a fraction of that gas) affects the miner of adding more transactions to the block?
Each Opcode coast some value of gas. How Ethereum measure the cost of each EVM opcode? (any reference for explanation?).
Thanks

Q1 The block gas limit is an upper bound on the total cost of transactions that can be included in a block. Yes, the miner can and should send a solved block to the network, even if the gas cost is 0. Blocks are meant to arrive at a steady pace in any case. So "nothing happened during this period" is a valid solution.
Q2a The gas cost of a transaction is the total cost of executing the transaction. Not subject to guesswork. If the actual cost exceeds the supplied gas then the transaction fails with an out-of-gas exception. If there is surplus gas, it's returned to the sender.
Q2b Yes, a miner can and should include multiple transactions in a block. A block is a well-ordered set of transactions that were accepted by the network. It's a unit of disambiguation that clearly defines the accepted order of events. Have a look here for exact meaning of this: https://ethereum.stackexchange.com/questions/13887/is-consensus-necessary-for-ethereum
Q3 I can't say for sure (possibly someone can confirm) that this is an up-to-date list: https://docs.google.com/spreadsheets/d/1m89CVujrQe5LAFJ8-YAUCcNK950dUzMQPMJBxRtGCqs/edit#gid=0

Related

HardHat with gas-report reports not understandable

I'm using HardHat with gas-report but I'm not able to understand the following results:
Optimizer enabled: false
Runs: 200
Block limit: 30000000 gas
% of limit
Here I have marked with red square the fields:
enter image description here
Optimizer (whether it's enabled or disabled) and the target amount of contract runs, to which the optimizer should optimize the contract bytecode, are options of the Solidity compiler. When you compile the contract with an optimizer, it can decrease either the total bytecode size - or the amount of gas needed to execute some functions. (docs)
Block limit states the amount of gas units that can fit into one block. Different networks might have different values, some have dynamically adjusted limits, plus you can usually set your own limit if you're using an emulator or a private network. (docs)
% of limit states a portion of how much your contract deployment took in the total block limit. Example from your table: Deployment of HashContract cost 611k gas units, which is approx. 2% of the 30M block limit. If the number exceeds 100%, the transaction would never be included in a block - at least not in a block with the same or smaller limit. Also, if the transaction has a low gasPrice and a high % of the total block limit, some miners/validators might not be able to fit the transaction into a block (as transactions are usually ordered from highest gasPrice to lowest), so it might take longer to be included in a block.

Checking the number of confirmed blocks for a transaction?

How does one check the number of "block confirmations" for a given transaction?
I tried checking the transaction hash in block heights of +1, +2, etc. but they don't contain the transaction ID.
Would I instead need to wait for future blocks to be mined, and the transaction status to still be considered valid? the Receipt.Status.
After lots of research, I can say that it is the number of blocks that have been mined after the block your transaction was included in, and your transaction is still considered valid. So to check block confirmations, you would check whether the transaction is still valid, and see how many more blocks above the transaction block height have been mined.
Therefore, if your transaction has 13 block confirmations (see above graphic), then there have been 12 blocks mined since the block was mined that included your transaction.
https://jaredstauffer.medium.com/what-is-a-block-confirmation-on-ethereum-e27d29ca8c01#:~:text=A%20block%20confirmation%20is%20simply,mined%20that%20included%20your%20transaction.

How is gas usage calculated when using if statements

Let's say I have a smart contract with branch, where each branch has a different number of operations.
if (someCondition) {
// do operations costing 10 gas
} else {
//do operations costing 100 gas
}
When a user goes to call this function from their client, say metamask, how do they know how much gas their transaction will cost? Do they just have to guess and include enough gas for the most expensive path?
The client app is almost always able to calculate the gas usage. Either by running their own EVM emulator, or by querying an external API that emulates the transaction and returns the result.
All blockchain data is publicly available for reading (even values of private properties - just not with Solidity, but with using more low-level approach and querying the storage slots), and the gas cost of each operation is predetermined.
So the client knows, that the transaction is going to
read one slot from memory
write into storage slot
declare another slot in memory and return it
And it also knows that one MLOAD costs 3 gas, one SSTORE costs 5,000 gas, etc.
It can use all this data to calculate the final cost.
The exception is when the decision tree is based on block data such as block.timestamp, that is unknown beforehand. Then it depends on the client, but my guess is that most suggest the most expensive combination, so that they lower the risk of having the transaction reverted due to insufficient gas.
Example:
if (block.timestamp % 2 == 0) {
// even second, do operations costing 10 gas
} else {
// odd second, do operations costing 100 gas
}
You can find all the values corresponding to the relative costs, in gas, of a number of abstract operations that a transaction may affect in the Ethereum yellow paper (page 27).
The "if" statment in a low level languaje, is consider a "JUMP" operation (alters de program counter). So in the gas cost table (page 27) says that a JUMPDEST operation cost 1 gas value.

How to get transaction cost in smart contract - Solidity, Ethereum

How would I get the transaction cost inside of my contract? Would it just be: tx.gasprice ? And will this always be a value in gwei or will it be wei ?
The cost of a transaction isn't really known until execution completes. In an extreme example, perhaps your function which is computing this is being called by another function, and after you return, that function throws, consuming all remaining gas. There's no way for you to know in advance that this will happen.
To calculate the cost of the transaction, you'd need two pieces of information:
The gas price.
How much gas will be consumed.
If you knew both, you could multiple them together and get the total cost. tx.gasprice tells you (1), but as explained above, you can't really know (2). The best you can do is probably to use msg.gas at the top and bottom of a function to tell you roughly how much gas that function consumes.

Exact explanation of statistics displayed on https://ethstats.net

The site https://ethstats.net displays statistics about the Ethereum network but I did not find a precise definition of each statistic and graph displayed. Is there somewhere I can get this information?
These are the items of the overview:
Best Block is the heaviest block regarding the cummultative difficulty, or in simple words: the highest block number of the longest valid chain.
Uncles are orphaned blocks, but in oposite to other blockchain systems, uncles are rewarded and included in the blockchain. Shows current bloc's uncle count and uncle count of last 50 blocks.
Last Block shows the time since the last block was mined, usually in seconds.
Average Block Time is, well, the average time between two blocks, excluding uncles, in seconds. Should be something around 15 seconds.
Average Network Hashrate is the number of hashes bruteforced by the network miners to find a new block. 5 TH/s means the network power is at five trillion hashes per second.
Difficulty is the current mining difficulty to find a new block which basicly means how hard it is to find a matching hash.
Active Nodes is the number of connected nodes to the Ethstats dashboard, (not the whole ethereum network!)
Gas Price is the price miners accept for gas. While gas is used to calculate fees. 20 gwei is the current default, which stands for 20 Giga-Wei which are twenty billion wei that is 0.00000002 ETH.
Gas Limit is the block gas limit. It defaults to 1.5 pi million gas (4,712,388) and miner can only include transactions until the gas limit is met (and the block is full). The gas limit is the analogy to bitcoin's block size limit, but not fixed in size.
Page Latency and Uptime are specific stats for the dashboard.
Block Time Chart shows the actual time between the last blocks.
Difficulty Chart shows the actual difficulty of the last blocks.
Block Propagation Chart shows how fast blocks are shared among the nodes connected to the dashboard.
Last Block Miners are the public keys of the miners who found most of the last blocks.
Uncle Count Chart shows numbers of uncles per 25 blocks per bar.
Transactions Chart shows numbers of transactions included in last blocks.
Gas Spending Chart shows how much gas was spent on transactions in each block, note the correlation to the transactions chart.
Gas Limit Chart shows the dynamicly adjusted block gas limit for each block.
And below you see details of connected nodes.