Skip to content

Commit

Permalink
punish no karst (crustio#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
LowEntropyBody authored Sep 23, 2020
1 parent 0644cd5 commit b103890
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/app/include/CrustStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef enum _crust_status_t
CRUST_BLOCK_HEIGHT_EXPIRED = CRUST_MK_ERROR(0x2000),
CRUST_FIRST_WORK_REPORT_AFTER_REPORT = CRUST_MK_ERROR(0x2001),
CRUST_WORK_REPORT_NOT_VALIDATED = CRUST_MK_ERROR(0x2002),
CRUST_NO_KARST = CRUST_MK_ERROR(0x2003),
// Failed
CRUST_MALLOC_FAILED = CRUST_MK_ERROR(0x4000),
CRUST_SEAL_DATA_FAILED = CRUST_MK_ERROR(0x4001),
Expand Down
4 changes: 4 additions & 0 deletions src/app/process/WorkReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ void work_report_loop(void)
{
p_log->info("Can't generate work report for the first time after restart\n");
}
else if (crust_status == CRUST_NO_KARST)
{
p_log->info("Can't generate work report. You have meaningful files, please start karst\n");
}
else
{
p_log->err("Get signed validation report failed! Error code: %x\n", crust_status);
Expand Down
1 change: 1 addition & 0 deletions src/enclave/include/CrustStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef enum _crust_status_t
CRUST_BLOCK_HEIGHT_EXPIRED = CRUST_MK_ERROR(0x2000),
CRUST_FIRST_WORK_REPORT_AFTER_REPORT = CRUST_MK_ERROR(0x2001),
CRUST_WORK_REPORT_NOT_VALIDATED = CRUST_MK_ERROR(0x2002),
CRUST_NO_KARST = CRUST_MK_ERROR(0x2003),
// Failed
CRUST_MALLOC_FAILED = CRUST_MK_ERROR(0x4000),
CRUST_SEAL_DATA_FAILED = CRUST_MK_ERROR(0x4001),
Expand Down
42 changes: 24 additions & 18 deletions src/enclave/report/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,23 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh
}
id_set_report_slot((block_height - 1)/ERA_LENGTH + 1);


Workload *wl = Workload::get_instance();
// The first report after restart will not be processed
if (id_just_after_restart())
{
id_set_just_after_restart(false);
wl->set_report_flag(true);
return CRUST_FIRST_WORK_REPORT_AFTER_REPORT;
}

Workload *wl = Workload::get_instance();
// Have files and no karst
if (!wl->get_report_flag())
{
wl->set_report_flag(true);
return CRUST_NO_KARST;
}

ecc_key_pair id_key_pair = id_get_key_pair();
crust_status_t crust_status = CRUST_SUCCESS;
sgx_status_t sgx_status;
Expand Down Expand Up @@ -125,27 +134,24 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh

// ----- Get files info ----- //
std::string old_files = "[";
if (wl->get_report_flag())
sgx_thread_mutex_lock(&g_checked_files_mutex);
for (uint32_t i = 0; i < wl->checked_files.size(); i++)
{
sgx_thread_mutex_lock(&g_checked_files_mutex);
for (uint32_t i = 0; i < wl->checked_files.size(); i++)
if (wl->checked_files[i][FILE_STATUS].ToString().compare(FILE_STATUS_VALID) != 0)
{
continue;
}

old_files.append("{\"").append(FILE_HASH).append("\":")
.append("\"").append(wl->checked_files[i][FILE_OLD_HASH].ToString()).append("\",");
old_files.append("\"").append(FILE_SIZE).append("\":")
.append(std::to_string(wl->checked_files[i][FILE_OLD_SIZE].ToInt())).append("}");
if (i != wl->checked_files.size() - 1)
{
if (wl->checked_files[i][FILE_STATUS].ToString().compare(FILE_STATUS_VALID) != 0)
{
continue;
}

old_files.append("{\"").append(FILE_HASH).append("\":")
.append("\"").append(wl->checked_files[i][FILE_OLD_HASH].ToString()).append("\",");
old_files.append("\"").append(FILE_SIZE).append("\":")
.append(std::to_string(wl->checked_files[i][FILE_OLD_SIZE].ToInt())).append("}");
if (i != wl->checked_files.size() - 1)
{
old_files.append(",");
}
old_files.append(",");
}
sgx_thread_mutex_unlock(&g_checked_files_mutex);
}
sgx_thread_mutex_unlock(&g_checked_files_mutex);
old_files.append("]");

// ----- Create signature data ----- //
Expand Down
5 changes: 4 additions & 1 deletion src/enclave/validator/Validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ void validate_meaningful_file()
ocall_validate_init(&crust_status);
if (CRUST_SUCCESS != crust_status)
{
wl->set_report_flag(false);
if(wl->checked_files.size() != 0)
{
wl->set_report_flag(false);
}
ocall_validate_close();
sgx_thread_mutex_unlock(&g_checked_files_mutex);
return;
Expand Down
8 changes: 7 additions & 1 deletion src/enclave/workload/Workload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sgx_thread_mutex_t g_srd_mutex = SGX_THREAD_MUTEX_INITIALIZER;
sgx_thread_mutex_t g_checked_files_mutex = SGX_THREAD_MUTEX_INITIALIZER;
sgx_thread_mutex_t g_new_files_mutex = SGX_THREAD_MUTEX_INITIALIZER;
sgx_thread_mutex_t g_order_files_mutex = SGX_THREAD_MUTEX_INITIALIZER;
sgx_thread_mutex_t g_report_flag_mutex = SGX_THREAD_MUTEX_INITIALIZER;

Workload *Workload::workload = NULL;

Expand Down Expand Up @@ -320,7 +321,9 @@ void Workload::add_order_file(std::pair<std::string, size_t> file)
*/
void Workload::set_report_flag(bool flag)
{
sgx_thread_mutex_lock(&g_report_flag_mutex);
this->report_files = flag;
sgx_thread_mutex_unlock(&g_report_flag_mutex);
}

/**
Expand All @@ -329,7 +332,10 @@ void Workload::set_report_flag(bool flag)
*/
bool Workload::get_report_flag()
{
return this->report_files;
sgx_thread_mutex_lock(&g_report_flag_mutex);
bool flag = this->report_files;
sgx_thread_mutex_unlock(&g_report_flag_mutex);
return flag;
}

/**
Expand Down

0 comments on commit b103890

Please sign in to comment.