Skip to content

Commit b492e90

Browse files
authored
Fix workreport process (#248)
* Fix workreport process * Fix workload spec information bug
1 parent 8475635 commit b492e90

8 files changed

Lines changed: 97 additions & 36 deletions

File tree

src/app/process/Data.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ upgrade_status_t get_g_upgrade_status()
9393

9494
void set_g_upgrade_status(upgrade_status_t upgrade_status)
9595
{
96-
g_upgrade_status_mutex.lock();
96+
SafeLock sl(g_upgrade_status_mutex);
97+
sl.lock();
98+
if (g_upgrade_status == upgrade_status)
99+
{
100+
return;
101+
}
97102
g_upgrade_status = upgrade_status;
98103
switch(g_upgrade_status)
99104
{
@@ -118,7 +123,7 @@ void set_g_upgrade_status(upgrade_status_t upgrade_status)
118123
default:
119124
p_log->warn("Unknown upgrade status!\n");
120125
}
121-
g_upgrade_status_mutex.unlock();
126+
sl.unlock();
122127

123128
if (UPGRADE_STATUS_NONE == get_g_upgrade_status())
124129
{

src/app/process/Data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <mutex>
77

88
#include "Resource.h"
9+
#include "SafeLock.h"
910
#include "Log.h"
1011

1112
#if defined(__cplusplus)

src/app/utils/SafeLock.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "SafeLock.h"
2+
3+
SafeLock::~SafeLock()
4+
{
5+
if (this->lock_time > 0)
6+
{
7+
this->_mutex.unlock();
8+
}
9+
}
10+
11+
void SafeLock::lock()
12+
{
13+
if (this->lock_time == 0)
14+
{
15+
this->_mutex.lock();
16+
this->lock_time++;
17+
}
18+
}
19+
20+
void SafeLock::unlock()
21+
{
22+
if (this->lock_time > 0)
23+
{
24+
this->_mutex.unlock();
25+
this->lock_time--;
26+
}
27+
}

src/app/utils/SafeLock.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _CRUST_SAFELOCK_H_
2+
#define _CRUST_SAFELOCK_H_
3+
4+
#include <mutex>
5+
6+
class SafeLock
7+
{
8+
public:
9+
SafeLock(std::mutex &mutex): _mutex(mutex), lock_time(0) {}
10+
~SafeLock();
11+
void lock();
12+
void unlock();
13+
14+
private:
15+
std::mutex &_mutex;
16+
int lock_time;
17+
};
18+
19+
20+
#endif /* !_CRUST_SAFELOCK_H_ */

src/enclave/identity/Identity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ crust_status_t id_gen_upgrade_data(size_t block_height)
11451145
// TODO: Wait a random time:[10, 50] block time
11461146
sgx_read_rand(reinterpret_cast<uint8_t *>(&random_time), sizeof(size_t));
11471147
random_time = ((random_time % (UPGRADE_WAIT_BLOCK_MAX - UPGRADE_WAIT_BLOCK_MIN + 1)) + UPGRADE_WAIT_BLOCK_MIN) * BLOCK_TIME_BASE;
1148-
log_info("Upgrade: generate workreport successfully!Will send after %ld seconds...\n", random_time);
1148+
log_info("Upgrade: generate workreport successfully!Will send after %ld blocks...\n", random_time / BLOCK_TIME_BASE);
11491149
ocall_usleep(random_time * 1000000);
11501150
ocall_upload_workreport(&crust_status, work_report.c_str());
11511151
if (CRUST_SUCCESS != crust_status)

src/enclave/report/Report.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,37 @@ std::string get_generated_work_report()
2626
crust_status_t get_signed_work_report(const char *block_hash, size_t block_height, bool locked /*=true*/)
2727
{
2828
Workload *wl = Workload::get_instance();
29-
// Judge whether the current data is validated
30-
if (!wl->report_has_validated_proof())
31-
{
32-
return CRUST_WORK_REPORT_NOT_VALIDATED;
33-
}
34-
29+
crust_status_t crust_status = CRUST_SUCCESS;
3530
// Judge whether block height is expired
3631
if (block_height == 0 || block_height - wl->get_report_height() < ERA_LENGTH)
3732
{
3833
return CRUST_BLOCK_HEIGHT_EXPIRED;
3934
}
4035

41-
// The first report after restart will not be processed
4236
if (wl->get_restart_flag())
4337
{
44-
wl->set_restart_flag(false);
45-
wl->set_report_file_flag(true);
46-
return CRUST_FIRST_WORK_REPORT_AFTER_REPORT;
38+
// The first report after restart will not be processed
39+
crust_status = CRUST_FIRST_WORK_REPORT_AFTER_REPORT;
40+
}
41+
else if (!wl->report_has_validated_proof())
42+
{
43+
// Judge whether the current data is validated
44+
crust_status = CRUST_WORK_REPORT_NOT_VALIDATED;
45+
}
46+
else if (!wl->get_report_file_flag())
47+
{
48+
// Have files and no karst
49+
crust_status = CRUST_NO_KARST;
4750
}
4851

49-
// Have files and no karst
50-
if (!wl->get_report_file_flag())
52+
// Don't meet workreport condition
53+
if (CRUST_SUCCESS != crust_status)
5154
{
55+
wl->set_report_height(block_height);
56+
wl->set_restart_flag(false);
5257
wl->set_report_file_flag(true);
53-
return CRUST_NO_KARST;
58+
wl->report_reduce_validated_proof();
59+
return crust_status;
5460
}
5561

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

6167
ecc_key_pair id_key_pair = wl->get_key_pair();
62-
crust_status_t crust_status = CRUST_SUCCESS;
6368
sgx_status_t sgx_status;
6469
size_t hashs_len = 0;
6570
size_t files_buffer_len = 0;
@@ -337,12 +342,6 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh
337342
store_large_data(reinterpret_cast<const uint8_t *>(wr_str.c_str()), wr_str.size(), ocall_store_workreport, wl->ocall_wr_mutex);
338343
g_work_report = wr_str;
339344

340-
// Reset meaningful data
341-
wl->set_report_file_flag(true);
342-
343-
// Set report height
344-
wl->set_report_height(block_height);
345-
346345

347346
cleanup:
348347
if (locked)
@@ -355,9 +354,15 @@ crust_status_t get_signed_work_report(const char *block_hash, size_t block_heigh
355354
sgx_ecc256_close_context(ecc_state);
356355
}
357356

358-
free(p_sigbuf);
357+
if (p_sigbuf != NULL)
358+
{
359+
free(p_sigbuf);
360+
}
359361

360-
wl->report_reduce_validated_proof();
362+
wl->set_report_height(block_height);
361363
wl->set_restart_flag(false);
364+
wl->set_report_file_flag(true);
365+
wl->report_reduce_validated_proof();
366+
362367
return crust_status;
363368
}

src/enclave/storage/Storage.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ crust_status_t storage_seal_file(const char *p_tree, size_t tree_len, const char
118118
wl->add_new_file(file_entry_json);
119119

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

123123
return crust_status;
124124
}
@@ -482,7 +482,7 @@ crust_status_t storage_confirm_file(const char *hash)
482482
// Set workload spec information
483483
if (is_confirmed)
484484
{
485-
wl->set_wl_spec(WL_SPEC_FILE_VALID_SIZE, WL_SPEC_FILE_UNCONFIRMED_SIZE, confirmed_file[FILE_SIZE].ToInt());
485+
wl->set_wl_spec(WL_SPEC_FILE_VALID_SIZE, WL_SPEC_FILE_UNCONFIRMED_SIZE, confirmed_file[FILE_OLD_SIZE].ToInt());
486486
}
487487

488488
return crust_status;
@@ -496,7 +496,7 @@ crust_status_t storage_confirm_file(const char *hash)
496496
crust_status_t storage_delete_file(const char *hash)
497497
{
498498
// ----- Delete file items in metadata ----- //
499-
std::string deleted_file;
499+
json::JSON deleted_file;
500500
crust_status_t crust_status = CRUST_SUCCESS;
501501

502502
// ----- Delete file items in checked_files ----- //
@@ -508,8 +508,8 @@ crust_status_t storage_delete_file(const char *hash)
508508
std::string hash_str = wl->checked_files[i][FILE_HASH].ToString();
509509
if (hash_str.compare(hash) == 0)
510510
{
511+
deleted_file = wl->checked_files[i];
511512
wl->checked_files[i][FILE_STATUS].set_char(CURRENT_STATUS, FILE_STATUS_DELETED);
512-
deleted_file = hash_str;
513513
is_deleted = true;
514514
break;
515515
}
@@ -523,7 +523,7 @@ crust_status_t storage_delete_file(const char *hash)
523523
std::string hash_str = (*it)[FILE_HASH].ToString();
524524
if (hash_str.compare(hash) == 0)
525525
{
526-
deleted_file = hash_str;
526+
deleted_file = *it;
527527
wl->new_files.erase((++it).base());
528528
is_deleted = true;
529529
break;
@@ -535,20 +535,24 @@ crust_status_t storage_delete_file(const char *hash)
535535
// Print deleted info
536536
if (is_deleted)
537537
{
538-
log_info("Delete file:%s successfully!\n", deleted_file.c_str());
538+
log_info("Delete file:%s successfully!\n", deleted_file[FILE_HASH].ToString().c_str());
539539
}
540540
else
541541
{
542-
log_warn("Delete file:%s failed(not found)!\n", deleted_file.c_str());
542+
log_warn("Delete file:%s failed(not found)!\n", deleted_file[FILE_HASH].ToString().c_str());
543543
}
544544

545545
// ----- Delete file related data ----- //
546546
if (is_deleted)
547547
{
548548
// Delete file tree structure
549-
persist_del(deleted_file);
549+
persist_del(deleted_file[FILE_HASH].ToString());
550550
// Delete file metadata
551-
persist_del(deleted_file + "_meta");
551+
persist_del(deleted_file[FILE_HASH].ToString() + "_meta");
552+
// Update workload spec info
553+
std::pair<wl_spec_t, wl_spec_t> wl_p;
554+
wl->get_wl_spec_by_file_status(deleted_file[FILE_STATUS].get_char(CURRENT_STATUS), wl_p);
555+
wl->set_wl_spec(wl_p.second, -deleted_file[FILE_OLD_SIZE].ToInt());
552556
}
553557

554558
return crust_status;

test/integration/build_test.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,7 @@ function data_h_test
582582
function enc_report_cpp_test()
583583
{
584584
local pos=$(sed -n '/crust_status_t get_signed_work_report(/=' $enclave_report_cpp)
585-
sed -i "$((pos+2)),$((pos+13))d" $enclave_report_cpp
586-
sed -i "$((pos+2)) a \\\tWorkload *wl = Workload::get_instance();" $enclave_report_cpp
585+
sed -i "$((pos+4)),$((pos+34))d" $enclave_report_cpp
587586
}
588587

589588
########## enc_validate_cpp_test ##########

0 commit comments

Comments
 (0)