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. (FISCO-BCOS#4622)
  • Loading branch information
kyonRay authored Sep 13, 2024
1 parent 8679a6e commit ce7c03e
Show file tree
Hide file tree
Showing 35 changed files with 196 additions and 81 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
8 changes: 4 additions & 4 deletions bcos-executor/src/precompiled/ConsensusPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ static int addSealerImpl(bool isConsensus,
// version >= 3.1.0, only allow adding sealer in observer list
return CODE_ADD_SEALER_SHOULD_IN_OBSERVER;
}
consensusList.emplace_back(nodeIDPtr, consensus::Type::consensus_sealer, voteWeight, 0,
blockContext.number() + 1);
consensusList.emplace_back(consensus::ConsensusNode{nodeIDPtr,
consensus::Type::consensus_sealer, voteWeight, 0, blockContext.number() + 1});
}
node->enableNumber = blockContext.number() + 1;
}
Expand All @@ -205,8 +205,8 @@ static int addSealerImpl(bool isConsensus,
}
else
{
consensusList.emplace_back(
nodeIDPtr, consensus::Type::consensus_observer, 0, 0, blockContext.number() + 1);
consensusList.emplace_back(consensus::ConsensusNode{
nodeIDPtr, consensus::Type::consensus_observer, 0, 0, blockContext.number() + 1});
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ BOOST_AUTO_TEST_CASE(testRotate)

// Node list
consensus::ConsensusNodeList nodeList;
nodeList.emplace_back(std::make_shared<crypto::KeyImpl>(fromHex(node1)),
consensus::Type::consensus_candidate_sealer, 0, 70, 0);
nodeList.emplace_back(std::make_shared<crypto::KeyImpl>(fromHex(node2)),
consensus::Type::consensus_candidate_sealer, 0, 20, 0);
nodeList.emplace_back(std::make_shared<crypto::KeyImpl>(fromHex(node3)),
consensus::Type::consensus_candidate_sealer, 0, 7, 0);
nodeList.emplace_back(std::make_shared<crypto::KeyImpl>(fromHex(node4)),
consensus::Type::consensus_candidate_sealer, 0, 3, 0);
nodeList.emplace_back(
consensus::ConsensusNode{std::make_shared<crypto::KeyImpl>(fromHex(node1)),
consensus::Type::consensus_candidate_sealer, 0, 70, 0});
nodeList.emplace_back(
consensus::ConsensusNode{std::make_shared<crypto::KeyImpl>(fromHex(node2)),
consensus::Type::consensus_candidate_sealer, 0, 20, 0});
nodeList.emplace_back(
consensus::ConsensusNode{std::make_shared<crypto::KeyImpl>(fromHex(node3)),
consensus::Type::consensus_candidate_sealer, 0, 7, 0});
nodeList.emplace_back(
consensus::ConsensusNode{std::make_shared<crypto::KeyImpl>(fromHex(node4)),
consensus::Type::consensus_candidate_sealer, 0, 3, 0});
co_await ledger::setNodeList(storage, nodeList);

auto storageWrapper =
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
15 changes: 8 additions & 7 deletions bcos-ledger/test/unittests/ledger/LedgerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ class LedgerFixture : public TestPromptFixture
auto signImpl = std::make_shared<Secp256k1Crypto>();
consensus::ConsensusNodeList consensusNodeList;
consensus::ConsensusNodeList observerNodeList;
for (int i = 0; i < 4; ++i)
for (size_t i = 0; i < 4; ++i)
{
auto node = consensus::ConsensusNode(signImpl->generateKeyPair()->publicKey(),
consensus::Type::consensus_sealer, 10 + i, 0, 0);
auto node = consensus::ConsensusNode{signImpl->generateKeyPair()->publicKey(),
consensus::Type::consensus_sealer, 10 + i, 0, 0};
consensusNodeList.emplace_back(node);
}
auto observer_node = consensus::ConsensusNode(signImpl->generateKeyPair()->publicKey(),
consensus::Type::consensus_observer, -1, 0, 0);
auto observer_node = consensus::ConsensusNode{signImpl->generateKeyPair()->publicKey(),
consensus::Type::consensus_observer, uint64_t(-1), 0, 0};
observerNodeList.emplace_back(observer_node);

m_param->setConsensusNodeList(consensusNodeList);
Expand Down Expand Up @@ -733,8 +733,9 @@ BOOST_AUTO_TEST_CASE(testNodeListByType)

std::promise<bool> setSealer1;
auto nodeList = task::syncWait(ledger::getNodeList(*m_storage));
nodeList.emplace_back(std::make_shared<KeyImpl>(bcos::crypto::HashType("56789").asBytes()),
consensus::Type::consensus_sealer, 100, 0, 5);
nodeList.emplace_back(consensus::ConsensusNode{
std::make_shared<KeyImpl>(bcos::crypto::HashType("56789").asBytes()),
consensus::Type::consensus_sealer, 100, 0, 5});
task::syncWait(ledger::setNodeList(*m_storage, nodeList));

std::promise<bool> p2;
Expand Down
2 changes: 1 addition & 1 deletion bcos-pbft/test/unittests/pbft/PBFTFixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class PBFTFixture

void appendConsensusNode(PublicPtr _nodeId)
{
auto node = ConsensusNode(_nodeId, consensus::Type::consensus_sealer, 1, 0, 0);
auto node = ConsensusNode{_nodeId, consensus::Type::consensus_sealer, 1, 0, 0};
appendConsensusNode(node);
}

Expand Down
12 changes: 6 additions & 6 deletions bcos-rpbft/test/unittests/config/RPBFTConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ class RPBFTConfigFixture : public TestPromptFixture

for (size_t i = 0; i < consensusNodeNum; ++i)
{
ledgerConfig->mutableConsensusList().push_back(consensus::ConsensusNode(
ledgerConfig->mutableConsensusList().push_back(consensus::ConsensusNode{
m_cryptoSuite->signatureImpl()->generateKeyPair()->publicKey(),
consensus::Type::consensus_sealer, 1, 0, 0));
consensus::Type::consensus_sealer, 1, 0, 0});
}

for (size_t i = 0; i < observerNodeNum; ++i)
{
ledgerConfig->mutableObserverList().push_back(consensus::ConsensusNode(
ledgerConfig->mutableObserverList().push_back(consensus::ConsensusNode{
m_cryptoSuite->signatureImpl()->generateKeyPair()->publicKey(),
consensus::Type::consensus_observer, 0, 0, 0));
consensus::Type::consensus_observer, 0, 0, 0});
}

for (size_t i = 0; i < candidateSealerNodeNum; ++i)
{
ledgerConfig->mutableCandidateSealerNodeList().push_back(consensus::ConsensusNode(
ledgerConfig->mutableCandidateSealerNodeList().push_back(consensus::ConsensusNode{
m_cryptoSuite->signatureImpl()->generateKeyPair()->publicKey(),
consensus::Type::consensus_candidate_sealer, 1, 0, 0));
consensus::Type::consensus_candidate_sealer, 1, 0, 0});
}
return ledgerConfig;
}
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
Loading

0 comments on commit ce7c03e

Please sign in to comment.