Skip to content

Commit

Permalink
Refactored reset/wipe for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Jan 27, 2025
1 parent 6cf032f commit b51fa65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
9 changes: 4 additions & 5 deletions contracts/api/include/api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ class [[eosio::contract("api")]] api : public contract
[[eosio::action, eosio::read_only]] std::vector<asset> getbalances(const name account);
using getbalances_action = action_wrapper<"getbalances"_n, &api::getbalances>;

// DEBUG (used to help testing)
#ifdef DEBUG
[[eosio::action]] void
cleartable(const name table_name, const optional<name> scope, const optional<uint64_t> max_rows);
[[eosio::action]] void wipe();
[[eosio::action]] void reset();
#endif

private:
// DEBUG (used to help testing)
#ifdef DEBUG
// @debug
template <typename T>
void clear_table(T& table, uint64_t rows_to_clear);
void reset_singletons();
void wipe_singletons();
void wipe_tables();
#endif
};

Expand Down
56 changes: 24 additions & 32 deletions contracts/api/src/debug.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace api {

// @debug
template <typename T>
void api::clear_table(T& table, uint64_t rows_to_clear)
{
Expand All @@ -10,46 +9,39 @@ void api::clear_table(T& table, uint64_t rows_to_clear)
}
}

// @debug
[[eosio::action]] void
api::cleartable(const name table_name, const optional<name> scope, const optional<uint64_t> max_rows)
void api::reset_singletons()
{
require_auth(get_self());
const uint64_t rows_to_clear = (!max_rows || *max_rows == 0) ? -1 : *max_rows;
const uint64_t value = scope ? scope->value : get_self().value;

// list out all the tables that could be cleared
//
// api::table1 _table1(get_self(), value);
// api::table2 _table2(get_self(), value);

// Use an if/else if/else chain to determine which table to clear
//
// if (table_name == "table1"_n)
// clear_table(_table1, rows_to_clear);
// else if (table_name == "table2"_n)
// clear_table(_table2, rows_to_clear);
// else
// check(false, "cleartable: [table_name] unknown table to clear");
config_table _config(get_self(), get_self().value);
config_row default_row;
_config.set(default_row, get_self());
}

void api::wipe_singletons()
{
config_table _config(get_self(), get_self().value);
_config.remove();
}

void api::wipe_tables()
{
token_table tokens(get_self(), get_self().value);
clear_table(tokens, -1);
}

// @debug
[[eosio::action]] void api::wipe()
{
require_auth(get_self());

api::token_table tokens(get_self(), get_self().value);
clear_table(tokens, -1);
wipe_singletons();
wipe_tables();
}

// Define the tables to wipe
//
// api::table1 _table1(get_self(), get_self().value);
// api::table2 _table2(get_self(), get_self().value);
[[eosio::action]] void api::reset()
{
require_auth(get_self());

// Call clear on the tables to wipe
//
// clear_table(_table1, -1);
// clear_table(_table2, -1);
reset_singletons();
wipe_tables();
}

} // namespace api
2 changes: 1 addition & 1 deletion test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const contracts = {
}

export async function resetContracts() {
await contracts.api.actions.wipe().send()
await contracts.api.actions.reset().send()
}

export function advanceTime(seconds: number) {
Expand Down

0 comments on commit b51fa65

Please sign in to comment.