Browse Source

common: move gossip_store read routine where subdaemons can access it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
htlc_accepted_hook
Rusty Russell 6 years ago
parent
commit
0e37ac2433
  1. 1
      common/Makefile
  2. 41
      common/gossip_store.c
  3. 19
      common/gossip_store.h
  4. 3
      devtools/create-gossipstore.c
  5. 2
      devtools/dump-gossipstore.c
  6. 1
      gossipd/Makefile
  7. 34
      gossipd/gossip_store.c
  8. 1
      gossipd/gossip_store.h
  9. 3
      gossipd/test/run-bench-find_route.c
  10. 3
      gossipd/test/run-find_route-specific.c
  11. 3
      gossipd/test/run-find_route.c
  12. 3
      gossipd/test/run-overlong.c

1
common/Makefile

@ -18,6 +18,7 @@ COMMON_SRC_NOGEN := \
common/dev_disconnect.c \ common/dev_disconnect.c \
common/features.c \ common/features.c \
common/funding_tx.c \ common/funding_tx.c \
common/gossip_store.c \
common/hash_u5.c \ common/hash_u5.c \
common/htlc_state.c \ common/htlc_state.c \
common/htlc_tx.c \ common/htlc_tx.c \

41
common/gossip_store.c

@ -0,0 +1,41 @@
#include <ccan/crc/crc.h>
#include <ccan/endian/endian.h>
#include <common/gossip_store.h>
#include <common/status.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>
u8 *gossip_store_read(const tal_t *ctx, int gossip_store_fd, u64 offset)
{
beint32_t hdr[2];
u32 msglen, checksum;
u8 *msg;
if (offset == 0)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: can't access offset %"PRIu64,
offset);
if (pread(gossip_store_fd, hdr, sizeof(hdr), offset) != sizeof(hdr)) {
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: can't read hdr offset %"PRIu64
": %s",
offset, strerror(errno));
}
msglen = be32_to_cpu(hdr[0]);
checksum = be32_to_cpu(hdr[1]);
msg = tal_arr(ctx, u8, msglen);
if (pread(gossip_store_fd, msg, msglen, offset + sizeof(hdr)) != msglen)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: can't read len %u offset %"PRIu64,
msglen, offset);
if (checksum != crc32c(0, msg, msglen))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: bad checksum offset %"PRIu64,
offset);
return msg;
}

19
common/gossip_store.h

@ -0,0 +1,19 @@
#ifndef LIGHTNING_COMMON_GOSSIP_STORE_H
#define LIGHTNING_COMMON_GOSSIP_STORE_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
/**
* gossip_store -- On-disk storage related information
*/
#define GOSSIP_STORE_VERSION 4
/**
* Direct store accessor: loads gossip msg from store.
*
* Doesn't return; status_failed() on error.
*/
u8 *gossip_store_read(const tal_t *ctx, int gossip_store_fd, u64 offset);
#endif /* LIGHTNING_COMMON_GOSSIP_STORE_H */

3
devtools/create-gossipstore.c

@ -4,11 +4,12 @@
#include <ccan/opt/opt.h> #include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h> #include <ccan/read_write_all/read_write_all.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/gossip_store.h>
#include <common/node_id.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <common/utils.h> #include <common/utils.h>
#include <fcntl.h> #include <fcntl.h>
#include <gossipd/gen_gossip_store.h> #include <gossipd/gen_gossip_store.h>
#include <gossipd/gossip_store.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

2
devtools/dump-gossipstore.c

@ -3,9 +3,9 @@
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <common/utils.h> #include <common/utils.h>
#include <fcntl.h> #include <fcntl.h>
#include <common/gossip_store.h>
#include <gossipd/gen_gossip_peerd_wire.h> #include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_store.h> #include <gossipd/gen_gossip_store.h>
#include <gossipd/gossip_store.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>

1
gossipd/Makefile

