Skip to content

Commit

Permalink
<feat&fix>(executor,utilities): add feature_evm_timestamp feat, fix u…
Browse files Browse the repository at this point in the history
…256 hex abbort.
  • Loading branch information
kyonRay committed Sep 13, 2024
1 parent 8679a6e commit 941faf1
Show file tree
Hide file tree
Showing 28 changed files with 161 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

set(VERSION "3.11.0")
set(VERSION "3.12.0")
set(VERSION_SUFFIX "")
include(Options)
configure_project()
Expand Down
2 changes: 1 addition & 1 deletion bcos-crypto/bcos-crypto/ChecksumAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ inline std::string newLegacyEVMAddress(bytesConstRef sender, u256 nonce) noexcep

inline std::string newLegacyEVMAddress(bytesConstRef sender, std::string const& nonce) noexcept
{
const auto uNonce = u256(nonce);
const auto uNonce = hex2u(nonce);
return newLegacyEVMAddress(sender, uNonce);
}

Expand Down
8 changes: 8 additions & 0 deletions bcos-crypto/test/unittests/AddressTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ BOOST_AUTO_TEST_CASE(testCreatLegacyEVMAddress)
auto newAddress = newLegacyEVMAddress(ref(sender), u256(0));
BOOST_CHECK_EQUAL(newAddress, "5fbdb2315678afecb367f032d93f642f64180aa3");
}

// string nonce
{
auto sender = fromHex("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"sv);
auto nonce = "0";
auto newAddress = newLegacyEVMAddress(ref(sender), nonce);
BOOST_CHECK_EQUAL(newAddress, "5fbdb2315678afecb367f032d93f642f64180aa3");
}
}

BOOST_AUTO_TEST_CASE(testCreate2Address)
Expand Down
5 changes: 3 additions & 2 deletions bcos-executor/src/executive/TransactionExecutive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ CallParameters::UniquePtr TransactionExecutive::execute(CallParameters::UniquePt
co_await ledger::account::create(addr);
}
auto const nonceInStorage = co_await ledger::account::nonce(addr);
auto const baseNonce = u256(nonceInStorage.value_or("0"));
auto const newNonce = std::max(callNonce, baseNonce) + 1;
// FIXME)) : only web3 tx use this
auto const storageNonce = u256(nonceInStorage.value_or("0"));
auto const newNonce = std::max(callNonce, storageNonce) + 1;
co_await ledger::account::setNonce(addr, newNonce.convert_to<std::string>());
}(std::move(address), callParameters->nonce));
}
Expand Down
15 changes: 14 additions & 1 deletion bcos-executor/src/executor/TransactionExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,12 @@ void TransactionExecutor::removeCommittedState()
m_ledgerCache->clearCacheByNumber(number);
}

