Browse Source

Reduce variable scopes

ppa-0.6.1
practicalswift 7 years ago
committed by Rusty Russell
parent
commit
7e9750ffee
  1. 5
      channeld/channel.c
  2. 4
      common/bolt11.c
  3. 3
      common/test/run-json.c
  4. 3
      gossipd/routing.c
  5. 6
      hsmd/hsm.c
  6. 7
      lightningd/bitcoind.c
  7. 3
      lightningd/channel.c
  8. 3
      lightningd/connect_control.c
  9. 7
      lightningd/jsonrpc.c
  10. 9
      lightningd/pay.c
  11. 5
      lightningd/payalgo.c
  12. 3
      wallet/db.c
  13. 3
      wallet/txfilter.c
  14. 6
      wallet/wallet.c
  15. 28
      wallet/walletrpc.c

5
channeld/channel.c

@ -2299,11 +2299,10 @@ static void handle_fail(struct peer *peer, const u8 *inmsg)
failcode);
} else {
u8 *reply;
u8 *failmsg;
if (failcode) {
failmsg = make_failmsg(inmsg, peer, h,
failcode, &scid);
u8 *failmsg = make_failmsg(inmsg, peer, h,
failcode, &scid);
errpkt = create_onionreply(inmsg,
h->shared_secret,
failmsg);

4
common/bolt11.c

@ -889,14 +889,12 @@ char *bolt11_encode_(const tal_t *ctx,
{
u5 *data = tal_arr(tmpctx, u5, 0);
char *hrp, *output;
char postfix;
u64 amount;
struct bolt11_field *extra;
secp256k1_ecdsa_recoverable_signature rsig;
u8 sig_and_recid[65];
u8 *hrpu8;
int recid;
size_t i;
/* BOLT #11:
*
@ -905,10 +903,12 @@ char *bolt11_encode_(const tal_t *ctx,
* representation possible.
*/
if (b11->msatoshi) {
char postfix;
if (*b11->msatoshi % MSAT_PER_BTC == 0) {
postfix = '\0';
amount = *b11->msatoshi / MSAT_PER_BTC;
} else {
size_t i;
for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) {
if (!(*b11->msatoshi * 10 % multipliers[i].m10))
break;

3
common/test/run-json.c

@ -82,7 +82,6 @@ static int test_json_filter(void)
static void test_json_escape(void)
{
int i;
const char *str;
for (i = 1; i < 256; i++) {
char badstr[2];
@ -97,7 +96,7 @@ static void test_json_escape(void)
json_add_escaped_string(result, "x", take(esc));
json_object_end(result);
str = json_result_string(result);
const char *str = json_result_string(result);
if (i == '\\' || i == '"'
|| i == '\n' || i == '\r' || i == '\b'
|| i == '\t' || i == '\f')

3
gossipd/routing.c

@ -1302,7 +1302,6 @@ void routing_failure(struct routing_state *rstate,
const u8 *channel_update)
{
struct node *node;
int i;
time_t now = time_now().ts.tv_sec;
status_trace("Received routing failure 0x%04x (%s), "
@ -1330,7 +1329,7 @@ void routing_failure(struct routing_state *rstate,
*
*/
if (failcode & NODE) {
for (i = 0; i < tal_count(node->chans); ++i) {
for (int i = 0; i < tal_count(node->chans); ++i) {
routing_failure_channel_out(tmpctx, node, failcode,
node->chans[i],
now);

6
hsmd/hsm.c

@ -608,7 +608,6 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
struct pubkey local_pubkey, remote_pubkey;
struct utxo **utxomap;
struct bitcoin_tx *tx;
u8 *wscript;
u16 outnum;
size_t i;
struct pubkey changekey;
@ -644,7 +643,7 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
else
subscript = NULL;
wscript = p2wpkh_scriptcode(tmpctx, &inkey);
u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey);
sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey,
&sig);
@ -673,7 +672,6 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
u64 satoshi_out, change_out;
u32 change_keyindex;
struct utxo **utxos;
u8 *wscript;
u8 **scriptSigs;
struct bitcoin_tx *tx;
struct ext_key ext;
@ -715,7 +713,7 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
else
subscript = NULL;
wscript = p2wpkh_scriptcode(tmpctx, &inkey);
u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey);
sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey,
&sig);

7
lightningd/bitcoind.c

@ -738,10 +738,9 @@ static void fatal_bitcoind_failure(struct bitcoind *bitcoind, const char *error_
void wait_for_bitcoind(struct bitcoind *bitcoind)
{
int from, ret, status;
int from, status;
pid_t child;
const char **cmd = cmdarr(bitcoind, bitcoind, "echo", NULL);
char *output;
bool printed = false;
for (;;) {
@ -753,12 +752,12 @@ void wait_for_bitcoind(struct bitcoind *bitcoind)
fatal("%s exec failed: %s", cmd[0], strerror(errno));
}
output = grab_fd(cmd, from);
char *output = grab_fd(cmd, from);
if (!output)
fatal("Reading from %s failed: %s",
cmd[0], strerror(errno));
ret = waitpid(child, &status, 0);
int ret = waitpid(child, &status, 0);
if (ret != child)
fatal("Waiting for %s: %s", cmd[0], strerror(errno));
if (!WIFEXITED(status))

3
lightningd/channel.c

@ -287,7 +287,6 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...)
struct lightningd *ld = channel->peer->ld;
va_list ap;
char *why;
u8 *msg;
struct channel_id cid;
va_start(ap, fmt);
@ -295,7 +294,7 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...)
va_end(ap);
if (channel->scid) {
msg = towire_gossip_disable_channel(NULL,
u8 *msg = towire_gossip_disable_channel(NULL,
channel->scid,
channel->peer->direction,
false);

3
lightningd/connect_control.c

@ -111,7 +111,6 @@ static void json_connect(struct command *cmd,
char *id_str;
char *atptr;
char *ataddr = NULL;
int atidx;
const char *name;
struct wireaddr addr;
u8 *msg;
@ -130,7 +129,7 @@ static void json_connect(struct command *cmd,
idtok->end - idtok->start);
atptr = strchr(id_str, '@');
if (atptr) {
atidx = atptr - id_str;
int atidx = atptr - id_str;
ataddr = tal_strdup(cmd, atptr + 1);
/* Cut id. */
idtok->end = idtok->start + atidx;

7
lightningd/jsonrpc.c

@ -487,7 +487,7 @@ bool json_get_params(struct command *cmd,
const char **names;
size_t num_names;
/* Uninitialized warnings on p and end */
const jsmntok_t **tokptr, *p = NULL, *end = NULL;
const jsmntok_t *p = NULL, *end = NULL;
if (param->type == JSMN_ARRAY) {
if (param->size == 0)
@ -505,7 +505,7 @@ bool json_get_params(struct command *cmd,
names = tal_arr(cmd, const char *, num_names + 1);
va_start(ap, param);
while ((names[num_names] = va_arg(ap, const char *)) != NULL) {
tokptr = va_arg(ap, const jsmntok_t **);
const jsmntok_t **tokptr = va_arg(ap, const jsmntok_t **);
bool compulsory = true;
if (names[num_names][0] == '?') {
names[num_names]++;
@ -788,7 +788,6 @@ json_tok_address_scriptpubkey(const tal_t *cxt,
* of fixed size 40 will not overflow. */
uint8_t witness_program[40];
size_t witness_program_len;
bool witness_ok;
char *addrz;
const char *bip173;
@ -826,7 +825,7 @@ json_tok_address_scriptpubkey(const tal_t *cxt,
&witness_program_len, addrz);
if (bip173) {
witness_ok = false;
bool witness_ok = false;
if (witness_version == 0 && (witness_program_len == 20 ||
witness_program_len == 32)) {
witness_ok = true;

9
lightningd/pay.c

@ -419,8 +419,6 @@ void payment_store(struct lightningd *ld,
void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
const char *localfail)
{
struct onionreply *reply;
struct secret *path_secrets;
struct wallet_payment *payment;
struct routing_failure* fail = NULL;
const char *failmsg;
@ -468,8 +466,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
assert(!hout->failcode);
failmsg = "reply from remote";
/* Try to parse reply. */
path_secrets = payment->path_secrets;
reply = unwrap_onionreply(tmpctx, path_secrets,
struct secret *path_secrets = payment->path_secrets;
struct onionreply *reply = unwrap_onionreply(tmpctx, path_secrets,
tal_count(path_secrets),
hout->failuremsg);
if (!reply) {
@ -895,11 +893,10 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r,
void *vcmd)
{
struct command *cmd = (struct command*) vcmd;
struct json_result *response;
if (!r->succeeded && r->errorcode == PAY_IN_PROGRESS) {
/* This is normal for sendpay. Succeed. */
response = new_json_result(cmd);
struct json_result *response = new_json_result(cmd);
json_object_start(response, NULL);
json_add_string(response, "message",
"Monitor status with listpayments or waitsendpay");

5
lightningd/payalgo.c

@ -200,7 +200,7 @@ json_pay_success(struct pay *pay,
static void json_pay_failure(struct pay *pay,
const struct sendpay_result *r)
{
struct json_result *data = NULL;
struct json_result *data;
const char *msg = NULL;
struct routing_failure *fail;
@ -485,12 +485,11 @@ static bool json_pay_try(struct pay *pay)
u8 *req;
struct command *cmd = pay->cmd;
struct timeabs now = time_now();
struct json_result *data;
struct siphash_seed seed;
/* If too late anyway, fail now. */
if (time_after(now, pay->expiry)) {
data = new_json_result(cmd);
struct json_result *data = new_json_result(cmd);
json_object_start(data, NULL);
json_add_num(data, "now", now.ts.tv_sec);
json_add_num(data, "expiry", pay->expiry.ts.tv_sec);

3
wallet/db.c

@ -525,7 +525,6 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval)
{
int err;
s64 res = defval;
const unsigned char *stringvar;
sqlite3_stmt *stmt =
db_query(__func__, db,
"SELECT val FROM vars WHERE name='%s' LIMIT 1", varname);
@ -535,7 +534,7 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval)
err = sqlite3_step(stmt);
if (err == SQLITE_ROW) {
stringvar = sqlite3_column_text(stmt, 0);
const unsigned char *stringvar = sqlite3_column_text(stmt, 0);
res = atol((const char *)stringvar);
}
sqlite3_finalize(stmt);

3
wallet/txfilter.c

@ -75,9 +75,8 @@ void txfilter_add_derkey(struct txfilter *filter,
bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx)
{
u8 *oscript;
for (size_t i = 0; i < tal_count(tx->output); i++) {
oscript = tx->output[i].script;
u8 *oscript = tx->output[i].script;
for (size_t j = 0; j < tal_count(filter->scriptpubkeys); j++) {
if (scripteq(oscript, filter->scriptpubkeys[j]))

6
wallet/wallet.c

@ -526,7 +526,6 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid,
struct peer *peer)
{
bool ok;
const unsigned char *addrstr;
sqlite3_stmt *stmt = db_prepare(w->db, "SELECT id, node_id, address FROM peers WHERE node_id=?;");
sqlite3_bind_pubkey(stmt, 1, nodeid);
@ -534,7 +533,7 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid,
if (ok) {
peer->dbid = sqlite3_column_int64(stmt, 0);
ok &= sqlite3_column_pubkey(stmt, 1, &peer->id);
addrstr = sqlite3_column_text(stmt, 2);
const unsigned char *addrstr = sqlite3_column_text(stmt, 2);
if (addrstr)
parse_wireaddr((const char*)addrstr, &peer->addr, DEFAULT_PORT, NULL);
@ -1852,7 +1851,6 @@ void wallet_payment_set_failinfo(struct wallet *wallet,
const u8 *failupdate /*tal_arr*/)
{
sqlite3_stmt *stmt;
struct short_channel_id *scid;
stmt = db_prepare(wallet->db,
"UPDATE payments"
@ -1880,7 +1878,7 @@ void wallet_payment_set_failinfo(struct wallet *wallet,
if (failchannel) {
/* sqlite3_bind_short_channel_id requires the input
* channel to be tal-allocated... */
scid = tal(tmpctx, struct short_channel_id);
struct short_channel_id *scid = tal(tmpctx, struct short_channel_id);
*scid = *failchannel;
sqlite3_bind_short_channel_id(stmt, 6, scid);
} else

28
wallet/walletrpc.c

@ -46,7 +46,6 @@ static void wallet_withdrawal_broadcast(struct bitcoind *bitcoind UNUSED,
{
struct command *cmd = withdraw->cmd;
struct lightningd *ld = withdraw->cmd->ld;
struct bitcoin_tx *tx;
u64 change_satoshi = 0;
/* Massage output into shape so it doesn't kill the JSON serialization */
@ -57,7 +56,7 @@ static void wallet_withdrawal_broadcast(struct bitcoind *bitcoind UNUSED,
/* Parse the tx and extract the change output. We
* generated the hex tx, so this should always work */
tx = bitcoin_tx_from_hex(withdraw, withdraw->hextx, strlen(withdraw->hextx));
struct bitcoin_tx *tx = bitcoin_tx_from_hex(withdraw, withdraw->hextx, strlen(withdraw->hextx));
assert(tx != NULL);
wallet_extract_owned_outputs(ld->wallet, tx, NULL, &change_satoshi);
@ -208,11 +207,9 @@ static void json_newaddr(struct command *cmd, const char *buffer UNUSED,
struct ripemd160 h160;
struct pubkey pubkey;
jsmntok_t *addrtype;
u8 *redeemscript;
bool is_p2wpkh, ok;
bool is_p2wpkh;
s64 keyidx;
char *out;
const char *hrp;
if (!json_get_params(cmd, buffer, params,
"?addresstype", &addrtype, NULL)) {
@ -251,18 +248,18 @@ static void json_newaddr(struct command *cmd, const char *buffer UNUSED,
txfilter_add_derkey(cmd->ld->owned_txfilter, ext.pub_key);
if (is_p2wpkh) {
hrp = get_chainparams(cmd->ld)->bip173_name;
const char *hrp = get_chainparams(cmd->ld)->bip173_name;
/* out buffer is 73 + strlen(human readable part). see bech32.h */
out = tal_arr(cmd, char, 73 + strlen(hrp));
pubkey_to_hash160(&pubkey, &h160);
ok = segwit_addr_encode(out, hrp, 0, h160.u.u8, sizeof(h160.u.u8));
bool ok = segwit_addr_encode(out, hrp, 0, h160.u.u8, sizeof(h160.u.u8));
if (!ok) {
command_fail(cmd, "p2wpkh address encoding failure.");
return;
}
}
else {
redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey);
u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey);
sha256(&h, redeemscript, tal_count(redeemscript));
ripemd160(&h160, h.u.u8, sizeof(h));
out = p2sh_to_base58(cmd,
@ -292,11 +289,6 @@ static void json_listaddrs(struct command *cmd,
struct ripemd160 h160;
struct pubkey pubkey;
jsmntok_t *bip32tok;
u8 *redeemscript;
bool ok;
char *out_p2sh;
char *out_p2wpkh;
const char *hrp;
u64 bip32_max_index;
if (!json_get_params(cmd, buffer, params,
@ -330,18 +322,18 @@ static void json_listaddrs(struct command *cmd,
}
// p2sh
redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey);
u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey);
sha256(&h, redeemscript, tal_count(redeemscript));
ripemd160(&h160, h.u.u8, sizeof(h));
out_p2sh = p2sh_to_base58(cmd,
char *out_p2sh = p2sh_to_base58(cmd,
get_chainparams(cmd->ld)->testnet, &h160);
// bech32 : p2wpkh
hrp = get_chainparams(cmd->ld)->bip173_name;
const char *hrp = get_chainparams(cmd->ld)->bip173_name;
/* out buffer is 73 + strlen(human readable part). see bech32.h */
out_p2wpkh = tal_arr(cmd, char, 73 + strlen(hrp));
char *out_p2wpkh = tal_arr(cmd, char, 73 + strlen(hrp));
pubkey_to_hash160(&pubkey, &h160);
ok = segwit_addr_encode(out_p2wpkh, hrp, 0, h160.u.u8, sizeof(h160.u.u8));
bool ok = segwit_addr_encode(out_p2wpkh, hrp, 0, h160.u.u8, sizeof(h160.u.u8));
if (!ok) {
command_fail(cmd, "p2wpkh address encoding failure.");
return;

Loading…
Cancel
Save