提交 5a72a4b7 编写于 作者: A arhag

intrinsics to return current and published time now return microseconds

The now() intrinsic was replaced with a current_time() intrinsic which returns the current time (not head block time) in microseconds since epoch.

A now() function was defined in eosiolib/system.h which returns current_time()/1000000 so that existing contracts which use now() need minimal changes.

The publication_time() intrinsic was modified to also return the time as microseconds since epoch.
The only contracts that used publication_time() so far were for tests and those tests have been updated to reflect the new meaning of publication_time() and current_time().
上级 35af3e8a
......@@ -58,7 +58,7 @@ extern "C" {
* require_auth(N(inita)); // Do nothing since inita exists in the auth list
* require_auth(N(initb)); // Throws an exception
*
* print(now()); // Output: timestamp of last accepted block
* print(current_time()); // Output: timestamp (in microseconds since 1970) of current block
*
* @endcode
*
......@@ -137,11 +137,11 @@ extern "C" {
void require_read_lock( account_name name );
/**
* Returns the time in seconds from 1970 of the publication_time
* Returns the time in microseconds from 1970 of the publication_time
* @brief Get the publication time
* @return the time in seconds from 1970 of the publication_time
* @return the time in microseconds from 1970 of the publication_time
*/
time publication_time();
uint64_t publication_time();
/**
* Get the current receiver of the action
......
......@@ -37,14 +37,23 @@ extern "C" {
*/
[[noreturn]] void eosio_exit( int32_t code );
/**
* Returns the time in seconds from 1970 of the last accepted block (not the block including this action)
* @brief Get time of the last accepted block
* @return time in seconds from 1970 of the last accepted block
* Returns the time in microseconds from 1970 of the current block
* @brief Get time of the current block (i.e. the block including this action)
* @return time in microseconds from 1970 of the current block
*/
time now();
uint64_t current_time();
/**
* Returns the time in seconds from 1970 of the block including this action
* @brief Get time (rounded down to the nearest second) of the current block (i.e. the block including this action)
* @return time in seconds from 1970 of the current block
*/
uint32_t now() {
return (uint32_t)( current_time() / 1000000 );
}
///@ } systemcapi
}
......@@ -166,8 +166,9 @@ void test_action::test_abort() {
}
void test_action::test_publication_time() {
uint32_t pub_time = 0;
read_action_data(&pub_time, sizeof(uint32_t));
uint64_t pub_time = 0;
uint32_t total = read_action_data(&pub_time, sizeof(uint64_t));
eosio_assert( total == sizeof(uint64_t), "total == sizeof(uint64_t)");
eosio_assert( pub_time == publication_time(), "pub_time == publication_time()" );
}
......@@ -179,9 +180,9 @@ void test_action::test_current_receiver(uint64_t receiver, uint64_t code, uint64
eosio_assert( receiver == cur_rec, "the current receiver does not match" );
}
void test_action::now() {
uint32_t tmp = 0;
uint32_t total = read_action_data(&tmp, sizeof(uint32_t));
eosio_assert( total == sizeof(uint32_t), "total == sizeof(uint32_t)");
eosio_assert( tmp == ::now(), "tmp == now()" );
void test_action::test_current_time() {
uint64_t tmp = 0;
uint32_t total = read_action_data(&tmp, sizeof(uint64_t));
eosio_assert( total == sizeof(uint64_t), "total == sizeof(uint64_t)");
eosio_assert( tmp == current_time(), "tmp == current_time()" );
}
......@@ -74,7 +74,7 @@ extern "C" {
WASM_TEST_HANDLER(test_action, require_auth);
WASM_TEST_HANDLER(test_action, assert_false);
WASM_TEST_HANDLER(test_action, assert_true);
WASM_TEST_HANDLER(test_action, now);
WASM_TEST_HANDLER(test_action, test_current_time);
WASM_TEST_HANDLER(test_action, test_abort);
WASM_TEST_HANDLER_EX(test_action, test_current_receiver);
WASM_TEST_HANDLER(test_action, test_publication_time);
......
......@@ -63,7 +63,7 @@ struct test_action {
static void assert_false();
static void assert_true();
static void assert_true_cf();
static void now();
static void test_current_time();
static void test_abort() __attribute__ ((noreturn)) ;
static void test_current_receiver(uint64_t receiver, uint64_t code, uint64_t action);
static void test_publication_time();
......
......@@ -189,7 +189,7 @@ void apply_context::execute_inline( action&& a ) {
if ( !privileged ) {
if( a.account != receiver ) {
const auto delay = control.get_authorization_manager().check_authorization({a}, flat_set<public_key_type>(), false, {receiver});
FC_ASSERT( published_time + delay <= control.head_block_time(),
FC_ASSERT( published_time + delay <= control.pending_block_time(),
"inline action uses a permission that imposes a delay that is not met, set delay_sec in transaction header to at least ${delay} seconds",
("delay", delay.to_seconds()) );
}
......@@ -220,7 +220,7 @@ void apply_context::schedule_deferred_transaction( deferred_transaction&& trx )
}
auto id = trx.id();
/// TODO: validate authority and delay
/// TODO: validate authority and delay
const auto& tr = static_cast<const transaction&>(trx);
auto trx_size = fc::raw::pack_size(tr);
......
......@@ -577,7 +577,7 @@ class apply_context {
generic_index<index_double_object> idx_double;
uint32_t recurse_depth; // how deep inline actions can recurse
fc::time_point_sec published_time;
fc::time_point published_time;
fc::time_point processing_deadline;
uint64_t max_cpu = uint64_t(-1);
......
......@@ -809,8 +809,8 @@ class system_api : public context_aware_api {
throw wasm_exit{code};
}
fc::time_point_sec now() {
return context.control.head_block_time();
uint64_t current_time() {
return static_cast<uint64_t>( context.control.pending_block_time().time_since_epoch().count() );
}
};
......@@ -830,8 +830,8 @@ class action_api : public context_aware_api {
return context.act.data.size();
}
fc::time_point_sec publication_time() {
return context.published_time;
uint64_t publication_time() {
return static_cast<uint64_t>( context.published_time.time_since_epoch().count() );
}
name current_receiver() {
......@@ -1533,16 +1533,16 @@ REGISTER_INTRINSICS(string_api,
);
REGISTER_INTRINSICS(system_api,
(abort, void())
(eosio_assert, void(int, int))
(eosio_exit, void(int ))
(now, int())
(abort, void() )
(eosio_assert, void(int, int) )
(eosio_exit, void(int) )
(current_time, int64_t() )
);
REGISTER_INTRINSICS(action_api,
(read_action_data, int(int, int) )
(action_data_size, int() )
(publication_time, int32_t() )
(publication_time, int64_t() )
(current_receiver, int64_t() )
);
......@@ -1573,7 +1573,7 @@ REGISTER_INTRINSICS(context_free_transaction_api,
(tapos_block_prefix, int() )
(tapos_block_num, int() )
(get_action, int (int, int, int, int) )
(check_auth, void(int, int, int, int) )
(check_auth, void(int, int, int, int) )
);
REGISTER_INTRINSICS(transaction_api,
......
......@@ -357,14 +357,14 @@ BOOST_FIXTURE_TEST_CASE(action_tests, TESTER) { try {
BOOST_CHECK_EQUAL(res.status, transaction_receipt::executed);
}
uint32_t now = control->head_block_time().sec_since_epoch();
CALL_TEST_FUNCTION( *this, "test_action", "now", fc::raw::pack(now));
uint64_t now = static_cast<uint64_t>( control->head_block_time().time_since_epoch().count() );
CALL_TEST_FUNCTION( *this, "test_action", "test_current_time", fc::raw::pack(now));
// test now
// test current_time
produce_block();
BOOST_CHECK_EXCEPTION(CALL_TEST_FUNCTION( *this, "test_action", "now", fc::raw::pack(now)), transaction_exception,
BOOST_CHECK_EXCEPTION(CALL_TEST_FUNCTION( *this, "test_action", "test_current_time", fc::raw::pack(now)), transaction_exception,
[](const fc::exception& e) {
return expect_assert_message(e, "assertion failed: tmp == now");
return expect_assert_message(e, "assertion failed: tmp == current_time()");
}
);
......@@ -376,7 +376,7 @@ BOOST_FIXTURE_TEST_CASE(action_tests, TESTER) { try {
produce_block();
// test_publication_time
uint32_t pub_time = control->head_block_time().sec_since_epoch();
uint64_t pub_time = static_cast<uint64_t>( control->pending_block_time().time_since_epoch().count() );
CALL_TEST_FUNCTION( *this, "test_action", "test_publication_time", fc::raw::pack(pub_time) );
// test test_abort
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册