Can we write a generic interface that deletes any contract table data?
I found that I could use the db_remove_i64 function to delete table data, but I don't know how to pass the parameters of the db_remove_i64 function in the system contract
What do you mean generic interface? Can you give me an example code or function signature?
What do you mean
generic interface? Can you give me an example code or function signature?
I mean action that removes other contract table data from the system contract
You can't. Deleting data on other contract table is not allowed.
You appear to be attempting to implement deleting contract table data into the standard system contract. If you intend to generate a pull request, be sure to base your work on the version of the contract in the eosio.contracts repository, not the eos repository. The version in the eos repository is obsolete, currently only used for automated testing. It will eventually be removed.
Interesting. @jgiszczak Do you think eosio needs this feature? I don't think this feature possible in current design, because table doesn't have information about secondary index by itself. If this feature is implemented, table should have information about its secondary indices to be deleted by other contract.
I think it would be a very nice feature to have, in some form or other. Unfortunately as it stands now, it's impossible. From void apply_context::db_remove_i64():
const key_value_object& obj = keyval_cache.get( iterator );
const auto& table_obj = keyval_cache.get_table( obj.t_id );
EOS_ASSERT( table_obj.code == receiver, table_access_violation, "db access violation" );
If you want to delete a table that belongs to the same contract then there are some options.
If efficiency is not a concern you could just take a multi_index table for you and do something like the following:
auto itr = table.begin();
const auto end = table.end();
while (itr != end) {
table.erase(*itr++);
}
or you could be a little more efficient and do something like:
template<uint64_t TableName, typename T, typename... Indices>
void delete_table(eosio::multi_index<TableName, T, Indices...>& table) {
uint64_t dummy = 0;
int32_t itr = db_lowerbound_i64(table.get_code(), table.get_scope(), TableName, 0);
while (itr > -1) {
const int32_t to_remove = itr;
itr = db_next_i64(itr, &dummy);
db_remove_i64(to_remove);
}
}
Obviously this won't be as efficient as it being implemented on the other side of the C API, but I believe this would be the fastest way without changing the C API.
Should probably make it
template<uint64_t TableName, typename T, typename... Indices>
void delete_table(eosio::multi_index<TableName, T, Indices...>& table, uint32_t max_rows = std::numeric_limits<uint32_t>::max()) {
uint64_t dummy = 0;
int32_t itr = db_lowerbound_i64(table.get_code(), table.get_scope(), TableName, 0);
uint32_t rows_deleted = 0;
while (itr > -1 && rows_deleted++ < max_rows) {
const int32_t to_remove = itr;
itr = db_next_i64(itr, &dummy);
db_remove_i64(to_remove);
}
}
To allow you to be able to pass your action a number of rows so you can make multiple passes to delete larger tables or work around a low staking scenario.