171 lines
4.8 KiB
C
Raw Normal View History

2024-02-17 06:36:08 +00:00
#include <stdint.h>
#include <stdio.h>
2024-03-04 16:28:35 +05:30
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
2024-02-17 06:36:08 +00:00
#include "../negentropy_wrapper.h"
#define MAX_FRAME_SIZE 153600
2024-03-05 11:27:30 +05:30
void printHexBuffer(buffer buf){
for (uint64_t i = 0; i < buf.len; ++i) {
printf("%0hhx", buf.data[i]);
}
printf("\n");
}
void rec_callback(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output){
printf("needIds count:%llu , haveIds count: %llu \n",need_ids_len, have_ids_len);
for (int i=0; i < need_ids_len ; i++) {
printf("need ID at %d :", i);
printHexBuffer(need_ids[i]);
}
for (int j=0; j < have_ids_len ; j++) {
printf("need ID at %d :", j);
printHexBuffer(have_ids[j]);
}
}
2024-03-04 16:28:35 +05:30
2024-02-17 06:36:08 +00:00
int main(){
2024-03-04 16:28:35 +05:30
void* st1 = storage_new("","");
if(st1 == NULL){
2024-02-17 06:36:08 +00:00
perror("failed to create storage");
}
2024-04-30 14:09:11 +05:30
2024-02-17 06:36:08 +00:00
2024-03-04 16:28:35 +05:30
void* st2 = storage_new("","");
if(st2 == NULL){
perror("failed to create storage");
}
2024-04-30 14:09:11 +05:30
2024-03-04 16:28:35 +05:30
unsigned char m1[] = {0x6a, 0xdf, 0xaa, 0xe0, 0x31, 0xeb, 0x61, 0xa8, \
0x3c, 0xff, 0x9c, 0xfd, 0xd2, 0xae, 0xf6, 0xed, \
0x63, 0xda, 0xcf, 0xaa, 0x96, 0xd0, 0x51, 0x26, \
0x7e, 0xf1, 0x0c, 0x8b, 0x61, 0xae, 0x35, 0xe9};//"61dfaae031eb61a83cff9cfdd2aef6ed63dacfaa96d051267ef10c8b61ae35e9";
buffer b1 ;
b1.len = 32;
b1.data = m1;
2024-03-05 11:27:30 +05:30
unsigned char m2[] = {0x28 ,0x79 ,0x8d ,0x29 ,0x5c ,0x30 ,0xc7 ,0xe6 \
,0xd9 ,0xa4 ,0xa9 ,0x6c ,0xdd ,0xa7 ,0xe0 ,0x20 \
,0xf7 ,0xaa ,0x71 ,0x68 ,0xcc ,0xe0 ,0x63 ,0x30 \
,0x2e ,0xd1 ,0x9b ,0x85 ,0x63 ,0x32 ,0x95 ,0x9e}; //28798d295c30c7e6d9a4a96cdda7e020f7aa7168cce063302ed19b856332959e
2024-03-04 16:28:35 +05:30
buffer b2 ;
b2.len = 32;
2024-03-05 11:27:30 +05:30
b2.data = m2;
2024-03-04 16:28:35 +05:30
bool ret = storage_insert(st1,time(NULL),&b1);
if (ret){
printf("inserted hash successfully in st1\n");
}
2024-03-05 11:27:30 +05:30
2024-03-04 16:28:35 +05:30
ret = storage_insert(st2,time(NULL),&b2);
if (ret){
2024-03-05 11:27:30 +05:30
printf("inserted hash successfully in st2\n");
2024-03-04 16:28:35 +05:30
}
ret = storage_insert(st2,time(NULL),&b1);
if (ret){
2024-03-05 11:27:30 +05:30
printf("inserted hash successfully in st2\n");
2024-03-04 16:28:35 +05:30
}
2024-03-05 11:27:30 +05:30
buffer b4 ;
2024-03-13 15:43:05 +05:30
b4.len = 0;
b4.data = (unsigned char*)malloc(37*sizeof(unsigned char));
2024-03-11 14:01:21 +05:30
printf("storage size of st2 is %d \n",storage_size(st2));
2024-04-30 14:09:11 +05:30
void* subrange = subrange_new(st2, 0 , UINT64_MAX);
if (subrange == NULL){
perror("failed to init subrange");
return -1;
2024-04-30 14:09:11 +05:30
}
printf("subrange init successful with size %d \n ", subrange_size(subrange) );
2024-04-30 14:09:11 +05:30
void* subrange1 = subrange_new(st1, 0 , UINT64_MAX);
if (subrange == NULL){
perror("failed to init subrange");
return -1;
2024-04-30 14:09:11 +05:30
}
printf("subrange init successful with size %d \n ", subrange_size(subrange1) );
2024-04-30 14:09:11 +05:30
void* ngn_inst1 = negentropy_new(subrange1, MAX_FRAME_SIZE);
2024-04-30 14:09:11 +05:30
if(ngn_inst1 == NULL){
perror("failed to create negentropy instance");
return -1;
2024-04-30 14:09:11 +05:30
}
void* ngn_inst2 = negentropy_new(subrange, MAX_FRAME_SIZE);
2024-04-30 14:09:11 +05:30
if(ngn_inst2 == NULL){
perror("failed to create negentropy instance");
return -1;
2024-04-30 14:09:11 +05:30
}
2024-03-13 15:43:05 +05:30
result res;
2024-04-30 14:09:11 +05:30
int ret1 = negentropy_subrange_initiate(ngn_inst1, &res);
2024-03-13 15:43:05 +05:30
if(ret1 < 0){
2024-02-17 06:36:08 +00:00
perror("failed to initiate negentropy instance");
return -1;
2024-02-17 06:36:08 +00:00
}
2024-03-13 15:43:05 +05:30
printf("initiated negentropy successfully with output of len %llu \n", res.output.len);
b4.len = res.output.len;
memcpy(b4.data, res.output.data, res.output.len);
free_result(&res);
2024-03-05 11:27:30 +05:30
2024-03-04 16:28:35 +05:30
buffer b3 ;
2024-03-13 15:43:05 +05:30
b3.len = 0;
b3.data = (unsigned char*)malloc(69*sizeof(unsigned char));
2024-03-05 11:27:30 +05:30
2024-04-30 14:09:11 +05:30
ret1 = reconcile_subrange(ngn_inst2, &b4, &res);
2024-03-13 15:43:05 +05:30
if(ret1 < 0){
perror("error from reconcile");
2024-03-05 11:27:30 +05:30
}
2024-03-13 15:43:05 +05:30
if (res.output.len == 0){
perror("nothing to reconcile");
}
printf("reconcile returned with output of len %llu \n", res.output.len);
b3.len = res.output.len;
memcpy(b3.data, res.output.data, res.output.len);
free_result(&res);
//outSize = reconcile_with_ids(ngn_inst1, &b3, &rec_callback);
2024-03-13 15:43:05 +05:30
result res1;
2024-04-30 14:09:11 +05:30
reconcile_with_ids_subrange_no_cbk(ngn_inst1, &b3, &res1);
2024-03-13 15:43:05 +05:30
printf("needIds count:%llu , haveIds count: %llu \n",res1.need_ids_len, res1.have_ids_len);
2024-03-13 15:43:05 +05:30
for (int i=0; i < res1.need_ids_len ; i++) {
printf("need ID at %d :", i);
2024-03-13 15:43:05 +05:30
printHexBuffer(res1.need_ids[i]);
}
2024-03-13 15:43:05 +05:30
for (int j=0; j < res1.have_ids_len ; j++) {
printf("need ID at %d :", j);
2024-03-13 15:43:05 +05:30
printHexBuffer(res1.have_ids[j]);
}
2024-03-04 16:28:35 +05:30
2024-03-05 11:27:30 +05:30
free(b3.data);
free(b4.data);
2024-03-13 15:43:05 +05:30
free_result(&res1);
2024-03-15 15:24:33 +05:30
ret = storage_insert(st1, time(NULL), &b2);
if (ret){
printf("inserted hash successfully in st1\n");
}
printf("\n storage size after adding 1 more elem is %d, subrange size is %d \n", storage_size(st1), subrange_size(subrange1));
2024-04-30 14:09:11 +05:30
subrange_delete(subrange);
subrange_delete(subrange1);
printf("storage after subrange deletion, st1 size: %d, st2 size: %d.", storage_size(st1), storage_size(st2));
2024-03-11 14:01:21 +05:30
}