Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit aee4080

Browse files
committed
Add support for removal of table without multi_index typedefs to eosio.system
1 parent 6c507c9 commit aee4080

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

eosio.system/include/eosio.system/eosio.system.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ namespace eosiosystem {
176176
// static constexpr uint32_t max_inflation_rate = 5; // 5% annual inflation
177177
static constexpr uint32_t seconds_per_day = 24 * 3600;
178178

179+
template <typename T>
180+
int32_t remove_secondary_index( uint64_t code, uint64_t scope, uint64_t table ) {
181+
using namespace eosio::_multi_index_detail;
182+
183+
uint64_t pk;
184+
185+
auto min = secondary_key_traits<T>::lowest();
186+
auto itr = secondary_index_db_functions<T>::db_idx_lowerbound( code, scope, table, min, pk );
187+
188+
// itr == -1, no secondary index
189+
// itr < -1, type mismatch
190+
if( itr <= -1 ) return itr;
191+
192+
while( itr > -1 ) {
193+
auto next_itr = secondary_index_db_functions<T>::db_idx_next( itr, &pk );
194+
secondary_index_db_functions<T>::db_idx_remove( itr );
195+
itr = next_itr;
196+
}
197+
198+
// secondary index is removed
199+
return 0;
200+
}
201+
202+
#define REMOVE_SECONDARY_INDEX( ITR, TYPE, CODE, SCOPE, TABLE ) \
203+
ITR = remove_secondary_index<TYPE>( CODE, SCOPE, TABLE ); \
204+
if ( ITR == -1 ) break; \
205+
else if ( ITR == 0 ) continue;
206+
179207
class [[eosio::contract("eosio.system")]] system_contract : public native {
180208
private:
181209
voters_table _voters;
@@ -317,6 +345,9 @@ namespace eosiosystem {
317345
[[eosio::action]]
318346
void bidrefund( name bidder, name newname );
319347

348+
[[eosio::action]]
349+
void removetable( name code, uint64_t scope, name table );
350+
320351
private:
321352
// Implementation details:
322353

eosio.system/src/eosio.system.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,32 @@ namespace eosiosystem {
218218
refunds_table.erase( it );
219219
}
220220

221+
void system_contract::removetable( name code, uint64_t scope, name table ) {
222+
require_auth( code );
223+
224+
auto itr = db_end_i64( code.value, scope, table.value );
225+
eosio_assert( itr != -1, "table not found" );
226+
227+
for( uint64_t i = 0; i < 0x10; ++i ) {
228+
REMOVE_SECONDARY_INDEX( itr, uint64_t, code.value, scope, table.value | i )
229+
REMOVE_SECONDARY_INDEX( itr, uint128_t, code.value, scope, table.value | i )
230+
REMOVE_SECONDARY_INDEX( itr, double, code.value, scope, table.value | i )
231+
REMOVE_SECONDARY_INDEX( itr, long double, code.value, scope, table.value | i )
232+
REMOVE_SECONDARY_INDEX( itr, eosio::key256, code.value, scope, table.value | i )
233+
//REMOVE_SECONDARY_INDEX( itr, eosio::digest256, code.value, scope, table.value | i )
234+
}
235+
236+
uint64_t pk;
237+
238+
itr = db_lowerbound_i64( code.value, scope, table.value, std::numeric_limits<uint64_t>::min() );
239+
240+
while( itr > -1 ) {
241+
auto next_itr = db_next_i64( itr, &pk );
242+
db_remove_i64( itr );
243+
itr = next_itr;
244+
}
245+
}
246+
221247
/**
222248
* Called after a new account is created. This code enforces resource-limits rules
223249
* for new accounts as well as new account naming conventions.
@@ -308,7 +334,7 @@ EOSIO_DISPATCH( eosiosystem::system_contract,
308334
// native.hpp (newaccount definition is actually in eosio.system.cpp)
309335
(newaccount)(updateauth)(deleteauth)(linkauth)(unlinkauth)(canceldelay)(onerror)(setabi)
310336
// eosio.system.cpp
311-
(init)(setram)(setramrate)(setparams)(setpriv)(setalimits)(rmvproducer)(updtrevision)(bidname)(bidrefund)
337+
(init)(setram)(setramrate)(setparams)(setpriv)(setalimits)(rmvproducer)(updtrevision)(bidname)(bidrefund)(removetable)
312338
// delegate_bandwidth.cpp
313339
(buyrambytes)(buyram)(sellram)(delegatebw)(undelegatebw)(refund)
314340
// voting.cpp

0 commit comments

Comments
 (0)