/**
* call when scheduler ExecutionMessage::MESSAGE
* @param input scheduler input
* @param staticCall whether static call
* @return callParameters
*/
std::unique_ptr<CallParameters> TransactionExecutor::createCallParameters(
bcos::protocol::ExecutionMessage& input, bool staticCall)
{
Expand Down Expand Up @@ -2734,6 +2740,12 @@ std::unique_ptr<CallParameters> TransactionExecutor::createCallParameters(
return callParameters;
}

/**
* call when scheduler ExecutionMessage::TXHASH
* @param input scheduler input
* @param tx transaction
* @return callParameters
*/
std::unique_ptr<CallParameters> TransactionExecutor::createCallParameters(
bcos::protocol::ExecutionMessage& input, const bcos::protocol::Transaction& tx)
{
Expand Down Expand Up @@ -2764,7 +2776,8 @@ std::unique_ptr<CallParameters> TransactionExecutor::createCallParameters(
callParameters->gasLimit = input.gasLimit();
callParameters->maxFeePerGas = u256(input.maxFeePerGas());
callParameters->maxPriorityFeePerGas = u256(input.maxPriorityFeePerGas());
callParameters->nonce = u256(input.nonce());
// TODO: depends on whether web3 tx?
callParameters->nonce = hex2u(input.nonce());

if (!m_isWasm && !callParameters->create)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static WorkingSealer pickNodeByWeight(
auto weight =
nodeIt->offset - ((nodeWeightRanges.begin() == nodeIt) ? 0LU : (nodeIt - 1)->offset);
totalWeight -= weight;
for (auto& it : RANGES::subrange(nodeIt + 1, nodeWeightRanges.end()))
for (auto& it : RANGES::subrange<decltype(nodeIt)>(nodeIt + 1, nodeWeightRanges.end()))
{
it.offset -= weight;
}
Expand Down
6 changes: 6 additions & 0 deletions bcos-executor/src/vm/HostContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ evmc_result HostContext::externalRequest(const evmc_message* _msg)
*m_executive->storage().getRawStorage(), request->senderAddress);
request->nonce = task::syncWait([](decltype(account) contract) -> task::Task<u256> {
auto const nonceString = co_await ledger::account::nonce(contract);
// uint in storage
auto const nonce = u256(nonceString.value_or("0"));
co_return nonce;
}(std::move(account)));
Expand Down Expand Up @@ -702,6 +703,11 @@ uint32_t HostContext::blockVersion() const

uint64_t HostContext::timestamp() const
{
if (m_executive->blockContext().features().get(ledger::Features::Flag::feature_evm_timestamp))
{
// millisecond to second
return m_executive->blockContext().timestamp() / 1000;
}
return m_executive->blockContext().timestamp();
}

Expand Down
9 changes: 9 additions & 0 deletions bcos-framework/bcos-framework/consensus/ConsensusNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ inline std::ostream& operator<<(std::ostream& stream, Type const& type)

struct ConsensusNode
{
ConsensusNode() = default;
ConsensusNode(bcos::crypto::PublicPtr nodeID, Type type, uint64_t voteWeight,
uint64_t termWeight, protocol::BlockNumber enableNumber)
: nodeID(std::move(nodeID)),
type(type),
voteWeight(voteWeight),
termWeight(termWeight),
enableNumber(enableNumber)
{}
bcos::crypto::PublicPtr nodeID;
Type type;
uint64_t voteWeight;
Expand Down
54 changes: 35 additions & 19 deletions bcos-framework/bcos-framework/ledger/Features.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class Features
feature_paillier_add_raw,
feature_evm_cancun,
feature_calculate_gasPrice,
feature_evm_timestamp,
feature_evm_address,
feature_ethereum_compatible, // will enbale all bugfixes, all features about evm
feature_ethereum_compatible, // will enable all bugfixes, all features about evm
feature_rpbft_term_weight,
};

Expand All @@ -77,6 +78,11 @@ class Features
return *value;
}

static bool contains(std::string_view flag)
{
return magic_enum::enum_cast<Flag>(flag).has_value();
}

void validate(std::string_view flag) const
{
auto value = magic_enum::enum_cast<Flag>(flag);
Expand Down Expand Up @@ -114,36 +120,46 @@ class Features
}
bool get(std::string_view flag) const { return get(string2Flag(flag)); }

void set(Flag flag)
{
auto index = magic_enum::enum_index(flag);
if (!index)
{
BOOST_THROW_EXCEPTION(NoSuchFeatureError{});
}

validate(flag);
m_flags[*index] = true;
turnOnMainSwitch(flag);
}

void turnOnMainSwitch(Flag flag)
// DO NOT use now, there is some action after set feature in systemPrecompiled
static auto getFeatureDependencies(Flag flag)
{
/// NOTE:请不要在此处添加旧版本feature依赖!否则会出现数据不一致!
/// Do NOT add old version feature dependencies here! Otherwise, data inconsistency will
/// occur!
const auto mainSwitchDependence = std::unordered_map<Flag, std::set<Flag>>(
{{Flag::feature_ethereum_compatible, {
Flag::feature_balance,
Flag::feature_balance_precompiled,
Flag::feature_calculate_gasPrice,
Flag::feature_evm_timestamp,
Flag::feature_evm_address,
Flag::feature_evm_cancun,
}}});
if (mainSwitchDependence.contains(flag))
{
for (const auto& dependence : mainSwitchDependence.at(flag))
{
set(dependence);
}
return mainSwitchDependence.at(flag);
}
return std::set<Flag>();
}
void enableDependencyFeatures(Flag flag)
{
for (const auto& dependence : getFeatureDependencies(flag))
{
set(dependence);
}
}

void set(Flag flag)
{
auto index = magic_enum::enum_index(flag);
if (!index)
{
BOOST_THROW_EXCEPTION(NoSuchFeatureError{});
}

validate(flag);
m_flags[*index] = true;
// enableDependencyFeatures(flag);
}

void set(std::string_view flag) { set(string2Flag(flag)); }
Expand Down
9 changes: 5 additions & 4 deletions bcos-framework/bcos-framework/protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum ProtocolVersion : uint32_t

enum class BlockVersion : uint32_t
{
V3_12_0_VERSION = 0x030c0000, // 3.12.0
V3_11_0_VERSION = 0x030b0000,
V3_10_0_VERSION = 0x030a0000,
V3_9_0_VERSION = 0x03090000,
Expand All @@ -138,7 +139,7 @@ enum class BlockVersion : uint32_t
V3_0_VERSION = 0x03000000,
RC4_VERSION = 4,
MIN_VERSION = RC4_VERSION,
MAX_VERSION = V3_11_0_VERSION,
MAX_VERSION = V3_12_0_VERSION, // 3.12.0
};

enum class TransactionVersion : uint32_t
Expand All @@ -153,10 +154,10 @@ const std::string RC4_VERSION_STR = "3.0.0-rc4";
const std::string RC_VERSION_PREFIX = "3.0.0-rc";
const std::string V3_9_VERSION_STR = "3.9.0";

const BlockVersion DEFAULT_VERSION = bcos::protocol::BlockVersion::V3_11_0_VERSION;
constexpr BlockVersion DEFAULT_VERSION = bcos::protocol::BlockVersion::V3_12_0_VERSION;
const std::string DEFAULT_VERSION_STR = V3_9_VERSION_STR;
const uint8_t MAX_MAJOR_VERSION = std::numeric_limits<uint8_t>::max();
const uint8_t MIN_MAJOR_VERSION = 3;
constexpr uint8_t MAX_MAJOR_VERSION = std::numeric_limits<uint8_t>::max();
constexpr uint8_t MIN_MAJOR_VERSION = 3;

[[nodiscard]] inline int versionCompareTo(
std::variant<uint32_t, BlockVersion> const& _v1, BlockVersion const& _v2)
Expand Down
13 changes: 13 additions & 0 deletions bcos-framework/test/unittests/interfaces/FeaturesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ BOOST_AUTO_TEST_CASE(feature)
"feature_paillier_add_raw",
"feature_evm_cancun",
"feature_calculate_gasPrice",
"feature_evm_timestamp",
"feature_evm_address",
"feature_ethereum_compatible",
"feature_rpbft_term_weight",
Expand Down Expand Up @@ -394,4 +395,16 @@ BOOST_AUTO_TEST_CASE(genesis)
}
}


