Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: 9017.exe on December 07, 2022, 11:35:06 AM



Title: I didn't get something
Post by: 9017.exe on December 07, 2022, 11:35:06 AM
Hello !

I have some questions about the bitcoin blockchain, and I'm pretty sure to find them here  :D.

1) If I check a blockchain explorer, I see that some blocks are mined very fast and others not. Some blocks have only 100 transactions in it (https://btcscan.org/block/00000000000000000007465f8d675de9de8f075d4da9b9fb843efc7600d9a944) and others have more than 3000. Why ? It's a question of luck ? Or is the network less used at this moment ?

2) It takes approximately 10 minutes per block to be mined. If I make a transaction, my transaction is validated before a new block is mined ? If not, do I have to wait 10 minutes between 2 transactions ?

2.1) Every information added in a bloc that will be mined change the hash. Is some transactions added in a block DURING the time the miners are working to find the right hash ?

I hope you understand  ::)


Title: Re: I didn't get something
Post by: hosseinimr93 on December 07, 2022, 11:41:51 AM
1) If I check a blockchain explorer, I see that some blocks are mined very fast and others not. Some blocks have only 100 transactions in it
Miners are free to pick any number of transactions they want as long as the block size doesn't exceed 1 MB.
Edit: I forgot to mention that witness data are not counted in the 1 MB limit and any block can include up to 1 million virtual bytes or 4 million weight units of transactions.


2) It takes approximately 10 minutes per block to be mined. If I make a transaction, my transaction is validated before a new block is mined ? If not, do I have to wait 10 minutes between 2 transactions ?
10 minutes on average, not exactly 10 minutes.
Your transaction will have the chance to be mined in the next block. Depending on the fee rate you set for the transaction, it may take more time to be confirmed.


2.1) Every information added in a bloc that will be mined change the hash. Is some transactions added in a block DURING the time the miners are working to find the right hash ?
Miners make a candidate block and can add or remove transactions from the candidate block until the block is mined.


Title: Re: I didn't get something
Post by: 9017.exe on December 07, 2022, 11:47:08 AM
Thank you !

Quote
Miners are free to pick any number of transactions they want, as long as the block size doesn't exceed 1 MB.

They are all working together, right ? So they all choose the number of transactions in the next block, and then they ALL work to find the hash ?


Title: Re: I didn't get something
Post by: hosseinimr93 on December 07, 2022, 11:52:16 AM
They are all working together, right ? So they all choose the number of transactions in the next block, and then they ALL work to find the hash ?
All miners are trying to solve the blocks at the same time and each of them can have their own candidate block.
Once you make a transaction, it goes to mempool of the nodes. Miners can pick any of the transactions they want from the mempool and put them in their candidate block. There is nothing forcing them to include or not include a certain transaction.


Title: Re: I didn't get something
Post by: mocacinno on December 07, 2022, 12:02:19 PM
A small, simplified explanation:

You create a new transaction and broadcast it over the network. Miners have nodes running on said network. Each miner can have or multiple nodes, and i don't think two seperate solo miners (or pools) share a node (at least, this isn't something that i have ever seen... theoretically they can.. but they're competing against eachother, so it wouldn't make any sense).
The miner (and the other nodes for that matter) receive the transaction you broadcasted and they store it in their mempool.

The mempool of the nodes is filled with unconfirmed transactions, but each node can have it's own rule as to how many transactions it keeps in its mempool, for how low, the minimum fee to accept a transaction in it's mempool,... So the mempool of most nodes will differ.


When a miner starts mining, he takes the sha256d  hash of the header of the last block known to him/her, he creates a merkle tree of transactions it picked from it's mempool and adds a nonce (basically, a piece of random data) and some other data that isn't important for this explanation. The combination of this data is the header of the new block the miner is trying to solve. Then he creates a sha256d hash of this new header and sees if it's under the current target. If this is the case, the block he created is valid. If not, he increments the nonce and tries again.

