mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-03 22:34:21 +01:00
utils: remove tal_tmpctx altogether, use global.
In particular, we now only free tmpctx at the end of main(). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -410,7 +410,6 @@ static char *decode_r(struct bolt11 *b11,
|
||||
u5 **data, size_t *data_len,
|
||||
size_t data_length)
|
||||
{
|
||||
tal_t *tmpctx = tal_tmpctx(b11);
|
||||
size_t rlen = data_length * 5 / 8;
|
||||
u8 *r8 = tal_arr(tmpctx, u8, rlen);
|
||||
size_t n = 0;
|
||||
@@ -423,7 +422,6 @@ static char *decode_r(struct bolt11 *b11,
|
||||
do {
|
||||
tal_resize(&r, n+1);
|
||||
if (!fromwire_route_info(&cursor, &rlen, &r[n])) {
|
||||
tal_free(tmpctx);
|
||||
return tal_fmt(b11, "r: hop %zu truncated", n);
|
||||
}
|
||||
n++;
|
||||
@@ -434,7 +432,6 @@ static char *decode_r(struct bolt11 *b11,
|
||||
tal_resize(&b11->routes, n+1);
|
||||
b11->routes[n] = tal_steal(b11, r);
|
||||
|
||||
tal_free(tmpctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -464,7 +461,6 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
|
||||
u5 *data;
|
||||
size_t data_len;
|
||||
struct bolt11 *b11 = new_bolt11(ctx, NULL);
|
||||
tal_t *tmpctx = tal_tmpctx(b11);
|
||||
u8 sig_and_recid[65];
|
||||
secp256k1_ecdsa_recoverable_signature sig;
|
||||
struct hash_u5 hu5;
|
||||
@@ -701,7 +697,6 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
|
||||
return decode_fail(b11, fail, "invalid signature");
|
||||
}
|
||||
|
||||
tal_free(tmpctx);
|
||||
return b11;
|
||||
}
|
||||
|
||||
@@ -893,7 +888,6 @@ char *bolt11_encode_(const tal_t *ctx,
|
||||
void *arg),
|
||||
void *arg)
|
||||
{
|
||||
tal_t *tmpctx = tal_tmpctx(ctx);
|
||||
u5 *data = tal_arr(tmpctx, u5, 0);
|
||||
char *hrp, *output;
|
||||
char postfix;
|
||||
@@ -971,12 +965,12 @@ char *bolt11_encode_(const tal_t *ctx,
|
||||
|
||||
/* FIXME: towire_ should check this? */
|
||||
if (tal_len(data) > 65535)
|
||||
return tal_free(tmpctx);
|
||||
return NULL;
|
||||
|
||||
/* Need exact length here */
|
||||
hrpu8 = tal_dup_arr(tmpctx, u8, (const u8 *)hrp, strlen(hrp), 0);
|
||||
if (!sign(data, hrpu8, &rsig, arg))
|
||||
return tal_free(tmpctx);
|
||||
return NULL;
|
||||
|
||||
secp256k1_ecdsa_recoverable_signature_serialize_compact(
|
||||
secp256k1_ctx,
|
||||
@@ -991,7 +985,6 @@ char *bolt11_encode_(const tal_t *ctx,
|
||||
if (!bech32_encode(output, hrp, data, tal_count(data), (size_t)-1))
|
||||
output = tal_free(output);
|
||||
|
||||
tal_free(tmpctx);
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,6 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
u64 obscured_commitment_number,
|
||||
enum side side)
|
||||
{
|
||||
const tal_t *tmpctx = tal_tmpctx(ctx);
|
||||
u64 base_fee_msat;
|
||||
struct bitcoin_tx *tx;
|
||||
size_t n, untrimmed;
|
||||
@@ -195,6 +194,5 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
|
||||
/* Input amount needed for signature code. */
|
||||
tx->input[0].amount = tal_dup(tx->input, u64, &funding_satoshis);
|
||||
|
||||
tal_free(tmpctx);
|
||||
return tx;
|
||||
}
|
||||
|
||||
@@ -530,7 +530,6 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
|
||||
const struct secret *shared_secrets,
|
||||
const int numhops, const u8 *reply)
|
||||
{
|
||||
tal_t *tmpctx = tal_tmpctx(ctx);
|
||||
struct onionreply *oreply = tal(tmpctx, struct onionreply);
|
||||
u8 *msg = tal_arr(oreply, u8, tal_len(reply));
|
||||
u8 key[KEY_LEN], hmac[HMAC_SIZE];
|
||||
@@ -539,7 +538,7 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
|
||||
u16 msglen;
|
||||
|
||||
if (tal_len(reply) != ONION_REPLY_SIZE + sizeof(hmac) + 4) {
|
||||
goto fail;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(msg, reply, tal_len(reply));
|
||||
@@ -561,7 +560,7 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
|
||||
}
|
||||
}
|
||||
if (oreply->origin_index == -1) {
|
||||
goto fail;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cursor = msg + sizeof(hmac);
|
||||
@@ -569,15 +568,13 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
|
||||
msglen = fromwire_u16(&cursor, &max);
|
||||
|
||||
if (msglen > ONION_REPLY_SIZE) {
|
||||
goto fail;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
oreply->msg = tal_arr(oreply, u8, msglen);
|
||||
fromwire(&cursor, &max, oreply->msg, msglen);
|
||||
|
||||
tal_steal(ctx, oreply);
|
||||
tal_free(tmpctx);
|
||||
return oreply;
|
||||
fail:
|
||||
return tal_free(tmpctx);
|
||||
|
||||
}
|
||||
|
||||
@@ -68,7 +68,6 @@ static void test_b11(const char *b11str,
|
||||
const char *hashed_desc)
|
||||
{
|
||||
struct bolt11 *b11;
|
||||
const void *tmpctx = tal_tmpctx(NULL);
|
||||
char *fail;
|
||||
char *reproduce;
|
||||
|
||||
@@ -111,7 +110,6 @@ static void test_b11(const char *b11str,
|
||||
abort();
|
||||
}
|
||||
assert(streq(reproduce, b11str));
|
||||
tal_free(tmpctx);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -119,10 +117,10 @@ int main(void)
|
||||
struct bolt11 *b11;
|
||||
struct pubkey node;
|
||||
u64 msatoshi;
|
||||
const void *ctx = tal_tmpctx(NULL);
|
||||
|
||||
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
||||
| SECP256K1_CONTEXT_SIGN);
|
||||
setup_tmpctx();
|
||||
|
||||
/* BOLT #11:
|
||||
*
|
||||
@@ -159,7 +157,7 @@ int main(void)
|
||||
* * `8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcq`: signature
|
||||
* * `ca784w`: Bech32 checksum
|
||||
*/
|
||||
b11 = new_bolt11(ctx, NULL);
|
||||
b11 = new_bolt11(tmpctx, NULL);
|
||||
b11->chain = chainparams_for_network("bitcoin");
|
||||
b11->timestamp = 1496314658;
|
||||
if (!hex_decode("0001020304050607080900010203040506070809000102030405060708090102",
|
||||
@@ -193,7 +191,7 @@ int main(void)
|
||||
* * `fj9srp`: Bech32 checksum
|
||||
*/
|
||||
msatoshi = 2500 * (1000ULL * 100000000) / 1000000;
|
||||
b11 = new_bolt11(ctx, &msatoshi);
|
||||
b11 = new_bolt11(tmpctx, &msatoshi);
|
||||
b11->chain = chainparams_for_network("bitcoin");
|
||||
b11->timestamp = 1496314658;
|
||||
if (!hex_decode("0001020304050607080900010203040506070809000102030405060708090102",
|
||||
@@ -225,7 +223,7 @@ int main(void)
|
||||
* * `2yxxz7`: Bech32 checksum
|
||||
*/
|
||||
msatoshi = 20 * (1000ULL * 100000000) / 1000;
|
||||
b11 = new_bolt11(ctx, &msatoshi);
|
||||
b11 = new_bolt11(tmpctx, &msatoshi);
|
||||
b11->chain = chainparams_for_network("bitcoin");
|
||||
b11->timestamp = 1496314658;
|
||||
if (!hex_decode("0001020304050607080900010203040506070809000102030405060708090102",
|
||||
@@ -239,6 +237,6 @@ int main(void)
|
||||
/* FIXME: Test the others! */
|
||||
|
||||
secp256k1_context_destroy(secp256k1_ctx);
|
||||
tal_free(ctx);
|
||||
tal_free(tmpctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,48 +28,49 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
|
||||
int main(void)
|
||||
{
|
||||
struct wireaddr addr;
|
||||
tal_t *ctx = tal_tmpctx(NULL);
|
||||
char *ip;
|
||||
u16 port;
|
||||
|
||||
setup_tmpctx();
|
||||
|
||||
/* Grossly invalid. */
|
||||
assert(!separate_address_and_port(ctx, "[", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "[123", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "[::1]:8f", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "127.0.0.1:8f", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "127.0.0.1:0", &ip, &port));
|
||||
assert(!separate_address_and_port(ctx, "127.0.0.1:ff", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "[", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "[123", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "[::1]:8f", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "127.0.0.1:8f", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "127.0.0.1:0", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "127.0.0.1:ff", &ip, &port));
|
||||
|
||||
/* ret = getaddrinfo("[::1]:80", NULL, NULL, &res); */
|
||||
assert(separate_address_and_port(ctx, "[::1]:80", &ip, &port));
|
||||
assert(separate_address_and_port(tmpctx, "[::1]:80", &ip, &port));
|
||||
assert(streq(ip, "::1"));
|
||||
assert(port == 80);
|
||||
|
||||
port = 0;
|
||||
assert(separate_address_and_port(ctx, "ip6-localhost", &ip, &port));
|
||||
assert(separate_address_and_port(tmpctx, "ip6-localhost", &ip, &port));
|
||||
assert(streq(ip, "ip6-localhost"));
|
||||
assert(port == 0);
|
||||
|
||||
assert(separate_address_and_port(ctx, "::1", &ip, &port));
|
||||
assert(separate_address_and_port(tmpctx, "::1", &ip, &port));
|
||||
assert(streq(ip, "::1"));
|
||||
assert(port == 0);
|
||||
|
||||
assert(separate_address_and_port(ctx, "192.168.1.1:8000", &ip, &port));
|
||||
assert(separate_address_and_port(tmpctx, "192.168.1.1:8000", &ip, &port));
|
||||
assert(streq(ip, "192.168.1.1"));
|
||||
assert(port == 8000);
|
||||
|
||||
port = 0;
|
||||
assert(separate_address_and_port(ctx, "192.168.2.255", &ip, &port));
|
||||
assert(separate_address_and_port(tmpctx, "192.168.2.255", &ip, &port));
|
||||
assert(streq(ip, "192.168.2.255"));
|
||||
assert(port == 0);
|
||||
|
||||
// unusual but possibly valid case
|
||||
assert(separate_address_and_port(ctx, "[::1]", &ip, &port));
|
||||
assert(separate_address_and_port(tmpctx, "[::1]", &ip, &port));
|
||||
assert(streq(ip, "::1"));
|
||||
assert(port == 0);
|
||||
|
||||
// service names not supported yet
|
||||
assert(!separate_address_and_port(ctx, "[::1]:http", &ip, &port));
|
||||
assert(!separate_address_and_port(tmpctx, "[::1]:http", &ip, &port));
|
||||
|
||||
// localhost hostnames for backward compat
|
||||
parse_wireaddr("localhost", &addr, 200, NULL);
|
||||
@@ -78,18 +79,18 @@ int main(void)
|
||||
// string should win the port battle
|
||||
parse_wireaddr("[::1]:9735", &addr, 500, NULL);
|
||||
assert(addr.port == 9735);
|
||||
ip = fmt_wireaddr(ctx, &addr);
|
||||
ip = fmt_wireaddr(tmpctx, &addr);
|
||||
assert(streq(ip, "[::1]:9735"));
|
||||
|
||||
// should use argument if we have no port in string
|
||||
parse_wireaddr("2001:db8:85a3::8a2e:370:7334", &addr, 9777, NULL);
|
||||
assert(addr.port == 9777);
|
||||
|
||||
ip = fmt_wireaddr(ctx, &addr);
|
||||
ip = fmt_wireaddr(tmpctx, &addr);
|
||||
assert(streq(ip, "[2001:db8:85a3::8a2e:370:7334]:9777"));
|
||||
|
||||
assert(parse_wireaddr("[::ffff:127.0.0.1]:49150", &addr, 1, NULL));
|
||||
assert(addr.port == 49150);
|
||||
tal_free(ctx);
|
||||
tal_free(tmpctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -72,7 +72,6 @@ static struct secret secret_from_hex(const char *hex)
|
||||
* we match the test vectors and that we can also unwrap it. */
|
||||
static void run_unit_tests(void)
|
||||
{
|
||||
tal_t *tmpctx = tal_tmpctx(NULL);
|
||||
struct onionreply *oreply;
|
||||
u8 *reply;
|
||||
u8 *raw = tal_hexdata(tmpctx, "2002", 4);
|
||||
@@ -155,19 +154,17 @@ static void run_unit_tests(void)
|
||||
oreply = unwrap_onionreply(tmpctx, ss, 5, reply);
|
||||
printf("unwrapped %s\n", tal_hex(tmpctx, oreply->msg));
|
||||
assert(memcmp(raw, oreply->msg, tal_len(raw)) == 0);
|
||||
|
||||
tal_free(tmpctx);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
bool generate = false, decode = false, unit = false;
|
||||
const tal_t *ctx = talz(NULL, tal_t);
|
||||
u8 assocdata[32];
|
||||
memset(assocdata, 'B', sizeof(assocdata));
|
||||
|
||||
secp256k1_ctx = secp256k1_context_create(
|
||||
SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
|
||||
setup_tmpctx();
|
||||
|
||||
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
||||
"--generate <pubkey1> <pubkey2>... OR\n"
|
||||
@@ -194,6 +191,6 @@ int main(int argc, char **argv)
|
||||
}
|
||||
secp256k1_context_destroy(secp256k1_ctx);
|
||||
opt_free_table();
|
||||
tal_free(ctx);
|
||||
tal_free(tmpctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -33,10 +33,8 @@ struct oneshot *new_reltimer_(struct timers *timers,
|
||||
void timer_expired(tal_t *ctx, struct timer *timer)
|
||||
{
|
||||
struct oneshot *t = container_of(timer, struct oneshot, timer);
|
||||
const tal_t *tmpctx = tal_tmpctx(ctx);
|
||||
|
||||
/* If it doesn't free itself, freeing tmpctx will do it */
|
||||
tal_steal(tmpctx, t);
|
||||
t->cb(t->arg);
|
||||
tal_free(tmpctx);
|
||||
}
|
||||
|
||||
@@ -16,9 +16,6 @@ char *tal_hex(const tal_t *ctx, const tal_t *data);
|
||||
/* Allocate and fill a buffer with the data of this hex string. */
|
||||
u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len);
|
||||
|
||||
/* FIXME: Remove in favor of global */
|
||||
#define tal_tmpctx(ctx) tal((ctx), char)
|
||||
|
||||
/* Global temporary convenience context: freed in io loop core. */
|
||||
extern const tal_t *tmpctx;
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ REGISTER_TYPE_TO_STRING(wireaddr, fmt_wireaddr);
|
||||
* Returns false if it wasn't one of these forms. If it returns true,
|
||||
* it only overwrites *port if it was specified by <number> above.
|
||||
*/
|
||||
static bool separate_address_and_port(tal_t *ctx, const char *arg,
|
||||
static bool separate_address_and_port(const tal_t *ctx, const char *arg,
|
||||
char **addr, u16 *port)
|
||||
{
|
||||
char *portcolon;
|
||||
@@ -126,7 +126,6 @@ bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 defport,
|
||||
u16 port;
|
||||
char *ip;
|
||||
bool res;
|
||||
tal_t *tmpctx = tal_tmpctx(NULL);
|
||||
|
||||
res = false;
|
||||
port = defport;
|
||||
@@ -195,6 +194,5 @@ bool parse_wireaddr(const char *arg, struct wireaddr *addr, u16 defport,
|
||||
finish:
|
||||
if (!res && err_msg && !*err_msg)
|
||||
*err_msg = "Error parsing hostname";
|
||||
tal_free(tmpctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user