Skip to content

Commit eacc610

Browse files
Fix missing block bug (crustio#238)
* fix miss block * rename var * add some logs
1 parent e758d3e commit eacc610

File tree

1 file changed

+78
-85
lines changed

1 file changed

+78
-85
lines changed

src/app/process/WorkReport.cpp

Lines changed: 78 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ crust::Log *p_log = crust::Log::get_instance();
1212
size_t get_random_wait_time(std::string seed)
1313
{
1414
unsigned int seed_number = 0;
15-
for(size_t i = 0; i < seed.size(); i++)
15+
for (size_t i = 0; i < seed.size(); i++)
1616
{
1717
seed_number += seed[i];
1818
}
19-
srand(time(NULL)+seed_number);
19+
srand(time(NULL) + seed_number);
2020
//[9 199]
2121
return (rand() % (REPORT_INTERVAL_BLCOK_NUMBER_UPPER_LIMIT - REPORT_INTERVAL_BLCOK_NUMBER_LOWER_LIMIT + 1) + REPORT_INTERVAL_BLCOK_NUMBER_LOWER_LIMIT - 1) * BLOCK_INTERVAL;
2222
}
@@ -29,124 +29,117 @@ void work_report_loop(void)
2929
crust_status_t crust_status = CRUST_SUCCESS;
3030
crust::Chain *p_chain = crust::Chain::get_instance();
3131
size_t offline_base_height = REPORT_BLOCK_HEIGHT_BASE;
32-
//int order_report_interval = 0;
32+
size_t target_block_height = REPORT_BLOCK_HEIGHT_BASE;
3333

34-
while (true)
34+
// Generate target block height
35+
if (!offline_chain_mode)
3536
{
36-
// ----- Report order report ----- //
37-
/*
38-
if (3 == order_report_interval)
37+
crust::BlockHeader *block_header = p_chain->get_block_header();
38+
if (block_header == NULL)
3939
{
40-
// Ecall_get_signed_order_report will store order report in g_order_report
41-
if(SGX_SUCCESS != Ecall_get_signed_order_report(global_eid, &crust_status)
42-
|| CRUST_SUCCESS != crust_status)
43-
{
44-
if (CRUST_REPORT_NO_ORDER_FILE != crust_status)
45-
{
46-
p_log->err("Get signed order report failed! Error code: %x\n", crust_status);
47-
}
48-
}
49-
else
50-
{
51-
p_log->info("Get order report:%s\n", get_g_order_report().c_str());
52-
}
53-
set_g_order_report("");
54-
order_report_interval = 0;
40+
p_log->warn("Cannot get block header! Set target block height %d\n", target_block_height);
5541
}
56-
order_report_interval++;
57-
*/
42+
else
43+
{
44+
target_block_height = (block_header->number / REPORT_BLOCK_HEIGHT_BASE + 1) * REPORT_BLOCK_HEIGHT_BASE;
45+
p_log->info("Set target block height %d\n", target_block_height);
46+
}
47+
}
5848

49+
while (true)
50+
{
5951
// ----- Report work report ----- //
6052
crust::BlockHeader *block_header = NULL;
6153
if (!offline_chain_mode)
6254
{
6355
block_header = p_chain->get_block_header();
56+
if (block_header == NULL)
57+
{
58+
p_log->warn("Cannot get block header!\n");
59+
goto loop;
60+
}
61+
62+
if (block_header->number < target_block_height)
63+
{
64+
goto loop;
65+
}
66+
67+
block_header->number = (block_header->number / REPORT_BLOCK_HEIGHT_BASE) * REPORT_BLOCK_HEIGHT_BASE;
68+
69+
size_t wait_time = get_random_wait_time(Config::get_instance()->chain_address + Config::get_instance()->base_url);
70+
p_log->info("It is estimated that the workload will be reported at the %lu block\n", block_header->number + (wait_time / BLOCK_INTERVAL) + 1);
71+
sleep(wait_time);
72+
73+
// Get confirmed block hash
74+
block_header->hash = p_chain->get_block_hash(block_header->number);
75+
if (block_header->hash == "" || block_header->hash == "0000000000000000000000000000000000000000000000000000000000000000")
76+
{
77+
p_log->warn("Get block hash failed");
78+
goto loop;
79+
}
80+
81+
target_block_height = block_header->number + REPORT_BLOCK_HEIGHT_BASE;
6482
}
6583
else
6684
{
6785
block_header = new crust::BlockHeader();
68-
block_header->hash = "0000000000000000000000000000000000000000000000000000000000000000";
86+
block_header->hash = "1000000000000000000000000000000000000000000000000000000000000001";
6987
block_header->number = offline_base_height;
7088
offline_base_height += REPORT_BLOCK_HEIGHT_BASE;
89+
sleep(60);
7190
}
7291

73-
if (block_header == NULL)
92+
// Get signed validation report
93+
if (SGX_SUCCESS != Ecall_get_signed_work_report(global_eid, &crust_status,
94+
block_header->hash.c_str(), block_header->number))
7495
{
75-
p_log->warn("Cannot get block header!\n");
76-
goto loop;
96+
p_log->err("Get signed work report failed!\n");
7797
}
78-
if (0 == block_header->number % REPORT_BLOCK_HEIGHT_BASE)
98+
else
7999
{
80-
if (!offline_chain_mode)
100+
if (CRUST_SUCCESS == crust_status)
81101
{
82-
size_t wait_time = get_random_wait_time(Config::get_instance()->chain_address+Config::get_instance()->base_url);
83-
p_log->info("It is estimated that the workload will be reported at the %lu block\n", block_header->number + (wait_time / BLOCK_INTERVAL) + 1);
84-
sleep(wait_time);
102+
// Send signed validation report to crust chain
103+
std::string work_str = get_g_enclave_workreport();
104+
p_log->info("Sign validation report successfully!\n%s\n", work_str.c_str());
85105

86-
// Get confirmed block hash
87-
block_header->hash = p_chain->get_block_hash(block_header->number);
88-
if (block_header->hash == "")
106+
if (!offline_chain_mode)
89107
{
90-
goto loop;
91-
}
92-
}
93-
else
94-
{
95-
sleep(60);
96-
}
97-
98-
// Get signed validation report
99-
if (SGX_SUCCESS != Ecall_get_signed_work_report(global_eid, &crust_status,
100-
block_header->hash.c_str(), block_header->number))
101-
{
102-
p_log->err("Get signed work report failed!\n");
103-
}
104-
else
105-
{
106-
if (CRUST_SUCCESS == crust_status)
107-
{
108-
// Send signed validation report to crust chain
109-
std::string work_str = get_g_enclave_workreport();
110-
p_log->info("Sign validation report successfully!\n%s\n", work_str.c_str());
111-
112-
if (!offline_chain_mode)
108+
// Delete space and line break
109+
remove_char(work_str, '\\');
110+
remove_char(work_str, '\n');
111+
remove_char(work_str, ' ');
112+
if (!p_chain->post_sworker_work_report(work_str))
113113
{
114-
// Delete space and line break
115-
remove_char(work_str, '\\');
116-
remove_char(work_str, '\n');
117-
remove_char(work_str, ' ');
118-
if (!p_chain->post_sworker_work_report(work_str))
119-
{
120-
p_log->err("Send work report to crust chain failed!\n");
121-
}
122-
else
123-
{
124-
p_log->info("Send work report to crust chain successfully!\n");
125-
report_add_callback();
126-
}
114+
p_log->err("Send work report to crust chain failed!\n");
127115
}
128116
else
129117
{
118+
p_log->info("Send work report to crust chain successfully!\n");
130119
report_add_callback();
131120
}
132121
}
133-
else if (crust_status == CRUST_BLOCK_HEIGHT_EXPIRED)
134-
{
135-
p_log->info("Block height expired.\n");
136-
}
137-
else if (crust_status == CRUST_FIRST_WORK_REPORT_AFTER_REPORT)
138-
{
139-
p_log->info("Can't generate work report for the first time after restart\n");
140-
}
141-
else if (crust_status == CRUST_NO_KARST)
142-
{
143-
p_log->info("Can't generate work report. You have meaningful files, please start karst\n");
144-
}
145122
else
146123
{
147-
p_log->err("Get signed validation report failed! Error code: %x\n", crust_status);
124+
report_add_callback();
148125
}
149126
}
127+
else if (crust_status == CRUST_BLOCK_HEIGHT_EXPIRED)
128+
{
129+
p_log->info("Block height expired.\n");
130+
}
131+
else if (crust_status == CRUST_FIRST_WORK_REPORT_AFTER_REPORT)
132+
{
133+
p_log->info("Can't generate work report for the first time after restart\n");
134+
}
135+
else if (crust_status == CRUST_NO_KARST)
136+
{
137+
p_log->info("Can't generate work report. You have meaningful files, please start karst\n");
138+
}
139+
else
140+
{
141+
p_log->err("Get signed validation report failed! Error code: %x\n", crust_status);
142+
}
150143
}
151144

152145
loop:

0 commit comments

Comments
 (0)