What is important is that each miner is working independantly. He has his own node and his own set of transactions in the mempool. When generating a block, he can add a maximum of 1Mb of transaction data (not including the signatures), so odds are the transactions picked by 2 miners at a certain point in time differ.

Every 2016 blocks, a new difficulty is set in such a way that the average time between 2 blocks is ~10 minutes. But since hashing outcome cannot be predicted, sometimes it can take an hour before a valid block is found, and sometimes it takes mere seconds.
Some pools start mining with an "empty" block, only containing the coinbase reward whilst they are still creating a transaction set witch gives them the most income (due to the transaction fees) whilst still fitting in 1Mb (excluding signatures). That's why you sometimes see empty blocks... Other times blocks aren't full because the miner just doesn't have enough unconfirmed transactions in the mempool of his node.


Title: Re: I didn't get something
Post by: LoyceV on December 07, 2022, 01:04:10 PM
do I have to wait 10 minutes between 2 transactions ?
No. You can use your unconfirmed outputs for a new transaction. If you use the same fee rate, they'll probably confirm in the same block.
It is however cheaper (in terms of transaction fee) to make one transaction with multiple outputs.


Title: Re: I didn't get something
Post by: hosseinimr93 on December 07, 2022, 01:34:36 PM
If you use the same fee rate, they'll probably confirm in the same block.
Just to be more accurate:
If you use the same or higher fee rate for the second transaction, both transactions will be probably confirmed in the same block.


Title: Re: I didn't get something
Post by: NeuroticFish on December 07, 2022, 01:44:21 PM
Miners are free to pick any number of transactions they want as long as the block size doesn't exceed 1 MB.
Edit: I forgot to mention that witness data are not counted in the 1 MB limit and any block can include up to 1 million virtual bytes or 4 million weight units of transactions.

It may worth adding that sometimes the mempool just doesn't have enough transactions to fill an entire block, hence no matter the block is not full and the miners would want to add more, they can't; the block will not wait for new transactions to come.

Your transaction will have the chance to be mined in the next block. Depending on the fee rate you set for the transaction, it may take more time to be confirmed.

Again, I'll add that it depends on fee rate vs how full the mempool is.
Plus, the fee cannot really be under the default minimum of 1 sat/vByte (but long ago in the past even 0 satoshi was sometimes OK as transaction fee).


Title: Re: I didn't get something
Post by: 9017.exe on December 07, 2022, 02:30:25 PM
I think I begin to understand

So, when I send a bitcoin to somebody, the transaction goes to the mempool. The mempool is like a big pool where the transactions wait to be picked up. All the miners have access to the mempool to choose the transactions they want to put in their block.

1) When a miner pick up a transaction in the mempool, the others miners can't take it ?
2) If all the miners can choose what they put in the block, they are all searching for a different hash to solve the block, right ? I'm saying this because with this simulator (https://blockchaindemo.io/) The "Data" input represent the different transactions in it, no ?


Title: Re: I didn't get something
Post by: mocacinno on December 07, 2022, 02:42:40 PM
--snip--
So, when I send a bitcoin to somebody, the transaction goes to the mempool. The mempool is like a big pool where the transactions wait to be picked up. All the miners have access to the mempool to choose the transactions they want to put in their block.
More or less... It's actually your transaction going into the mempool of each node that choses to accept it (some nodes might reject transactions that have, for example, a low feerate). Each miner (or mining pool) has one or more nodes in the network, and they have access to the unconfirmed transactions in the mempool of their own nodes.

1) When a miner pick up a transaction in the mempool, the others miners can't take it ?
2) If all the miners can choose what they put in the block, they are all searching for a different hash to solve the block, right ? I'm saying this because with this simulator (https://blockchaindemo.io/) The "Data" input represent the different transactions in it, no ?

1) that's incorrect. All miners in the world can try to solve a block containing exactly the same transaction. Miners don't have to communicate with eachother. You can run a node at home and setup a solo mining operation without telling anybody. You'd be getting the header of the latest valid block straight from your node, and you'd get the transactions from the mempool of your own node. If you'd ever solve a block, you'd just broadcast said block to the peers you are connected to and nobody would ever know you were the miner that solved the block in the first place.

