-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlws_protocol.c
More file actions
1263 lines (1056 loc) · 34.3 KB
/
lws_protocol.c
File metadata and controls
1263 lines (1056 loc) · 34.3 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "lws_protocol.h"
// ArrayList used c-algorithms--https://fragglet.github.io/c-algorithms/doc/arraylist_8h.html
typedef void *ArrayListValue;
typedef struct _ArrayList ArrayList;
struct _ArrayList {
ArrayListValue *data;
unsigned int length;
unsigned int _alloced;
};
typedef int (*ArrayListEqualFunc)(ArrayListValue value1, ArrayListValue value2);
typedef int (*ArrayListCompareFunc)(ArrayListValue value1, ArrayListValue value2);
static ArrayList *arraylist_new(unsigned int length)
{
ArrayList *new_arraylist;
if (length <= 0) {
length = 16;
}
new_arraylist = (ArrayList *)malloc(sizeof(ArrayList));
if (new_arraylist == NULL) {
return NULL;
}
new_arraylist->_alloced = length;
new_arraylist->length = 0;
new_arraylist->data = malloc(length * sizeof(ArrayListValue));
if (new_arraylist->data == NULL) {
free(new_arraylist);
return NULL;
}
return new_arraylist;
}
static void arraylist_free(ArrayList *arraylist)
{
if (arraylist != NULL) {
free(arraylist->data);
free(arraylist);
}
}
static int arraylist_enlarge(ArrayList *arraylist)
{
ArrayListValue *data;
unsigned int newsize;
newsize = arraylist->_alloced * 2;
data = realloc(arraylist->data, sizeof(ArrayListValue) * newsize);
if (data == NULL) {
return 0;
} else {
arraylist->data = data;
arraylist->_alloced = newsize;
return 1;
}
}
static int arraylist_insert(ArrayList *arraylist, unsigned int index, ArrayListValue data)
{
if (index > arraylist->length) {
return 0;
}
if (arraylist->length + 1 > arraylist->_alloced) {
if (!arraylist_enlarge(arraylist)) {
return 0;
}
}
memmove(&arraylist->data[index + 1], &arraylist->data[index], (arraylist->length - index) * sizeof(ArrayListValue));
arraylist->data[index] = data;
++arraylist->length;
return 1;
}
static int arraylist_append(ArrayList *arraylist, ArrayListValue data)
{
return arraylist_insert(arraylist, arraylist->length, data);
}
// static int arraylist_prepend(ArrayList *arraylist, ArrayListValue data) { return arraylist_insert(arraylist, 0,
// data); }
static void arraylist_remove_range(ArrayList *arraylist, unsigned int index, unsigned int length)
{
if (index > arraylist->length || index + length > arraylist->length) {
return;
}
memmove(&arraylist->data[index], &arraylist->data[index + length],
(arraylist->length - (index + length)) * sizeof(ArrayListValue));
arraylist->length -= length;
}
static void arraylist_remove(ArrayList *arraylist, unsigned int index) { arraylist_remove_range(arraylist, index, 1); }
static int arraylist_index_of(ArrayList *arraylist, ArrayListEqualFunc callback, ArrayListValue data)
{
unsigned int i;
for (i = 0; i < arraylist->length; ++i) {
if (callback(arraylist->data[i], data) != 0)
return (int)i;
}
return -1;
}
static void arraylist_clear(ArrayList *arraylist) { arraylist->length = 0; }
static void arraylist_sort_internal(ArrayListValue *list_data, unsigned int list_length,
ArrayListCompareFunc compare_func)
{
ArrayListValue pivot;
ArrayListValue tmp;
unsigned int i;
unsigned int list1_length;
unsigned int list2_length;
if (list_length <= 1) {
return;
}
pivot = list_data[list_length - 1];
list1_length = 0;
for (i = 0; i < list_length - 1; ++i) {
if (compare_func(list_data[i], pivot) < 0) {
tmp = list_data[i];
list_data[i] = list_data[list1_length];
list_data[list1_length] = tmp;
++list1_length;
} else {
}
}
list2_length = list_length - list1_length - 1;
list_data[list_length - 1] = list_data[list1_length];
list_data[list1_length] = pivot;
arraylist_sort_internal(list_data, list1_length, compare_func);
arraylist_sort_internal(&list_data[list1_length + 1], list2_length, compare_func);
}
static void arraylist_sort(ArrayList *arraylist, ArrayListCompareFunc compare_func)
{
arraylist_sort_internal(arraylist->data, arraylist->length, compare_func);
}
typedef struct {
uint16_t version;
uint16_t type;
uint32_t timestamp;
uint32_t lock_until;
unsigned char hash_anchor[32];
uint8_t size0;
unsigned char *input;
uint8_t prefix;
unsigned char address[32];
uint64_t amount;
uint64_t tx_fee;
uint8_t size1;
unsigned char *vch_data;
uint8_t size2;
unsigned char sign[64];
} Transaction;
struct _LWSProtocol {
uint32_t listunspent_index;
uint32_t sendtx_index;
LWSProtocolHook *hook;
ArrayList *utxo_list;
unsigned char last_block_hash[32];
uint32_t last_block_height;
uint32_t last_block_time;
uint64_t next_amount;
};
struct ListUnspentRequest {
uint32_t nonce;
unsigned char address[33];
unsigned char fork_id[32];
};
struct SendTxRequest {
uint32_t nonce;
unsigned char tx_id[32];
unsigned char fork_id[32];
uint16_t data_size;
unsigned char *tx_data;
};
struct UTXO {
unsigned char txid[32];
uint8_t out;
uint32_t timestamp;
uint16_t type;
uint64_t amount;
unsigned char sender[33];
uint32_t lock_until;
uint16_t data_size;
unsigned char *data;
};
struct ListUnspentBody {
uint16_t command;
uint32_t nonce;
unsigned char block_hash[32];
uint32_t block_height;
uint32_t block_time;
uint16_t utxo_number;
ArrayList *utxo_list;
};
struct SendTxBody {
uint16_t command;
uint32_t nonce;
unsigned char tx_id[32];
};
struct RequestHead {
uint16_t version;
uint8_t length;
unsigned char *id;
unsigned char hash[32];
};
/**
* @brief hex2char
* Convert hex to unsigned char array(bytes)
* @author gaochun
* @email gaochun@dabank.io
* @date 2019/11/18 17:0:47
* @param char * hex -input hex string
* @param unsigned char * bin -output unsigned char array
* @return static size_t
*/
static size_t hex_to_uchar(const char *hex, const size_t length, unsigned char *bin)
{
// size_t len = strlen(hex);
size_t final_len = length / 2;
size_t i, j;
for (i = 0, j = 0; j < final_len; i += 2, j++) {
bin[j] = (unsigned char)((hex[i] % 32 + 9) % 25 * 16 + (hex[i + 1] % 32 + 9) % 25);
}
return final_len;
}
/**
* @brief reverse
* reverse the unisgned char array
* @author gaochun
* @email gaochun@dabank.io
* @date 2020/9/11 11:8:51
* @param unsigned char * p
* @param int size
* @return void
*/
static void reverse(unsigned char *p, int size)
{
int i;
unsigned char tmp;
for (i = 0; i < size / 2; i++) {
tmp = p[i];
p[i] = p[size - 1 - i];
p[size - 1 - i] = tmp;
}
}
/**
* @brief big_num_compare
* compare two uint256
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 11:46:31
* @param big_num data1
* @param big_num data2
* @return static int
*/
static int big_num_compare(big_num data1, big_num data2)
{
int i;
for (i = 31; i >= 0; i--) {
// printf("%d, data1:%u, data2:%u\n", i, data1->pn[i], data2->pn[i]);
if (data1[i] > data2[i]) {
return 1;
}
if (data1[i] == data2[i]) {
continue;
}
if (data1[i] < data2[i]) {
return -1;
}
}
return 0;
}
/**
* @brief serialize_join
* serialize unsigned char array
* @author gaochun
* @email gaochun@dabank.io
* @date 2019/8/7 17:4:50
* @param size_t * size -i/o array len,first call set to zero
* @param void * thing -void ptr for something that need to be serialized
* @param size_t size_thing -length of things that need to be serialized(byte size)
* @param unsigned char * data -output series
* @return static size_t -size
*/
static size_t serialize_join(size_t *size, void *thing, size_t size_thing, unsigned char *data)
{
memcpy(data + *size, thing, size_thing);
*size += size_thing;
return *size;
}
/**
* @brief deserialize_join
* Deserialize to struct case
* @author gaochun
* @email gaochun@dabank.io
* @date 2019/8/19 21:20:28
* @param size_t * size -byte length counter
* @param unsigned char * data -data series
* @param void * thing -struct case ptr
* @param size_t size_thing -something that need tobe deserialized
* @return static size_t -size
*/
static size_t deserialize_join(size_t *size, const unsigned char *data, void *thing, size_t size_thing)
{
memcpy(thing, data + *size, size_thing);
*size += size_thing;
return *size;
}
/**
* @brief protocol_utils_hex2bin
* hex to bin
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 11:47:59
* @param const char * hex-input data hex
* @param unsigned char * bin-output bin data
* @return size_t
*/
size_t protocol_utils_hex2bin(const char *hex, const size_t length, unsigned char *bin)
{
return hex_to_uchar(hex, length, bin);
}
/**
* @brief protocol_utils_reverse
* reverse bytes
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 11:48:55
* @param void * data-input/output data
* @param size_t size-data length
* @return void
*/
void protocol_utils_reverse(void *data, size_t size) { return reverse((unsigned char *)data, size); }
/**
* @brief protocol_new
* create new protocol object
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/1/26 22:22:0
* @param const LWSProtocolHook * hook-input hook object
* @param LWSProtocol ** protocol-output protocol point
* @return LWSPError
*/
LWSPError protocol_new(const LWSProtocolHook *hook, LWSProtocol **protocol)
{
if (NULL == hook) {
return LWSPError_Hook_NULL;
}
if (NULL == hook->hook_id_get) {
return LWSPError_HookDevieIDGet_NULL;
}
if (NULL == hook->hook_nonce_get) {
return LWSPError_HookNonceGet_NULL;
}
if (NULL == hook->hook_datetime_get) {
return LWSPError_HookDatetimeGet_NULL;
}
if (NULL == hook->hook_public_key_get) {
return LWSPError_HookPublicKeyGet_NULL;
}
if (NULL == hook->hook_fork_get) {
return LWSPError_HookForkGet_NULL;
}
if (NULL == hook->hook_sha256_get) {
return LWSPError_HookSHA256GET_NULL;
}
unsigned char id[256];
int id_length = hook->hook_id_get(hook->hook_id_context, id);
if (256 < id_length || 0 == id_length) {
return LWSPError_ID_Length;
}
*protocol = malloc(sizeof(LWSProtocol));
if (NULL == *protocol) {
return LWSPError_Allocate_Fail;
}
memset(*protocol, 0x00, sizeof(LWSProtocol));
(*protocol)->hook = malloc(sizeof(LWSProtocolHook));
if (NULL == (*protocol)->hook) {
free(*protocol);
*protocol = NULL;
return LWSPError_Allocate_Fail;
}
memcpy((*protocol)->hook, hook, sizeof(LWSProtocolHook));
(*protocol)->listunspent_index = 0;
(*protocol)->sendtx_index = 0;
(*protocol)->utxo_list = arraylist_new(0);
return LWSPError_Success;
}
/**
* @brief check_endian
* check system endian
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 11:52:29
* @return static int 1-little endian;0-big endian
*/
static int check_endian()
{
int a = 1;
char *p = (char *)&a;
return (*p == 1); /*1:little-endian, 0:big-endian*/
}
/**
* @brief wrap_request
* wrap a request
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 11:55:12
* @param LWSProtocol * protocol-input protocol point
* @param const unsigned char * data-input message body
* @param const size_t data_len-input message body length
* @param sha256_hash hash-input message hash
* @param unsigned char * request-output request data
* @param size_t * length-output request data length
* @return static LWSPError error
*/
static LWSPError wrap_request(LWSProtocol *protocol, const unsigned char *data, const size_t data_len, sha256_hash hash,
unsigned char *request, size_t *length)
{
struct RequestHead head;
head.version = VERSION;
unsigned char device_id[256];
head.length = protocol->hook->hook_id_get(protocol->hook->hook_id_context, device_id);
head.id = device_id;
memcpy(head.hash, hash, 32);
uint32_t foot = 0;
size_t len = 2 + 1 + head.length + 32 + data_len + 4;
memcpy(request, &head.version, 2);
memcpy(&request[2], &head.length, 1);
memcpy(&request[3], head.id, head.length);
memcpy(&request[3 + head.length], hash, 32);
memcpy(&request[3 + head.length + 32], data, data_len);
foot = protocol->hook->hook_crc32_get(protocol->hook->hook_crc32_context, request, len - 4);
memcpy(&request[3 + head.length + 32 + data_len], &foot, 4);
*length = len;
return LWSPError_Success;
}
/**
* @brief protocol_listunspent_request
* generate listunspent request
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 12:27:55
* @param LWSProtocol * protocol-input protocol object
* @param sha256_hash hash-input sha256 hash
* @param unsigned char * data-output request serilized data
* @param size_t * length-output request data length
* @return LWSPError
*/
LWSPError protocol_listunspent_request(LWSProtocol *protocol, sha256_hash hash, unsigned char *data, size_t *length)
{
if (NULL == protocol) {
return LWSPError_Protocol_NULL;
}
if (NULL == protocol->hook) {
return LWSPError_Hook_NULL;
}
struct ListUnspentRequest request;
request.nonce = protocol->hook->hook_nonce_get(protocol->hook->hook_nonce_context);
protocol->hook->hook_public_key_get(protocol->hook->hook_public_key_context, request.address);
protocol->hook->hook_fork_get(protocol->hook->hook_fork_context, request.fork_id);
size_t body_len = 4 + 33 + 32 + 2;
unsigned char body[body_len];
uint16_t command = ListUnspent;
memcpy(body, &command, 2);
memcpy(&body[2], &request, body_len - 2);
protocol->hook->hook_sha256_get(protocol->hook->hook_sha256_context, body, body_len, hash);
wrap_request(protocol, body, body_len, hash, data, length);
protocol->listunspent_index++;
return LWSPError_Success;
}
/**
* @brief compare_utxo
* Compare v1(UTXO) and v2(UTXO)
* @author gaochun
* @email gaochun@dabank.io
* @date 2019/11/18 17:50:58
* @param ArrayListValue v1 -input UTXO instance
* @param ArrayListValue v2 -input UTXO instance
* @return static int --if equal return 0, v1 greater then v2 return 1, else return -1
*/
static int compare_utxo(ArrayListValue v1, ArrayListValue v2)
{
struct UTXO *utxo1 = (struct UTXO *)v1;
struct UTXO *utxo2 = (struct UTXO *)v2;
big_num txid1, txid2;
memcpy(txid1, utxo1->txid, 32);
memcpy(txid2, utxo2->txid, 32);
int ret = big_num_compare(txid1, txid2);
// sort rule--ArrayListCompareFunc
// https://fragglet.github.io/c-algorithms/doc/arraylist_8h.html
// utxo == utxo2
if (0 == ret) {
if (utxo1->out == utxo2->out) {
return 0;
}
if (utxo1->out > utxo2->out) {
return 1;
}
if (utxo1->out < utxo2->out) {
return -1;
}
}
// utxo1 > utxo2
if (1 == ret) {
return 1;
}
// utxo1 < utxo2
if (-1 == ret) {
return -1;
}
return -1;
}
/**
* @brief reply_remove_head
* remove reply head
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 12:31:49
* @param const unsigned char * data
* @param const size_t length
* @param unsigned char * body
* @param size_t * body_len
* @return static LWSPError
*/
static LWSPError reply_remove_head(const unsigned char *data, const size_t length, unsigned char *body,
size_t *body_len)
{
if (36 > length) {
return LWSPError_Reply_Too_Short; // 数据长度错误
}
if ((length - 36) > 0) {
memcpy(body, &data[36], length - 36);
*body_len = length - 36;
} else {
return LWSPError_Empty_Command_Body;
}
return LWSPError_Success;
}
/**
* @brief listunspent_body_deserialize
* listunspent reply deserialize
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 12:33:16
* @param const unsigned char * data
* @return static struct ListUnspentBody
*/
static struct ListUnspentBody listunspent_body_deserialize(const unsigned char *data)
{
struct ListUnspentBody body;
body.utxo_list = arraylist_new(0);
size_t size = 0;
size_t size_thing = sizeof(body.command);
deserialize_join(&size, data, &body.command, size_thing);
size_thing = sizeof(body.nonce);
deserialize_join(&size, data, &body.nonce, size_thing);
size_thing = sizeof(body.block_hash);
deserialize_join(&size, data, body.block_hash, size_thing);
size_thing = sizeof(body.block_height);
deserialize_join(&size, data, &body.block_height, size_thing);
size_thing = sizeof(body.block_time);
deserialize_join(&size, data, &body.block_time, size_thing);
size_thing = sizeof(body.utxo_number);
deserialize_join(&size, data, &body.utxo_number, size_thing);
// 端序转换
reverse((unsigned char *)&body.command, 2);
reverse((unsigned char *)&body.nonce, 4);
reverse((unsigned char *)&body.block_height, 4);
reverse((unsigned char *)&body.block_time, 4);
reverse((unsigned char *)&body.utxo_number, 2);
// UTXOList
int i;
for (i = 0; i < body.utxo_number; i++) {
struct UTXO *utxo = (struct UTXO *)malloc(sizeof(struct UTXO));
size_thing = sizeof(utxo->txid);
deserialize_join(&size, data, utxo->txid, size_thing);
size_thing = sizeof(utxo->out);
deserialize_join(&size, data, &utxo->out, size_thing);
size_thing = sizeof(utxo->timestamp);
deserialize_join(&size, data, &utxo->timestamp, size_thing);
size_thing = sizeof(utxo->type);
deserialize_join(&size, data, &utxo->type, size_thing);
size_thing = sizeof(utxo->amount);
deserialize_join(&size, data, &utxo->amount, size_thing);
size_thing = sizeof(utxo->sender);
deserialize_join(&size, data, utxo->sender, size_thing);
size_thing = sizeof(utxo->lock_until);
deserialize_join(&size, data, &utxo->lock_until, size_thing);
size_thing = sizeof(utxo->data_size);
deserialize_join(&size, data, &utxo->data_size, size_thing);
size_thing = utxo->data_size;
unsigned char *d = (unsigned char *)malloc(sizeof(unsigned char) * size_thing);
deserialize_join(&size, data, d, size_thing);
utxo->data = d;
// 端序转换
reverse((unsigned char *)&utxo->txid, 32);
reverse((unsigned char *)&utxo->out, 1);
reverse((unsigned char *)&utxo->timestamp, 4);
reverse((unsigned char *)&utxo->type, 2);
reverse((unsigned char *)&utxo->amount, 8);
reverse((unsigned char *)&utxo->lock_until, 4);
reverse((unsigned char *)&utxo->data_size, 2);
arraylist_append(body.utxo_list, utxo);
}
return body;
}
static LWSPError reply_crc32(LWSProtocol *protocol, const unsigned char *data, const size_t len)
{
uint32_t crc32 = 0;
memcpy(&crc32, &data[len - 4], 4);
uint32_t foot = protocol->hook->hook_crc32_get(protocol->hook->hook_crc32_context, data, len - 4);
reverse((unsigned char *)&crc32, 4);
if (foot != crc32) {
return LWSPError_CRC32_Different;
}
return LWSPError_Success;
}
/**
* @brief protocol_listunspent_reply_handle
* listunspent reply handle
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 12:48:27
* @param LWSProtocol * protocol
* @param const unsigned char * data
* @param const size_t len
* @return LWSPError
*/
LWSPError protocol_listunspent_reply_handle(LWSProtocol *protocol, const unsigned char *data, const size_t len)
{
LWSPError error = reply_crc32(protocol, data, len);
if (LWSPError_Success != error) {
return error;
}
unsigned char out[len];
size_t out_len = 0;
error = reply_remove_head(data, len, out, &out_len);
if (LWSPError_Success == error) {
struct ListUnspentBody body = listunspent_body_deserialize(out);
// 全局状态
memcpy(protocol->last_block_hash, body.block_hash, 32);
protocol->last_block_height = body.block_height;
protocol->last_block_time = body.block_time;
// clear globle utxo list
int i, len = protocol->utxo_list->length;
// printf("utxo list length:%d\n", len);
for (i = 0; i < len; i++) {
struct UTXO *utxo = (struct UTXO *)protocol->utxo_list->data[i];
if (!utxo && !utxo->data) {
free(utxo->data);
free(utxo);
}
}
arraylist_clear(protocol->utxo_list);
len = body.utxo_list->length;
for (i = 0; i < len; i++) {
struct UTXO *utxo = (struct UTXO *)body.utxo_list->data[i];
arraylist_append(protocol->utxo_list, utxo);
}
arraylist_sort(protocol->utxo_list, compare_utxo);
arraylist_clear(body.utxo_list);
arraylist_free(body.utxo_list);
} else {
return error;
}
return LWSPError_Success;
}
/**
* @brief protocol_reply_info
* get protocol reply infomation
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 12:49:45
* @param LWSProtocol * protocol
* @param const unsigned char * data
* @param const size_t length
* @param ReplyInfo * info
* @return LWSPError
*/
LWSPError protocol_reply_info(LWSProtocol *protocol, const unsigned char *data, const size_t length, ReplyInfo *info)
{
if (38 > length) {
return LWSPError_Reply_Too_Short; // 数据长度错误
}
memcpy(info, data, 38);
// 转小端序
reverse((unsigned char *)&info->version, 2);
reverse((unsigned char *)&info->error, 2);
reverse((unsigned char *)&info->command, 2);
return LWSPError_Success;
}
/**
* @brief calc_tx_fee
* calc min transaction fee
* @author shang
* @email shang_qd@qq.com
* @date 2020/5/19 20:00
* @param nVchData * vchdata size
* @return static size_t -return vchdata fee
*/
static size_t calc_tx_fee(size_t nVchData)
{
size_t nMinFee = 10000;
if (0 == nVchData) {
return nMinFee;
}
uint32_t multiplier = nVchData / 200;
if (nVchData % 200 > 0) {
multiplier++;
}
if (multiplier > 5) {
return nMinFee + nMinFee * 10 + (multiplier - 5) * nMinFee * 4;
} else {
return nMinFee + multiplier * nMinFee * 2;
}
}
/**
* @brief transaction_new
* create new transaction
* @author gaochun
* @email gaochun@dabank.io
* @date 2021/2/6 13:21:26
* @param LWSProtocol * protocol
* @param const unsigned char * address
* @param const TxVchData * vch_data
* @return static Transaction *
*/
static Transaction *transaction_new(LWSProtocol *protocol, const unsigned char *address, const TxVchData *vch_data)
{
size_t list_len = protocol->utxo_list->length;
struct UTXO *utxo = NULL;
struct UTXO *utxo2 = NULL;
if (0 == list_len) {
return NULL;
}
utxo = (struct UTXO *)protocol->utxo_list->data[0];
if (2 >= list_len) {
utxo2 = (struct UTXO *)protocol->utxo_list->data[1];
}
if (NULL == utxo) {
arraylist_clear(protocol->utxo_list);
return NULL;
}
arraylist_remove(protocol->utxo_list, 0);
if (utxo2 != NULL) {
arraylist_remove(protocol->utxo_list, 1);
}
// VchData
size_t uuid_session_size = 16;
size_t timestamp_session_size = 4;
size_t description_session_size = vch_data->desc_size + 1;
size_t user_data_session_size = vch_data->len;
size_t vch_data_len =
uuid_session_size + timestamp_session_size + description_session_size + user_data_session_size;
size_t fee = calc_tx_fee(vch_data_len);
utxo->amount = utxo->amount - fee;
if (utxo2 != NULL) {
utxo->amount += utxo2->amount;
}
protocol->next_amount = utxo->amount; // Set global amount
Transaction *tx = (Transaction *)malloc(sizeof(Transaction));
memset(tx, 0x00, sizeof(Transaction));
tx->version = 1;
tx->type = 0x00;
tx->timestamp = protocol->hook->hook_datetime_get(protocol->hook->hook_datetime_context);
tx->lock_until = 0;
// TODO: The protocol USES the hash of the previous block, but for now only forkid is used
// memcpy(tx->hash_anchor, G.last_block_hash, 32);
unsigned char fork[32];
protocol->hook->hook_fork_get(protocol->hook->hook_fork_context, fork);
memcpy(tx->hash_anchor, fork, 32);
reverse(tx->hash_anchor, 32);
tx->size0 = 1;
if (utxo2 != NULL) {
tx->size0 = 2;
}
int len = sizeof(unsigned char) * (32 + 1) * tx->size0;
unsigned char *input = (unsigned char *)malloc(len);
memcpy(input, utxo->txid, 32);
memcpy(input + 32, &utxo->out, 1);
if (utxo2 != NULL) {
memcpy(input + 33, utxo2->txid, 32);
memcpy(input + 65, &utxo2->out, 1);
}
tx->input = input;
tx->prefix = 1;
if (address) {
memcpy(tx->address, address, sizeof(tx->address));
}
tx->tx_fee = fee;
tx->amount = utxo->amount;
unsigned char *data = (unsigned char *)malloc(vch_data_len);
size_t size = 0;
size_t size_thing = sizeof(vch_data->uuid);
serialize_join(&size, (void *)vch_data->uuid, size_thing, data);
size_thing = sizeof(vch_data->timestamp);
serialize_join(&size, (void *)vch_data->timestamp, size_thing, data);
unsigned char uc8 = vch_data->desc_size;
serialize_join(&size, &uc8, 1, data);
if (uc8 > 0) {
size_thing = vch_data->desc_size;
serialize_join(&size, vch_data->desc, size_thing, data);
}
size_thing = vch_data->len;
serialize_join(&size, vch_data->data, size_thing, data);
tx->size1 = size;
tx->vch_data = data;
tx->size2 = 64;
return tx;
}
/**
* @brief transaction delete
* Destroy a transaction instance
* @author gaochun
* @email gaochun@dabank.io
* @date 2019/11/19 17:30:47
* @param LwsClient * lws_client -LWS client
* @param Transaction * tx -transaction instance
* @return void
*/
static void transaction_delete(Transaction *tx)
{
if (tx) {
if (tx->vch_data) {
free(tx->vch_data);
}
if (tx->input) {
free(tx->input);
}
free(tx);
}
}
/**
* @brief transaction_serialize_without_sign
* Serialize Transaction to byte stream without sign
* @author gaochun
* @email gaochun@dabank.io
* @date 2019/11/19 17:15:46
* @param Transaction * tx -input transaction instance
* @param unsigned char * data -output byte stream
* @return static size_t -byte stream length
*/
static size_t transaction_serialize_without_sign(Transaction *tx, unsigned char *data)
{