4

In all of the blockchain samples that I see e.g. adilmoujahid, Savjee as I checked, they added previous Hash by creating a block.

Suppose that, L is the current last block in a chain. Block A and B are created at the same time. So they set their previous hash with the same hash e.g. Hash of L.

What happens by mining when both of them are mined successfully, but B was mined faster.

Therefore normally previous hash of B is correct, but previous hash of A should be changed to the hash of B.

What I want to reach, I think assigning previous hash by creating a block is not necessary, but by mining. Am I right?

Murch
  • 75,206
  • 34
  • 186
  • 622
masoud2011
  • 141
  • 6
  • 3
  • Thanks to mention that, that helped me to differ my situation from normal blockchain.

    I am working on arduino device for health care, that it cannot synch it self every time with main ledger. Because of that, Arduino just sing the data and send it without previous hash to the server. And my server as a miner verify the signature and add the hash of last block as previous hash.

    My solution is not proper for shared ledger, but because I am using individual ledger for each member there shouldn't be any conflict between transaction and member data

    – masoud2011 Dec 18 '19 at 12:58

2 Answers2

4

A fork happens. If A and B are competing for the next block, they probably share transactions. So you cannot simply append A to B.

They're both valid blocks, nodes hold both of them until they hear about a new block that was built on top of either A or B. Let's say a node hears a new block C was built on top of B, that node will then disregard A because it isn't a part of the longest/heaviest chain. The transactions in A that are not in B are returned to the mempool to be mined in a new block.

The network reaches consensus by trusting the longest chain. This is why it is suggested to wait 6 confirmations for a transaction. This gives extremely high confidence that the network has reached consensus on your transaction and there is a very low probability that your transaction will be apart of a reorganization of the blockchain.

Matthew Cruz
  • 428
  • 4
  • 15
  • Blocks are not discarded if they’re not part of the main chain. – Claris Dec 15 '19 at 23:00
  • 1
    @Anonymous when I refer to the stale block A being disregarded, I mean that it simply isn't part of the main chain. I said, "disregarded", because I was not sure what actually happened to it. – Matthew Cruz Dec 16 '19 at 09:53
  • They’re retained on disk just in case they become part of the main chain, it’s a minor distinction. – Claris Dec 16 '19 at 15:02
  • @RedGrittyBrick is there a timeout period that addresses how long a node should hold a stale block? – Matthew Cruz Dec 17 '19 at 15:19
  • @MatthewCruz: I don't know how long stale blocks are retained in memory or on disk for any/all software implementations. The duration probably depends on the particular software that is in use. Some may keep stale blocks on disk forever, others may not. This may also be affected by what pruning options are configured. Obviously software is unlikely to keep a stale block in memory indefinitely. I don't know any reason the Bitcoin network protocol or algorithms would mandate any specific duration for stale blocks in memory nor in persistent storage. – RedGrittyBrick Dec 17 '19 at 15:30
  • @MatthewCruz ,Do you have any reference for what you said about fork? Fork can be used when we have several branches in chain they are all valid but they follow different rules(e.g. different) – masoud2011 Dec 22 '19 at 09:23
  • What here I talked about is more like Orphan block, regarding to this page https://www.investopedia.com/terms/o/orphan-block-cryptocurrency.asp, they don't create a fork they will be rejected, or if they are valid transaction they will be added to chain but mine don'receive any reward for them – masoud2011 Dec 22 '19 at 09:28
  • 1
    @masoud2011 we’re actually talking about the same thing here and what you said in your first comment is correct. In this scenario it is just a one block fork and is generally resolved after the next block is mined. The fork you’re thinking of is like Bitcoin Cash. The difference is that there are miners building on the new branch, where as just working within Bitcoin’s blockchain the miners will only work on the longest chain (because of the reward). – Matthew Cruz Dec 22 '19 at 11:38
  • @MatthewCruz that is correct, The blockchain completely is new concept and expression could have not identical meaning. fork and branch could be similar in general meaning but in blockchain concept they refer to different thingshttps://swissborg.com/blog/bitcoin-fork. In my older reference it said orphan block could refer stale block too.https://www.investopedia.com/terms/o/orphan-block-cryptocurrency.asp . – masoud2011 Dec 22 '19 at 11:52
  • @masoud2011 the blockchain is not a new concept. Look up Cipher Block Chaining (CBC) encryption/decryption, this where the idea came from. Also, forks and branches also have historic meaning in computer science, so I wouldn't try and separate blockchain from other topics because they're inherently related. – Matthew Cruz Dec 22 '19 at 15:23
  • @MatthewCruz Anyway "fork" is a wrong word that you used here. Because when you search for fork in Blockchain you get completely different result as we discuss here. – masoud2011 Dec 22 '19 at 15:39
  • 2
    @masoud2011: The term "fork" appears in more than one context in Bitcoin. You can read more about forks here: https://bitcoin.stackexchange.com/q/30817/5406 – Murch Dec 22 '19 at 18:12
