diff --git a/contracts/eosiolib/transaction.h b/contracts/eosiolib/transaction.h index 068b8e93571473bf081ece30885c7f3283e3ee5f..749706b46c108e8a17b1f41ef69e07343adf0500 100644 --- a/contracts/eosiolib/transaction.h +++ b/contracts/eosiolib/transaction.h @@ -61,5 +61,39 @@ extern "C" { void send_deferred(uint32_t sender_id, time delay_until, char *serialized_transaction, size_t size); + /** + * access a copy of the currently executing transaction + * + * @param buffer - a buffer to write the current transaction to + * @param size - the size of the buffer + * @return the size of the transaction written to the buffer + */ + size_t read_transaction(char *buffer, size_t size); + + /** + * get the size of the currently executing transaction + * @return + */ + size_t transaction_size(); + + /** + * get the block number used for TAPOS on the currently executing transaction + * + * @return + */ + int tapos_block_num(); + + /** + * get the block prefix used for TAPOS on the currently executing transaction + * @return + */ + int tapos_block_prefix(); + + /** + * get the expiration of the currently executing transaction + * @return + */ + time expiration(); + ///@ } transactioncapi } diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index a993d2884b59e8f100155ec4daf9b2684791bee8..d82a549d5b4e0d975e2c775e1aed3d3e809ca9c6 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -233,4 +233,21 @@ vector apply_context::get_active_producers() const { return accounts; } + +const bytes& apply_context::get_packed_transaction() { + if( !trx_meta.packed_trx.size() ) { + if (_cached_trx.empty()) { + auto size = fc::raw::pack_size(trx_meta.trx); + _cached_trx.resize(size); + fc::datastream ds(_cached_trx.data(), size); + fc::raw::pack(ds, trx_meta.trx); + } + + return _cached_trx; + } + + return trx_meta.packed_trx; + +} + } } /// eosio::chain diff --git a/libraries/chain/include/eosio/chain/apply_context.hpp b/libraries/chain/include/eosio/chain/apply_context.hpp index e17b233166456ca143f78f330b6063f4e82a9a4a..09ac70a6859153c73102a1ddd4af895be27324f4 100644 --- a/libraries/chain/include/eosio/chain/apply_context.hpp +++ b/libraries/chain/include/eosio/chain/apply_context.hpp @@ -96,6 +96,8 @@ class apply_context { vector get_active_producers() const; + const bytes& get_packed_transaction(); + const chain_controller& controller; const chainbase::database& db; ///< database where state is stored const action& act; ///< message being applied @@ -194,6 +196,7 @@ class apply_context { vector _read_locks; vector _write_scopes; + bytes _cached_trx; }; using apply_handler = std::function; diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 91d25e7bc7268d0ab05dcb5edf841e3d349a2f00..0dcd6b37c0c0b05986e91bad6594c449aae84eee 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -690,18 +690,16 @@ class transaction_api : public context_aware_api { public: using context_aware_api::context_aware_api; - size_t current_transaction( array_ptr data, size_t data_len ) { - if( !context.trx_meta.packed_trx.size() ) { - auto size = fc::raw::pack_size( context.trx_meta.trx ); - if( size > data_len ) - return size; - datastream ds( data, data_len ); - fc::raw::pack( ds, context.trx_meta.trx ); - } - auto size = context.trx_meta.packed_trx.size(); - if( data_len >= size ); - memcpy( data, context.trx_meta.packed_trx.data(), size ); - return size; + int read_transaction( array_ptr data, size_t data_len ) { + bytes trx = context.get_packed_transaction(); + if (data_len >= trx.size()) { + memcpy(data, trx.data(), trx.size()); + } + return trx.size(); + } + + int transaction_size() { + return context.get_packed_transaction().size(); } int expiration() { @@ -711,7 +709,7 @@ class transaction_api : public context_aware_api { int tapos_block_num() { return context.trx_meta.trx.ref_block_num; } - int tapos_prefix() { + int tapos_block_prefix() { return context.trx_meta.trx.ref_block_prefix; } @@ -785,10 +783,12 @@ REGISTER_INTRINSICS(console_api, ); REGISTER_INTRINSICS(transaction_api, - (expiration, int() ) - (tapos_prefix, int() ) - (tapos_block_num, int() ) - (send_inline, void(int, int) ) + (read_transaction, int(int, int) ) + (transaction_size, int() ) + (expiration, int() ) + (tapos_block_prefix, int() ) + (tapos_block_num, int() ) + (send_inline, void(int, int) ) (send_deferred, void(int, int, int, int) ) );