mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +01:00
remove_dust: don't ever create dust outputs.
This behavior will be specified in BOLT #3. Closes: #14 Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
2
Makefile
2
Makefile
@@ -44,6 +44,7 @@ CORE_SRC := \
|
|||||||
opt_bits.c \
|
opt_bits.c \
|
||||||
permute_tx.c \
|
permute_tx.c \
|
||||||
protobuf_convert.c \
|
protobuf_convert.c \
|
||||||
|
remove_dust.c \
|
||||||
version.c
|
version.c
|
||||||
CORE_OBJS := $(CORE_SRC:.c=.o)
|
CORE_OBJS := $(CORE_SRC:.c=.o)
|
||||||
|
|
||||||
@@ -152,6 +153,7 @@ CORE_HEADERS := close_tx.h \
|
|||||||
overflows.h \
|
overflows.h \
|
||||||
permute_tx.h \
|
permute_tx.h \
|
||||||
protobuf_convert.h \
|
protobuf_convert.h \
|
||||||
|
remove_dust.h \
|
||||||
state.h \
|
state.h \
|
||||||
state_types.h \
|
state_types.h \
|
||||||
version.h
|
version.h
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "funding.h"
|
#include "funding.h"
|
||||||
#include "overflows.h"
|
#include "overflows.h"
|
||||||
#include "permute_tx.h"
|
#include "permute_tx.h"
|
||||||
|
#include "remove_dust.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
static bool add_htlc(struct bitcoin_tx *tx, size_t n,
|
static bool add_htlc(struct bitcoin_tx *tx, size_t n,
|
||||||
@@ -98,5 +99,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
|
|||||||
|
|
||||||
*map = tal_arr(ctx, int, tx->output_count);
|
*map = tal_arr(ctx, int, tx->output_count);
|
||||||
permute_outputs(tx->output, tx->output_count, *map);
|
permute_outputs(tx->output, tx->output_count, *map);
|
||||||
|
remove_dust(tx, *map);
|
||||||
|
|
||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|||||||
28
remove_dust.c
Normal file
28
remove_dust.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "remove_dust.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void remove_dust(struct bitcoin_tx *tx, int *map)
|
||||||
|
{
|
||||||
|
size_t i, j, num = tx->output_count;
|
||||||
|
|
||||||
|
assert(tal_count(map) == num);
|
||||||
|
/* Do it in map order so we can remove from map, too */
|
||||||
|
for (i = 0; i < num; i++) {
|
||||||
|
assert(map[i] < tx->output_count);
|
||||||
|
if (tx->output[map[i]].amount >= DUST_THRESHOLD)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* Eliminate that output from tx */
|
||||||
|
tx->output_count--;
|
||||||
|
memmove(tx->output + map[i], tx->output + map[i] + 1,
|
||||||
|
(tx->output_count-map[i]) * sizeof(*tx->output));
|
||||||
|
|
||||||
|
/* Fixup map. */
|
||||||
|
for (j = 0; j < num; j++)
|
||||||
|
if (map[j] > map[i])
|
||||||
|
map[j]--;
|
||||||
|
map[i] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
remove_dust.h
Normal file
12
remove_dust.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef LIGHTNING_REMOVE_DUST_H
|
||||||
|
#define LIGHTNING_REMOVE_DUST_H
|
||||||
|
#include "config.h"
|
||||||
|
#include "bitcoin/tx.h"
|
||||||
|
|
||||||
|
/* Remove all dust outputs from tx */
|
||||||
|
void remove_dust(struct bitcoin_tx *tx, int *map);
|
||||||
|
|
||||||
|
/* Less than this is dust. */
|
||||||
|
#define DUST_THRESHOLD 546
|
||||||
|
|
||||||
|
#endif /* LIGHTNING_REMOVE_DUST_H */
|
||||||
Reference in New Issue
Block a user