Skip to content

Commit 23a8a74

Browse files
committed
no message
1 parent 34f61f6 commit 23a8a74

File tree

3 files changed

+66
-126
lines changed

3 files changed

+66
-126
lines changed

tool3/MainFrm.cpp

Lines changed: 58 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,13 @@ const std::wstring bin2hex(const unsigned char *p, size_t len)
9494
return f.str();
9595
}
9696

97-
size_t hex2bin(unsigned char *p , const char *hexstr, size_t len)
98-
{
99-
size_t wlen = 0;
100-
while ((wlen<len) && *hexstr && *(hexstr+1)) //last condition cause np if check fails on middle one.thats coz of short-circuit evaluation
101-
{
102-
len*=sscanf(hexstr, "%2hhx",p++); // 0 or 1 (maybe smth else too) . Slow stuff , np
103-
hexstr += 2;
104-
wlen++;
97+
size_t hex2bin(unsigned char *p , const char *hexstr,const size_t length) {
98+
size_t wcount = 0;
99+
while ( wcount++ < length && *hexstr && *(hexstr + 1)) { //last condition cause np if check fails on middle one.thats coz of short-circuit evaluation
100+
sscanf(hexstr, "%2hhx",p++); //7x slower than tables but doesnt metter
101+
hexstr = hexstr+2;
105102
}
106-
return (wlen == len - 1)*len; // zero if error .had enough
103+
return --wcount; // error check here is a waste
107104
}
108105

109106
int bren=5;
@@ -119,129 +116,82 @@ VOID c(VOID *x)
119116
SETTEXTEX fw;
120117
fw.flags=4;
121118
fw.codepage=1200;
119+
std::string timestamp = CW2A((LPCWSTR)mount_tx->m_timestamp);
120+
std::string pubkey = CW2A((LPCWSTR)mount_tx->m_pubkey);
121+
uint32_t nBits = mount_tx->m_nb;
122122

123-
unsigned char hash1[32];
124-
uint32_t timestamp_length = wcslen(mount_tx->m_timestamp);
125-
CW2A timestamp((LPCWSTR)mount_tx->m_timestamp);
126-
CW2A pubkey((LPCWSTR)mount_tx->m_pubkey);
127-
Transaction transaction={ {},/* version */1 ,1 , {},0xFFFFFFFF ,/* sequ */0xFFFFFFFF ,1 ,50*COIN , pubscriptlength , {} ,0 };
128-
129-
// Encode pubkey to binary and prepend pubkey size, then append the OP_CHECKSIG byte
130-
transaction.pubkeyScript[0] = 0x41; // A public key is 32 bytes X coordinate, 32 bytes Y coordinate and one byte 0x04, so 65 bytes i.e 0x41 in Hex.
131-
hex2bin(transaction.pubkeyScript + 1, pubkey ,65 ); // No error checking, yeah.
132-
transaction.pubkeyScript[66] = OP_CHECKSIG;
133-
134-
/* nbits related stuff , first is real size of value in bytes , next is value with
135-
null bytes on the left removed */
136-
uint8_t serializedData[500];
137-
uint32_t serializedData_pos = 0;
138-
memcpy(serializedData, &transaction.version, 41);
139-
serializedData_pos = serializedData_pos + 41;
140-
/* fo' sho' */
141-
// std::cout << offsetof(Transaction,prevoutIndex) + 4 /* size of prevoutIndex */ - offsetof(Transaction,version);/* OK output : " 41 " */
142-
serializedData_pos++; // +41 byte reserved
143-
144-
short pl= 0x01+(mount_tx->m_nb >> 8 > 0)+(mount_tx->m_nb >> 16 > 0)+(mount_tx->m_nb >> 24 > 0);
145-
serializedData[serializedData_pos++] =pl; // statement (smth > 0) returns 0 or 1
146-
memcpy(serializedData + serializedData_pos, &mount_tx->m_nb, pl);
123+
unsigned char hash1[32], serializedData[857];
124+
Transaction transaction = {/*version*/1,/*inputs*/1, {},/*poutindex*/0xFFFFFFFF,/*sequ*/0xFFFFFFFF,/*outputs*/1 ,50*COIN ,pubscriptlength, {0x41}/*pubscript[0]:(?)opcode or size */};
125+
hex2bin(transaction.pubkeyScript + 1, pubkey.c_str(), pubkey.length()/2); // pubkey to bytes , then append the OP_CHECKSIG byte
126+
transaction.pubkeyScript[pubscriptlength - 1] = OP_CHECKSIG;
127+
short sizeone = offsetof(Transaction, sequence); /*, OK output : 41 */
128+
short sizetwo = sizeof transaction - sizeone;/* OK output : 85 */
129+
memcpy(serializedData, &transaction, sizeone);
130+
uint32_t serializedData_pos = sizeone + 1;// 42nd byte reserved
131+
short pl = 0x01 + (nBits >> 8 > 0) + (nBits >> 16>0)+(nBits >> 24>0); // size of nbits , not neccesary same as sizeof(nbits)
132+
memcpy(serializedData + serializedData_pos, &nBits, serializedData[serializedData_pos++]/*a:1*/ = pl); // scriptSig {
147133
serializedData_pos = serializedData_pos + pl;
148-
149-
// It should essentially mean PUSH 1 byte on the stack which in this case is 0x04 or just 4
150-
serializedData[serializedData_pos++] = 0x01;
151-
serializedData[serializedData_pos++] = 0x04;
152-
serializedData[serializedData_pos++] = timestamp_length;
153-
uint32_t scriptSig_length = serializedData_pos - 42 + timestamp_length; // serializedData_pos inc'ed already to go as size
154-
serializedData[41] = scriptSig_length;
155-
156-
uint32_t serializedLength =
157-
4 // tx version
158-
+1 // number of inputs
159-
+32 // hash of previous output
160-
+4 // previous output's index
161-
+1 // 1 byte for the size of scriptSig (?)
162-
+scriptSig_length
163-
+4 // size of sequence
164-
+1 // number of outputs
165-
+8 // 8 bytes for coin value
166-
+1 // 1 byte to represent size of the pubkey Script
167-
+pubscriptlength
168-
+4; // 4 bytes for lock time
169-
170-
memcpy(serializedData + serializedData_pos, timestamp, timestamp_length);
171-
serializedData_pos = serializedData_pos + timestamp_length;
172-
/*scriptsig is done*/
173-
174-
memcpy(serializedData + serializedData_pos, &transaction.sequence, 85);
175-
176-
// hash it with SHA256 and then hash that result to get merkle hash
177-
SHA256(serializedData, serializedLength, hash1);
178-
SHA256(hash1, 32, transaction.merkleHash);
179-
180-
std::wstring merkleHash = bin2hex(transaction.merkleHash, 32);
181-
std::reverse(transaction.merkleHash,transaction.merkleHash + 32);
182-
std::wstring merkleHashSwapped = bin2hex(transaction.merkleHash, 32);
183-
std::wstring txScriptSig = bin2hex(serializedData + 42, scriptSig_length);
134+
*(short*)(serializedData + serializedData_pos) = 0x0401/*b:2*/; // {0x01,0x04} ,script op's
135+
serializedData_pos = serializedData_pos + 2;
136+
memcpy(serializedData + serializedData_pos, timestamp.c_str(), serializedData[serializedData_pos++]/*c:1*/ = timestamp.length()); // scriptSig end
137+
memcpy(serializedData + serializedData_pos + timestamp.length(), &transaction.sequence, sizetwo);
138+
uint32_t scriptSig_length = serializedData[sizeone] = pl + 4/*abc round-up*/ + timestamp.length();
139+
SHA256(serializedData, sizeof transaction + 1 + scriptSig_length, hash1); /* + 1 coz of serializedData[sizeone]*/
140+
blockheader block_header = {1/*version*/,{}/*hprev*/,{}/*merk*/,mount_tx->m_ut == 0 ? time(NULL) : mount_tx->m_ut ,nBits ,mount_tx->m_nonce };
141+
SHA256(hash1, 32, block_header.merk);// hash it with SHA256 and then hash that result to get merkle hash
142+
std::wstring merkleHash = bin2hex( block_header.merk, 32);
143+
std::reverse(block_header.merk,block_header.merk +32);
144+
std::wstring merkleHashSw = bin2hex(block_header.merk, 32);
145+
std::reverse(block_header.merk,block_header.merk +32);
146+
std::wstring txScriptSig = bin2hex(serializedData + sizeone + 1/*same*/, scriptSig_length);
184147
std::wstring pubScriptSig = bin2hex(transaction.pubkeyScript, pubscriptlength);
185-
186-
t<<L"Coinbase: "<< txScriptSig <<L"\n\nPubkeyScript: "<<pubScriptSig <<L"\n\nMerkle Hash: "<<merkleHash<<L"\nByteswapped: "<< merkleHashSwapped << std::endl;
187-
t<<L"Generating block...\n";
148+
t<<L"Coinbase: "<< txScriptSig <<L"\n\nPubkeyScript: "<<pubScriptSig <<L"\n\nMerkle Hash: "<<merkleHash<<L"\nByteswapped: "<< merkleHashSw <<std::endl <<L"Generating block...\n";
188149

189150
SendMessage(hc,EM_SETTEXTEX,(WPARAM)&fw,(LPARAM)(LPCWSTR)t.str().c_str());
190151
PostMessage(hc, WM_VSCROLL, SB_BOTTOM, 0);
191-
192-
unsigned char block_hash1[32];
193-
blockheader block_header={/* drift */ 1 , {} , {} , mount_tx->m_ut == 0 ? time(NULL) + c : mount_tx->m_ut + c, mount_tx->m_nb , mount_tx->m_nonce };
194-
blockhash block_hashf;
195-
unsigned char* block_headerp=(unsigned char*)&block_header;
196-
unsigned char* block_hashfp=(unsigned char*)&block_hashf;
197-
std::reverse(transaction.merkleHash,transaction.merkleHash + 32);
198-
memcpy(&block_header.merk, transaction.merkleHash, 32);
152+
unsigned char block_hash1[32] , block_hashfp[32];
199153
unsigned int counter=0, start = time(NULL);
200-
std::wstringstream w;
201-
std::ios_base::iostate xh=0;
202-
while (1) {
203-
{
204-
SHA256(block_headerp, 80, block_hash1);
205-
SHA256(block_hash1, 32, block_hashfp);
154+
std::wstringstream w;
155+
std::ios_base::iostate xh=0;
156+
while (1)
157+
{
158+
SHA256((unsigned char*)&block_header, 80, block_hash1);
159+
SHA256(block_hash1, 32, block_hashfp);
206160

207161
// The hash is in little-endian, so we check the last 4 bytes.
208-
if(block_hashf.checkbytes == 0) // { .. , 0x00, 0x00, 0x00, 0x00 }
209-
{
162+
if(*(uint32_t *)(block_hashfp + 28) == 0) // { .. , 0x00, 0x00, 0x00, 0x00 }
163+
{
210164
std::reverse(block_hashfp,block_hashfp +32);
211165
std::wstring blockHash = bin2hex(block_hashfp, 32);
212166
t<<L"\nBlock found!\nHash: " << blockHash <<L"\nNonce: " << block_header.startNonce << L"\nUnix time: "<< block_header.unixtime << std::endl;
213167
SendMessage(hc,EM_SETTEXTEX,(WPARAM)&fw,(LPARAM)(LPCWSTR)t.str().c_str());
214168
PostMessage(hc, WM_VSCROLL, SB_BOTTOM, 0);
215169
b=9;
216-
}
170+
}
217171

218-
if(time(NULL)-start > 0)
219-
{
172+
if(time(NULL)-start > 0)
173+
{
220174
w<<std::setw(7)<< counter<<L" Hashes/s, Nonce "<< std::setw(10.10)<< block_header.startNonce;
221175
counter = 0;
222176
start = time(NULL);
223177
b7[c]->SetWindowTextW((LPCWSTR)w.str().c_str());
224178
w.clear(xh, 0);
225179
w.str(L"");
226-
}
180+
}
227181

228-
if(block_header.startNonce == 0x100000000 - 1)
229-
{
230-
block_header.unixtime=block_header.unixtime + 3;//trick is that to change pre-start time to find a block(really it's smth else) faster then nonce wraps
231-
}
232-
block_header.startNonce++; //see what happens on out-bounds
233-
counter++;
182+
if(++block_header.startNonce == 0 ) block_header.unixtime=block_header.unixtime + 3;//trick is that to change pre-start time to find a block(really it's smth else) faster then nonce wraps
183+
counter++;
184+
185+
if (b)
186+
{
187+
q->EnableWindow(0);
188+
bh->EnableWindow();
189+
if (terminator) { PostMessage(hz, WM_CLOSE, NULL, NULL);}
190+
else { }
191+
b -= 3;
192+
bren = 5;
193+
break;
234194
}
235-
236-
if (b) {
237-
q->EnableWindow(0);
238-
bh->EnableWindow();
239-
if (terminator) { PostMessage(hz, WM_CLOSE, NULL, NULL);}
240-
else { }
241-
b -= 3;
242-
bren = 5;
243-
break;
244-
}
245195
}
246196
}
247197

tool3/tool3.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@ second : if structure has alignment you can copy it as block too but it gives no
1515

1616

1717
//here are PODs coz there is no constructors and others
18-
struct Transaction
19-
{
20-
#pragma pack(1)
21-
uint8_t merkleHash[32];
22-
/* +0 */uint32_t version;
23-
uint8_t numInputs;
24-
uint8_t prevOutput[32];
18+
struct Transaction {
19+
#pragma pack(1)
20+
/* +0 */uint32_t version; /* vs bitcoin-api tx block : merkleHash[32] removed */
21+
uint8_t numInputs;
22+
uint8_t prevOutput[32];
2523
uint32_t prevoutIndex; // +41
2624
/* +0 */uint32_t sequence;
27-
uint8_t numOutputs;
25+
uint8_t numOutputs;
2826
uint64_t outValue;
29-
uint8_t pubscriptlength;
30-
uint8_t pubkeyScript[::pubscriptlength];
27+
uint8_t pubscriptlength; //it isn't in the bitcoin-api tx block also
28+
uint8_t pubkeyScript[::pubscriptlength];
3129
uint32_t locktime; // +85
3230
} ;
33-
3431
struct blockheader {
3532
#pragma pack(1)
3633
uint32_t blockversion;
@@ -42,13 +39,6 @@ struct blockheader {
4239
};
4340

4441

45-
struct blockhash {
46-
#pragma pack(1)
47-
unsigned char bl[28];
48-
uint32_t checkbytes;
49-
};
50-
51-
5242
// Ctool3App:
5343
// See tool3.cpp for the implementation of this class
5444
//

x64/Release/tool3.exe

2.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)