2) well, they're all trying to find a header whose sha256d hash is under the current target. The thing is: if 2 miners would pick 100% exactly the same set of unconfirmed transactions, they'd still have to add the coinbase transaction which is paying the miners themselves to the merkle tree (which ends up in the header of the candidate block). And since 2 different miners won't fund the same address with the coinbase transaction, the headers those 2 miners are working on will never be 100% identical.
EDIT: on a sidenote to question2 => miners are not looking for "the" hash... There's looking for "a" hash that's under the current target... There are billions of valid hashes that are under a certain target. The thing is: eventough there are billions of valid hashes, it's still extremely hard to find one of those hashes since the number of possible hashes is waaaaaaaaaaaaaay bigger.
It's like filling a big swimmingpool with blue marbles and adding 100 red ones, then dive in wearing a blindfold and picking a single marble: eventough there are100 red ones, the odds of picking one is slim to none since there's are several million blue marbles in there.


Title: Re: I didn't get something
Post by: 9017.exe on December 07, 2022, 03:04:18 PM
Quote
they'd still have to add the coinbase transaction which is paying the miners themselves to the merkle tree (which ends up in the header of the candidate block)
I didn't understand this...

Quote
miners are not looking for "the" hash... There's looking for "a" hash that's under the current target...
You unlocked something in my head.

Still something I don't understand. If I send a bitcoin now. The transaction will be accepted in the next block because the miners are already working on the block at the moment, right ? Or can they add unconfirmed transactions while they are solving the block ?


Title: Re: I didn't get something
Post by: LoyceV on December 07, 2022, 03:14:10 PM
The transaction will be accepted in the next block because the miners are already working on the block at the moment, right ? Or can they add unconfirmed transactions while they are solving the block ?
From what I understand, they can only include the transactions they know about while mining the transaction. But I also assume mining pools continuously look for more profit, so they continuously adjust which transactions are included in the block they're mining.


Title: Re: I didn't get something
Post by: NeuroticFish on December 07, 2022, 03:16:58 PM
Quote
they'd still have to add the coinbase transaction which is paying the miners themselves to the merkle tree (which ends up in the header of the candidate block)
I didn't understand this...

A block may or may not contain users' transactions. But even if miners choose to not add users' transactions they will normally still include the transaction that pays them ( = to the miners) the block reward (called the coinbase tx). And since 2 miners will most probably have different addresses to send to that money, the headers will be never identical.


Title: Re: I didn't get something
Post by: decodx on December 07, 2022, 03:34:53 PM
@mocacinno, you said: "sometimes it can take an hour before a valid block is found". Is one hour the maximum time between two blocks? What happens if no miner finds a valid hash in that time? If not, is there a maximum time that can elapse between two confirmed blocks?


Title: Re: I didn't get something
Post by: LoyceV on December 07, 2022, 03:43:23 PM
Is one hour the maximum time between two blocks?
No.

Quote
What happens if no miner finds a valid hash in that time?
Nothing. We wait.

Quote
is there a maximum time that can elapse between two confirmed blocks?
No.

It's basically a Poisson distribution: there's no minimum, and there's no maximum. The average is 10 minutes by design, although it has been less than 10 minutes (on average) because of the (more or less) continuous increase in mining power.


Title: Re: I didn't get something
Post by: FatFork on December 07, 2022, 03:58:53 PM
Quote
they'd still have to add the coinbase transaction which is paying the miners themselves to the merkle tree (which ends up in the header of the candidate block)
I didn't understand this...

Each block must contain at least one transaction. The first transaction in the block is called a coinbase transaction. It is a unique type of transaction that contains a block reward for the miners.

Quote
miners are not looking for "the" hash... There's looking for "a" hash that's under the current target...
You unlocked something in my head.

Still something I don't understand. If I send a bitcoin now. The transaction will be accepted in the next block because the miners are already working on the block at the moment, right ? Or can they add unconfirmed transactions while they are solving the block ?

