Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/app/process/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ upgrade_status_t get_g_upgrade_status()

void set_g_upgrade_status(upgrade_status_t upgrade_status)
{
g_upgrade_status_mutex.lock();
SafeLock sl(g_upgrade_status_mutex);
sl.lock();
if (g_upgrade_status == upgrade_status)
{
return;
}
g_upgrade_status = upgrade_status;
switch(g_upgrade_status)
{
Expand All @@ -118,7 +123,7 @@ void set_g_upgrade_status(upgrade_status_t upgrade_status)
default:
p_log->warn("Unknown upgrade status!\n");
}
g_upgrade_status_mutex.unlock();
sl.unlock();

if (UPGRADE_STATUS_NONE == get_g_upgrade_status())
{
Expand Down
1 change: 1 addition & 0 deletions src/app/process/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mutex>

#include "Resource.h"
#include "SafeLock.h"
#include "Log.h"

#if defined(__cplusplus)
Expand Down
27 changes: 27 additions & 0 deletions src/app/utils/SafeLock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "SafeLock.h"

SafeLock::~SafeLock()
{
if (this->lock_time > 0)
{
this->_mutex.unlock();
}
}

void SafeLock::lock()
{
if (this->lock_time == 0)
{
this->_mutex.lock();
this->lock_time++;
}
}

void SafeLock::unlock()
{
if (this->lock_time > 0)
{
this->_mutex.unlock();
this->lock_time--;
}
}
20 changes: 20 additions & 0 deletions src/app/utils/SafeLock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _CRUST_SAFELOCK_H_
#define _CRUST_SAFELOCK_H_

#include <mutex>

class SafeLock
{
public:
SafeLock(std::mutex &mutex): _mutex(mutex), lock_time(0) {}
~SafeLock();
void lock();
void unlock();

private:
std::mutex &_mutex;
int lock_time;
};


#endif /* !_CRUST_SAFELOCK_H_ */
2 changes: 1 addition & 1 deletion src/enclave/identity/Identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ crust_status_t id_gen_upgrade_data(size_t block_height)
// TODO: Wait a random time:[10, 50] block time
sgx_read_rand(reinterpret_cast<uint8_t *>(&random_time), sizeof(size_t));
random_time = ((random_time % (UPGRADE_WAIT_BLOCK_MAX - UPGRADE_WAIT_BLOCK_MIN + 1)) + UPGRADE_WAIT_BLOCK_MIN) * BLOCK_TIME_BASE;
log_info("Upgrade: generate workreport successfully!Will send after %ld seconds...\n", random_time);
log_info("Upgrade: generate workreport successfully!Will send after %ld blocks...\n", random_time / BLOCK_TIME_BASE);
ocall_usleep(random_time * 1000000);
ocall_upload_workreport(&crust_status, work_report.c_str());
if (CRUST_SUCCESS != crust_status)
Expand Down
49 changes: 27 additions & 22 deletions src/enclave/report/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,37 @@ std::string get_generated_work_report()
crust_status_t get_signed_work_report(const char *block_hash, size_t block_height, bool locked /*=true*/)
{
Workload *wl = Workload::get_instance();
// Judge whether the current data is validated
if (!wl->report_has_validated_proof())
{
return CRUST_WORK_REPORT_NOT_VALIDATED;
}

crust_status_t crust_status = CRUST_SUCCESS;
// Judge whether block height is expired
if (block_height == 0 || block_height - wl->get_report_height() < ERA_LENGTH)
{
return CRUST_BLOCK_HEIGHT_EXPIRED;
}

// The first report after restart will not be processed
if (wl->get_restart_flag())
{
wl->set_restart_flag(false);
wl->set_report_file_flag(true);
return CRUST_FIRST_WORK_REPORT_AFTER_REPORT;
// The first report after restart will not be processed
crust_status = CRUST_FIRST_WORK_REPORT_AFTER_REPORT;
}
else if (!wl->report_has_validated_proof())
{
// Judge whether the current data is validated
crust_status = CRUST_WORK_REPORT_NOT_VALIDATED;
}
else if (!wl->get_report_file_flag())
{
// Have files and no karst
crust_status = CRUST_NO_KARST;
}

// Have files and no karst
if (!wl->get_report_file_flag())
// Don't meet workreport condition
if (CRUST_SUCCESS != crust_status)
{
wl->set_report_height(block_height);
wl->set_restart_flag(false);
wl->set_report_file_flag(true);
return CRUST_NO_KARST;
wl->report_reduce_validated_proof();
return crust_status;
}

if (locked)
Expand All @@ -59,7 +65,6 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh
}

ecc_key_pair id_key_pair = wl->get_key_pair();
crust_status_t crust_status = CRUST_SUCCESS;
sgx_status_t sgx_status;
size_t hashs_len = 0;
size_t files_buffer_len = 0;
Expand Down Expand Up @@ -337,12 +342,6 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh
store_large_data(reinterpret_cast<const uint8_t *>(wr_str.c_str()), wr_str.size(), ocall_store_workreport, wl->ocall_wr_mutex);
g_work_report = wr_str;

// Reset meaningful data
wl->set_report_file_flag(true);

// Set report height
wl->set_report_height(block_height);


cleanup:
if (locked)
Expand All @@ -355,9 +354,15 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh
sgx_ecc256_close_context(ecc_state);
}

free(p_sigbuf);
if (p_sigbuf != NULL)
{
free(p_sigbuf);
}

wl->report_reduce_validated_proof();
wl->set_report_height(block_height);
wl->set_restart_flag(false);
wl->set_report_file_flag(true);
wl->report_reduce_validated_proof();

return crust_status;
}
22 changes: 13 additions & 9 deletions src/enclave/storage/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ crust_status_t storage_seal_file(const char *p_tree, size_t tree_len, const char
wl->add_new_file(file_entry_json);

// Add info in workload spec
wl->set_wl_spec(WL_SPEC_FILE_UNCONFIRMED_SIZE, file_entry_json[FILE_SIZE].ToInt());
wl->set_wl_spec(WL_SPEC_FILE_UNCONFIRMED_SIZE, file_entry_json[FILE_OLD_SIZE].ToInt());

return crust_status;
}
Expand Down Expand Up @@ -482,7 +482,7 @@ crust_status_t storage_confirm_file(const char *hash)
// Set workload spec information
if (is_confirmed)
{
wl->set_wl_spec(WL_SPEC_FILE_VALID_SIZE, WL_SPEC_FILE_UNCONFIRMED_SIZE, confirmed_file[FILE_SIZE].ToInt());
wl->set_wl_spec(WL_SPEC_FILE_VALID_SIZE, WL_SPEC_FILE_UNCONFIRMED_SIZE, confirmed_file[FILE_OLD_SIZE].ToInt());
}

