Skip to content

Commit

Permalink
Removed redundancies in withdraw and fundchannel.
Browse files Browse the repository at this point in the history
No new functionality, just a continuation of my work toward completing ElementsProject#665.

I removed the common members of `struct withdrawal` and `struct fund_channel`
and placed them in a new `struct wallet_tx`.  Then it was fairly straightforward
to reimplement the existing code in terms of `wallet_tx`.

Since I made some structural changes I wanted to get this approved before I
go any farther.

Added 'all' to fundchannel help message.
  • Loading branch information
wythe committed Apr 30, 2018
1 parent 91d149b commit 9a513e2
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 195 deletions.
1 change: 1 addition & 0 deletions common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ COMMON_SRC_NOGEN := \
common/utils.c \
common/utxo.c \
common/version.c \
common/wallet_tx.c \
common/wireaddr.c \
common/wire_error.c \
common/withdraw_tx.c
Expand Down
49 changes: 49 additions & 0 deletions common/wallet_tx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <common/wallet_tx.h>
#include <inttypes.h>
#include <lightningd/jsonrpc_errors.h>
#include <wallet/wallet.h>

void wtx_init(struct command *cmd, struct wallet_tx * wtx)
{
wtx->cmd = cmd;
wtx->amount = 0;
wtx->change_key_index = 0;
wtx->utxos = NULL;
wtx->all_funds = false;
wtx->desttok = NULL;
}

bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
size_t out_len)
{
u64 fee_estimate;
if (tx->all_funds) {
tx->utxos = wallet_select_all(tx->cmd, tx->cmd->ld->wallet,
fee_rate_per_kw, out_len,
&tx->amount,
&fee_estimate);
if (!tx->utxos || tx->amount < 546) {
command_fail(tx->cmd, "Cannot afford fee %"PRIu64,
fee_estimate);
return false;
}
tx->change = 0;
} else {
tx->utxos = wallet_select_coins(tx->cmd, tx->cmd->ld->wallet,
tx->amount,
fee_rate_per_kw, out_len,
&fee_estimate, &tx->change);
if (!tx->utxos || tx->amount < 546) {
command_fail(tx->cmd,
"Cannot afford funding transaction");
return false;
}
if (tx->change < 546) {
tx->change = 0;
tx->change_key_index = 0;
} else {
tx->change_key_index = wallet_get_newindex(tx->cmd->ld);
}
}
return true;
}
27 changes: 27 additions & 0 deletions common/wallet_tx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef LIGHTNING_COMMON_WALLET_TX_H
#define LIGHTNING_COMMON_WALLET_TX_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>

/* A specification of funds in the wallet used for funding channels and
* withdrawal.
*/
struct wallet_tx {
struct command *cmd;
u64 amount;
u64 change;
u32 change_key_index;
const struct utxo **utxos;

bool all_funds;
const jsmntok_t *desttok;
const jsmntok_t *sattok;
};

void wtx_init(struct command *cmd, struct wallet_tx *wtx);
bool wtx_select_utxos(struct wallet_tx * tx, u32 fee_rate_per_kw,
size_t out_len);
#endif /* LIGHTNING_COMMON_WALLET_TX_H */
2 changes: 1 addition & 1 deletion lightningd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ LIGHTNINGD_COMMON_OBJS := \
common/utils.o \
common/utxo.o \
common/version.o \
common/wallet_tx.o \
common/wire_error.o \
common/wireaddr.o \
common/withdraw_tx.o

LIGHTNINGD_SRC := \
lightningd/bitcoind.c \
lightningd/build_utxos.c \
lightningd/chaintopology.c \
lightningd/channel.c \
lightningd/channel_control.c \
Expand Down
34 changes: 0 additions & 34 deletions lightningd/build_utxos.c

This file was deleted.

15 changes: 0 additions & 15 deletions lightningd/build_utxos.h

This file was deleted.

12 changes: 12 additions & 0 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <common/json_escaped.h>
#include <common/memleak.h>
#include <common/version.h>
#include <common/wallet_tx.h>
#include <common/wireaddr.h>
#include <errno.h>
#include <fcntl.h>
Expand Down Expand Up @@ -852,3 +853,14 @@ json_tok_address_scriptpubkey(const tal_t *cxt,

return ADDRESS_PARSE_UNRECOGNIZED;
}

bool json_tok_wtx(struct wallet_tx * tx, const char * buffer)
{
if (json_tok_streq(buffer, tx->sattok, "all")) {
tx->all_funds = true;
} else if (!json_tok_u64(buffer, tx->sattok, &tx->amount)) {
command_fail(tx->cmd, "Invalid satoshis");
return false;
}
return true;
}
4 changes: 4 additions & 0 deletions lightningd/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

struct bitcoin_txid;
struct wireaddr;
struct wallet_tx;

/* Context for a command (from JSON, but might outlive the connection!)
* You can allocate off this for temporary objects. */
Expand Down Expand Up @@ -101,5 +102,8 @@ json_tok_address_scriptpubkey(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok, const u8 **scriptpubkey);

/* Parse the satoshi token in wallet_tx. */
bool json_tok_wtx(struct wallet_tx * tx, const char * buffer);

AUTODATA_TYPE(json_command, struct json_command);
#endif /* LIGHTNING_LIGHTNINGD_JSONRPC_H */
Loading

0 comments on commit 9a513e2

Please sign in to comment.