Skip to content

Commit

Permalink
Use ccan/structeq.
Browse files Browse the repository at this point in the history
I caught myself earlier using sizeof(ptr) instead of sizeof(*ptr)...

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jun 25, 2014
1 parent 5537ea5 commit b04dc9f
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
3 changes: 2 additions & 1 deletion block.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "block.h"
#include "chain.h"
#include "protocol.h"
Expand Down Expand Up @@ -103,7 +104,7 @@ struct block *block_find_any(struct state *state,
/* Search recent blocks first. */
for (i = n - 1; i >= 0; i--) {
list_for_each(state->block_depth[i], b, list) {
if (memcmp(b->sha.sha, sha->sha, sizeof(sha->sha)) == 0)
if (structeq(&b->sha, sha))
return b;
}
}
Expand Down
5 changes: 3 additions & 2 deletions chain.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "chain.h"
#include "block.h"
#include "peer.h"
Expand Down Expand Up @@ -103,8 +104,8 @@ void check_chains(const struct state *state)
if (n == 0)
assert(i == genesis_block(state));
else {
assert(memcmp(&i->hdr->prev_block, &i->prev->sha,
sizeof(i->prev->sha)) == 0);
assert(structeq(&i->hdr->prev_block,
&i->prev->sha));
if (i->prev->complaint)
assert(i->complaint);
}
Expand Down
10 changes: 4 additions & 6 deletions check_block.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "check_block.h"
#include "version.h"
#include "overflows.h"
Expand Down Expand Up @@ -123,8 +124,7 @@ bool shard_belongs_in_block(const struct block *block,
== block->shard_nums[shard->shardnum]);
merkle_txs(NULL, 0, shard->txp_or_hash, shard->u, 0,
block->shard_nums[shard->shardnum], &merkle);
return memcmp(block->merkles[shard->shardnum].sha, merkle.sha,
sizeof(merkle.sha)) == 0;
return structeq(&block->merkles[shard->shardnum], &merkle);
}

