Repository: valkey-io/valkey
File: src/cluster_legacy.h (C/C++)

Code before:
static_assert(offsetof(clusterMsgLight, data) == 16, "unexpected field offset");

#define CLUSTERMSG_LIGHT_MIN_LEN (sizeof(clusterMsgLight) - sizeof(union clusterMsgData))

typedef struct {
    char sig[4];       /* Signature "RCmb" (Cluster message bus). */
    uint32_t totlen;   /* Total length of this message */
    uint16_t ver;      /* Protocol version, currently set to CLUSTER_PROTO_VER. */
    uint16_t notused1; /* full: port, light: notused1 */
    uint16_t type;     /* Message type */
    uint16_t notused2; /* full: count, light: notused2 */
} clusterMsgUnknown;

static_assert(offsetof(clusterMsgUnknown, sig) == offsetof(clusterMsg, sig), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, totlen) == offsetof(clusterMsg, totlen), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, ver) == offsetof(clusterMsg, ver), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, type) == offsetof(clusterMsg, type), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, notused1) == offsetof(clusterMsg, port), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, notused2) == offsetof(clusterMsg, count), "unexpected field offset");

static_assert(offsetof(clusterMsgUnknown, sig) == offsetof(clusterMsgLight, sig), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, totlen) == offsetof(clusterMsgLight, totlen), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, ver) == offsetof(clusterMsgLight, ver), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, type) == offsetof(clusterMsgLight, type), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, notused1) == offsetof(clusterMsgLight, notused1), "unexpected field offset");
static_assert(offsetof(clusterMsgUnknown, notused2) == offsetof(clusterMsgLight, notused2), "unexpected field offset");

struct _clusterNode {
    mstime_t ctime;                         /* Node object creation time. */
    char name[CLUSTER_NAMELEN];             /* Node name, hex string, sha1-size */
    char shard_id[CLUSTER_NAMELEN];         /* shard id, hex string, sha1-size */
    int flags;                              /* CLUSTER_NODE_... */
    uint64_t configEpoch;                   /* Last configEpoch observed for this node */
    unsigned char slots[CLUSTER_SLOTS / 8]; /* slots handled by this node */
    uint16_t *slot_info_pairs;              /* Slots info represented as (start/end) pair (consecutive index). */
    int slot_info_pairs_count;              /* Used number of slots in slot_info_pairs */
    int numslots;                           /* Number of slots handled by this node */
    int num_replicas;                       /* Number of replica nodes, if this is a primary */
    clusterNode **replicas;                 /* pointers to replica nodes */
    clusterNode *replicaof;                 /* pointer to the primary node. Note that it
                                             may be NULL even if the node is a replica
                                             if we don't have the primary node in our
                                             tables. */
    unsigned long long last_in_ping_gossip; /* The number of the last carried in the ping gossip section */
    mstime_t ping_sent;                     /* Unix time we sent latest ping */
    mstime_t pong_received;                 /* Unix time we received the pong */
    mstime_t data_received;                 /* Unix time we received any data */
    mstime_t meet_sent;                     /* Unix time we sent latest meet packet */
    mstime_t fail_time;                     /* Unix time when FAIL flag was set */
    mstime_t orphaned_time;                 /* Starting time of orphaned primary condition */
    mstime_t outbound_link_attempt_time;    /* Unix time we last tried to establish an outgoing link */

Review comment (suggestion):
```suggestion
```
We already assert all the fields from msg = msgLight, so just asserting msg is equal to the main packet is sufficient.

Code after:
static_assert(offsetof(clusterMsgLight, data) == 16, "unexpected field offset");

#define CLUSTERMSG_LIGHT_MIN_LEN (sizeof(clusterMsgLight) - sizeof(union clusterMsgData))

typedef struct {
    char sig[4];       /* Signature "RCmb" (Cluster message bus). */
    uint32_t totlen;   /* Total length of this message */
    uint16_t ver;      /* Protocol version, currently set to CLUSTER_PROTO_VER. */
    uint16_t notused1; /* full: port, light: notused1 */
    uint16_t type;     /* Message type */
    uint16_t notused2; /* full: count, light: notused2 */
} clusterMsgHeader;

static_assert(offsetof(clusterMsgHeader, sig) == offsetof(clusterMsg, sig), "unexpected field offset");
static_assert(offsetof(clusterMsgHeader, totlen) == offsetof(clusterMsg, totlen), "unexpected field offset");
static_assert(offsetof(clusterMsgHeader, ver) == offsetof(clusterMsg, ver), "unexpected field offset");
static_assert(offsetof(clusterMsgHeader, type) == offsetof(clusterMsg, type), "unexpected field offset");
static_assert(offsetof(clusterMsgHeader, notused1) == offsetof(clusterMsg, port), "unexpected field offset");
static_assert(offsetof(clusterMsgHeader, notused2) == offsetof(clusterMsg, count), "unexpected field offset");

struct _clusterNode {
    mstime_t ctime;                         /* Node object creation time. */
    char name[CLUSTER_NAMELEN];             /* Node name, hex string, sha1-size */
    char shard_id[CLUSTER_NAMELEN];         /* shard id, hex string, sha1-size */
    int flags;                              /* CLUSTER_NODE_... */
    uint64_t configEpoch;                   /* Last configEpoch observed for this node */
    unsigned char slots[CLUSTER_SLOTS / 8]; /* slots handled by this node */
    uint16_t *slot_info_pairs;              /* Slots info represented as (start/end) pair (consecutive index). */
    int slot_info_pairs_count;              /* Used number of slots in slot_info_pairs */
    int numslots;                           /* Number of slots handled by this node */
    int num_replicas;                       /* Number of replica nodes, if this is a primary */
    clusterNode **replicas;                 /* pointers to replica nodes */
    clusterNode *replicaof;                 /* pointer to the primary node. Note that it
                                             may be NULL even if the node is a replica
                                             if we don't have the primary node in our
                                             tables. */
    unsigned long long last_in_ping_gossip; /* The number of the last carried in the ping gossip section */
    mstime_t ping_sent;                     /* Unix time we sent latest ping */
    mstime_t pong_received;                 /* Unix time we received the pong */
    mstime_t data_received;                 /* Unix time we received any data */
    mstime_t meet_sent;                     /* Unix time we sent latest meet packet */
    mstime_t fail_time;                     /* Unix time when FAIL flag was set */
    mstime_t orphaned_time;                 /* Starting time of orphaned primary condition */
    mstime_t outbound_link_attempt_time;    /* Unix time we last tried to establish an outgoing link */
    mstime_t inbound_link_freed_time;       /* Last time we freed the inbound link for this node.
                                               If it was never freed, it is the same as ctime */
    long long repl_offset;                  /* Last known repl offset for this node. */
    char ip[NET_IP_STR_LEN];                /* Latest known IP address of this node */
    sds announce_client_ipv4;               /* IPv4 for clients only. */
    sds announce_client_ipv6;               /* IPv6 for clients only. */
    sds hostname;                           /* The known hostname for this node */