Skip to content

Commit

Permalink
Replace N macro with operator ""_n
Browse files Browse the repository at this point in the history
Cherry picked from c5674d4
with name.[ch]pp from 2.1 and additional fixes.

Because the N macro did not have a project-specific prefix, it clashed
with its namesakes from third-party projects like LLVM. This commit
introduces a namespace-scoped operator ""_n and replaces all invocations
of the N macro with that operator.
  • Loading branch information
swatanabe committed Jan 27, 2022
1 parent 979a449 commit 8c567fd
Show file tree
Hide file tree
Showing 37 changed files with 2,153 additions and 2,124 deletions.
2 changes: 1 addition & 1 deletion libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void apply_context::exec_one()

if( ( receiver_account->code_hash != digest_type() ) &&
( !( act->account == config::system_account_name
&& act->name == N( setcode )
&& act->name == "setcode"_n
&& receiver == config::system_account_name )
|| control.is_builtin_activated( builtin_protocol_feature_t::forward_setcode )
)
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,7 @@ struct controller_impl {
{
action on_block_act;
on_block_act.account = config::system_account_name;
on_block_act.name = N(onblock);
on_block_act.name = "onblock"_n;
on_block_act.authorization = vector<permission_level>{{config::system_account_name, config::active_name}};
on_block_act.data = fc::raw::pack(self.head_block_header());

Expand Down
22 changes: 11 additions & 11 deletions libraries/chain/include/eosio/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ const static auto default_state_size = 1*1024*1024*1024ll;
const static auto default_state_guard_size = 128*1024*1024ll;


const static name system_account_name { N(eosio) };
const static name null_account_name { N(eosio.null) };
const static name producers_account_name { N(eosio.prods) };
const static name system_account_name { "eosio"_n };
const static name null_account_name { "eosio.null"_n };
const static name producers_account_name { "eosio.prods"_n };

// Active permission of producers account requires greater than 2/3 of the producers to authorize
const static name majority_producers_permission_name { N(prod.major) }; // greater than 1/2 of producers needed to authorize
const static name minority_producers_permission_name { N(prod.minor) }; // greater than 1/3 of producers needed to authorize0
const static name majority_producers_permission_name { "prod.major"_n }; // greater than 1/2 of producers needed to authorize
const static name minority_producers_permission_name { "prod.minor"_n }; // greater than 1/3 of producers needed to authorize0

const static name eosio_auth_scope { N(eosio.auth) };
const static name eosio_all_scope { N(eosio.all) };
const static name eosio_auth_scope { "eosio.auth"_n };
const static name eosio_all_scope { "eosio.all"_n };

const static name active_name { N(active) };
const static name owner_name { N(owner) };
const static name eosio_any_name { N(eosio.any) };
const static name eosio_code_name { N(eosio.code) };
const static name active_name { "active"_n };
const static name owner_name { "owner"_n };
const static name eosio_any_name { "eosio.any"_n };
const static name eosio_code_name { "eosio.code"_n };

const static int block_interval_ms = 500;
const static int block_interval_us = block_interval_ms*1000;
Expand Down
18 changes: 9 additions & 9 deletions libraries/chain/include/eosio/chain/contract_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct newaccount {
}

static action_name get_name() {
return N(newaccount);
return "newaccount"_n;
}
};

Expand All @@ -33,7 +33,7 @@ struct setcode {
}

static action_name get_name() {
return N(setcode);
return "setcode"_n;
}
};

Expand All @@ -46,7 +46,7 @@ struct setabi {
}

static action_name get_name() {
return N(setabi);
return "setabi"_n;
}
};

Expand All @@ -62,7 +62,7 @@ struct updateauth {
}

static action_name get_name() {
return N(updateauth);
return "updateauth"_n;
}
};

Expand All @@ -80,7 +80,7 @@ struct deleteauth {
}

static action_name get_name() {
return N(deleteauth);
return "deleteauth"_n;
}
};

Expand All @@ -100,7 +100,7 @@ struct linkauth {
}

static action_name get_name() {
return N(linkauth);
return "linkauth"_n;
}
};

Expand All @@ -119,7 +119,7 @@ struct unlinkauth {
}

static action_name get_name() {
return N(unlinkauth);
return "unlinkauth"_n;
}
};

Expand All @@ -132,7 +132,7 @@ struct canceldelay {
}

static action_name get_name() {
return N(canceldelay);
return "canceldelay"_n;
}
};

Expand All @@ -148,7 +148,7 @@ struct onerror {
}

static action_name get_name() {
return N(onerror);
return "onerror"_n;
}
};

