mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-11 21:39:16 +02:00
mining: fix merkle root calculation and add unit test
This commit is contained in:
parent
64161f9db2
commit
f98cbf208b
@ -6,6 +6,6 @@
|
||||
char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
|
||||
const char * extranonce, int extranonce_2_len);
|
||||
|
||||
char * calculate_merkle_hash(const char * coinbase_tx, const uint8_t ** merkle_branches);
|
||||
char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t merkle_branches[][32], const int num_merkle_branches);
|
||||
|
||||
#endif // MINING_H
|
@ -5,18 +5,18 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX_MERKLE_BRANCHES 32
|
||||
#define PREV_BLOCK_HASH_SIZE 32
|
||||
#define HASH_SIZE 32
|
||||
#define COINBASE_SIZE 100
|
||||
#define COINBASE2_SIZE 128
|
||||
|
||||
typedef struct {
|
||||
uint32_t job_id;
|
||||
uint8_t prev_block_hash[PREV_BLOCK_HASH_SIZE];
|
||||
uint8_t prev_block_hash[HASH_SIZE];
|
||||
uint8_t coinbase_1[COINBASE_SIZE];
|
||||
size_t coinbase_1_len;
|
||||
uint8_t coinbase_2[COINBASE2_SIZE];
|
||||
size_t coinbase_2_len;
|
||||
uint8_t merkle_branches[MAX_MERKLE_BRANCHES][PREV_BLOCK_HASH_SIZE];
|
||||
uint8_t merkle_branches[MAX_MERKLE_BRANCHES][HASH_SIZE];
|
||||
size_t n_merkle_branches;
|
||||
uint32_t version;
|
||||
uint32_t curtime;
|
||||
|
@ -21,7 +21,7 @@ char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
|
||||
return coinbase_tx;
|
||||
}
|
||||
|
||||
char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t ** merkle_branches, const int num_merkle_branches)
|
||||
char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t merkle_branches[][32], const int num_merkle_branches)
|
||||
{
|
||||
size_t coinbase_tx_bin_len = strlen(coinbase_tx) / 2;
|
||||
uint8_t * coinbase_tx_bin = malloc(coinbase_tx_bin_len);
|
||||
@ -33,13 +33,13 @@ char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t ** mer
|
||||
free(new_root);
|
||||
for (int i = 0; i < num_merkle_branches; i++)
|
||||
{
|
||||
memcpy(both_merkles + 32, merkle_branches + 32*i, 32);
|
||||
memcpy(both_merkles + 32, merkle_branches[i], 32);
|
||||
uint8_t * new_root = double_sha256_bin(both_merkles, 64);
|
||||
memcpy(both_merkles, new_root, 32);
|
||||
free(new_root);
|
||||
}
|
||||
|
||||
char * merkle_root_hash = malloc(33);
|
||||
bin2hex(both_merkles, 32, merkle_root_hash, 33);
|
||||
char * merkle_root_hash = malloc(65);
|
||||
bin2hex(both_merkles, 32, merkle_root_hash, 65);
|
||||
return merkle_root_hash;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#include "unity.h"
|
||||
#include "mining.h"
|
||||
#include "utils.h"
|
||||
|
||||
TEST_CASE("Check coinbase tx construction", "[mining]")
|
||||
{
|
||||
@ -9,4 +10,29 @@ TEST_CASE("Check coinbase tx construction", "[mining]")
|
||||
const int extranonce_2_len = 4;
|
||||
char * coinbase_tx = construct_coinbase_tx(coinbase_1, coinbase_2, extranonce, extranonce_2_len);
|
||||
TEST_ASSERT_EQUAL_STRING(coinbase_tx, "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff20020862062f503253482f04b8864e5008e969579199999999072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000");
|
||||
free(coinbase_tx);
|
||||
}
|
||||
|
||||
TEST_CASE("Validate merkle root calculation", "[mining]")
|
||||
{
|
||||
const char * coinbase_tx = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff20020862062f503253482f04b8864e5008e969579199999999072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000";
|
||||
uint8_t merkles[12][32];
|
||||
int num_merkles = 12;
|
||||
|
||||
hex2bin("ae23055e00f0f697cc3640124812d96d4fe8bdfa03484c1c638ce5a1c0e9aa81", merkles[0], 32);
|
||||
hex2bin("980fb87cb61021dd7afd314fcb0dabd096f3d56a7377f6f320684652e7410a21", merkles[1], 32);
|
||||
hex2bin("a52e9868343c55ce405be8971ff340f562ae9ab6353f07140d01666180e19b52", merkles[2], 32);
|
||||
hex2bin("7435bdfa004e603953b2ed39f118803934d9cf17b06d979ceb682f2251bafac2", merkles[3], 32);
|
||||
hex2bin("2a91f061a22d27cb8f44eea79938fb241ebeb359891aa907f05ffde7ed44e52e", merkles[4], 32);
|
||||
hex2bin("302401f80eb5e958155135e25200bb8ea181ad2d05e804a531c7314d86403cdc", merkles[5], 32);
|
||||
hex2bin("318ecb6161eb9b4cfd802bd730e2d36c167ddf102e70aa7b4158e2870dd47392", merkles[6], 32);
|
||||
hex2bin("1114332a9858e0cf84b2425bb1e59eaabf91dd102d114aa443d57fc1b3beb0c9", merkles[7], 32);
|
||||
hex2bin("f43f38095c810613ed795a44d9fab02ff25269706f454885db9be05cdf9c06e1", merkles[8], 32);
|
||||
hex2bin("3e2fc26b27fddc39668b59099cd9635761bb72ed92404204e12bdff08b16fb75", merkles[9], 32);
|
||||
hex2bin("463c19427286342120039a83218fa87ce45448e246895abac11fff0036076758", merkles[10], 32);
|
||||
hex2bin("03d287f655813e540ddb9c4e7aeb922478662b0f5d8e9d0cbd564b20146bab76", merkles[11], 32);
|
||||
|
||||
char * root_hash = calculate_merkle_root_hash(coinbase_tx, merkles, num_merkles);
|
||||
TEST_ASSERT_EQUAL_STRING("adbcbc21e20388422198a55957aedfa0e61be0b8f2b87d7c08510bb9f099a893", root_hash);
|
||||
free(root_hash);
|
||||
}
|
@ -4,13 +4,8 @@
|
||||
#include <pthread.h>
|
||||
#include "stratum_api.h"
|
||||
|
||||
|
||||
#define QUEUE_SIZE 10
|
||||
|
||||
#define MAX_MERKLE_BRANCHES 32
|
||||
#define PREV_BLOCK_HASH_SIZE 32
|
||||
#define COINBASE_SIZE 100
|
||||
|
||||
typedef struct {
|
||||
work buffer[QUEUE_SIZE];
|
||||
int head;
|
||||
|
Loading…
x
Reference in New Issue
Block a user