@@ -518,23 +518,6 @@ void handleSystemEvents(void) {
518
518
yield ();
519
519
}
520
520
521
- // https://stackoverflow.com/questions/9072320/split-string-into-string-array
522
- String getValue (String data, char separator, int index)
523
- {
524
- int found = 0 ;
525
- int strIndex[] = {0 , -1 };
526
- int max_index = data.length () - 1 ;
527
-
528
- for (int i = 0 ; i <= max_index && found <= index; i++) {
529
- if (data.charAt (i) == separator || i == max_index) {
530
- found++;
531
- strIndex[0 ] = strIndex[1 ] + 1 ;
532
- strIndex[1 ] = (i == max_index) ? i + 1 : i;
533
- }
534
- }
535
- return found > index ? data.substring (strIndex[0 ], strIndex[1 ]) : " " ;
536
- }
537
-
538
521
void waitForClientData (void ) {
539
522
client_buffer = " " ;
540
523
@@ -610,6 +593,33 @@ uint8_t *hexStringToUint8Array(const String &hexString, uint8_t *uint8Array, con
610
593
return uint8Array;
611
594
}
612
595
596
+ struct MiningJob
597
+ {
598
+ String last_block_hash;
599
+ String expected_hash_str;
600
+ uint8_t expected_hash[20 ];
601
+ unsigned int difficulty;
602
+
603
+ bool parse (char * job_str)
604
+ {
605
+ String tokens[3 ];
606
+ char *token = strtok (job_str, " ," );
607
+ for (int i = 0 ; token != NULL && i < 3 ; i++)
608
+ {
609
+ tokens[i] = token;
610
+ token = strtok (NULL , " ," );
611
+ }
612
+
613
+ last_block_hash = tokens[0 ];
614
+ expected_hash_str = tokens[1 ];
615
+ hexStringToUint8Array (expected_hash_str, expected_hash, 20 );
616
+ difficulty = tokens[2 ].toInt () * 100 + 1 ;
617
+
618
+ return true ;
619
+ }
620
+ };
621
+
622
+
613
623
void setup () {
614
624
Serial.begin (500000 );
615
625
Serial.println (" \n Duino-Coin " + String (MINER_VER));
@@ -714,21 +724,17 @@ void loop() {
714
724
#endif
715
725
716
726
waitForClientData ();
717
- String last_block_hash = getValue (client_buffer, SEP_TOKEN, 0 );
718
- String expected_hash_str = getValue (client_buffer, SEP_TOKEN, 1 );
719
- difficulty = getValue (client_buffer, SEP_TOKEN, 2 ).toInt () * 100 + 1 ;
720
-
721
- if (USE_HIGHER_DIFF) system_update_cpu_freq (160 );
727
+ Serial.println (" Received job with size of " + String (client_buffer));
722
728
723
- int job_len = last_block_hash.length () + expected_hash_str.length () + String (difficulty).length ();
729
+ MiningJob job;
730
+ job.parse ((char *)client_buffer.c_str ());
724
731
725
- Serial.println (" Received job with size of " + String (job_len) + " bytes : " + last_block_hash + " " + expected_hash_str + " " + difficulty);
732
+ Serial.println (" Parsed job: " + job. last_block_hash + " " + job. expected_hash_str + " " + String (job. difficulty ) );
726
733
727
- uint8_t expected_hash[20 ];
728
- hexStringToUint8Array (expected_hash_str, expected_hash, 20 );
734
+ if (USE_HIGHER_DIFF) system_update_cpu_freq (160 );
729
735
730
736
br_sha1_init (&sha1_ctx_base);
731
- br_sha1_update (&sha1_ctx_base, last_block_hash.c_str (), last_block_hash.length ());
737
+ br_sha1_update (&sha1_ctx_base, job. last_block_hash .c_str (), job. last_block_hash .length ());
732
738
733
739
float start_time = micros ();
734
740
max_micros_elapsed (start_time, 0 );
@@ -743,7 +749,7 @@ void loop() {
743
749
br_sha1_update (&sha1_ctx, duco_numeric_result_str.c_str (), duco_numeric_result_str.length ());
744
750
br_sha1_out (&sha1_ctx, hashArray);
745
751
746
- if (memcmp (expected_hash, hashArray, 20 ) == 0 ) {
752
+ if (memcmp (job. expected_hash , hashArray, 20 ) == 0 ) {
747
753
// If result is found
748
754
if (LED_BLINKING) digitalWrite (LED_BUILTIN, HIGH);
749
755
unsigned long elapsed_time = micros () - start_time;
0 commit comments