Expand Down
46 changes: 29 additions & 17 deletions libraries/chain/include/eosio/chain/name.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <string>
#include <fc/reflect/reflect.hpp>
#include <fc/exception/exception.hpp>
#include <eosio/chain/exceptions.hpp>
#include <iosfwd>

namespace eosio::chain {
Expand All @@ -13,29 +15,41 @@ namespace fc {
} // fc

namespace eosio::chain {
inline constexpr uint64_t char_to_symbol( char c ) {
constexpr uint64_t char_to_symbol( char c ) {
if( c >= 'a' && c <= 'z' )
return (c - 'a') + 6;
if( c >= '1' && c <= '5' )
return (c - '1') + 1;
else if( c == '.')
return 0;
else
FC_THROW_EXCEPTION(name_type_exception, "Name contains invalid character: (${c}) ", ("c", std::string(1, c)));

//unreachable
return 0;
}

inline constexpr uint64_t string_to_uint64_t( std::string_view str ) {
// true if std::string can be converted to name
bool is_string_valid_name(std::string_view str);

constexpr uint64_t string_to_uint64_t( std::string_view str ) {
EOS_ASSERT(str.size() <= 13, name_type_exception, "Name is longer than 13 characters (${name}) ", ("name", std::string(str)));

uint64_t n = 0;
int i = 0;
for ( ; i < str.size() && i < 12; ++i) {
// NOTE: char_to_symbol() returns char type, and without this explicit
// expansion to uint64 type, the compilation fails at the point of usage
// of string_to_name(), where the usage requires constant (compile time) expression.
n |= (char_to_symbol(str[i]) & 0x1f) << (64 - 5 * (i + 1));
int i = (int) str.size();
if (i >= 13) {
// Only the first 12 characters can be full-range ([.1-5a-z]).
i = 12;

// The 13th character must be in the range [.1-5a-j] because it needs to be encoded
// using only four bits (64_bits - 5_bits_per_char * 12_chars).
n = char_to_symbol(str[12]);
EOS_ASSERT(n <= 0x0Full, name_type_exception, "invalid 13th character: (${c})", ("c", std::string(1, str[12])));
}
// Encode full-range characters.
while (--i >= 0) {
n |= char_to_symbol(str[i]) << (64 - 5 * (i + 1));
}

// The for-loop encoded up to 60 high bits into uint64 'name' variable,
// if (strlen(str) > 12) then encode str[12] into the low (remaining)
// 4 bits of 'name'
if (i < str.size() && i == 12)
n |= char_to_symbol(str[12]) & 0x0F;
return n;
}

Expand Down Expand Up @@ -81,7 +95,7 @@ namespace eosio::chain {
// to its 5-bit slot starting with the highest slot for the first char.
// The 13th char, if str is long enough, is encoded into 4-bit chunk
// and placed in the lowest 4 bits. 64 = 12 * 5 + 4
inline constexpr name string_to_name( std::string_view str )
constexpr name string_to_name( std::string_view str )
{
return name( string_to_uint64_t( str ) );
}
Expand All @@ -101,8 +115,6 @@ namespace eosio::chain {
#endif
} // namespace literals

#define N(X) eosio::chain::string_to_name(#X)

} // eosio::chain

namespace std {
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace eosio { namespace chain {
if (tt.action_traces.empty())
return false;
const auto& act = tt.action_traces[0].act;
if (act.account != eosio::chain::config::system_account_name || act.name != N(onblock) ||
if (act.account != eosio::chain::config::system_account_name || act.name != "onblock"_n ||
act.authorization.size() != 1)
return false;
const auto& auth = act.authorization[0];
Expand Down
33 changes: 26 additions & 7 deletions libraries/chain/name.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#include <eosio/chain/name.hpp>
#include <fc/variant.hpp>
#include <boost/algorithm/string.hpp>
#include <fc/exception/exception.hpp>
#include <eosio/chain/exceptions.hpp>

namespace eosio::chain {

void name::set( std::string_view str ) {
const auto len = str.size();
EOS_ASSERT(len <= 13, name_type_exception, "Name is longer than 13 characters (${name}) ", ("name", std::string(str)));
value = string_to_uint64_t(str);
EOS_ASSERT(to_string() == str, name_type_exception,
"Name not properly normalized (name: ${name}, normalized: ${normalized}) ",
("name", std::string(str))("normalized", to_string()));
}

// keep in sync with name::to_string() in contract definition for name
Expand All @@ -32,6 +25,32 @@ namespace eosio::chain {
return str;
}

bool is_string_valid_name(std::string_view str)
{
size_t slen = str.size();
if( slen > 13)
return false;

size_t len = (slen <= 12) ? slen : 12;
for( size_t i = 0; i < len; ++i ) {
char c = str[i];
if ((c >= 'a' && c <= 'z') || (c >= '1' && c <= '5') || (c == '.'))
continue;
else
return false;
}

if( slen == 13) {
char c = str[12];
if ((c >= 'a' && c <= 'j') || (c >= '1' && c <= '5') || (c == '.'))
return true;
else
return false;
}

return true;
}

} // eosio::chain

namespace fc {
Expand Down
10 changes: 8 additions & 2 deletions libraries/state_history/include/eosio/state_history/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ namespace eosio {
* payload
*/

inline uint64_t ship_magic(uint32_t version) { return N(ship).to_uint64_t() | version; }
inline bool is_ship(uint64_t magic) { return (magic & 0xffff'ffff'0000'0000) == N(ship).to_uint64_t(); }
inline uint64_t ship_magic(uint32_t version) {
using namespace eosio::chain::literals;
return "ship"_n.to_uint64_t() | version;
}
inline bool is_ship(uint64_t magic) {
using namespace eosio::chain::literals;
return (magic & 0xffff'ffff'0000'0000) == "ship"_n.to_uint64_t();
}
inline uint32_t get_ship_version(uint64_t magic) { return magic; }
inline bool is_ship_supported_version(uint64_t magic) { return get_ship_version(magic) == 0; }
static const uint32_t ship_current_version = 0;
Expand Down
8 changes: 4 additions & 4 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ namespace eosio { namespace testing {
const symbol& asset_symbol,
const account_name& account ) const {
const auto& db = control->db();
const auto* tbl = db.template find<table_id_object, by_code_scope_table>(boost::make_tuple(code, account, N(accounts)));
const auto* tbl = db.template find<table_id_object, by_code_scope_table>(boost::make_tuple(code, account, "accounts"_n));
share_type result = 0;

// the balance is implied to be 0 if either the table or row does not exist
Expand Down Expand Up @@ -1098,7 +1098,7 @@ namespace eosio { namespace testing {
schedule_variant.emplace_back(e.get_abi_variant());
}

return push_action( config::system_account_name, N(setprods), config::system_account_name,
return push_action( config::system_account_name, "setprods"_n, config::system_account_name,
fc::mutable_variant_object()("schedule", schedule_variant));

}
Expand All @@ -1115,7 +1115,7 @@ namespace eosio { namespace testing {
});
}

return push_action( config::system_account_name, N(setprods), config::system_account_name,
return push_action( config::system_account_name, "setprods"_n, config::system_account_name,
fc::mutable_variant_object()("schedule", legacy_keys));

}
Expand All @@ -1136,7 +1136,7 @@ namespace eosio { namespace testing {

void base_tester::preactivate_protocol_features(const vector<digest_type> feature_digests) {
for( const auto& feature_digest: feature_digests ) {
push_action( config::system_account_name, N(activate), config::system_account_name,
push_action( config::system_account_name, "activate"_n, config::system_account_name,
fc::mutable_variant_object()("feature_digest", feature_digest) );
}
}
Expand Down
9 changes: 5 additions & 4 deletions plugins/chain_plugin/account_query_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <shared_mutex>

using namespace eosio;
using namespace eosio::chain::literals;
using namespace boost::multi_index;
using namespace boost::bimaps;

Expand Down Expand Up @@ -68,7 +69,7 @@ namespace {
if (p->action_traces.empty())
return false;
const auto& act = p->action_traces[0].act;
if (act.account != eosio::chain::config::system_account_name || act.name != N(onblock) ||
if (act.account != eosio::chain::config::system_account_name || act.name != "onblock"_n ||
act.authorization.size() != 1)
return false;
const auto& auth = act.authorization[0];
Expand Down Expand Up @@ -327,9 +328,9 @@ namespace eosio::chain_apis {
auto itr = deleted.emplace(chain::permission_level{data.account, data.permission}).first;
updated.erase(*itr);
} else if (at.act.name == chain::newaccount::get_name()) {
auto data = at.act.data_as<chain::newaccount>();
updated.emplace(chain::permission_level{data.name, N(owner)});
updated.emplace(chain::permission_level{data.name, N(active)});
auto data = at.act.data_as<chain::newaccount>();
updated.emplace(chain::permission_level{data.name, "owner"_n});
updated.emplace(chain::permission_level{data.name, "active"_n});
}
}
};
Expand Down
Loading

0 comments on commit 8c567fd

Please sign in to comment.