MCPcopy Create free account
hub / github.com/ElementsProject/lightning / reopen_store

Function reopen_store

common/gossmap.c:735–801  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

733}
734
735static bool reopen_store(struct gossmap *map, u64 ended_off)
736{
737 u64 equivalent_off;
738 u8 verbyte;
739 u8 expected_uuid[32], uuid[32];
740 struct gossip_hdr ghdr;
741 bool changed;
742
743 /* This tells us the equivalent offset in new map */
744 equivalent_off = map_be64(map, ended_off + 2);
745 map_copy(map, ended_off + 2 + 8, expected_uuid, sizeof(expected_uuid));
746 close(map->fd);
747
748 map->fd = open(map->fname, O_RDONLY);
749 if (map->fd < 0)
750 err(1, "Failed to reopen %s", map->fname);
751
752 /* If mmap isn't disabled, we try to mmap if we can. */
753 map->map_size = lseek(map->fd, 0, SEEK_END);
754 if (map->mmap) {
755 map->mmap = mmap(NULL, map->map_size, PROT_READ, MAP_SHARED, map->fd, 0);
756 if (map->mmap == MAP_FAILED)
757 map->mmap = NULL;
758 }
759
760 if (map->map_size < 1 + sizeof(ghdr) + 2 + sizeof(uuid))
761 errx(1, "Truncated gossip_store len %"PRIu64, map->map_size);
762
763 /* version then ghdr then uuid. */
764 verbyte = map_u8(map, 0);
765 if (GOSSIP_STORE_MAJOR_VERSION(verbyte) != 0 || GOSSIP_STORE_MINOR_VERSION(verbyte) < 16)
766 errx(1, "Bad gossip_store version %u", verbyte);
767
768 if (map_be16(map, 1 + sizeof(struct gossip_hdr)) != WIRE_GOSSIP_STORE_UUID)
769 errx(1, "First gossip_store record is not uuid?");
770 map_copy(map, 1 + sizeof(struct gossip_hdr) + 2, uuid, sizeof(uuid));
771
772 /* FIXME: we can skip if uuid is as expected, but we'd have to re-calc all
773 * our offsets anyway. It's not worth it, given how fast we are. */
774 if (memcmp(uuid, expected_uuid, sizeof(uuid)) == 0) {
775 map->logcb(map->cbarg, LOG_DBG,
776 "Reopened gossip_store, reduced to offset %"PRIu64,
777 equivalent_off);
778 } else {
779 /* Start from scratch */
780 map->logcb(map->cbarg, LOG_INFORM,
781 "Reopened gossip_store, but we missed some (%s vs %s)",
782 tal_hexstr(tmpctx, uuid, sizeof(uuid)),
783 tal_hexstr(tmpctx, expected_uuid, sizeof(expected_uuid)));
784 }
785
786 tal_free(map->channels);
787 tal_free(map->nodes);
788 tal_free(map->chan_arr);
789 tal_free(map->node_arr);
790 init_map_structs(map);
791 map->map_end = 1;
792 map->generation++;

Callers 1

map_catchupFunction · 0.85

Calls 10

map_be64Function · 0.85
map_copyFunction · 0.85
errFunction · 0.85
errxFunction · 0.85
map_u8Function · 0.85
map_be16Function · 0.85
tal_hexstrFunction · 0.85
tal_freeFunction · 0.85
init_map_structsFunction · 0.85
map_catchupFunction · 0.85

Tested by

no test coverage detected