-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxrule_manager.cpp
More file actions
206 lines (191 loc) · 8.27 KB
/
xrule_manager.cpp
File metadata and controls
206 lines (191 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2017-2018 Telos Foundation & contributors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "xrule_manager.h"
namespace top
{
namespace httpserver
{
bool xrule_manager::account_balance_filter(xjson_proc& json_proc)
{
if(json_proc.m_request_json["account"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
if(json_proc.m_request_json["property_key"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::property_key_is_empty)
return false;
}
return true;
}
bool xrule_manager::account_info_filter(xjson_proc& json_proc)
{
if(json_proc.m_request_json["account"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
return true;
}
bool xrule_manager::account_create_filter(xjson_proc& json_proc)
{
if(json_proc.m_request_json["account"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
if(m_store_intf->exist_account(json_proc.m_request_json["account"].asString()))
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_exist)
return false;
}
return true;
}
bool xrule_manager::transfer_out_filter(xjson_proc& json_proc)
{
if(json_proc.m_request_json["source"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
if(json_proc.m_request_json["destination"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
if(json_proc.m_request_json["source"].asString() == json_proc.m_request_json["destination"].asString())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::source_and_destination_same);
return false;
}
std::string balance;
int32_t ret = m_store_intf->get_account_balance(json_proc.m_request_json["source"].asString(), json_proc.m_request_json["property_key"].asString(), balance);
if(store::store_intf_return_type::account_not_exist == ret)
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
else if(store::store_intf_return_type::no_property_key == ret)
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::property_key_is_not_exist)
return false;
}
int64_t out_amount = std::stoll(json_proc.m_request_json["amount"].asString());
if(std::stoull(balance) < out_amount && out_amount <= 0)
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::balance_is_not_enough)
return false;
}
//check last hash
uint256_t acct_last_hash;
if(m_store_intf->get_last_hash(json_proc.m_request_json["source"].asString(), acct_last_hash) && acct_last_hash != uint256_t((uint8_t*)string_utl::base64_decode(json_proc.m_request_json["last_digest"].asString()).c_str()))
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::last_hash_is_not_equal)
return false;
}
return true;
}
bool xrule_manager::transfer_in_filter(xjson_proc& json_proc)
{
if(json_proc.m_request_json["account"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
//check last hash
uint256_t acct_last_hash;
if(m_store_intf->get_last_hash(json_proc.m_request_json["account"].asString(), acct_last_hash) && acct_last_hash != uint256_t((uint8_t*)string_utl::base64_decode(json_proc.m_request_json["last_digest"].asString()).c_str()))
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::last_hash_is_not_equal)
return false;
}
return true;
}
bool xrule_manager::account_history_filter(xjson_proc& json_proc)
{
if(json_proc.m_request_json["account"].isNull())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_empty)
return false;
}
if(json_proc.m_request_json["per_page"].asInt() > LARGEST_PER_PAGE)
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::per_page_too_large)
return false;
}
return true;
}
bool xrule_manager::set_property_filter(xjson_proc& json_proc)
{
std::string property_key = json_proc.m_request_json["property_key"].asString();
//note: the key_name of any data must start with "@" but NOT allow have "#"or "$"
if((property_key.find_first_of("@") != 0) || (property_key.find_first_of("#$") != std::string::npos))
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::property_key_not_valid)
return false;
}
return true;
}
bool xrule_manager::give_filter(xjson_proc& json_proc)
{
std::string dst_account = json_proc.m_request_json["destination"].asString();
if(!m_store_intf->exist_account(dst_account))
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::account_is_not_exist)
return false;
}
std::vector<std::shared_ptr<chain::xtransaction_t>> receive_list;
m_store_intf->get_receive_blocks(dst_account, receive_list);
if(receive_list.size() > 0)
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::already_give)
return false;
}
std::vector<std::shared_ptr<chain::xtransaction_t>> settle_list;
m_store_intf->get_settle_blocks(dst_account, settle_list);
//Response will start with the latest block for the account
for(auto iter=settle_list.begin(); iter<settle_list.end();iter++)
{
if((*iter)->m_transaction_type == chain::xtransbase_t::enum_xtransaction_type::enum_xtransaction_type_transfer_asset_in)
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::already_give)
return false;
}
}
return true;
}
bool xrule_manager::filter(xjson_proc& json_proc)
{
auto iter = g_action_map.find(json_proc.m_request_json["action"].asString());
if(iter == g_action_map.end())
{
SET_JSON_PROC_RESPONSE(xhttp_error_code::action_not_find)
return false;
}
switch(iter->second)
{
case xhttp_action::account_balance:
return account_balance_filter(json_proc);
case xhttp_action::account_info:
return account_info_filter(json_proc);
case xhttp_action::account_create:
return account_create_filter(json_proc);
case xhttp_action::transfer_out:
return transfer_out_filter(json_proc);
case xhttp_action::transfer_in:
return transfer_in_filter(json_proc);
case xhttp_action::account_history:
return account_history_filter(json_proc);
case xhttp_action::set_property:
return set_property_filter(json_proc);
case xhttp_action::give:
return give_filter(json_proc);
default:
break;
}
return true;
}
}
}