bool CBAddressStorageSaveAddress(uint64_t iself, void * address){ CBAddressStore * self = (CBAddressStore *)iself; CBNetworkAddress * addrObj = address; // Create key memcpy(CB_ADDRESS_KEY + 1, CBByteArrayGetData(addrObj->ip), 16); CBInt16ToArray(CB_ADDRESS_KEY, 17, addrObj->port); // Create data CBInt64ToArray(CB_DATA_ARRAY, 0, addrObj->lastSeen); CBInt64ToArray(CB_DATA_ARRAY, 8, (uint64_t) addrObj->services); CBInt32ToArray(CB_DATA_ARRAY, 16, addrObj->penalty); // Write data if (NOT CBDatabaseWriteValue(CBGetDatabase(self), CB_ADDRESS_KEY, CB_DATA_ARRAY, 20)) { CBLogError("Could not write an address to storage."); return false; } // Increase the number of addresses CBInt32ToArray(CB_DATA_ARRAY, 0, --self->numAddresses); if (NOT CBDatabaseWriteValue(CBGetDatabase(self), CB_ADDR_NUM_KEY, CB_DATA_ARRAY, 4)) { CBLogError("Could not write the new number of addresses to storage."); return false; } // Commit changes if (NOT CBDatabaseCommit(CBGetDatabase(self))) { CBLogError("Could not commit adding a new network address to storage."); return false; } return true; }
bool CBAddressStorageDeleteAddress(uint64_t iself, void * address){ CBAddressStore * self = (CBAddressStore *)iself; CBNetworkAddress * addrObj = address; memcpy(CB_ADDRESS_KEY + 1, CBByteArrayGetData(addrObj->ip), 16); CBInt16ToArray(CB_ADDRESS_KEY, 17, addrObj->port); // Remove address if (NOT CBDatabaseRemoveValue(CBGetDatabase(self), CB_ADDRESS_KEY)) { CBLogError("Could not remove an address from storage."); return false; } // Decrease number of addresses CBInt32ToArray(CB_DATA_ARRAY, 0, --self->numAddresses); if (NOT CBDatabaseWriteValue(CBGetDatabase(self), CB_ADDR_NUM_KEY, CB_DATA_ARRAY, 4)) { CBLogError("Could not write the new number of addresses to storage."); return false; } // Commit changes if (NOT CBDatabaseCommit(CBGetDatabase(self))) { CBLogError("Could not commit the removal of a network address."); return false; } return true; }
uint64_t CBNewAddressStorage(char * dataDir){ CBAddressStore * self = malloc(sizeof(*self)); if (NOT self) { CBLogError("Could not create the address storage object."); return 0; } if (NOT CBInitDatabase(CBGetDatabase(self), dataDir, "addr")) { CBLogError("Could not create a database object for address storage."); free(self); return 0; } if (CBDatabaseGetLength(CBGetDatabase(self), CB_ADDR_NUM_KEY)) { // Get the number of addresses if (NOT CBDatabaseReadValue(CBGetDatabase(self), CB_ADDR_NUM_KEY, CB_DATA_ARRAY, 4, 0)) { CBLogError("Could not read the number of addresses from storage."); free(self); return 0; } self->numAddresses = CBArrayToInt32(CB_DATA_ARRAY, 0); }else{ self->numAddresses = 0; // Insert zero number of addresses. CBInt32ToArray(CB_DATA_ARRAY, 0, 0); if (NOT CBDatabaseWriteValue(CBGetDatabase(self), CB_ADDR_NUM_KEY, CB_DATA_ARRAY, 4)) { CBLogError("Could not write the initial number of addresses to storage."); free(self); return 0; } // Commit changes if (NOT CBDatabaseCommit(CBGetDatabase(self))) { CBLogError("Could not commit the initial number of addresses to storage."); free(self); return 0; } } return (uint64_t) self; }
bool CBBlockChainStorageCommitData(uint64_t iself){ return CBDatabaseCommit((CBDatabase *)iself); }