static u32 get_shard_start(const struct block *block,
Expand Down Expand Up @@ -253,8 +253,7 @@ static void copy_old_txs(struct state *state,
for (i = 0; i < num; i++) {
if (!shard_is_tx(old, i)) {
/* Both hashes must be identical. */
assert(memcmp(old->u[i].hash, new->u[i].hash,
sizeof(*new->u[i].hash)) == 0);
assert(structeq(&old->u[i].hash, &new->u[i].hash));
continue;
}
if (!tx_for(old, i))
Expand Down Expand Up @@ -313,8 +312,7 @@ void put_tx_in_block(struct state *state,
struct protocol_net_txrefhash hashes;
hash_tx(txp->tx, &hashes.txhash);
hash_refs(refs_for(*txp), num_inputs(txp->tx), &hashes.refhash);
assert(memcmp(shard->u[txoff].hash, &hashes, sizeof(hashes))
== 0);
assert(structeq(shard->u[txoff].hash, &hashes));
}

/* Now it's a transaction. */
Expand Down
3 changes: 2 additions & 1 deletion check_tx.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "check_tx.h"
#include "tx.h"
#include "block.h"
Expand Down Expand Up @@ -173,7 +174,7 @@ check_tx_normal_inputs(struct state *state,
}

/* Check it was to this address. */
if (memcmp(&my_addr, &addr, sizeof(addr)) != 0) {
if (!structeq(&my_addr, &addr)) {
*bad_input_num = i;
log_debug(state->log, "Address mismatch against output %i of ", le16_to_cpu(inp[i].output));
log_add_struct(state->log, union protocol_tx,
Expand Down
9 changes: 5 additions & 4 deletions peer_cache.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "peer_cache.h"
#include "peer.h"
#include "protocol_net.h"
Expand Down Expand Up @@ -167,7 +168,7 @@ static bool peer_already(struct state *state, const struct protocol_net_address
struct peer *p;

list_for_each(&state->peers, p, list)
if (memcmp(&p->you, a, sizeof(*a)) == 0)
if (structeq(&p->you, a))
return true;
return false;
}
Expand Down Expand Up @@ -219,7 +220,7 @@ void peer_cache_update(struct state *state,
struct peer_hash_entry *e = peer_hash_entry(state->peer_cache, addr);

/* Don't update if not in cache. */
if (memcmp(&e->addr, addr, sizeof(*addr)) != 0) {
if (!structeq(&e->addr, addr)) {
log_debug(state->log, "Can't update address not in peer_cache ");
log_add_struct(state->log, struct protocol_net_address, addr);
return;
Expand All @@ -245,7 +246,7 @@ void peer_cache_add(struct state *state,
{
struct peer_hash_entry *e = peer_hash_entry(state->peer_cache, addr);

if (memcmp(&e->addr, addr, sizeof(*addr)) == 0) {
if (structeq(&e->addr, addr)) {
log_debug(state->log, "peer_cache adding repeat for ");
log_add_struct(state->log, struct protocol_net_address, addr);
return;
Expand Down Expand Up @@ -277,7 +278,7 @@ void peer_cache_del(struct state *state,
{
struct peer_hash_entry *e = peer_hash_entry(state->peer_cache, addr);

if (memcmp(&e->addr, addr, sizeof(*addr)) == 0) {
if (structeq(&e->addr, addr)) {
log_debug(state->log, "peer_cache deleting ");
log_add_struct(state->log, struct protocol_net_address, addr);
memset(e, 0, sizeof(*e));
Expand Down
5 changes: 3 additions & 2 deletions pending.c
@@ -1,4 +1,5 @@
#include <ccan/asort/asort.h>
#include <ccan/structeq/structeq.h>
#include <ccan/array_size/array_size.h>
#include "pending.h"
#include "prev_merkles.h"
Expand Down Expand Up @@ -267,7 +268,7 @@ find_pending_tx_with_ref(const tal_t *ctx,

/* FIXME: Cache sha of tx in pending? */
hash_tx(pend[i]->tx, &sha);
if (memcmp(&hash->txhash, &sha, sizeof(sha)) != 0)
if (!structeq(&hash->txhash, &sha))
continue;

/* FIXME: If peer->state->longest_knowns[0]->prev ==
Expand All @@ -279,7 +280,7 @@ find_pending_tx_with_ref(const tal_t *ctx,
continue;

hash_refs(refs, tal_count(refs), &sha);
if (memcmp(&hash->refhash, &sha, sizeof(sha)) != 0) {
if (!structeq(&hash->refhash, &sha)) {
tal_free(refs);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions proof.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "proof.h"
#include "merkle_txs.h"
#include "block.h"
Expand Down Expand Up @@ -73,6 +74,5 @@ bool check_proof(const struct protocol_proof *proof,

proof_merkles_to(tx, txoff, proof, &merkle);

return memcmp(b->merkles[shardnum].sha, merkle.sha, sizeof(merkle.sha))
== 0;
return structeq(&b->merkles[shardnum], &merkle);
}
5 changes: 3 additions & 2 deletions todo.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "todo.h"
#include "state.h"
#include "protocol_net.h"
Expand Down Expand Up @@ -61,7 +62,7 @@ static struct todo_request *find_todo(struct state *state,
continue;

get_todo_ptrs(state, i, &i_sha, &i_shardnum, &i_txoff);
if (memcmp(i_sha, blk, sizeof(*blk)) != 0)
if (!structeq(i_sha, blk))
continue;
if (i_shardnum && le16_to_cpu(*i_shardnum) != shardnum)
continue;
Expand Down Expand Up @@ -287,7 +288,7 @@ void todo_forget_about_block(struct state *state,
u8 *i_txoff;

get_todo_ptrs(state, i, &i_sha, &i_shardnum, &i_txoff);
if (memcmp(i_sha, block, sizeof(*i_sha)) != 0)
if (!structeq(i_sha, block))
continue;

list_del_from(&state->todo, &i->list);
Expand Down
4 changes: 2 additions & 2 deletions welcome.c
@@ -1,3 +1,4 @@
#include <ccan/structeq/structeq.h>
#include "welcome.h"
#include "state.h"
#include "version.h"
Expand Down Expand Up @@ -134,8 +135,7 @@ enum protocol_ecode check_welcome(const struct state *state,
return PROTOCOL_ECODE_INVALID_LEN;

/* We must agree on genesis block. */
if (memcmp(&(*blocks)[le16_to_cpu(w->num_blocks) - 1],
&genesis->sha, sizeof(genesis->sha)) != 0)
if (!structeq(&(*blocks)[le16_to_cpu(w->num_blocks)-1], &genesis->sha))
return PROTOCOL_ECODE_WRONG_GENESIS;

return PROTOCOL_ECODE_NONE;
Expand Down

0 comments on commit b04dc9f

Please sign in to comment.