Exemplo n.º 1
0
Arquivo: hfile.c Projeto: pipul/lab
block_t *block_load(int32_t fd, int32_t offset, int32_t size)
{
	block_t *l;
	entry_t *o;
	int32_t len;
	int8_t *buffer, *ptr;
	
	if (fd < 0 || offset < 0 || size < 0)
		return(NULL);
	if ((l = block_new()) == NULL)
		return(NULL);
	if ((buffer = malloc(size)) == NULL)
		__ERROR_LOG(B_ERROR);
	lseek(fd,offset,SEEK_SET);
	if (size != read(fd,buffer,size))
		__ERROR_LOG(R_ERROR);

	ptr = buffer;
	l->magic = *(uint64_t *)ptr;
	ptr = ptr + sizeof(uint64_t);
	if (l->magic != crc32_encode(ptr,size - sizeof(uint64_t)))
		__ERROR_LOG(C_ERROR);

	while (ptr - buffer < size) {
		if ((o = entry_new()) == NULL)
			continue;
		len = *(int32_t *)ptr;
		ptr = ptr + sizeof(int32_t);
		o->key = sdsnnew(ptr,len);
		ptr = ptr + len;

		len = *(int32_t *)ptr;
		ptr = ptr + sizeof(int32_t);
		o->value = sdsnnew(ptr,len);
		ptr = ptr + len;
		
		if (o->key && o->value) {
			block_add(l,o);
			continue;
		}
		if (o->key)
			sdsdel(o->key);
		if (o->value)
			sdsdel(o->value);
	}

	free(buffer);
	return(l);

C_ERROR:
R_ERROR:
	free(buffer);
B_ERROR:
	block_free(l);
	return(NULL);
}
Exemplo n.º 2
0
void client_do_submit(YAAMP_CLIENT *client, YAAMP_JOB *job, YAAMP_JOB_VALUES *submitvalues, char *extranonce2, char *ntime, char *nonce)
{
	YAAMP_COIND *coind = job->coind;
	YAAMP_JOB_TEMPLATE *templ = job->templ;

	if(job->block_found) return;
	if(job->deleted) return;

	uint64_t hash_int = get_hash_difficulty(submitvalues->hash_bin);
	uint64_t coin_target = decode_compact(templ->nbits);

	int block_size = YAAMP_SMALLBUFSIZE;
	vector<string>::const_iterator i;

	for(i = templ->txdata.begin(); i != templ->txdata.end(); ++i)
		block_size += strlen((*i).c_str());

	char *block_hex = (char *)malloc(block_size);
	if(!block_hex) return;

	// do aux first
	for(int i=0; i<templ->auxs_size; i++)
	{
		if(!templ->auxs[i]) continue;
		YAAMP_COIND *coind_aux = templ->auxs[i]->coind;

		unsigned char target_aux[1024];
		binlify(target_aux, coind_aux->aux.target);

		uint64_t coin_target_aux = get_hash_difficulty(target_aux);
		if(hash_int <= coin_target_aux)
		{
			memset(block_hex, 0, block_size);

			strcat(block_hex, submitvalues->coinbase);		// parent coinbase
			strcat(block_hex, submitvalues->hash_be);		// parent hash

			////////////////////////////////////////////////// parent merkle steps

			sprintf(block_hex+strlen(block_hex), "%02x", (unsigned char)templ->txsteps.size());

			vector<string>::const_iterator i;
			for(i = templ->txsteps.begin(); i != templ->txsteps.end(); ++i)
				sprintf(block_hex + strlen(block_hex), "%s", (*i).c_str());

			strcat(block_hex, "00000000");

			////////////////////////////////////////////////// auxs merkle steps

			vector<string> lresult = coind_aux_merkle_branch(templ->auxs, templ->auxs_size, coind_aux->aux.index);
			sprintf(block_hex+strlen(block_hex), "%02x", (unsigned char)lresult.size());

			for(i = lresult.begin(); i != lresult.end(); ++i)
				sprintf(block_hex+strlen(block_hex), "%s", (*i).c_str());

			sprintf(block_hex+strlen(block_hex), "%02x000000", (unsigned char)coind_aux->aux.index);

			////////////////////////////////////////////////// parent header

			strcat(block_hex, submitvalues->header_be);

			bool b = coind_submitgetauxblock(coind_aux, coind_aux->aux.hash, block_hex);
			if(b)
			{
				debuglog("*** ACCEPTED %s %d\n", coind_aux->name, coind_aux->height+1);

				block_add(client->userid, coind_aux->id, coind_aux->height, target_to_diff(coin_target_aux),
					target_to_diff(hash_int), coind_aux->aux.hash, "");
			}

			else
				debuglog("%s %d rejected\n", coind_aux->name, coind_aux->height+1);
		}
	}

	if(hash_int <= coin_target)
	{
		memset(block_hex, 0, block_size);
		sprintf(block_hex, "%s%02x%s", submitvalues->header_be, (unsigned char)templ->txcount, submitvalues->coinbase);

		vector<string>::const_iterator i;
		for(i = templ->txdata.begin(); i != templ->txdata.end(); ++i)
			sprintf(block_hex+strlen(block_hex), "%s", (*i).c_str());

		if(coind->txmessage)
			strcat(block_hex, "00");

		bool b = coind_submit(coind, block_hex);
		if(b)
		{
			debuglog("*** ACCEPTED %s %d\n", coind->name, templ->height);
			job->block_found = true;

			char doublehash2[128];
			memset(doublehash2, 0, 128);

			sha256_double_hash_hex((char *)submitvalues->header_bin, doublehash2, strlen(submitvalues->header_be)/2);

			char hash1[1024];
			memset(hash1, 0, 1024);

			string_be(doublehash2, hash1);

			block_add(client->userid, coind->id, templ->height, target_to_diff(coin_target), target_to_diff(hash_int),
				hash1, submitvalues->hash_be);

//			if(!strcmp(coind->symbol, "HAL"))
//			{
//				debuglog("--------------------------------------------------------------\n");
//				debuglog("hash1 %s\n", hash1);
//				debuglog("hash2 %s\n", submitvalues->hash_be);
//			}
		}

		else
			debuglog("%s %d rejected\n", coind->name, templ->height);
	}

	free(block_hex);
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
	block_t *l;
	entry_t *e;
	int8_t buffer[KEY_MAX];
	sds key;
	int32_t count, cost, i;
	int32_t ok_c, err_c;

	if (argc != 2)
		return(-1);
	count = atoi(argv[1]);
	l = block_new();
	

	printf("\nBlock add Test...\n");
	ok_c = err_c = 0;
	cost = time(NULL);
	for (i = 0; i < count; i++) {
		e = entry_new();
		snprintf(buffer,KEY_MAX,"%d",i);
		e->key = sdsnew(buffer);
		snprintf(buffer,KEY_MAX,"key = %d",i);
		e->value = sdsnew(buffer);
		if (0 == block_add(l,e))
			ok_c++;
		else
			err_c++;
	}
	cost = time(NULL) - cost;
	printf("%d add ok and %d error\n",ok_c,err_c);
	printf("total cost : %d\n",cost);

	printf("\nBlock search Test...\n");	
	ok_c = err_c = 0;
	cost = time(NULL);
	for (i = 0; i < count; i++) {
		snprintf(buffer,KEY_MAX,"%d",i);
		key = sdsnew(buffer);
		if ((e = block_loup(l,key)) == NULL)
			err_c++;
		else {
			if (strstr(e->value,"0") != NULL) {
				printf("%s del ok\n",e->value);
				block_del(l,e);;
			}
			ok_c++;
		}
		sdsdel(key);
	}
	cost = time(NULL) - cost;
	printf("%d find ok and %d error\n",ok_c,err_c);
	printf("total cost : %d\n",cost);

	printf("\nBlock research Test...\n");	
	ok_c = err_c = 0;
	cost = time(NULL);
	for (i = 0; i < count; i++) {
		snprintf(buffer,KEY_MAX,"%d",i);
		key = sdsnew(buffer);
		if ((e = block_loup(l,key)) == NULL)
			err_c++;
		else {
			ok_c++;
		}
		sdsdel(key);
	}
	cost = time(NULL) - cost;
	printf("%d find ok and %d error\n",ok_c,err_c);
	printf("total cost : %d\n",cost);



	sleep(10);
	block_destroy(l);
	return(0);
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
	struct state *s;
	struct working_block *w;
	unsigned int i;
	union protocol_tx *t;
	struct protocol_gateway_payment payment;
	struct block *prev, *b;
	struct block_shard *shard;
	u8 *prev_txhashes;
	enum protocol_ecode e;
	struct gen_update update;
	struct protocol_input_ref *refs;
	struct protocol_block_id sha;
	struct protocol_block_id prevs[PROTOCOL_NUM_PREV_IDS];

	/* We need enough of state to use the real init function here. */
	pseudorand_init();
	s = new_state(true);
	check_chains(s, true);

	fake_time = le32_to_cpu(genesis_tlr.timestamp) + 1;

	/* Create a block after that, with a gateway tx in it. */
	prev_txhashes = make_prev_txhashes(s, &genesis, helper_addr(1));

	/* We should need 1 prev_merkle per shard per block. */
	assert(num_prev_txhashes(&genesis) == (1 << genesis.bi.hdr->shard_order));
	assert(tal_count(prev_txhashes) == num_prev_txhashes(&genesis));

	memset(prevs, 0, sizeof(prevs));
	prevs[0] = genesis.sha;
	w = new_working_block(s, 0x1ffffff0,
			      prev_txhashes, tal_count(prev_txhashes),
			      block_height(&genesis.bi) + 1,
			      next_shard_order(&genesis),
			      prevs, helper_addr(1));

	payment.send_amount = cpu_to_le32(1000);
	payment.output_addr = *helper_addr(0);
	t = create_from_gateway_tx(s, helper_gateway_public_key(),
				   1, &payment, false, helper_gateway_key(s));
	/* Gateway txs have empty refs, so this gives 0-len array. */
	refs = create_refs(s, &genesis, t, 1);

	update.shard = shard_of_tx(t, next_shard_order(&genesis));
	update.txoff = 0;
	update.features = 0;
	update.unused = 0;
	hash_tx_and_refs(t, refs, &update.hashes);
	assert(add_tx(w, &update));
	for (i = 0; !solve_block(w); i++);

	e = check_block_header(s, &w->bi, &prev, &sha.sha);
	assert(e == PROTOCOL_ECODE_NONE);
	assert(prev == &genesis);

	b = block_add(s, prev, &sha, &w->bi);

	/* This is a NOOP, so should succeed. */
	assert(check_prev_txhashes(s, b, NULL, NULL));

	/* Put the single tx into the shard. */
	shard = new_block_shard(s, update.shard, 1);
	shard->txcount = 1;
	shard->u[0].txp = txptr_with_ref(shard, t, refs);

	/* This should all be correct. */
	check_block_shard(s, b, shard);
	b->shard[shard->shardnum] = shard;

	/* Should require a prev_merkle per shard for each of 2 prev blocks. */
	assert(num_prev_txhashes(b) == (2 << genesis.bi.hdr->shard_order));
	prev_txhashes = make_prev_txhashes(s, b, helper_addr(1));
	assert(tal_count(prev_txhashes) == num_prev_txhashes(b));

	/* Solve third block. */
	fake_time++;
	prevs[0] = b->sha;
	prevs[1] = genesis.sha;
	w = new_working_block(s, 0x1ffffff0, prev_txhashes, num_prev_txhashes(b),
			      block_height(&b->bi) + 1,
			      next_shard_order(b),
			      prevs, helper_addr(1));
	for (i = 0; !solve_block(w); i++);

	e = check_block_header(s, &w->bi, &prev, &sha.sha);
	assert(e == PROTOCOL_ECODE_NONE);
	assert(prev == b);

	b = block_add(s, prev, &sha, &w->bi);

	/* This should be correct. */
	assert(check_prev_txhashes(s, b, NULL, NULL));

	tal_free(s);
	return 0;
}
Exemplo n.º 5
0
void groestl512_add (groestl512_context* ctxt, const uint8_t data[], size_t length) {
	block_add (&ctxt->b, length, data);
}
Exemplo n.º 6
0
void sha1_add (sha1_context* ctxt, const uint8_t data[], size_t length) {
	block_add (&ctxt->b, length, data);
}