In this code, there should be check of params in upsert action before modifying. Otherwise, the action (with same params) will be repeatedly pushed & the entire NET, CPU resources of the user will be completely drained.
Changes in upsert action
...
...
check((it->first_name != first_name) || (it->last_name != last_name) || (it->street != street) || (it->city != city) || (it->state != state), "At least one of all data must be different.");
...
...
- Complete code of the `upsert` action:
```cpp
[[eosio::action]]
void upsert(
name user, string first_name, string last_name, string street, string city,
string state ) {
require_auth(user);
address_index addresses(get_first_receiver(), get_first_receiver().value);
auto it = addresses.find(user.value);
if(it == addresses.end()) {
addresses.emplace(user, [&](auto& row) {
row.key = user;
row.first_name = first_name;
row.last_name = last_name;
row.street = street;
row.city = city;
row.state = state;
});
}
else {
// check whether either of the new data is different
check((it->first_name != first_name) || (it->last_name != last_name) || (it->street != street) || (it->city != city) || (it->state != state), "At least one of all data must be different.");
addresses.modify(it, user, [&](auto& row) {
row.first_name = first_name;
row.last_name = last_name;
row.street = street;
row.city = city;
row.state = state;
});
}
}
In this code, there should be check of params in
upsert
action before modifying. Otherwise, the action (with same params) will be repeatedly pushed & the entire NET, CPU resources of the user will be completely drained.upsert
action... ...