@ -50,6 +50,7 @@ GOSSIPD_COMMON_OBJS := \
common/dev_disconnect.o \ common/dev_disconnect.o \
common/features.o \ common/features.o \
common/gen_status_wire.o \ common/gen_status_wire.o \
common/gossip_store.o \
common/key_derive.o \ common/key_derive.o \
common/memleak.o \ common/memleak.o \
common/msg_queue.o \ common/msg_queue.o \

34
gossipd/gossip_store.c

@ -3,6 +3,7 @@
#include <ccan/crc/crc.h> #include <ccan/crc/crc.h>
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
#include <ccan/read_write_all/read_write_all.h> #include <ccan/read_write_all/read_write_all.h>
#include <common/gossip_store.h>
#include <common/status.h> #include <common/status.h>
#include <common/utils.h> #include <common/utils.h>
#include <errno.h> #include <errno.h>
@ -531,38 +532,7 @@ const u8 *gossip_store_get(const tal_t *ctx,
struct gossip_store *gs, struct gossip_store *gs,
u64 offset) u64 offset)
{ {
beint32_t hdr[2]; return gossip_store_read(ctx, gs->fd, offset);
u32 msglen, checksum;
u8 *msg;
if (offset == 0 || offset > gs->len)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: can't access offset %"PRIu64
", store len %"PRIu64,
offset, gs->len);
if (pread(gs->fd, hdr, sizeof(hdr), offset) != sizeof(hdr)) {
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: can't read hdr offset %"PRIu64
", store len %"PRIu64": %s",
offset, gs->len, strerror(errno));
}
msglen = be32_to_cpu(hdr[0]);
checksum = be32_to_cpu(hdr[1]);
msg = tal_arr(ctx, u8, msglen);
if (pread(gs->fd, msg, msglen, offset + sizeof(hdr)) != msglen)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: can't read len %u offset %"PRIu64
", store len %"PRIu64,
msglen, offset, gs->len);
if (checksum != crc32c(0, msg, msglen))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"gossip_store: bad checksum offset %"PRIu64
", store len %"PRIu64,
offset, gs->len);
return msg;
} }
int gossip_store_readonly_fd(struct gossip_store *gs) int gossip_store_readonly_fd(struct gossip_store *gs)

1
gossipd/gossip_store.h

@ -11,7 +11,6 @@
/** /**
* gossip_store -- On-disk storage related information * gossip_store -- On-disk storage related information
*/ */
#define GOSSIP_STORE_VERSION 4
struct broadcast_state; struct broadcast_state;
struct gossip_store; struct gossip_store;

3
gossipd/test/run-bench-find_route.c

@ -69,6 +69,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_wireaddr */ /* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
/* Generated stub for gossip_store_read */
u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED)
{ fprintf(stderr, "gossip_store_read called!\n"); abort(); }
/* Generated stub for onion_type_name */ /* Generated stub for onion_type_name */
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }

3
gossipd/test/run-find_route-specific.c

@ -58,6 +58,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_wireaddr */ /* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
/* Generated stub for gossip_store_read */
u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED)
{ fprintf(stderr, "gossip_store_read called!\n"); abort(); }
/* Generated stub for onion_type_name */ /* Generated stub for onion_type_name */
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }

3
gossipd/test/run-find_route.c

@ -56,6 +56,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_wireaddr */ /* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
/* Generated stub for gossip_store_read */
u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED)
{ fprintf(stderr, "gossip_store_read called!\n"); abort(); }
/* Generated stub for onion_type_name */ /* Generated stub for onion_type_name */
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }

3
gossipd/test/run-overlong.c

@ -56,6 +56,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_wireaddr */ /* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED) bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); } { fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
/* Generated stub for gossip_store_read */
u8 *gossip_store_read(const tal_t *ctx UNNEEDED, int gossip_store_fd UNNEEDED, u64 offset UNNEEDED)
{ fprintf(stderr, "gossip_store_read called!\n"); abort(); }
/* Generated stub for onion_type_name */ /* Generated stub for onion_type_name */
const char *onion_type_name(int e UNNEEDED) const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); } { fprintf(stderr, "onion_type_name called!\n"); abort(); }

Loading…
Cancel
Save