Skip to content

Commit

Permalink
Fix persist_set previous data error (crustio#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCode2012 authored Sep 21, 2020
1 parent 88ae17c commit f19bfe2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
38 changes: 38 additions & 0 deletions src/enclave/persistence/Persistence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

using namespace std;

void inner_ocall_persist_get(crust_status_t* crust_status, const char *key, uint8_t **value, size_t *value_len);

/**
* @description: Add value by key
* @param key -> Pointer to key
Expand Down Expand Up @@ -129,6 +131,24 @@ crust_status_t persist_set(std::string key, const uint8_t *value, size_t value_l
}
else
{
// Delete old data
std::string sum_key = key + "_sum";
uint8_t *p_sum_key = NULL;
size_t sum_key_len = 0;
inner_ocall_persist_get(&crust_status, sum_key.c_str(), &p_sum_key, &sum_key_len);
if (CRUST_SUCCESS == crust_status)
{
json::JSON sum_json = json::JSON::Load(std::string(reinterpret_cast<char *>(p_sum_key), sum_key_len));
free(p_sum_key);
uint32_t piece_num = sum_json[PERSIST_SUM].ToInt();
for (uint32_t i = 0; i < piece_num; i++)
{
std::string del_key = key + "_" + std::to_string(i);
persist_del(del_key);
}
persist_del(sum_key);
}
// Set new data
ocall_persist_set(&crust_status, key.c_str(), (uint8_t *)p_sealed_data, sealed_data_size);
}

Expand Down Expand Up @@ -178,6 +198,24 @@ crust_status_t persist_set_unsafe(std::string key, const uint8_t *value, size_t
}
else
{
// Delete old data
std::string sum_key = key + "_sum";
uint8_t *p_sum_key = NULL;
size_t sum_key_len = 0;
inner_ocall_persist_get(&crust_status, sum_key.c_str(), &p_sum_key, &sum_key_len);
if (CRUST_SUCCESS == crust_status)
{
json::JSON sum_json = json::JSON::Load(std::string(reinterpret_cast<char *>(p_sum_key), sum_key_len));
free(p_sum_key);
uint32_t piece_num = sum_json[PERSIST_SUM].ToInt();
for (uint32_t i = 0; i < piece_num; i++)
{
std::string del_key = key + "_" + std::to_string(i);
persist_del(del_key);
}
persist_del(sum_key);
}
// Set new data
ocall_persist_set(&crust_status, key.c_str(), value, value_len);
}

Expand Down
2 changes: 2 additions & 0 deletions src/enclave/storage/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ crust_status_t storage_seal_file(const char *p_tree, size_t tree_len, const char
file_entry_json[FILE_BLOCK_NUM] = block_num;
// Status indicates current new file's status, which must be one of valid, lost and unconfirmed
file_entry_json[FILE_STATUS] = FILE_STATUS_UNCONFIRMED;
free(new_root_hash_u);
free(org_root_hash_u);

// Store new tree structure
crust_status = persist_set_unsafe(new_root_hash_str, (const uint8_t*)new_tree.c_str(), new_tree.size());
Expand Down
6 changes: 5 additions & 1 deletion src/enclave/workload/Workload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,9 @@ void Workload::set_srd_info(std::string path, long change)
*/
json::JSON Workload::get_srd_info()
{
return this->srd_info_json;
sgx_thread_mutex_lock(&this->srd_info_mutex);
json::JSON srd_info = this->srd_info_json;
sgx_thread_mutex_unlock(&this->srd_info_mutex);

return srd_info;
}
15 changes: 13 additions & 2 deletions test/integration/performance/run_test.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#!/bin/bash
function _seal()
{
echo 0 > $sealsyncfile
file_num_g=0
local total_num=$(get_config ".performance|.file_num")
while true; do
sleep 3
if [ $file_num_g -lt $total_num ]; then
local num=1000
while [ $num -ge 0 ]; do
_seal_r
while [ $num -ge 0 ] && [ $(cat $sealsyncfile) -lt 10 ]; do
(
flock -w 30 202
if [ $(cat $sealsyncfile) -lt 10 ]; then
_seal_r &
local cur_num=$(cat $sealsyncfile)
((cur_num++))
echo $cur_num > $sealsyncfile
fi
) 202>$seallockfile
((num--))
done
fi
Expand Down Expand Up @@ -295,6 +304,8 @@ srdresfile=$ptmpdir/srd_info
deletefilethres=3
deletetime=100
deletelockfile=$ptmpdir/deletelockfile
seallockfile=$ptmpdir/seallockfile
sealsyncfile=$ptmpdir/sealsyncfile

# Control sig num
sigShowInfo=28
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scripts/start_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fi
### Start crust-sworker
cd $testdir
rm -rf tee_base_path
rm -rf files/*
rm -rf files
verbose INFO "starting crust-sworker..." h
./bin/crust-sworker -c etc/Config.json --offline --debug &>$sworkerlog &
pid=$!
Expand Down
1 change: 1 addition & 0 deletions test/integration/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function seal_file()
{
local data_path=$1
local store_path=$2
store_path=${store_path}.$RANDOM
local tmp_file=tmp_file.$RANDOM

### Split file
Expand Down

0 comments on commit f19bfe2

Please sign in to comment.