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

DAWN-637 Removed apply_context's old table methods #1730

Merged
Merged
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
66 changes: 0 additions & 66 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,42 +279,6 @@ const bytes& apply_context::get_packed_transaction() {
return trx_meta.packed_trx;
}

const char* to_string(contracts::table_key_type key_type) {
switch(key_type) {
case contracts::type_unassigned:
return "unassigned";
case contracts::type_i64:
return "i64";
case contracts::type_str:
return "str";
case contracts::type_i128i128:
return "i128i128";
case contracts::type_i64i64:
return "i64i64";
case contracts::type_i64i64i64:
return "i64i64i64";
default:
return "<unkown table_key_type>";
}
}

void apply_context::validate_table_key( const table_id_object& t_id, contracts::table_key_type key_type ) {
FC_ASSERT( t_id.key_type == contracts::table_key_type::type_unassigned || key_type == t_id.key_type,
"Table entry for ${code}-${scope}-${table} uses key type ${act_type} should have had type of ${exp_type}",
("code",t_id.code)("scope",t_id.scope)("table",t_id.table)("act_type",to_string(t_id.key_type))("exp_type", to_string(key_type)) );
}

void apply_context::validate_or_add_table_key( const table_id_object& t_id, contracts::table_key_type key_type ) {
if (t_id.key_type == contracts::table_key_type::type_unassigned)
mutable_db.modify( t_id, [&key_type]( auto& o) {
o.key_type = key_type;
});
else
FC_ASSERT( key_type == t_id.key_type,
"Table entry for ${code}-${scope}-${table} uses key type ${act_type} should have had type of ${exp_type}",
("code",t_id.code)("scope",t_id.scope)("table",t_id.table)("act_type",to_string(t_id.key_type))("exp_type", to_string(key_type)) );
}

