Skip to content

Commit

Permalink
Add init genesis feature (FISCO-BCOS#4264)
Browse files Browse the repository at this point in the history
  • Loading branch information
morebtcg authored Feb 29, 2024
1 parent 4ae5709 commit 91b07dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bcos-ledger/src/libledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,9 @@ bool Ledger::buildGenesisBlock(
", No support this version"));
}

// Before return, make sure sharding flag is placed
// 已有的链,仅替换了二进制,还没有执行升级版本交易
// The existing chain, which only replaces the binary, has not yet executed the upgraded
// version of the transaction
task::syncWait([&]() -> task::Task<void> {
auto versionEntry = co_await storage2::readOne(
*m_storage, transaction_executor::StateKeyView(
Expand All @@ -1754,8 +1756,12 @@ bool Ledger::buildGenesisBlock(
auto [versionStr, _] = versionEntry->getObject<SystemConfigEntry>();
auto storageVersion = tool::toVersionNumber(versionStr);

// 设置sharding default,相关的bugfix也要设置上,否则会不一致
// Set sharding default, and related bugfixes should also be set, otherwise it
// will be inconsistent
Features shardingFeature;
shardingFeature.setToShardingDefault((protocol::BlockVersion)storageVersion);
shardingFeature.setGenesisFeatures((protocol::BlockVersion)storageVersion);
co_await shardingFeature.writeToStorage(
*m_storage, boost::lexical_cast<long>(blockNumberEntry->get()));
}
Expand Down
38 changes: 38 additions & 0 deletions bcos-ledger/test/unittests/ledger/LedgerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,5 +1436,43 @@ BOOST_AUTO_TEST_CASE(genesisBlockWithAllocs)
}());
}

BOOST_AUTO_TEST_CASE(replaceBinary)
{
task::syncWait([this]() -> task::Task<void> {
auto hashImpl = std::make_shared<Keccak256>();
auto memoryStorage = std::make_shared<StateStorage>(nullptr);
auto storage = std::make_shared<MockStorage>(memoryStorage);
auto ledger = std::make_shared<Ledger>(m_blockFactory, storage, 1);

LedgerConfig param;
param.setBlockNumber(0);
param.setHash(HashType(""));
param.setBlockTxCountLimit(0);

GenesisConfig genesisConfig;
genesisConfig.m_txGasLimit = 3000000000;
genesisConfig.m_compatibilityVersion =
tool::toVersionNumber(bcos::protocol::V3_5_VERSION_STR);
auto code = "I am a solidity code!"s;
std::string hexCode;
boost::algorithm::hex_lower(code, std::back_inserter(hexCode));

co_await ledger::buildGenesisBlock(*ledger, genesisConfig, param);

// Delete the bugfix_statestorage_hash
BOOST_CHECK(co_await storage2::existsOne(*storage,
transaction_executor::StateKeyView(ledger::SYS_CONFIG, "bugfix_statestorage_hash")));
Entry entry;
entry.setStatus(Entry::DELETED);
co_await storage2::writeOne(*storage,
transaction_executor::StateKey(ledger::SYS_CONFIG, "bugfix_statestorage_hash"), entry);

co_await ledger::buildGenesisBlock(*ledger, genesisConfig, param);
auto result = co_await storage2::readOne(*storage,
transaction_executor::StateKeyView(ledger::SYS_CONFIG, "bugfix_statestorage_hash"));
BOOST_REQUIRE(result);
}());
}

BOOST_AUTO_TEST_SUITE_END()
} // namespace bcos::test

0 comments on commit 91b07dd

Please sign in to comment.