forked from hyc/wolf-xmr-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stratum.c
66 lines (49 loc) · 2.28 KB
/
stratum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdint.h>
#include <string.h>
#include "sha256.h"
#include "stratum.h"
#include "minerutils.h"
// Returns buffer that must be freed in RawCoinbase, and
// length of said buffer as the return value
void CreateRawCoinbaseHash( uint8_t * RawCoinbaseHash, const uint8_t * Coinbase1, const uint8_t * Coinbase2, const uint8_t * Extranonce1,
const uint8_t * Extranonce2, const uint8_t ENonce2Len)
{
uint32_t Length = 0;
uint8_t *RawCoinbase = (uint8_t *)malloc(sizeof(uint8_t) * ((strlen(Coinbase1) + strlen(Coinbase2) + strlen(Extranonce1) + (ENonce2Len << 1)) >> 1));
Length += ASCIIHexToBinary(RawCoinbase, Coinbase1, strlen(Coinbase1));
Length += ASCIIHexToBinary(RawCoinbase + Length, Extranonce1, strlen(Extranonce1));
memcpy(RawCoinbase + Length, Extranonce2, ENonce2Len);
Length += ENonce2Len;
Length += ASCIIHexToBinary(RawCoinbase + Length, Coinbase2, strlen(Coinbase2));
sha256d(RawCoinbaseHash, RawCoinbase, Length);
free(RawCoinbase);
}
// Job MUST be a local copy
void StratumCreateBlockHeader(uint8_t *hdr, JobInfo *job, uint32_t enonce2)
{
uint8_t *RawCoinbase, RawCoinbaseHash[32], MerkleRoot[64], temp[64];
uint32_t RawHeaderLen;
CreateRawCoinbaseHash(RawCoinbaseHash, job->Coinbase1, job->Coinbase2, job->Extranonce1, (uint8_t *)&enonce2, job->ENonce2Bytes);
RawHeaderLen = ASCIIHexToBinary(hdr, job->Version, strlen(job->Version));
RawHeaderLen += ASCIIHexToBinary(hdr + RawHeaderLen, job->PrevHash, strlen(job->PrevHash));
memcpy(MerkleRoot, RawCoinbaseHash, 32);
if(job->MerkleBranches)
{
for(int i = 0; job->MerkleBranches[i]; ++i)
{
uint8_t RawMerkleBranch[32];
ASCIIHexToBinary(RawMerkleBranch, job->MerkleBranches[i], 64);
memcpy(MerkleRoot + 32, RawMerkleBranch, 32);
sha256d(temp, MerkleRoot, 64);
memcpy(MerkleRoot, temp, 32);
}
}
SwapBuffer32(MerkleRoot, 8);
memcpy(hdr + RawHeaderLen, MerkleRoot, 32);
RawHeaderLen += 32;
RawHeaderLen += ASCIIHexToBinary(hdr + RawHeaderLen, job->Time, strlen(job->Time));
RawHeaderLen += ASCIIHexToBinary(hdr + RawHeaderLen, job->NetworkDiff, strlen(job->NetworkDiff));
memset(hdr + RawHeaderLen, 0x00, 4);
RawHeaderLen += 4;
ASCIIHexToBinary(hdr + RawHeaderLen, "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000", 48 * 2);
}