eos icon indicating copy to clipboard operation
eos copied to clipboard

Can we write a generic interface that deletes any contract table data?

Open yhc166188 opened this issue 8 years ago • 9 comments

yhc166188 avatar Oct 17 '18 02:10 yhc166188

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

yhc166188 avatar Oct 17 '18 11:10 yhc166188

What do you mean generic interface? Can you give me an example code or function signature?

conr2d avatar Oct 17 '18 15:10 conr2d

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

yhc166188 avatar Oct 17 '18 15:10 yhc166188

You can't. Deleting data on other contract table is not allowed.

conr2d avatar Oct 17 '18 16:10 conr2d

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.

jgiszczak avatar Oct 17 '18 19:10 jgiszczak

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.

conr2d avatar Oct 18 '18 12:10 conr2d

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" );

jgiszczak avatar Oct 18 '18 22:10 jgiszczak

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.

brianjohnson5972 avatar Oct 19 '18 19:10 brianjohnson5972

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.

brianjohnson5972 avatar Oct 19 '18 19:10 brianjohnson5972