As I understand it, the moment one block is mined (confirmed and included in the blockchain) all miners start searching for the hash of the next block. So yes, you could say that they are adding new transactions to the block they are currently "working" on.

But I think it is wrong to imagine that miners solve some kind of complex operations on the block (calculating the hash is actually a simple process, and you can even calculate it manually), but they actually compete with each other to find a hash that meets specific conditions (the one that is under the current target). So it makes sense for them to start calculating the hash of the next block as soon as possible because it gives them a better chance of finding it first.


Title: Re: I didn't get something
Post by: hosseinimr93 on December 07, 2022, 04:13:07 PM
Still something I don't understand. If I send a bitcoin now. The transaction will be accepted in the next block because the miners are already working on the block at the moment, right ?
Your transaction will be included in the next block if you pay enough fee for that.
As each block has a size limit and can't include any number of transactions, miners prioritize transactions based on their fee rate.

Or can they add unconfirmed transactions while they are solving the block ?
As I said in my previous post, each of miners has a candidate block. Miners can add new transactions or remove the exisiting transactions from the candidate block when they are trying to solve the block. So, whenever you broadcast your transaction, it can be included in the next block, assuming the fee rate you set for it is high enough.


Title: Re: I didn't get something
Post by: LoyceV on December 07, 2022, 04:17:05 PM
Each block must contain at least one transaction.
No it doesn't. Some miners have forgotten to claim their block reward, that's possible.


Title: Re: I didn't get something
Post by: hosseinimr93 on December 07, 2022, 04:30:10 PM
Each block must contain at least one transaction.
No it doesn't. Some miners have forgotten to claim their block reward, that's possible.
I doubt we can have a block without any transaction.
Take a look at the block number 501726 (https://mempool.space/block/0000000000000000004b27f9ee7ba33d6f048f684aaeb0eea4befd80f1701126). The miner hasn't claimed the block reward, but the block still includes 1 transaction.


Title: Re: I didn't get something
Post by: LoyceV on December 07, 2022, 04:38:12 PM
I doubt we can have a block without any transaction.
I can't find other examples, I thought they exist.

Quote
Take a look at the block number 501726 (https://mempool.space/block/0000000000000000004b27f9ee7ba33d6f048f684aaeb0eea4befd80f1701126). The miner hasn't claimed the block reward, but the block still includes 1 transaction.
Blockchair (https://blockchair.com/bitcoin/transaction/9bf8853b3a823bbfa1e54017ae11a9e1f4d08a854dcce9f24e08114f2c921182) makes it look like an OP_RETURN transaction.


Title: Re: I didn't get something
Post by: 9017.exe on December 07, 2022, 04:50:41 PM
Great ! Thank you everyone. Everything is "clear" now  ;D


Title: Re: I didn't get something
Post by: o_e_l_e_o on December 07, 2022, 09:06:02 PM
I can't find other examples, I thought they exist.
They do not.

Every block must have exactly one coinbase transaction, and that coinbase transaction must be the first transaction in the block. The coinbase transaction may collect the block subsidy and the block fees, or any portion thereof, but it doesn't have to. The coinbase transaction must also contain the block height as the first entry in its scriptsig (4 bytes) with a further 96 bytes of arbitrary data, which is how pools tag blocks as being found by them.

Blockchair (https://blockchair.com/bitcoin/transaction/9bf8853b3a823bbfa1e54017ae11a9e1f4d08a854dcce9f24e08114f2c921182) makes it look like an OP_RETURN transaction.
It's supposed to be, but it isn't, because the miner clearly messed up. If you pull the hex script from that output, you'll get the following:
Code:
52534b424c4f434b3addbf517adf8ffd4bca7751505b39c9013a0d1fd479fc4e901b39dd57b347c624

The start of that - 52534b424c4f434b - decodes to RSKBLOCK. If you look at any other recent block which has an RSKBLOCK OP_RETURN output, you'll see the same string in its coinbase transaction. However, in this case, the miner has neglected to include the necessary OP_RETURN bytes at the start of the script (0x6a29).