BOOST_AUTO_TEST_CASE(testDependenciesFeatures)
{
Features features;
features.set(ledger::Features::Flag::feature_ethereum_compatible);
// BOOST_CHECK(features.get(ledger::Features::Flag::feature_balance));
// BOOST_CHECK(features.get(ledger::Features::Flag::feature_balance_precompiled));
// BOOST_CHECK(features.get(ledger::Features::Flag::feature_calculate_gasPrice));
// BOOST_CHECK(features.get(ledger::Features::Flag::feature_evm_address));
// BOOST_CHECK(features.get(ledger::Features::Flag::feature_evm_cancun));
}

BOOST_AUTO_TEST_SUITE_END()
4 changes: 2 additions & 2 deletions bcos-ledger/bcos-ledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,14 @@ task::Task<std::optional<ledger::StorageState>> Ledger::getStorageState(
{
co_return std::nullopt;
}
state.nonce = std::stoull(std::string(nonceEntry->get()));
state.nonce = std::string(nonceEntry->get());
auto const balanceEntry =
co_await getStorageAt(_address, ACCOUNT_TABLE_FIELDS::BALANCE, _blockNumber);
if (!balanceEntry.has_value())
{
co_return std::nullopt;
}
state.balance = std::stoull(std::string(balanceEntry->get()));
state.balance = std::string(balanceEntry->get());
co_return state;
}

Expand Down
2 changes: 1 addition & 1 deletion bcos-rpc/test/unittests/common/RPCFixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class RPCFixture : public TestPromptFixture
factory =
std::make_shared<bcos::rpc::RpcFactory>(chainId, gateway, cryptoSuite->keyFactory());
nodeConfig = std::make_shared<bcos::tool::NodeConfig>();

