Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Convert GArray use to pointer arrays (parr)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Garzik committed May 4, 2015
1 parent ae4dd25 commit 691534e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
9 changes: 4 additions & 5 deletions include/ccoin/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <glib.h>
#include <ccoin/buffer.h>
#include <ccoin/buint.h>
#include <ccoin/coredefs.h>
Expand Down Expand Up @@ -231,12 +230,12 @@ extern void bp_block_free(struct bp_block *block);
extern void bp_block_vtx_free(struct bp_block *block);
extern void bp_block_calc_sha256(struct bp_block *block);
extern void bp_block_merkle(bu256_t *vo, const struct bp_block *block);
extern GArray *bp_block_merkle_tree(const struct bp_block *block);
extern GArray *bp_block_merkle_branch(const struct bp_block *block,
const GArray *mrktree,
extern parr *bp_block_merkle_tree(const struct bp_block *block);
extern parr *bp_block_merkle_branch(const struct bp_block *block,
const parr *mrktree,
unsigned int txidx);
extern void bp_check_merkle_branch(bu256_t *hash, const bu256_t *txhash_in,
const GArray *mrkbranch, unsigned int txidx);
const parr *mrkbranch, unsigned int txidx);
extern bool bp_block_valid(struct bp_block *block);
extern unsigned int bp_block_ser_size(const struct bp_block *block);
extern void bp_block_free_cb(void *data);
Expand Down
31 changes: 16 additions & 15 deletions lib/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <openssl/bn.h>
#include <ccoin/core.h>
#include <ccoin/util.h>
#include <ccoin/parr.h>
#include <ccoin/coredefs.h>
#include <ccoin/serialize.h>

Expand Down Expand Up @@ -83,12 +84,12 @@ bool bp_tx_valid(const struct bp_tx *tx)
return true;
}

GArray *bp_block_merkle_tree(const struct bp_block *block)
parr *bp_block_merkle_tree(const struct bp_block *block)
{
if (!block->vtx || !block->vtx->len)
return NULL;

GArray *arr = g_array_new(FALSE, TRUE, sizeof(bu256_t));
parr *arr = parr_new(0, bu256_free);

unsigned int i;
for (i = 0; i < block->vtx->len; i++) {
Expand All @@ -97,7 +98,7 @@ GArray *bp_block_merkle_tree(const struct bp_block *block)
tx = parr_idx(block->vtx, i);
bp_tx_calc_sha256(tx);

g_array_append_val(arr, tx->sha256);
parr_add(arr, bu256_new(&tx->sha256));
}

unsigned int j = 0, nSize;
Expand All @@ -106,10 +107,10 @@ GArray *bp_block_merkle_tree(const struct bp_block *block)
unsigned int i2 = MIN(i+1, nSize-1);
bu256_t hash;
bu_Hash_((unsigned char *) &hash,
&g_array_index(arr, bu256_t, j+i), sizeof(bu256_t),
&g_array_index(arr, bu256_t, j+i2),sizeof(bu256_t));
parr_idx(arr, j+i), sizeof(bu256_t),
parr_idx(arr, j+i2),sizeof(bu256_t));

g_array_append_val(arr, hash);
parr_add(arr, bu256_new(&hash));
}

j += nSize;
Expand All @@ -125,28 +126,28 @@ void bp_block_merkle(bu256_t *vo, const struct bp_block *block)
if (!block->vtx || !block->vtx->len)
return;

GArray *arr = bp_block_merkle_tree(block);
parr *arr = bp_block_merkle_tree(block);
if (!arr)
return;

*vo = g_array_index(arr, bu256_t, arr->len - 1);
bu256_copy(vo, parr_idx(arr, arr->len - 1));

g_array_free(arr, TRUE);
parr_free(arr, true);
}

GArray *bp_block_merkle_branch(const struct bp_block *block,
const GArray *mrktree,
parr *bp_block_merkle_branch(const struct bp_block *block,
const parr *mrktree,
unsigned int txidx)
{
if (!block || !block->vtx || !mrktree || (txidx >= block->vtx->len))
return NULL;

GArray *ret = g_array_new(FALSE, TRUE, sizeof(bu256_t));
parr *ret = parr_new(0, bu256_free);

unsigned int j = 0, nSize;
for (nSize = block->vtx->len; nSize > 1; nSize = (nSize + 1) / 2) {
unsigned int i = MIN(txidx ^ 1, nSize - 1);
g_array_append_val(ret, g_array_index(mrktree, bu256_t, j+i));
parr_add(ret, bu256_new(parr_idx(mrktree, j+i)));
txidx >>= 1;
j += nSize;
}
Expand All @@ -155,13 +156,13 @@ GArray *bp_block_merkle_branch(const struct bp_block *block,
}

void bp_check_merkle_branch(bu256_t *hash, const bu256_t *txhash_in,
const GArray *mrkbranch, unsigned int txidx)
const parr *mrkbranch, unsigned int txidx)
{
bu256_copy(hash, txhash_in);

unsigned int i;
for (i = 0; i < mrkbranch->len; i++) {
const bu256_t *otherside = &g_array_index(mrkbranch, bu256_t,i);
const bu256_t *otherside = parr_idx(mrkbranch, i);
if (txidx & 1)
bu_Hash_((unsigned char *)hash,
otherside, sizeof(bu256_t),
Expand Down
9 changes: 5 additions & 4 deletions test/wallet-basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <jansson.h>
#include <ccoin/core.h>
#include <ccoin/util.h>
#include <ccoin/parr.h>
#include <ccoin/buffer.h>
#include <ccoin/buint.h>
#include <ccoin/hexcode.h>
Expand Down Expand Up @@ -124,9 +125,9 @@ static void runtest(const char *json_base_fn, const char *ser_in_fn,
assert(BN_cmp(&tmp_mask, &match->mask) == 0);

/* build merkle tree, tx's branch */
GArray *mtree = bp_block_merkle_tree(&block_in);
parr *mtree = bp_block_merkle_tree(&block_in);
assert(mtree != NULL);
GArray *mbranch = bp_block_merkle_branch(&block_in, mtree, match->n);
parr *mbranch = bp_block_merkle_branch(&block_in, mtree, match->n);
assert(mbranch != NULL);

/* verify merkle branch for tx matches expected */
Expand All @@ -135,8 +136,8 @@ static void runtest(const char *json_base_fn, const char *ser_in_fn,
assert(bu256_equal(&mrk_check, &block_in.hashMerkleRoot) == true);

/* release resources */
g_array_free(mtree, TRUE);
g_array_free(mbranch, TRUE);
parr_free(mtree, true);
parr_free(mbranch, true);
BN_clear_free(&tmp_mask);
parr_free(matches, true);
bpks_free(&ks);
Expand Down

0 comments on commit 691534e

Please sign in to comment.