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

Unify types #18

Merged
merged 2 commits into from
May 4, 2017
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
2 changes: 1 addition & 1 deletion libraries/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace eos { namespace chain {
* as part of "generated input" its ID will be used without the extra hash.
*/
for( const auto& trx : output_transactions )
ids.push_back( digest_type::hash(trx.digest()) );
ids.push_back( digest_type::hash(trx.merkle_digest()) );


return merkle(ids);
Expand Down
39 changes: 22 additions & 17 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ optional<signed_block> database::fetch_block_by_number(uint32_t num)const
return optional<signed_block>();
}

const signed_transaction& database::get_recent_transaction(const transaction_id_type& trx_id) const
const SignedTransaction& database::get_recent_transaction(const transaction_id_type& trx_id) const
{
auto& index = get_index<transaction_multi_index, by_trx_id>();
auto itr = index.find(trx_id);
Expand Down Expand Up @@ -250,7 +250,7 @@ bool database::_push_block(const signed_block& new_block)
* queues full as well, it will be kept in the queue to be propagated later when a new block flushes out the pending
* queues.
*/
void database::push_transaction(const signed_transaction& trx, uint32_t skip)
void database::push_transaction(const SignedTransaction& trx, uint32_t skip)
{ try {
with_skip_flags(skip, [&]() {
with_producing([&](){
Expand All @@ -261,7 +261,7 @@ void database::push_transaction(const signed_transaction& trx, uint32_t skip)
});
} FC_CAPTURE_AND_RETHROW((trx)) }

void database::_push_transaction(const signed_transaction& trx) {
void database::_push_transaction(const SignedTransaction& trx) {

// If this is the first transaction pushed after applying a block, start a new undo session.
// This allows us to quickly rewind to the clean state of the head block, in case a new block arrives.
Expand Down Expand Up @@ -339,7 +339,7 @@ signed_block database::_generate_block(

uint64_t postponed_tx_count = 0;
// pop pending state (reset to head block state)
for( const signed_transaction& tx : _pending_transactions )
for( const SignedTransaction& tx : _pending_transactions )
{
size_t new_total_size = total_block_size + fc::raw::pack_size( tx );

Expand Down Expand Up @@ -492,12 +492,12 @@ void database::_apply_block(const signed_block& next_block)

} FC_CAPTURE_AND_RETHROW( (next_block.block_num()) ) }

void database::apply_transaction(const signed_transaction& trx, uint32_t skip)
void database::apply_transaction(const SignedTransaction& trx, uint32_t skip)
{
with_skip_flags( skip, [&]() { _apply_transaction(trx); });
}

void database::validate_transaction(const signed_transaction& trx)const {
void database::validate_transaction(const SignedTransaction& trx)const {
try {
EOS_ASSERT(trx.messages.size() > 0, transaction_exception, "A transaction must have at least one message");

Expand All @@ -507,7 +507,8 @@ try {
validate_expiration(trx);
validate_message_types(trx);

for (const auto& m : trx.messages) { /// TODO: this loop can be processed in parallel
for (const auto& tm : trx.messages) { /// TODO: this loop can be processed in parallel
Message m(tm);
message_validate_context mvc(trx, m);
auto contract_handlers_itr = message_validate_handlers.find(m.recipient);
if (contract_handlers_itr != message_validate_handlers.end()) {
Expand All @@ -522,7 +523,7 @@ try {
}
} FC_CAPTURE_AND_RETHROW( (trx) ) }

void database::validate_uniqueness( const signed_transaction& trx )const {
void database::validate_uniqueness( const SignedTransaction& trx )const {
if( !should_check_for_duplicate_transactions() ) return;

auto trx_id = trx.id();
Expand All @@ -531,20 +532,20 @@ void database::validate_uniqueness( const signed_transaction& trx )const {
transaction_exception, "Transaction is not unique");
}

void database::validate_tapos(const signed_transaction& trx)const {
void database::validate_tapos(const SignedTransaction& trx)const {
if (!should_check_tapos()) return;

const auto& tapos_block_summary = get<block_summary_object>(trx.ref_block_num);
const auto& tapos_block_summary = 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(trx.verify_reference_block(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));
}

void database::validate_referenced_accounts(const signed_transaction& trx)const {
for(const auto& auth : trx.provided_authorizations) {
get_account(auth.authorizing_account);
void database::validate_referenced_accounts(const SignedTransaction& trx)const {
for(const auto& auth : trx.authorizations) {
get_account(auth.account);
}
for(const auto& msg : trx.messages) {
get_account(msg.sender);
Expand All @@ -566,7 +567,7 @@ void database::validate_referenced_accounts(const signed_transaction& trx)const
}
}

void database::validate_expiration(const signed_transaction& trx) const
void database::validate_expiration(const SignedTransaction& trx) const
{ try {
fc::time_point_sec now = head_block_time();
const chain_parameters& chain_parameters = get_global_properties().parameters;
Expand All @@ -579,7 +580,7 @@ void database::validate_expiration(const signed_transaction& trx) const
("now",now)("trx.exp",trx.expiration));
} FC_CAPTURE_AND_RETHROW((trx)) }

void database::validate_message_types(const signed_transaction& trx)const {
void database::validate_message_types(const SignedTransaction& trx)const {
try {
for( const auto& msg : trx.messages ) {
try {
Expand Down Expand Up @@ -639,11 +640,15 @@ void database::apply_message( apply_context& context )
} FC_CAPTURE_AND_RETHROW() }


void database::_apply_transaction(const signed_transaction& trx)
void database::_apply_transaction(const SignedTransaction& trx)
{ try {
validate_transaction(trx);

for( const auto& m : trx.messages ) {
// trx.messages is a vector<types::Message>, but we want a chain::Message, so go ahead and copy it into one.
for( Message m : trx.messages ) {
// apply_context will store a reference to the chain::Message argument. m must *not* be a types::Message here, or
// the compiler will create a temporary chain::Message out of the types::Message and pass a reference to that to
// ac. ac will keep the temporary reference, which will be invalid as soon as this next line finishes running.
apply_context ac( *this, trx, m, m.recipient );

/** TODO: pre condition validation and application can occur in parallel */
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eos/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace eos { namespace chain {

struct thread {
vector<generated_transaction_id_type> generated_input;
vector<signed_transaction> user_input;
vector<SignedTransaction> user_input;
vector<generated_transaction> output_transactions;

digest_type merkle_digest() const;
Expand Down
36 changes: 18 additions & 18 deletions libraries/chain/include/eos/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ namespace eos { namespace chain {

class message_validate_context {
public:
message_validate_context( const transaction& t, const message& m )
message_validate_context( const Transaction& t, const Message& m )
:trx(t),msg(m){}

const transaction& trx;
const message& msg;
const Transaction& trx;
const Message& msg;
};


class precondition_validate_context : public message_validate_context {
public:
precondition_validate_context( const database& d, const transaction& t, const message& m, const AccountName& r )
precondition_validate_context( const database& d, const Transaction& t, const Message& m, const AccountName& r )
:message_validate_context(t,m),recipient(r),db(d){}


Expand All @@ -66,7 +66,7 @@ namespace eos { namespace chain {

class apply_context : public precondition_validate_context {
public:
apply_context( database& d, const transaction& t, const message& m, const AccountName& recipient )
apply_context( database& d, const Transaction& t, const Message& m, const AccountName& recipient )
:precondition_validate_context(d,t,m,recipient),mutable_db(d){}

String get( String key )const;
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace eos { namespace chain {
* This signal is emitted any time a new transaction is added to the pending
* block state.
*/
fc::signal<void(const signed_transaction&)> on_pending_transaction;
fc::signal<void(const SignedTransaction&)> on_pending_transaction;

template<typename T>
void register_type( AccountName scope ) {
Expand Down Expand Up @@ -190,7 +190,7 @@ namespace eos { namespace chain {
block_id_type get_block_id_for_num( uint32_t block_num )const;
optional<signed_block> fetch_block_by_id( const block_id_type& id )const;
optional<signed_block> fetch_block_by_number( uint32_t num )const;
const signed_transaction& get_recent_transaction( const transaction_id_type& trx_id )const;
const SignedTransaction& get_recent_transaction( const transaction_id_type& trx_id )const;
std::vector<block_id_type> get_block_ids_on_fork(block_id_type head_of_fork) const;

const account_object& get_account(const AccountName& name)const;
Expand All @@ -207,9 +207,9 @@ namespace eos { namespace chain {
bool before_last_checkpoint()const;

bool push_block( const signed_block& b, uint32_t skip = skip_nothing );
void push_transaction( const signed_transaction& trx, uint32_t skip = skip_nothing );
void push_transaction( const SignedTransaction& trx, uint32_t skip = skip_nothing );
bool _push_block( const signed_block& b );
void _push_transaction( const signed_transaction& trx );
void _push_transaction( const SignedTransaction& trx );

signed_block generate_block(
const fc::time_point_sec when,
Expand Down Expand Up @@ -331,23 +331,23 @@ namespace eos { namespace chain {

// these were formerly private, but they have a fairly well-defined API, so let's make them public
void apply_block(const signed_block& next_block, uint32_t skip = skip_nothing);
void apply_transaction(const signed_transaction& trx, uint32_t skip = skip_nothing);
void apply_transaction(const SignedTransaction& trx, uint32_t skip = skip_nothing);

private:
void _apply_block(const signed_block& next_block);
void _apply_transaction(const signed_transaction& trx);
void _apply_transaction(const SignedTransaction& trx);

/**
* This method validates transactions without adding it to the pending state.
* @return true if the transaction would validate
*/
void validate_transaction(const signed_transaction& trx)const;
void validate_transaction(const SignedTransaction& trx)const;
/// Validate transaction helpers @{
void validate_uniqueness(const signed_transaction& trx)const;
void validate_tapos(const signed_transaction& trx)const;
void validate_referenced_accounts(const signed_transaction& trx)const;
void validate_expiration(const signed_transaction& trx) const;
void validate_message_types( const signed_transaction& trx )const;
void validate_uniqueness(const SignedTransaction& trx)const;
void validate_tapos(const SignedTransaction& trx)const;
void validate_referenced_accounts(const SignedTransaction& trx)const;
void validate_expiration(const SignedTransaction& trx) const;
void validate_message_types( const SignedTransaction& trx )const;
/// @}

void validate_message_precondition(precondition_validate_context& c)const;
Expand All @@ -371,7 +371,7 @@ namespace eos { namespace chain {
void clear_expired_transactions();

optional<session> _pending_tx_session;
deque<signed_transaction> _pending_transactions;
deque<SignedTransaction> _pending_transactions;
fork_database _fork_db;

block_log _block_log;
Expand Down
34 changes: 8 additions & 26 deletions libraries/chain/include/eos/chain/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,17 @@ namespace eos { namespace chain {
* @ref data, whose type is determined by @ref type, which is
* dynamic and defined by the scripting language.
*/
struct message {
/// The account which sent the message
AccountName sender;

/// The account to receive the message
AccountName recipient;

/// Other accounts to notify about this message
vector<AccountName> notify;

/**
* Every contract defines the set of types that it accepts, these types are
* scoped according to the recipient. This means two contracts can can define
* two different types with the same name.
*/
types::TypeName type;

/// The message contents
vector<char> data;

message() = default;
message(const AccountName& sender, const AccountName& recipient, const vector<AccountName>& notify)
: sender(sender), recipient(recipient), notify(notify) {}
struct Message : public types::Message {
Message() = default;
Message(const AccountName& sender, const AccountName& recipient, const vector<AccountName>& notify)
: types::Message(sender, recipient, notify, "", {}) {}
template<typename T>
message(const AccountName& sender, const AccountName& recipient, const vector<AccountName>& notify,
Message(const AccountName& sender, const AccountName& recipient, const vector<AccountName>& notify,
const TypeName& type, T&& value)
: message(sender, recipient, notify) {
: Message(sender, recipient, notify) {
set<T>(type, std::forward<T>(value));
}
Message(const types::Message& m) : types::Message(m) {}

template<typename T>
void set(const TypeName& t, const T& value) {
Expand All @@ -68,4 +50,4 @@ struct message {

} } // namespace eos::chain

FC_REFLECT(eos::chain::message, (sender)(recipient)(notify)(type)(data))
FC_REFLECT(eos::chain::Message, (sender)(recipient)(notify)(type)(data))
Loading