void apply_context::update_db_usage( const account_name& payer, int64_t delta ) {
require_write_lock( payer );
if( (delta > 0) && payer != account_name(receiver) ) {
Expand Down Expand Up @@ -366,7 +330,6 @@ int apply_context::db_store_i64( uint64_t scope, uint64_t table, const account_n
require_write_lock( scope );
const auto& tab = find_or_create_table( receiver, scope, table );
auto tableid = tab.id;
validate_or_add_table_key(tab, contracts::table_key_type::type_i64);

FC_ASSERT( payer != account_name(), "must specify a valid account to pay for new record" );

Expand Down Expand Up @@ -485,7 +448,6 @@ int apply_context::db_find_i64( uint64_t code, uint64_t scope, uint64_t table, u

const auto* tab = find_table( code, scope, table );
if( !tab ) return -1;
validate_table_key(*tab, contracts::table_key_type::type_i64);

auto table_end_itr = keyval_cache.cache_table( *tab );

Expand All @@ -500,7 +462,6 @@ int apply_context::db_lowerbound_i64( uint64_t code, uint64_t scope, uint64_t ta

const auto* tab = find_table( code, scope, table );
if( !tab ) return -1;
validate_table_key(*tab, contracts::table_key_type::type_i64);

auto table_end_itr = keyval_cache.cache_table( *tab );

Expand All @@ -517,7 +478,6 @@ int apply_context::db_upperbound_i64( uint64_t code, uint64_t scope, uint64_t ta

const auto* tab = find_table( code, scope, table );
if( !tab ) return -1;
validate_table_key(*tab, contracts::table_key_type::type_i64);

auto table_end_itr = keyval_cache.cache_table( *tab );

Expand All @@ -534,33 +494,7 @@ int apply_context::db_end_i64( uint64_t code, uint64_t scope, uint64_t table ) {

const auto* tab = find_table( code, scope, table );
if( !tab ) return -1;
validate_table_key(*tab, contracts::table_key_type::type_i64);

return keyval_cache.cache_table( *tab );
}

template<>
contracts::table_key_type apply_context::get_key_type<contracts::key_value_object>() {
return contracts::table_key_type::type_i64;
}

template<>
contracts::table_key_type apply_context::get_key_type<contracts::keystr_value_object>() {
return contracts::table_key_type::type_str;
}

template<>
contracts::table_key_type apply_context::get_key_type<contracts::key128x128_value_object>() {
return contracts::table_key_type::type_i128i128;
}

template<>
contracts::table_key_type apply_context::get_key_type<contracts::key64x64_value_object>() {
return contracts::table_key_type::type_i64i64;
}

template<>
contracts::table_key_type apply_context::get_key_type<contracts::key64x64x64_value_object>() {
return contracts::table_key_type::type_i64i64i64;
}
} } /// eosio::chain
61 changes: 36 additions & 25 deletions libraries/chain/contracts/eosio_contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,34 +294,36 @@ static const abi_serializer& get_abi_serializer() {
}

static optional<variant> get_pending_recovery(apply_context& context, account_name account ) {
const auto* t_id = context.find_table(config::system_account_name, account, N(recovery));
if (t_id) {
uint64_t key = account;
int32_t record_size = context.front_record<key_value_index, by_scope_primary>(*t_id, &key, nullptr, 0);
if (record_size > 0) {
bytes value(record_size + sizeof(uint64_t));
uint64_t* key_p = reinterpret_cast<uint64_t *>(value.data());
*key_p = key;

record_size = context.front_record<key_value_index, by_scope_primary>(*t_id, &key, value.data() + sizeof(uint64_t), value.size() - sizeof(uint64_t));
assert(record_size == value.size() - sizeof(uint64_t));

return get_abi_serializer().binary_to_variant("pending_recovery", value);
}
const uint64_t id = account;
const auto table = N(recovery);
const auto iter = context.db_find_i64(config::system_account_name, account, table, id);
if (iter != -1) {
const auto buffer_size = context.db_get_i64(iter, nullptr, 0);
bytes value(buffer_size);

assert(context.db_get_i64(iter, value.data(), buffer_size) == buffer_size);

return get_abi_serializer().binary_to_variant("pending_recovery", value);
}

return optional<variant_object>();
}

static uint32_t get_next_sender_id(apply_context& context) {
context.require_write_lock( config::eosio_auth_scope );
const auto& t_id = context.find_or_create_table(config::system_account_name, config::eosio_auth_scope, N(deferred.seq));
uint64_t key = N(config::eosio_auth_scope);
uint32_t next_serial = 0;
context.front_record<key_value_index, by_scope_primary>(t_id, &key, (char *)&next_serial, sizeof(uint32_t));
const uint64_t id = N(config::eosio_auth_scope);
const auto table = N(deferred.seq);
const auto payer = config::system_account_name;
const auto iter = context.db_find_i64(config::system_account_name, config::eosio_auth_scope, table, id);
if (iter == -1) {
const uint32_t next_serial = 1;
context.db_store_i64(config::eosio_auth_scope, table, payer, id, (const char*)&next_serial, sizeof(next_serial));
return 0;
}

uint32_t result = next_serial++;
context.store_record<key_value_object>(t_id, config::system_account_name, &key, (char *)&next_serial, sizeof(uint32_t));
uint32_t next_serial = 0;
context.db_get_i64(iter, (char*)&next_serial, sizeof(next_serial));
const auto result = next_serial++;
context.db_update_i64(iter, payer, (const char*)&next_serial, sizeof(next_serial));
return result;
}

Expand Down Expand Up @@ -408,16 +410,25 @@ void apply_eosio_postrecovery(apply_context& context) {
context.execute_deferred(std::move(dtrx));


const auto& t_id = context.find_or_create_table(config::system_account_name, account, N(recovery));
auto data = get_abi_serializer().variant_to_binary("pending_recovery", record_data);
context.store_record<key_value_object>(t_id, 0, &account.value, data.data() + sizeof(uint64_t), data.size() - sizeof(uint64_t));
const uint64_t id = account;
const uint64_t table = N(recovery);
const auto payer = account;
const auto iter = context.db_find_i64(config::system_account_name, account, table, id);
if (iter == -1) {
context.db_store_i64(account, table, payer, id, (const char*)data.data(), data.size());
} else {
context.db_update_i64(iter, payer, (const char*)data.data(), data.size());
}

context.console_append_formatted("Recovery Started for account ${account} : ${memo}\n", mutable_variant_object()("account", account)("memo", recover_act.memo));
}

static void remove_pending_recovery(apply_context& context, const account_name& account) {
const auto& t_id = context.find_or_create_table(config::system_account_name, account, N(recovery));
context.remove_record<key_value_object>(t_id, &account.value);
const auto iter = context.db_find_i64(config::system_account_name, account, N(recovery), account);
if (iter != -1) {
context.db_remove_i64(iter);
}
}

void apply_eosio_passrecovery(apply_context& context) {
Expand Down
Loading