return crust_status;
Expand All @@ -496,7 +496,7 @@ crust_status_t storage_confirm_file(const char *hash)
crust_status_t storage_delete_file(const char *hash)
{
// ----- Delete file items in metadata ----- //
std::string deleted_file;
json::JSON deleted_file;
crust_status_t crust_status = CRUST_SUCCESS;

// ----- Delete file items in checked_files ----- //
Expand All @@ -508,8 +508,8 @@ crust_status_t storage_delete_file(const char *hash)
std::string hash_str = wl->checked_files[i][FILE_HASH].ToString();
if (hash_str.compare(hash) == 0)
{
deleted_file = wl->checked_files[i];
wl->checked_files[i][FILE_STATUS].set_char(CURRENT_STATUS, FILE_STATUS_DELETED);
deleted_file = hash_str;
is_deleted = true;
break;
}
Expand All @@ -523,7 +523,7 @@ crust_status_t storage_delete_file(const char *hash)
std::string hash_str = (*it)[FILE_HASH].ToString();
if (hash_str.compare(hash) == 0)
{
deleted_file = hash_str;
deleted_file = *it;
wl->new_files.erase((++it).base());
is_deleted = true;
break;
Expand All @@ -535,20 +535,24 @@ crust_status_t storage_delete_file(const char *hash)
// Print deleted info
if (is_deleted)
{
log_info("Delete file:%s successfully!\n", deleted_file.c_str());
log_info("Delete file:%s successfully!\n", deleted_file[FILE_HASH].ToString().c_str());
}
else
{
log_warn("Delete file:%s failed(not found)!\n", deleted_file.c_str());
log_warn("Delete file:%s failed(not found)!\n", deleted_file[FILE_HASH].ToString().c_str());
}

// ----- Delete file related data ----- //
if (is_deleted)
{
// Delete file tree structure
persist_del(deleted_file);
persist_del(deleted_file[FILE_HASH].ToString());
// Delete file metadata
persist_del(deleted_file + "_meta");
persist_del(deleted_file[FILE_HASH].ToString() + "_meta");
// Update workload spec info
std::pair<wl_spec_t, wl_spec_t> wl_p;
wl->get_wl_spec_by_file_status(deleted_file[FILE_STATUS].get_char(CURRENT_STATUS), wl_p);
wl->set_wl_spec(wl_p.second, -deleted_file[FILE_OLD_SIZE].ToInt());
}

return crust_status;
Expand Down
3 changes: 1 addition & 2 deletions test/integration/build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,7 @@ function data_h_test
function enc_report_cpp_test()
{
local pos=$(sed -n '/crust_status_t get_signed_work_report(/=' $enclave_report_cpp)
sed -i "$((pos+2)),$((pos+13))d" $enclave_report_cpp
sed -i "$((pos+2)) a \\\tWorkload *wl = Workload::get_instance();" $enclave_report_cpp
sed -i "$((pos+4)),$((pos+34))d" $enclave_report_cpp
}

########## enc_validate_cpp_test ##########
Expand Down