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

bos blacklist #205

Open
wants to merge 12 commits 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
3 changes: 3 additions & 0 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ namespace eosiosystem {
[[eosio::action]]
void setparams( const eosio::blockchain_parameters& params );

[[eosio::action]]
void setblacklist(namelist, name action, const std::vector<name>& names );

// functions defined in producer_pay.cpp
[[eosio::action]]
void claimrewards( const name owner );
Expand Down
44 changes: 42 additions & 2 deletions contracts/eosio.system/src/eosio.system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "voting.cpp"
#include "exchange_state.cpp"
#include "rex.cpp"

#include <map>
namespace eosiosystem {

system_contract::system_contract( name s, name code, datastream<const char*> ds )
Expand Down Expand Up @@ -120,6 +120,46 @@ namespace eosiosystem {
check( 3 <= _gstate.max_authority_depth, "max_authority_depth should be at least 3" );
set_blockchain_parameters( params );
}

void system_contract::setblacklist(name list, name action, const std::vector<name>& names)
{
const int blacklist_limit_size = 100;
enum class list_type:int64_t
{
actor_blacklist_type = 1,
contract_blacklist_type,
resource_greylist_type,
list_type_count
};
enum class list_action_type:int64_t{
insert_type = 1,
remove_type,
list_action_type_count
};

std::map<name, list_type> list_type_to_enum = {
{"actor"_n, list_type::actor_blacklist_type},
{"contract"_n, list_type::contract_blacklist_type},
{"resource"_n, list_type::resource_greylist_type}};

std::map<name, list_action_type> list_action_type_to_enum = {
{"insert"_n, list_action_type::insert_type},
{"remove"_n, list_action_type::remove_type}};

std::map<name, list_type>::iterator itlt = list_type_to_enum.find(list);
std::map<name, list_action_type>::iterator itlat = list_action_type_to_enum.find(action);

require_auth(_self);
check(3 <= _gstate.max_authority_depth, "max_authority_depth should be at least 3");
check(names.size() < blacklist_limit_size, "the size of 'names' must be less than 100");
check(itlt != list_type_to_enum.end(), " unknown list type string support 'actor' ,'contract', 'resource'");
check(itlat != list_action_type_to_enum.end(), " unknown list type string support 'insert' or 'remove'");

auto packed_names = pack(names);

set_blacklist_packed(static_cast<int64_t>(itlt->second), static_cast<int64_t>(itlat->second), packed_names.data(), packed_names.size());
}


void system_contract::setpriv( name account, uint8_t ispriv ) {
require_auth( _self );
Expand Down Expand Up @@ -464,7 +504,7 @@ EOSIO_DISPATCH( eosiosystem::system_contract,
(newaccount)(updateauth)(deleteauth)(linkauth)(unlinkauth)(canceldelay)(onerror)(setabi)
// eosio.system.cpp
(init)(setram)(setramrate)(setparams)(setpriv)(setalimits)(setacctram)(setacctnet)(setacctcpu)
(rmvproducer)(updtrevision)(bidname)(bidrefund)
(rmvproducer)(updtrevision)(bidname)(bidrefund)(setblacklist)
// rex.cpp
(deposit)(withdraw)(buyrex)(unstaketorex)(sellrex)(cnclrexorder)(rentcpu)(rentnet)(fundcpuloan)(fundnetloan)
(defcpuloan)(defnetloan)(updaterex)(consolidate)(mvtosavings)(mvfrsavings)(setrex)(rexexec)(closerex)
Expand Down
14 changes: 14 additions & 0 deletions contracts/eosio.token/include/eosio.token/eosio.token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ namespace eosio {
[[eosio::action]]
void close( name owner, const symbol& symbol );

[[eosio::action]]
void addblacklist(const std::vector<name>& accounts);

[[eosio::action]]
void rmblacklist(const std::vector<name>& accounts);

static asset get_supply( name token_contract_account, symbol_code sym_code )
{
stats statstable( token_contract_account, sym_code.raw() );
Expand Down Expand Up @@ -78,6 +84,14 @@ namespace eosio {
uint64_t primary_key()const { return supply.symbol.code().raw(); }
};

struct [[eosio::table]] account_blacklist
{
name account;
uint64_t primary_key() const { return account.value; }
};

typedef eosio::multi_index<"blacklist"_n, account_blacklist> blacklist;
static const uint8_t blacklist_limit_size = 100 ;
typedef eosio::multi_index< "accounts"_n, account > accounts;
typedef eosio::multi_index< "stat"_n, currency_stats > stats;

Expand Down
44 changes: 43 additions & 1 deletion contracts/eosio.token/src/eosio.token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ void token::transfer( name from,
asset quantity,
string memo )
{
blacklist blacklisttable(_self, _self.value);
check(blacklisttable.find(from.value) == blacklisttable.end(), "account is on the blacklist");
check( from != to, "cannot transfer to self" );
require_auth( from );
check( is_account( to ), "to account does not exist");
Expand Down Expand Up @@ -165,6 +167,46 @@ void token::close( name owner, const symbol& symbol )
acnts.erase( it );
}

void token::addblacklist(const std::vector<name>& accounts)
{
require_auth("eosio"_n);

check(blacklist_limit_size >= accounts.size(), "accounts' size must be less than 100.");
static const std::string msg = std::string("account does not exist");
bool is_executed = false;
for (auto acc : accounts){
std::string m = acc.to_string() + msg;
check(is_account(acc), m.c_str());
blacklist blacklisttable(_self, _self.value);
auto it = blacklisttable.find(acc.value);
if (it == blacklisttable.end()) {
blacklisttable.emplace(_self, [&](auto &a) {
a.account = acc;
is_executed = true;
});
}
}

eosio_assert( is_executed, "all accounts were on blacklist." );
}

void token::rmblacklist(const std::vector<name>& accounts)
{
require_auth("eosio"_n);

check( blacklist_limit_size>=accounts.size(), "accounts' size must be less than 100." );
bool is_executed = false;
for (auto acc : accounts){
blacklist blacklisttable(_self, _self.value);
auto it = blacklisttable.find(acc.value);
if (it != blacklisttable.end()){
blacklisttable.erase(it);
is_executed = true;
}
}

check( is_executed, "all accounts were not on blacklist." );
}
} /// namespace eosio

EOSIO_DISPATCH( eosio::token, (create)(issue)(transfer)(open)(close)(retire) )
EOSIO_DISPATCH( eosio::token, (create)(issue)(transfer)(open)(close)(retire)(addblacklist)(rmblacklist) )
34 changes: 34 additions & 0 deletions tests/eosio.system_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,40 @@ class eosio_system_tester : public TESTER {
return msig_abi_ser;
}

bool is_full_contains_subset(const shared_vector<account_name> &allblklst, const std::vector<account_name> &blklst)
{
std::vector<account_name> allblacklist = std::vector<account_name>(allblklst.begin(), allblklst.end());
std::sort(allblacklist.begin(), allblacklist.end());
std::vector<account_name> blacklist = std::vector<account_name>(blklst.begin(), blklst.end());
std::sort(blacklist.begin(), blacklist.end());

// vector<account_name> blacklisted;
// blacklisted.reserve(allblacklist.size());
// set_intersection(allblacklist.begin(), allblacklist.end(), blacklist.begin(),
// blacklist.end(), std::back_inserter(blacklisted));

// std::sort(blacklisted.begin(), blacklisted.end());
vector<account_name> excluded;
excluded.reserve(blacklist.size());
std::set_difference( blacklist.begin(),
blacklist.end(),allblacklist.begin(),allblacklist.end(),std::back_inserter(excluded));

return excluded.empty();
}

bool is_empty_intersection(const shared_vector<account_name> &allblklst, const std::vector<account_name> &blklst)
{
std::vector<account_name> allblacklist = std::vector<account_name>(allblklst.begin(), allblklst.end());
std::sort(allblacklist.begin(), allblacklist.end());
std::vector<account_name> blacklist = std::vector<account_name>(blklst.begin(), blklst.end());
std::sort(blacklist.begin(), blacklist.end());

vector<account_name> excluded;
excluded.reserve(blacklist.size());
std::set_intersection(blacklist.begin(), blacklist.end(), allblacklist.begin(), allblacklist.end(), std::back_inserter(excluded));

return excluded.empty();
}
vector<name> active_and_vote_producers() {
//stake more than 15% of total EOS supply to activate chain
transfer( "eosio", "alice1111111", core_sym::from_string("650000000.0000"), "eosio" );
Expand Down
Loading