Keep correct ordered hash in txn->hash[1] for use internally while remaining ABI-safe

This commit is contained in:
Luke Dashjr
2014-09-02 16:53:02 +00:00
parent e86fe28fa0
commit 62e2f6178b
4 changed files with 29 additions and 20 deletions

View File

@@ -132,14 +132,17 @@ bool blkmk_hash_transactions(blktemplate_t * const tmpl)
for (unsigned long i = 0; i < tmpl->txncount; ++i)
{
struct blktxn_t * const txn = &tmpl->txns[i];
if (txn->hash_)
if (txn->hash)
continue;
txn->hash_ = malloc(sizeof(*txn->hash_));
if (!dblsha256(txn->hash_, txn->data, txn->datasz))
txn->hash = malloc(sizeof(*txn->hash) * 2);
if (!dblsha256(&txn->hash[1], txn->data, txn->datasz))
{
free(txn->hash_);
free(txn->hash);
txn->hash = NULL;
return false;
}
else
blkmk_flip(&txn->hash[0], &txn->hash[1], sizeof(*txn->hash));
}
return true;
}
@@ -170,7 +173,7 @@ bool blkmk_build_merkle_branches(blktemplate_t * const tmpl)
unsigned char hashes[(hashcount + 1) * 32];
for (i = 0; i < tmpl->txncount; ++i)
memcpy(&hashes[0x20 * (i + 1)], tmpl->txns[i].hash_, 0x20);
memcpy(&hashes[0x20 * (i + 1)], &tmpl->txns[i].hash[1], 0x20);
for (i = 0; i < branchcount; ++i)
{

View File

@@ -146,12 +146,14 @@ const char *parse_txn(struct blktxn_t *txn, json_t *txnj) {
if ((vv = json_object_get(txnj, "hash")) && json_is_string(vv))
{
hexdata = json_string_value(vv);
txn->hash = malloc(sizeof(*txn->hash));
if (!my_hex2bin(*txn->hash, hexdata, sizeof(*txn->hash)))
txn->hash = malloc(sizeof(*txn->hash) * 2);
if (!my_hex2bin(txn->hash, hexdata, sizeof(*txn->hash)))
{
free(txn->hash);
txn->hash = NULL;
}
else
blkmk_flip(&txn->hash[1], &txn->hash[0], sizeof(*txn->hash));
}
// TODO: dependcount/depends, fee, required, sigops
@@ -159,19 +161,7 @@ const char *parse_txn(struct blktxn_t *txn, json_t *txnj) {
return NULL;
}
static
void my_flip(void *data, size_t datasz) {
char *cdata = (char*)data;
--datasz;
size_t hds = datasz / 2;
for (size_t i = 0; i <= hds; ++i)
{
int altp = datasz - i;
char c = cdata[i];
cdata[i] = cdata[altp];
cdata[altp] = c;
}
}
#define my_flip(data, datasz) blkmk_flip(data, data, datasz)
const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t time_rcvd) {
if (tmpl->version)

View File

@@ -23,6 +23,7 @@ typedef uint32_t blknonce_t;
struct blktxn_t {
unsigned char *data;
size_t datasz;
// NOTE: The byte order of hash is backward
txnhash_t *hash;
signed long dependcount;

View File

@@ -30,4 +30,19 @@ int blkmk_flsl(unsigned long n)
return i;
}
static inline
void blkmk_flip(void * const out, void * const data, size_t datasz) {
char * const cout = out;
char * const cdata = data;
--datasz;
size_t hds = datasz / 2;
for (size_t i = 0; i <= hds; ++i)
{
int altp = datasz - i;
char c = cdata[i];
cout[i] = cdata[altp];
cout[altp] = c;
}
}
#endif