Skip to content

Commit

Permalink
remove transaction_helpers namespace in favor of method prefix, ref E…
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderingbort committed Aug 28, 2017
1 parent 65a07be commit 81e9261
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 102 deletions.
2 changes: 1 addition & 1 deletion libraries/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace eos { namespace chain {
*/

for( const auto& trx : user_input )
ids.push_back( transaction_helpers::digest(trx) );
ids.push_back( transaction_digest(trx) );

/**
* When generating the merkle hash of an output transaction we hash it
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/chain_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ void chain_controller::validate_tapos(const Transaction& trx)const {
const auto& tapos_block_summary = _db.get<block_summary_object>((uint16_t)trx.refBlockNum);

//Verify TaPoS block summary has correct ID prefix, and that this block's time is not past the expiration
EOS_ASSERT(transaction_helpers::verify_reference_block(trx, tapos_block_summary.block_id), transaction_exception,
EOS_ASSERT(transaction_verify_reference_block(trx, tapos_block_summary.block_id), transaction_exception,
"Transaction's reference block did not match. Is this transaction from a different fork?",
("tapos_summary", tapos_block_summary));
}
Expand Down
58 changes: 28 additions & 30 deletions libraries/chain/include/eos/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,41 @@ namespace eos { namespace chain {


/**
* @brief A base transaction without any explicit or implied authorizations.
* @brief methods that operate on @ref eos::types::Transaction.
*
* This is a utility class for sharing common operations between inheriting types which defines
* additional features and requirements.
* These are utility methods for sharing common operations between inheriting types which define
* additional features and requirements based off of @ref eos::types::Transaction.
*/
namespace transaction_helpers {
/// Calculate the digest for a transaction
digest_type digest(const Transaction& t);
/// Calculate the digest for a transaction
digest_type transaction_digest(const Transaction& t);

void set_reference_block(Transaction& t, const block_id_type& reference_block);
void transaction_set_reference_block(Transaction& t, const block_id_type& reference_block);

bool verify_reference_block(const Transaction& t, const block_id_type& reference_block);
bool transaction_verify_reference_block(const Transaction& t, const block_id_type& reference_block);

template <typename T>
void set_message(Transaction& t, int index, const types::FuncName& type, T&& value) {
Message m(t.messages[index]);
m.set(type, std::forward<T>(value));
t.messages[index] = m;
}
template <typename T>
void transaction_set_message(Transaction& t, int index, const types::FuncName& type, T&& value) {
Message m(t.messages[index]);
m.set(type, std::forward<T>(value));
t.messages[index] = m;
}

template <typename T>
T message_as(Transaction& t, int index) {
Message m(t.messages[index]);
return m.as<T>();
}
template <typename T>
T transaction_message_as(Transaction& t, int index) {
Message m(t.messages[index]);
return m.as<T>();
}

template <typename... Args>
void emplace_message(Transaction& t, Args&&... a) {
t.messages.emplace_back(Message(std::forward<Args>(a)...));
}
template <typename... Args>
void transaction_emplace_message(Transaction& t, Args&&... a) {
t.messages.emplace_back(Message(std::forward<Args>(a)...));
}

/**
* clear all common data
*/
inline void clear(Transaction& t) {
t.messages.clear();
}
/**
* clear all common data
*/
inline void transaction_clear(Transaction& t) {
t.messages.clear();
}

/**
Expand Down Expand Up @@ -146,7 +144,7 @@ namespace eos { namespace chain {
/**
* Removes all messages, signatures, and authorizations
*/
void clear() { transaction_helpers::clear(*this); signatures.clear(); }
void clear() { transaction_clear(*this); signatures.clear(); }

digest_type merkle_digest() const;

Expand Down
12 changes: 4 additions & 8 deletions libraries/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,22 @@

namespace eos { namespace chain {

namespace transaction_helpers {

digest_type digest(const Transaction& t) {
digest_type transaction_digest(const Transaction& t) {
digest_type::encoder enc;
fc::raw::pack( enc, t );
return enc.result();
}

void set_reference_block(Transaction& t, const block_id_type& reference_block) {
void transaction_set_reference_block(Transaction& t, const block_id_type& reference_block) {
t.refBlockNum = fc::endian_reverse_u32(reference_block._hash[0]);
t.refBlockPrefix = reference_block._hash[1];
}

bool verify_reference_block(const Transaction& t, const block_id_type& reference_block) {
bool transaction_verify_reference_block(const Transaction& t, const block_id_type& reference_block) {
return t.refBlockNum == (decltype(t.refBlockNum))fc::endian_reverse_u32(reference_block._hash[0]) &&
t.refBlockPrefix == (decltype(t.refBlockPrefix))reference_block._hash[1];
}

} // namespace transaction_helpers

digest_type SignedTransaction::sig_digest( const chain_id_type& chain_id )const {
digest_type::encoder enc;
fc::raw::pack( enc, chain_id );
Expand All @@ -59,7 +55,7 @@ digest_type SignedTransaction::sig_digest( const chain_id_type& chain_id )const
}

eos::chain::transaction_id_type SignedTransaction::id() const {
auto h = transaction_helpers::digest(*this);
auto h = transaction_digest(*this);
transaction_id_type result;
memcpy(result._hash, h._hash, std::min(sizeof(result), sizeof(h)));
return result;
Expand Down
10 changes: 5 additions & 5 deletions programs/eosc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ eos::chain_apis::read_only::get_info_results get_info() {
fc::variant push_transaction( SignedTransaction& trx ) {
auto info = get_info();
trx.expiration = info.head_block_time + 100; //chain.head_block_time() + 100;
transaction_helpers::set_reference_block(trx, info.head_block_id);
transaction_set_reference_block(trx, info.head_block_id);
boost::sort( trx.scope );

return call( push_txn_func, trx );
Expand All @@ -127,7 +127,7 @@ void create_account( const vector<string>& cmd_line ) {

SignedTransaction trx;
trx.scope = sort_names({creator,eosaccnt});
transaction_helpers::emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{creator,"active"}}, "newaccount",
transaction_emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{creator,"active"}}, "newaccount",
types::newaccount{creator, newaccount, owner_auth,
active_auth, recovery_auth, deposit});

Expand Down Expand Up @@ -308,7 +308,7 @@ int send_command (const vector<string> &cmd_line)

SignedTransaction trx;
trx.scope = { config::EosContractName, account };
transaction_helpers::emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{account,"active"}},
transaction_emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{account,"active"}},
"setcode", handler );

std::cout << fc::json::to_pretty_string( push_transaction(trx) ) << std::endl;
Expand All @@ -322,11 +322,11 @@ int send_command (const vector<string> &cmd_line)

SignedTransaction trx;
trx.scope = sort_names({sender,recipient});
transaction_helpers::emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{sender,"active"}}, "transfer",
transaction_emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{sender,"active"}}, "transfer",
types::transfer{sender, recipient, amount});
auto info = get_info();
trx.expiration = info.head_block_time + 100; //chain.head_block_time() + 100;
transaction_helpers::set_reference_block(trx, info.head_block_id);
transaction_set_reference_block(trx, info.head_block_id);

std::cout << fc::json::to_pretty_string( call( push_txn_func, trx )) << std::endl;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/api_tests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ uint32_t CallFunction( testing_blockchain& chain, const types::Message& msg, con
std::copy(data.begin(), data.end(), std::back_inserter(dest));

//std::cout << "MANDO: " << msg.code << " " << msg.type << std::endl;
transaction_helpers::emplace_message(trx, msg);
transaction_emplace_message(trx, msg);

trx.expiration = chain.head_block_time() + expiration++;
transaction_helpers::set_reference_block(trx, chain.head_block_id());
transaction_set_reference_block(trx, chain.head_block_id());
//idump((trx));
chain.push_transaction(trx);

Expand Down Expand Up @@ -177,9 +177,9 @@ void send_set_code_message(testing_blockchain& chain, types::setcode& handler, A
trx.messages.resize(1);
trx.messages[0].authorization = {{account,"active"}};
trx.messages[0].code = config::EosContractName;
transaction_helpers::set_message(trx, 0, "setcode", handler);
transaction_set_message(trx, 0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
transaction_helpers::set_reference_block(trx, chain.head_block_id());
transaction_set_reference_block(trx, chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}
Expand Down
48 changes: 24 additions & 24 deletions tests/common/macro_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names({ #creator, config::EosContractName }); \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#creator, "active"}}, \
"newaccount", types::newaccount{#creator, #name, owner, active, recovery, deposit}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Created account " << #name); \
}
Expand Down Expand Up @@ -78,11 +78,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account}; \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#account,"active"}}, \
"updateauth", types::updateauth{#account, authname, parentname, auth}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Set " << #account << "'s " << authname << " authority."); \
}
Expand All @@ -91,11 +91,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account}; \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#account,"active"}}, \
"deleteauth", types::deleteauth{#account, authname}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Deleted " << #account << "'s " << authname << " authority."); \
}
Expand All @@ -104,11 +104,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account}; \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#account,"active"}}, \
"linkauth", types::linkauth{#account, #codeacct, messagetype, authname}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Link " << #codeacct << "::" << messagetype << " to " << #account \
<< "'s " << authname << " authority."); \
Expand All @@ -119,11 +119,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account}; \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#account,"active"}}, \
"unlinkauth", types::unlinkauth{#account, #codeacct, messagetype}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Unlink " << #codeacct << "::" << messagetype << " from " << #account); \
}
Expand All @@ -133,11 +133,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names({#sender,#recipient}); \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{ {#sender,"active"} }, \
"transfer", types::transfer{#sender, #recipient, Amount.amount}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Transfered " << Amount << " from " << #sender << " to " << #recipient); \
}
Expand All @@ -147,10 +147,10 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { #sender, #recipient, config::EosContractName } ); \
transaction_helpers::emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{#sender, "active"}}, \
transaction_emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{#sender, "active"}}, \
"lock", types::lock{#sender, #recipient, amount}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Staked " << amount << " to " << #recipient); \
}
Expand All @@ -160,11 +160,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { config::EosContractName } ); \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#account, "active"}}, \
"unlock", types::unlock{#account, amount}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Begin unstake " << amount << " to " << #account); \
}
Expand All @@ -173,10 +173,10 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { config::EosContractName, #account } ); \
transaction_helpers::emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{#account, "active"}}, \
transaction_emplace_message(trx, config::EosContractName, vector<types::AccountPermission>{{#account, "active"}}, \
"claim", types::claim{#account, amount}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Finish unstake " << amount << " to " << #account); \
}
Expand All @@ -185,11 +185,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {#owner, config::EosContractName} ); \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#owner, "active"}}, \
"setproducer", types::setproducer{#owner, key, cfg}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Create producer " << #owner); \
}
Expand All @@ -202,11 +202,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {#voter, config::EosContractName} ); \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{#voter, "active"}}, \
"okproducer", types::okproducer{#voter, #producer, approved? 1 : 0}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Set producer approval from " << #voter << " for " << #producer << " to " << approved); \
}
Expand All @@ -215,11 +215,11 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {owner, config::EosContractName} ); \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{{owner, "active"}}, \
"setproducer", types::setproducer{owner, key, cfg}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
BOOST_TEST_CHECKPOINT("Update producer " << owner); \
}
Expand Down
6 changes: 3 additions & 3 deletions tests/common/testing_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@
{ \
eos::chain::SignedTransaction trx; \
if (std::string(#stakeholder) != std::string(#proxy)) \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{ {#stakeholder,"active"} }, "setproxy", types::setproxy{#stakeholder, #proxy}); \
else \
transaction_helpers::emplace_message(trx, config::EosContractName, \
transaction_emplace_message(trx, config::EosContractName, \
vector<types::AccountPermission>{ {#stakeholder,"active"} }, "setproxy", types::setproxy{#stakeholder, #proxy}); \
trx.expiration = chain.head_block_time() + 100; \
transaction_helpers::set_reference_block(trx, chain.head_block_id()); \
transaction_set_reference_block(trx, chain.head_block_id()); \
chain.push_transaction(trx); \
}

Expand Down
Loading

0 comments on commit 81e9261

Please sign in to comment.