3

Each block commits to the previous block in the block header by including the previous block's hash. A block's hash is defined as the output of hashing the block header, and for a block to be valid, this hash must fulfill the current difficulty requirement. While blocks can have more than one child block, the Bitcoin network finally agrees on just one best chain. In the best chain there is only one block at each height, so while blocks can have multiple child blocks, only one of them will be relevant in the longterm.

Therefore normally previous hash of B is correct, but previous hash of A should be changed to the hash of B.

Firstly, changing even a single bit in the block header will cause the hash of the block header to come out differently (see e.g. Mastering Bitcoin (Ch8, section "Mining the Block")). It's astronomically unlikely that the new hash also fulfills the difficulty statement. Therefore, the modified block header would no longer represent a valid block.

Secondly, given that A and B were drawing on the same set of unconfirmed transactions when they were found, it's extremely likely that the transactions included in the blocks for confirmation have some overlap. However, each transaction can only be included in one block, because each transaction output can only be spent once, and therefore the inputs of a transaction that was already confirmed are no longer available in a later block. It follows that A would likely include invalid transactions if it were following B.

So, once both A and B were found, and both have each independently committed to block L as their predecessor as well as including some of the same transactions, the blocks A and B are competing to be part of the best chain and exclusive to each other.

Murch
  • 75,206
  • 34
  • 186
  • 622
  • An important problem that I have here, nobody talks with references, what you said here is wrong, please read here https://blog.goodaudience.com/blockchain-for-beginners-what-is-blockchain-519db8c6677a , We create identical hash with data inside the block. But previous hash is part of header of block, and changing in header of block doesn't make any change in hash of block. – masoud2011 Dec 22 '19 at 12:28
  • On the other hand please read here https://99bitcoins.com/bitcoin/mempool/ . Most of the block in memory pool with lower fee will be delayed. Because they are priorities and transactions with more fee have more priority. That means previous hash of most of transaction with lower fee should be changed by mining. Because they should be added after transaction with higher fee, even if previous hash of lower fee is earlier than previous hash of higher fee – masoud2011 Dec 22 '19 at 12:38
  • 1
    Hi Masoud, "changing in header of block doesn't make any change in hash of block". Maybe that was a confusing. The block hash is the digest of the block header, so if one changes anything in a block header that block's hash will change and all subsequent blocks also need to change because they referred to that previous header directly or indirectly. (See e.g. Mastering Bitcoin https://www.oreilly.com/library/view/mastering-bitcoin/9781491902639/ch08.html – Murch Dec 22 '19 at 18:05
  • Transaction hashes are only dependent on the content of the transaction. Mining a transaction doesn't change the transaction (that would invalidate the signature) or the transaction hash. The only thing that changes about the transaction is that it is now being referred to by a block, i.e. it's part of a Merkle tree whose Merkle root is part of a block header that's part of the best chain. – Murch Dec 22 '19 at 18:09