# How 2 chains co-exist together?

I am not able to understand, how 2 chains co-exist together, like these were the observations which are ambiguity for me:

• In one reading I have read, Only longest chain is valid and and rest chains transactions are sent back to unconfirmed transactions, but in bitcoin blockchain how more than 2 chains(Side Chains) co-exist together? There should be only 1 single valid chain right ?
• If a honest miner has a double spent transaction in his block, which was done by some malicious person, honest miner after broadcasting his block, his block will be rejected because he has double spent transaction of some other malicious person, where this is not miner's fault. How miner is this problem solved?
• If only longest chain is valid, after how many blocks in small chain transactions are discarded and sent back to unconfirmed pool?
• If attacker tried to double spend and somehow tried to surpass the valid chain, how to deal with this problem?
• Hi Arjun Reddy, as I wrote earlier, please only ask one question per topic. Also, I'm confused about your repeated questions about doublespends. I thought I had explained this twice before, but it seems that there is an underlying assumption that I keep missing. You do realize that there is only one block at each height in the best chain? Perhaps this topic will help: bitcoin.stackexchange.com/q/12427/5406?
– Murch
Jul 18, 2022 at 19:59
• Sorry Murch, I will make sure not to repeat again and ask a one question per topic Jul 20, 2022 at 6:55

In one reading I have read, Only longest chain is valid and and rest chains transactions are sent back to unconfirmed transactions, but in bitcoin blockchain how more than 2 chains(Side Chains) co-exist together? There should be only 1 single valid chain right ?

From the point of view of the side chain, blocks in the main chain are not blocks at all, they're meaningless garbage because they don't follow the side chain's rules. From the point of view of the main chain, blocks in the side chain are not blocks at all because they don't follow all the main chain's rule.

In short, if there is more than one persistent distinct chain, it can only be because the two chains follow different rules.

If a honest miner has a double spent transaction in his block, which was done by some malicious person, honest miner after broadcasting his block, his block will be rejected because he has double spent transaction of some other malicious person, where this is not miner's fault. How miner is this problem solved?

An honest miner would never include a double spend in his block.

If only longest chain is valid, after how many blocks in small chain transactions are discarded and sent back to unconfirmed pool?

From the point of view of a longer chain that doesn't include those transactions, if they are valid in that chain, they are considered unconfirmed. From the point of view of any particular chain, any valid transaction not included in that chain is considered unconfirmed to long as it remains neither included nor conflicted due to inclusion of a conflicting transaction.

If attacker tried to double spend and somehow tried to surpass the valid chain, how to deal with this problem?

A chain with a double spend in it can never be valid because double spends are against the chain rules. Such a "chain" is treated no differently from random garbage.

• Blocks in main chain are not valid in side chain and vice versa that is okay, but my doubt is "If only longest chain is taken into consideration and short chain is sent back to unconfirmed transactions(un technically deleting short chain) why there are other chains in bitcoin, there should be only 1 single chain right?" Jul 20, 2022 at 7:13
• @ArjunReddy What do you mean by "other chains" exactly? There only is one bitcoin chain. A side chain is not the bitcoin blockchain which has only one chain. Jul 20, 2022 at 14:11
• Sorry, My bad I had interpreted something wrong about side chain. Thank you for information :) Jul 20, 2022 at 15:44

## Blockchain forks, chain reorganizations, and stale blocks

You're mixing up a few separate concepts. Since there is no central coordination, occasionally two miners will randomly find a new block at the same time. As each of the competing blocks is seen first by some nodes and they both have the same amount of total chainwork, the network briefly disagrees on what the best chaintip is. Let's say the blockheight is 500 and the blocks are 500_A and 500_B.

Naturally the two competing blocks include much of the same transactions, since both miners were seeing the same unconfirmed transactions and picked whatever provided the most transaction fees. Perhaps there is even a transaction in block 500_A that conflicts with a transaction in block 500_B.

However, the best chain can only have one block at height 500! From the perspective of nodes that saw the 500_A first, the other block, 500_B, is not part of the best chain and doesn't count. From the perspective of the nodes that saw 500_B first, 500_A is not part of the best chain and doesn't count.

Eventually, another block is found. Let's say it is 501_A and builds on top of 500_A. As the chain-tip consisting of 500_A+501_A is now longer and has more work than the chain-tip 500_B, the tie is broken and every node recognizes the same best chain. The nodes that were already following that chain-tip with 500_A simply add the new block 501_A. The ones that had seen the block 500_B first reorganize to the new best chain-tip by reverting block 500_B, then applying 500_A and 501_A. We call the block 500_B which does not become part of the best chain a stale block. From the perspective of the best chain, it does not matter whether the stale block was ever found. It's not part of the best chain.

## Sidechains

Sidechains are distinct other blockchain networks that are pegged to the Bitcoin blockchain. They can use different consensus mechanisms, allow other rules, and have specialized purposes such as more private ways of transacting or enabling additional smart contract functionality. Examples for sidechains include Liquid, and RSK.

## "An honest miner includes a doublespend in his block"

In Bitcoin we track every spendable amount of bitcoin separately in the form of unspent transaction outputs (UTXOs). Every transaction output is uniquely identifiable. When creating a transaction, the transaction explicitly specifies which UTXOs it spends. Each transaction output can be spent only once.

A doublespend is an attempt to spend the same transaction output twice. Such an attempt could for example entail creating a transaction that spends the UTXO to send Alice money, and a second transaction that spends the same UTXO to send money to the attacker. The attacker then shows Alice the transaction sending money to Alice, but tries to get the transaction that returns the money to themselves included in a block instead. Since these two transactions conflict with each other, they can never be included in the same transaction history, let alone in one block.

An honest node validates all unconfirmed transactions they hear about before adding them to their own mempool. A miner builds their block template from their mempool. An honest miner would never have two conflicting transactions in their mempool at the same time, and therefore would never include them both in the same block, because they would notice that the UTXO is already spent when validating the second transaction of the conflicting pair before accepting the transaction to their mempool.

If a miner does not validate transactions before building blocks, and they end up producing an invalid block, it is their fault for not following the consensus rules.

## No honest network participant accepts invalid blocks

Transactions that spend money that has been spent before are invalid. Blocks that include invalid transactions are invalid. Nodes do not accept invalid blocks. An attacker that tries to broadcast an invalid block is simply banned and ignored.

Only longest chain is valid

There can briefly exist multiple equally long chains. Eventually one of them wins.

where this is not miner's fault

It's always the miner's fault too, because he could've validated what he was mining.

after how many blocks in small chain transactions are discarded

In the moment the longer chain is detected, all the transactions from the "short chain" are put into the memory pool. After that the longest chain absorbs the transactions (that it confirms) from the memory pool.

tried to double spend and somehow tried to surpass the valid chain

chain containing a double spend is never valid (no matter how long it is).