Skip to content

Commit

Permalink
Merge branch 'main' into update-tests-to-use-leap-dev-install
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwaldp-oci committed Mar 21, 2023
2 parents 13fe485 + 3b5185b commit 6076d9e
Show file tree
Hide file tree
Showing 39 changed files with 3,445 additions and 646 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ HunterGate(
)

project(trustevm_node)
set(PROJECT_VERSION 0.2.1)
set(PROJECT_VERSION 0.3.0)

string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ ${PROJECT_VERSION})
set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
Expand Down
33 changes: 19 additions & 14 deletions cmd/block_conversion_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,27 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
return;
}

uint64_t block_interval_ms = silkworm::endian::load_big_u64(genesis_header->nonce.data());
// TODO: Consider redefining genesis nonce to be in units of seconds rather than milliseconds? Or just hardcode to 1 second?
bm.emplace(genesis_header->timestamp, 1); // Hardcoded to 1 second block interval

if (block_interval_ms == 0 || block_interval_ms % 1000 != 0) {
SILK_CRIT << "Genesis nonce is invalid. Must be a positive multiple of 1000 representing the block interval in milliseconds. "
"Instead got: " << block_interval_ms;
SILK_INFO << "Block interval (in seconds): " << bm->block_interval;
SILK_INFO << "Genesis timestamp (in seconds since Unix epoch): " << bm->genesis_timestamp;

// The nonce in the genesis header encodes the name of the Antelope account on which the EVM contract has been deployed.
// This name is necessary to determine which reserved address to use as the beneficiary of the blocks.
evm_contract_name = silkworm::endian::load_big_u64(genesis_header->nonce.data());

SILK_INFO << "Genesis nonce (as hex): " << silkworm::to_hex(evm_contract_name, true);
SILK_INFO << "Genesis nonce (as Antelope name): " << eosio::name{evm_contract_name}.to_string();

if (evm_contract_name == 0 || (evm_contract_name == 1000)) {
// TODO: Remove the (evm_contract_name == 1000) condition once we feel comfortable other tests and scripts have been
// updated to reflect this new meaning of the nonce (used to be block interval in milliseconds).

SILK_CRIT << "Genesis nonce does not represent a valid Antelope account name. "
"It must be the name of the account on which the EVM contract is deployed";
sys::error("Invalid genesis nonce");
return;
}

bm.emplace(genesis_header->timestamp, block_interval_ms/1e3);

SILK_INFO << "Block interval: " << bm->block_interval;
SILK_INFO << "Genesis timestamp: " << bm->genesis_timestamp;
}

