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

Code before:
    uint32_t totlen; /* Total length of this message */
    uint16_t ver;    /* Protocol version, currently set to CLUSTER_PROTO_VER. */
    uint16_t notused1;
    uint16_t type; /* Message type */
    uint16_t notused2;
    union clusterMsgData data;
} clusterMsgLight;

static_assert(offsetof(clusterMsgLight, sig) == offsetof(clusterMsg, sig), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, totlen) == offsetof(clusterMsg, totlen), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, ver) == offsetof(clusterMsg, ver), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, notused1) == offsetof(clusterMsg, port), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, type) == offsetof(clusterMsg, type), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, notused2) == offsetof(clusterMsg, count), "unexpected field offset");
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 */

Review comment (suggestion):
```suggestion
} clusterMsgHeader;
```
Not a very strong preference, but unknown isn't true, because at this point we do know what type it is (type is filled here) we just haven't parsed it yet to create the right structure. You can tell this was sort of always in the intention since hdr (stands for header) is used in most of the places you are creating this struct.

As a side effect, all the hdr -> hdr_unknown also all go away.

Code after:
    uint32_t totlen; /* Total length of this message */
    uint16_t ver;    /* Protocol version, currently set to CLUSTER_PROTO_VER. */
    uint16_t notused1;
    uint16_t type; /* Message type */
    uint16_t notused2;
    union clusterMsgData data;
} clusterMsgLight;

static_assert(offsetof(clusterMsgLight, sig) == offsetof(clusterMsg, sig), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, totlen) == offsetof(clusterMsg, totlen), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, ver) == offsetof(clusterMsg, ver), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, notused1) == offsetof(clusterMsg, port), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, type) == offsetof(clusterMsg, type), "unexpected field offset");
static_assert(offsetof(clusterMsgLight, notused2) == offsetof(clusterMsg, count), "unexpected field offset");
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 */