<p>Since there weren't other entries and the only answer didn't provide a clear answer to my question, I'm going to sum up what I found to be the solution to my question.</p>

<p>There are no properties or constraints that allow the algorithm to start from a more recent block. In order to check if a transaction from an orphan block needs to go back to the mem pool, one needs to start from the genesis block and check every block along the way to guarantee that the transaction doesn't already belong to a validated block. If at the end no blockchain no block contains said transaction, we need to add it back to the mem pool.</p>

<p>If in the meantime someone participates with more relevant information I'll happily delete this answer.</p>

CC BY-SA 3.0
2018-04-11T11:49:45.883Z
2018-04-11T11:49:45.883Z
Some_user_qwerty
<p>Proto code for returning transactions from orphan block:</p>

<pre><code>foreach ( tx: orphanBlock )
  if ( tx.isValidAfterReorg ( ) )
    mempool.insert ( tx );
</code></pre>

CC BY-SA 3.0
2018-04-10T11:02:00.993Z
What does "isValidAfterReorg" does? Does it check the entirety of the chain like my proto code does?
Some_user_qwerty
CC BY-SA 3.0
2018-04-10T11:03:48.673Z
it checks that transaction is valid in reorganized chain. it does not matter where transaction came from - either from network or from orphan block - if it is valid we can add it to the mempool
amaclin
CC BY-SA 3.0
2018-04-10T11:08:25.063Z
What if another block already has said Tx?  Even if it's valid, it's duplicated. Since orphan blocks occur from forks/concurrency, it's highly probable that block A already has many of the Tx that block B has (block A and B create a fork in this case)
Some_user_qwerty
CC BY-SA 3.0
2018-04-10T15:40:55.040Z
if the mainchain already contains the transaction the `tx` will be invalid-for-mainchain because its inputs are not connected to utxo set
amaclin
CC BY-SA 3.0
2018-04-10T11:00:07.743Z
2018-04-10T11:00:07.743Z
amaclin
<p>This question is centered around the implementation of how transactions from a reorganized block return to the mempool.</p>
<p>In my understanding, once a block becomes organized out of the best chain all its transactions (that aren't in blocks of the best chain) need to return to the mempool.</p>
<p>Do we start from genesis block until we reach the last block of the active chain checking every transaction along the way, or is it there a more efficient algorithm that's based on some constraint/property that allows the algorithm to start from a more recent block?</p>
<p>Proto code for starting from the genesis block:</p>
<pre><code>Blockchain blockchain = ...
function Dictionary&lt;Key, Tx&gt; deleteOrphanTx (Dictionary&lt;Key, Tx&gt; orphanTx) {
    foreach (Block currBlock : blockchain.getBlocks())
        foreach (Tx tx : currBlock.getTx())
            Key txKey = tx.getKey();
            if(orphanTx.contains(txKey)
                orphanTx.remove(txKey);
    return orphanTx;
}
</code></pre>

<mempool><algorithms><chain-reorganization><stale-block>
How to return tx to mempool from stale block?
Some_user_qwerty
# How to return tx to mempool from stale block?

(Asked by: username_0 on 2018-04-10)

This question is centered around the implementation of how transactions from a reorganized block return to the mempool.

In my understanding, once a block becomes organized out of the best chain all its transactions (that aren't in blocks of the best chain) need to return to the mempool.

Do we start from genesis block until we reach the last block of the active chain checking every transaction along the way, or is it there a more efficient algorithm that's based on some constraint/property that allows the algorithm to start from a more recent block?

Proto code for starting from the genesis block:

```
Blockchain blockchain = ...
function Dictionary<Key, Tx> deleteOrphanTx (Dictionary<Key, Tx> orphanTx) {
    foreach (Block currBlock : blockchain.getBlocks())
        foreach (Tx tx : currBlock.getTx())
            Key txKey = tx.getKey();
            if(orphanTx.contains(txKey)
                orphanTx.remove(txKey);
    return orphanTx;
}

```

**Tags:** mempool, algorithms, chain-reorganization, stale-block


---

## 2 answers

### Answer by username_0 on 2018-04-11. Score: 0 | Accepted answer

Since there weren't other entries and the only answer didn't provide a clear answer to my question, I'm going to sum up what I found to be the solution to my question.

There are no properties or constraints that allow the algorithm to start from a more recent block. In order to check if a transaction from an orphan block needs to go back to the mem pool, one needs to start from the genesis block and check every block along the way to guarantee that the transaction doesn't already belong to a validated block. If at the end no blockchain no block contains said transaction, we need to add it back to the mem pool.

If in the meantime someone participates with more relevant information I'll happily delete this answer.


---

### Answer by username_1 on 2018-04-10. Score: 1

Proto code for returning transactions from orphan block:

```
foreach ( tx: orphanBlock )
  if ( tx.isValidAfterReorg ( ) )
    mempool.insert ( tx );

```

> **Comments:**
> **username_0**: What does "isValidAfterReorg" does? Does it check the entirety of the chain like my proto code does?  
> **username_1**: it checks that transaction is valid in reorganized chain. it does not matter where transaction came from - either from network or from orphan block - if it is valid we can add it to the mempool  
> **username_0**: What if another block already has said Tx?  Even if it's valid, it's duplicated. Since orphan blocks occur from forks/concurrency, it's highly probable that block A already has many of the Tx that block B has (block A and B create a fork in this case)  
> **username_1**: if the mainchain already contains the transaction the `tx` will be invalid-for-mainchain because its inputs are not connected to utxo set  

---

