|
| 1 | +/* Tool we can use to convert our testing gossip_store files */ |
| 2 | +#include "config.h" |
| 3 | +#include <ccan/crc32c/crc32c.h> |
| 4 | +#include <ccan/err/err.h> |
| 5 | +#include <ccan/opt/opt.h> |
| 6 | +#include <ccan/read_write_all/read_write_all.h> |
| 7 | +#include <common/gossip_store.h> |
| 8 | +#include <fcntl.h> |
| 9 | +#include <gossipd/gossip_store_wiregen.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <wire/peer_wire.h> |
| 13 | + |
| 14 | +/* Current versions we support */ |
| 15 | +#define GSTORE_MAJOR 0 |
| 16 | +#define GSTORE_MINOR 15 |
| 17 | + |
| 18 | +/* Obsolete ZOMBIE bit */ |
| 19 | +#define GOSSIP_STORE_ZOMBIE_BIT_V13 0x1000U |
| 20 | + |
| 21 | +static bool upgrade_field(u8 oldversion, |
| 22 | + be16 *hdr_flags, |
| 23 | + u8 **msg) |
| 24 | +{ |
| 25 | + int type = fromwire_peektype(*msg); |
| 26 | + |
| 27 | + switch (oldversion) { |
| 28 | + case 10: |
| 29 | + /* Remove old channel_update with no htlc_maximum_msat */ |
| 30 | + if (type == WIRE_CHANNEL_UPDATE |
| 31 | + && tal_bytelen(*msg) == 130) { |
| 32 | + *msg = tal_free(*msg); |
| 33 | + return true; |
| 34 | + } |
| 35 | + /* fall thru */ |
| 36 | + case 11: |
| 37 | + case 12: |
| 38 | + /* Remove private entries */ |
| 39 | + if (type == WIRE_GOSSIP_STORE_PRIVATE_CHANNEL_OBS) { |
| 40 | + *msg = tal_free(*msg); |
| 41 | + return true; |
| 42 | + } else if (type == WIRE_GOSSIP_STORE_PRIVATE_UPDATE_OBS) { |
| 43 | + *msg = tal_free(*msg); |
| 44 | + return true; |
| 45 | + } |
| 46 | + /* fall thru */ |
| 47 | + case 13: |
| 48 | + /* Discard any zombies */ |
| 49 | + if (be16_to_cpu(*hdr_flags) & GOSSIP_STORE_ZOMBIE_BIT_V13) { |
| 50 | + *msg = tal_free(*msg); |
| 51 | + return true; |
| 52 | + } |
| 53 | + case 14: |
| 54 | + /* Add completed field */ |
| 55 | + *hdr_flags |= CPU_TO_BE16(GOSSIP_STORE_COMPLETED_BIT); |
| 56 | + /* fall thru */ |
| 57 | + case 15: |
| 58 | + /* Noop */ |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | +} |
| 64 | + |
| 65 | +int main(int argc, char *argv[]) |
| 66 | +{ |
| 67 | + u8 oldversion, version; |
| 68 | + struct gossip_hdr hdr; |
| 69 | + |
| 70 | + setup_locale(); |
| 71 | + if (!read_all(STDIN_FILENO, &oldversion, sizeof(oldversion))) |
| 72 | + errx(1, "Empty file"); |
| 73 | + |
| 74 | + if (GOSSIP_STORE_MAJOR_VERSION(oldversion) != GSTORE_MAJOR) |
| 75 | + errx(1, "Unsupported major gossip_version %u (expected %u)", |
| 76 | + GOSSIP_STORE_MAJOR_VERSION(oldversion), GSTORE_MAJOR); |
| 77 | + |
| 78 | + version = ((GSTORE_MAJOR << 5) | GSTORE_MINOR); |
| 79 | + if (!write_all(STDOUT_FILENO, &version, sizeof(version))) |
| 80 | + err(1, "Write error"); |
| 81 | + |
| 82 | + while (read_all(STDIN_FILENO, &hdr, sizeof(hdr))) { |
| 83 | + u8 *msg; |
| 84 | + msg = tal_arr(NULL, u8, be16_to_cpu(hdr.len)); |
| 85 | + if (!read_all(STDIN_FILENO, msg, tal_bytelen(msg))) |
| 86 | + err(1, "truncated file"); |
| 87 | + if (!upgrade_field(oldversion, &hdr.flags, &msg)) |
| 88 | + errx(1, "Cannot upgrade from version %u", oldversion); |
| 89 | + if (msg) { |
| 90 | + if (!write_all(STDOUT_FILENO, &hdr, sizeof(hdr)) |
| 91 | + || !write_all(STDOUT_FILENO, msg, tal_bytelen(msg))) |
| 92 | + err(1, "Write error"); |
| 93 | + tal_free(msg); |
| 94 | + } |
| 95 | + } |
| 96 | + return 0; |
| 97 | +} |
0 commit comments