Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require dry-run trxs to have authorization and add tests #1489

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ struct controller_impl {
);
}

sha256 calculate_integrity_hash() {
sha256::encoder enc;
fc::sha256 calculate_integrity_hash() {
fc::sha256::encoder enc;
auto hash_writer = std::make_shared<integrity_hash_snapshot_writer>(enc);
add_to_snapshot(hash_writer);
hash_writer->finalize();
Expand Down Expand Up @@ -2091,7 +2091,7 @@ struct controller_impl {
} else {
packed_transaction_ptr ptrx( b, &pt ); // alias signed_block_ptr
auto fut = transaction_metadata::start_recover_keys(
std::move( ptrx ), thread_pool.get_executor(), chain_id, microseconds::maximum(), transaction_metadata::trx_type::input );
std::move( ptrx ), thread_pool.get_executor(), chain_id, fc::microseconds::maximum(), transaction_metadata::trx_type::input );
trx_metas.emplace_back( transaction_metadata_ptr{}, std::move( fut ) );
}
}
Expand Down Expand Up @@ -3218,7 +3218,7 @@ block_id_type controller::get_block_id_for_num( uint32_t block_num )const { try
return id;
} FC_CAPTURE_AND_RETHROW( (block_num) ) }

sha256 controller::calculate_integrity_hash() { try {
fc::sha256 controller::calculate_integrity_hash() { try {
return my->calculate_integrity_hash();
} FC_LOG_AND_RETHROW() }

Expand Down
1 change: 1 addition & 0 deletions libraries/chain/include/eosio/chain/apply_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace chainbase { class database; }
namespace eosio { namespace chain {

class controller;
class account_metadata_object;

class apply_context {
private:
Expand Down
4 changes: 1 addition & 3 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <chainbase/pinnable_mapped_file.hpp>
#include <boost/signals2/signal.hpp>

#include <eosio/chain/abi_serializer.hpp>
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/snapshot.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/webassembly/eos-vm-oc/config.hpp>
Expand Down Expand Up @@ -259,7 +257,7 @@ namespace eosio { namespace chain {
// thread-safe
block_id_type get_block_id_for_num( uint32_t block_num )const;

sha256 calculate_integrity_hash();
fc::sha256 calculate_integrity_hash();
void write_snapshot( const snapshot_writer_ptr& snapshot );

bool sender_avoids_whitelist_blacklist_enforcement( account_name sender )const;
Expand Down
13 changes: 2 additions & 11 deletions libraries/chain/transaction_context.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain/apply_context.hpp>
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/chain/exceptions.hpp>
Expand All @@ -8,16 +9,6 @@
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/deep_mind.hpp>

#pragma push_macro("N")
#undef N
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/min.hpp>
#include <boost/accumulators/statistics/max.hpp>
#include <boost/accumulators/statistics/weighted_mean.hpp>
#include <boost/accumulators/statistics/weighted_variance.hpp>
#pragma pop_macro("N")

#include <chrono>

namespace eosio { namespace chain {
Expand Down Expand Up @@ -828,7 +819,7 @@ namespace eosio { namespace chain {
actors.insert( auth.actor );
}
}
EOS_ASSERT( one_auth || is_transient(), tx_no_auths, "transaction must have at least one authorization" );
EOS_ASSERT( one_auth || is_read_only(), tx_no_auths, "transaction must have at least one authorization" );
linh2931 marked this conversation as resolved.
Show resolved Hide resolved

if( enforce_actor_whitelist_blacklist ) {
control.check_actor_list( actors );
Expand Down
4 changes: 3 additions & 1 deletion libraries/chain/webassembly/authorization.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <eosio/chain/webassembly/interface.hpp>
#include <eosio/chain/apply_context.hpp>

#include <fc/io/datastream.hpp>

namespace eosio { namespace chain { namespace webassembly {
void interface::require_auth( account_name account ) const {
context.require_authorization( account );
Expand Down Expand Up @@ -42,7 +44,7 @@ namespace eosio { namespace chain { namespace webassembly {

auto s = fc::raw::pack_size(result);
if (s <= packed_result.size()) {
datastream<char*> ds(packed_result.data(), s);
fc::datastream<char*> ds(packed_result.data(), s);
fc::raw::pack(ds, result);
}
return s;
Expand Down
9 changes: 5 additions & 4 deletions libraries/chain/webassembly/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <eosio/chain/protocol_state_object.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/apply_context.hpp>
#include <fc/io/datastream.hpp>
#include <fc/crypto/modular_arithmetic.hpp>
#include <fc/crypto/blake2.hpp>
#include <fc/crypto/sha3.hpp>
Expand All @@ -25,8 +26,8 @@ namespace eosio { namespace chain { namespace webassembly {
legacy_span<const char> pub ) const {
fc::crypto::signature s;
fc::crypto::public_key p;
datastream<const char*> ds( sig.data(), sig.size() );
datastream<const char*> pubds ( pub.data(), pub.size() );
fc::datastream<const char*> ds( sig.data(), sig.size() );
fc::datastream<const char*> pubds ( pub.data(), pub.size() );

fc::raw::unpack( ds, s );
fc::raw::unpack( pubds, p );
Expand All @@ -48,7 +49,7 @@ namespace eosio { namespace chain { namespace webassembly {
legacy_span<const char> sig,
legacy_span<char> pub ) const {
fc::crypto::signature s;
datastream<const char*> ds( sig.data(), sig.size() );
fc::datastream<const char*> ds( sig.data(), sig.size() );
fc::raw::unpack(ds, s);

EOS_ASSERT(s.which() < context.db.get<protocol_state_object>().num_supported_key_types, unactivated_signature_type,
Expand All @@ -74,7 +75,7 @@ namespace eosio { namespace chain { namespace webassembly {
// this will do one less copy for those keys while maintaining the rules of
// [0..33) dest sizes: assert (asserts in fc::raw::pack)
// [33..inf) dest sizes: return packed size (always 33)
datastream<char*> out_ds( pub.data(), pub.size() );
fc::datastream<char*> out_ds( pub.data(), pub.size() );
fc::raw::pack(out_ds, recovered);
return out_ds.tellp();
}
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/webassembly/permission.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain/webassembly/interface.hpp>
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/apply_context.hpp>
Expand Down
21 changes: 12 additions & 9 deletions libraries/chain/webassembly/privileged.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/webassembly/interface.hpp>
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/protocol_state_object.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/apply_context.hpp>

#include <fc/io/datastream.hpp>

#include <vector>
#include <set>

Expand Down Expand Up @@ -94,15 +97,15 @@ namespace eosio { namespace chain { namespace webassembly {
return s;

if ( s <= packed_parameters.size() ) {
datastream<char*> ds( packed_parameters.data(), s );
fc::datastream<char*> ds( packed_parameters.data(), s );
fc::raw::pack(ds, version);
fc::raw::pack(ds, params);
}
return s;
}
void interface::set_wasm_parameters_packed( span<const char> packed_parameters ) {
EOS_ASSERT(!context.trx_context.is_read_only(), wasm_execution_error, "set_wasm_parameters_packed not allowed in a readonly transaction");
datastream<const char*> ds( packed_parameters.data(), packed_parameters.size() );
fc::datastream<const char*> ds( packed_parameters.data(), packed_parameters.size() );
uint32_t version;
chain::wasm_config cfg;
fc::raw::unpack(ds, version);
Expand All @@ -117,7 +120,7 @@ namespace eosio { namespace chain { namespace webassembly {
}
int64_t interface::set_proposed_producers( legacy_span<const char> packed_producer_schedule) {
EOS_ASSERT(!context.trx_context.is_read_only(), wasm_execution_error, "set_proposed_producers not allowed in a readonly transaction");
datastream<const char*> ds( packed_producer_schedule.data(), packed_producer_schedule.size() );
fc::datastream<const char*> ds( packed_producer_schedule.data(), packed_producer_schedule.size() );
std::vector<producer_authority> producers;
std::vector<legacy::producer_key> old_version;
fc::raw::unpack(ds, old_version);
Expand All @@ -137,7 +140,7 @@ namespace eosio { namespace chain { namespace webassembly {
if (packed_producer_format == 0) {
return set_proposed_producers(std::move(packed_producer_schedule));
} else if (packed_producer_format == 1) {
datastream<const char*> ds( packed_producer_schedule.data(), packed_producer_schedule.size() );
fc::datastream<const char*> ds( packed_producer_schedule.data(), packed_producer_schedule.size() );
vector<producer_authority> producers;

fc::raw::unpack(ds, producers);
Expand All @@ -154,7 +157,7 @@ namespace eosio { namespace chain { namespace webassembly {
if( packed_blockchain_parameters.size() == 0 ) return s;

if ( s <= packed_blockchain_parameters.size() ) {
datastream<char*> ds( packed_blockchain_parameters.data(), s );
fc::datastream<char*> ds( packed_blockchain_parameters.data(), s );
fc::raw::pack(ds, gpo.configuration.v0());
return s;
}
Expand All @@ -163,7 +166,7 @@ namespace eosio { namespace chain { namespace webassembly {

void interface::set_blockchain_parameters_packed( legacy_span<const char> packed_blockchain_parameters ) {
EOS_ASSERT(!context.trx_context.is_read_only(), wasm_execution_error, "set_blockchain_parameters_packed not allowed in a readonly transaction");
datastream<const char*> ds( packed_blockchain_parameters.data(), packed_blockchain_parameters.size() );
fc::datastream<const char*> ds( packed_blockchain_parameters.data(), packed_blockchain_parameters.size() );
chain::chain_config_v0 cfg;
fc::raw::unpack(ds, cfg);
cfg.validate();
Expand All @@ -174,7 +177,7 @@ namespace eosio { namespace chain { namespace webassembly {
}

uint32_t interface::get_parameters_packed( span<const char> packed_parameter_ids, span<char> packed_parameters) const{
datastream<const char*> ds_ids( packed_parameter_ids.data(), packed_parameter_ids.size() );
fc::datastream<const char*> ds_ids( packed_parameter_ids.data(), packed_parameter_ids.size() );

chain::chain_config cfg = context.control.get_global_properties().configuration;
std::vector<fc::unsigned_int> ids;
Expand All @@ -188,14 +191,14 @@ namespace eosio { namespace chain { namespace webassembly {
chain::config_parse_error,
"get_parameters_packed: buffer size is smaller than ${size}", ("size", size));

datastream<char*> ds( packed_parameters.data(), size );
fc::datastream<char*> ds( packed_parameters.data(), size );
fc::raw::pack( ds, config_range );
return size;
}

void interface::set_parameters_packed( span<const char> packed_parameters ){
EOS_ASSERT(!context.trx_context.is_read_only(), wasm_execution_error, "set_parameters_packed not allowed in a readonly transaction");
datastream<const char*> ds( packed_parameters.data(), packed_parameters.size() );
fc::datastream<const char*> ds( packed_parameters.data(), packed_parameters.size() );

chain::chain_config cfg = context.control.get_global_properties().configuration;
config_range config_range(cfg, {context.control});
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/webassembly/runtimes/eos-vm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <eosio/chain/webassembly/eos-vm.hpp>
#include <eosio/chain/webassembly/interface.hpp>
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/apply_context.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/global_property_object.hpp>
Expand Down
6 changes: 4 additions & 2 deletions unittests/delay_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1901,9 +1901,11 @@ BOOST_AUTO_TEST_CASE( canceldelay_test ) { try {

chain.set_transaction_headers(trx);
trx.sign(chain.get_private_key("tester"_n, "active"), chain.control->get_chain_id());
trace = chain.push_transaction(trx);
//wdump((fc::json::to_pretty_string(trace)));
// first push as a dry_run trx
trace = chain.push_transaction(trx, fc::time_point::maximum(), base_tester::DEFAULT_BILLED_CPU_TIME_US, false, transaction_metadata::trx_type::dry_run);
BOOST_REQUIRE_EQUAL(transaction_receipt::executed, trace->receipt->status);
// now push for real
trace = chain.push_transaction(trx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably check the status again after the real execution.

gen_size = chain.control->db().get_index<generated_transaction_multi_index,by_trx_id>().size();
BOOST_CHECK_EQUAL(2u, gen_size);

Expand Down
Loading