Working with nodes to add protobuf to persist to db

adding a link to a node does a realloc. I believe this is causing
problems. I am going to replace this and see if that fixes the issue.
This commit is contained in:
jmjatlanta 2016-12-12 15:06:17 -05:00
parent e0b0552b39
commit 4fe768c2c5
8 changed files with 682 additions and 244 deletions

View file

@ -40,11 +40,12 @@ int test_merkledag_get_data() {
}
// create a node
struct Node* node1 = N_Create_From_Data(binary_data, binary_data_size);
struct Node* node1;
retVal = ipfs_node_new_from_data(binary_data, binary_data_size, &node1);
retVal = ipfs_merkledag_add(node1, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
ipfs_node_free(node1);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
@ -53,15 +54,15 @@ int test_merkledag_get_data() {
struct Node* results_node;
retVal = ipfs_merkledag_get(node1->cached, &results_node, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
Node_Delete(results_node);
ipfs_node_free(node1);
ipfs_node_free(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
if (results_node->data_size != 256) {
Node_Delete(node1);
Node_Delete(results_node);
ipfs_node_free(node1);
ipfs_node_free(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
@ -69,15 +70,15 @@ int test_merkledag_get_data() {
// the data should be the same
for(int i = 0; i < results_node->data_size; i++) {
if (results_node->data[i] != node1->data[i]) {
Node_Delete(node1);
Node_Delete(results_node);
ipfs_node_free(node1);
ipfs_node_free(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
}
Node_Delete(node1);
Node_Delete(results_node);
ipfs_node_free(node1);
ipfs_node_free(results_node);
ipfs_repo_fsrepo_free(fs_repo);
return retVal;
@ -86,7 +87,7 @@ int test_merkledag_get_data() {
int test_merkledag_add_data() {
int retVal = 0;
struct FSRepo* fs_repo = createAndOpenRepo("/tmp.ipfs");
struct FSRepo* fs_repo = createAndOpenRepo("/tmp/.ipfs");
if (fs_repo == NULL)
return 0;
@ -101,11 +102,12 @@ int test_merkledag_add_data() {
}
// create a node
struct Node* node1 = N_Create_From_Data(binary_data, binary_data_size);
struct Node* node1;
retVal = ipfs_node_new_from_data(binary_data, binary_data_size, &node1);
retVal = ipfs_merkledag_add(node1, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
ipfs_node_free(node1);
return 0;
}
@ -115,30 +117,31 @@ int test_merkledag_add_data() {
int first_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
if (first_add_size == start_file_size) { // uh oh, database should have increased in size
Node_Delete(node1);
ipfs_node_free(node1);
return 0;
}
// adding the same binary again should do nothing (the hash should be the same)
struct Node* node2 = N_Create_From_Data(binary_data, binary_data_size);
struct Node* node2;
retVal = ipfs_node_new_from_data(binary_data, binary_data_size, &node2);
retVal = ipfs_merkledag_add(node2, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
Node_Delete(node2);
ipfs_node_free(node1);
ipfs_node_free(node2);
return 0;
}
// make sure everything is correct
if (node2->cached == NULL) {
Node_Delete(node1);
Node_Delete(node2);
ipfs_node_free(node1);
ipfs_node_free(node2);
return 0;
}
for(int i = 0; i < node1->cached->hash_length; i++) {
if (node1->cached->hash[i] != node2->cached->hash[i]) {
printf("hash of node1 does not match node2 at position %d\n", i);
Node_Delete(node1);
Node_Delete(node2);
ipfs_node_free(node1);
ipfs_node_free(node2);
return 0;
}
}
@ -146,35 +149,36 @@ int test_merkledag_add_data() {
int second_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
if (first_add_size != second_add_size) { // uh oh, the database shouldn't have changed size
printf("looks as if a new record was added when it shouldn't have. Old file size: %d, new file size: %d\n", first_add_size, second_add_size);
Node_Delete(node1);
Node_Delete(node2);
ipfs_node_free(node1);
ipfs_node_free(node2);
return 0;
}
// now change 1 byte, which should change the hash
binary_data[10] = 0;
// create a node
struct Node* node3 = N_Create_From_Data(binary_data, binary_data_size);
struct Node* node3;
retVal = ipfs_node_new_from_data(binary_data, binary_data_size, &node3);
retVal = ipfs_merkledag_add(node3, fs_repo);
if (retVal == 0) {
Node_Delete(node1);
Node_Delete(node2);
Node_Delete(node3);
ipfs_node_free(node1);
ipfs_node_free(node2);
ipfs_node_free(node3);
return 0;
}
// make sure everything is correct
if (node3->cached == NULL) {
Node_Delete(node1);
Node_Delete(node2);
Node_Delete(node3);
ipfs_node_free(node1);
ipfs_node_free(node2);
ipfs_node_free(node3);
return 0;
}
Node_Delete(node1);
Node_Delete(node2);
Node_Delete(node3);
ipfs_node_free(node1);
ipfs_node_free(node2);
ipfs_node_free(node3);
int third_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
if (third_add_size == second_add_size || third_add_size < second_add_size) {// uh oh, it didn't add it
printf("Node 3 should have been added, but the file size did not change from %d.\n", third_add_size);
@ -201,46 +205,47 @@ int test_merkledag_add_node_with_links() {
}
// make link
link = Create_Link("", "abc123");
node1 = N_Create_From_Link(link);
retVal = ipfs_node_link_new("", "abc123", &link);
retVal = ipfs_node_new_from_link(link, &node1);
retVal = ipfs_merkledag_add(node1, fs_repo);
if (retVal == 0) {
ipfs_repo_fsrepo_free(fs_repo);
Node_Delete(node1);
ipfs_node_free(node1);
printf("Unable to add node\n");
return 0;
}
// now look for it
struct Node* node2 = NULL;
retVal = ipfs_merkledag_get(node1->cached, &node2, fs_repo);
if (retVal == 0) {
ipfs_repo_fsrepo_free(fs_repo);
Node_Delete(node1);
ipfs_node_free(node1);
return 0;
}
if (node2->link_amount != node1->link_amount) {
printf("Link amount %d does not match %d\n", node2->link_amount, node1->link_amount);
ipfs_repo_fsrepo_free(fs_repo);
Node_Delete(node1);
Node_Delete(node2);
printf("Link number do not match. Should be %d and are %d\n", node1->link_amount, node2->link_amount);
ipfs_node_free(node1);
ipfs_node_free(node2);
return 0;
}
// make sure hashes match
for(int i = 0; i < node1->links[0]->Lcid->hash_length; i++) {
if(node1->links[0]->Lcid->hash[i] != node2->links[0]->Lcid->hash[i]) {
ipfs_repo_fsrepo_free(fs_repo);
Node_Delete(node1);
Node_Delete(node2);
for(int i = 0; i < node1->links[0]->cid->hash_length; i++) {
if(node1->links[0]->cid->hash[i] != node2->links[0]->cid->hash[i]) {
printf("Hashes do not match\n");
ipfs_repo_fsrepo_free(fs_repo);
ipfs_node_free(node1);
ipfs_node_free(node2);
return 0;
}
}
Node_Delete(node1);
Node_Delete(node2);
ipfs_node_free(node1);
ipfs_node_free(node2);
ipfs_repo_fsrepo_free(fs_repo);
return 1;