<p>I don't see any reason for the second <code>if</code> statement, either. I'm not speaking from any form of authority here, but it looks like a (tiny) efficiency bug to me.</p>

<p>The second <code>if</code> statement has been around since the <a href="https://github.com/bitcoin/bitcoin/commit/e071a3f6c06f41068ad17134189a4ac3073ef76b#diff-118fcbaaba162ba17933c7893247df3aR2119" rel="nofollow">first commit</a>. The first <code>if</code> statement was <a href="https://github.com/bitcoin/bitcoin/commit/f93d5f9ffe1e12079e560a735111735924726a06#diff-118fcbaaba162ba17933c7893247df3aR2338" rel="nofollow">inserted about a year later</a> when "trickling" of tx inv messages was added, presumably to check the set membership earlier to avoid running through the trickling logic when it's not necessary. I'd guess that the removal of the second <code>if</code> was simply overlooked.</p>

CC BY-SA 3.0
2015-06-10T16:46:44.123Z
2015-06-10T16:46:44.123Z
Christopher Gurnee
<p>I'm not sure if the 2nd if is useless.</p>

<p>The second if does a more detailed check (see <code>mruset.hL50</code> <a href="https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/mruset.h#L51" rel="nofollow">https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/mruset.h#L51</a>) which i think reduces the size of the set, etc.</p>

CC BY-SA 3.0
2015-06-10T19:56:37.270Z
I wasn't arguing (although maybe I was unclear) that the *insert* was useless, only that checking the *result* of the insert was useless because (as Aliakbar Ahmadi noticed) the result will always be `true`. Does my answer need updating?
Christopher Gurnee
CC BY-SA 3.0
2015-06-10T20:02:55.877Z
Ah. Right. The if itself is useless but not the expression within the if. Fair enough. :)
Jonas Schnelli
CC BY-SA 3.0
2015-06-10T19:05:04.563Z
2015-06-10T19:05:04.563Z
Jonas Schnelli
<p><br/>
Going through <code>main.cpp</code> I couldn't completely understand the purpose of a double barrier coded as if-statements:<br/>
I'm referring to ...</p>

<p><a href="https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/main.cpp#L4981" rel="nofollow"><code>if (pto-&gt;setInventoryKnown.count(inv))</code></a></p>

<p>and</p>

<p><a href="https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/main.cpp#L5003" rel="nofollow"><code>if (pto-&gt;setInventoryKnown.insert(inv).second)</code></a></p>

<p><strong>What is the idea behind checking if an inventory is known to the target peer twice?</strong> <br/>I'm aware that <code>if (pto-&gt;setInventoryKnown.count(inv))</code> only checks if entry is known to target peer and <code>if (pto-&gt;setInventoryKnown.insert(inv).second)</code> inserts the inventory item after checking its uniqueness.<br/>I mean couldn't one just use only <em>one</em> if-statement combining?</p>

<network><message><c++><inventory>
Two if's before sending an inventory message
Aliakbar Ahmadi
# Two if's before sending an inventory message

(Asked by: username_0 on 2015-06-10)

Going through `main.cpp` I couldn't completely understand the purpose of a double barrier coded as if-statements:  
I'm referring to ...

[`if (pto->setInventoryKnown.count(inv))`](https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/main.cpp#L4981)

and

[`if (pto->setInventoryKnown.insert(inv).second)`](https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/main.cpp#L5003)

**What is the idea behind checking if an inventory is known to the target peer twice?**   
I'm aware that `if (pto->setInventoryKnown.count(inv))` only checks if entry is known to target peer and `if (pto->setInventoryKnown.insert(inv).second)` inserts the inventory item after checking its uniqueness.  
I mean couldn't one just use only *one* if-statement combining?

**Tags:** network, message, c++, inventory


---

## 2 answers

### Answer by username_1 on 2015-06-10. Score: 1

I don't see any reason for the second `if` statement, either. I'm not speaking from any form of authority here, but it looks like a (tiny) efficiency bug to me.

The second `if` statement has been around since the [first commit](https://github.com/bitcoin/bitcoin/commit/e071a3f6c06f41068ad17134189a4ac3073ef76b#diff-118fcbaaba162ba17933c7893247df3aR2119). The first `if` statement was [inserted about a year later](https://github.com/bitcoin/bitcoin/commit/f93d5f9ffe1e12079e560a735111735924726a06#diff-118fcbaaba162ba17933c7893247df3aR2338) when "trickling" of tx inv messages was added, presumably to check the set membership earlier to avoid running through the trickling logic when it's not necessary. I'd guess that the removal of the second `if` was simply overlooked.


---

### Answer by username_2 on 2015-06-10. Score: 0

I'm not sure if the 2nd if is useless.

The second if does a more detailed check (see `mruset.hL50` <https://github.com/bitcoin/bitcoin/blob/3fce72eaa3ea75aa911e32c4d96313848338cede/src/mruset.h#L51>) which i think reduces the size of the set, etc.

> **Comments:**
> **username_1**: I wasn't arguing (although maybe I was unclear) that the *insert* was useless, only that checking the *result* of the insert was useless because (as username_0 noticed) the result will always be `true`. Does my answer need updating?  
> **username_2**: Ah. Right. The if itself is useless but not the expression within the if. Fair enough. :)  

---