nodeConfig->loadConfigFromString(configini);
factory->setNodeConfig(nodeConfig);

Expand Down
33 changes: 33 additions & 0 deletions bcos-rpc/test/unittests/rpc/Web3TypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,39 @@ BOOST_AUTO_TEST_CASE(testLegacyTransactionDecode)
BOOST_CHECK_EQUAL(rawTx, rawTx2);
}

BOOST_AUTO_TEST_CASE(testConstructTx)
{
Web3Transaction testTx;
testTx.value = 1000000000000000000;
testTx.type = rpc::TransactionType::Legacy;
testTx.data = {};
testTx.to = Address("0x1e58529dAA467406645d0f4B63dec96CA0b87d70");
testTx.nonce = 19;
testTx.gasLimit = 210000;
testTx.maxFeePerGas = 20000000000;
testTx.maxPriorityFeePerGas = 20000000000;
testTx.chainId = 31337;

auto signData = testTx.encodeForSign();
std::string priv = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
auto key = std::make_shared<KeyImpl>(fromHex(priv));
auto newHash = crypto::keccak256Hash(ref(signData));
auto signatureImpl = bcos::crypto::Secp256k1Crypto();
auto keyPair = std::make_unique<Secp256k1KeyPair>(key);

auto signature = signatureImpl.sign(*keyPair, newHash, false);
auto [_, addr] = signatureImpl.recoverAddress(*hashImpl, newHash, ref(*signature));
auto newAddr = toHex(addr);
BOOST_CHECK_EQUAL(newAddr, Address("C96aAa54E2d44c299564da76e1cD3184A2386B8D").hex());
testTx.signatureR = {signature->begin(), signature->begin() + 32};
testTx.signatureS = {signature->begin() + 32, signature->begin() + 64};
testTx.signatureV = signature->back();
bcos::bytes toData;
bcos::codec::rlp::encode(toData, testTx);
auto const newTx = toHexStringWithPrefix(toData);
BOOST_CHECK(!newTx.empty());
}

BOOST_AUTO_TEST_CASE(testEIP2930Transaction)
{
// clang-format off
Expand Down
2 changes: 1 addition & 1 deletion bcos-tars-protocol/bcos-tars-protocol/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#pragma once
// if windows, manual include tup/Tars.h first
#include "bcos-framework/consensus/ConsensusNode.h"
#include <bcos-framework/consensus/ConsensusNode.h>
#ifdef _WIN32
#include <tup/Tars.h>
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* @date 2022-05-09
*/
#include "ExecutionMessageImpl.h"
#include "../Common.h"
using namespace bcostars;
using namespace bcostars::protocol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"

#include <bcos-tars-protocol/Common.h>
// if windows, manual include tup/Tars.h first
#ifdef _WIN32
#include <tup/Tars.h>
Expand Down
14 changes: 14 additions & 0 deletions bcos-utilities/bcos-utilities/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ u256 s2u(s256 _u)
else
return u256(c_end + _u);
}

u256 hex2u(std::string_view _hexStr)
{
if (_hexStr.empty())
{
return u256{0};
}
if (_hexStr.starts_with("0x") || _hexStr.starts_with("0X"))
{
return u256(_hexStr);
}
return u256("0x" + std::string(_hexStr));
}

bool isalNumStr(std::string const& _stringData)
{
for (auto ch : _stringData)
Expand Down
2 changes: 2 additions & 0 deletions bcos-utilities/bcos-utilities/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ s256 u2s(u256 _u);
/// @returns the two's complement signed representation of the signed number _u.
u256 s2u(s256 _u);

u256 hex2u(std::string_view _hexStr);

bool isalNumStr(std::string const& _stringData);

inline bool isNumStr(std::string const& _stringData)
Expand Down
2 changes: 1 addition & 1 deletion bcos-utilities/bcos-utilities/DataConvertUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ inline uint64_t fromQuantity(std::string const& quantity)

inline u256 fromBigQuantity(std::string_view quantity)
{
return u256(quantity);
return hex2u(quantity);
}

/**
Expand Down
Loading

0 comments on commit 941faf1

Please sign in to comment.