mining: add method to construct coinbase tx

This commit is contained in:
johnny9 2023-05-01 22:45:01 -04:00
parent 799d00dc2b
commit 10fc3d017e
4 changed files with 37 additions and 1 deletions

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "stratum_api.c"
idf_component_register(SRCS "mining.c" "stratum_api.c"
INCLUDE_DIRS "include"
REQUIRES json)

View File

@ -0,0 +1,4 @@
#include "stratum_api.h"
char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
const char * extranonce, int extranonce_2_len);

View File

@ -0,0 +1,20 @@
#include <string.h>
char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
const char * extranonce, int extranonce_2_len)
{
int coinbase_tx_len = strlen(coinbase_1) + strlen(coinbase_2)
+ strlen(extranonce) + extranonce_2_len * 2 + 1;
char extranonce_2[extranonce_2_len * 2];
memset(extranonce_2, '9', extranonce_2_len * 2);
char * coinbase_tx = malloc(coinbase_tx_len * sizeof(char));
strcpy(coinbase_tx, coinbase_1);
strcat(coinbase_tx, extranonce);
strcat(coinbase_tx, extranonce_2);
strcat(coinbase_tx, coinbase_2);
coinbase_tx[coinbase_tx_len - 1] = '\0';
return coinbase_tx;
}

View File

@ -0,0 +1,12 @@
#include "unity.h"
#include "mining.h"
TEST_CASE("Check coinbase tx construction", "[mining]")
{
const char * coinbase_1 = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff20020862062f503253482f04b8864e5008";
const char * coinbase_2 = "072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000";
const char * extranonce = "e9695791";
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");
}