Skip to content

Commit 9b73e8a

Browse files
committedAug 5, 2014
pending: report already-known TXs so we don't infinitely rebroadcast.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 1e592c6 commit 9b73e8a

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed
 

‎peer.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ recv_tx(struct peer *peer, const struct protocol_pkt_tx *pkt)
668668
unsigned int bad_input_num;
669669
struct txhash_iter it;
670670
struct txhash_elem *te;
671-
bool old;
671+
bool old, already_known;
672672

673673
log_debug(peer->log, "Received PKT_TX");
674674

@@ -701,8 +701,15 @@ recv_tx(struct peer *peer, const struct protocol_pkt_tx *pkt)
701701
todo_done_get_tx(peer, &sha, true);
702702

703703
/* If inputs are malformed, it might not have known so don't hang up. */
704-
switch (add_pending_tx(peer->state, tx, &sha, &bad_input_num, &old)) {
704+
switch (add_pending_tx(peer->state, tx, &sha, &bad_input_num, &old,
705+
&already_known)) {
705706
case ECODE_INPUT_OK:
707+
if (already_known) {
708+
log_info(peer->log, "gave us duplicate TX ");
709+
log_add_struct(peer->log, struct protocol_double_sha,
710+
&sha);
711+
return PROTOCOL_ECODE_NONE;
712+
}
706713
break;
707714
case ECODE_INPUT_UNKNOWN:
708715
/* We don't resolve inputs which are still pending, so

‎pending.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void recheck_pending_txs(struct state *state)
207207
struct protocol_double_sha sha;
208208

209209
hash_tx(txs[i], &sha);
210-
add_pending_tx(state, txs[i], &sha, &bad_input_num, NULL);
210+
add_pending_tx(state, txs[i], &sha, &bad_input_num, NULL, NULL);
211211
}
212212

213213
/* Just to print the debug! */
@@ -249,18 +249,28 @@ enum input_ecode add_pending_tx(struct state *state,
249249
const union protocol_tx *tx,
250250
const struct protocol_double_sha *sha,
251251
unsigned int *bad_input_num,
252-
bool *too_old)
252+
bool *too_old,
253+
bool *already_known)
253254
{
254255
enum input_ecode ierr;
255256

256257
/* If it's already in longest known chain, would look like
257258
* doublespend so sort that out now. */
258-
if (txhash_gettx_ancestor(state, sha, state->longest_knowns[0]))
259+
if (txhash_gettx_ancestor(state, sha, state->longest_knowns[0])) {
260+
if (already_known)
261+
*already_known = true;
259262
return ECODE_INPUT_OK;
263+
}
260264

261265
/* If we already have it in pending, don't re-add. */
262-
if (txhash_get_pending_tx(state, sha))
266+
if (txhash_get_pending_tx(state, sha)) {
267+
if (already_known)
268+
*already_known = true;
263269
return ECODE_INPUT_OK;
270+
}
271+
272+
if (already_known)
273+
*already_known = false;
264274

265275
/* We check inputs for where *we* would mine it.
266276
* We currently don't allow two dependent txs in the same block,

‎pending.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ enum input_ecode add_pending_tx(struct state *state,
4949
const union protocol_tx *tx,
5050
const struct protocol_double_sha *sha,
5151
unsigned int *bad_input_num,
52-
bool *too_old);
52+
bool *too_old, bool *already_known);
5353

5454
/* Get a new working block. */
5555
struct pending_block *new_pending_block(struct state *state);

‎sendrawtransaction.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static char *json_sendrawtransaction(struct json_connection *jcon,
5454
struct protocol_double_sha sha;
5555
enum protocol_ecode e;
5656
unsigned int bad_input_num;
57-
bool old;
57+
bool old, already_known;
5858

5959
json_get_params(jcon->buffer, params, "tx", &tok, NULL);
6060
if (!tok)
@@ -78,8 +78,11 @@ static char *json_sendrawtransaction(struct json_connection *jcon,
7878
json_object_start(response, NULL);
7979
json_add_double_sha(response, "tx", &sha);
8080

81-
switch (add_pending_tx(jcon->state, tx, &sha, &bad_input_num, &old)) {
81+
switch (add_pending_tx(jcon->state, tx, &sha, &bad_input_num, &old,
82+
&already_known)) {
8283
case ECODE_INPUT_OK:
84+
if (already_known)
85+
return tal_fmt(jcon, "Transaction already known");
8386
break;
8487
case ECODE_INPUT_UNKNOWN:
8588
/* Ask about this input. */

0 commit comments

Comments
 (0)
Please sign in to comment.