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

Add support for removal of table without multi_index typedefs to eosio.system #120

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,34 @@ namespace eosiosystem {
// static constexpr uint32_t max_inflation_rate = 5; // 5% annual inflation
static constexpr uint32_t seconds_per_day = 24 * 3600;

template <typename T>
int32_t remove_secondary_index( uint64_t code, uint64_t scope, uint64_t table ) {
using namespace eosio::_multi_index_detail;

uint64_t pk;

auto min = secondary_key_traits<T>::lowest();
auto itr = secondary_index_db_functions<T>::db_idx_lowerbound( code, scope, table, min, pk );

// itr == -1, no secondary index
// itr < -1, type mismatch
if( itr <= -1 ) return itr;

while( itr > -1 ) {
auto next_itr = secondary_index_db_functions<T>::db_idx_next( itr, &pk );
secondary_index_db_functions<T>::db_idx_remove( itr );
itr = next_itr;
}

// secondary index is removed
return 0;
}

#define REMOVE_SECONDARY_INDEX( ITR, TYPE, CODE, SCOPE, TABLE ) \
ITR = remove_secondary_index<TYPE>( CODE, SCOPE, TABLE ); \
if ( ITR == -1 ) break; \
else if ( ITR == 0 ) continue;

class [[eosio::contract("eosio.system")]] system_contract : public native {
private:
voters_table _voters;
Expand Down Expand Up @@ -317,6 +345,9 @@ namespace eosiosystem {
[[eosio::action]]
void bidrefund( name bidder, name newname );

[[eosio::action]]
void removetable( name code, uint64_t scope, name table );

private:
// Implementation details:

Expand Down
28 changes: 27 additions & 1 deletion eosio.system/src/eosio.system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,32 @@ namespace eosiosystem {
refunds_table.erase( it );
}

void system_contract::removetable( name code, uint64_t scope, name table ) {
require_auth( code );

auto itr = db_end_i64( code.value, scope, table.value );
eosio_assert( itr != -1, "table not found" );

for( uint64_t i = 0; i < 0x10; ++i ) {
REMOVE_SECONDARY_INDEX( itr, uint64_t, code.value, scope, table.value | i )
REMOVE_SECONDARY_INDEX( itr, uint128_t, code.value, scope, table.value | i )
REMOVE_SECONDARY_INDEX( itr, double, code.value, scope, table.value | i )
REMOVE_SECONDARY_INDEX( itr, long double, code.value, scope, table.value | i )
REMOVE_SECONDARY_INDEX( itr, eosio::key256, code.value, scope, table.value | i )
//REMOVE_SECONDARY_INDEX( itr, eosio::digest256, code.value, scope, table.value | i )
}

uint64_t pk;

itr = db_lowerbound_i64( code.value, scope, table.value, std::numeric_limits<uint64_t>::min() );

while( itr > -1 ) {
auto next_itr = db_next_i64( itr, &pk );
db_remove_i64( itr );
itr = next_itr;
}
}

/**
* Called after a new account is created. This code enforces resource-limits rules
* for new accounts as well as new account naming conventions.
Expand Down Expand Up @@ -308,7 +334,7 @@ EOSIO_DISPATCH( eosiosystem::system_contract,
// native.hpp (newaccount definition is actually in eosio.system.cpp)
(newaccount)(updateauth)(deleteauth)(linkauth)(unlinkauth)(canceldelay)(onerror)(setabi)
// eosio.system.cpp
(init)(setram)(setramrate)(setparams)(setpriv)(setalimits)(rmvproducer)(updtrevision)(bidname)(bidrefund)
(init)(setram)(setramrate)(setparams)(setpriv)(setalimits)(rmvproducer)(updtrevision)(bidname)(bidrefund)(removetable)
// delegate_bandwidth.cpp
(buyrambytes)(buyram)(sellram)(delegatebw)(undelegatebw)(refund)
// voting.cpp
Expand Down