Browse Source
The database is hidden behind the wallet interface, which has all the wallet specific functionality. First up is the tracking of outputs.ppa-0.6.1
Christian Decker
8 years ago
committed by
Rusty Russell
7 changed files with 93 additions and 5 deletions
@ -0,0 +1,27 @@ |
|||
#include "wallet.h" |
|||
|
|||
struct wallet *wallet_new(const tal_t *ctx, struct log *log) |
|||
{ |
|||
struct wallet *wallet = tal(ctx, struct wallet); |
|||
wallet->db = db_setup(wallet); |
|||
wallet->log = log; |
|||
if (!wallet->db) { |
|||
fatal("Unable to setup the wallet database"); |
|||
} |
|||
return wallet; |
|||
} |
|||
|
|||
bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, |
|||
enum wallet_output_type type) |
|||
{ |
|||
tal_t *tmpctx = tal_tmpctx(w); |
|||
char *hextxid = tal_hexstr(tmpctx, &utxo->txid, 32); |
|||
bool result = db_exec( |
|||
__func__, w->db, |
|||
"INSERT INTO outputs (prev_out_tx, prev_out_index, value, type, " |
|||
"status, keyindex) VALUES ('%s', %d, %zu, %d, %d, %d);", |
|||
hextxid, utxo->outnum, utxo->amount, type, output_state_available, |
|||
utxo->keyindex); |
|||
tal_free(tmpctx); |
|||
return result; |
|||
} |
@ -0,0 +1,53 @@ |
|||
#ifndef WALLET_WALLET_H |
|||
#define WALLET_WALLET_H |
|||
|
|||
#include "config.h" |
|||
#include "db.h" |
|||
#include <ccan/tal/tal.h> |
|||
#include <lightningd/utxo.h> |
|||
|
|||
struct wallet { |
|||
struct db *db; |
|||
struct log *log; |
|||
}; |
|||
|
|||
/* Possible states for tracked outputs in the database. Not sure yet
|
|||
* whether we really want to have reservations reflected in the |
|||
* database, it would simplify queries at the cost of some IO ops */ |
|||
enum output_status { |
|||
output_state_available= 0, |
|||
output_state_reserved = 1, |
|||
output_state_spent = 2, |
|||
/* Special status used to express that we don't care in
|
|||
* queries */ |
|||
output_state_any = 255 |
|||
}; |
|||
|
|||
/* Enumeration of all known output types. These include all types that
|
|||
* could ever end up on-chain and we may need to react upon. Notice |
|||
* that `to_local`, `htlc_offer`, and `htlc_recv` may need immediate |
|||
* action since they are encumbered with a CSV. */ |
|||
enum wallet_output_type { |
|||
p2sh_wpkh = 0, |
|||
to_local = 1, |
|||
htlc_offer = 3, |
|||
htlc_recv = 4 |
|||
}; |
|||
|
|||
/**
|
|||
* wallet_new - Constructor for a new sqlite3 based wallet |
|||
* |
|||
* This is guaranteed to either return a valid wallet, or abort with |
|||
* `fatal` if it cannot be initialized. |
|||
*/ |
|||
struct wallet *wallet_new(const tal_t *ctx, struct log *log); |
|||
|
|||
/**
|
|||
* wallet_add_utxo - Register a UTXO which we (partially) own |
|||
* |
|||
* Add a UTXO to the set of outputs we care about. |
|||
*/ |
|||
bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, |
|||
enum wallet_output_type type); |
|||
|
|||
#endif /* WALLET_WALLET_H */ |
Loading…
Reference in new issue