evmc::bytes32 compute_transaction_root(const silkworm::BlockBody& body) {
Expand All @@ -84,11 +91,8 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi

silkworm::Block new_block(uint64_t num, const evmc::bytes32& parent_hash) {
silkworm::Block new_block;
evm_common::prepare_block_header(new_block.header, bm.value(), evm_contract_name, num);
new_block.header.parent_hash = parent_hash;
new_block.header.difficulty = 1;
new_block.header.number = num;
new_block.header.gas_limit = 0x7ffffffffff;
new_block.header.timestamp = bm.value().evm_block_num_to_evm_timestamp(num)/1e6;
new_block.header.transactions_root = silkworm::kEmptyRoot;
//new_block.header.mix_hash
//new_block.header.nonce
Expand Down Expand Up @@ -239,6 +243,7 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
channels::evm_blocks::channel_type& evm_blocks_channel;
channels::native_blocks::channel_type::handle native_blocks_subscription;
std::optional<evm_common::block_mapping> bm;
uint64_t evm_contract_name = 0;
};

block_conversion_plugin::block_conversion_plugin() : my(new block_conversion_plugin_impl()) {}
Expand Down
4 changes: 2 additions & 2 deletions cmd/block_conversion_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include <eosio/name.hpp>

struct pushtx {
eosio::name ram_payer;
eosio::name miner;
std::vector<uint8_t> rlpx;
};

EOSIO_REFLECT(pushtx, ram_payer, rlpx)
EOSIO_REFLECT(pushtx, miner, rlpx)
class block_conversion_plugin : public appbase::plugin<block_conversion_plugin> {
public:
APPBASE_PLUGIN_REQUIRES((sys_plugin)(ship_receiver_plugin)(engine_plugin));
Expand Down
62 changes: 42 additions & 20 deletions cmd/contract_common/evm_common/block_mapping.hpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,74 @@
#pragma once

#include <cstdint>

#include <silkworm/types/block.hpp>
#include <silkworm/common/util.hpp>
namespace evm_common {

struct block_mapping {
struct block_mapping
{
/**
* @brief Construct object that maps from Antelope timestamp to EVM block number and timestamp
*
*
* @param genesis_timestamp_sec - the EVM genesis timestamp in seconds
* @param block_interval_sec - time interval between consecutive EVM blocks in seconds (must be positive)
*
* @note The timestamp of the Antelope block containing the init action rounded down to the nearest second must equal genesis_timestamp_sec.
*
* @note The timestamp of the Antelope block containing the init action rounded down to the nearest second must equal
* genesis_timestamp_sec.
*/
block_mapping(uint64_t genesis_timestamp_sec, uint32_t block_interval_sec = 1)
: block_interval(block_interval_sec == 0 ? 1 : block_interval_sec),
genesis_timestamp(genesis_timestamp_sec)
block_mapping(uint64_t genesis_timestamp_sec, uint32_t block_interval_sec = 1) :
block_interval(block_interval_sec == 0 ? 1 : block_interval_sec), genesis_timestamp(genesis_timestamp_sec)
{}

const uint64_t block_interval; // seconds
const uint64_t genesis_timestamp; // seconds
const uint64_t block_interval; // seconds
const uint64_t genesis_timestamp; // seconds

/**
* @brief Map Antelope timestamp to EVM block num
*
*
* @param timestamp - Antelope timestamp in microseconds
* @return mapped EVM block number (returns 0 for all timestamps prior to the genesis timestamp)
*/
inline uint32_t timestamp_to_evm_block_num(uint64_t timestamp_us) const {
inline uint32_t timestamp_to_evm_block_num(uint64_t timestamp_us) const
{
uint64_t timestamp = timestamp_us / 1e6; // map Antelope block timestamp to EVM block timestamp
if( timestamp < genesis_timestamp ) {
if (timestamp < genesis_timestamp) {
// There should not be any transactions prior to the init action.
// But any entity with an associated timestamp prior to the genesis timestamp can be considered as part of the genesis block.
// But any entity with an associated timestamp prior to the genesis timestamp can be considered as part of the
// genesis block.
return 0;
}
return 1 + (timestamp - genesis_timestamp) / block_interval;
return 1 + (timestamp - genesis_timestamp) / block_interval;
}

/**
* @brief Map EVM block num to EVM block timestamp
*
*
* @param block_num - EVM block number
* @return EVM block timestamp associated with the given EVM block number
*/
inline uint64_t evm_block_num_to_evm_timestamp(uint32_t block_num) const {
inline uint64_t evm_block_num_to_evm_timestamp(uint32_t block_num) const
{
return genesis_timestamp + block_num * block_interval;
}
};

} // namespace evm_common
/**
* @brief Prepare block header
*
* Modifies header by setting the common fields shared between the contract code and the EVM node.
* It sets the beneficiary, difficulty, number, gas_limit, and timestamp fields only.
*/
inline void prepare_block_header(silkworm::BlockHeader& header,
const block_mapping& bm,
uint64_t evm_contract_name,
uint32_t evm_block_num)
{
header.beneficiary = silkworm::make_reserved_address(evm_contract_name);
header.difficulty = 1;
header.number = evm_block_num;
header.gas_limit = 0x7ffffffffff;
header.timestamp = bm.evm_block_num_to_evm_timestamp(header.number);
}


} // namespace evm_common
8 changes: 8 additions & 0 deletions contract/include/evm_runtime/eosio.token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ namespace eosio {
const asset& quantity,
const string& memo );

//exists to send along a std::basic_string<uint8_t> as a memo without copying over to string first
[[eosio::action]]
void transferb( const name& from,
const name& to,
const asset& quantity,
const std::basic_string<uint8_t>& memo );

[[eosio::action]]
void open( const name& owner, const symbol& symbol, const name& ram_payer );

Expand All @@ -39,6 +46,7 @@ namespace eosio {
using issue_action = eosio::action_wrapper<"issue"_n, &token::issue>;
using retire_action = eosio::action_wrapper<"retire"_n, &token::retire>;
using transfer_action = eosio::action_wrapper<"transfer"_n, &token::transfer>;
using transfer_bytes_memo_action = eosio::action_wrapper<"transfer"_n, &token::transferb>;
using open_action = eosio::action_wrapper<"open"_n, &token::open>;
using close_action = eosio::action_wrapper<"close"_n, &token::close>;
};
Expand Down
Loading

0 comments on commit 6076d9e

Please sign in to comment.