From 90d268ac5ac6a7f0d50c82da3f9e3952123be2f0 Mon Sep 17 00:00:00 2001 From: iizuka Date: Thu, 16 Jul 2026 12:04:43 +0800 Subject: [PATCH 1/7] random and remote utxo index --- src/crypto/Schnorr.cpp | 4 +- src/ecda/ScPrivateKey.cpp | 2 +- src/ecda/ScSignature.cpp | 2 +- src/musig/SimpleMuSigSigner.cpp | 2 +- src_blockchain/CMakeLists.txt | 1 + src_blockchain/bc/CodablecashNodeInstance.cpp | 16 +- src_blockchain/bc/CodablecashNodeInstance.h | 4 + src_blockchain/bc/CodablecashSystemParam.cpp | 6 + src_blockchain/bc/CodablecashSystemParam.h | 14 ++ src_blockchain/bc_base/Abstract32BytesId.cpp | 2 +- .../TransactionDataFactory.cpp | 7 +- .../TransactionIdDataFactory.cpp | 3 +- .../AddressDescriptorDataFactory.cpp | 6 +- .../bc_base_utxo_index/UtxoDataFactory.cpp | 3 +- src_blockchain/bc_block/Block.cpp | 1 + src_blockchain/bc_block/BlockHeader.h | 4 + .../bc_block_generator/BlockGenerator.cpp | 5 + .../AbstractBlockHeaderCommand.cpp | 5 + .../AbstractBlockHeaderCommand.h | 10 + .../bc_block_header_command/CMakeLists.txt | 1 + .../NewShardZoneCommand.cpp | 91 ++++++++- .../NewShardZoneCommand.h | 15 +- .../RecognizedNewShardCommand.cpp | 44 ++++ .../RecognizedNewShardCommand.h | 33 +++ .../bc_block_validator/BlockValidator.cpp | 40 +++- .../bc_block_validator/BlockValidator.h | 1 + .../bc_blockstore/CodablecashBlockchain.cpp | 10 + .../bc_blockstore/CodablecashBlockchain.h | 14 +- .../bc_finalizer/VoterEntryFactory.cpp | 3 +- .../CodablecashNetworkNode.cpp | 8 +- .../CodablecashNetworkNode.h | 4 +- .../MinerMinedReportCommandMessage.cpp | 8 +- .../TransferedMinedReportCommandMessage.cpp | 10 +- .../AbstractShardExtentionValidator.cpp | 28 +++ .../AbstractShardExtentionValidator.h | 37 ++++ .../bc_status_cache/BlockchainController.cpp | 83 +++++++- .../bc_status_cache/BlockchainController.h | 4 + .../bc_status_cache/BlockchainStatusCache.cpp | 63 +++++- .../bc_status_cache/BlockchainStatusCache.h | 24 ++- src_blockchain/bc_status_cache/CMakeLists.txt | 1 + .../bc_status_cache/ZoneStatusCache.cpp | 26 ++- .../bc_status_cache/ZoneStatusCache.h | 11 +- .../bc_status_cache_context/CMakeLists.txt | 2 + .../CachedStatusCache.cpp | 30 ++- .../CachedStatusCache.h | 19 ++ .../CachedStatusCacheContext.cpp | 16 ++ .../IStatusCacheContext.h | 14 +- .../RemoteUtxoDetector.cpp | 69 +++++++ .../RemoteUtxoDetector.h | 52 +++++ .../RemoteUtxoHeight.cpp | 29 +++ .../RemoteUtxoHeight.h | 36 ++++ .../StatusCacheContext.cpp | 61 ++++-- .../StatusCacheContext.h | 24 ++- .../VotingBlockStatusDataFactory.cpp | 3 +- .../bc_status_cache_data/CMakeLists.txt | 7 + .../FinalizedDataCache.cpp | 44 +++- .../bc_status_cache_data/FinalizedDataCache.h | 11 +- .../bc_status_cache_data/RemoteUtxoData.cpp | 58 ++++++ .../bc_status_cache_data/RemoteUtxoData.h | 43 ++++ .../RemoteUtxoDataFactory.cpp | 41 ++++ .../RemoteUtxoDataFactory.h | 32 +++ .../RemoteUtxoHeightIndex.cpp | 93 +++++++++ .../RemoteUtxoHeightIndex.h | 50 +++++ .../RemoteUtxoHeightIndexData.cpp | 121 +++++++++++ .../RemoteUtxoHeightIndexData.h | 50 +++++ .../RemoteUtxoHeightIndexDataFactory.cpp | 59 ++++++ .../RemoteUtxoHeightIndexDataFactory.h | 32 +++ .../RemoteUtxoHistory.cpp | 96 +++++++++ .../bc_status_cache_data/RemoteUtxoHistory.h | 49 +++++ .../RemoteUtxoRepository.cpp | 115 +++++++++++ .../RemoteUtxoRepository.h | 51 +++++ .../CMakeLists.txt | 9 + .../GenerateNewGenesisBlockCommandMessage.cpp | 29 +++ .../GenerateNewGenesisBlockCommandMessage.h | 32 +++ ...NotifyShardExtendRequestCommandMessage.cpp | 54 +++++ .../NotifyShardExtendRequestCommandMessage.h | 40 ++++ .../NotifyZoneExtendRequestedTransaction.cpp | 189 ++++++++++++++++++ .../NotifyZoneExtendRequestedTransaction.h | 61 ++++++ .../HeightVoteDataFactory.cpp | 7 +- .../bc_trx/AbstractBlockchainTransaction.h | 1 + src_blockchain/bc_trx/AbstractUtxoReference.h | 4 + .../NopInterChainCommunicationTransaction.cpp | 9 +- src_blockchain/pow/PoWNonce.cpp | 4 +- src_blockchain/pow_pool/PoWRequestStatus.cpp | 2 +- src_db/numeric/BigInteger.cpp | 41 +++- src_db/numeric/BigInteger.h | 4 +- src_db/osenv/funcs.cpp | 17 ++ src_db/osenv/funcs.h | 5 +- .../blockchain/multi_shard/CMakeLists.txt | 1 + .../blockchain/multi_shard/NewShardAdder.cpp | 17 +- .../blockchain/multi_shard/NewShardAdder.h | 6 + .../multi_shard/NewShardValidator.cpp | 38 ++++ .../multi_shard/NewShardValidator.h | 28 +++ .../multi_shard/test_multi_shard.cpp | 10 +- .../blockchain/pow/test_pow_random_hash.cpp | 8 +- .../blockchain/testnet/test_first_net.cpp | 6 +- .../test_pending_request_processor.cpp | 12 +- .../blockchain/testnet/test_sync_block.cpp | 4 +- .../testnet/test_sync_header_only.cpp | 2 +- .../blockchain/testnet/test_sync_mempool.cpp | 4 +- .../utils_testnet/TestnetInstanceWrapper.cpp | 9 +- .../utils_testnet/TestnetInstanceWrapper.h | 2 + src_test/crypto/test_secp256.cpp | 2 +- src_test/crypto/test_sha256.cpp | 4 +- src_test/db/db_numeric/CMakeLists.txt | 1 + .../db/db_numeric/test_big_integer_misc.cpp | 4 +- .../db/db_numeric/test_big_integer_rand.cpp | 69 +++++++ .../p2p/instance/test_p2p_network_cmd.cpp | 12 +- .../transaction/test_random_address.cpp | 4 +- 109 files changed, 2552 insertions(+), 162 deletions(-) create mode 100644 src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp create mode 100644 src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h create mode 100644 src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp create mode 100644 src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h create mode 100644 src_blockchain/bc_status_cache_context/RemoteUtxoDetector.cpp create mode 100644 src_blockchain/bc_status_cache_context/RemoteUtxoDetector.h create mode 100644 src_blockchain/bc_status_cache_context/RemoteUtxoHeight.cpp create mode 100644 src_blockchain/bc_status_cache_context/RemoteUtxoHeight.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoData.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoData.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHistory.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoHistory.h create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoRepository.cpp create mode 100644 src_blockchain/bc_status_cache_data/RemoteUtxoRepository.h create mode 100644 src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt create mode 100644 src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp create mode 100644 src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h create mode 100644 src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp create mode 100644 src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h create mode 100644 src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp create mode 100644 src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h create mode 100644 src_test/blockchain/multi_shard/NewShardValidator.cpp create mode 100644 src_test/blockchain/multi_shard/NewShardValidator.h create mode 100644 src_test/db/db_numeric/test_big_integer_rand.cpp diff --git a/src/crypto/Schnorr.cpp b/src/crypto/Schnorr.cpp index 1b675de..a1b42cf 100644 --- a/src/crypto/Schnorr.cpp +++ b/src/crypto/Schnorr.cpp @@ -47,7 +47,7 @@ SchnorrKeyPair* Schnorr::generateKey(BigInteger seed) { } SchnorrKeyPair* Schnorr::generateKey(){ - BigInteger s = BigInteger::ramdom(); + BigInteger s = BigInteger::random(); return generateKey(s); } @@ -59,7 +59,7 @@ SchnorrSignature* codablecash::Schnorr::sign(const BigInteger& s, const BigInteg } SchnorrSignature* Schnorr::sign(const BigInteger& s, const BigInteger& p, const uint8_t* data, size_t size){ - BigInteger r = BigInteger::ramdom().modSelf(cnsts.Q_1); + BigInteger r = BigInteger::random().modSelf(cnsts.Q_1); BigInteger powG = cnsts.G.modPow(r, cnsts.Q); BigInteger* e = nullptr; diff --git a/src/ecda/ScPrivateKey.cpp b/src/ecda/ScPrivateKey.cpp index 230e49b..4eff0c0 100644 --- a/src/ecda/ScPrivateKey.cpp +++ b/src/ecda/ScPrivateKey.cpp @@ -23,7 +23,7 @@ ScPrivateKey::ScPrivateKey(const BigInteger* seed, uint64_t solt) : keyvalue((in } ScPrivateKey::ScPrivateKey() : keyvalue((int64_t)0) { - this->keyvalue = BigInteger::ramdom().mod(ScPrivateKey::p); + this->keyvalue = BigInteger::random().mod(ScPrivateKey::p); } ScPrivateKey::~ScPrivateKey() { diff --git a/src/ecda/ScSignature.cpp b/src/ecda/ScSignature.cpp index 6d8b498..2551a0e 100644 --- a/src/ecda/ScSignature.cpp +++ b/src/ecda/ScSignature.cpp @@ -29,7 +29,7 @@ ScSignature::~ScSignature() { void ScSignature::sign(ByteBuffer* data, const BigInteger s) { Secp256k1Point G; - BigInteger r = BigInteger::ramdom().mod(Secp256k1Point::p); + BigInteger r = BigInteger::random().mod(Secp256k1Point::p); Secp256k1Point rG = G.multiple(r); diff --git a/src/musig/SimpleMuSigSigner.cpp b/src/musig/SimpleMuSigSigner.cpp index 98530a6..01d2355 100644 --- a/src/musig/SimpleMuSigSigner.cpp +++ b/src/musig/SimpleMuSigSigner.cpp @@ -27,7 +27,7 @@ Secp256k1Point SimpleMuSigSigner::getxG() { } Secp256k1Point SimpleMuSigSigner::getrG() { - this->r = BigInteger::ramdom(BigInteger(0L), Secp256k1Point::n); + this->r = BigInteger::random(BigInteger(0L), Secp256k1Point::n); Secp256k1Point G; return G.multiple(this->r); diff --git a/src_blockchain/CMakeLists.txt b/src_blockchain/CMakeLists.txt index b4fd68f..45700bc 100644 --- a/src_blockchain/CMakeLists.txt +++ b/src_blockchain/CMakeLists.txt @@ -30,6 +30,7 @@ add_subdirectory(bc_status_cache) add_subdirectory(bc_status_cache_context) add_subdirectory(bc_status_cache_context_finalizer) add_subdirectory(bc_status_cache_data) +add_subdirectory(bc_status_cache_extend_shard) add_subdirectory(bc_status_cache_lockin) add_subdirectory(bc_status_cache_vote) add_subdirectory(bc_trx) diff --git a/src_blockchain/bc/CodablecashNodeInstance.cpp b/src_blockchain/bc/CodablecashNodeInstance.cpp index fe48e30..e198221 100644 --- a/src_blockchain/bc/CodablecashNodeInstance.cpp +++ b/src_blockchain/bc/CodablecashNodeInstance.cpp @@ -183,7 +183,7 @@ void CodablecashNodeInstance::startNetwork(const UnicodeString *host, int port) this->p2pServer = new P2pServer(this->logger, this); this->p2pManager = new BlochchainP2pManager(); - this->p2pManager->init(this->blockchain->getNumZones()); + this->p2pManager->init(this->statusCache->getNumZones()); this->p2pServer->addConnectionListener(this->p2pManager); @@ -233,7 +233,7 @@ void CodablecashNodeInstance::shutdownNetwork() { } void CodablecashNodeInstance::startBlockGenerator(const MiningConfig *config) { - uint16_t zone = this->blockchain->getZoneSelf(); + uint16_t zone = this->statusCache->getZoneSelf(); this->powManager = new PoWManager(this->logger, config); this->blockGenerator = this->allocator->newBlockGenerator(zone, this->param, this->memoryPool, this->ctrl, config, this->logger); @@ -378,7 +378,7 @@ void CodablecashNodeInstance::loginNode(uint16_t zone, P2pHandshake *handshake, // login NodeIdentifierSource* source = this->p2pRequestProcessor->getNetworkKey(); - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); LoginPubSubCommand cmd(zoneSelf, canonicalName); { @@ -414,11 +414,11 @@ NodeIdentifierSource* CodablecashNodeInstance::getNetworkKey() const noexcept { } uint16_t CodablecashNodeInstance::getZoneSelf() const noexcept { - return this->blockchain->getZoneSelf(); + return this->statusCache->getZoneSelf(); } int CodablecashNodeInstance::getNumZones() const noexcept { - return this->blockchain->getNumZones(); + return this->statusCache->getNumZones(); } void CodablecashNodeInstance::maintainNetwork() { @@ -495,7 +495,7 @@ void CodablecashNodeInstance::setNodeName(const UnicodeString *name) noexcept { } void CodablecashNodeInstance::validateZone(uint16_t zone) const { - uint16_t numZone = this->blockchain->getNumZones(); + uint16_t numZone = this->statusCache->getNumZones(); ExceptionThrower::throwExceptionIfCondition(numZone <= zone, L"", __FILE__, __LINE__); } @@ -515,4 +515,8 @@ int CodablecashNodeInstance::getListningPort() const noexcept { return this->p2pServer->getListningPort(); } +void CodablecashNodeInstance::setShardExtendValidator(const AbstractShardExtentionValidator *validator) { + this->statusCache->setShardExtentionValidator(validator); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc/CodablecashNodeInstance.h b/src_blockchain/bc/CodablecashNodeInstance.h index e60ff02..f951869 100644 --- a/src_blockchain/bc/CodablecashNodeInstance.h +++ b/src_blockchain/bc/CodablecashNodeInstance.h @@ -39,6 +39,8 @@ class P2pDnsManager; class P2pHandshake; class P2pNodeRecord; class NodeIdentifier; +class AbstractShardExtentionValidator; + class CodablecashNodeInstance : public IPubsubCommandExecutor { public: @@ -132,6 +134,8 @@ class CodablecashNodeInstance : public IPubsubCommandExecutor { return this->blockGenerator; } + void setShardExtendValidator(const AbstractShardExtentionValidator *validator); + private: void __init(const File* baseDir, ISystemLogger* logger, const CodablecashSystemParam* config); void __maintainNetwork(uint16_t zone); diff --git a/src_blockchain/bc/CodablecashSystemParam.cpp b/src_blockchain/bc/CodablecashSystemParam.cpp index 8a62fe3..e457437 100644 --- a/src_blockchain/bc/CodablecashSystemParam.cpp +++ b/src_blockchain/bc/CodablecashSystemParam.cpp @@ -46,6 +46,9 @@ CodablecashSystemParam::CodablecashSystemParam(const CodablecashSystemParam &ins this->consensusTrxAllowedDelayMillis = inst.consensusTrxAllowedDelayMillis; this->consensusPosVoteLimitMillis = inst.consensusPosVoteLimitMillis; + + this->remoteUtxoSaveHeightPeriod = inst.remoteUtxoSaveHeightPeriod; + this->remoteUtxoExpireHeight = inst.remoteUtxoExpireHeight; } CodablecashSystemParam::CodablecashSystemParam() { @@ -85,6 +88,9 @@ CodablecashSystemParam::CodablecashSystemParam() { this->consensusTrxAllowedDelayMillis = 3000; this->consensusPosVoteLimitMillis = 20000; + + this->remoteUtxoSaveHeightPeriod = 10000; + this->remoteUtxoExpireHeight = 5000; } CodablecashSystemParam::~CodablecashSystemParam() { diff --git a/src_blockchain/bc/CodablecashSystemParam.h b/src_blockchain/bc/CodablecashSystemParam.h index 2b698be..c633614 100644 --- a/src_blockchain/bc/CodablecashSystemParam.h +++ b/src_blockchain/bc/CodablecashSystemParam.h @@ -12,6 +12,9 @@ namespace codablecash { +class BlockchainSoftwareVersion; + + class CodablecashSystemParam { public: CodablecashSystemParam(const CodablecashSystemParam& inst); @@ -112,6 +115,13 @@ class CodablecashSystemParam { return this->consensusPosVoteLimitMillis; } + uint64_t getRemoteUtxoSaveHeightPeriod(const BlockchainSoftwareVersion* version) const noexcept { + return this->remoteUtxoSaveHeightPeriod; + } + uint64_t getRemoteUtxoExpireHeight(const BlockchainSoftwareVersion* version) const noexcept { + return this->remoteUtxoExpireHeight; + } + private: // pow uint16_t powHashrateBlocks; @@ -160,6 +170,10 @@ class CodablecashSystemParam { // Pos Vote Limit uint32_t consensusPosVoteLimitMillis; + // Remote UtxoId to Store period by height + uint64_t remoteUtxoSaveHeightPeriod; + uint64_t remoteUtxoExpireHeight; + }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_base/Abstract32BytesId.cpp b/src_blockchain/bc_base/Abstract32BytesId.cpp index c0bcf8d..58828dd 100644 --- a/src_blockchain/bc_base/Abstract32BytesId.cpp +++ b/src_blockchain/bc_base/Abstract32BytesId.cpp @@ -99,7 +99,7 @@ ByteBuffer* Abstract32BytesId::makeRandom16Bytes() { int size = 0; BigInteger p(L"0", 16); do{ - BigInteger seed = BigInteger::ramdom(); + BigInteger seed = BigInteger::random(); BigInteger s = seed.mod(Abstract32BytesId::Q); p = G.modPow(s, Abstract32BytesId::Q); diff --git a/src_blockchain/bc_base_trx_index/TransactionDataFactory.cpp b/src_blockchain/bc_base_trx_index/TransactionDataFactory.cpp index 331a388..1953603 100644 --- a/src_blockchain/bc_base_trx_index/TransactionDataFactory.cpp +++ b/src_blockchain/bc_base_trx_index/TransactionDataFactory.cpp @@ -27,11 +27,8 @@ IBlockObject* TransactionDataFactory::makeDataFromBinary(ByteBuffer *in) { return TransactionData::fromBinary(in); } -void TransactionDataFactory::registerData(const AbstractBtreeKey *key, - const IBlockObject *data, DataNode *dataNode, - BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); - dataNode->setDataFpos(dataFpos); +void TransactionDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); } bool TransactionDataFactory::beforeRemove(DataNode *dataNode, diff --git a/src_blockchain/bc_base_trx_index/TransactionIdDataFactory.cpp b/src_blockchain/bc_base_trx_index/TransactionIdDataFactory.cpp index e39fc8e..122d818 100644 --- a/src_blockchain/bc_base_trx_index/TransactionIdDataFactory.cpp +++ b/src_blockchain/bc_base_trx_index/TransactionIdDataFactory.cpp @@ -27,8 +27,7 @@ IBlockObject* TransactionIdDataFactory::makeDataFromBinary(ByteBuffer *in) { void TransactionIdDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); - dataNode->setDataFpos(dataFpos); + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); } bool TransactionIdDataFactory::beforeRemove(DataNode *dataNode, diff --git a/src_blockchain/bc_base_utxo_index/AddressDescriptorDataFactory.cpp b/src_blockchain/bc_base_utxo_index/AddressDescriptorDataFactory.cpp index 6e1fd0e..5cf0d92 100644 --- a/src_blockchain/bc_base_utxo_index/AddressDescriptorDataFactory.cpp +++ b/src_blockchain/bc_base_utxo_index/AddressDescriptorDataFactory.cpp @@ -27,10 +27,8 @@ IBlockObject* AddressDescriptorDataFactory::makeDataFromBinary(ByteBuffer *in) { } void AddressDescriptorDataFactory::registerData(const AbstractBtreeKey *key, - const IBlockObject *data, DataNode *dataNode, - BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); - dataNode->setDataFpos(dataFpos); + const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); } bool AddressDescriptorDataFactory::beforeRemove(DataNode *dataNode, diff --git a/src_blockchain/bc_base_utxo_index/UtxoDataFactory.cpp b/src_blockchain/bc_base_utxo_index/UtxoDataFactory.cpp index 64b21e8..77ba9e4 100644 --- a/src_blockchain/bc_base_utxo_index/UtxoDataFactory.cpp +++ b/src_blockchain/bc_base_utxo_index/UtxoDataFactory.cpp @@ -28,8 +28,7 @@ IBlockObject* UtxoDataFactory::makeDataFromBinary(ByteBuffer *in) { void UtxoDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); - dataNode->setDataFpos(dataFpos); + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); } bool UtxoDataFactory::beforeRemove(DataNode *dataNode, BtreeStorage *store, const AbstractBtreeKey *key) const { diff --git a/src_blockchain/bc_block/Block.cpp b/src_blockchain/bc_block/Block.cpp index d909733..3ac5409 100644 --- a/src_blockchain/bc_block/Block.cpp +++ b/src_blockchain/bc_block/Block.cpp @@ -62,6 +62,7 @@ void Block::addControlTransaction(const AbstractControlTransaction *trx) noexcep case AbstractBlockchainTransaction::TRX_TYPE_VOTE_BLOCK: addVote(dynamic_cast(trx)); break; + // FIXME[multishard]add header command to register new zone default: this->body->addControlTransaction(trx); break; diff --git a/src_blockchain/bc_block/BlockHeader.h b/src_blockchain/bc_block/BlockHeader.h index 5988370..9c1f282 100644 --- a/src_blockchain/bc_block/BlockHeader.h +++ b/src_blockchain/bc_block/BlockHeader.h @@ -112,6 +112,10 @@ class BlockHeader : public alinous::IBlockObject { void addHeaderCommand(const AbstractBlockHeaderCommand* cmd); bool hasHeaderCommnads() const noexcept; + ArrayList* getHeaderCommands() const noexcept { + return this->commnads; + } + private: BlockVersion* version; diff --git a/src_blockchain/bc_block_generator/BlockGenerator.cpp b/src_blockchain/bc_block_generator/BlockGenerator.cpp index 9db61a7..bfb7d5f 100644 --- a/src_blockchain/bc_block_generator/BlockGenerator.cpp +++ b/src_blockchain/bc_block_generator/BlockGenerator.cpp @@ -249,6 +249,11 @@ void BlockGenerator::importInterChainCommunicationTransactions2Block(MemPoolTran if(result == TrxValidationResult::OK){ block->addInterChainCommunicationTransaction(trx); context->importInterChainCommunicationTransaction(header, trx, this->logger); + + uint8_t tyxType = trx->getType(); + if(tyxType == AbstractInterChainCommunicationTansaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ + // FIXME[multishard] add RecognizedNewShardCommand to the header + } } } } diff --git a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp index 28b3db3..59f0638 100644 --- a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp +++ b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp @@ -7,11 +7,13 @@ #include "bc_block_header_command/AbstractBlockHeaderCommand.h" #include "bc_block_header_command/NewShardZoneCommand.h" +#include "bc_block_header_command/RecognizedNewShardCommand.h" #include "base_io/ByteBuffer.h" #include "base/StackRelease.h" + namespace codablecash { AbstractBlockHeaderCommand::AbstractBlockHeaderCommand(const AbstractBlockHeaderCommand &inst) { @@ -35,6 +37,9 @@ AbstractBlockHeaderCommand* AbstractBlockHeaderCommand::createFromBinary(ByteBuf case NEW_SHARD_COMMAND: ret = new NewShardZoneCommand(); break; + case RECOGNIZED_SHARD_COMMAND: + ret = new RecognizedNewShardCommand(); + break; default: return nullptr; } diff --git a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h index 26c19ab..f2817ed 100644 --- a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h +++ b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h @@ -18,9 +18,16 @@ using namespace alinous; namespace codablecash { +class BlockHeader; +class BlockchainStatusCache; +class CodablecashBlockchain; +class CodablecashSystemParam; +class ILockinManager; + class AbstractBlockHeaderCommand : public alinous::IBlockObject { public: static constexpr const uint16_t NEW_SHARD_COMMAND = 1; + static constexpr const uint16_t RECOGNIZED_SHARD_COMMAND = 2; AbstractBlockHeaderCommand(const AbstractBlockHeaderCommand& inst); explicit AbstractBlockHeaderCommand(uint16_t type); @@ -30,6 +37,9 @@ class AbstractBlockHeaderCommand : public alinous::IBlockObject { virtual void fromBinary(ByteBuffer* in) = 0; + virtual void onFinalize(const BlockHeader *header, BlockchainStatusCache* statusCache, CodablecashBlockchain* blockchain, ILockinManager *lockinManager, const CodablecashSystemParam* config) = 0; + + protected: uint16_t type; }; diff --git a/src_blockchain/bc_block_header_command/CMakeLists.txt b/src_blockchain/bc_block_header_command/CMakeLists.txt index 883f191..14e9d7a 100644 --- a/src_blockchain/bc_block_header_command/CMakeLists.txt +++ b/src_blockchain/bc_block_header_command/CMakeLists.txt @@ -3,5 +3,6 @@ set(__src AbstractBlockHeaderCommand.cpp NewShardZoneCommand.cpp + RecognizedNewShardCommand.cpp ) handle_sub(codablecashlib "${__src}" blockchain bc_block_header_command) diff --git a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp index 46415e2..653319b 100644 --- a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp +++ b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp @@ -7,45 +7,122 @@ #include "bc_block_header_command/NewShardZoneCommand.h" +#include "bc_blockstore/CodablecashBlockchain.h" + +#include "bc_status_cache/BlockchainStatusCache.h" + +#include "bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h" + +#include "bc_processor/CentralProcessor.h" + +#include "bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h" + +#include "bc_block/BlockHeaderId.h" +#include "bc_block/BlockHeader.h" +#include "bc_block/Block.h" + +#include "bc_base/BinaryUtils.h" namespace codablecash { NewShardZoneCommand::NewShardZoneCommand(const NewShardZoneCommand &inst) : AbstractBlockHeaderCommand(inst) { - this->newShardNo = inst.newShardNo; + this->newShardZone = inst.newShardZone; + this->requestingZone = inst.requestingZone; + this->genesisBlock = inst.genesisBlock != nullptr ? new Block(*inst.genesisBlock) : nullptr; } NewShardZoneCommand::NewShardZoneCommand() : AbstractBlockHeaderCommand(AbstractBlockHeaderCommand::NEW_SHARD_COMMAND) { - this->newShardNo = 0; + this->newShardZone = 0; + this->requestingZone = 0; + this->genesisBlock = nullptr; } NewShardZoneCommand::~NewShardZoneCommand() { - + delete this->genesisBlock; } int NewShardZoneCommand::binarySize() const { + BinaryUtils::checkNotNull(this->genesisBlock); + int total = sizeof(uint16_t); total += sizeof(uint16_t); + total += sizeof(uint16_t); + total += this->genesisBlock->binarySize(); return total; } void NewShardZoneCommand::toBinary(ByteBuffer *out) const { + BinaryUtils::checkNotNull(this->genesisBlock); + out->putShort(this->type); - out->putShort(this->newShardNo); + out->putShort(this->newShardZone); + out->putShort(this->requestingZone); + this->genesisBlock->toBinary(out); } void NewShardZoneCommand::fromBinary(ByteBuffer* in) { - this->newShardNo = in->getShort(); + this->newShardZone = in->getShort(); + this->requestingZone = in->getShort(); + + this->genesisBlock = Block::createFromBinary(in); } IBlockObject* NewShardZoneCommand::copyData() const noexcept { return new NewShardZoneCommand(*this); } -void NewShardZoneCommand::setNewShardNo(uint16_t newShardNo) noexcept { - this->newShardNo = newShardNo; +void NewShardZoneCommand::setNewShardZone(uint16_t newShardZone) noexcept { + this->newShardZone = newShardZone; +} + +void NewShardZoneCommand::setRequestingZone(uint16_t requestingZone) noexcept { + this->requestingZone = requestingZone; +} + +void NewShardZoneCommand::onFinalize(const BlockHeader *header, BlockchainStatusCache *statusCache, CodablecashBlockchain *blockchain, + ILockinManager *lockinManager, const CodablecashSystemParam *config) { + uint16_t zoneSelf = statusCache->getZoneSelf(); + + // FIXME[multishard] create phisical store + if(zoneSelf != this->newShardZone){ + statusCache->newZone(false); + blockchain->addZone(this->newShardZone); + }else{ + statusCache->newZone(true); + blockchain->addZone(this->newShardZone); + + //[multishard] generate genesis block + CentralProcessor* processor = blockchain->getProcessor(); + + GenerateNewGenesisBlockCommandMessage* message = new GenerateNewGenesisBlockCommandMessage(); + message->setNewShardZone(this->newShardZone); + + processor->addCommandMessage(message); + } + + + // broad cast ICC + if(zoneSelf == this->requestingZone){ // make inter shard communication trx + CentralProcessor* processor = blockchain->getProcessor(); + + NotifyShardExtendRequestCommandMessage* message = new NotifyShardExtendRequestCommandMessage(); + message->setNewShardZone(this->newShardZone); + message->setRequestingZone(this->requestingZone); + + uint64_t height = header->getHeight(); + const BlockHeaderId* headerId = header->getId(); + message->setHeaderInfo(height, headerId); + + processor->addCommandMessage(message); + } +} + +void NewShardZoneCommand::setGenesisblock(const Block *block) { + delete this->genesisBlock; + this->genesisBlock = new Block(*block); } } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/NewShardZoneCommand.h b/src_blockchain/bc_block_header_command/NewShardZoneCommand.h index dc5c788..76975a8 100644 --- a/src_blockchain/bc_block_header_command/NewShardZoneCommand.h +++ b/src_blockchain/bc_block_header_command/NewShardZoneCommand.h @@ -12,6 +12,9 @@ namespace codablecash { +class BlockHeaderId; +class Block; + class NewShardZoneCommand : public AbstractBlockHeaderCommand { public: NewShardZoneCommand(const NewShardZoneCommand& inst); @@ -24,10 +27,18 @@ class NewShardZoneCommand : public AbstractBlockHeaderCommand { virtual IBlockObject* copyData() const noexcept; - void setNewShardNo(uint16_t newShardNo) noexcept; + void setNewShardZone(uint16_t newShardZone) noexcept; + void setRequestingZone(uint16_t requestingZone) noexcept; + void setGenesisblock(const Block* block); + + virtual void onFinalize(const BlockHeader *header, BlockchainStatusCache* statusCache, CodablecashBlockchain* blockchain, ILockinManager *lockinManager, const CodablecashSystemParam* config); private: - uint16_t newShardNo; + uint16_t newShardZone; + uint16_t requestingZone; + + Block* genesisBlock; + }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp new file mode 100644 index 0000000..f4cab71 --- /dev/null +++ b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp @@ -0,0 +1,44 @@ +/* + * RecognizedNewShardCommand.cpp + * + * Created on: Jul 4, 2026 + * Author: iizuka + */ + +#include "bc_block_header_command/RecognizedNewShardCommand.h" + +namespace codablecash { + +RecognizedNewShardCommand::RecognizedNewShardCommand(const RecognizedNewShardCommand &inst) : AbstractBlockHeaderCommand(inst) { +} + +RecognizedNewShardCommand::RecognizedNewShardCommand() : AbstractBlockHeaderCommand(AbstractBlockHeaderCommand::RECOGNIZED_SHARD_COMMAND) { + // TODO Auto-generated constructor stub + +} + +RecognizedNewShardCommand::~RecognizedNewShardCommand() { + +} + +int RecognizedNewShardCommand::binarySize() const { + int total = sizeof(uint16_t); + + return total; +} + +void RecognizedNewShardCommand::toBinary(ByteBuffer *out) const { +} + +void RecognizedNewShardCommand::fromBinary(ByteBuffer *in) { +} + +IBlockObject* RecognizedNewShardCommand::copyData() const noexcept { + return new RecognizedNewShardCommand(*this); +} + +void RecognizedNewShardCommand::onFinalize(const BlockHeader *header,BlockchainStatusCache *statusCache, CodablecashBlockchain *blockchain, + ILockinManager *lockinManager, const CodablecashSystemParam *config) { +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h new file mode 100644 index 0000000..e57e30a --- /dev/null +++ b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h @@ -0,0 +1,33 @@ +/* + * RecognizedNewShardCommand.h + * + * Created on: Jul 4, 2026 + * Author: iizuka + */ + +#ifndef BC_BLOCK_HEADER_COMMAND_RECOGNIZEDNEWSHARDCOMMAND_H_ +#define BC_BLOCK_HEADER_COMMAND_RECOGNIZEDNEWSHARDCOMMAND_H_ + +#include "bc_block_header_command/AbstractBlockHeaderCommand.h" + +namespace codablecash { + +class RecognizedNewShardCommand : public AbstractBlockHeaderCommand { +public: + RecognizedNewShardCommand(const RecognizedNewShardCommand& inst); + RecognizedNewShardCommand(); + virtual ~RecognizedNewShardCommand(); + + virtual int binarySize() const; + virtual void toBinary(ByteBuffer* out) const; + virtual void fromBinary(ByteBuffer* in); + + virtual IBlockObject* copyData() const noexcept; + + virtual void onFinalize(const BlockHeader *header, BlockchainStatusCache* statusCache, CodablecashBlockchain* blockchain, ILockinManager *lockinManager, const CodablecashSystemParam* config); + +}; + +} /* namespace codablecash */ + +#endif /* BC_BLOCK_HEADER_COMMAND_RECOGNIZEDNEWSHARDCOMMAND_H_ */ diff --git a/src_blockchain/bc_block_validator/BlockValidator.cpp b/src_blockchain/bc_block_validator/BlockValidator.cpp index fd9cb56..8ca35a8 100644 --- a/src_blockchain/bc_block_validator/BlockValidator.cpp +++ b/src_blockchain/bc_block_validator/BlockValidator.cpp @@ -30,10 +30,13 @@ #include "bc_block_validator/BlockValidationException.h" #include "bc_status_cache/BlockchainController.h" +#include "bc_status_cache/AbstractShardExtentionValidator.h" +#include "bc_status_cache/BlockchainStatusCache.h" #include "bc_status_cache_context/IStatusCacheContext.h" #include "bc_status_cache_context_finalizer/VotingBlockStatus.h" +#include "bc_status_cache_context_finalizer/VoteCandidate.h" #include "bc_memorypool/MemoryPool.h" #include "bc_memorypool/MemPoolTransaction.h" @@ -54,8 +57,6 @@ #include "bc_block_vote/VotePart.h" #include "bc_block_vote/VotedHeaderIdGroup.h" -#include "bc_status_cache_context_finalizer/VoteCandidate.h" - #include "bc_finalizer_trx/VoteBlockTransaction.h" #include "bc_finalizer_trx/RevokeMissVotedTicket.h" #include "bc_finalizer_trx/RevokeMissedTicket.h" @@ -72,6 +73,8 @@ #include "base_timestamp/SystemTimestamp.h" +#include "bc_block_header_command/NewShardZoneCommand.h" + namespace codablecash { BlockValidator::BlockValidator(const Block* block, CodablecashSystemParam* config, MemoryPool* memoryPool, BlockchainController* ctrl) { @@ -94,8 +97,8 @@ void BlockValidator::validate() { // validate last header validateLastHeader(); - validateHashrate(); + validateHeaderCommand(); { uint16_t zoneSelf = this->ctrl->getZoneSelf(); @@ -110,6 +113,35 @@ void BlockValidator::validate() { } } +void BlockValidator::validateHeaderCommand() { + const BlockHeader* header = this->block->getHeader(); + + ArrayList* list = header->getHeaderCommands(); + if(!list->isEmpty()){ + const BlockHeaderId* lastheaderId = header->getLastHeaderId(); + uint64_t lastheight = header->getHeight() - 1; + uint16_t zone = header->getZone(); + + IStatusCacheContext* context = this->ctrl->getStatusCacheContext(zone, lastheaderId, lastheight); __STP(context); + + BlockchainStatusCache* cache = context->getBlockchainStatusCache(); + AbstractShardExtentionValidator* extValidator = cache->getShardExtentionValidator(); + + // [multishard] header command validate + int maxLoop = list->size(); + for(int i = 0; i != maxLoop; ++i){ + AbstractBlockHeaderCommand* cmd = list->get(i); + + NewShardZoneCommand* newShardCommand = dynamic_cast(cmd); + if(newShardCommand != nullptr){ + bool res = extValidator->validate(newShardCommand, context, this->ctrl); + ExceptionThrower::throwExceptionIfCondition(res == false + , L"The header command to extend new shard is wrong.", __FILE__, __LINE__); + } + } + } +} + void BlockValidator::validateLastHeader() { if(this->block->getHeight() == 1){ return; @@ -140,8 +172,6 @@ void BlockValidator::validateTransactionsInBlock(MemPoolTransaction *memTrx) { IStatusCacheContext* context = this->ctrl->getStatusCacheContext(zone, lastheaderId, lastheight); __STP(context); - // FIXME [multishard] header command validate - validateControlTransactions(memTrx, header, context); validateInterChainCommunicationTransactions(memTrx, header, context); validateBalanceTransactions(memTrx, header, context); diff --git a/src_blockchain/bc_block_validator/BlockValidator.h b/src_blockchain/bc_block_validator/BlockValidator.h index 1a07f7a..62f38d0 100644 --- a/src_blockchain/bc_block_validator/BlockValidator.h +++ b/src_blockchain/bc_block_validator/BlockValidator.h @@ -29,6 +29,7 @@ class BlockValidator { virtual ~BlockValidator(); void validate(); + void validateHeaderCommand(); // if mined self private: void validateLastHeader(); diff --git a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp index 1dd40cf..390ae91 100644 --- a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp +++ b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp @@ -143,6 +143,15 @@ void CodablecashBlockchain::initBlankchain() { initZonesStore(); } +void CodablecashBlockchain::addZone(uint16_t zone) { + if(zone == this->numZones){ + this->numZones++; + saveCondig(); + + initZone(zone); + } +} + void CodablecashBlockchain::initZonesStore() { int maxLoop = this->numZones; for(int i = 0; i != maxLoop; ++i){ @@ -326,4 +335,5 @@ ArrayList* CodablecashBlockchain::getBlockHeadersHeightAt(uint16_t return __STP_MV(headers); } + } /* namespace codablecash */ diff --git a/src_blockchain/bc_blockstore/CodablecashBlockchain.h b/src_blockchain/bc_blockstore/CodablecashBlockchain.h index aef0678..110fe61 100644 --- a/src_blockchain/bc_blockstore/CodablecashBlockchain.h +++ b/src_blockchain/bc_blockstore/CodablecashBlockchain.h @@ -50,6 +50,9 @@ class CodablecashBlockchain : public IBlockchainStoreProvider { static const UnicodeString KEY_BLOCK_VERSION_MINOR; static const UnicodeString KEY_BLOCK_VERSION_PATCH; +public: + void addZone(uint16_t zone); + private: CodablecashBlockchain(const File* baseDir, uint16_t zoneSelf, uint16_t numZones); void initBlankchain(); @@ -78,13 +81,6 @@ class CodablecashBlockchain : public IBlockchainStoreProvider { virtual BlockHeaderStoreManager* getHeaderManager(uint16_t zone) const noexcept; virtual BlockBodyStoreManager* getBlockBodyStoreManager(uint16_t zone) const noexcept; - uint16_t getZoneSelf() const noexcept { - return this->zoneSelf; - } - int getNumZones() const noexcept { - return this->zonesStore->size(); - } - void setProcessor(CentralProcessor* processor) noexcept { this->processor = processor; } @@ -94,6 +90,10 @@ class CodablecashBlockchain : public IBlockchainStoreProvider { void firePostBlockAdded(const Block* block); + const BlockchainSoftwareVersion* getVersion() const noexcept { + return this->version; + } + private: void fireBlockAdded(MemPoolTransaction* memTrx, const Block* block); void fireBlockHeaderAdded(MemPoolTransaction* memTrx, const BlockHeader* header); diff --git a/src_blockchain/bc_finalizer/VoterEntryFactory.cpp b/src_blockchain/bc_finalizer/VoterEntryFactory.cpp index afdf1a2..b8402f8 100644 --- a/src_blockchain/bc_finalizer/VoterEntryFactory.cpp +++ b/src_blockchain/bc_finalizer/VoterEntryFactory.cpp @@ -28,8 +28,7 @@ IBlockObject* VoterEntryFactory::makeDataFromBinary(ByteBuffer *in) { void VoterEntryFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); - dataNode->setDataFpos(dataFpos); + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); } AbstractBtreeDataFactory* VoterEntryFactory::copy() const noexcept { diff --git a/src_blockchain/bc_network_instance/CodablecashNetworkNode.cpp b/src_blockchain/bc_network_instance/CodablecashNetworkNode.cpp index bc94bfa..692f463 100644 --- a/src_blockchain/bc_network_instance/CodablecashNetworkNode.cpp +++ b/src_blockchain/bc_network_instance/CodablecashNetworkNode.cpp @@ -71,16 +71,16 @@ void CodablecashNetworkNode::setNetworkConfig(const CodablecashNetworkNodeConfig this->nwconfig = new CodablecashNetworkNodeConfig(*nwconfig); } -bool CodablecashNetworkNode::initBlank(uint16_t zoneSelf) { +bool CodablecashNetworkNode::initBlank(uint16_t zoneSelf, int numZones) { CodablecashNodeInstance inst(this->baseDir, this->logger, this->nwconfig->getSysConfig()); - bool bl = inst.initBlankInstance(zoneSelf, 1); + bool bl = inst.initBlankInstance(zoneSelf, numZones); return bl; } -bool CodablecashNetworkNode::generateNetwork(uint16_t zoneSelf) { +bool CodablecashNetworkNode::generateNetwork(uint16_t zoneSelf, int numZones) { CodablecashNodeInstance inst(this->baseDir, this->logger, this->nwconfig->getSysConfig()); - bool bl = inst.initBlankInstance(zoneSelf, 1); + bool bl = inst.initBlankInstance(zoneSelf, numZones); const GenesisBalanceConfig* gconfig = this->nwconfig->getGenesisConfig(); bl = bl && (gconfig != nullptr); diff --git a/src_blockchain/bc_network_instance/CodablecashNetworkNode.h b/src_blockchain/bc_network_instance/CodablecashNetworkNode.h index 628bceb..22d9672 100644 --- a/src_blockchain/bc_network_instance/CodablecashNetworkNode.h +++ b/src_blockchain/bc_network_instance/CodablecashNetworkNode.h @@ -35,8 +35,8 @@ class CodablecashNetworkNode { void setNetworkConfig(const CodablecashNetworkNodeConfig* nwconfig); - bool initBlank(uint16_t zoneSelf); - bool generateNetwork(uint16_t zoneSelf); + bool initBlank(uint16_t zoneSelf, int numZones); + bool generateNetwork(uint16_t zoneSelf, int numZones); void startNetwork(INetworkSeeder* seeder, bool pending); void syncNetwork(); diff --git a/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp b/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp index ce1739d..28486ac 100644 --- a/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp +++ b/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp @@ -29,6 +29,7 @@ #include "bc_p2p_cmd_client_notify/ClientNotifyBlockMinedCommand.h" +#include "bc_block_validator/BlockValidator.h" namespace codablecash { @@ -45,6 +46,12 @@ MinerMinedReportCommandMessage::~MinerMinedReportCommandMessage() { */ void MinerMinedReportCommandMessage::process(CentralProcessor *processor) { BlockchainController* ctrl = processor->getCtrl(); + CodablecashSystemParam* config = processor->getCodablecashSystemParam(); + MemoryPool* memoryPool = processor->getMemoryPool(); + + BlockValidator validator(this->block, config, memoryPool, ctrl); + validator.validateHeaderCommand(); + ctrl->addBlock(this->block); // Pos Voting Request @@ -67,7 +74,6 @@ void MinerMinedReportCommandMessage::process(CentralProcessor *processor) { { const BlockHeader* header = data->getHeader(); uint16_t zone = header->getZone(); - CodablecashSystemParam* config = processor->getCodablecashSystemParam(); ctrl->registerBlockHeader4Limit(zone, header, config); } diff --git a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp index 333b36b..c6a4246 100644 --- a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp +++ b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp @@ -59,6 +59,11 @@ #include "bc_block_body/OmittedBlockBodyFixer.h" #include "bc_block_validator/BlockHeaderValidator.h" + +#include "base_timestamp/SystemTimestamp.h" + +#include "bc_block_validator/BlockValidationException.h" + namespace codablecash { TransferedMinedReportCommandMessage::TransferedMinedReportCommandMessage() { @@ -101,7 +106,10 @@ void TransferedMinedReportCommandMessage::process(CentralProcessor *processor) { // [consensus]check if the block time is after PoSLimit { uint64_t height = header->getHeight(); - ctrl->getPosVoteLimit(zone, height); + SystemTimestamp* limit = ctrl->getPosVoteLimit(zone, height); __STP(limit); + const SystemTimestamp* blockGenerated = header->getTimestamp(); + + ExceptionThrower::throwExceptionIfCondition(blockGenerated->compareTo(limit) > 0, L"The block time must be after PoSLimit", __FILE__, __LINE__); } diff --git a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp new file mode 100644 index 0000000..800b49d --- /dev/null +++ b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp @@ -0,0 +1,28 @@ +/* + * AbstractShardExtentionValidator.cpp + * + * Created on: Jun 29, 2026 + * Author: iizuka + */ + +#include "bc_status_cache/AbstractShardExtentionValidator.h" + +namespace codablecash { + +AbstractShardExtentionValidator::AbstractShardExtentionValidator(const AbstractShardExtentionValidator &inst) { + this->statusCache = inst.statusCache; +} + +AbstractShardExtentionValidator::AbstractShardExtentionValidator() { + this->statusCache = nullptr; +} + +AbstractShardExtentionValidator::~AbstractShardExtentionValidator() { + this->statusCache = nullptr; +} + +void AbstractShardExtentionValidator::setStatusCache(BlockchainStatusCache *stcache) noexcept { + this->statusCache = stcache; +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h new file mode 100644 index 0000000..0b62af0 --- /dev/null +++ b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h @@ -0,0 +1,37 @@ +/* + * AbstractShardExtentionValidator.h + * + * Created on: Jun 29, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_ABSTRACTSHARDEXTENTIONVALIDATOR_H_ +#define BC_STATUS_CACHE_ABSTRACTSHARDEXTENTIONVALIDATOR_H_ + +namespace codablecash { + +class BlockchainStatusCache; +class IStatusCacheContext; +class BlockchainController; +class NewShardZoneCommand; + + +class AbstractShardExtentionValidator { +public: + AbstractShardExtentionValidator(const AbstractShardExtentionValidator& inst); + AbstractShardExtentionValidator(); + virtual ~AbstractShardExtentionValidator(); + + virtual AbstractShardExtentionValidator* copy() const = 0; + + void setStatusCache(BlockchainStatusCache* stcache) noexcept; + + virtual bool validate(NewShardZoneCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl) = 0; + +private: + BlockchainStatusCache* statusCache; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_ABSTRACTSHARDEXTENTIONVALIDATOR_H_ */ diff --git a/src_blockchain/bc_status_cache/BlockchainController.cpp b/src_blockchain/bc_status_cache/BlockchainController.cpp index 0140883..8218a2d 100644 --- a/src_blockchain/bc_status_cache/BlockchainController.cpp +++ b/src_blockchain/bc_status_cache/BlockchainController.cpp @@ -16,6 +16,8 @@ #include "bc_status_cache_context_finalizer/AlreadyFinalizedException.h" +#include "bc_status_cache_lockin/LockinManager.h" + #include "bc_blockstore/CodablecashBlockchain.h" #include "bc_blockstore_header/BlockHeaderStoreManager.h" @@ -57,6 +59,8 @@ #include "transaction/AbstractSmartcontractTransaction.h" +#include "bc_block_header_command/AbstractBlockHeaderCommand.h" + namespace codablecash { @@ -182,13 +186,13 @@ uint64_t BlockchainController::getHeadHeight(uint16_t zone) { uint16_t BlockchainController::getZoneSelf() const noexcept { StackReadLock __lock(this->rwLock, __FILE__, __LINE__); - return this->blockchain->getZoneSelf(); + return this->statusCache->getZoneSelf(); } uint16_t BlockchainController::getNUmZones() const noexcept { StackReadLock __lock(this->rwLock, __FILE__, __LINE__); - return this->blockchain->getNumZones(); + return this->statusCache->getNumZones(); } uint64_t BlockchainController::getFinalizedHeight(uint16_t zone) const { @@ -429,7 +433,7 @@ void BlockchainController::finalize(uint16_t zone, uint64_t finalizingHeight, co this->blockchain->cleanOnFinalize(zone, finalizingHeight, headerId, lastFinalizedHeight); // update finalized data - this->statusCache->updateFinalizedCacheData(zone, finalizingHeight, headerId, this->blockchain, memTrx, context); + this->statusCache->updateFinalizedCacheData(zone, finalizingHeight, headerId, this->blockchain, memTrx, context, this->config); // clean memory Pool { @@ -471,6 +475,9 @@ void BlockchainController::finalizeHeader(uint16_t zone, uint64_t finalizingHeig uint64_t lastFinalizedHeight = this->statusCache->getFinalizedHeight(zone); this->blockchain->cleanOnFinalize(zone, finalizingHeight, headerId, lastFinalizedHeight); + // handle header commands + handleHeaderCommandsOnFinalize(zone, finalizingHeight, headerId); + // set finalized height // update finalized data this->statusCache->updateFinalizedHeaderCacheData(zone, finalizingHeight, headerId, this->blockchain, memTrx); @@ -479,6 +486,60 @@ void BlockchainController::finalizeHeader(uint16_t zone, uint64_t finalizingHeig // header does not use status cache } +void BlockchainController::handleHeaderCommandsOnFinalize(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId) { + ZoneStatusCache* zoneCashe = this->statusCache->getZoneStatusCache(zone); + assert(zoneCashe != nullptr); + + uint64_t lastFinalizedHeight = zoneCashe->getFinalizedHeight(); + + ArrayList list; + list.setDeleteOnExit(); + + BlockHeaderStoreManager* headerManager = this->blockchain->getHeaderManager(zone); + BlockBodyStoreManager* bodyManager = this->blockchain->getBlockBodyStoreManager(zone); + + // make list + { + uint64_t height = finalizingHeight; + BlockHeaderId* currentHeaderId = dynamic_cast(headerId->copyData()); + + while(height > lastFinalizedHeight){ + __STP(currentHeaderId); + list.addElement(dynamic_cast(currentHeaderId->copyData())); + + BlockHeader* header = headerManager->getHeader(currentHeaderId, height); __STP(header); + + currentHeaderId = dynamic_cast(header->getLastHeaderId()->copyData()); + height--; + } + __STP(currentHeaderId); + } + + // [multishard] handle header commands + { + uint64_t height = lastFinalizedHeight + 1; + LockinManager* lockinManager = this->statusCache->getLockInManager(this->statusCache->getZoneSelf()); + + int startLoop = list.size() - 1; + for(int i = startLoop; i >= 0; --i){ + BlockHeaderId* id = list.get(i); + + BlockHeader* header = headerManager->getHeader(id, height); __STP(header); + assert(header != nullptr); + height++; + + ArrayList* commands = header->getHeaderCommands(); + int maxLoop = commands->size(); + for(int j = 0; j != maxLoop; ++j){ + AbstractBlockHeaderCommand* cmd = commands->get(j); + + cmd->onFinalize(header, this->statusCache, this->blockchain, lockinManager, this->config); + } + + } + } +} + void BlockchainController::checkFinalizedHeight(uint64_t finalizedHeight, uint64_t finalizingHeight) { if(finalizedHeight >= finalizingHeight){ throw new AlreadyFinalizedException(__FILE__, __LINE__); @@ -647,7 +708,7 @@ BigInteger BlockchainController::calcTargetDifficulty(uint16_t zone, uint64_t la void BlockchainController::requestMiningBlock(MemPoolTransaction* memTrx) { StackWriteLock __lock(this->rwLock, __FILE__, __LINE__); - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); ZoneStatusCache* cache = this->statusCache->getZoneStatusCache(zoneSelf); cache->updateBlockStatus(memTrx, this->blockchain, this->config); @@ -656,7 +717,7 @@ void BlockchainController::requestMiningBlock(MemPoolTransaction* memTrx) { } const VoterEntry* BlockchainController::getVoterEntry(const NodeIdentifier *nodeId) { - uint16_t zone = this->blockchain->getZoneSelf(); + uint16_t zone = this->statusCache->getZoneSelf(); IStatusCacheContext* context = getStatusCacheContext(zone); __STP(context); @@ -667,7 +728,7 @@ const VoterEntry* BlockchainController::getVoterEntry(const NodeIdentifier *node BlockHeader* BlockchainController::getTopHeader() const { StackWriteLock __lock(this->rwLock, __FILE__, __LINE__); - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); const BlockHead* head = this->statusCache->getHead(zoneSelf); const BlockHeader* header = head->getHeadHeader(); @@ -679,7 +740,7 @@ bool BlockchainController::checkAcceptSecondRealBlockOnMining() const { bool result = false; - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); const BlockHead* head = this->statusCache->getHead(zoneSelf); // scheduled block const BlockHead* secondHead = this->statusCache->getSecondHead(zoneSelf); // real block @@ -714,7 +775,7 @@ bool BlockchainController::checkAcceptSecondRealBlockOnMining() const { BlockHeader* BlockchainController::changeMiningTarget() { StackWriteLock __lock(this->rwLock, __FILE__, __LINE__); - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); return this->statusCache->changeMiningTarget(zoneSelf); } @@ -722,7 +783,7 @@ BlockHeader* BlockchainController::changeMiningTarget() { void BlockchainController::setScheduledBlock(const Block *block) { StackWriteLock __lock(this->rwLock, __FILE__, __LINE__); - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); this->statusCache->setScheduledBlock(zoneSelf, block); } @@ -733,12 +794,12 @@ Block* BlockchainController::fetechScheduledBlock() { } Block* BlockchainController::__fetechScheduledBlock() { - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); return this->statusCache->fetchScheduledBlock(zoneSelf); } Block* BlockchainController::__getScheduledBlock() { - uint16_t zoneSelf = this->blockchain->getZoneSelf(); + uint16_t zoneSelf = this->statusCache->getZoneSelf(); return this->statusCache->getScheduledBlock(zoneSelf); } diff --git a/src_blockchain/bc_status_cache/BlockchainController.h b/src_blockchain/bc_status_cache/BlockchainController.h index ffc1c1d..e15375f 100644 --- a/src_blockchain/bc_status_cache/BlockchainController.h +++ b/src_blockchain/bc_status_cache/BlockchainController.h @@ -65,6 +65,10 @@ class BlockchainController { void finalize(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId, MemoryPool* memPool); void finalizeHeader(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId, MemoryPool* memPool); +private: + void handleHeaderCommandsOnFinalize(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId); + +public: //BlockHeader* getNblocksBefore(uint16_t zone, const BlockHeaderId *headerId, uint64_t height, int voteBeforeNBlocks) const; BlockHeader* __getNblocksBefore(uint16_t zone, const BlockHeaderId *headerId, uint64_t height, int voteBeforeNBlocks) const; diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp b/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp index 28af0ba..66f5b61 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp @@ -60,11 +60,13 @@ #include "bc/CodablecashSystemParam.h" +#include "bc_status_cache/AbstractShardExtentionValidator.h" namespace codablecash { BlockchainStatusCache::BlockchainStatusCache(const File* baseDir, const CodablecashSystemParam* config, MemoryPool* memPool, const File* tmpCacheBaseDir, ISystemLogger* logger) { this->config = config; this->baseDir = new File(*baseDir); + this->zoneListSize = 0; this->numZones = 0; this->zoneSelf = 0; this->statusStore = nullptr; @@ -74,6 +76,7 @@ BlockchainStatusCache::BlockchainStatusCache(const File* baseDir, const Codablec this->memPool = memPool; this->lastVoted = 0; this->logger = logger; + this->shardExtentionValidator = nullptr; } BlockchainStatusCache::~BlockchainStatusCache() { @@ -84,6 +87,7 @@ BlockchainStatusCache::~BlockchainStatusCache() { delete this->memberLock; this->memPool = nullptr; + delete this->shardExtentionValidator; } void BlockchainStatusCache::initBlankCache(uint16_t zoneSelf, uint16_t numZones) { @@ -91,7 +95,6 @@ void BlockchainStatusCache::initBlankCache(uint16_t zoneSelf, uint16_t numZones) this->zoneSelf = zoneSelf; this->statusStore = new StatusStore(this->baseDir, CONFIG_FILE_NAME); - saveConfig(); for(int i = 0; i != numZones; ++i){ ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, i, i != zoneSelf, this->logger, this->config); @@ -99,14 +102,17 @@ void BlockchainStatusCache::initBlankCache(uint16_t zoneSelf, uint16_t numZones) cache->initBlank(); } + + this->zoneListSize = this->zoneList.size(); + saveConfig(); } void BlockchainStatusCache::open() { this->statusStore = new StatusStore(this->baseDir, CONFIG_FILE_NAME); loadConfig(); - for(int i = 0; i != this->numZones; ++i){ - ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, this->logger, i != this->zoneSelf, this->config); + for(int i = 0; i != this->zoneListSize; ++i){ + ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, i, this->logger, i != this->zoneSelf, this->config); this->zoneList.addElement(cache); cache->open(); @@ -124,6 +130,26 @@ void BlockchainStatusCache::close() { this->zoneList.reset(); } +void BlockchainStatusCache::newZone(bool headerOnly) { + uint16_t nZone = this->numZones; + + // init blank + { + ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, nZone, headerOnly, this->logger, this->config); __STP(cache); + cache->initBlank(); + } + // open and add list + { + ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, nZone, this->logger, headerOnly, this->config); __STP(cache); + cache->open(); + + this->zoneList.addElement(__STP_MV(cache)); + } + + this->zoneListSize = this->zoneList.size(); + saveConfig(); +} + void BlockchainStatusCache::initCacheStatus(CodablecashBlockchain *blockchain) { int maxLoop = this->zoneList.size(); for(int i = 0; i != maxLoop; ++i){ @@ -141,12 +167,14 @@ void BlockchainStatusCache::initCacheStatus(CodablecashBlockchain *blockchain) { void BlockchainStatusCache::saveConfig() { this->statusStore->addShortValue(KEY_NUM_ZONES, this->numZones); this->statusStore->addShortValue(KEY_ZONE_SELF, this->zoneSelf); + this->statusStore->addShortValue(KEY_ZONE_LIST_SIZE, this->zoneListSize); } void BlockchainStatusCache::loadConfig() { this->statusStore->load(); this->numZones = this->statusStore->getShortValue(KEY_NUM_ZONES); this->zoneSelf = this->statusStore->getShortValue(KEY_ZONE_SELF); + this->zoneListSize = this->statusStore->getShortValue(KEY_ZONE_LIST_SIZE); } void BlockchainStatusCache::setPowManager(PoWManager *pow) noexcept { @@ -382,6 +410,13 @@ uint16_t BlockchainStatusCache::getNumZones() const { return this->numZones; } +int BlockchainStatusCache::getRequestedNewShards(uint16_t zone) const noexcept { + ZoneStatusCache* cache = this->zoneList.get(zone); + assert(cache != nullptr); + + return cache->getRequestedNewShards(); +} + void BlockchainStatusCache::markConsumedTransactions(MemPoolTransaction *memTrx, BlockBody *body) { { const ArrayList* list = body->getControlTransactions(); @@ -426,10 +461,15 @@ void BlockchainStatusCache::markConsumedTransactions(MemPoolTransaction *memTrx, } void BlockchainStatusCache::updateFinalizedCacheData(uint16_t zone, uint64_t finalizingHeight - , const BlockHeaderId *headerId, CodablecashBlockchain* blockchain, MemPoolTransaction* memtrx, IStatusCacheContext* context) { + , const BlockHeaderId *headerId, CodablecashBlockchain* blockchain, MemPoolTransaction* memtrx, IStatusCacheContext* context, const CodablecashSystemParam* config) { ZoneStatusCache* cache = this->zoneList.get(zone); - cache->finalizeUpdateCacheData(finalizingHeight, headerId, blockchain, context); + // update zone data of blank context + context->setNumZones(this->numZones); + context->setRequestedNewShards(cache->getRequestedNewShards()); + + // import blocks and the transactons + cache->finalizeUpdateCacheData(finalizingHeight, headerId, blockchain, context, config); cache->updateBlockStatus(memtrx, blockchain, this->config); } @@ -481,7 +521,20 @@ void BlockchainStatusCache::requestPosVote(uint16_t zone, uint64_t calculatedNon } } +void BlockchainStatusCache::setNumZones(uint16_t numZones) noexcept { + this->numZones = numZones; +} + +void BlockchainStatusCache::setShardExtentionValidator(const AbstractShardExtentionValidator *validator) noexcept { + delete this->shardExtentionValidator; + this->shardExtentionValidator = validator->copy(); + this->shardExtentionValidator->setStatusCache(this); +} +RemoteUtxoRepository* BlockchainStatusCache::getRemoteUtxoRepository(uint16_t zone) const noexcept { + ZoneStatusCache* cache = this->zoneList.get(zone); + return cache->getRemoteUtxoRepository(); +} } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.h b/src_blockchain/bc_status_cache/BlockchainStatusCache.h index 3cec98f..44a3915 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.h +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.h @@ -42,7 +42,8 @@ class MemoryPool; class ISystemLogger; class BlockHeaderStoreManager; class LockinManager; - +class AbstractShardExtentionValidator; +class RemoteUtxoRepository; class BlockchainStatusCache : public IBlockchainEventListner { public: @@ -50,6 +51,7 @@ class BlockchainStatusCache : public IBlockchainEventListner { static const constexpr wchar_t* KEY_NUM_ZONES{L"numZones"}; static const constexpr wchar_t* KEY_ZONE_SELF{L"zoneSelf"}; + static const constexpr wchar_t* KEY_ZONE_LIST_SIZE{L"zoneListSize"}; explicit BlockchainStatusCache(const File* baseDir, const CodablecashSystemParam* config, MemoryPool* memPool, const File* tmpCacheBaseDir, ISystemLogger* logger); virtual ~BlockchainStatusCache(); @@ -60,6 +62,8 @@ class BlockchainStatusCache : public IBlockchainEventListner { void close(); void initCacheStatus(CodablecashBlockchain* blockchain); + void newZone(bool headerOnly); + virtual void onBlockAdded(MemPoolTransaction* memTrx, const Block* block, CodablecashBlockchain* chain); virtual void postBlockAdded(const Block* block, CodablecashBlockchain* chain); virtual void onBlockHeaderAdded(MemPoolTransaction* memTrx, const BlockHeader* block, CodablecashBlockchain* chain); @@ -74,7 +78,7 @@ class BlockchainStatusCache : public IBlockchainEventListner { void setFinalizer(FinalizerPool* finalizer); void updateFinalizedCacheData(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId - , CodablecashBlockchain* blockchain, MemPoolTransaction* memtrx, IStatusCacheContext* context); + , CodablecashBlockchain* blockchain, MemPoolTransaction* memtrx, IStatusCacheContext* context, const CodablecashSystemParam* config); void updateFinalizedHeaderCacheData(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId , CodablecashBlockchain* blockchain, MemPoolTransaction* memTrx); @@ -90,6 +94,12 @@ class BlockchainStatusCache : public IBlockchainEventListner { void importCosumedMemTransactions(uint16_t zone, MemPoolTransaction* memTrx, uint64_t heigh, const BlockHeaderId *headerId, CodablecashBlockchain* blockchain); uint16_t getNumZones() const; + uint16_t getZoneSelf() const noexcept { + return this->zoneSelf; + } + + int getRequestedNewShards(uint16_t zone) const noexcept; + void setNumZones(uint16_t numZones) noexcept; ZoneStatusCache* getZoneStatusCache(uint16_t zone) const noexcept { return this->zoneList.get(zone); @@ -115,6 +125,13 @@ class BlockchainStatusCache : public IBlockchainEventListner { LockinManager* getLockInManager(uint16_t zone) const noexcept; + void setShardExtentionValidator(const AbstractShardExtentionValidator* validator) noexcept; + AbstractShardExtentionValidator* getShardExtentionValidator() const noexcept { + return this->shardExtentionValidator; + } + + RemoteUtxoRepository* getRemoteUtxoRepository(uint16_t zone) const noexcept; + private: void saveConfig(); void loadConfig(); @@ -125,6 +142,7 @@ class BlockchainStatusCache : public IBlockchainEventListner { const CodablecashSystemParam* config; File* baseDir; + int zoneListSize; ArrayList zoneList; uint16_t numZones; @@ -140,6 +158,8 @@ class BlockchainStatusCache : public IBlockchainEventListner { MemoryPool* memPool; ISystemLogger* logger; + + AbstractShardExtentionValidator* shardExtentionValidator; }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/CMakeLists.txt b/src_blockchain/bc_status_cache/CMakeLists.txt index a862fbd..d0b027b 100644 --- a/src_blockchain/bc_status_cache/CMakeLists.txt +++ b/src_blockchain/bc_status_cache/CMakeLists.txt @@ -1,6 +1,7 @@ set(__src + AbstractShardExtentionValidator.cpp BlockchainController.cpp BlockchainStatusCache.cpp BlockHead.cpp diff --git a/src_blockchain/bc_status_cache/ZoneStatusCache.cpp b/src_blockchain/bc_status_cache/ZoneStatusCache.cpp index 3c2521e..271a0e9 100644 --- a/src_blockchain/bc_status_cache/ZoneStatusCache.cpp +++ b/src_blockchain/bc_status_cache/ZoneStatusCache.cpp @@ -46,6 +46,7 @@ #include "bc_status_cache_vote/VoteManager.h" +#include "bc_status_cache/BlockchainStatusCache.h" namespace codablecash { @@ -56,7 +57,7 @@ ZoneStatusCache::ZoneStatusCache(const File* baseDir, uint16_t zone, bool header this->finalizedHeight = 0; UnicodeString name(DIR_NAME_BASE); - addIdex2String(&name); + addIdex2String(&name); // add zone this->baseDir = baseDir->get(&name); this->headBlockDetector = new HeadBlockDetector(logger); @@ -71,13 +72,13 @@ ZoneStatusCache::ZoneStatusCache(const File* baseDir, uint16_t zone, bool header this->requestedNewShards = 0; } -ZoneStatusCache::ZoneStatusCache(const File *baseDir, ISystemLogger* logger, bool headerOnly, const CodablecashSystemParam* config) { - this->zone = 0; +ZoneStatusCache::ZoneStatusCache(const File *baseDir, uint16_t zone, ISystemLogger* logger, bool headerOnly, const CodablecashSystemParam* config) { + this->zone = zone; this->headerOnly = headerOnly; this->finalizedHeight = 0; UnicodeString name(DIR_NAME_BASE); - addIdex2String(&name); + addIdex2String(&name); // add zone this->baseDir = baseDir->get(&name); this->headBlockDetector = new HeadBlockDetector(logger); @@ -184,7 +185,7 @@ void ZoneStatusCache::updateBlockStatus(MemPoolTransaction* memTrx, CodablecashB } void ZoneStatusCache::finalizeUpdateCacheData(uint64_t finalizingHeight, const BlockHeaderId *headerId - , CodablecashBlockchain *blockchain, IStatusCacheContext* context) { + , CodablecashBlockchain *blockchain, IStatusCacheContext* context, const CodablecashSystemParam* config) { uint64_t lastFinalizedHeight = this->finalizedHeight; ArrayList list; @@ -192,6 +193,7 @@ void ZoneStatusCache::finalizeUpdateCacheData(uint64_t finalizingHeight, const B BlockHeaderStoreManager* headerManager = blockchain->getHeaderManager(this->zone); BlockBodyStoreManager* bodyManager = blockchain->getBlockBodyStoreManager(this->zone); + const BlockchainSoftwareVersion* version = blockchain->getVersion(); // make list { @@ -232,6 +234,16 @@ void ZoneStatusCache::finalizeUpdateCacheData(uint64_t finalizingHeight, const B } } + // [multishard] writeback zone data + this->requestedNewShards = context->getRequestedNewShards(); + + BlockchainStatusCache* statusCache = context->getBlockchainStatusCache(); + statusCache->setNumZones(context->getNumZones()); + + // writeback remote utxo + this->finalizedCache->writeBackRemoteUtxo(finalizingHeight, context, config, version); + + // writeback Voter this->finalizedCache->writeBackVoterEntries(context); this->finalizedCache->writeBackVoterStatus(context); @@ -367,4 +379,8 @@ SystemTimestamp* ZoneStatusCache::getPosVoteLimit(uint64_t lastHeight) { return this->voteManager->getPosVoteLimit(lastHeight); } +RemoteUtxoRepository* ZoneStatusCache::getRemoteUtxoRepository() const noexcept { + return this->finalizedCache->getRemoteUtxoRepository(); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/ZoneStatusCache.h b/src_blockchain/bc_status_cache/ZoneStatusCache.h index 67b75a7..a555e5a 100644 --- a/src_blockchain/bc_status_cache/ZoneStatusCache.h +++ b/src_blockchain/bc_status_cache/ZoneStatusCache.h @@ -38,6 +38,7 @@ class Block; class LockinManager; class BlockHeaderStoreManager; class VoteManager; +class RemoteUtxoRepository; class ZoneStatusCache { public: @@ -51,7 +52,7 @@ class ZoneStatusCache { static const constexpr wchar_t* KEY_REQUESTED_SHARDS{L"requestedShards"}; ZoneStatusCache(const File* baseDir, uint16_t zone, bool headerOnly, ISystemLogger* logger, const CodablecashSystemParam* config); - ZoneStatusCache(const File* baseDir, ISystemLogger* logger, bool headerOnly, const CodablecashSystemParam* config); + ZoneStatusCache(const File* baseDir, uint16_t zone, ISystemLogger* logger, bool headerOnly, const CodablecashSystemParam* config); virtual ~ZoneStatusCache(); void initBlank(); @@ -60,7 +61,7 @@ class ZoneStatusCache { void close(); void updateBlockStatus(MemPoolTransaction* memTrx, CodablecashBlockchain *chain, const CodablecashSystemParam* config); - void finalizeUpdateCacheData(uint64_t finalizingHeight, const BlockHeaderId *headerId, CodablecashBlockchain* blockchain, IStatusCacheContext* context); + void finalizeUpdateCacheData(uint64_t finalizingHeight, const BlockHeaderId *headerId, CodablecashBlockchain* blockchain, IStatusCacheContext* context, const CodablecashSystemParam* config); void finalizeHeaderUpdateCacheData(uint64_t finalizingHeight, const BlockHeaderId *headerId, CodablecashBlockchain* blockchain); const BlockHead* getHead() const noexcept; @@ -94,6 +95,12 @@ class ZoneStatusCache { bool registerBlockHeader4Limit(const BlockHeader* header, const CodablecashSystemParam* param); SystemTimestamp* getPosVoteLimit(uint64_t lastHeight); + int getRequestedNewShards() const noexcept { + return this->requestedNewShards; + } + + RemoteUtxoRepository* getRemoteUtxoRepository() const noexcept; + private: void addIdex2String(UnicodeString *str) const noexcept; diff --git a/src_blockchain/bc_status_cache_context/CMakeLists.txt b/src_blockchain/bc_status_cache_context/CMakeLists.txt index 630150a..7b9db8e 100644 --- a/src_blockchain/bc_status_cache_context/CMakeLists.txt +++ b/src_blockchain/bc_status_cache_context/CMakeLists.txt @@ -6,6 +6,8 @@ set(__src CachedStatusCacheRepository.cpp IStatusCacheContext.cpp NullLockinManager.cpp + RemoteUtxoDetector.cpp + RemoteUtxoHeight.cpp RemovedDummyUtxo.cpp StatusCacheContext.cpp TransactionContextCache.cpp diff --git a/src_blockchain/bc_status_cache_context/CachedStatusCache.cpp b/src_blockchain/bc_status_cache_context/CachedStatusCache.cpp index c3849c0..ce438ed 100644 --- a/src_blockchain/bc_status_cache_context/CachedStatusCache.cpp +++ b/src_blockchain/bc_status_cache_context/CachedStatusCache.cpp @@ -9,6 +9,8 @@ #include "bc_status_cache_context/TransactionContextCache.h" #include "bc_status_cache_context/UtxoCacheContext.h" #include "bc_status_cache_context/StatusCacheContext.h" +#include "bc_status_cache_context/RemoteUtxoDetector.h" +#include "bc_status_cache_context/RemoteUtxoHeight.h" #include "bc_status_cache_context_finalizer/VoterStatusMappedCacheContext.h" @@ -28,9 +30,10 @@ #include "bc_base_trx_index/TransactionIdData.h" #include "bc_base_utxo_index/UtxoIdKey.h" - #include "bc_base_utxo_index/UtxoData.h" +#include "bc_trx/UtxoId.h" + namespace codablecash { @@ -53,6 +56,12 @@ CachedStatusCache::CachedStatusCache(uint64_t serial, const File* cacheRepoBaseD this->voterCache = nullptr; this->ticketPrice = 0; + + // new shards history + this->requestedNewShards = 0; + this->numZones = 0; + + this->utxolist = new ArrayList(); } CachedStatusCache::~CachedStatusCache() { @@ -62,6 +71,9 @@ CachedStatusCache::~CachedStatusCache() { this->baseDir->deleteDir(); delete this->baseDir; + + this->utxolist->deleteElements(); + delete this->utxolist; } void CachedStatusCache::init() { @@ -90,6 +102,22 @@ void CachedStatusCache::importStatusContext(StatusCacheContext *context) { importVoterStatusCache(context->getVoterStatusCacheContext()); this->ticketPrice = context->getTicketPrice(); + + this->requestedNewShards = context->getRequestedNewShards(); + this->numZones = context->getNumZones(); + + // remote utxos + { + RemoteUtxoDetector* remoteUtxos = context->getRemoteUtxoDetector(); + ArrayList* list = remoteUtxos->getList(); + + int maxLoop = list->size(); + for(int i = 0; i != maxLoop; ++i){ + const RemoteUtxoHeight* utxoIdHeight = list->get(i); + + this->utxolist->addElement(new RemoteUtxoHeight(*utxoIdHeight)); + } + } } void CachedStatusCache::importOtherCache(const CachedStatusCache *previousCache) { diff --git a/src_blockchain/bc_status_cache_context/CachedStatusCache.h b/src_blockchain/bc_status_cache_context/CachedStatusCache.h index 23e52d2..436c1c4 100644 --- a/src_blockchain/bc_status_cache_context/CachedStatusCache.h +++ b/src_blockchain/bc_status_cache_context/CachedStatusCache.h @@ -34,6 +34,7 @@ class BlockHeaderId; class UtxoData; class TransactionId; class AbstractBlockchainTransaction; +class RemoteUtxoHeight; class CachedStatusCache { public: @@ -61,6 +62,17 @@ class CachedStatusCache { AbstractBlockchainTransaction* getTransaction(const TransactionId *trxId) const; + int getRequestedNewShards() const noexcept { + return this->requestedNewShards; + } + uint16_t getNumZones() const noexcept { + return this->numZones; + } + + ArrayList* getRemoteUtxoList() const noexcept { + return this->utxolist; + } + private: void importOtherCache(const CachedStatusCache* previousCache); @@ -78,6 +90,13 @@ class CachedStatusCache { TransactionContextCache* trxCache; UtxoCacheContext* utxoCache; VoterStatusMappedCacheContext* voterCache; + + // new shards history + int requestedNewShards; + uint16_t numZones; + + // remote utxo + ArrayList* utxolist; }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context/CachedStatusCacheContext.cpp b/src_blockchain/bc_status_cache_context/CachedStatusCacheContext.cpp index f732fff..5564ca8 100644 --- a/src_blockchain/bc_status_cache_context/CachedStatusCacheContext.cpp +++ b/src_blockchain/bc_status_cache_context/CachedStatusCacheContext.cpp @@ -6,6 +6,7 @@ */ #include "bc_status_cache_context/CachedStatusCacheContext.h" +#include "bc_status_cache_context/RemoteUtxoDetector.h" #include "base/UnicodeString.h" @@ -43,6 +44,21 @@ CachedStatusCacheContext::CachedStatusCacheContext(const CachedStatusCache* cach this->cache = cache; this->ticketPrice = cache->getTicketPrice(); + + this->requestedNewShards = cache->getRequestedNewShards(); + this->numZones = cache->getNumZones(); + + // [multishard]remoteUtxos + { + ArrayList* list = cache->getRemoteUtxoList(); + + int maxLoop = list->size(); + for(int i = 0; i != maxLoop; ++i){ + const RemoteUtxoHeight* utxo = list->get(i); + + this->remoteUtxos->add(utxo); + } + } } CachedStatusCacheContext::~CachedStatusCacheContext() { diff --git a/src_blockchain/bc_status_cache_context/IStatusCacheContext.h b/src_blockchain/bc_status_cache_context/IStatusCacheContext.h index ef7d7a0..b2faa9c 100644 --- a/src_blockchain/bc_status_cache_context/IStatusCacheContext.h +++ b/src_blockchain/bc_status_cache_context/IStatusCacheContext.h @@ -41,7 +41,7 @@ class ILockinManager; class VoterStatusMappedCacheContext; class ISystemLogger; class BlockBody; - +class RemoteUtxoDetector; class IStatusCacheContext { public: @@ -60,7 +60,6 @@ class IStatusCacheContext { virtual VotingBlockStatus* getVotingBlockStatus(const BlockHeaderId *blockHeaderId) = 0; virtual VotingBlockStatus* getVotingBlockStatus(const BlockHeader *header) = 0; virtual const VoterEntry* getVoterEntry(const NodeIdentifier* nodeId) const noexcept = 0; - virtual uint16_t getNumZones() const = 0; virtual const CodablecashSystemParam* getConfig() const noexcept = 0; virtual CodablecashBlockchain* getBlockChain() const noexcept = 0; @@ -73,8 +72,8 @@ class IStatusCacheContext { virtual void registerTicket(const BlockHeader *header, const RegisterTicketTransaction* trx) = 0; virtual void registerVote(const BlockHeader *header, const VoteBlockTransaction* trx) = 0; - virtual void beginBlock(const BlockHeader* header, ILockinManager* lockinManager) = 0; - virtual void endBlock(const BlockHeader* header, ILockinManager* lockinManager) = 0; + virtual void beginBlock(const BlockHeader* header, ILockinManager* lockinManager, bool finalize) = 0; + virtual void endBlock(const BlockHeader* header, ILockinManager* lockinManager, bool finalize) = 0; virtual uint16_t getZone() const noexcept = 0; virtual uint64_t getTicketPrice() const noexcept = 0; @@ -85,6 +84,13 @@ class IStatusCacheContext { virtual uint64_t getTopHeight() const noexcept = 0; virtual AbstractBlockchainTransaction* getTransaction(const TransactionId *trxId) = 0; + + virtual void setNumZones(uint16_t nuMzones) noexcept = 0; + virtual void setRequestedNewShards(int requestedNewShards) noexcept = 0; + virtual uint16_t getNumZones() const noexcept = 0; + virtual int getRequestedNewShards() const noexcept = 0; + + virtual RemoteUtxoDetector* getRemoteUtxoDetector() const noexcept = 0; }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context/RemoteUtxoDetector.cpp b/src_blockchain/bc_status_cache_context/RemoteUtxoDetector.cpp new file mode 100644 index 0000000..ed85596 --- /dev/null +++ b/src_blockchain/bc_status_cache_context/RemoteUtxoDetector.cpp @@ -0,0 +1,69 @@ +/* + * RemoteUtxoDetector.cpp + * + * Created on: Jul 9, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_context/RemoteUtxoDetector.h" +#include "bc_status_cache_context/RemoteUtxoHeight.h" + +#include "bc_trx/UtxoId.h" + +#include "bc_status_cache_data/RemoteUtxoRepository.h" + +#include "bc/CodablecashSystemParam.h" + +#include "base/StackRelease.h" + +#include "bc_status_cache_data/RemoteUtxoData.h" +namespace codablecash { + +RemoteUtxoDetector::RemoteUtxoDetector(RemoteUtxoRepository* finalizedUtxoRepo, uint64_t finalizedHeight, const CodablecashSystemParam* config, const BlockchainSoftwareVersion* version) { + this->finalizedUtxoRepo = finalizedUtxoRepo; + this->list = new ArrayList(); + + uint64_t expire = config->getRemoteUtxoExpireHeight(version); + this->expiredHeight = finalizedHeight > expire ? finalizedHeight - expire : 0; +} + +RemoteUtxoDetector::~RemoteUtxoDetector() { + this->list->deleteElements(); + delete this->list; +} + +void RemoteUtxoDetector::add(const RemoteUtxoHeight *utxo) noexcept { + this->list->addElement(new RemoteUtxoHeight(*utxo)); +} + +bool RemoteUtxoDetector::isRemoteUtxoUsed(const UtxoId *utxoId) const noexcept { + RemoteUtxoData* data = this->finalizedUtxoRepo->getUtxo(utxoId); __STP(data); + uint64_t height = data != nullptr ? data->getHeight() : 0; + + bool bl = (data != nullptr && height > this->expiredHeight) ? true : hasUtxo(utxoId); + return bl; +} + +void RemoteUtxoDetector::consumeRemoteUtxo(const UtxoId *utxoId, uint64_t height) { + RemoteUtxoHeight* data = new RemoteUtxoHeight(height, utxoId); + this->list->addElement(data); +} + +bool RemoteUtxoDetector::hasUtxo(const UtxoId *utxoId) const noexcept { + bool ret = false; + + int maxLoop = this->list->size(); + for(int i = 0; i != maxLoop; ++i){ + RemoteUtxoHeight* data = this->list->get(i); + const UtxoId* id = data->getUtxoId(); + + if(utxoId->equals(id)){ + ret = true; + break; + } + } + + return ret; +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context/RemoteUtxoDetector.h b/src_blockchain/bc_status_cache_context/RemoteUtxoDetector.h new file mode 100644 index 0000000..b08c6e9 --- /dev/null +++ b/src_blockchain/bc_status_cache_context/RemoteUtxoDetector.h @@ -0,0 +1,52 @@ +/* + * RemoteUtxoDetector.h + * + * Created on: Jul 9, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_CONTEXT_REMOTEUTXODETECTOR_H_ +#define BC_STATUS_CACHE_CONTEXT_REMOTEUTXODETECTOR_H_ + +#include "base/ArrayList.h" +#include + +using namespace alinous; + +namespace codablecash { + +class RemoteUtxoRepository; +class RemoteUtxoHeight; +class UtxoId; +class CodablecashSystemParam; +class BlockchainSoftwareVersion; + + +class RemoteUtxoDetector { +public: + RemoteUtxoDetector(RemoteUtxoRepository* finalizedUtxoRepo, uint64_t finalizedHeight, const CodablecashSystemParam* config, const BlockchainSoftwareVersion* version); + virtual ~RemoteUtxoDetector(); + + ArrayList* getList() const noexcept { + return this->list; + } + + void add(const RemoteUtxoHeight* utxo) noexcept; + + bool isRemoteUtxoUsed(const UtxoId* utxoId) const noexcept; + void consumeRemoteUtxo(const UtxoId* utxoId, uint64_t height); + +private: + bool hasUtxo(const UtxoId* utxoId) const noexcept; + +private: + uint64_t expiredHeight; + + RemoteUtxoRepository* finalizedUtxoRepo; + + ArrayList* list; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_CONTEXT_REMOTEUTXODETECTOR_H_ */ diff --git a/src_blockchain/bc_status_cache_context/RemoteUtxoHeight.cpp b/src_blockchain/bc_status_cache_context/RemoteUtxoHeight.cpp new file mode 100644 index 0000000..06a68e5 --- /dev/null +++ b/src_blockchain/bc_status_cache_context/RemoteUtxoHeight.cpp @@ -0,0 +1,29 @@ +/* + * RemoteUtxoHeight.cpp + * + * Created on: Jul 9, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_context/RemoteUtxoHeight.h" + +#include "bc_trx/UtxoId.h" + + +namespace codablecash { + +RemoteUtxoHeight::RemoteUtxoHeight(const RemoteUtxoHeight &inst) { + this->height = inst.height; + this->utxoId = dynamic_cast(inst.utxoId->copyData()); +} + +RemoteUtxoHeight::RemoteUtxoHeight(uint64_t height, const UtxoId* utxoId) { + this->height = height; + this->utxoId = dynamic_cast(utxoId->copyData()); +} + +RemoteUtxoHeight::~RemoteUtxoHeight() { + delete this->utxoId; +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context/RemoteUtxoHeight.h b/src_blockchain/bc_status_cache_context/RemoteUtxoHeight.h new file mode 100644 index 0000000..c51b152 --- /dev/null +++ b/src_blockchain/bc_status_cache_context/RemoteUtxoHeight.h @@ -0,0 +1,36 @@ +/* + * RemoteUtxoHeight.h + * + * Created on: Jul 9, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_CONTEXT_REMOTEUTXOHEIGHT_H_ +#define BC_STATUS_CACHE_CONTEXT_REMOTEUTXOHEIGHT_H_ +#include + +namespace codablecash { + +class UtxoId; + +class RemoteUtxoHeight { +public: + RemoteUtxoHeight(const RemoteUtxoHeight& inst); + RemoteUtxoHeight(uint64_t height, const UtxoId* utxoId); + virtual ~RemoteUtxoHeight(); + + uint64_t getHeight() const noexcept { + return this->height; + } + UtxoId* getUtxoId() const noexcept { + return this->utxoId; + } + +private: + uint64_t height; + UtxoId* utxoId; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_CONTEXT_REMOTEUTXOHEIGHT_H_ */ diff --git a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp index 2e9cd4d..6eda6ed 100644 --- a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp +++ b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp @@ -11,6 +11,7 @@ #include "bc_status_cache_context/TransactionContextCache.h" #include "bc_status_cache_context/UtxoCacheContext.h" #include "bc_status_cache_context/NullLockinManager.h" +#include "bc_status_cache_context/RemoteUtxoDetector.h" #include "bc_status_cache_context_finalizer/VoterStatusCacheContext.h" @@ -72,12 +73,15 @@ #include "bc_status_cache_data/FinalizedDataCache.h" #include "bc_network/NodeIdentifier.h" + #include "bc_status_cache_lockin/LockInOperationsData.h" #include "numeric/BigInteger.h" #include "merkletree/MerkleCertificate.h" +#include "bc_block_header_command/AbstractBlockHeaderCommand.h" + namespace codablecash { @@ -119,6 +123,14 @@ StatusCacheContext::StatusCacheContext(const CodablecashSystemParam* config, con this->voterCache = nullptr; this->topHeight = 0; + + this->numZones = statusCache->getNumZones(); + this->requestedNewShards = statusCache->getRequestedNewShards(zone); + + RemoteUtxoRepository* remoteUtxoRepo = statusCache->getRemoteUtxoRepository(zone); + uint64_t finalizedHeight = statusCache->getFinalizedHeight(zone); + const BlockchainSoftwareVersion* version = blockchain->getVersion(); + this->remoteUtxos = new RemoteUtxoDetector(remoteUtxoRepo, finalizedHeight, config, version); } StatusCacheContext::~StatusCacheContext() { @@ -128,6 +140,8 @@ StatusCacheContext::~StatusCacheContext() { this->rwLock->open(); } this->rwLock = nullptr; + + delete this->remoteUtxos; } void StatusCacheContext::close() { @@ -160,7 +174,7 @@ void StatusCacheContext::importBlock(const BlockHeader *header, const BlockBody LockinManager* liManager = this->statusCache->getLockInManager(this->zone); NullLockinManager lockinManager(liManager); - beginBlock(header, &lockinManager); + beginBlock(header, &lockinManager, false); importControlTransactions(header, blockBody, logger); importInterChainCommunicationTransactions(header, blockBody, logger); @@ -168,10 +182,15 @@ void StatusCacheContext::importBlock(const BlockHeader *header, const BlockBody importSmartcontractTransactions(header, blockBody, logger); importRewordTransactions(header, blockBody, logger); - endBlock(header, &lockinManager); + endBlock(header, &lockinManager, false); } -void StatusCacheContext::beginBlock(const BlockHeader *header, ILockinManager* lockinManager) { +void StatusCacheContext::beginBlock(const BlockHeader *header, ILockinManager* lockinManager, bool finalize) { + // [multishard] header commands + if(finalize){ + handleHeaderCommands(header, lockinManager); + } + // select voters ArrayList* list = getVoterEntries(); __STP(list); list->setDeleteOnExit(); @@ -211,7 +230,19 @@ void StatusCacheContext::beginBlock(const BlockHeader *header, ILockinManager* l } } -void StatusCacheContext::endBlock(const BlockHeader *header, ILockinManager* lockinManager) { +void StatusCacheContext::handleHeaderCommands(const BlockHeader *header, ILockinManager *lockinManager) { + // [multishard] header commands + ArrayList* list = header->getHeaderCommands(); + + int maxLoop = list->size(); + for(int i = 0; i != maxLoop; ++i){ + AbstractBlockHeaderCommand* cmd = list->get(i); + + cmd->onFinalize(header, this->statusCache, this->blockchain, lockinManager, this->config); + } +} + +void StatusCacheContext::endBlock(const BlockHeader *header, ILockinManager* lockinManager, bool finalize) { // update voted status check voted // This is what lockin operation have to do. @@ -219,7 +250,7 @@ void StatusCacheContext::endBlock(const BlockHeader *header, ILockinManager* loc * status of block height, which tickets of the block height to include in this block */ VotingBlockStatus* status = getVotingBlockStatus(header); __STP(status); - if(status != nullptr){ + if(finalize == true && status != nullptr){ // on Finalizing uint64_t height = header->getHeight(); int missingLimit = this->config->getVoteMissingLimit(height); @@ -363,6 +394,7 @@ void StatusCacheContext::importControlTransaction(const BlockHeader *header, co this->trxCache->putTransaction(trx); + // handle utxo importUtxo(trx, header); uint8_t type = trx->getType(); @@ -379,10 +411,10 @@ void StatusCacheContext::importControlTransaction(const BlockHeader *header, co registerVote(header, voteTrx); } else if(type == AbstractBlockchainTransaction::TRX_TYPE_REVOKE_MISSED_TICKET){ - // do nothing + // do nothing about controlling } else if(type == AbstractBlockchainTransaction::TRX_TYPE_REVOKE_MISS_VOTED_TICKET){ - // do nothing + // do nothing about controlling } } @@ -404,7 +436,8 @@ void StatusCacheContext::importUtxo(const AbstractBlockchainTransaction *trx, co // coinbase && stakebase uint8_t type = ref->getType(); if(type == AbstractUtxoReference::UTXO_REF_TYPE_COINBASE || - type == AbstractUtxoReference::UTXO_REF_TYPE_STAKEBASE){ + type == AbstractUtxoReference::UTXO_REF_TYPE_STAKEBASE || + ref->isRemote()){ continue; } @@ -562,10 +595,6 @@ const VoterEntry* StatusCacheContext::getVoterEntry(const NodeIdentifier *nodeId return entry; } -uint16_t StatusCacheContext::getNumZones() const { - return this->numZones; -} - void StatusCacheContext::loadInitialVotersData() { this->voterCache->loadFinalyzedVoters(this->zone, this->statusCache); @@ -623,4 +652,12 @@ AbstractBlockchainTransaction* StatusCacheContext::getTransaction(const Transact return this->trxCache->getTransaction(trxId); } +void StatusCacheContext::setNumZones(uint16_t numZones) noexcept { + this->numZones = numZones; +} + +void StatusCacheContext::setRequestedNewShards(int requestedNewShards) noexcept { + this->requestedNewShards = requestedNewShards; +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context/StatusCacheContext.h b/src_blockchain/bc_status_cache_context/StatusCacheContext.h index 6ea4574..0f44b97 100644 --- a/src_blockchain/bc_status_cache_context/StatusCacheContext.h +++ b/src_blockchain/bc_status_cache_context/StatusCacheContext.h @@ -45,6 +45,7 @@ class AbstractUtxoReference; class CachedStatusCache; class ReservedVotesStore; class TransactionId; +class RemoteUtxoDetector; class StatusCacheContext : public IStatusCacheContext { public: @@ -61,8 +62,8 @@ class StatusCacheContext : public IStatusCacheContext { void importBlock(const BlockHeader* header, const BlockBody* blockBody, ISystemLogger* logger); - virtual void beginBlock(const BlockHeader* header, ILockinManager* lockinManager); - virtual void endBlock(const BlockHeader* header, ILockinManager* lockinManager); + virtual void beginBlock(const BlockHeader* header, ILockinManager* lockinManager, bool finalize); + virtual void endBlock(const BlockHeader* header, ILockinManager* lockinManager, bool finalize); virtual void importBalanceTransaction(const BlockHeader* header, const AbstractBalanceTransaction* trx, ISystemLogger* logger); virtual void importControlTransaction(const BlockHeader* header, const BlockBody* body, const AbstractControlTransaction* trx, ISystemLogger* logger); @@ -84,7 +85,6 @@ class StatusCacheContext : public IStatusCacheContext { } virtual const VoterEntry* getVoterEntry(const NodeIdentifier* nodeId) const noexcept; - virtual uint16_t getNumZones() const; virtual const CodablecashSystemParam* getConfig() const noexcept { return this->config; @@ -127,7 +127,22 @@ class StatusCacheContext : public IStatusCacheContext { virtual AbstractBlockchainTransaction* getTransaction(const TransactionId *trxId); + virtual void setNumZones(uint16_t numZones) noexcept; + virtual void setRequestedNewShards(int requestedNewShards) noexcept; + virtual uint16_t getNumZones() const noexcept { + return this->numZones; + } + virtual int getRequestedNewShards() const noexcept { + return this->requestedNewShards; + } + + virtual RemoteUtxoDetector* getRemoteUtxoDetector() const noexcept { + return this->remoteUtxos; + } + protected: + void handleHeaderCommands(const BlockHeader *header, ILockinManager* lockinManager); + void importBalanceTransactions(const BlockHeader* header, const BlockBody* blockBody, ISystemLogger* logger); void importControlTransactions(const BlockHeader* header, const BlockBody* blockBody, ISystemLogger* logger); void importInterChainCommunicationTransactions(const BlockHeader* header, const BlockBody* blockBody, ISystemLogger* logger); @@ -169,6 +184,9 @@ class StatusCacheContext : public IStatusCacheContext { int requestedNewShards; uint16_t numZones; + // remote utxo + RemoteUtxoDetector* remoteUtxos; + }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context_finalizer/VotingBlockStatusDataFactory.cpp b/src_blockchain/bc_status_cache_context_finalizer/VotingBlockStatusDataFactory.cpp index b392875..e3661b2 100644 --- a/src_blockchain/bc_status_cache_context_finalizer/VotingBlockStatusDataFactory.cpp +++ b/src_blockchain/bc_status_cache_context_finalizer/VotingBlockStatusDataFactory.cpp @@ -27,8 +27,7 @@ IBlockObject* VotingBlockStatusDataFactory::makeDataFromBinary(ByteBuffer *in) { void VotingBlockStatusDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); - dataNode->setDataFpos(dataFpos); + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); } /* bool VotingBlockStatusDataFactory::beforeRemove(DataNode *dataNode, diff --git a/src_blockchain/bc_status_cache_data/CMakeLists.txt b/src_blockchain/bc_status_cache_data/CMakeLists.txt index 7bcc098..3a514fa 100644 --- a/src_blockchain/bc_status_cache_data/CMakeLists.txt +++ b/src_blockchain/bc_status_cache_data/CMakeLists.txt @@ -4,6 +4,13 @@ set(__src FinalizedDataCache.cpp FinalizedUtxoRepository.cpp FinalizedVoterRepository.cpp + RemoteUtxoData.cpp + RemoteUtxoDataFactory.cpp + RemoteUtxoHeightIndex.cpp + RemoteUtxoHeightIndexData.cpp + RemoteUtxoHeightIndexDataFactory.cpp + RemoteUtxoHistory.cpp; + RemoteUtxoRepository.cpp ) handle_sub(codablecashlib "${__src}" blockchain bc_status_cache_data) diff --git a/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp b/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp index 3653f9d..1b7711f 100644 --- a/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp +++ b/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp @@ -8,6 +8,7 @@ #include "bc_status_cache_data/FinalizedDataCache.h" #include "bc_status_cache_data/FinalizedUtxoRepository.h" #include "bc_status_cache_data/FinalizedVoterRepository.h" +#include "bc_status_cache_data/RemoteUtxoRepository.h" #include "base_io/File.h" @@ -36,7 +37,9 @@ #include "bc_status_cache/BlockchainStatusCache.h" +#include "bc_status_cache_context/RemoteUtxoDetector.h" #include "bc_status_cache_context/IStatusCacheContext.h" +#include "bc_status_cache_context/RemoteUtxoHeight.h" #include "bc_status_cache_context_finalizer/VoterStatusCacheContext.h" #include "bc_status_cache_context_finalizer/VoterStatusMappedCacheContext.h" @@ -45,15 +48,18 @@ #include "transaction/AbstractSmartcontractTransaction.h" +#include "bc/CodablecashSystemParam.h" namespace codablecash { FinalizedDataCache::FinalizedDataCache(const File* baseDir) { this->baseDir = baseDir->get(NAME_FINALIZED_DATA); - this->utxoRepo = new FinalizedUtxoRepository(baseDir); - this->voterRepo = new FinalizedVoterRepository(baseDir); - this->votingStatusCache = new VoterStatusCacheContext(baseDir); + this->utxoRepo = new FinalizedUtxoRepository(this->baseDir); + this->voterRepo = new FinalizedVoterRepository(this->baseDir); + this->votingStatusCache = new VoterStatusCacheContext(this->baseDir); + + this->remoteUrxo = new RemoteUtxoRepository(this->baseDir); } FinalizedDataCache::~FinalizedDataCache() { @@ -69,12 +75,14 @@ void FinalizedDataCache::initBlank() { this->utxoRepo->initBlank(); this->voterRepo->initBlank(); this->votingStatusCache->initBlank(); + this->remoteUrxo->initBlank(); } void FinalizedDataCache::open() { this->utxoRepo->open(); this->voterRepo->open(); this->votingStatusCache->open(); + this->remoteUrxo->open(); } void FinalizedDataCache::close() { @@ -90,6 +98,10 @@ void FinalizedDataCache::close() { this->votingStatusCache->close(); delete this->votingStatusCache, this->votingStatusCache = nullptr; } + if(this->remoteUrxo != nullptr){ + this->remoteUrxo->close(); + delete this->remoteUrxo, this->remoteUrxo = nullptr; + } } void FinalizedDataCache::importBlockData(uint64_t finalizingHeight, const BlockHeader *header, const BlockBody *body, IStatusCacheContext* context) { @@ -100,7 +112,7 @@ void FinalizedDataCache::importBlockData(uint64_t finalizingHeight, const BlockH lockinManager->setFinalizingHeight(finalizingHeight); - context->beginBlock(header, lockinManager); + context->beginBlock(header, lockinManager, true); importControlTransactions(header, body, context); importInterChainCommunicationTransactions(header, body); @@ -109,7 +121,7 @@ void FinalizedDataCache::importBlockData(uint64_t finalizingHeight, const BlockH importRewardBaseTransactions(header, body); // register lockin actions on Finalize @endBlock(); - context->endBlock(header, lockinManager); + context->endBlock(header, lockinManager, true); } void FinalizedDataCache::importRewardBaseTransactions(const BlockHeader *header, const BlockBody *body) { @@ -223,7 +235,8 @@ void FinalizedDataCache::importTransactionUtxos(const AbstractBlockchainTransact uint8_t type = ref->getType(); if(type == AbstractUtxoReference::UTXO_REF_TYPE_COINBASE - || type == AbstractUtxoReference::UTXO_REF_TYPE_STAKEBASE){ + || type == AbstractUtxoReference::UTXO_REF_TYPE_STAKEBASE + || ref->isRemote()){ continue; } @@ -273,6 +286,25 @@ void FinalizedDataCache::writeBackVoterStatus(IStatusCacheContext *context) { this->votingStatusCache->importRepo(voterStatusCache); } +void FinalizedDataCache::writeBackRemoteUtxo(uint64_t finalizingHeight, IStatusCacheContext *context, const CodablecashSystemParam* config, const BlockchainSoftwareVersion* version) { + RemoteUtxoDetector* utxoDetector = context->getRemoteUtxoDetector(); + ArrayList* list = utxoDetector->getList(); + + int maxLoop = list->size(); + for(int i = 0; i != maxLoop; ++i){ + RemoteUtxoHeight* utxoHeight = list->get(i); + + uint64_t height = utxoHeight->getHeight(); + const UtxoId* utxoId = utxoHeight->getUtxoId(); + this->remoteUrxo->addUtxo(height, utxoId); + } + + // clean + uint64_t period = config->getRemoteUtxoSaveHeightPeriod(version); + uint64_t cleanHeight = period < finalizingHeight ? finalizingHeight - period : 0; + this->remoteUrxo->clean(cleanHeight); +} + UtxoData* FinalizedDataCache::findUtxo(const UtxoId *utxoId) const { return this->utxoRepo->find(utxoId); } diff --git a/src_blockchain/bc_status_cache_data/FinalizedDataCache.h b/src_blockchain/bc_status_cache_data/FinalizedDataCache.h index 95d69f2..9244638 100644 --- a/src_blockchain/bc_status_cache_data/FinalizedDataCache.h +++ b/src_blockchain/bc_status_cache_data/FinalizedDataCache.h @@ -29,7 +29,9 @@ class IStatusCacheContext; class UtxoData; class UtxoId; class VoterStatusCacheContext; - +class RemoteUtxoRepository; +class CodablecashSystemParam; +class BlockchainSoftwareVersion; class FinalizedDataCache { public: @@ -46,6 +48,7 @@ class FinalizedDataCache { void importBlockData(uint64_t finalizingHeight, const BlockHeader* header, const BlockBody* body, IStatusCacheContext* context); void writeBackVoterEntries(IStatusCacheContext* context); void writeBackVoterStatus(IStatusCacheContext* context); + void writeBackRemoteUtxo(uint64_t finalizingHeight, IStatusCacheContext* context, const CodablecashSystemParam* config, const BlockchainSoftwareVersion* version); FinalizedVoterRepository* getFinalizedVoterRepository() const noexcept { return this->voterRepo; @@ -57,6 +60,10 @@ class FinalizedDataCache { UtxoData* findUtxo(const UtxoId* utxoId) const; + RemoteUtxoRepository* getRemoteUtxoRepository() const noexcept { + return this->remoteUrxo; + } + private: void importControlTransactions(const BlockHeader* header, const BlockBody* body, IStatusCacheContext* context); void importRegisterVotePoolTransaction(const BlockHeader* header, const BlockBody* body, const RegisterVotePoolTransaction* trx, IStatusCacheContext* context); @@ -77,6 +84,8 @@ class FinalizedDataCache { FinalizedUtxoRepository* utxoRepo; FinalizedVoterRepository* voterRepo; VoterStatusCacheContext* votingStatusCache; + + RemoteUtxoRepository* remoteUrxo; }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoData.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoData.cpp new file mode 100644 index 0000000..c011343 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoData.cpp @@ -0,0 +1,58 @@ +/* + * RemoteUtxoData.cpp + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoData.h" + +#include "bc_trx/UtxoId.h" + +#include "bc_base/BinaryUtils.h" + +#include "base/StackRelease.h" + + +namespace codablecash { + +RemoteUtxoData::RemoteUtxoData(const RemoteUtxoData &inst) { + this->utxoId = inst.utxoId != nullptr ? dynamic_cast(inst.utxoId->copyData()) : nullptr; + this->height = inst.height; +} + +RemoteUtxoData::RemoteUtxoData(const UtxoId* id, uint64_t height) { + this->utxoId = dynamic_cast(id->copyData()); + this->height = height; +} + +RemoteUtxoData::~RemoteUtxoData() { + delete this->utxoId; +} + +int RemoteUtxoData::binarySize() const { + BinaryUtils::checkNotNull(this->utxoId); + + int total = this->utxoId->binarySize(); + total += sizeof(uint64_t); + + return total; +} + +void RemoteUtxoData::toBinary(ByteBuffer *out) const { + this->utxoId->toBinary(out); + out->putLong(this->height); +} + +RemoteUtxoData* RemoteUtxoData::createFromBinary(ByteBuffer *in) { + UtxoId* id = UtxoId::fromBinary(in); __STP(id); + uint64_t height = in->getLong(); + + return new RemoteUtxoData(__STP_MV(id), height); +} + +IBlockObject* RemoteUtxoData::copyData() const noexcept { + return new RemoteUtxoData(*this); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoData.h b/src_blockchain/bc_status_cache_data/RemoteUtxoData.h new file mode 100644 index 0000000..e738087 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoData.h @@ -0,0 +1,43 @@ +/* + * RemoteUtxoData.h + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXODATA_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXODATA_H_ + +#include "filestore_block/IBlockObject.h" + +using namespace alinous; + +namespace codablecash { + +class UtxoId; + + +class RemoteUtxoData : public IBlockObject { +public: + RemoteUtxoData(const RemoteUtxoData& inst); + RemoteUtxoData(const UtxoId* id, uint64_t height); + virtual ~RemoteUtxoData(); + + virtual int binarySize() const; + virtual void toBinary(ByteBuffer* out) const; + static RemoteUtxoData* createFromBinary(ByteBuffer* in); + + virtual IBlockObject* copyData() const noexcept; + + uint64_t getHeight() const noexcept { + return this->height; + } + +private: + UtxoId* utxoId; + uint64_t height; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXODATA_H_ */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.cpp new file mode 100644 index 0000000..d634a76 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.cpp @@ -0,0 +1,41 @@ +/* + * RemoteUtxoDataFactory.cpp + * + * Created on: Jul 8, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoDataFactory.h" +#include "bc_status_cache_data/RemoteUtxoData.h" + +#include "btree/BtreeStorage.h" +#include "btree/DataNode.h" + + +namespace codablecash { + +RemoteUtxoDataFactory::RemoteUtxoDataFactory() { + +} + +RemoteUtxoDataFactory::~RemoteUtxoDataFactory() { + +} + +IBlockObject* RemoteUtxoDataFactory::makeDataFromBinary(ByteBuffer *in) { + return RemoteUtxoData::createFromBinary(in); +} + +void RemoteUtxoDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { + AbstractBtreeDataFactory::registerData(key, data, dataNode, store); +} + +bool RemoteUtxoDataFactory::beforeRemove(DataNode *dataNode, BtreeStorage *store, const AbstractBtreeKey *key) const { + return true; +} + +AbstractBtreeDataFactory* RemoteUtxoDataFactory::copy() const noexcept { + return new RemoteUtxoDataFactory(); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.h b/src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.h new file mode 100644 index 0000000..caba82a --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoDataFactory.h @@ -0,0 +1,32 @@ +/* + * RemoteUtxoDataFactory.h + * + * Created on: Jul 8, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXODATAFACTORY_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXODATAFACTORY_H_ + +#include "btree/AbstractBtreeDataFactory.h" + +using namespace alinous; + +namespace codablecash { + +class RemoteUtxoDataFactory : public AbstractBtreeDataFactory { +public: + RemoteUtxoDataFactory(); + virtual ~RemoteUtxoDataFactory(); + + virtual IBlockObject* makeDataFromBinary(ByteBuffer* in); + + virtual void registerData(const AbstractBtreeKey* key, const IBlockObject* data, DataNode* dataNode, BtreeStorage* store) const; + virtual bool beforeRemove(DataNode* dataNode, BtreeStorage* store, const AbstractBtreeKey* key) const; + + virtual AbstractBtreeDataFactory* copy() const noexcept; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXODATAFACTORY_H_ */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp new file mode 100644 index 0000000..f85bb41 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp @@ -0,0 +1,93 @@ +/* + * RemoteUtxoHeightIndex.cpp + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoHeightIndex.h" +#include "bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.h" +#include "bc_status_cache_data/RemoteUtxoHeightIndexData.h" + +#include "base/StackRelease.h" + +#include "base_io/File.h" + +#include "random_access_file/DiskCacheManager.h" + +#include "btree/Btree.h" +#include "btree/BtreeConfig.h" + +#include "btreekey/BtreeKeyFactory.h" +#include "btreekey/ULongKey.h" + + +namespace codablecash { + +RemoteUtxoHeightIndex::RemoteUtxoHeightIndex(const File* baseDir) { + this->baseDir = new File(*baseDir); + + this->cacheManager = new DiskCacheManager(); + this->btree = nullptr; +} + +RemoteUtxoHeightIndex::~RemoteUtxoHeightIndex() { + close(); + + delete this->baseDir; + delete this->cacheManager; +} + +void RemoteUtxoHeightIndex::initBlank() { + UnicodeString fileName(HISTORY_BASE_DIR); + + BtreeKeyFactory* keyFactory = new BtreeKeyFactory(); __STP(keyFactory); + RemoteUtxoHeightIndexDataFactory* dataFactory = new RemoteUtxoHeightIndexDataFactory(); __STP(dataFactory); + + Btree btree(this->baseDir, &fileName, this->cacheManager, keyFactory, dataFactory); + BtreeConfig config; + config.nodeNumber = 8; + config.defaultSize = 1024; + config.blockSize = 32; + btree.create(&config); +} + +void RemoteUtxoHeightIndex::open() { + UnicodeString fileName(HISTORY_BASE_DIR); + + BtreeKeyFactory* keyFactory = new BtreeKeyFactory(); __STP(keyFactory); + RemoteUtxoHeightIndexDataFactory* dataFactory = new RemoteUtxoHeightIndexDataFactory(); __STP(dataFactory); + + this->btree = new Btree(this->baseDir, &fileName, this->cacheManager, keyFactory, dataFactory); + + BtreeOpenConfig opconf; + opconf.numDataBuffer = 256; + opconf.numNodeBuffer = 512; + this->btree->open(&opconf); +} + +void RemoteUtxoHeightIndex::close() { + if(this->btree != nullptr){ + this->btree->close(); + delete this->btree, this->btree = nullptr; + } +} + +void RemoteUtxoHeightIndex::addUtxo(uint64_t height, const UtxoId *utxoId) { + ULongKey key(height); + RemoteUtxoHeightIndexData* data; + data->add(utxoId); + + this->btree->putData(&key, data); +} + +BtreeReverseScanner* RemoteUtxoHeightIndex::getReverseScanner() { + return this->btree->getReverseScanner(); +} + +void RemoteUtxoHeightIndex::remove(uint64_t height) { + ULongKey key(height); + this->btree->remove(&key); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.h b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.h new file mode 100644 index 0000000..e98a27b --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.h @@ -0,0 +1,50 @@ +/* + * RemoteUtxoHeightIndex.h + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEX_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEX_H_ +#include + +namespace alinous { +class File; +class DiskCacheManager; +class Btree; +class BtreeReverseScanner; +} +using namespace alinous; + +namespace codablecash { + +class UtxoId; + +class RemoteUtxoHeightIndex { +public: + static constexpr const wchar_t* HISTORY_BASE_DIR = L"heightindex"; + + explicit RemoteUtxoHeightIndex(const File* baseDir); + virtual ~RemoteUtxoHeightIndex(); + + void initBlank(); + + void open(); + void close(); + + void addUtxo(uint64_t height, const UtxoId* utxoId); + void remove(uint64_t height); + + BtreeReverseScanner* getReverseScanner(); + +private: + File* baseDir; + DiskCacheManager* cacheManager; + + Btree* btree; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEX_H_ */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.cpp new file mode 100644 index 0000000..68e0232 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.cpp @@ -0,0 +1,121 @@ +/* + * RemoteUtxoHeightIndexData.cpp + * + * Created on: Jul 8, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoHeightIndexData.h" + +#include "bc_trx/UtxoId.h" + +#include "base/StackRelease.h" + + +namespace codablecash { + +RemoteUtxoHeightIndexData::RemoteUtxoHeightIndexData(const RemoteUtxoHeightIndexData &inst) { + this->list = new ArrayList(); + + int maxLoop = inst.list->size(); + for(int i = 0; i != maxLoop; ++i){ + const UtxoId* v = inst.list->get(i); + + this->list->addElement(dynamic_cast(v->copyData())); + } +} + +RemoteUtxoHeightIndexData::RemoteUtxoHeightIndexData() { + this->list = new ArrayList(); +} + +RemoteUtxoHeightIndexData::~RemoteUtxoHeightIndexData() { + this->list->deleteElements(); + delete this->list; +} + +int RemoteUtxoHeightIndexData::binarySize() const { + int total = 0; + + int maxLoop = this->list->size(); + total += sizeof(uint16_t); + + for(int i = 0; i != maxLoop; ++i){ + const UtxoId* v = this->list->get(i); + total += v->binarySize(); + } + + return total; +} + +void RemoteUtxoHeightIndexData::toBinary(ByteBuffer *out) const { + int maxLoop = this->list->size(); + out->putShort(maxLoop); + + for(int i = 0; i != maxLoop; ++i){ + const UtxoId* v = this->list->get(i); + v->toBinary(out); + } +} + +RemoteUtxoHeightIndexData* RemoteUtxoHeightIndexData::createFromBinary(ByteBuffer *in) { + RemoteUtxoHeightIndexData* data = new RemoteUtxoHeightIndexData(); __STP(data); + + int maxLoop = in->getShort(); + + for(int i = 0; i != maxLoop; ++i){ + UtxoId* v = UtxoId::fromBinary(in); + data->list->addElement(v); + } + + return data; +} + +IBlockObject* RemoteUtxoHeightIndexData::copyData() const noexcept { + return new RemoteUtxoHeightIndexData(*this); +} + +void RemoteUtxoHeightIndexData::join(const RemoteUtxoHeightIndexData *value) noexcept { + int maxLoop = value->list->size(); + for(int i = 0; i != maxLoop; ++i){ + const UtxoId* v = value->list->get(i); + + if(contains(v)){ + continue; + } + + UtxoId* newTrx = dynamic_cast(v->copyData()); + this->list->addElement(newTrx); + } +} + +bool RemoteUtxoHeightIndexData::contains(const UtxoId *utxoId) const noexcept { + int index = indexof(utxoId); + return index >= 0; +} + +void RemoteUtxoHeightIndexData::remove(const UtxoId *utxoId) noexcept { + int index = indexof(utxoId); + + if(index >= 0){ + this->list->remove(index); + } +} + +int RemoteUtxoHeightIndexData::indexof(const UtxoId *utxoId) const noexcept { + int maxLoop = this->list->size(); + for(int i = 0; i != maxLoop; ++i){ + UtxoId* v = this->list->get(i); + if(utxoId->equals(v)){ + return i; + } + } + + return -1; +} + +void RemoteUtxoHeightIndexData::add(const UtxoId *utxoId) noexcept { + this->list->addElement(dynamic_cast(utxoId->copyData())); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.h b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.h new file mode 100644 index 0000000..3b4daed --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexData.h @@ -0,0 +1,50 @@ +/* + * RemoteUtxoHeightIndexData.h + * + * Created on: Jul 8, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEXDATA_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEXDATA_H_ + +#include "base/ArrayList.h" + +#include "filestore_block/IBlockObject.h" + +using namespace alinous; + +namespace codablecash { + +class UtxoId; + +class RemoteUtxoHeightIndexData : public alinous::IBlockObject { +public: + RemoteUtxoHeightIndexData(const RemoteUtxoHeightIndexData& inst); + RemoteUtxoHeightIndexData(); + virtual ~RemoteUtxoHeightIndexData(); + + virtual int binarySize() const; + virtual void toBinary(ByteBuffer* out) const; + static RemoteUtxoHeightIndexData* createFromBinary(ByteBuffer* in); + + virtual IBlockObject* copyData() const noexcept; + + void join(const RemoteUtxoHeightIndexData* value) noexcept; + bool contains(const UtxoId* utxoId) const noexcept; + void remove(const UtxoId* utxoId) noexcept; + int indexof(const UtxoId* utxoId) const noexcept; + + void add(const UtxoId* utxoId) noexcept; + + ArrayList* getList() const noexcept { + return this->list; + } + +private: + ArrayList* list; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEXDATA_H_ */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.cpp new file mode 100644 index 0000000..5678cb0 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.cpp @@ -0,0 +1,59 @@ +/* + * RemoteUtxoHeightIndexDataFactory.cpp + * + * Created on: Jul 8, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.h" +#include "bc_status_cache_data/RemoteUtxoHeightIndexData.h" + +#include "btree/DataNode.h" +#include "btree/BtreeStorage.h" + +#include "base/StackRelease.h" + +#include "filestore_block/IBlockObject.h" + +namespace codablecash { + +RemoteUtxoHeightIndexDataFactory::RemoteUtxoHeightIndexDataFactory() { + +} + +RemoteUtxoHeightIndexDataFactory::~RemoteUtxoHeightIndexDataFactory() { + +} + +IBlockObject* RemoteUtxoHeightIndexDataFactory::makeDataFromBinary(ByteBuffer *in) { + return RemoteUtxoHeightIndexData::createFromBinary(in); +} + +void RemoteUtxoHeightIndexDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { + uint64_t dataFpos = dataNode->getDataFpos(); + if(dataFpos != 0){ + IBlockObject* obj = store->loadData(dataFpos); __STP(obj); + + RemoteUtxoHeightIndexData* baseValue = dynamic_cast(obj); + const RemoteUtxoHeightIndexData* newValue = dynamic_cast(data); + + baseValue->join(newValue); + dataFpos = store->storeData(baseValue, dataFpos); + dataNode->setDataFpos(dataFpos); + + return; + } + + dataFpos = store->storeData(data); + dataNode->setDataFpos(dataFpos); +} + +bool RemoteUtxoHeightIndexDataFactory::beforeRemove(DataNode *dataNode, BtreeStorage *store, const AbstractBtreeKey *key) const { + return true; // remove +} + +AbstractBtreeDataFactory* RemoteUtxoHeightIndexDataFactory::copy() const noexcept { + return new RemoteUtxoHeightIndexDataFactory(); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.h b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.h new file mode 100644 index 0000000..a9b7c29 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndexDataFactory.h @@ -0,0 +1,32 @@ +/* + * RemoteUtxoHeightIndexDataFactory.h + * + * Created on: Jul 8, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEXDATAFACTORY_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEXDATAFACTORY_H_ + +#include "btree/AbstractBtreeDataFactory.h" + +using namespace alinous; + +namespace codablecash { + +class RemoteUtxoHeightIndexDataFactory : public AbstractBtreeDataFactory { +public: + RemoteUtxoHeightIndexDataFactory(); + virtual ~RemoteUtxoHeightIndexDataFactory(); + + virtual IBlockObject* makeDataFromBinary(ByteBuffer* in); + + virtual void registerData(const AbstractBtreeKey* key, const IBlockObject* data, DataNode* dataNode, BtreeStorage* store) const; + virtual bool beforeRemove(DataNode* dataNode, BtreeStorage* store, const AbstractBtreeKey* key) const; + + virtual AbstractBtreeDataFactory* copy() const noexcept; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXOHEIGHTINDEXDATAFACTORY_H_ */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHistory.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoHistory.cpp new file mode 100644 index 0000000..5b37b22 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHistory.cpp @@ -0,0 +1,96 @@ +/* + * RemoteUtxoHistory.cpp + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoHistory.h" +#include "bc_status_cache_data/RemoteUtxoDataFactory.h" + +#include "bc_base_utxo_index/UtxoIdKeyFactory.h" +#include "bc_base_utxo_index/UtxoIdKey.h" + +#include "base/UnicodeString.h" +#include "base/StackRelease.h" + +#include "base_io/File.h" + +#include "btree/Btree.h" +#include "btree/BtreeConfig.h" + +#include "random_access_file/DiskCacheManager.h" + +#include "bc_status_cache_data/RemoteUtxoData.h" + +namespace codablecash { + +RemoteUtxoHistory::RemoteUtxoHistory(const File* baseDir) { + this->baseDir = new File(*baseDir); + + this->cacheManager = new DiskCacheManager(); + this->btree = nullptr; +} + +RemoteUtxoHistory::~RemoteUtxoHistory() { + close(); + + delete baseDir; + delete this->cacheManager; +} + +void RemoteUtxoHistory::initBlank() { + UnicodeString fileName(HISTORY_FILE); + + UtxoIdKeyFactory* keyFactory = new UtxoIdKeyFactory(); __STP(keyFactory); + RemoteUtxoDataFactory* dataFactory = new RemoteUtxoDataFactory(); __STP(dataFactory); + + Btree btree(this->baseDir, &fileName, this->cacheManager, keyFactory, dataFactory); + + BtreeConfig config; + config.nodeNumber = 8; + config.defaultSize = 1024; + config.blockSize = 32; + btree.create(&config); +} + +void RemoteUtxoHistory::open() { + UnicodeString fileName(HISTORY_FILE); + + UtxoIdKeyFactory* keyFactory = new UtxoIdKeyFactory(); __STP(keyFactory); + RemoteUtxoDataFactory* dataFactory = new RemoteUtxoDataFactory(); __STP(dataFactory); + + this->btree = new Btree(this->baseDir, &fileName, this->cacheManager, keyFactory, dataFactory); + BtreeOpenConfig opconf; + opconf.numDataBuffer = 256; + opconf.numNodeBuffer = 512; + this->btree->open(&opconf); +} + +void RemoteUtxoHistory::close() { + if(this->btree != nullptr){ + this->btree->close(); + delete this->btree, this->btree = nullptr; + } +} + +void RemoteUtxoHistory::add(const UtxoId *utxoId, uint64_t height) { + UtxoIdKey key(utxoId); + RemoteUtxoData data(utxoId, height); + + this->btree->putData(&key, &data); +} + +void RemoteUtxoHistory::remove(const UtxoId *utxoId) { + UtxoIdKey key(utxoId); + this->btree->remove(&key); +} + +RemoteUtxoData* RemoteUtxoHistory::getData(const UtxoId *utxoId) { + UtxoIdKey key(utxoId); + + IBlockObject* obj = this->btree->findByKey(&key); + return dynamic_cast(obj); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHistory.h b/src_blockchain/bc_status_cache_data/RemoteUtxoHistory.h new file mode 100644 index 0000000..67dbfc4 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHistory.h @@ -0,0 +1,49 @@ +/* + * RemoteUtxoHistory.h + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXOHISTORY_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXOHISTORY_H_ +#include + +namespace alinous { +class File; +class DiskCacheManager; +class Btree; +} +using namespace alinous; + +namespace codablecash { + +class UtxoId; +class RemoteUtxoData; + +class RemoteUtxoHistory { +public: + static constexpr const wchar_t* HISTORY_FILE = L"utxohistory"; + + explicit RemoteUtxoHistory(const File* baseDir); + virtual ~RemoteUtxoHistory(); + + void initBlank(); + + void open(); + void close(); + + void add(const UtxoId* utxoId, uint64_t height); + void remove(const UtxoId* utxoId); + RemoteUtxoData* getData(const UtxoId* utxoId); + +private: + File* baseDir; + DiskCacheManager* cacheManager; + + Btree* btree; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXOHISTORY_H_ */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoRepository.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoRepository.cpp new file mode 100644 index 0000000..b748d8b --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoRepository.cpp @@ -0,0 +1,115 @@ +/* + * RemoteUtxoRepository.cpp + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_data/RemoteUtxoRepository.h" +#include "bc_status_cache_data/RemoteUtxoHistory.h" +#include "bc_status_cache_data/RemoteUtxoHeightIndex.h" +#include "bc_status_cache_data/RemoteUtxoHeightIndexData.h" +#include "bc_status_cache_data/RemoteUtxoData.h" + +#include "base_io/File.h" + +#include "base/StackRelease.h" + +#include "btree/BtreeReverseScanner.h" + +#include "btreekey/ULongKey.h" + +#include "bc_trx/UtxoId.h" + + +namespace codablecash { + +RemoteUtxoRepository::RemoteUtxoRepository(const File* baseDir) { + this->baseDir = baseDir->get(REMOTE_UTXO_ID); + + this->utxoHistory = new RemoteUtxoHistory(this->baseDir); + this->heightIndex = new RemoteUtxoHeightIndex(this->baseDir); +} + +RemoteUtxoRepository::~RemoteUtxoRepository() { + close(); + + delete this->baseDir; +} + +void RemoteUtxoRepository::initBlank() { + this->utxoHistory->initBlank(); + this->heightIndex->initBlank(); +} + +void RemoteUtxoRepository::open() { + this->utxoHistory->open(); + this->heightIndex->open(); +} + +void RemoteUtxoRepository::close() { + if(this->utxoHistory != nullptr){ + this->utxoHistory->close(); + delete this->utxoHistory, this->utxoHistory = nullptr; + } + if(this->heightIndex != nullptr){ + this->heightIndex->close(); + delete this->heightIndex, this->heightIndex = nullptr; + } +} + +void RemoteUtxoRepository::addUtxo(uint64_t height, const UtxoId *utxoId) noexcept { + this->utxoHistory->add(utxoId, height); + this->heightIndex->addUtxo(height, utxoId); +} + +RemoteUtxoData* RemoteUtxoRepository::getUtxo(const UtxoId *utxoId) { + RemoteUtxoData* data = this->utxoHistory->getData(utxoId); + return data; +} + +void RemoteUtxoRepository::clean(uint64_t cleanHeight) { + ArrayList removeUtxo; + removeUtxo.setDeleteOnExit(); + + BtreeReverseScanner* scanner = this->heightIndex->getReverseScanner(); __STP(scanner); + + ULongKey key(cleanHeight); + scanner->begin(&key); + + uint64_t cleanStartHeight = cleanHeight; + while(scanner->hasPrevious()){ + const IBlockObject* obj = scanner->previous(); + const RemoteUtxoHeightIndexData* data = dynamic_cast(obj); + + ArrayList* utxoList = data->getList(); + int maxLoop = utxoList->size(); + for(int i = 0; i != maxLoop; ++i){ + const UtxoId* utxoId = utxoList->get(i); + removeUtxo.addElement(dynamic_cast(utxoId->copyData())); + } + + const AbstractBtreeKey* key = scanner->previousKey(); + const ULongKey* ukey = dynamic_cast(key); + cleanStartHeight = ukey->getValue(); + } + + // remove height index + { + for(uint64_t i = cleanStartHeight; i <= cleanHeight; i++){ + this->heightIndex->remove(i); + } + } + + // remove utxo + { + int maxLoop = removeUtxo.size(); + for(int i = 0; i != maxLoop; ++i){ + UtxoId* utxoId = removeUtxo.get(i); + + this->utxoHistory->remove(utxoId); + } + } +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoRepository.h b/src_blockchain/bc_status_cache_data/RemoteUtxoRepository.h new file mode 100644 index 0000000..8cea2b7 --- /dev/null +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoRepository.h @@ -0,0 +1,51 @@ +/* + * RemoteUtxoRepository.h + * + * Created on: Jul 7, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_DATA_REMOTEUTXOREPOSITORY_H_ +#define BC_STATUS_CACHE_DATA_REMOTEUTXOREPOSITORY_H_ +#include + +namespace alinous { +class File; +} +using namespace alinous; + +namespace codablecash { + +class UtxoId; + +class RemoteUtxoHeightIndex; +class RemoteUtxoHistory; +class RemoteUtxoData; + +class RemoteUtxoRepository { +public: + static constexpr const wchar_t* REMOTE_UTXO_ID = L"utxoid"; + + explicit RemoteUtxoRepository(const File* baseDir); + virtual ~RemoteUtxoRepository(); + + void initBlank(); + + void open(); + void close(); + + void addUtxo(uint64_t height, const UtxoId* utxoId) noexcept; + RemoteUtxoData* getUtxo(const UtxoId* utxoId); + + void clean(uint64_t cleanHeight); + +private: + File* baseDir; + + RemoteUtxoHistory* utxoHistory; + RemoteUtxoHeightIndex* heightIndex; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_DATA_REMOTEUTXOREPOSITORY_H_ */ diff --git a/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt b/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt new file mode 100644 index 0000000..0a597db --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt @@ -0,0 +1,9 @@ + + +set(__src + GenerateNewGenesisBlockCommandMessage.cpp + NotifyShardExtendRequestCommandMessage.cpp + NotifyZoneExtendRequestedTransaction.cpp +) +handle_sub(codablecashlib "${__src}" blockchain bc_status_cache_extend_shard) + diff --git a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp new file mode 100644 index 0000000..781f0dd --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp @@ -0,0 +1,29 @@ +/* + * GenerateNewGenesisBlockCommandMessage.cpp + * + * Created on: Jul 5, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h" + +namespace codablecash { + +GenerateNewGenesisBlockCommandMessage::GenerateNewGenesisBlockCommandMessage() { + this->newShardZone = 0; +} + +GenerateNewGenesisBlockCommandMessage::~GenerateNewGenesisBlockCommandMessage() { + +} + + +void GenerateNewGenesisBlockCommandMessage::process(CentralProcessor *processor) { + +} + +void GenerateNewGenesisBlockCommandMessage::setNewShardZone(uint16_t newShardZone) noexcept { + this->newShardZone = newShardZone; +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h new file mode 100644 index 0000000..c74aa4e --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h @@ -0,0 +1,32 @@ +/* + * GenerateNewGenesisBlockCommandMessage.h + * + * Created on: Jul 5, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_EXTEND_SHARD_GENERATENEWGENESISBLOCKCOMMANDMESSAGE_H_ +#define BC_STATUS_CACHE_EXTEND_SHARD_GENERATENEWGENESISBLOCKCOMMANDMESSAGE_H_ + +#include "bc_processor/AbstractCentralProcessorCommandMessage.h" +#include + +namespace codablecash { + +class GenerateNewGenesisBlockCommandMessage : public AbstractCentralProcessorCommandMessage { +public: + GenerateNewGenesisBlockCommandMessage(); + virtual ~GenerateNewGenesisBlockCommandMessage(); + + void setNewShardZone(uint16_t newShardZone) noexcept; + +protected: + virtual void process(CentralProcessor* processor); + +private: + uint16_t newShardZone; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_EXTEND_SHARD_GENERATENEWGENESISBLOCKCOMMANDMESSAGE_H_ */ diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp new file mode 100644 index 0000000..4c806f1 --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp @@ -0,0 +1,54 @@ +/* + * NotifyShardExtendRequestCommandMessage.cpp + * + * Created on: Jul 5, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h" + +#include "bc_p2p_processor/P2pRequestProcessor.h" + +#include "bc_p2p/BlochchainP2pManager.h" + +#include "bc_processor/CentralProcessor.h" + +#include "bc_block/BlockHeaderId.h" + +namespace codablecash { + +NotifyShardExtendRequestCommandMessage::NotifyShardExtendRequestCommandMessage() { + this->newShardZone = 0; + this->requestingZone = 0; + this->height = 0; + this->headerId = nullptr; +} + +NotifyShardExtendRequestCommandMessage::~NotifyShardExtendRequestCommandMessage() { + delete this->headerId; +} + +void NotifyShardExtendRequestCommandMessage::process(CentralProcessor *processor) { + P2pRequestProcessor* p2pRequestProcessor = processor->getP2pRequestProcessor(); + BlochchainP2pManager* p2pManager = processor->getBlochchainP2pManager(); + + + +} + +void NotifyShardExtendRequestCommandMessage::setNewShardZone(uint16_t newShardZone) noexcept { + this->newShardZone = newShardZone; +} + +void NotifyShardExtendRequestCommandMessage::setRequestingZone(uint16_t requestingZone) noexcept { + this->requestingZone = requestingZone; +} + +void NotifyShardExtendRequestCommandMessage::setHeaderInfo(uint64_t height, const BlockHeaderId *headerId) noexcept { + this->height = height; + + delete this->headerId; + this->headerId = dynamic_cast(headerId->copyData()); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h new file mode 100644 index 0000000..0d34e53 --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h @@ -0,0 +1,40 @@ +/* + * NotifyShardExtendRequestCommandMessage.h + * + * Created on: Jul 5, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_EXTEND_SHARD_NOTIFYSHARDEXTENDREQUESTCOMMANDMESSAGE_H_ +#define BC_STATUS_CACHE_EXTEND_SHARD_NOTIFYSHARDEXTENDREQUESTCOMMANDMESSAGE_H_ + +#include "bc_processor/AbstractCentralProcessorCommandMessage.h" +#include + +namespace codablecash { + +class BlockHeaderId; + +class NotifyShardExtendRequestCommandMessage : public AbstractCentralProcessorCommandMessage { +public: + NotifyShardExtendRequestCommandMessage(); + virtual ~NotifyShardExtendRequestCommandMessage(); + + void setNewShardZone(uint16_t newShardZone) noexcept; + void setRequestingZone(uint16_t requestingZone) noexcept; + void setHeaderInfo(uint64_t height, const BlockHeaderId* headerId) noexcept; + +protected: + virtual void process(CentralProcessor* processor); + +private: + uint16_t newShardZone; + uint16_t requestingZone; + + uint64_t height; + BlockHeaderId* headerId; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_EXTEND_SHARD_NOTIFYSHARDEXTENDREQUESTCOMMANDMESSAGE_H_ */ diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp new file mode 100644 index 0000000..ed490a1 --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp @@ -0,0 +1,189 @@ +/* + * NotifyZoneExtendRequestedTransaction.cpp + * + * Created on: Jun 29, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" + +#include "bc_base/BalanceUnit.h" +#include "bc_base/BinaryUtils.h" + +#include "bc_trx/TransactionVersion.h" +#include "bc_trx/TransactionId.h" +#include "bc_trx/UtxoId.h" + +#include "base_timestamp/SystemTimestamp.h" + +#include "base/StackRelease.h" + +#include "crypto/Sha256.h" + +#include "bc_block/BlockHeaderId.h" + + +namespace codablecash { + +NotifyZoneExtendRequestedTransaction::NotifyZoneExtendRequestedTransaction(const NotifyZoneExtendRequestedTransaction &inst) + : AbstractInterChainCommunicationTansaction(inst) { + this->zone = inst.zone; + this->height = inst.height; + this->headerId = inst.headerId != nullptr ? dynamic_cast(inst.headerId->copyData()) : nullptr; + + this->utxoId = inst.utxoId != nullptr ? dynamic_cast(inst.utxoId->copyData()) : nullptr; +} + +NotifyZoneExtendRequestedTransaction::NotifyZoneExtendRequestedTransaction() : AbstractInterChainCommunicationTansaction() { + this->zone = 0; + this->height = 0; + this->headerId = nullptr; + + this->utxoId = nullptr; +} + +NotifyZoneExtendRequestedTransaction::~NotifyZoneExtendRequestedTransaction() { + delete this->headerId; +} + +uint8_t NotifyZoneExtendRequestedTransaction::getType() const noexcept { + return TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED; +} + +int NotifyZoneExtendRequestedTransaction::binarySize() const { + BinaryUtils::checkNotNull(this->version); + BinaryUtils::checkNotNull(this->timestamp); + BinaryUtils::checkNotNull(this->headerId); + + int total = sizeof(uint8_t); + total += this->version->binarySize(); + total += this->timestamp->binarySize(); + + total += sizeof(uint16_t); // zone + total += sizeof(uint64_t); // height + total += this->headerId->binarySize(); + + return total; +} + +void NotifyZoneExtendRequestedTransaction::toBinary(ByteBuffer *out) const { + BinaryUtils::checkNotNull(this->version); + BinaryUtils::checkNotNull(this->timestamp); + BinaryUtils::checkNotNull(this->headerId); + + out->put(getType()); + + this->version->toBinary(out); + this->timestamp->toBinary(out); + + out->putShort(this->zone); + out->putLong(this->height); + this->headerId->toBinary(out); +} + +void NotifyZoneExtendRequestedTransaction::fromBinary(ByteBuffer *in) { + delete this->version; + this->version = TransactionVersion::createFromBinary(in); + + delete this->timestamp; + this->timestamp = SystemTimestamp::fromBinary(in); + + this->zone = in->getShort(); + this->height = in->getLong(); + this->headerId = BlockHeaderId::fromBinary(in); +} + +void NotifyZoneExtendRequestedTransaction::build() { + BinaryUtils::checkNotNull(this->version); + BinaryUtils::checkNotNull(this->timestamp); + BinaryUtils::checkNotNull(this->headerId); + + { + int capacity = sizeof(uint8_t) + this->version->binarySize() + this->timestamp->binarySize(); + capacity += sizeof(uint16_t) + sizeof(uint64_t) + this->headerId->binarySize(); + + ByteBuffer* buff = ByteBuffer::allocateWithEndian(capacity, true); __STP(buff); + buff->put(getType()); + + this->version->toBinary(buff); + this->timestamp->toBinary(buff); + + buff->putShort(this->zone); + buff->putLong(this->height); + this->headerId->toBinary(buff); + + buff->position(0); + ByteBuffer* sha = Sha256::sha256(buff, true); __STP(sha); + + delete this->trxId; + this->trxId = new TransactionId((const char*)sha->array(), sha->limit()); + } + + { + int capacity = sizeof(uint16_t) + sizeof(uint64_t) + this->headerId->binarySize(); + + ByteBuffer* buff = ByteBuffer::allocateWithEndian(capacity, true); __STP(buff); + buff->putShort(this->zone); + buff->putLong(this->height); + this->headerId->toBinary(buff); + + buff->position(0); + + ByteBuffer* sha = Sha256::sha256(buff, true); __STP(sha); + + delete this->utxoId; + this->utxoId = new UtxoId((const char*)sha->array(), sha->limit()); + } +} + +IBlockObject* NotifyZoneExtendRequestedTransaction::copyData() const noexcept { + return new NotifyZoneExtendRequestedTransaction(*this); +} + +BalanceUnit NotifyZoneExtendRequestedTransaction::getFee() const noexcept { + return BalanceUnit(0); +} + +BalanceUnit NotifyZoneExtendRequestedTransaction::getFeeRate() const noexcept { + return BalanceUnit(0); +} + +int NotifyZoneExtendRequestedTransaction::getUtxoSize() const noexcept { + return 0; +} + +AbstractUtxo* NotifyZoneExtendRequestedTransaction::getUtxo(int i) const noexcept { + return nullptr; +} + +int NotifyZoneExtendRequestedTransaction::getUtxoReferenceSize() const noexcept { + return 0; +} + +AbstractUtxoReference* NotifyZoneExtendRequestedTransaction::getUtxoReference(int i) const noexcept { + return nullptr; +} + +bool NotifyZoneExtendRequestedTransaction::validateOnAccept(MemPoolTransaction *memTrx, IStatusCacheContext *context) const { + // FIXME[multishard] validate; + + return true; +} + +TrxValidationResult NotifyZoneExtendRequestedTransaction::validateFinal(const BlockHeader *header, MemPoolTransaction *memTrx, IStatusCacheContext *context) const { + return TrxValidationResult::OK; +} + +bool NotifyZoneExtendRequestedTransaction::checkFilter(const ArrayList *filtersList) const { + return false; +} + +void NotifyZoneExtendRequestedTransaction::setHeaderInfo(uint16_t zone, uint64_t height, const BlockHeaderId *headerId) { + this->zone = zone; + this->height = height; + + delete this->headerId; + this->headerId = dynamic_cast(headerId->copyData()); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h new file mode 100644 index 0000000..c97b26c --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h @@ -0,0 +1,61 @@ +/* + * NotifyZoneExtendRequestedTransaction.h + * + * Created on: Jun 29, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_EXTEND_SHARD_NOTIFYZONEEXTENDREQUESTEDTRANSACTION_H_ +#define BC_STATUS_CACHE_EXTEND_SHARD_NOTIFYZONEEXTENDREQUESTEDTRANSACTION_H_ + +#include "bc_trx/AbstractInterChainCommunicationTansaction.h" + +namespace codablecash { + +class BlockHeaderId; +class UtxoId; + +class NotifyZoneExtendRequestedTransaction : public AbstractInterChainCommunicationTansaction { +public: + NotifyZoneExtendRequestedTransaction(const NotifyZoneExtendRequestedTransaction& inst); + NotifyZoneExtendRequestedTransaction(); + virtual ~NotifyZoneExtendRequestedTransaction(); + + virtual uint8_t getType() const noexcept; + + virtual int binarySize() const; + virtual void toBinary(ByteBuffer* out) const; + virtual void fromBinary(ByteBuffer* in); + + virtual void build(); + + virtual IBlockObject* copyData() const noexcept; + + virtual BalanceUnit getFee() const noexcept; + virtual BalanceUnit getFeeRate() const noexcept; + + virtual int getUtxoSize() const noexcept; + virtual AbstractUtxo* getUtxo(int i) const noexcept; + virtual int getUtxoReferenceSize() const noexcept; + virtual AbstractUtxoReference* getUtxoReference(int i) const noexcept; + + virtual bool validateOnAccept(MemPoolTransaction *memTrx, IStatusCacheContext* context) const; + virtual TrxValidationResult validateFinal(const BlockHeader* header, MemPoolTransaction *memTrx, IStatusCacheContext* context) const; + + virtual bool checkFilter(const ArrayList *filtersList) const; + + void setHeaderInfo(uint16_t zone, uint64_t height, const BlockHeaderId* headerId); + +private: + // header info + uint16_t zone; + uint64_t height; + BlockHeaderId* headerId; + + // build automatically + UtxoId* utxoId; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_EXTEND_SHARD_NOTIFYZONEEXTENDREQUESTEDTRANSACTION_H_ */ diff --git a/src_blockchain/bc_status_cache_vote/HeightVoteDataFactory.cpp b/src_blockchain/bc_status_cache_vote/HeightVoteDataFactory.cpp index f6aca22..f587563 100644 --- a/src_blockchain/bc_status_cache_vote/HeightVoteDataFactory.cpp +++ b/src_blockchain/bc_status_cache_vote/HeightVoteDataFactory.cpp @@ -27,7 +27,12 @@ IBlockObject* HeightVoteDataFactory::makeDataFromBinary(ByteBuffer *in) { } void HeightVoteDataFactory::registerData(const AbstractBtreeKey *key, const IBlockObject *data, DataNode *dataNode, BtreeStorage *store) const { - uint64_t dataFpos = store->storeData(data); + uint64_t dataFpos = dataNode->getDataFpos(); + if(dataFpos != 0){ + store->removeData(dataFpos); + } + + dataFpos = store->storeData(data); dataNode->setDataFpos(dataFpos); } diff --git a/src_blockchain/bc_trx/AbstractBlockchainTransaction.h b/src_blockchain/bc_trx/AbstractBlockchainTransaction.h index 52bb974..cbbbfbd 100644 --- a/src_blockchain/bc_trx/AbstractBlockchainTransaction.h +++ b/src_blockchain/bc_trx/AbstractBlockchainTransaction.h @@ -63,6 +63,7 @@ class AbstractBlockchainTransaction : public alinous::IBlockObject { static const constexpr uint8_t TRX_TYPE_SMARTCONTRACT_NOP{10}; static const constexpr uint8_t TRX_TYPE_ICC_NOP{20}; + static const constexpr uint8_t TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED{21}; AbstractBlockchainTransaction(const AbstractBlockchainTransaction& inst); diff --git a/src_blockchain/bc_trx/AbstractUtxoReference.h b/src_blockchain/bc_trx/AbstractUtxoReference.h index 827e767..8c07c50 100644 --- a/src_blockchain/bc_trx/AbstractUtxoReference.h +++ b/src_blockchain/bc_trx/AbstractUtxoReference.h @@ -39,6 +39,10 @@ class AbstractUtxoReference : public alinous::IBlockObject { virtual uint8_t getType() const noexcept = 0; virtual void fromBinary(ByteBuffer* in) = 0; + virtual bool isRemote() const noexcept { + return false; + } + virtual bool checkFilter(const ArrayList *filtersList) const; const UtxoId* getUtxoId() const noexcept { diff --git a/src_blockchain/bc_trx/NopInterChainCommunicationTransaction.cpp b/src_blockchain/bc_trx/NopInterChainCommunicationTransaction.cpp index ec27a8b..bd014ce 100644 --- a/src_blockchain/bc_trx/NopInterChainCommunicationTransaction.cpp +++ b/src_blockchain/bc_trx/NopInterChainCommunicationTransaction.cpp @@ -57,7 +57,7 @@ int NopInterChainCommunicationTransaction::binarySize() const { BinaryUtils::checkNotNull(this->timestamp); int total = sizeof(uint8_t); - + total += this->version->binarySize(); total += this->timestamp->binarySize(); total += sizeof(this->nonce); @@ -78,6 +78,7 @@ void NopInterChainCommunicationTransaction::toBinary(ByteBuffer *out) const { out->put(getType()); + this->version->toBinary(out); this->timestamp->toBinary(out); out->putLong(this->nonce); @@ -92,6 +93,9 @@ void NopInterChainCommunicationTransaction::toBinary(ByteBuffer *out) const { } void NopInterChainCommunicationTransaction::fromBinary(ByteBuffer *in) { + delete this->version; + this->version = TransactionVersion::createFromBinary(in); + delete this->timestamp; this->timestamp = SystemTimestamp::fromBinary(in); this->nonce = in->getLong(); @@ -110,11 +114,12 @@ void NopInterChainCommunicationTransaction::build() { setUtxoNonce(); - int capacity = sizeof(uint8_t) + this->timestamp->binarySize() + sizeof(this->nonce); + int capacity = sizeof(uint8_t) + this->version->binarySize() + this->timestamp->binarySize() + sizeof(this->nonce); ByteBuffer* buff = ByteBuffer::allocateWithEndian(capacity, true); __STP(buff); buff->put(getType()); + this->version->toBinary(buff); this->timestamp->toBinary(buff); buff->putLong(this->nonce); diff --git a/src_blockchain/pow/PoWNonce.cpp b/src_blockchain/pow/PoWNonce.cpp index f0a83c3..28587c3 100644 --- a/src_blockchain/pow/PoWNonce.cpp +++ b/src_blockchain/pow/PoWNonce.cpp @@ -51,14 +51,14 @@ PoWNonce::~PoWNonce() { } PoWNonce* PoWNonce::createRandomNonce() noexcept { const BigInteger* max = getMaxBigInt(); - BigInteger nonce = BigInteger::ramdom(BigInteger(0L), BigInteger(*max)); + BigInteger nonce = BigInteger::random(BigInteger(0L), BigInteger(*max)); return new PoWNonce(&nonce); } PoWNonce* PoWNonce::createRandomNonce(const BigInteger *solt) noexcept { const BigInteger* max = getMaxBigInt(); - BigInteger nonce = BigInteger::ramdom(BigInteger(0L), BigInteger(*max)); + BigInteger nonce = BigInteger::random(BigInteger(0L), BigInteger(*max)); nonce = nonce.multiply(*solt).mod(*max); diff --git a/src_blockchain/pow_pool/PoWRequestStatus.cpp b/src_blockchain/pow_pool/PoWRequestStatus.cpp index 5eac024..818dacb 100644 --- a/src_blockchain/pow_pool/PoWRequestStatus.cpp +++ b/src_blockchain/pow_pool/PoWRequestStatus.cpp @@ -72,7 +72,7 @@ PoWRequest2Client* PoWRequestStatus::getClientPoWRequest() { req->setRequestData(this->data); const BigInteger* max = PoWNonce::getMaxBigInt(); - BigInteger nonceSolt = BigInteger::ramdom(BigInteger(0L), BigInteger(*max)); + BigInteger nonceSolt = BigInteger::random(BigInteger(0L), BigInteger(*max)); BigInteger s(this->solt++); nonceSolt.addSelf(s); diff --git a/src_db/numeric/BigInteger.cpp b/src_db/numeric/BigInteger.cpp index 21fd5e3..32c7b2e 100644 --- a/src_db/numeric/BigInteger.cpp +++ b/src_db/numeric/BigInteger.cpp @@ -23,6 +23,9 @@ #include "cassert" #include "../../src_ext/mod_sqrt/bn_sqrt.h" +#include "crypto/Sha256.h" + +using codablecash::Sha256; namespace alinous { const BigInteger BigInteger::ZERO((int64_t)0); @@ -395,7 +398,7 @@ BigInteger* BigInteger::fromBinary(const char* buff, int length) { return big; } -BigInteger BigInteger::ramdom() noexcept { +BigInteger BigInteger::random() noexcept { static uint64_t solt = 1; mpz_t s; @@ -404,10 +407,36 @@ BigInteger BigInteger::ramdom() noexcept { gmp_randstate_t state; gmp_randinit_default(state); - int shift = solt % 3; - uint64_t tm = (Os::getMicroSec() << shift) + solt++; - gmp_randseed_ui(state, tm); + { + mpz_t seed; + mpz_init(seed); + + for(int i = 0; i != 10; ++i){ + uint64_t cspr = Os::getOsCspring(); + + mpz_mul_2exp(seed, seed, 32); + mpz_add_ui(seed, seed, cspr); + } + + // hash + { + size_t count; + uint8_t* data = (uint8_t*)mpz_export(NULL, &count, 1, 1, 1, 0, seed); + + ByteBuffer* buff = ByteBuffer::wrapWithEndian(data, count, true); __STP(buff); + ::free(data); + + ByteBuffer* shabuff = Sha256::sha256(buff, true); __STP(shabuff); + + mpz_import(seed, shabuff->capacity(), 1, 1, 1, 0, shabuff->array()); // big endian + } + + + gmp_randseed(state, seed); + + mpz_clear(seed); + } mpz_urandomb(s, state, 256); @@ -419,11 +448,11 @@ BigInteger BigInteger::ramdom() noexcept { return *big; } -BigInteger BigInteger::ramdom(const BigInteger &min, const BigInteger &max) noexcept { +BigInteger BigInteger::random(const BigInteger &min, const BigInteger &max) noexcept { BigInteger val(0L); do{ - val = ramdom(); + val = random(); val = val.mod(max); } while(!between(val, min, max)); diff --git a/src_db/numeric/BigInteger.h b/src_db/numeric/BigInteger.h index 5932a14..930bafd 100644 --- a/src_db/numeric/BigInteger.h +++ b/src_db/numeric/BigInteger.h @@ -85,8 +85,8 @@ class BigInteger { ByteBuffer* toBinary() const; static BigInteger* fromBinary(const char* buff, int length); - static BigInteger ramdom() noexcept; - static BigInteger ramdom(const BigInteger& min, const BigInteger& max) noexcept; + static BigInteger random() noexcept; + static BigInteger random(const BigInteger& min, const BigInteger& max) noexcept; static ByteBuffer* padBuffer(ByteBuffer* bin, int size); diff --git a/src_db/osenv/funcs.cpp b/src_db/osenv/funcs.cpp index 9b3068e..b54cd7c 100644 --- a/src_db/osenv/funcs.cpp +++ b/src_db/osenv/funcs.cpp @@ -23,6 +23,8 @@ #include #endif +#include + #include "base/UnicodeString.h" #include "base/StackRelease.h" @@ -78,6 +80,21 @@ uint64_t Os::getMicroSec() noexcept { return microsec; } +uint64_t Os::getNanoSec() noexcept { + struct timespec startTime; + clock_gettime(CLOCK_REALTIME, &startTime); + + uint64_t nano = startTime.tv_nsec; + return nano; +} + +uint32_t Os::getOsCspring() noexcept { + std::random_device rd; + unsigned int secure_rand = rd(); + + return secure_rand; +} + SystemTimestamp Os::now() noexcept { struct timespec tp; diff --git a/src_db/osenv/funcs.h b/src_db/osenv/funcs.h index 392c601..85da629 100644 --- a/src_db/osenv/funcs.h +++ b/src_db/osenv/funcs.h @@ -18,8 +18,6 @@ namespace alinous { - - #ifdef _WIN64 #define PATH_SEPARATOR L"\\" #elseif _WIN32 @@ -64,9 +62,12 @@ class Os { static void usleep(uint32_t microsec) noexcept; static uint64_t getMicroSec() noexcept; + static uint64_t getNanoSec() noexcept; static uint64_t getTimestampLong() noexcept; static SystemTimestamp now() noexcept; + static uint32_t getOsCspring() noexcept; + /************************************************************************** * File functions */ diff --git a/src_test/blockchain/multi_shard/CMakeLists.txt b/src_test/blockchain/multi_shard/CMakeLists.txt index b8349a5..0db3803 100644 --- a/src_test/blockchain/multi_shard/CMakeLists.txt +++ b/src_test/blockchain/multi_shard/CMakeLists.txt @@ -1,6 +1,7 @@ set(testsrc NewShardAdder.cpp + NewShardValidator.cpp test_multi_shard.cpp test_netwallet_stake.cpp ) diff --git a/src_test/blockchain/multi_shard/NewShardAdder.cpp b/src_test/blockchain/multi_shard/NewShardAdder.cpp index 7b552e7..ae6b3df 100644 --- a/src_test/blockchain/multi_shard/NewShardAdder.cpp +++ b/src_test/blockchain/multi_shard/NewShardAdder.cpp @@ -16,16 +16,26 @@ namespace codablecash { NewShardAdder::NewShardAdder(const NewShardAdder &inst) { this->height = inst.height; + + this->newShardZone = inst.newShardZone; + this->requestingZone = inst.requestingZone; } NewShardAdder::NewShardAdder() { this->height = 0; + this->newShardZone = 0; + this->requestingZone = 0; } NewShardAdder::~NewShardAdder() { } +void NewShardAdder::init(uint16_t newShardZone, uint16_t requestingZone) { + this->newShardZone = newShardZone; + this->requestingZone = requestingZone; +} + void NewShardAdder::onBlockGenerated(Block *block, MemPoolTransaction *memTrx, BlockchainController *ctrl) { uint16_t height = block->getHeight(); if(height == this->height){ @@ -37,7 +47,12 @@ void NewShardAdder::__addCommand(Block *block, MemPoolTransaction *memTrx, Block BlockHeader* header = block->getHeader(); NewShardZoneCommand cmd; - cmd.setNewShardNo(1); + cmd.setNewShardZone(this->newShardZone); + cmd.setRequestingZone(this->requestingZone); + + Block gblock(this->newShardZone, 1); + gblock.build(); + cmd.setGenesisblock(&gblock); header->addHeaderCommand(&cmd); } diff --git a/src_test/blockchain/multi_shard/NewShardAdder.h b/src_test/blockchain/multi_shard/NewShardAdder.h index 6e76f01..451adb4 100644 --- a/src_test/blockchain/multi_shard/NewShardAdder.h +++ b/src_test/blockchain/multi_shard/NewShardAdder.h @@ -19,6 +19,8 @@ class NewShardAdder: public IBlockGenerationListner { NewShardAdder(); virtual ~NewShardAdder(); + void init(uint16_t newShardZone, uint16_t requestingZone); + virtual void onBlockGenerated(Block* block, MemPoolTransaction *memTrx, BlockchainController* ctrl); virtual IBlockGenerationListner* copy() const noexcept; @@ -29,6 +31,10 @@ class NewShardAdder: public IBlockGenerationListner { private: uint64_t height; + + uint16_t newShardZone; + uint16_t requestingZone; + }; } /* namespace codablecash */ diff --git a/src_test/blockchain/multi_shard/NewShardValidator.cpp b/src_test/blockchain/multi_shard/NewShardValidator.cpp new file mode 100644 index 0000000..ff0534e --- /dev/null +++ b/src_test/blockchain/multi_shard/NewShardValidator.cpp @@ -0,0 +1,38 @@ +/* + * NewShardValidator.cpp + * + * Created on: Jun 30, 2026 + * Author: iizuka + */ + +#include "NewShardValidator.h" + +#include + +#include "bc_status_cache_context/IStatusCacheContext.h" + +namespace codablecash { + +NewShardValidator::NewShardValidator(const NewShardValidator &inst) : AbstractShardExtentionValidator(inst) { +} + +NewShardValidator::NewShardValidator() : AbstractShardExtentionValidator() { + +} + +NewShardValidator::~NewShardValidator() { + +} + +bool NewShardValidator::validate(NewShardZoneCommand *newShardCommand, IStatusCacheContext *context, BlockchainController *ctrl) { + uint16_t numZones = context->getNumZones(); + int requestedZones = context->getRequestedNewShards(); + + return requestedZones == 0; +} + +AbstractShardExtentionValidator* NewShardValidator::copy() const { + return new NewShardValidator(*this); +} + +} /* namespace codablecash */ diff --git a/src_test/blockchain/multi_shard/NewShardValidator.h b/src_test/blockchain/multi_shard/NewShardValidator.h new file mode 100644 index 0000000..db1b855 --- /dev/null +++ b/src_test/blockchain/multi_shard/NewShardValidator.h @@ -0,0 +1,28 @@ +/* + * NewShardValidator.h + * + * Created on: Jun 30, 2026 + * Author: iizuka + */ + +#ifndef BLOCKCHAIN_MULTI_SHARD_NEWSHARDVALIDATOR_H_ +#define BLOCKCHAIN_MULTI_SHARD_NEWSHARDVALIDATOR_H_ + +#include "bc_status_cache/AbstractShardExtentionValidator.h" + +namespace codablecash { + +class NewShardValidator: public AbstractShardExtentionValidator { +public: + NewShardValidator(const NewShardValidator& inst); + NewShardValidator(); + virtual ~NewShardValidator(); + + virtual bool validate(NewShardZoneCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl); + + virtual AbstractShardExtentionValidator* copy() const; +}; + +} /* namespace codablecash */ + +#endif /* BLOCKCHAIN_MULTI_SHARD_NEWSHARDVALIDATOR_H_ */ diff --git a/src_test/blockchain/multi_shard/test_multi_shard.cpp b/src_test/blockchain/multi_shard/test_multi_shard.cpp index 13420a5..346bbf7 100644 --- a/src_test/blockchain/multi_shard/test_multi_shard.cpp +++ b/src_test/blockchain/multi_shard/test_multi_shard.cpp @@ -53,6 +53,7 @@ #include "blockchain/utils/DebugCodablecashSystemParamSetup.h" #include "blockchain/wallet_util/WalletDriver.h" #include "NewShardAdder.h" +#include "NewShardValidator.h" using namespace codablecash; @@ -134,9 +135,14 @@ TEST(TestMultiShardGroup, case01){ { TestnetInstanceWrapper* inst = testnet.getInstance(0, L"first"); NewShardAdder adder; + adder.init(1, 0); adder.setHeight(6); inst->addIBlockGenerationListner(&adder); + NewShardValidator shardValidator; + inst->setShardExtendValidator(&shardValidator); + + int port01 = inst->getListeningPort(); const NodeIdentifierSource* source = inst->getVoterIdentifierSource(); @@ -186,7 +192,7 @@ TEST(TestMultiShardGroup, case01){ // staking ticket testnet.suspendMining(0); - int maxLoop = 40; + int maxLoop = 10; for(int i = 0; i != maxLoop; ++i){ BalanceUnit fee(1L); BalanceUnit stakeAmount(100L); @@ -246,7 +252,7 @@ TEST(TestMultiShardGroup, case01){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // second - node01.initBlank(1); // zone 0 + node01.initBlank(1, 1); // zone 1 // after init node01.setNodeName(L"node02"); diff --git a/src_test/blockchain/pow/test_pow_random_hash.cpp b/src_test/blockchain/pow/test_pow_random_hash.cpp index 329eb6a..4ed9e93 100644 --- a/src_test/blockchain/pow/test_pow_random_hash.cpp +++ b/src_test/blockchain/pow/test_pow_random_hash.cpp @@ -23,8 +23,8 @@ TEST_GROUP(TestPoWRandomHashGroup) { }; TEST(TestPoWRandomHashGroup, case01){ - BigInteger blockHash = BigInteger::ramdom(BigInteger(0L), BigInteger(Secp256k1Point::p)); - BigInteger nonce = BigInteger::ramdom(BigInteger(0L), BigInteger(Secp256k1Point::p)); + BigInteger blockHash = BigInteger::random(BigInteger(0L), BigInteger(Secp256k1Point::p)); + BigInteger nonce = BigInteger::random(BigInteger(0L), BigInteger(Secp256k1Point::p)); PowRandomHashManager manager; ByteBuffer* buff = manager.calculate(&blockHash, &nonce); __STP(buff); @@ -32,13 +32,13 @@ TEST(TestPoWRandomHashGroup, case01){ } TEST(TestPoWRandomHashGroup, shaker01){ - BigInteger nonce = BigInteger::ramdom(BigInteger(0L), BigInteger(Secp256k1Point::p)); + BigInteger nonce = BigInteger::random(BigInteger(0L), BigInteger(Secp256k1Point::p)); RandomShaker shaker(&nonce, 4); } TEST(TestPoWRandomHashGroup, TotalNumberSplitter01){ - BigInteger nonce = BigInteger::ramdom(BigInteger(0L), BigInteger(Secp256k1Point::p)); + BigInteger nonce = BigInteger::random(BigInteger(0L), BigInteger(Secp256k1Point::p)); int num = 4; TotalNumberSplitter splitter(&nonce, num, 200); diff --git a/src_test/blockchain/testnet/test_first_net.cpp b/src_test/blockchain/testnet/test_first_net.cpp index 5a028ba..9551f2b 100644 --- a/src_test/blockchain/testnet/test_first_net.cpp +++ b/src_test/blockchain/testnet/test_first_net.cpp @@ -113,7 +113,7 @@ TEST(TestFirstNetGroup, case01){ { // First node01.setNodeName(L"node01"); - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); @@ -142,7 +142,7 @@ TEST(TestFirstNetGroup, case01){ CodablecashNetworkNode node02(dirNode02, config02, &logger); { // second - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 //UnicodeString idstr = nodeId01.getPublicKey()->toString(16); @@ -199,7 +199,7 @@ TEST(TestFirstNetGroup, case01){ CodablecashNetworkNode node03(dirNode03, config03, &logger); { // third - node03.initBlank(0); // zone 0 + node03.initBlank(0, 1); // zone 0 // after init node03.setNodeName(L"node03"); diff --git a/src_test/blockchain/testnet/test_pending_request_processor.cpp b/src_test/blockchain/testnet/test_pending_request_processor.cpp index 43ab1c9..dcab8bd 100644 --- a/src_test/blockchain/testnet/test_pending_request_processor.cpp +++ b/src_test/blockchain/testnet/test_pending_request_processor.cpp @@ -121,7 +121,7 @@ TEST(TestPendingRequestProcessorGroup, case01){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); @@ -174,7 +174,7 @@ TEST(TestPendingRequestProcessorGroup, case01){ CodablecashNetworkNode node02(dirNode02, config02, &logger); // second - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, true); @@ -260,7 +260,7 @@ TEST(TestPendingRequestProcessorGroup, case01_02){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); @@ -313,7 +313,7 @@ TEST(TestPendingRequestProcessorGroup, case01_02){ CodablecashNetworkNode node02(dirNode02, config02, &logger); // second - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, true); @@ -382,7 +382,7 @@ TEST(TestPendingRequestProcessorGroup, case02_err){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, true); @@ -434,7 +434,7 @@ TEST(TestPendingRequestProcessorGroup, queue01){ CodablecashNetworkNodeConfig* config01 = new CodablecashNetworkNodeConfig(nwconfig); __STP(config01); CodablecashNetworkNode node01(dirNode01, config01, &logger); - node01.initBlank(0); + node01.initBlank(0, 1); node01.startNetwork(nullptr, false); CodablecashNodeInstance* inst = node01.getInstance(); diff --git a/src_test/blockchain/testnet/test_sync_block.cpp b/src_test/blockchain/testnet/test_sync_block.cpp index 8fa6304..d7aa8f6 100644 --- a/src_test/blockchain/testnet/test_sync_block.cpp +++ b/src_test/blockchain/testnet/test_sync_block.cpp @@ -124,7 +124,7 @@ TEST(TestSyncBlockGroup, case01){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); @@ -264,7 +264,7 @@ TEST(TestSyncBlockGroup, case01){ CodablecashNetworkNode node02(dirNode02, config02, &logger); { // second - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, true); diff --git a/src_test/blockchain/testnet/test_sync_header_only.cpp b/src_test/blockchain/testnet/test_sync_header_only.cpp index 605fc58..8ed0920 100644 --- a/src_test/blockchain/testnet/test_sync_header_only.cpp +++ b/src_test/blockchain/testnet/test_sync_header_only.cpp @@ -123,7 +123,7 @@ TEST(TestSyncHeaderOnlyGroup, case01){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // second - node01.initBlank(1); // zone 0 + node01.initBlank(1, 1); // zone 0 // after init node01.setNodeName(L"node02"); diff --git a/src_test/blockchain/testnet/test_sync_mempool.cpp b/src_test/blockchain/testnet/test_sync_mempool.cpp index 57e9444..2946ae6 100644 --- a/src_test/blockchain/testnet/test_sync_mempool.cpp +++ b/src_test/blockchain/testnet/test_sync_mempool.cpp @@ -104,7 +104,7 @@ TEST(TestSyncMempoolGroup, case01) { CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); @@ -192,7 +192,7 @@ TEST(TestSyncMempoolGroup, case01) { CodablecashNetworkNode node02(dirNode02, config02, &logger); { // second - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, true); diff --git a/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.cpp b/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.cpp index 454ffeb..b81dc05 100644 --- a/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.cpp +++ b/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.cpp @@ -92,12 +92,12 @@ void TestnetInstanceWrapper::setFinalizerConfig(const FinalizerConfig *fconfig) void TestnetInstanceWrapper::initGenesis() { this->node->setNetworkConfig(this->nwconfig); - this->node->generateNetwork(this->zone); + this->node->generateNetwork(this->zone, 1); } void TestnetInstanceWrapper::initBlank() { this->node->setNetworkConfig(this->nwconfig); - this->node->initBlank(this->zone); + this->node->initBlank(this->zone, 1); } void TestnetInstanceWrapper::start(IDebugSeeder* seeder, bool pending) { @@ -221,4 +221,9 @@ void TestnetInstanceWrapper::addIBlockGenerationListner(const IBlockGenerationLi generator->addBlockGenerationListner(listner); } +void TestnetInstanceWrapper::setShardExtendValidator(const AbstractShardExtentionValidator *validator) { + CodablecashNodeInstance* instance = this->node->getInstance(); + instance->setShardExtendValidator(validator); +} + } /* namespace codablecash */ diff --git a/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.h b/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.h index 48d70ec..abb0c11 100644 --- a/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.h +++ b/src_test/blockchain/utils_testnet/TestnetInstanceWrapper.h @@ -27,6 +27,7 @@ class IDebugSeeder; class NodeIdentifierSource; class TransactionId; class IBlockGenerationListner; +class AbstractShardExtentionValidator; class TestnetInstanceWrapper { @@ -67,6 +68,7 @@ class TestnetInstanceWrapper { int getMempoolTransctionCount() const; void addIBlockGenerationListner(const IBlockGenerationListner* listner); + void setShardExtendValidator(const AbstractShardExtentionValidator* validator); private: uint16_t zone; diff --git a/src_test/crypto/test_secp256.cpp b/src_test/crypto/test_secp256.cpp index 5c6252e..48c945d 100644 --- a/src_test/crypto/test_secp256.cpp +++ b/src_test/crypto/test_secp256.cpp @@ -133,7 +133,7 @@ TEST(TestSecp256Group, binary){ } TEST(TestSecp256Group, mul01){ - BigInteger r = BigInteger::ramdom().mod(Secp256k1Point::p); + BigInteger r = BigInteger::random().mod(Secp256k1Point::p); Secp256k1Point pt; Secp256k1Point rG = pt.multiple(r); diff --git a/src_test/crypto/test_sha256.cpp b/src_test/crypto/test_sha256.cpp index d1a7bc2..0c8e40d 100644 --- a/src_test/crypto/test_sha256.cpp +++ b/src_test/crypto/test_sha256.cpp @@ -23,8 +23,8 @@ TEST_GROUP(Sha256TestGroup) { }; TEST(Sha256TestGroup, case01){ - BigInteger blockHash = BigInteger::ramdom(BigInteger(0L), BigInteger(Secp256k1Point::p)); - BigInteger nonce = BigInteger::ramdom(BigInteger(0L), BigInteger(Secp256k1Point::p)); + BigInteger blockHash = BigInteger::random(BigInteger(0L), BigInteger(Secp256k1Point::p)); + BigInteger nonce = BigInteger::random(BigInteger(0L), BigInteger(Secp256k1Point::p)); ByteBuffer* bin = blockHash.toBinary(); __STP(bin); ByteBuffer* bin2 = nonce.toBinary(); __STP(bin2); diff --git a/src_test/db/db_numeric/CMakeLists.txt b/src_test/db/db_numeric/CMakeLists.txt index 6ae8c89..548395a 100644 --- a/src_test/db/db_numeric/CMakeLists.txt +++ b/src_test/db/db_numeric/CMakeLists.txt @@ -3,6 +3,7 @@ set(testsrc test_big_decimal.cpp test_big_integer_misc.cpp test_big_integer.cpp + test_big_integer_rand.cpp test_div.cpp test_mul.cpp test_Tonelli_Shanks.cpp diff --git a/src_test/db/db_numeric/test_big_integer_misc.cpp b/src_test/db/db_numeric/test_big_integer_misc.cpp index c38ca69..2e33a51 100644 --- a/src_test/db/db_numeric/test_big_integer_misc.cpp +++ b/src_test/db/db_numeric/test_big_integer_misc.cpp @@ -20,7 +20,7 @@ TEST_GROUP(TestBigIntegerMiscGroup) { }; TEST(TestBigIntegerMiscGroup, rnd02){ - BigInteger inst = BigInteger::ramdom(BigInteger(1000L), BigInteger(100000000000L)); + BigInteger inst = BigInteger::random(BigInteger(1000L), BigInteger(100000000000L)); UnicodeString str = inst.toString("%Zd"); BigInteger mask(0xFF); @@ -30,7 +30,7 @@ TEST(TestBigIntegerMiscGroup, rnd02){ } TEST(TestBigIntegerMiscGroup, rnd01){ - BigInteger inst = BigInteger::ramdom(); + BigInteger inst = BigInteger::random(); } TEST(TestBigIntegerMiscGroup, pad01){ diff --git a/src_test/db/db_numeric/test_big_integer_rand.cpp b/src_test/db/db_numeric/test_big_integer_rand.cpp new file mode 100644 index 0000000..4ff320b --- /dev/null +++ b/src_test/db/db_numeric/test_big_integer_rand.cpp @@ -0,0 +1,69 @@ +/* + * test_big_integer_rand.cpp + * + * Created on: Jul 15, 2026 + * Author: iizuka + */ + +#include "base_io/ByteBuffer.h" +#include "test_utils/t_macros.h" + +#include "numeric/BigInteger.h" +#include "base/UnicodeString.h" +#include "base/StackRelease.h" + +using namespace alinous; + +TEST_GROUP(TestBigIntegerRandGroup) { + TEST_SETUP() {} + TEST_TEARDOWN() {} +}; + +TEST(TestBigIntegerRandGroup, nano){ + uint64_t nano = Os::getNanoSec(); + uint64_t nano2 = Os::getNanoSec(); +} + + +TEST(TestBigIntegerRandGroup, nanocat){ + mpz_t s; + mpz_init(s); + + uint64_t nano = Os::getNanoSec(); + uint32_t n32 = (uint32_t)nano; + + mpz_add_ui(s, s, n32); + + nano = Os::getNanoSec(); + mpz_mul_2exp(s, s, 32); + mpz_add_ui(s, s, n32); + + mpz_clear(s); +} + +TEST(TestBigIntegerRandGroup, nanocat2) { + mpz_t s; + mpz_init(s); + + int maxLoop = 256 / 32; + for(int i = 0; i != maxLoop; ++i){ + uint64_t nano = Os::getNanoSec(); + uint32_t n32 = (uint32_t)nano; + + mpz_mul_2exp(s, s, 32); + mpz_add_ui(s, s, n32); + } + + mpz_clear(s); +} + +TEST(TestBigIntegerRandGroup, CSPRNG) { + uint32_t rand = Os::getOsCspring(); + uint32_t rand2 = Os::getOsCspring(); +} + +TEST(TestBigIntegerRandGroup, rand01) { + BigInteger bi01 = BigInteger::random(); + BigInteger bi02 = BigInteger::random(); +} + diff --git a/src_test/p2p/instance/test_p2p_network_cmd.cpp b/src_test/p2p/instance/test_p2p_network_cmd.cpp index 6718a09..b98f82f 100644 --- a/src_test/p2p/instance/test_p2p_network_cmd.cpp +++ b/src_test/p2p/instance/test_p2p_network_cmd.cpp @@ -93,7 +93,7 @@ TEST(TestP2pNetworkGroup, case01){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); } @@ -102,7 +102,7 @@ TEST(TestP2pNetworkGroup, case01){ CodablecashNetworkNode node02(dirNode02, &nwconfig, &logger); { // First - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, false); @@ -174,7 +174,7 @@ TEST(TestP2pNetworkGroup, case02_err){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); } @@ -183,7 +183,7 @@ TEST(TestP2pNetworkGroup, case02_err){ CodablecashNetworkNode node02(dirNode02, &nwconfig, &logger); { // First - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, false); @@ -273,7 +273,7 @@ TEST(TestP2pNetworkGroup, case03_err){ CodablecashNetworkNode node01(dirNode01, config01, &logger); { // First - node01.generateNetwork(0); // zone 0 + node01.generateNetwork(0, 1); // zone 0 // after init node01.startNetwork(&seeder, false); @@ -283,7 +283,7 @@ TEST(TestP2pNetworkGroup, case03_err){ CodablecashNetworkNode node02(dirNode02, &nwconfig, &logger); { // First - node02.initBlank(0); // zone 0 + node02.initBlank(0, 1); // zone 0 // after init node02.startNetwork(&seeder, false); diff --git a/src_test/smartcontract_modular/transaction/test_random_address.cpp b/src_test/smartcontract_modular/transaction/test_random_address.cpp index 237eb53..fcc92ad 100644 --- a/src_test/smartcontract_modular/transaction/test_random_address.cpp +++ b/src_test/smartcontract_modular/transaction/test_random_address.cpp @@ -54,8 +54,8 @@ TEST(TestRandomAddressGroup, case01){ TEST(TestRandomAddressGroup, case02){ - BigInteger seed = BigInteger::ramdom(); - BigInteger seed2 = BigInteger::ramdom(); + BigInteger seed = BigInteger::random(); + BigInteger seed2 = BigInteger::random(); bool bl = seed.equals(&seed2); CHECK(!bl); From 9cd263a33d2754f3064aabadd80b0b6801d053cb Mon Sep 17 00:00:00 2001 From: iizuka Date: Thu, 16 Jul 2026 21:35:03 +0800 Subject: [PATCH 2/7] generate genesis block --- .../NewShardZoneCommand.cpp | 1 + .../bc_blockstore/CodablecashBlockchain.cpp | 3 +++ .../GenerateNewGenesisBlockCommandMessage.cpp | 18 ++++++++++++++++-- .../GenerateNewGenesisBlockCommandMessage.h | 4 ++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp index 653319b..d1915c7 100644 --- a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp +++ b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp @@ -99,6 +99,7 @@ void NewShardZoneCommand::onFinalize(const BlockHeader *header, BlockchainStatus GenerateNewGenesisBlockCommandMessage* message = new GenerateNewGenesisBlockCommandMessage(); message->setNewShardZone(this->newShardZone); + message->setGenesisBlock(this->genesisBlock); processor->addCommandMessage(message); } diff --git a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp index 390ae91..6745ac3 100644 --- a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp +++ b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp @@ -149,6 +149,9 @@ void CodablecashBlockchain::addZone(uint16_t zone) { saveCondig(); initZone(zone); + + ZoneStore* store = this->zonesStore->get(zone); + store->open(); } } diff --git a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp index 781f0dd..a155570 100644 --- a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp @@ -7,23 +7,37 @@ #include "bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h" +#include "bc_processor/CentralProcessor.h" + +#include "bc_status_cache/BlockchainController.h" + +#include "bc_block/Block.h" + + namespace codablecash { GenerateNewGenesisBlockCommandMessage::GenerateNewGenesisBlockCommandMessage() { this->newShardZone = 0; + this->genesisBlock = nullptr; } GenerateNewGenesisBlockCommandMessage::~GenerateNewGenesisBlockCommandMessage() { - + delete this->genesisBlock; } void GenerateNewGenesisBlockCommandMessage::process(CentralProcessor *processor) { - + BlockchainController* ctrl = processor->getCtrl(); + ctrl->addBlock(this->genesisBlock); } void GenerateNewGenesisBlockCommandMessage::setNewShardZone(uint16_t newShardZone) noexcept { this->newShardZone = newShardZone; } +void GenerateNewGenesisBlockCommandMessage::setGenesisBlock(const Block *block) { + delete this->genesisBlock; + this->genesisBlock = new Block(*block); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h index c74aa4e..2aa21d0 100644 --- a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h @@ -13,18 +13,22 @@ namespace codablecash { +class Block; + class GenerateNewGenesisBlockCommandMessage : public AbstractCentralProcessorCommandMessage { public: GenerateNewGenesisBlockCommandMessage(); virtual ~GenerateNewGenesisBlockCommandMessage(); void setNewShardZone(uint16_t newShardZone) noexcept; + void setGenesisBlock(const Block* block); protected: virtual void process(CentralProcessor* processor); private: uint16_t newShardZone; + Block* genesisBlock; }; } /* namespace codablecash */ From 060158c32afdab1529d6b23b64d0e2e619d07019 Mon Sep 17 00:00:00 2001 From: iizuka Date: Sun, 26 Jul 2026 09:33:13 +0800 Subject: [PATCH 3/7] Cmakefile --- CMakeLists.txt | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4d9c08..c9d7f23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.5) project(codablecash) @@ -7,11 +7,11 @@ add_subdirectory(src_ext) if (CMAKE_BUILD_TYPE STREQUAL "Debug") message(STATUS "Debug Build") - set(CMAKE_CXX_FLAGS "-std=c++0x -D__cplusplus=201103L -D__DEBUG__ -D__USE_LARGEFILE64 -fmessage-length=0 -g -O0 -ftest-coverage -fprofile-arcs") - #set(CMAKE_CXX_FLAGS "-std=c++0x -D__cplusplus=201103L -D__DEBUG__ -fmessage-length=0 -O3 -march=native -MMD -MP -MF") + set(CMAKE_CXX_FLAGS "-g -O0 -std=c++11 -D__cplusplus=201103L -D__DEBUG__ -D__USE_LARGEFILE64 -fmessage-length=0 -coverage") + #set(CMAKE_CXX_FLAGS "-std=c++0x -D__cplusplus=201103L -D__DEBUG__ -fmessage-length=0 -O3 -march=native -MMD -MP -MF -ftest-coverage") - #set(CMAKE_C_FLAGS "-g -O0 -coverage -fprofile-arcs -ftest-coverage") - #set(CMAKE_EXE_LINKER_FLAGS="-fprofile-arcs -ftest-coverage") + #set(CMAKE_C_FLAGS "-g -O0 -coverage -coverage") + #set(CMAKE_EXE_LINKER_FLAGS="-ftest-coverage") set(CMAKE_C_FLAGS "-g -O0 -coverage") set(CMAKE_EXE_LINKER_FLAGS=" -coverage") @@ -23,7 +23,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") elseif(CMAKE_BUILD_TYPE STREQUAL "ReleaseTest") set(CMAKE_CXX_FLAGS "-std=c++0x -D__cplusplus=201103L -D__DEBUG__ -D__USE_LARGEFILE64 -fmessage-length=0 -g -march=native -MMD -MP -MF") else() - set(CMAKE_CXX_FLAGS "-std=c++0x -D__cplusplus=201103L -DNDEBUG -D__USE_LARGEFILE64 -Wall -fmessage-length=0 -O3 -march=native -MMD -MP -MF") + set(CMAKE_CXX_FLAGS "-std=c++0x -D__cplusplus=201103L -DNDEBUG -D__USE_LARGEFILE64 -Wall -fmessage-length=0 -O2 -march=native -MMD -MP -MF") endif() @@ -136,6 +136,7 @@ include_directories(${PROJECT_SOURCE_DIR}/src_gen/) include_directories(${PROJECT_SOURCE_DIR}/src_ext/) include_directories(${PROJECT_SOURCE_DIR}/src_smartcontract/) include_directories(${PROJECT_SOURCE_DIR}/src_smartcontract_db/) +include_directories(${PROJECT_SOURCE_DIR}/src_smartcontract_modular/) include_directories(${PROJECT_SOURCE_DIR}/src_smartcontract_vm/) include_directories(${PROJECT_SOURCE_DIR}/src_test/) @@ -146,10 +147,11 @@ add_subdirectory(src_db) add_subdirectory(src_gen) add_subdirectory(src_smartcontract) add_subdirectory(src_smartcontract_db) +add_subdirectory(src_smartcontract_modular) add_subdirectory(src_smartcontract_vm) -add_library(codablecashlib STATIC ${top_src} ${top_src_blockchain} ${top_src_blockchain_p2p} ${top_src_db} ${top_src_gen} ${top_src_smartcontract} ${top_src_smartcontract_db} ${top_src_smartcontract_vm}) +add_library(codablecashlib STATIC ${top_src} ${top_src_blockchain} ${top_src_blockchain_p2p} ${top_src_db} ${top_src_gen} ${top_src_smartcontract} ${top_src_smartcontract_db} ${top_src_smartcontract_modular} ${top_src_smartcontract_vm}) target_link_libraries(codablecashlib extlib pthread gmp) add_executable(codablecash ${srcmain}) @@ -191,7 +193,7 @@ add_custom_target(cppcheck add_custom_target(valgrind COMMAND valgrind --leak-check=full - --xml=yes --xml-file=testall.%p.valgrind.xml --main-stacksize=83886080 + --xml=yes --xml-file=testall.%p.valgrind.xml --main-stacksize=83886080 --max-threads=1000 --fair-sched=yes ./testall WORKING_DIRECTORY ${CMAKE_BINARY_DIR} @@ -199,14 +201,14 @@ add_custom_target(valgrind add_custom_target(valgrindp COMMAND valgrind --leak-check=full - --xml=yes --xml-file=testall.%p.valgrind.xml --main-stacksize=83886080 - ./testall -g TestVoteAndFinalizeTicketGroup + --xml=yes --xml-file=testall.%p.valgrind.xml --main-stacksize=83886080 --max-threads=1000 --fair-sched=yes + ./testall -g TestMultiShardGroup WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) add_custom_target(helgrindp - COMMAND valgrind --tool=helgrind --xml=yes --xml-file=testall.%p.helgrind.xml ./testall -g TestPoWManagerGroup + COMMAND valgrind --tool=helgrind --xml=yes --xml-file=testall.%p.helgrind.xml ./testall -g TestSyncHeaderOnlyGroup WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) From 97e5c060bf21839d4117e9c670238dc3698f49b0 Mon Sep 17 00:00:00 2001 From: iizuka Date: Thu, 30 Jul 2026 18:39:04 +0800 Subject: [PATCH 4/7] multi shard chain --- src_blockchain/bc_base/AbstractAddress.cpp | 2 +- src_blockchain/bc_block/Block.cpp | 5 +- src_blockchain/bc_block/Block.h | 4 + src_blockchain/bc_block/BlockHeader.cpp | 18 ++ src_blockchain/bc_block/BlockHeader.h | 2 + .../bc_block_generator/BlockGenerator.cpp | 9 +- .../AbstractBlockHeaderCommand.cpp | 21 +- .../AbstractBlockHeaderCommand.h | 7 + .../BlockHeaderCommandId.cpp | 49 +++++ .../BlockHeaderCommandId.h | 29 +++ .../bc_block_header_command/CMakeLists.txt | 1 + .../NewShardZoneCommand.cpp | 16 +- .../RecognizedNewShardCommand.cpp | 51 ++++- .../RecognizedNewShardCommand.h | 7 + .../bc_blockstore/CodablecashBlockchain.cpp | 3 + .../bc_blockstore/CodablecashBlockchain.h | 3 + .../DetectVotingTicketCommandMessage.cpp | 2 +- .../AbstractShardExtentionValidator.h | 4 +- .../bc_status_cache/BlockchainController.cpp | 8 + .../bc_status_cache/BlockchainController.h | 2 + .../bc_status_cache/BlockchainStatusCache.cpp | 7 + .../bc_status_cache/BlockchainStatusCache.h | 2 + .../bc_status_cache/ZoneStatusCache.cpp | 10 + .../bc_status_cache/ZoneStatusCache.h | 8 + .../StatusCacheContext.cpp | 8 +- .../CMakeLists.txt | 1 + .../GenerateNewGenesisBlockCommandMessage.cpp | 5 + ...NotifyShardExtendRequestCommandMessage.cpp | 73 ++++++- .../NotifyShardExtendRequestCommandMessage.h | 3 + .../NotifyZoneExtendRequestedTransaction.cpp | 98 ++++++++- .../NotifyZoneExtendRequestedTransaction.h | 13 ++ ...ndNotifyZoneExtendRequestedNodeCommand.cpp | 196 ++++++++++++++++++ ...SendNotifyZoneExtendRequestedNodeCommand.h | 49 +++++ .../bc_trx/AbstractBlockchainTransaction.cpp | 4 + .../bc_p2p/BlochchainP2pManager.cpp | 19 +- .../bc_p2p/BlochchainP2pManager.h | 2 + .../AbstractConsensusNodeCommand.cpp | 13 ++ .../AbstractConsensusNodeCommand.h | 2 + .../ReportNonceCalculatedNodeCommand.cpp | 12 +- .../SendVoteTransactionNodeCommand.cpp | 13 +- .../pubsub_cmd/AbstractPubSubCommand.cpp | 4 + .../pubsub_cmd/AbstractPubSubCommand.h | 1 + .../blockchain/multi_shard/NewShardAdder.cpp | 2 + .../multi_shard/NewShardValidator.cpp | 2 +- .../multi_shard/NewShardValidator.h | 2 +- .../multi_shard/test_multi_shard.cpp | 12 +- 46 files changed, 754 insertions(+), 50 deletions(-) create mode 100644 src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp create mode 100644 src_blockchain/bc_block_header_command/BlockHeaderCommandId.h create mode 100644 src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.cpp create mode 100644 src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h diff --git a/src_blockchain/bc_base/AbstractAddress.cpp b/src_blockchain/bc_base/AbstractAddress.cpp index a530ca5..9c0087c 100644 --- a/src_blockchain/bc_base/AbstractAddress.cpp +++ b/src_blockchain/bc_base/AbstractAddress.cpp @@ -58,7 +58,7 @@ AddressDescriptor* AbstractAddress::toAddressDescriptor() const noexcept { char zonech[4]; Mem::memset(zonech, 0, 4); - ::sprintf(zonech, "%03d", this->zone); + ::sprintf(zonech, "%03d", (uint8_t)this->zone); ByteBuffer* body = getBodyPart(); __STP(body); body->position(0); diff --git a/src_blockchain/bc_block/Block.cpp b/src_blockchain/bc_block/Block.cpp index 3ac5409..69385ee 100644 --- a/src_blockchain/bc_block/Block.cpp +++ b/src_blockchain/bc_block/Block.cpp @@ -62,7 +62,6 @@ void Block::addControlTransaction(const AbstractControlTransaction *trx) noexcep case AbstractBlockchainTransaction::TRX_TYPE_VOTE_BLOCK: addVote(dynamic_cast(trx)); break; - // FIXME[multishard]add header command to register new zone default: this->body->addControlTransaction(trx); break; @@ -170,4 +169,8 @@ bool Block::isScheduledBlock() const noexcept { return this->header->isScheduledBlock(); } +void Block::addHeaderCommand(const AbstractBlockHeaderCommand *cmd) { + this->header->addHeaderCommand(cmd); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_block/Block.h b/src_blockchain/bc_block/Block.h index 3cf22a7..57aa8f1 100644 --- a/src_blockchain/bc_block/Block.h +++ b/src_blockchain/bc_block/Block.h @@ -29,6 +29,8 @@ class BlockHeaderId; class PoWNonce; class VoteBlockTransaction; class ReservedVotes; +class AbstractBlockHeaderCommand; + class Block { public: @@ -70,6 +72,8 @@ class Block { bool isScheduledBlock() const noexcept; + void addHeaderCommand(const AbstractBlockHeaderCommand *cmd); + #ifdef __DEBUG__ bool checkMerkleRoot() const noexcept; #endif diff --git a/src_blockchain/bc_block/BlockHeader.cpp b/src_blockchain/bc_block/BlockHeader.cpp index 492a2d5..935bbd4 100644 --- a/src_blockchain/bc_block/BlockHeader.cpp +++ b/src_blockchain/bc_block/BlockHeader.cpp @@ -30,6 +30,7 @@ #include "bc_base/BinaryUtils.h" +#include "bc_block_header_command/BlockHeaderCommandId.h" namespace codablecash { @@ -303,4 +304,21 @@ bool BlockHeader::hasHeaderCommnads() const noexcept { return !this->commnads->isEmpty(); } +const AbstractBlockHeaderCommand* BlockHeader::getHeaderCommand(const BlockHeaderCommandId *commandId) const noexcept { + const AbstractBlockHeaderCommand* ret = nullptr; + + int maxLoop = this->commnads->size(); + for(int i = 0; i != maxLoop; ++i){ + AbstractBlockHeaderCommand* command = this->commnads->get(i); + const BlockHeaderCommandId* id = command->getCommandId(); + + if(commandId->equals(id)){ + ret = command; + break; + } + } + + return ret; +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_block/BlockHeader.h b/src_blockchain/bc_block/BlockHeader.h index 9c1f282..d115aee 100644 --- a/src_blockchain/bc_block/BlockHeader.h +++ b/src_blockchain/bc_block/BlockHeader.h @@ -32,6 +32,7 @@ class IVoteTransactionIdCertificatevisitor; class TransactionId; class BlockVersion; class AbstractBlockHeaderCommand; +class BlockHeaderCommandId; class BlockHeader : public alinous::IBlockObject { public: @@ -112,6 +113,7 @@ class BlockHeader : public alinous::IBlockObject { void addHeaderCommand(const AbstractBlockHeaderCommand* cmd); bool hasHeaderCommnads() const noexcept; + const AbstractBlockHeaderCommand* getHeaderCommand(const BlockHeaderCommandId* commandId) const noexcept; ArrayList* getHeaderCommands() const noexcept { return this->commnads; } diff --git a/src_blockchain/bc_block_generator/BlockGenerator.cpp b/src_blockchain/bc_block_generator/BlockGenerator.cpp index bfb7d5f..256b379 100644 --- a/src_blockchain/bc_block_generator/BlockGenerator.cpp +++ b/src_blockchain/bc_block_generator/BlockGenerator.cpp @@ -71,7 +71,9 @@ #include "bc_block_generator/IBlockGenerationListner.h" +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" +#include "bc_block_header_command/RecognizedNewShardCommand.h" namespace codablecash { BlockGenerator::BlockGenerator(uint16_t zone, CodablecashSystemParam* config, MemoryPool* memoryPool, BlockchainController* ctrl @@ -252,7 +254,12 @@ void BlockGenerator::importInterChainCommunicationTransactions2Block(MemPoolTran uint8_t tyxType = trx->getType(); if(tyxType == AbstractInterChainCommunicationTansaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ - // FIXME[multishard] add RecognizedNewShardCommand to the header + // [multishard] add RecognizedNewShardCommand to the header + NotifyZoneExtendRequestedTransaction* notifyTrx = dynamic_cast(trx); + + RecognizedNewShardCommand command; + command.setTransaction(notifyTrx); + block->addHeaderCommand(&command); } } } diff --git a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp index 59f0638..3a1c247 100644 --- a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp +++ b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.cpp @@ -8,24 +8,29 @@ #include "bc_block_header_command/AbstractBlockHeaderCommand.h" #include "bc_block_header_command/NewShardZoneCommand.h" #include "bc_block_header_command/RecognizedNewShardCommand.h" +#include "bc_block_header_command/BlockHeaderCommandId.h" #include "base_io/ByteBuffer.h" #include "base/StackRelease.h" +#include "crypto/Sha256.h" + namespace codablecash { AbstractBlockHeaderCommand::AbstractBlockHeaderCommand(const AbstractBlockHeaderCommand &inst) { this->type = inst.type; + this->commandId = inst.commandId != nullptr ? dynamic_cast(inst.commandId->copyData()) : nullptr; } AbstractBlockHeaderCommand::AbstractBlockHeaderCommand(uint16_t type) { this->type = type; + this->commandId = nullptr; } AbstractBlockHeaderCommand::~AbstractBlockHeaderCommand() { - + delete this->commandId; } AbstractBlockHeaderCommand* AbstractBlockHeaderCommand::createFromBinary(ByteBuffer* in) { @@ -46,8 +51,22 @@ AbstractBlockHeaderCommand* AbstractBlockHeaderCommand::createFromBinary(ByteBuf __STP(ret); ret->fromBinary(in); + ret->buildCommandId(); return __STP_MV(ret); } +void AbstractBlockHeaderCommand::buildCommandId() { + int cap = binarySize(); + + ByteBuffer* buff = ByteBuffer::allocateWithEndian(cap, true); __STP(buff); + toBinary(buff); + + buff->position(0); + ByteBuffer* sha = Sha256::sha256(buff, true); __STP(sha); + + delete this->commandId; + this->commandId = new BlockHeaderCommandId((const char*)sha->array(), sha->limit()); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h index f2817ed..a68d13b 100644 --- a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h +++ b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h @@ -23,6 +23,7 @@ class BlockchainStatusCache; class CodablecashBlockchain; class CodablecashSystemParam; class ILockinManager; +class BlockHeaderCommandId; class AbstractBlockHeaderCommand : public alinous::IBlockObject { public: @@ -39,9 +40,15 @@ class AbstractBlockHeaderCommand : public alinous::IBlockObject { virtual void onFinalize(const BlockHeader *header, BlockchainStatusCache* statusCache, CodablecashBlockchain* blockchain, ILockinManager *lockinManager, const CodablecashSystemParam* config) = 0; + void buildCommandId(); + + const BlockHeaderCommandId* getCommandId() const noexcept { + return this->commandId; + } protected: uint16_t type; + BlockHeaderCommandId* commandId; }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp new file mode 100644 index 0000000..aed19a9 --- /dev/null +++ b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp @@ -0,0 +1,49 @@ +/* + * BlockHeaderCommand.cpp + * + * Created on: Jul 23, 2026 + * Author: iizuka + */ + +#include "base/StackRelease.h" +#include "bc_block_header_command/BlockHeaderCommandId.h" + + +namespace codablecash { + +BlockHeaderCommandId::BlockHeaderCommandId(const BlockHeaderCommandId &inst) : Abstract32BytesId((const char*)inst.id->array(), inst.id->limit()) { +} + +BlockHeaderCommandId::BlockHeaderCommandId() : Abstract32BytesId() { + +} + +BlockHeaderCommandId::BlockHeaderCommandId(const char *binary, int length) : Abstract32BytesId(binary, length) { + +} + +BlockHeaderCommandId::~BlockHeaderCommandId() { + +} + +BlockHeaderCommandId* BlockHeaderCommandId::fromBinary(ByteBuffer *in) { + BlockHeaderCommandId* commandId = new BlockHeaderCommandId(); + + int cap = in->getInt(); + + uint8_t* buff = new uint8_t[cap]; + StackArrayRelease __st_buff(buff); + + in->get(buff, cap); + + commandId->id = ByteBuffer::wrapWithEndian(buff, cap, true); + commandId->id->position(0); + + return commandId; +} + +IBlockObject* BlockHeaderCommandId::copyData() const noexcept { + return new BlockHeaderCommandId(*this); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h new file mode 100644 index 0000000..1f9a8cb --- /dev/null +++ b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h @@ -0,0 +1,29 @@ +/* + * BlockHeaderCommand.h + * + * Created on: Jul 23, 2026 + * Author: iizuka + */ + +#ifndef BC_BLOCK_HEADER_COMMAND_BLOCKHEADERCOMMANDID_H_ +#define BC_BLOCK_HEADER_COMMAND_BLOCKHEADERCOMMANDID_H_ + +#include "bc_base/Abstract32BytesId.h" + +namespace codablecash { + +class BlockHeaderCommandId : public Abstract32BytesId { +public: + BlockHeaderCommandId(const BlockHeaderCommandId& inst); + BlockHeaderCommandId(); + BlockHeaderCommandId(const char* binary, int length); + virtual ~BlockHeaderCommandId(); + + static BlockHeaderCommandId* fromBinary(ByteBuffer* in); + + virtual IBlockObject* copyData() const noexcept; +}; + +} /* namespace codablecash */ + +#endif /* BC_BLOCK_HEADER_COMMAND_BLOCKHEADERCOMMANDID_H_ */ diff --git a/src_blockchain/bc_block_header_command/CMakeLists.txt b/src_blockchain/bc_block_header_command/CMakeLists.txt index 14e9d7a..2d8c1f7 100644 --- a/src_blockchain/bc_block_header_command/CMakeLists.txt +++ b/src_blockchain/bc_block_header_command/CMakeLists.txt @@ -2,6 +2,7 @@ set(__src AbstractBlockHeaderCommand.cpp + BlockHeaderCommandId.cpp NewShardZoneCommand.cpp RecognizedNewShardCommand.cpp ) diff --git a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp index d1915c7..83f8f36 100644 --- a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp +++ b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp @@ -22,6 +22,10 @@ #include "bc_block/Block.h" #include "bc_base/BinaryUtils.h" + +#include "bc_p2p/BlochchainP2pManager.h" + + namespace codablecash { @@ -86,14 +90,22 @@ void NewShardZoneCommand::onFinalize(const BlockHeader *header, BlockchainStatus ILockinManager *lockinManager, const CodablecashSystemParam *config) { uint16_t zoneSelf = statusCache->getZoneSelf(); - // FIXME[multishard] create phisical store + // [multishard] create phisical store if(zoneSelf != this->newShardZone){ + // other chains statusCache->newZone(false); blockchain->addZone(this->newShardZone); + + BlochchainP2pManager* p2pManager = blockchain->getBlochchainP2pManager(); + p2pManager->incNumZones(); }else{ + // new shard chain statusCache->newZone(true); blockchain->addZone(this->newShardZone); + BlochchainP2pManager* p2pManager = blockchain->getBlochchainP2pManager(); + p2pManager->incNumZones(); + //[multishard] generate genesis block CentralProcessor* processor = blockchain->getProcessor(); @@ -117,6 +129,8 @@ void NewShardZoneCommand::onFinalize(const BlockHeader *header, BlockchainStatus const BlockHeaderId* headerId = header->getId(); message->setHeaderInfo(height, headerId); + message->setCommandId(this->commandId); + processor->addCommandMessage(message); } } diff --git a/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp index f4cab71..e82836e 100644 --- a/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp +++ b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.cpp @@ -7,38 +7,81 @@ #include "bc_block_header_command/RecognizedNewShardCommand.h" +#include "bc_block/BlockHeader.h" + +#include "bc_processor/CentralProcessor.h" + +#include "bc_blockstore/CodablecashBlockchain.h" + +#include "bc_status_cache/BlockchainController.h" +#include "bc_status_cache/BlockchainStatusCache.h" + +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" + +#include "bc_base/BinaryUtils.h" + +#include "base/StackRelease.h" + + + namespace codablecash { RecognizedNewShardCommand::RecognizedNewShardCommand(const RecognizedNewShardCommand &inst) : AbstractBlockHeaderCommand(inst) { + this->trx = inst.trx != nullptr ? dynamic_cast(inst.trx->copyData()) : nullptr; + } RecognizedNewShardCommand::RecognizedNewShardCommand() : AbstractBlockHeaderCommand(AbstractBlockHeaderCommand::RECOGNIZED_SHARD_COMMAND) { - // TODO Auto-generated constructor stub - + this->trx = nullptr; } RecognizedNewShardCommand::~RecognizedNewShardCommand() { - + delete this->trx; } int RecognizedNewShardCommand::binarySize() const { + BinaryUtils::checkNotNull(this->trx); + int total = sizeof(uint16_t); + total += this->trx->binarySize(); + return total; } void RecognizedNewShardCommand::toBinary(ByteBuffer *out) const { + BinaryUtils::checkNotNull(this->trx); + + out->putShort(this->type); + + this->trx->toBinary(out); } void RecognizedNewShardCommand::fromBinary(ByteBuffer *in) { + AbstractBlockchainTransaction* atrx = AbstractBlockchainTransaction::createFromBinary(in); __STP(atrx); + NotifyZoneExtendRequestedTransaction* ntrx = dynamic_cast(atrx); + + BinaryUtils::checkNotNull(ntrx); + + this->trx = ntrx; __STP_MV(atrx); } IBlockObject* RecognizedNewShardCommand::copyData() const noexcept { return new RecognizedNewShardCommand(*this); } -void RecognizedNewShardCommand::onFinalize(const BlockHeader *header,BlockchainStatusCache *statusCache, CodablecashBlockchain *blockchain, +void RecognizedNewShardCommand::onFinalize(const BlockHeader *header, BlockchainStatusCache *statusCache, CodablecashBlockchain *blockchain, ILockinManager *lockinManager, const CodablecashSystemParam *config) { + uint16_t targetZone = header->getZone(); + uint16_t numRecognized = this->trx->getNewShardZone() + 1; + + statusCache->setNumRecognizedZones(targetZone, numRecognized); + +} + +void RecognizedNewShardCommand::setTransaction(const NotifyZoneExtendRequestedTransaction *trx) { + delete this->trx; + this->trx = dynamic_cast(trx->copyData()); } } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h index e57e30a..abd1e53 100644 --- a/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h +++ b/src_blockchain/bc_block_header_command/RecognizedNewShardCommand.h @@ -12,6 +12,8 @@ namespace codablecash { +class NotifyZoneExtendRequestedTransaction; + class RecognizedNewShardCommand : public AbstractBlockHeaderCommand { public: RecognizedNewShardCommand(const RecognizedNewShardCommand& inst); @@ -26,6 +28,11 @@ class RecognizedNewShardCommand : public AbstractBlockHeaderCommand { virtual void onFinalize(const BlockHeader *header, BlockchainStatusCache* statusCache, CodablecashBlockchain* blockchain, ILockinManager *lockinManager, const CodablecashSystemParam* config); + void setTransaction(const NotifyZoneExtendRequestedTransaction* trx); + +private: + NotifyZoneExtendRequestedTransaction* trx; + }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp index 6745ac3..8e5e5fc 100644 --- a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp +++ b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp @@ -338,5 +338,8 @@ ArrayList* CodablecashBlockchain::getBlockHeadersHeightAt(uint16_t return __STP_MV(headers); } +BlochchainP2pManager* CodablecashBlockchain::getBlochchainP2pManager() const noexcept { + return this->processor->getBlochchainP2pManager(); +} } /* namespace codablecash */ diff --git a/src_blockchain/bc_blockstore/CodablecashBlockchain.h b/src_blockchain/bc_blockstore/CodablecashBlockchain.h index 110fe61..e4adadf 100644 --- a/src_blockchain/bc_blockstore/CodablecashBlockchain.h +++ b/src_blockchain/bc_blockstore/CodablecashBlockchain.h @@ -36,6 +36,7 @@ class BlockBodyStoreManager; class CentralProcessor; class BlockHeaderId; class MemPoolTransaction; +class BlochchainP2pManager; class CodablecashBlockchain : public IBlockchainStoreProvider { public: @@ -94,6 +95,8 @@ class CodablecashBlockchain : public IBlockchainStoreProvider { return this->version; } + BlochchainP2pManager* getBlochchainP2pManager() const noexcept; + private: void fireBlockAdded(MemPoolTransaction* memTrx, const Block* block); void fireBlockHeaderAdded(MemPoolTransaction* memTrx, const BlockHeader* header); diff --git a/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp b/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp index fd61cdd..1312e4f 100644 --- a/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp +++ b/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp @@ -142,7 +142,7 @@ void DetectVotingTicketCommandMessage::putTransaction(const BlockHeader *header2 // [Network] broad cast { - P2pRequestProcessor* p2pRequestProcessor = pool->getP2pRequestProcessor(); // mnetwork key + P2pRequestProcessor* p2pRequestProcessor = pool->getP2pRequestProcessor(); // network key BlochchainP2pManager* p2pManager = pool->getBlochchainP2pManager(); if(p2pRequestProcessor != nullptr && p2pManager != nullptr){ diff --git a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h index 0b62af0..e2d69c7 100644 --- a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h +++ b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h @@ -13,7 +13,7 @@ namespace codablecash { class BlockchainStatusCache; class IStatusCacheContext; class BlockchainController; -class NewShardZoneCommand; +class AbstractBlockHeaderCommand; class AbstractShardExtentionValidator { @@ -26,7 +26,7 @@ class AbstractShardExtentionValidator { void setStatusCache(BlockchainStatusCache* stcache) noexcept; - virtual bool validate(NewShardZoneCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl) = 0; + virtual bool validate(AbstractBlockHeaderCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl) = 0; private: BlockchainStatusCache* statusCache; diff --git a/src_blockchain/bc_status_cache/BlockchainController.cpp b/src_blockchain/bc_status_cache/BlockchainController.cpp index 8218a2d..ba8308e 100644 --- a/src_blockchain/bc_status_cache/BlockchainController.cpp +++ b/src_blockchain/bc_status_cache/BlockchainController.cpp @@ -816,7 +816,15 @@ SystemTimestamp* BlockchainController::getPosVoteLimit(uint16_t zone, uint64_t l } void BlockchainController::requestPosVote(uint16_t zone, uint64_t calculatedNonceHeight) { + StackWriteLock __lock(this->rwLock, __FILE__, __LINE__); + this->statusCache->requestPosVote(zone, calculatedNonceHeight, this->blockchain); } +void BlockchainController::setNumRecognizedZones(uint16_t zone,uint16_t numRecognizedZones) { + StackWriteLock __lock(this->rwLock, __FILE__, __LINE__); + + this->statusCache->setNumRecognizedZones(zone, numRecognizedZones); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/BlockchainController.h b/src_blockchain/bc_status_cache/BlockchainController.h index e15375f..f3a5bef 100644 --- a/src_blockchain/bc_status_cache/BlockchainController.h +++ b/src_blockchain/bc_status_cache/BlockchainController.h @@ -65,6 +65,8 @@ class BlockchainController { void finalize(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId, MemoryPool* memPool); void finalizeHeader(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId, MemoryPool* memPool); + void setNumRecognizedZones(uint16_t zone, uint16_t numRecognizedZones); + private: void handleHeaderCommandsOnFinalize(uint16_t zone, uint64_t finalizingHeight, const BlockHeaderId *headerId); diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp b/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp index 66f5b61..2c87ea5 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp @@ -144,6 +144,8 @@ void BlockchainStatusCache::newZone(bool headerOnly) { cache->open(); this->zoneList.addElement(__STP_MV(cache)); + + cache->setNumRecognizedZones(nZone + 1); // the zone cache recognizes itself } this->zoneListSize = this->zoneList.size(); @@ -525,6 +527,11 @@ void BlockchainStatusCache::setNumZones(uint16_t numZones) noexcept { this->numZones = numZones; } +void BlockchainStatusCache::setNumRecognizedZones(uint16_t zone,uint16_t numRecognizedZones) { + ZoneStatusCache* cache = this->zoneList.get(zone); + cache->setNumRecognizedZones(numRecognizedZones); +} + void BlockchainStatusCache::setShardExtentionValidator(const AbstractShardExtentionValidator *validator) noexcept { delete this->shardExtentionValidator; this->shardExtentionValidator = validator->copy(); diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.h b/src_blockchain/bc_status_cache/BlockchainStatusCache.h index 44a3915..06f68fc 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.h +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.h @@ -98,6 +98,8 @@ class BlockchainStatusCache : public IBlockchainEventListner { return this->zoneSelf; } + void setNumRecognizedZones(uint16_t zone,uint16_t numRecognizedZones); + int getRequestedNewShards(uint16_t zone) const noexcept; void setNumZones(uint16_t numZones) noexcept; diff --git a/src_blockchain/bc_status_cache/ZoneStatusCache.cpp b/src_blockchain/bc_status_cache/ZoneStatusCache.cpp index 271a0e9..79f5178 100644 --- a/src_blockchain/bc_status_cache/ZoneStatusCache.cpp +++ b/src_blockchain/bc_status_cache/ZoneStatusCache.cpp @@ -70,6 +70,7 @@ ZoneStatusCache::ZoneStatusCache(const File* baseDir, uint16_t zone, bool header this->ticketPrice = config->getTicketPriceDefault(1L); this->requestedNewShards = 0; + this->numRecognizingZones = 0; } ZoneStatusCache::ZoneStatusCache(const File *baseDir, uint16_t zone, ISystemLogger* logger, bool headerOnly, const CodablecashSystemParam* config) { @@ -90,6 +91,7 @@ ZoneStatusCache::ZoneStatusCache(const File *baseDir, uint16_t zone, ISystemLogg this->ticketPrice = 0; this->requestedNewShards = 0; + this->numRecognizingZones = 0; } ZoneStatusCache::~ZoneStatusCache() { @@ -161,6 +163,7 @@ void ZoneStatusCache::saveStatus() { this->statusStore->addLongValue(KEY_FINALIZED_HEIGHT, this->finalizedHeight); this->statusStore->addLongValue(KEY_FINALIZED_TICKET_PRICE, this->ticketPrice); this->statusStore->addShortValue(KEY_REQUESTED_SHARDS, this->requestedNewShards); + this->statusStore->addShortValue(KEY_NUM_RECOGNIZING_ZONES, this->numRecognizingZones); } void ZoneStatusCache::loadStatus() { @@ -172,6 +175,7 @@ void ZoneStatusCache::loadStatus() { this->finalizedHeight = this->statusStore->getLongValue(KEY_FINALIZED_HEIGHT); this->ticketPrice = this->statusStore->getLongValue(KEY_FINALIZED_TICKET_PRICE); this->requestedNewShards = this->statusStore->getShortValue(KEY_REQUESTED_SHARDS); + this->numRecognizingZones = this->statusStore->getShortValue(KEY_NUM_RECOGNIZING_ZONES); } void ZoneStatusCache::updateBlockStatus(MemPoolTransaction* memTrx, CodablecashBlockchain *chain, const CodablecashSystemParam* config) { @@ -383,4 +387,10 @@ RemoteUtxoRepository* ZoneStatusCache::getRemoteUtxoRepository() const noexcept return this->finalizedCache->getRemoteUtxoRepository(); } +void ZoneStatusCache::setNumRecognizedZones(uint16_t num) { + this->numRecognizingZones = num; + saveStatus(); +} + + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/ZoneStatusCache.h b/src_blockchain/bc_status_cache/ZoneStatusCache.h index a555e5a..6d39602 100644 --- a/src_blockchain/bc_status_cache/ZoneStatusCache.h +++ b/src_blockchain/bc_status_cache/ZoneStatusCache.h @@ -50,6 +50,8 @@ class ZoneStatusCache { static const constexpr wchar_t* KEY_FINALIZED_HEIGHT{L"finalizedHeight"}; static const constexpr wchar_t* KEY_FINALIZED_TICKET_PRICE{L"finalizedTicketPrice"}; static const constexpr wchar_t* KEY_REQUESTED_SHARDS{L"requestedShards"}; + static const constexpr wchar_t* KEY_NUM_RECOGNIZING_ZONES{L"numRecognizingZones"}; + ZoneStatusCache(const File* baseDir, uint16_t zone, bool headerOnly, ISystemLogger* logger, const CodablecashSystemParam* config); ZoneStatusCache(const File* baseDir, uint16_t zone, ISystemLogger* logger, bool headerOnly, const CodablecashSystemParam* config); @@ -99,6 +101,8 @@ class ZoneStatusCache { return this->requestedNewShards; } + void setNumRecognizedZones(uint16_t num); + RemoteUtxoRepository* getRemoteUtxoRepository() const noexcept; private: @@ -114,6 +118,8 @@ class ZoneStatusCache { uint64_t ticketPrice; + + File* baseDir; StatusStore* statusStore; HeadBlockDetector* headBlockDetector; @@ -123,6 +129,8 @@ class ZoneStatusCache { VoteManager* voteManager; int requestedNewShards; + // zones + uint16_t numRecognizingZones; }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp index 6eda6ed..47f11c2 100644 --- a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp +++ b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp @@ -364,7 +364,7 @@ void StatusCacheContext::importRewordTransactions(const BlockHeader *header, con } } -void StatusCacheContext::importInterChainCommunicationTransaction( const BlockHeader *header, const AbstractInterChainCommunicationTansaction *trx, ISystemLogger* logger) { +void StatusCacheContext::importInterChainCommunicationTransaction(const BlockHeader *header, const AbstractInterChainCommunicationTansaction *trx, ISystemLogger* logger) { #ifdef __DEBUG__ bool bl = this->trxCache->hasTransaction(trx->getTransactionId()); assert(bl == false); @@ -373,6 +373,12 @@ void StatusCacheContext::importInterChainCommunicationTransaction( const BlockHe this->trxCache->putTransaction(trx); importUtxo(trx, header); + + uint8_t type = trx->getType(); + if(type == AbstractBlockchainTransaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ + this->numZones++; + // FIXME [multishard] + } } void StatusCacheContext::importSmartcontractTransaction(const BlockHeader *header, const AbstractSmartcontractTransaction *trx, ISystemLogger* logger) { diff --git a/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt b/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt index 0a597db..8881538 100644 --- a/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt +++ b/src_blockchain/bc_status_cache_extend_shard/CMakeLists.txt @@ -4,6 +4,7 @@ set(__src GenerateNewGenesisBlockCommandMessage.cpp NotifyShardExtendRequestCommandMessage.cpp NotifyZoneExtendRequestedTransaction.cpp + SendNotifyZoneExtendRequestedNodeCommand.cpp ) handle_sub(codablecashlib "${__src}" blockchain bc_status_cache_extend_shard) diff --git a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp index a155570..04d20c1 100644 --- a/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp @@ -29,6 +29,11 @@ GenerateNewGenesisBlockCommandMessage::~GenerateNewGenesisBlockCommandMessage() void GenerateNewGenesisBlockCommandMessage::process(CentralProcessor *processor) { BlockchainController* ctrl = processor->getCtrl(); ctrl->addBlock(this->genesisBlock); + + uint16_t zoneSelf = ctrl->getZoneSelf(); + if(this->newShardZone == zoneSelf){ + ctrl->setNumRecognizedZones(zoneSelf, this->newShardZone + 1); + } } void GenerateNewGenesisBlockCommandMessage::setNewShardZone(uint16_t newShardZone) noexcept { diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp index 4c806f1..1a6735e 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp @@ -6,18 +6,37 @@ */ #include "bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h" +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" +#include "bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h" #include "bc_p2p_processor/P2pRequestProcessor.h" -#include "bc_p2p/BlochchainP2pManager.h" - #include "bc_processor/CentralProcessor.h" +#include "bc_p2p/BlochchainP2pManager.h" + #include "bc_block/BlockHeaderId.h" +#include "base/StackRelease.h" + +#include "bc_memorypool/MemoryPool.h" +#include "bc_memorypool/MemPoolTransaction.h" + +#include "data_history_data/TransactionTransferData.h" + +#include "bc_network/NodeIdentifier.h" + +#include "bc_block_header_command/BlockHeaderCommandId.h" + +#include "bc_status_cache/BlockchainController.h" + +#include "bc_status_cache_context/IStatusCacheContext.h" + + namespace codablecash { NotifyShardExtendRequestCommandMessage::NotifyShardExtendRequestCommandMessage() { + this->commandId = nullptr; this->newShardZone = 0; this->requestingZone = 0; this->height = 0; @@ -25,6 +44,7 @@ NotifyShardExtendRequestCommandMessage::NotifyShardExtendRequestCommandMessage() } NotifyShardExtendRequestCommandMessage::~NotifyShardExtendRequestCommandMessage() { + delete this->commandId; delete this->headerId; } @@ -32,8 +52,48 @@ void NotifyShardExtendRequestCommandMessage::process(CentralProcessor *processor P2pRequestProcessor* p2pRequestProcessor = processor->getP2pRequestProcessor(); BlochchainP2pManager* p2pManager = processor->getBlochchainP2pManager(); - - + // [multishard] broadcast transaction + NotifyZoneExtendRequestedTransaction* trx = new NotifyZoneExtendRequestedTransaction(); __STP(trx); + trx->setHeaderInfo(this->requestingZone, this->height, this->headerId); + trx->setNewShardZone(this->newShardZone); + trx->setCommandId(this->commandId); + trx->build(); + + // add self memory pool + { + BlockchainController* ctrl = processor->getCtrl(); + uint16_t zoneSelf = ctrl->getZoneSelf(); + MemoryPool* mempool = processor->getMemoryPool(); + + bool result = false; + { + MemPoolTransaction* memTrx = mempool->begin(); __STP(memTrx); + IStatusCacheContext* context = ctrl->getBlankStatusCacheContext(zoneSelf); __STP(context); + + result = trx->validateOnAccept(memTrx, context); + } + + if(result == true){ + mempool->putTransaction(trx); + } + } + + if(p2pRequestProcessor != nullptr && p2pManager != nullptr){ + SendNotifyZoneExtendRequestedNodeCommand command; + TransactionTransferData data; + data.setTransaction(trx); + command.setTransactionTransferData(&data); + + NodeIdentifierSource* networkKey = p2pRequestProcessor->getNetworkKey(); + command.sign(networkKey); + + // add into the history + p2pRequestProcessor->addHistory(&data); + + // broad cast + ArrayList list; + p2pManager->bloadCastHighPriorityAllZones(&list, &command, p2pRequestProcessor); + } } void NotifyShardExtendRequestCommandMessage::setNewShardZone(uint16_t newShardZone) noexcept { @@ -51,4 +111,9 @@ void NotifyShardExtendRequestCommandMessage::setHeaderInfo(uint64_t height, cons this->headerId = dynamic_cast(headerId->copyData()); } +void NotifyShardExtendRequestCommandMessage::setCommandId(const BlockHeaderCommandId *commandId) { + delete this->commandId; + this->commandId = dynamic_cast(commandId->copyData()); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h index 0d34e53..edf2fe3 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h @@ -14,6 +14,7 @@ namespace codablecash { class BlockHeaderId; +class BlockHeaderCommandId; class NotifyShardExtendRequestCommandMessage : public AbstractCentralProcessorCommandMessage { public: @@ -23,11 +24,13 @@ class NotifyShardExtendRequestCommandMessage : public AbstractCentralProcessorCo void setNewShardZone(uint16_t newShardZone) noexcept; void setRequestingZone(uint16_t requestingZone) noexcept; void setHeaderInfo(uint64_t height, const BlockHeaderId* headerId) noexcept; + void setCommandId(const BlockHeaderCommandId* commandId); protected: virtual void process(CentralProcessor* processor); private: + BlockHeaderCommandId* commandId; uint16_t newShardZone; uint16_t requestingZone; diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp index ed490a1..3961957 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp @@ -17,10 +17,25 @@ #include "base_timestamp/SystemTimestamp.h" #include "base/StackRelease.h" +#include "base/ArrayList.h" #include "crypto/Sha256.h" #include "bc_block/BlockHeaderId.h" +#include "bc_block/BlockHeader.h" + +#include "bc_status_cache_context/IStatusCacheContext.h" +#include "bc_status_cache_context/RemoteUtxoDetector.h" + +#include "bc_status_cache/BlockchainStatusCache.h" + +#include "bc_blockstore/CodablecashBlockchain.h" + +#include "bc_block_header_command/BlockHeaderCommandId.h" +#include "bc_block_header_command/NewShardZoneCommand.h" + + +using alinous::ArrayList; namespace codablecash { @@ -32,6 +47,9 @@ NotifyZoneExtendRequestedTransaction::NotifyZoneExtendRequestedTransaction(const this->headerId = inst.headerId != nullptr ? dynamic_cast(inst.headerId->copyData()) : nullptr; this->utxoId = inst.utxoId != nullptr ? dynamic_cast(inst.utxoId->copyData()) : nullptr; + this->newShardZone = inst.newShardZone; + + this->commandId = inst.commandId != nullptr ? dynamic_cast(inst.commandId->copyData()) : nullptr; } NotifyZoneExtendRequestedTransaction::NotifyZoneExtendRequestedTransaction() : AbstractInterChainCommunicationTansaction() { @@ -39,11 +57,15 @@ NotifyZoneExtendRequestedTransaction::NotifyZoneExtendRequestedTransaction() : A this->height = 0; this->headerId = nullptr; + this->newShardZone = 0; + this->utxoId = nullptr; + this->commandId = nullptr; } NotifyZoneExtendRequestedTransaction::~NotifyZoneExtendRequestedTransaction() { delete this->headerId; + delete this->commandId; } uint8_t NotifyZoneExtendRequestedTransaction::getType() const noexcept { @@ -54,6 +76,7 @@ int NotifyZoneExtendRequestedTransaction::binarySize() const { BinaryUtils::checkNotNull(this->version); BinaryUtils::checkNotNull(this->timestamp); BinaryUtils::checkNotNull(this->headerId); + BinaryUtils::checkNotNull(this->commandId); int total = sizeof(uint8_t); total += this->version->binarySize(); @@ -63,6 +86,10 @@ int NotifyZoneExtendRequestedTransaction::binarySize() const { total += sizeof(uint64_t); // height total += this->headerId->binarySize(); + total += sizeof(this->newShardZone); + + total += this->commandId->binarySize(); + return total; } @@ -70,6 +97,7 @@ void NotifyZoneExtendRequestedTransaction::toBinary(ByteBuffer *out) const { BinaryUtils::checkNotNull(this->version); BinaryUtils::checkNotNull(this->timestamp); BinaryUtils::checkNotNull(this->headerId); + BinaryUtils::checkNotNull(this->commandId); out->put(getType()); @@ -79,6 +107,10 @@ void NotifyZoneExtendRequestedTransaction::toBinary(ByteBuffer *out) const { out->putShort(this->zone); out->putLong(this->height); this->headerId->toBinary(out); + + out->putShort(this->newShardZone); + + this->commandId->toBinary(out); } void NotifyZoneExtendRequestedTransaction::fromBinary(ByteBuffer *in) { @@ -91,16 +123,24 @@ void NotifyZoneExtendRequestedTransaction::fromBinary(ByteBuffer *in) { this->zone = in->getShort(); this->height = in->getLong(); this->headerId = BlockHeaderId::fromBinary(in); + + this->newShardZone = in->getShort(); + + this->commandId = BlockHeaderCommandId::fromBinary(in); + + build(); } void NotifyZoneExtendRequestedTransaction::build() { BinaryUtils::checkNotNull(this->version); BinaryUtils::checkNotNull(this->timestamp); BinaryUtils::checkNotNull(this->headerId); + BinaryUtils::checkNotNull(this->commandId); { int capacity = sizeof(uint8_t) + this->version->binarySize() + this->timestamp->binarySize(); - capacity += sizeof(uint16_t) + sizeof(uint64_t) + this->headerId->binarySize(); + capacity += sizeof(uint16_t) + sizeof(uint64_t) + this->headerId->binarySize() + sizeof(this->newShardZone) + + this->commandId->binarySize(); ByteBuffer* buff = ByteBuffer::allocateWithEndian(capacity, true); __STP(buff); buff->put(getType()); @@ -112,6 +152,10 @@ void NotifyZoneExtendRequestedTransaction::build() { buff->putLong(this->height); this->headerId->toBinary(buff); + buff->putShort(this->newShardZone); + + this->commandId->toBinary(buff); + buff->position(0); ByteBuffer* sha = Sha256::sha256(buff, true); __STP(sha); @@ -131,7 +175,7 @@ void NotifyZoneExtendRequestedTransaction::build() { ByteBuffer* sha = Sha256::sha256(buff, true); __STP(sha); - delete this->utxoId; + delete this->utxoId, this->utxoId = nullptr; this->utxoId = new UtxoId((const char*)sha->array(), sha->limit()); } } @@ -165,12 +209,49 @@ AbstractUtxoReference* NotifyZoneExtendRequestedTransaction::getUtxoReference(in } bool NotifyZoneExtendRequestedTransaction::validateOnAccept(MemPoolTransaction *memTrx, IStatusCacheContext *context) const { - // FIXME[multishard] validate; + bool ret = false; - return true; + CodablecashBlockchain* blockchain = context->getBlockChain(); + BlockchainStatusCache* statusCache = context->getBlockchainStatusCache(); + + uint64_t finalizedHeight = statusCache->getFinalizedHeight(this->zone); + + { + const BlockHeader* header = nullptr; + + ArrayList* list = blockchain->getBlockHeadersHeightAt(this->zone, this->height); __STP(list); + list->setDeleteOnExit(); + + int maxLoop = list->size(); + for(int i = 0; i != maxLoop; ++i){ + const BlockHeader* h = list->get(i); + const BlockHeaderId* id = h->getId(); + if(this->headerId->equals(id)){ + header = h; + } + } + + if(header != nullptr){ + const AbstractBlockHeaderCommand* command = header->getHeaderCommand(this->commandId); + const NewShardZoneCommand* newShardZoneCommand = dynamic_cast(command); + + // const BlockHeaderCommandId* newCommnadId = newShardZoneCommand->getCommandId(); + uint64_t headerHeight = header->getHeight(); + + ret = newShardZoneCommand != nullptr && this->commandId->equals(newShardZoneCommand->getCommandId()); + } + } + + return ret; } TrxValidationResult NotifyZoneExtendRequestedTransaction::validateFinal(const BlockHeader *header, MemPoolTransaction *memTrx, IStatusCacheContext *context) const { + RemoteUtxoDetector* remoteDetector = context->getRemoteUtxoDetector(); + + if(remoteDetector->isRemoteUtxoUsed(this->utxoId)){ + return TrxValidationResult::INVALID; + } + return TrxValidationResult::OK; } @@ -186,4 +267,13 @@ void NotifyZoneExtendRequestedTransaction::setHeaderInfo(uint16_t zone, uint64_t this->headerId = dynamic_cast(headerId->copyData()); } +void NotifyZoneExtendRequestedTransaction::setNewShardZone(uint16_t newShardZone) noexcept { + this->newShardZone = newShardZone; +} + +void NotifyZoneExtendRequestedTransaction::setCommandId(const BlockHeaderCommandId *commandId) { + delete this->commandId; + this->commandId = dynamic_cast(commandId->copyData()); +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h index c97b26c..5b6e7bf 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h @@ -14,6 +14,7 @@ namespace codablecash { class BlockHeaderId; class UtxoId; +class BlockHeaderCommandId; class NotifyZoneExtendRequestedTransaction : public AbstractInterChainCommunicationTansaction { public: @@ -45,6 +46,12 @@ class NotifyZoneExtendRequestedTransaction : public AbstractInterChainCommunicat virtual bool checkFilter(const ArrayList *filtersList) const; void setHeaderInfo(uint16_t zone, uint64_t height, const BlockHeaderId* headerId); + void setNewShardZone(uint16_t newShardZone) noexcept; + void setCommandId(const BlockHeaderCommandId* commandId); + + uint16_t getNewShardZone() const noexcept { + return this->newShardZone; + } private: // header info @@ -52,6 +59,12 @@ class NotifyZoneExtendRequestedTransaction : public AbstractInterChainCommunicat uint64_t height; BlockHeaderId* headerId; + // new shard info + uint16_t newShardZone; + + // Header Command Info + BlockHeaderCommandId* commandId; + // build automatically UtxoId* utxoId; }; diff --git a/src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.cpp b/src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.cpp new file mode 100644 index 0000000..8df0113 --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.cpp @@ -0,0 +1,196 @@ +/* + * SendNotifyZoneExtendRequestedNodeCommand.cpp + * + * Created on: Jul 19, 2026 + * Author: iizuka + */ + +#include "bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h" +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" + +#include "bc_status_cache/BlockchainController.h" + +#include "bc_status_cache_context/IStatusCacheContext.h" + +#include "bc_base/BinaryUtils.h" + +#include "bc_trx/AbstractBlockchainTransaction.h" + +#include "data_history_data/TransactionTransferData.h" + +#include "base_timestamp/SystemTimestamp.h" + +#include "base/StackRelease.h" + +#include "bc/CodablecashNodeInstance.h" + +#include "bc_p2p_processor/P2pRequestProcessor.h" + +#include "bc_memorypool/MemoryPool.h" +#include "bc_memorypool/MemPoolTransaction.h" + +#include "bc/ExceptionThrower.h" + +#include "bc_p2p_cmd_node/InvalidTransactionException.h" + +#include "bc_p2p/BlockchainNodeHandshake.h" +#include "bc_p2p/BlochchainP2pManager.h" + +#include "command_queue_cmd/NodeTransactionAcceptionQueueCommand.h" + +#include "bc_network/NodeIdentifierSource.h" +#include "bc_network/NodeIdentifier.h" + +#include "bc_p2p_cmd_network/NodeNetworkInfo.h" + +#include "pubsub_cmd/OkPubsubResponse.h" +namespace codablecash { + +SendNotifyZoneExtendRequestedNodeCommand::SendNotifyZoneExtendRequestedNodeCommand(const SendNotifyZoneExtendRequestedNodeCommand &inst) + : AbstractConsensusNodeCommand(inst) { + this->data = inst.data != nullptr ? dynamic_cast(inst.data->copyData()) : nullptr; +} + +SendNotifyZoneExtendRequestedNodeCommand::SendNotifyZoneExtendRequestedNodeCommand() : AbstractConsensusNodeCommand(TYPE_CONSENSUS_SEND_NOTIFY_ZONE_EXTEND_REQUEST) { + this->data = nullptr; +} + +SendNotifyZoneExtendRequestedNodeCommand::~SendNotifyZoneExtendRequestedNodeCommand() { + delete this->data; +} + +int SendNotifyZoneExtendRequestedNodeCommand::binarySize() const { + BinaryUtils::checkNotNull(this->data); + + int total = AbstractConsensusNodeCommand::binarySize(); + total += this->data->binarySize(); + + return total; +} + +void SendNotifyZoneExtendRequestedNodeCommand::toBinary(ByteBuffer *buff) const { + BinaryUtils::checkNotNull(this->data); + + AbstractConsensusNodeCommand::toBinary(buff); + this->data->toBinary(buff); +} + +void SendNotifyZoneExtendRequestedNodeCommand::fromBinary(ByteBuffer *buff) { + AbstractConsensusNodeCommand::fromBinary(buff); + + AbstractTransferedData* d = TransactionTransferData::createFromBinary(buff); __STP(d); + this->data = dynamic_cast(d); + BinaryUtils::checkNotNull(this->data); + + __STP_MV(d); +} + +ByteBuffer* SendNotifyZoneExtendRequestedNodeCommand::getSignBinary() const { + BinaryUtils::checkNotNull(this->data); + + int total = this->data->binarySize(); + + ByteBuffer* buff = ByteBuffer::allocateWithEndian(total, true); + this->data->toBinary(buff); + + return buff; +} + +IBlockObject* SendNotifyZoneExtendRequestedNodeCommand::copyData() const noexcept { + return new SendNotifyZoneExtendRequestedNodeCommand(*this); +} + +SystemTimestamp SendNotifyZoneExtendRequestedNodeCommand::getFirstTimestamp() const { + AbstractBlockchainTransaction* trx = this->data->getTransaction(); + const SystemTimestamp* tm = trx->getTimestamp(); + + return *tm; +} + +AbstractCommandResponse* SendNotifyZoneExtendRequestedNodeCommand::executeAsNode(BlockchainNodeHandshake *nodeHandShake, CodablecashNodeInstance *inst, bool suspend) const { + P2pRequestProcessor* processor = inst->getP2pRequestProcessor(); + + uint16_t zoneSelf = inst->getZoneSelf(); + { + AbstractBlockchainTransaction* trx = this->data->getTransaction(); + NotifyZoneExtendRequestedTransaction* extendTrx = dynamic_cast(trx); + uint16_t newZone = extendTrx->getNewShardZone(); + if(newZone == zoneSelf){ + return new OkPubsubResponse(); + } + } + + + bool alreadyHas = processor->hasHistory(this->data); + if(!alreadyHas){ + BlockchainController* ctrl = inst->getController(); + MemoryPool* memPool = inst->getMemoryPool(); + + processor->addHistory(this->data); + + { + bool res = processTransaction(zoneSelf, memPool, ctrl); + ExceptionThrower::throwExceptionIfCondition(!res, L"Invalid Transaction was sent.", __FILE__, __LINE__); + } + + // put on queue + { + const PubSubId* pubsubId = nodeHandShake->getPubsubId(); + + NodeTransactionAcceptionQueueCommand cmd; + cmd.setData(this->data); + + processor->putQueue(pubsubId, &cmd); + } + + // [multishard] review it + if(!suspend){ + // bload cast + BlochchainP2pManager* manager = inst->getBlochchainP2pManager(); + { + SendNotifyZoneExtendRequestedNodeCommand command(*this); + + NodeIdentifierSource* nwkey = processor->getNetworkKey(); + { + uint16_t zone = inst->getZoneSelf(); + NodeIdentifier nodeId = nwkey->toNodeIdentifier(); + + int protocol = manager->getProtocol(); + const UnicodeString* host = manager->getHost(); + uint32_t port = manager->getPort(); + + command.addHistory(zone, &nodeId, protocol, host, port); + } + + command.sign(nwkey); + + // make list to exclude broad cast + ArrayList list; + list.setDeleteOnExit(true); + command.makeHistoryExcludeList(&list); + + // broadcast + manager->bloadCastHighPriorityAllZones(&list, &command, processor); + } + } + } + + return new OkPubsubResponse(); +} + +bool SendNotifyZoneExtendRequestedNodeCommand::processTransaction(uint16_t zoneSelf, MemoryPool *memPool, BlockchainController *ctrl) const { + AbstractBlockchainTransaction* trx = this->data->getTransaction(); + + MemPoolTransaction* memTrx = memPool->begin(); __STP(memTrx); + + IStatusCacheContext* context = ctrl->getStatusCacheContext(zoneSelf); __STP(context); + + return trx->validateOnAccept(memTrx, context); +} + +void SendNotifyZoneExtendRequestedNodeCommand::setTransactionTransferData(const TransactionTransferData *data) noexcept { + delete this->data; + this->data = dynamic_cast(data->copyData()); +} + +} /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h b/src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h new file mode 100644 index 0000000..1308bfc --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h @@ -0,0 +1,49 @@ +/* + * SendNotifyZoneExtendRequestedNodeCommand.h + * + * Created on: Jul 19, 2026 + * Author: iizuka + */ + +#ifndef BC_STATUS_CACHE_EXTEND_SHARD_SENDNOTIFYZONEEXTENDREQUESTEDNODECOMMAND_H_ +#define BC_STATUS_CACHE_EXTEND_SHARD_SENDNOTIFYZONEEXTENDREQUESTEDNODECOMMAND_H_ + +#include "bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.h" + +namespace codablecash { + +class TransactionTransferData; +class BlockchainController; +class MemoryPool; +class NotifyZoneExtendRequestedTransaction; + +class SendNotifyZoneExtendRequestedNodeCommand: public AbstractConsensusNodeCommand { +public: + SendNotifyZoneExtendRequestedNodeCommand(const SendNotifyZoneExtendRequestedNodeCommand& inst); + SendNotifyZoneExtendRequestedNodeCommand(); + virtual ~SendNotifyZoneExtendRequestedNodeCommand(); + + virtual int binarySize() const; + virtual void toBinary(ByteBuffer* buff) const; + virtual void fromBinary(ByteBuffer* buff); + + virtual ByteBuffer* getSignBinary() const; + virtual IBlockObject* copyData() const noexcept; + + virtual SystemTimestamp getFirstTimestamp() const; + + void setTransactionTransferData(const TransactionTransferData* data) noexcept; + +protected: + virtual AbstractCommandResponse* executeAsNode(BlockchainNodeHandshake* nodeHandShake, CodablecashNodeInstance* inst, bool suspend) const; + +private: + bool processTransaction(uint16_t zoneSelf, MemoryPool* memPool, BlockchainController* ctrl) const; + +private: + TransactionTransferData* data; +}; + +} /* namespace codablecash */ + +#endif /* BC_STATUS_CACHE_EXTEND_SHARD_SENDNOTIFYZONEEXTENDREQUESTEDNODECOMMAND_H_ */ diff --git a/src_blockchain/bc_trx/AbstractBlockchainTransaction.cpp b/src_blockchain/bc_trx/AbstractBlockchainTransaction.cpp index d3c2741..519a650 100644 --- a/src_blockchain/bc_trx/AbstractBlockchainTransaction.cpp +++ b/src_blockchain/bc_trx/AbstractBlockchainTransaction.cpp @@ -42,6 +42,7 @@ #include "bc_wallet_filter/BloomFilter1024.h" +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" using alinous::Os; @@ -103,6 +104,9 @@ AbstractBlockchainTransaction* AbstractBlockchainTransaction::createFromBinary(B case TRX_TYPE_ICC_NOP: ret = new NopInterChainCommunicationTransaction(); break; + case TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED: + ret = new NotifyZoneExtendRequestedTransaction(); + break; default: return nullptr; } diff --git a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp index e68eed6..6ec0bfa 100644 --- a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp +++ b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp @@ -39,6 +39,8 @@ BlochchainP2pManager::BlochchainP2pManager() { this->zones = new ArrayList(); this->end = false; + this->numZones = 0; + this->protocol = 0; this->host = nullptr; this->port = 0; @@ -148,12 +150,23 @@ void BlochchainP2pManager::__removeHandshake(const PubSubId *pubsubId) { void BlochchainP2pManager::init(int numZones) { StackUnlocker __lock(this->mutex, __FILE__, __LINE__); + this->numZones = numZones; + for(int i = 0; i != numZones; ++i){ P2pZone* z = new P2pZone(i); this->zones->addElement(z); } } +void BlochchainP2pManager::incNumZones() { + StackUnlocker __lock(this->mutex, __FILE__, __LINE__); + int newZone = this->numZones; + + this->numZones++; + P2pZone* z = new P2pZone(newZone); + this->zones->addElement(z); +} + void BlochchainP2pManager::onLoginHandshake(P2pHandshake *handshake, const LoginPubSubCommand *loginCommand, const UnicodeString* canonicalName) { StackUnlocker __lock(this->mutex, __FILE__, __LINE__); @@ -161,7 +174,7 @@ void BlochchainP2pManager::onLoginHandshake(P2pHandshake *handshake, const Login const NodeIdentifier* nodeId = loginCommand->getNodeId(); // zone size - ExceptionThrower::throwExceptionIfCondition(zone > this->zones->size(), L"Zone does not exists", __FILE__, __LINE__); + ExceptionThrower::throwExceptionIfCondition(zone > this->numZones, L"Zone does not exists", __FILE__, __LINE__); // use extra zone if(zone == this->zones->size()){ @@ -182,7 +195,7 @@ void BlochchainP2pManager::registerHandshake(uint16_t zone, P2pHandshake *handsh StackUnlocker __lock(this->mutex, __FILE__, __LINE__); - ExceptionThrower::throwExceptionIfCondition(zone > this->zones->size(), L"Zone does not exists", __FILE__, __LINE__); + ExceptionThrower::throwExceptionIfCondition(zone > this->numZones, L"Zone does not exists", __FILE__, __LINE__); // use extra zone if(zone == this->zones->size()){ @@ -369,7 +382,7 @@ void BlochchainP2pManager::bloadCastHighPriorityAllZones(const ArrayListmutex, __FILE__, __LINE__); if(!this->end){ - int maxLoop = this->zones->size(); + int maxLoop = this->numZones; // this->zones->size(); for(int i = 0; i != maxLoop; ++i){ P2pZone* p2pzone = this->zones->get(i); diff --git a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h index 51fd7b2..0f36a26 100644 --- a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h +++ b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h @@ -36,6 +36,7 @@ class BlochchainP2pManager : public IPubsubConnectionListener { virtual ~BlochchainP2pManager(); void init(int numZones); + void incNumZones(); void resetZones() noexcept; @@ -89,6 +90,7 @@ class BlochchainP2pManager : public IPubsubConnectionListener { private: SysMutex* mutex; + int numZones; ArrayList* zones; HashMap blockchainHandshakeHash; diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.cpp b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.cpp index 704d61b..f81054b 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.cpp +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.cpp @@ -13,6 +13,8 @@ #include "bc/CodablecashNodeInstance.h" #include "bc_p2p_cmd_network/NodeNetworkInfo.h" +#include "bc_network/NodeIdentifier.h" + namespace codablecash { @@ -141,4 +143,15 @@ bool AbstractConsensusNodeCommand::validateCurrentTime(const SystemTimestamp *la return hops <= maxHops && lim.compareTo(&tmnow) >= 0; } +void AbstractConsensusNodeCommand::makeHistoryExcludeList(ArrayList *list) const { + int maxLoop = this->history->size(); + for(int i = 0; i != maxLoop; ++i){ + NodeNetworkInfo* his = this->history->get(i); + const NodeIdentifier* nodeId = his->getNodeIdentifier(); + + NodeIdentifier* newId = dynamic_cast(nodeId->copyData()); + list->addElement(newId); + } +} + } /* namespace codablecash */ diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.h b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.h index af822ad..58f1e60 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.h +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/AbstractConsensusNodeCommand.h @@ -45,6 +45,8 @@ class AbstractConsensusNodeCommand : public AbstractNodeCommand { return this->history; } + void makeHistoryExcludeList(ArrayList* list) const; + protected: virtual bool validateHistory(const NodeNetworkInfo* his, const SystemTimestamp* lastTm, CodablecashNodeInstance *inst) const noexcept; virtual bool validateCurrentTime(const SystemTimestamp* lastTm, CodablecashNodeInstance *inst) const noexcept; diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp index 8335b81..555da35 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp @@ -192,17 +192,7 @@ AbstractCommandResponse* ReportNonceCalculatedNodeCommand::executeAsNode(Blockch ArrayList list; list.setDeleteOnExit(true); - { - const ArrayList* history = cmd.getHistory(); - int maxLoop = history->size(); - for(int i = 0; i != maxLoop; ++i){ - NodeNetworkInfo* his = history->get(i); - const NodeIdentifier* nodeId = his->getNodeIdentifier(); - - NodeIdentifier* newId = dynamic_cast(nodeId->copyData()); - list.addElement(newId); - } - } + cmd.makeHistoryExcludeList(&list); // broadcast p2pManager->bloadCastHighPriorityAllZones(&list, &cmd, p2pRequestProcessor); diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp index ebe797b..671fde9 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp @@ -152,17 +152,8 @@ AbstractCommandResponse* SendVoteTransactionNodeCommand::executeAsNode(Blockchai // make list to exclude broad cast ArrayList list; list.setDeleteOnExit(true); - { - const ArrayList* history = command.getHistory(); - int maxLoop = history->size(); - for(int i = 0; i != maxLoop; ++i){ - NodeNetworkInfo* his = history->get(i); - const NodeIdentifier* nodeId = his->getNodeIdentifier(); - - NodeIdentifier* newId = dynamic_cast(nodeId->copyData()); - list.addElement(newId); - } - } + command.makeHistoryExcludeList(&list); + // broadcast manager->bloadCastHighPriorityAllZones(&list, &command, processor); } diff --git a/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.cpp b/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.cpp index 1486b88..68caef7 100644 --- a/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.cpp +++ b/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.cpp @@ -54,6 +54,7 @@ #include "pow_pool_cmd/PoWPoolNotifyDataChangedCommand.h" +#include "bc_status_cache_extend_shard/SendNotifyZoneExtendRequestedNodeCommand.h" namespace codablecash { @@ -147,6 +148,9 @@ AbstractPubSubCommand* AbstractPubSubCommand::createFromBinary(ByteBuffer *buff) case TYPE_CONSENSUS_SEND_VOTE_TRANSACTION: ret = new SendVoteTransactionNodeCommand(); break; + case TYPE_CONSENSUS_SEND_NOTIFY_ZONE_EXTEND_REQUEST: + ret = new SendNotifyZoneExtendRequestedNodeCommand(); + break; case TYPE_POW_POOL_NOTIFY_DATA_CHANGED: ret = new PoWPoolNotifyDataChangedCommand(); diff --git a/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.h b/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.h index d0e93c0..1b0a008 100644 --- a/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.h +++ b/src_blockchain_p2p/pubsub_cmd/AbstractPubSubCommand.h @@ -57,6 +57,7 @@ class AbstractPubSubCommand : public IBlockObject { // Consensus Commands static const constexpr uint32_t TYPE_CONSENSUS_REPORT_NONCE_CALCULATED{350}; static const constexpr uint32_t TYPE_CONSENSUS_SEND_VOTE_TRANSACTION{351}; + static const constexpr uint32_t TYPE_CONSENSUS_SEND_NOTIFY_ZONE_EXTEND_REQUEST{352}; // Client Notify Commands static const constexpr uint32_t TYPE_CLIENT_NOTIFY_NEW_TRANSACTION{401}; diff --git a/src_test/blockchain/multi_shard/NewShardAdder.cpp b/src_test/blockchain/multi_shard/NewShardAdder.cpp index ae6b3df..0174d28 100644 --- a/src_test/blockchain/multi_shard/NewShardAdder.cpp +++ b/src_test/blockchain/multi_shard/NewShardAdder.cpp @@ -54,6 +54,8 @@ void NewShardAdder::__addCommand(Block *block, MemPoolTransaction *memTrx, Block gblock.build(); cmd.setGenesisblock(&gblock); + cmd.buildCommandId(); + header->addHeaderCommand(&cmd); } diff --git a/src_test/blockchain/multi_shard/NewShardValidator.cpp b/src_test/blockchain/multi_shard/NewShardValidator.cpp index ff0534e..120f752 100644 --- a/src_test/blockchain/multi_shard/NewShardValidator.cpp +++ b/src_test/blockchain/multi_shard/NewShardValidator.cpp @@ -24,7 +24,7 @@ NewShardValidator::~NewShardValidator() { } -bool NewShardValidator::validate(NewShardZoneCommand *newShardCommand, IStatusCacheContext *context, BlockchainController *ctrl) { +bool NewShardValidator::validate(AbstractBlockHeaderCommand *newShardCommand, IStatusCacheContext *context, BlockchainController *ctrl) { uint16_t numZones = context->getNumZones(); int requestedZones = context->getRequestedNewShards(); diff --git a/src_test/blockchain/multi_shard/NewShardValidator.h b/src_test/blockchain/multi_shard/NewShardValidator.h index db1b855..e24f84c 100644 --- a/src_test/blockchain/multi_shard/NewShardValidator.h +++ b/src_test/blockchain/multi_shard/NewShardValidator.h @@ -18,7 +18,7 @@ class NewShardValidator: public AbstractShardExtentionValidator { NewShardValidator(); virtual ~NewShardValidator(); - virtual bool validate(NewShardZoneCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl); + virtual bool validate(AbstractBlockHeaderCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl); virtual AbstractShardExtentionValidator* copy() const; }; diff --git a/src_test/blockchain/multi_shard/test_multi_shard.cpp b/src_test/blockchain/multi_shard/test_multi_shard.cpp index 346bbf7..f170d4c 100644 --- a/src_test/blockchain/multi_shard/test_multi_shard.cpp +++ b/src_test/blockchain/multi_shard/test_multi_shard.cpp @@ -127,10 +127,18 @@ TEST(TestMultiShardGroup, case01){ inst->setFinalizerConfig(&fconfig); } + // seeder ArrayDebugSeeder seeder; testnet.startGenesis(0, &seeder); + { + TestnetInstanceWrapper* inst = testnet.createInstance(0, L"second"); + + testnet.startInstancesFrom(0, 1, &seeder); + NewShardValidator shardValidator; + inst->setShardExtendValidator(&shardValidator); + } { TestnetInstanceWrapper* inst = testnet.getInstance(0, L"first"); @@ -192,7 +200,7 @@ TEST(TestMultiShardGroup, case01){ // staking ticket testnet.suspendMining(0); - int maxLoop = 10; + int maxLoop = 30; for(int i = 0; i != maxLoop; ++i){ BalanceUnit fee(1L); BalanceUnit stakeAmount(100L); @@ -271,7 +279,7 @@ TEST(TestMultiShardGroup, case01){ } } - testnet.waitForBlockHeight(0, 0, 10); + testnet.waitForBlockHeight(0, 0, 12); wallet->shutdownNetwork(); } From 06c4544976b3e351db3e65df6141e1a8a97c5787 Mon Sep 17 00:00:00 2001 From: iizuka Date: Thu, 30 Jul 2026 19:50:35 +0800 Subject: [PATCH 5/7] zone independent commands --- .../bc_block_generator/BlockGenerator.cpp | 4 ++- .../DetectVotingTicketCommandMessage.cpp | 9 ++++++- .../bc_finalizer_trx/VoteBlockTransaction.cpp | 13 ++++++++- .../bc_finalizer_trx/VoteBlockTransaction.h | 6 +++++ .../MinerMinedReportCommandMessage.cpp | 4 ++- .../TransferedMinedReportCommandMessage.cpp | 25 +++++------------ .../TransferedMinedReportCommandMessage.h | 1 - .../bc_status_cache/BlockchainStatusCache.h | 4 +++ .../StatusCacheContext.cpp | 12 +++++++++ .../NotifyZoneExtendRequestedTransaction.cpp | 5 +++- .../NotifyZoneExtendRequestedTransaction.h | 4 +++ .../bc_p2p/BlochchainP2pManager.cpp | 17 +++++++++++- .../bc_p2p/BlochchainP2pManager.h | 2 ++ .../ReportNonceCalculatedNodeCommand.cpp | 27 +++++++++++++++++++ .../ReportNonceCalculatedNodeCommand.h | 6 +++++ .../SendVoteTransactionNodeCommand.cpp | 3 ++- ...FinalizeMissDetectTicketCommandMessage.cpp | 2 ++ .../multi_shard/test_multi_shard.cpp | 2 +- .../utils_testnet/MultizoneTestnet.cpp | 2 +- 19 files changed, 119 insertions(+), 29 deletions(-) diff --git a/src_blockchain/bc_block_generator/BlockGenerator.cpp b/src_blockchain/bc_block_generator/BlockGenerator.cpp index 256b379..04df39e 100644 --- a/src_blockchain/bc_block_generator/BlockGenerator.cpp +++ b/src_blockchain/bc_block_generator/BlockGenerator.cpp @@ -74,6 +74,8 @@ #include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" #include "bc_block_header_command/RecognizedNewShardCommand.h" + +#include "bc_status_cache_context/RemoteUtxoDetector.h" namespace codablecash { BlockGenerator::BlockGenerator(uint16_t zone, CodablecashSystemParam* config, MemoryPool* memoryPool, BlockchainController* ctrl @@ -254,7 +256,7 @@ void BlockGenerator::importInterChainCommunicationTransactions2Block(MemPoolTran uint8_t tyxType = trx->getType(); if(tyxType == AbstractInterChainCommunicationTansaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ - // [multishard] add RecognizedNewShardCommand to the header + // FIXME [multishard] add RecognizedNewShardCommand to the header NotifyZoneExtendRequestedTransaction* notifyTrx = dynamic_cast(trx); RecognizedNewShardCommand command; diff --git a/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp b/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp index 1312e4f..5bcc6de 100644 --- a/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp +++ b/src_blockchain/bc_finalizer_pool/DetectVotingTicketCommandMessage.cpp @@ -118,6 +118,8 @@ void DetectVotingTicketCommandMessage::putTransaction(const BlockHeader *header2 // set time stamp of vote transaction VoteBlockTransaction* trx = new VoteBlockTransaction(); __STP(trx); + + trx->setZone(header2vote->getZone()); trx->setTicketUtxoId(utxoId, candidate->getTicletPrice(), desc); trx->setVoteBlockId(headerId); trx->setVoteBlockHeight(height2Vote); @@ -159,7 +161,12 @@ void DetectVotingTicketCommandMessage::putTransaction(const BlockHeader *header2 // broad cast uint16_t zone = header2vote->getZone(); - p2pManager->broadCastWithinZone(zone, &command, p2pRequestProcessor); + + ArrayList list; + list.setDeleteOnExit(true); + command.makeHistoryExcludeList(&list); + + p2pManager->bloadCastHighPriorityWithinZone(zone, &list, &command, p2pRequestProcessor); } } } diff --git a/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.cpp b/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.cpp index c785ff0..852d431 100644 --- a/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.cpp +++ b/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.cpp @@ -42,6 +42,7 @@ namespace codablecash { VoteBlockTransaction::VoteBlockTransaction(const VoteBlockTransaction &inst) : AbstractFinalizerTransaction(inst) { + this->zone = inst.zone; this->voterId = inst.voterId != nullptr ? new NodeIdentifier(*inst.voterId) : nullptr; this->ticketUtxoRef = inst.ticketUtxoRef != nullptr ? dynamic_cast(inst.ticketUtxoRef->copyData()) : nullptr; this->votedUtxo = inst.votedUtxo != nullptr ? dynamic_cast(inst.votedUtxo->copyData()) : nullptr; @@ -52,6 +53,7 @@ VoteBlockTransaction::VoteBlockTransaction(const VoteBlockTransaction &inst) } VoteBlockTransaction::VoteBlockTransaction() : AbstractFinalizerTransaction() { + this->zone = 0; this->voterId = nullptr; this->ticketUtxoRef = nullptr; this->votedUtxo = nullptr; @@ -84,7 +86,7 @@ bool VoteBlockTransaction::validateOnAccept(MemPoolTransaction *memTrx, IStatusC BlockHeaderStoreManager* headerManager = chain->getHeaderManager(zone); BlockHeader* vheader = headerManager->getHeader(this->voteBlockHeaderId, this->voteBlockHeight); __STP(vheader); - if(vheader == nullptr){ + if(vheader == nullptr && this->zone == zone){ return false; } } @@ -163,6 +165,7 @@ int VoteBlockTransaction::binarySize() const { int total = AbstractControlTransaction::__binarySize(); + total += sizeof(uint16_t); // zone total += this->voterId->binarySize(); total += this->ticketUtxoRef->binarySize(); total += this->votedUtxo->binarySize(); @@ -182,6 +185,7 @@ void VoteBlockTransaction::toBinary(ByteBuffer *out) const { AbstractControlTransaction::__toBinary(out); + out->putShort(this->zone); this->voterId->toBinary(out); this->ticketUtxoRef->toBinary(out); this->votedUtxo->toBinary(out); @@ -194,6 +198,7 @@ void VoteBlockTransaction::toBinary(ByteBuffer *out) const { void VoteBlockTransaction::fromBinary(ByteBuffer *in) { AbstractControlTransaction::__fromBinary(in); + in->putShort(this->zone); this->voterId = NodeIdentifier::fromBinary(in); AbstractUtxoReference* ref = AbstractUtxoReference::createFromBinary(in); __STP(ref); @@ -216,6 +221,7 @@ void VoteBlockTransaction::build() { setUtxoNonce(); int capacity = __binarySize(); + capacity += sizeof(uint16_t); // zone capacity += this->voterId->binarySize(); capacity += this->ticketUtxoRef->binarySize(); capacity += this->votedUtxo->binarySize(); @@ -225,6 +231,7 @@ void VoteBlockTransaction::build() { ByteBuffer* buff = ByteBuffer::allocateWithEndian(capacity, true); __STP(buff); __toBinary(buff); + buff->putShort(this->zone); this->voterId->toBinary(buff); this->ticketUtxoRef->toBinary(buff); this->votedUtxo->toBinary(buff); @@ -310,4 +317,8 @@ void VoteBlockTransaction::setFeeAmount(const BalanceUnit *fee) { this->fee = *fee; } +void VoteBlockTransaction::setZone(uint16_t z) noexcept { + this->zone = z; +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.h b/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.h index 25b7f58..cbe9819 100644 --- a/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.h +++ b/src_blockchain/bc_finalizer_trx/VoteBlockTransaction.h @@ -46,6 +46,11 @@ class VoteBlockTransaction : public AbstractFinalizerTransaction { virtual int getUtxoSize() const noexcept; virtual AbstractUtxo* getUtxo(int i) const noexcept; + void setZone(uint16_t z) noexcept; + /*uint16_t getZone() const noexcept { + return this->zone; + }*/ + void setVoterId(const NodeIdentifier* voterId) noexcept; void setTicketUtxoId(const UtxoId* utxoId, const BalanceUnit amount, const AddressDescriptor* desc) noexcept; @@ -78,6 +83,7 @@ class VoteBlockTransaction : public AbstractFinalizerTransaction { TrxValidationResult __validateFinal(const BlockHeader* header, MemPoolTransaction *memTrx, IStatusCacheContext* context, bool checkVotedHeaderId) const; private: + uint16_t zone; NodeIdentifier* voterId; TicketUtxoReference* ticketUtxoRef; TicketVotedUtxo* votedUtxo; diff --git a/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp b/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp index 28486ac..97ea2b4 100644 --- a/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp +++ b/src_blockchain/bc_processor_cmd/MinerMinedReportCommandMessage.cpp @@ -106,7 +106,9 @@ void MinerMinedReportCommandMessage::process(CentralProcessor *processor) { NodeIdentifierSource* nwkey = p2pRequestProcessor->getNetworkKey(); command.sign(nwkey); - p2pManager->broadCastAllZones(nullptr, &command, p2pRequestProcessor); + const BlockHeader* header = data->getHeader(); + uint16_t zone = header->getZone(); + p2pManager->bloadCastWithinZone(zone, nullptr, &command, p2pRequestProcessor); } // client notify diff --git a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp index c6a4246..f2122c4 100644 --- a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp +++ b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp @@ -103,6 +103,8 @@ void TransferedMinedReportCommandMessage::process(CentralProcessor *processor) { const BlockHeader* header = this->data->getHeader(); uint16_t zone = header->getZone(); + ExceptionThrower::throwExceptionIfCondition(zoneSelf != zone, L"The block must be within same zone.", __FILE__, __LINE__); + // [consensus]check if the block time is after PoSLimit { uint64_t height = header->getHeight(); @@ -112,14 +114,9 @@ void TransferedMinedReportCommandMessage::process(CentralProcessor *processor) { ExceptionThrower::throwExceptionIfCondition(blockGenerated->compareTo(limit) > 0, L"The block time must be after PoSLimit", __FILE__, __LINE__); } - - bool dataAdded = false; - if(zoneSelf == zone){ - NodeIdentifierSource* networkKey = requestProcessor->getNetworkKey(); - dataAdded = importBlock(memPool, ctrl, p2pManager, networkKey, logger, config); - }else{ - dataAdded = importHeader(memPool, ctrl, config); - } + // FIXME [multishard mine] + NodeIdentifierSource* networkKey = requestProcessor->getNetworkKey(); + bool dataAdded = importBlock(memPool, ctrl, p2pManager, networkKey, logger, config); // if the data contains new data, transfer it if(dataAdded){ @@ -130,7 +127,7 @@ void TransferedMinedReportCommandMessage::process(CentralProcessor *processor) { command.sign(networkKey); - p2pManager->broadCastAllZones(this->nodeId, &command, requestProcessor); + p2pManager->bloadCastWithinZone(zoneSelf, this->nodeId, &command, requestProcessor); } // client notify @@ -147,16 +144,6 @@ void TransferedMinedReportCommandMessage::process(CentralProcessor *processor) { } } -bool TransferedMinedReportCommandMessage::importHeader(MemoryPool* memPool, BlockchainController *ctrl, CodablecashSystemParam* config) { - const BlockHeader* header = this->data->getHeader(); - - // check the hashrate - BlockHeaderValidator validator(header, config, memPool, ctrl); - validator.validate(); - - return ctrl->addBlockHeader(header); -} - bool TransferedMinedReportCommandMessage::importBlock(MemoryPool* memPool, BlockchainController *ctrl, BlochchainP2pManager *p2pManager , NodeIdentifierSource* networkKey ,ISystemLogger* logger, CodablecashSystemParam* config) { const BlockHeader* header = this->data->getHeader(); diff --git a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.h b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.h index c351741..a1e140a 100644 --- a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.h +++ b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.h @@ -37,7 +37,6 @@ class TransferedMinedReportCommandMessage : public AbstractCentralProcessorComma virtual void process(CentralProcessor* processor); private: - bool importHeader(MemoryPool* memPool, BlockchainController* ctrl, CodablecashSystemParam* config); bool importBlock(MemoryPool* memPool, BlockchainController* ctrl, BlochchainP2pManager* p2pManager, NodeIdentifierSource* networkKey ,ISystemLogger* logger, CodablecashSystemParam* config); diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.h b/src_blockchain/bc_status_cache/BlockchainStatusCache.h index 06f68fc..1ff9579 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.h +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.h @@ -134,6 +134,10 @@ class BlockchainStatusCache : public IBlockchainEventListner { RemoteUtxoRepository* getRemoteUtxoRepository(uint16_t zone) const noexcept; + int getZoneListSize() const noexcept { + return this->zoneListSize; + } + private: void saveConfig(); void loadConfig(); diff --git a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp index 47f11c2..1e3d2cf 100644 --- a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp +++ b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp @@ -82,6 +82,7 @@ #include "bc_block_header_command/AbstractBlockHeaderCommand.h" +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" namespace codablecash { @@ -377,6 +378,17 @@ void StatusCacheContext::importInterChainCommunicationTransaction(const BlockHea uint8_t type = trx->getType(); if(type == AbstractBlockchainTransaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ this->numZones++; + +#ifdef __DEBUG__ + int zoneListSize = this->statusCache->getZoneListSize(); + assert(this->numZones <= zoneListSize); +#endif + + const NotifyZoneExtendRequestedTransaction* notifyTrx = dynamic_cast(trx); + + const UtxoId* cutxoId = notifyTrx->getCommandIdUtxo(); + uint64_t height = header->getHeight(); + this->remoteUtxos->consumeRemoteUtxo(cutxoId, height); // FIXME [multishard] } } diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp index 3961957..c8fa3e9 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp @@ -242,7 +242,10 @@ bool NotifyZoneExtendRequestedTransaction::validateOnAccept(MemPoolTransaction * } } - return ret; + RemoteUtxoDetector* remoteDetector = context->getRemoteUtxoDetector(); + bool detected = remoteDetector->isRemoteUtxoUsed(this->utxoId); + + return !detected && ret; } TrxValidationResult NotifyZoneExtendRequestedTransaction::validateFinal(const BlockHeader *header, MemPoolTransaction *memTrx, IStatusCacheContext *context) const { diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h index 5b6e7bf..cfcf57b 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h @@ -53,6 +53,10 @@ class NotifyZoneExtendRequestedTransaction : public AbstractInterChainCommunicat return this->newShardZone; } + const UtxoId* getCommandIdUtxo() const noexcept { + return this->utxoId; + } + private: // header info uint16_t zone; diff --git a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp index 6ec0bfa..71a29c3 100644 --- a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp +++ b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp @@ -329,6 +329,8 @@ ClientNodeHandshake* BlochchainP2pManager::getClientHandshakeByNodeId(const Node } void BlochchainP2pManager::broadCastWithinZone(uint16_t zoneSelf, const AbstractNodeCommand *command, P2pRequestProcessor* processor) { + ExceptionThrower::throwExceptionIfCondition(zoneSelf >= this->numZones, L"Zone does not exists", __FILE__, __LINE__); + StackUnlocker __lock(this->mutex, __FILE__, __LINE__); if(!this->end){ @@ -337,6 +339,8 @@ void BlochchainP2pManager::broadCastWithinZone(uint16_t zoneSelf, const Abstract } void BlochchainP2pManager::bloadCastWithinZone(uint16_t zoneSelf, const NodeIdentifier *excludeNodeId, const AbstractNodeCommand *command, P2pRequestProcessor *processor) { + ExceptionThrower::throwExceptionIfCondition(zoneSelf >= this->numZones, L"Zone does not exists", __FILE__, __LINE__); + StackUnlocker __lock(this->mutex, __FILE__, __LINE__); if(!this->end){ @@ -378,11 +382,22 @@ void BlochchainP2pManager::__broadCastWithinZone(uint16_t zone, const NodeIdenti } } +void BlochchainP2pManager::bloadCastHighPriorityWithinZone(uint16_t zoneSelf, const ArrayList *excludeNodeIds, + const AbstractConsensusNodeCommand *command, P2pRequestProcessor *processor) { + ExceptionThrower::throwExceptionIfCondition(zoneSelf >= this->numZones, L"Zone does not exists", __FILE__, __LINE__); + + StackUnlocker __lock(this->mutex, __FILE__, __LINE__); + + if(!this->end){ + P2pZone* p2pzone = this->zones->get(zoneSelf); + } +} + void BlochchainP2pManager::bloadCastHighPriorityAllZones(const ArrayList* excludeNodeIds, const AbstractConsensusNodeCommand *command, P2pRequestProcessor *processor) { StackUnlocker __lock(this->mutex, __FILE__, __LINE__); if(!this->end){ - int maxLoop = this->numZones; // this->zones->size(); + int maxLoop = this->zones->size(); for(int i = 0; i != maxLoop; ++i){ P2pZone* p2pzone = this->zones->get(i); diff --git a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h index 0f36a26..8cace00 100644 --- a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h +++ b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h @@ -58,6 +58,8 @@ class BlochchainP2pManager : public IPubsubConnectionListener { void bloadCastWithinZone(uint16_t zoneSelf, const NodeIdentifier* excludeNodeId, const AbstractNodeCommand* command, P2pRequestProcessor* processor); void broadCastAllZones(const NodeIdentifier* excludeNodeId, const AbstractNodeCommand* command, P2pRequestProcessor* processor); + + void bloadCastHighPriorityWithinZone(uint16_t zoneSelf, const ArrayList* excludeNodeIds, const AbstractConsensusNodeCommand* command, P2pRequestProcessor* processor); void bloadCastHighPriorityAllZones(const ArrayList* excludeNodeIds, const AbstractConsensusNodeCommand* command, P2pRequestProcessor* processor); void broadCastToClients(AbstractClientNotifyCommand* commnad, P2pRequestProcessor* processor); diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp index 555da35..d7eca83 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp @@ -40,6 +40,7 @@ #include "bc_network/NodeIdentifierSource.h" #include "bc_p2p_cmd_network/NodeNetworkInfo.h" +#include "bc_block_validator/BlockHeaderValidator.h" namespace codablecash { @@ -161,6 +162,11 @@ AbstractCommandResponse* ReportNonceCalculatedNodeCommand::executeAsNode(Blockch CodablecashSystemParam* config = inst->getCodablecashSystemParam(); updated = ctrl->registerBlockHeader4Limit(zone, this->header, config); + + // FIXME + if(ctrl->getZoneSelf() == 1){ + bool bl = zone == 1; + } } @@ -198,6 +204,19 @@ AbstractCommandResponse* ReportNonceCalculatedNodeCommand::executeAsNode(Blockch p2pManager->bloadCastHighPriorityAllZones(&list, &cmd, p2pRequestProcessor); } + if(updated){ + //FIXME[consensus] import header + uint16_t zone = this->header->getZone(); + uint16_t zoneSelf = ctrl->getZoneSelf(); + + if(zone != zoneSelf){ + MemoryPool *memPool = inst->getMemoryPool(); + CodablecashSystemParam* config = inst->getCodablecashSystemParam(); + const BlockHeader * header = this->header; + importHeader(memPool, ctrl, header, config); + } + } + return new OkPubsubResponse(); } @@ -206,4 +225,12 @@ void ReportNonceCalculatedNodeCommand::setHeader(const BlockHeader *header) { this->header = dynamic_cast(header->copyData()); } +bool ReportNonceCalculatedNodeCommand::importHeader(MemoryPool *memPool, BlockchainController *ctrl, const BlockHeader *header, CodablecashSystemParam *config) const { + // check the hashrate + BlockHeaderValidator validator(header, config, memPool, ctrl); + validator.validate(); + + return ctrl->addBlockHeader(header); +} + } /* namespace codablecash */ diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.h b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.h index 79973db..be58a1e 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.h +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.h @@ -13,6 +13,9 @@ namespace codablecash { class BlockHeader; +class MemoryPool; +class BlockchainController; +class CodablecashSystemParam; class ReportNonceCalculatedNodeCommand : public AbstractConsensusNodeCommand { public: @@ -34,6 +37,9 @@ class ReportNonceCalculatedNodeCommand : public AbstractConsensusNodeCommand { protected: virtual AbstractCommandResponse* executeAsNode(BlockchainNodeHandshake* nodeHandShake, CodablecashNodeInstance* inst, bool suspend) const; +private: + bool importHeader(MemoryPool* memPool, BlockchainController* ctrl, const BlockHeader* header, CodablecashSystemParam* config) const; + private: BlockHeader* header; diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp index 671fde9..ddc6a66 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/SendVoteTransactionNodeCommand.cpp @@ -106,6 +106,7 @@ AbstractCommandResponse* SendVoteTransactionNodeCommand::executeAsNode(Blockchai P2pRequestProcessor* processor = inst->getP2pRequestProcessor(); uint16_t zoneSelf = inst->getZoneSelf(); + bool alreadyHas = processor->hasHistory(this->data); if(!alreadyHas){ BlockchainController* ctrl = inst->getController(); @@ -155,7 +156,7 @@ AbstractCommandResponse* SendVoteTransactionNodeCommand::executeAsNode(Blockchai command.makeHistoryExcludeList(&list); // broadcast - manager->bloadCastHighPriorityAllZones(&list, &command, processor); + manager->bloadCastHighPriorityWithinZone(zoneSelf, &list, &command, processor); } { diff --git a/src_test/blockchain/finalizer/dummy/FinalizeMissDetectTicketCommandMessage.cpp b/src_test/blockchain/finalizer/dummy/FinalizeMissDetectTicketCommandMessage.cpp index 66fdbf0..99a18fb 100644 --- a/src_test/blockchain/finalizer/dummy/FinalizeMissDetectTicketCommandMessage.cpp +++ b/src_test/blockchain/finalizer/dummy/FinalizeMissDetectTicketCommandMessage.cpp @@ -146,6 +146,8 @@ void FinalizeMissDetectTicketCommandMessage::putTransaction(const BlockHeader *h // set time stamp of vote transaction VoteBlockTransaction* trx = new VoteBlockTransaction(); __STP(trx); + + trx->setZone(header2vote->getZone()); trx->setTicketUtxoId(utxoId, candidate->getTicletPrice(), candidate->getAddressDescriptor()); trx->setVoteBlockId(wrongHeaderId); // wrong id trx->setVoteBlockHeight(height2Vote); diff --git a/src_test/blockchain/multi_shard/test_multi_shard.cpp b/src_test/blockchain/multi_shard/test_multi_shard.cpp index f170d4c..5d74f81 100644 --- a/src_test/blockchain/multi_shard/test_multi_shard.cpp +++ b/src_test/blockchain/multi_shard/test_multi_shard.cpp @@ -279,7 +279,7 @@ TEST(TestMultiShardGroup, case01){ } } - testnet.waitForBlockHeight(0, 0, 12); + testnet.waitForBlockHeight(0, 0, 16); wallet->shutdownNetwork(); } diff --git a/src_test/blockchain/utils_testnet/MultizoneTestnet.cpp b/src_test/blockchain/utils_testnet/MultizoneTestnet.cpp index b21a1e9..b9d4991 100644 --- a/src_test/blockchain/utils_testnet/MultizoneTestnet.cpp +++ b/src_test/blockchain/utils_testnet/MultizoneTestnet.cpp @@ -92,7 +92,7 @@ void MultizoneTestnet::waitForBlockHeight(uint16_t zone, int index, uint64_t hei TestnetInstanceWrapper* inst = zoneNet->getInstance(index); uint64_t curHeight = inst->getHeight(); - while(curHeight < height){ + while(curHeight <= height){ Os::usleep(100 * 1000); curHeight = inst->getHeight(); } From 8092a1059e9027f9b33c95683e04cb9735fe57a0 Mon Sep 17 00:00:00 2001 From: iizuka Date: Fri, 31 Jul 2026 13:42:17 +0800 Subject: [PATCH 6/7] Multi zones --- .../bc_block_generator/BlockGenerator.cpp | 2 +- .../BlockHeaderCommandId.cpp | 8 +++ .../BlockHeaderCommandId.h | 2 + .../bc_block_validator/BlockValidator.cpp | 1 + .../TransferedMinedReportCommandMessage.cpp | 2 +- .../StatusCacheContext.cpp | 10 +-- .../FinalizedDataCache.cpp | 26 +++++++- .../bc_status_cache_data/FinalizedDataCache.h | 2 +- .../RemoteUtxoHeightIndex.cpp | 2 +- ...NotifyShardExtendRequestCommandMessage.cpp | 8 +++ .../NotifyZoneExtendRequestedTransaction.cpp | 4 +- .../ReportNonceCalculatedNodeCommand.cpp | 7 +- src_db/base_io/ByteBuffer.cpp | 4 +- src_db/base_io/ByteBuffer.h | 4 +- src_db/charsets/UTF8.cpp | 2 +- src_ext/yescrypt/yescrypt-opt.h | 2 +- src_test/p2p/parts/test_transfer_history.cpp | 65 +++++++++++++++++++ 17 files changed, 123 insertions(+), 28 deletions(-) diff --git a/src_blockchain/bc_block_generator/BlockGenerator.cpp b/src_blockchain/bc_block_generator/BlockGenerator.cpp index 04df39e..6da9dcb 100644 --- a/src_blockchain/bc_block_generator/BlockGenerator.cpp +++ b/src_blockchain/bc_block_generator/BlockGenerator.cpp @@ -256,7 +256,7 @@ void BlockGenerator::importInterChainCommunicationTransactions2Block(MemPoolTran uint8_t tyxType = trx->getType(); if(tyxType == AbstractInterChainCommunicationTansaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ - // FIXME [multishard] add RecognizedNewShardCommand to the header + // [multishard] add RecognizedNewShardCommand to the header NotifyZoneExtendRequestedTransaction* notifyTrx = dynamic_cast(trx); RecognizedNewShardCommand command; diff --git a/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp index aed19a9..98236cb 100644 --- a/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp +++ b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.cpp @@ -46,4 +46,12 @@ IBlockObject* BlockHeaderCommandId::copyData() const noexcept { return new BlockHeaderCommandId(*this); } +BlockHeaderCommandId* BlockHeaderCommandId::makeRandomHeaderId() { + ByteBuffer* buff = makeRandom16Bytes(); __STP(buff); + + BlockHeaderCommandId* blockHeaderId = new BlockHeaderCommandId((const char *)buff->array(), buff->capacity()); + + return blockHeaderId; +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h index 1f9a8cb..e92b243 100644 --- a/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h +++ b/src_blockchain/bc_block_header_command/BlockHeaderCommandId.h @@ -22,6 +22,8 @@ class BlockHeaderCommandId : public Abstract32BytesId { static BlockHeaderCommandId* fromBinary(ByteBuffer* in); virtual IBlockObject* copyData() const noexcept; + + static BlockHeaderCommandId* makeRandomHeaderId(); }; } /* namespace codablecash */ diff --git a/src_blockchain/bc_block_validator/BlockValidator.cpp b/src_blockchain/bc_block_validator/BlockValidator.cpp index 8ca35a8..d6a9417 100644 --- a/src_blockchain/bc_block_validator/BlockValidator.cpp +++ b/src_blockchain/bc_block_validator/BlockValidator.cpp @@ -132,6 +132,7 @@ void BlockValidator::validateHeaderCommand() { for(int i = 0; i != maxLoop; ++i){ AbstractBlockHeaderCommand* cmd = list->get(i); + // [multishard] validate NewShardZoneCommand* newShardCommand = dynamic_cast(cmd); if(newShardCommand != nullptr){ bool res = extValidator->validate(newShardCommand, context, this->ctrl); diff --git a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp index f2122c4..fd766dc 100644 --- a/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp +++ b/src_blockchain/bc_processor_cmd/TransferedMinedReportCommandMessage.cpp @@ -114,7 +114,7 @@ void TransferedMinedReportCommandMessage::process(CentralProcessor *processor) { ExceptionThrower::throwExceptionIfCondition(blockGenerated->compareTo(limit) > 0, L"The block time must be after PoSLimit", __FILE__, __LINE__); } - // FIXME [multishard mine] + // [multishard mine] NodeIdentifierSource* networkKey = requestProcessor->getNetworkKey(); bool dataAdded = importBlock(memPool, ctrl, p2pManager, networkKey, logger, config); diff --git a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp index 1e3d2cf..39c0c41 100644 --- a/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp +++ b/src_blockchain/bc_status_cache_context/StatusCacheContext.cpp @@ -377,19 +377,13 @@ void StatusCacheContext::importInterChainCommunicationTransaction(const BlockHea uint8_t type = trx->getType(); if(type == AbstractBlockchainTransaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ - this->numZones++; - -#ifdef __DEBUG__ - int zoneListSize = this->statusCache->getZoneListSize(); - assert(this->numZones <= zoneListSize); -#endif - const NotifyZoneExtendRequestedTransaction* notifyTrx = dynamic_cast(trx); + uint16_t newShard = notifyTrx->getNewShardZone(); + this->numZones = newShard + 1; const UtxoId* cutxoId = notifyTrx->getCommandIdUtxo(); uint64_t height = header->getHeight(); this->remoteUtxos->consumeRemoteUtxo(cutxoId, height); - // FIXME [multishard] } } diff --git a/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp b/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp index 1b7711f..0112e65 100644 --- a/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp +++ b/src_blockchain/bc_status_cache_data/FinalizedDataCache.cpp @@ -50,6 +50,7 @@ #include "bc/CodablecashSystemParam.h" +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" namespace codablecash { FinalizedDataCache::FinalizedDataCache(const File* baseDir) { @@ -115,7 +116,7 @@ void FinalizedDataCache::importBlockData(uint64_t finalizingHeight, const BlockH context->beginBlock(header, lockinManager, true); importControlTransactions(header, body, context); - importInterChainCommunicationTransactions(header, body); + importInterChainCommunicationTransactions(header, body, context); importBalanceTransactions(header, body); importSmartcontractTransactions(header, body); importRewardBaseTransactions(header, body); @@ -202,7 +203,7 @@ void FinalizedDataCache::importBalanceTransactions(const BlockHeader *header, co } } -void FinalizedDataCache::importInterChainCommunicationTransactions(const BlockHeader *header, const BlockBody *body) { +void FinalizedDataCache::importInterChainCommunicationTransactions(const BlockHeader *header, const BlockBody *body, IStatusCacheContext* context) { const ArrayList* list = body->getInterChainCommunicationTransactions(); int maxLoop = list->size(); @@ -210,6 +211,27 @@ void FinalizedDataCache::importInterChainCommunicationTransactions(const BlockHe AbstractInterChainCommunicationTansaction* trx = list->get(i); importTransactionUtxos(trx, header); + + uint8_t type = trx->getType(); + if(type == AbstractInterChainCommunicationTansaction::TRX_TYPE_ICC_ZONE_EXTEND_REQUESTED){ + uint16_t numZones = context->getNumZones(); + numZones++; + context->setNumZones(numZones); + +#ifdef __DEBUG__ + BlockchainStatusCache* statusCache = context->getBlockchainStatusCache(); + int zoneListSize = statusCache->getZoneListSize(); + assert(numZones <= zoneListSize); +#endif + + const NotifyZoneExtendRequestedTransaction* notifyTrx = dynamic_cast(trx); + + const UtxoId* cutxoId = notifyTrx->getCommandIdUtxo(); + uint64_t height = header->getHeight(); + + RemoteUtxoDetector* remoteUtxos = context->getRemoteUtxoDetector(); + remoteUtxos->consumeRemoteUtxo(cutxoId, height); + } } } diff --git a/src_blockchain/bc_status_cache_data/FinalizedDataCache.h b/src_blockchain/bc_status_cache_data/FinalizedDataCache.h index 9244638..cabd79b 100644 --- a/src_blockchain/bc_status_cache_data/FinalizedDataCache.h +++ b/src_blockchain/bc_status_cache_data/FinalizedDataCache.h @@ -71,7 +71,7 @@ class FinalizedDataCache { void importVoteBlockTransaction(const BlockHeader* header, const BlockBody* body, const VoteBlockTransaction* trx, IStatusCacheContext* context); void importBalanceTransactions(const BlockHeader* header, const BlockBody* body); - void importInterChainCommunicationTransactions(const BlockHeader* header, const BlockBody* body); + void importInterChainCommunicationTransactions(const BlockHeader* header, const BlockBody* body, IStatusCacheContext* context); void importSmartcontractTransactions(const BlockHeader* header, const BlockBody* body); void importRewardBaseTransactions(const BlockHeader* header, const BlockBody* body); diff --git a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp index f85bb41..93f44a5 100644 --- a/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp +++ b/src_blockchain/bc_status_cache_data/RemoteUtxoHeightIndex.cpp @@ -75,7 +75,7 @@ void RemoteUtxoHeightIndex::close() { void RemoteUtxoHeightIndex::addUtxo(uint64_t height, const UtxoId *utxoId) { ULongKey key(height); - RemoteUtxoHeightIndexData* data; + RemoteUtxoHeightIndexData* data = new RemoteUtxoHeightIndexData(); __STP(data); data->add(utxoId); this->btree->putData(&key, data); diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp index 1a6735e..09237c8 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.cpp @@ -90,6 +90,14 @@ void NotifyShardExtendRequestCommandMessage::process(CentralProcessor *processor // add into the history p2pRequestProcessor->addHistory(&data); +#ifdef __DEBUG__ + { + bool bl = p2pRequestProcessor->hasHistory(&data); + assert(bl == true); + } +#endif + + // broad cast ArrayList list; p2pManager->bloadCastHighPriorityAllZones(&list, &command, p2pRequestProcessor); diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp index c8fa3e9..e6ad9ab 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp @@ -138,7 +138,7 @@ void NotifyZoneExtendRequestedTransaction::build() { BinaryUtils::checkNotNull(this->commandId); { - int capacity = sizeof(uint8_t) + this->version->binarySize() + this->timestamp->binarySize(); + int capacity = sizeof(uint8_t) + this->version->binarySize() /*+ this->timestamp->binarySize()*/; capacity += sizeof(uint16_t) + sizeof(uint64_t) + this->headerId->binarySize() + sizeof(this->newShardZone) + this->commandId->binarySize(); @@ -146,7 +146,7 @@ void NotifyZoneExtendRequestedTransaction::build() { buff->put(getType()); this->version->toBinary(buff); - this->timestamp->toBinary(buff); + //this->timestamp->toBinary(buff); buff->putShort(this->zone); buff->putLong(this->height); diff --git a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp index d7eca83..e9b8c9e 100644 --- a/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp +++ b/src_blockchain_p2p/bc_p2p_cmd_node_consensus/ReportNonceCalculatedNodeCommand.cpp @@ -162,11 +162,6 @@ AbstractCommandResponse* ReportNonceCalculatedNodeCommand::executeAsNode(Blockch CodablecashSystemParam* config = inst->getCodablecashSystemParam(); updated = ctrl->registerBlockHeader4Limit(zone, this->header, config); - - // FIXME - if(ctrl->getZoneSelf() == 1){ - bool bl = zone == 1; - } } @@ -205,7 +200,7 @@ AbstractCommandResponse* ReportNonceCalculatedNodeCommand::executeAsNode(Blockch } if(updated){ - //FIXME[consensus] import header + // [consensus] import header uint16_t zone = this->header->getZone(); uint16_t zoneSelf = ctrl->getZoneSelf(); diff --git a/src_db/base_io/ByteBuffer.cpp b/src_db/base_io/ByteBuffer.cpp index be054b0..4cb3e75 100644 --- a/src_db/base_io/ByteBuffer.cpp +++ b/src_db/base_io/ByteBuffer.cpp @@ -118,7 +118,7 @@ int ByteBuffer::capacity() const noexcept { return this->cap; } -char ByteBuffer::get() noexcept(false) { +signed char ByteBuffer::get() noexcept(false) { if(remaining() < (int)sizeof(char)){ throw new BufferOverflowException(L"put(char b)", __FILE__, __LINE__); } @@ -126,7 +126,7 @@ char ByteBuffer::get() noexcept(false) { return data->get(this->pos++); } -char ByteBuffer::get(const int index) const noexcept(false) { +signed char ByteBuffer::get(const int index) const noexcept(false) { if(index + (int)sizeof(char) > lim){ throw new BufferOverflowException(L"put(int index, uint8_t b)", __FILE__, __LINE__); } diff --git a/src_db/base_io/ByteBuffer.h b/src_db/base_io/ByteBuffer.h index e2a9bb4..d602d05 100644 --- a/src_db/base_io/ByteBuffer.h +++ b/src_db/base_io/ByteBuffer.h @@ -40,8 +40,8 @@ class ByteBuffer { int limit() const noexcept; ByteBuffer* limit(const int limit) noexcept; int capacity() const noexcept; - char get() noexcept(false); - char get(const int index) const noexcept(false); + signed char get() noexcept(false); + signed char get(const int index) const noexcept(false); ByteBuffer* get(uint8_t* dest, int length) noexcept(false); ByteBuffer* get(uint8_t* dest, int off, int len) noexcept(false); diff --git a/src_db/charsets/UTF8.cpp b/src_db/charsets/UTF8.cpp index bad1f92..862e0a1 100644 --- a/src_db/charsets/UTF8.cpp +++ b/src_db/charsets/UTF8.cpp @@ -50,7 +50,7 @@ CoderResult UTF_8Converter::Decoder::decodeLoop(ByteBuffer* in, CharBuffer* out) ret = CoderResult::__OVERFLOW; goto END_LOOP; } - int jchar = in->get(); + int jchar = (signed char)in->get(); if(jchar < 0) { jchar = jchar & 0x7F; diff --git a/src_ext/yescrypt/yescrypt-opt.h b/src_ext/yescrypt/yescrypt-opt.h index c41ee12..5b640df 100644 --- a/src_ext/yescrypt/yescrypt-opt.h +++ b/src_ext/yescrypt/yescrypt-opt.h @@ -715,7 +715,7 @@ smix(uint64_t * B, size_t r, uint64_t N, uint32_t p, uint32_t t, * * Return 0 on success; or -1 on error. */ -static int +int yescrypt_kdf(const yescrypt_shared_t * shared, yescrypt_local_t * local, const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, size_t saltlen, diff --git a/src_test/p2p/parts/test_transfer_history.cpp b/src_test/p2p/parts/test_transfer_history.cpp index baec659..128ff55 100644 --- a/src_test/p2p/parts/test_transfer_history.cpp +++ b/src_test/p2p/parts/test_transfer_history.cpp @@ -10,6 +10,17 @@ #include "data_history/DataHistoryStore.h" +#include "base/StackRelease.h" + +#include "bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.h" + +#include "bc_block/BlockHeaderId.h" + +#include "bc_block_header_command/BlockHeaderCommandId.h" + +#include "data_history_data/TransactionTransferData.h" + +#include "data_history/TransferedDataId.h" using namespace codablecash; @@ -27,6 +38,60 @@ TEST(TestTransferHistoryGroup, case01){ _ST(File, baseDir, projectFolder.get(L"codable_home")) DataHistoryStore store(baseDir); + + store.createBlankDatabase(); + + { + store.open(); + + NotifyZoneExtendRequestedTransaction* trx = new NotifyZoneExtendRequestedTransaction(); __STP(trx); + { + BlockHeaderId* headerId = BlockHeaderId::makeRandomHeaderId(); __STP(headerId); + trx->setHeaderInfo(0, 1, headerId); + trx->setNewShardZone(1); + + BlockHeaderCommandId* commandId = BlockHeaderCommandId::makeRandomHeaderId(); __STP(commandId); + trx->setCommandId(commandId); + trx->build(); + } + + { + TransactionTransferData data; + data.setTransaction(trx); + TransferedDataId* dataId = data.getTransferedDataId(); __STP(dataId); + + store.add(dataId, &data); + + bool bl = store.hasId(dataId); + CHECK(bl == true); + } + + + NotifyZoneExtendRequestedTransaction* trx2 = nullptr; + { + int cap = trx->binarySize(); + ByteBuffer* buff = ByteBuffer::allocateWithEndian(cap, true); __STP(buff); + trx->toBinary(buff); + + buff->position(0); + AbstractBlockchainTransaction* _t = NotifyZoneExtendRequestedTransaction::createFromBinary(buff); + trx2 = dynamic_cast(_t); + } + __STP(trx2); + + { + TransactionTransferData data; + data.setTransaction(trx2); + TransferedDataId* dataId = data.getTransferedDataId(); __STP(dataId); + + bool bl = store.hasId(dataId); + CHECK(bl == true); + } + + + store.close(); + } + } From 5a7d5f331c56182a2284daf50b03dbd231085745 Mon Sep 17 00:00:00 2001 From: iizuka Date: Sat, 1 Aug 2026 14:59:12 +0800 Subject: [PATCH 7/7] Notify shard extention with header command --- .../AbstractBlockHeaderCommand.h | 4 +++ .../NewShardZoneCommand.cpp | 33 ++++++++++--------- .../bc_block_validator/BlockValidator.cpp | 9 ++--- .../AbstractShardExtentionValidator.cpp | 6 ++++ .../AbstractShardExtentionValidator.h | 7 ++-- .../bc_status_cache/BlockchainController.h | 3 ++ .../bc_status_cache/BlockchainStatusCache.cpp | 32 +++++++++--------- .../bc_status_cache/BlockchainStatusCache.h | 2 +- .../NotifyZoneExtendRequestedTransaction.cpp | 1 + .../bc_p2p/BlochchainP2pManager.cpp | 11 ++++--- .../bc_p2p/BlochchainP2pManager.h | 2 +- .../bc_p2p_processor/P2pRequestProcessor.cpp | 8 +++++ .../bc_p2p_processor/P2pRequestProcessor.h | 1 + .../multi_shard/NewShardValidator.cpp | 12 ++++++- .../multi_shard/test_multi_shard.cpp | 2 +- 15 files changed, 86 insertions(+), 47 deletions(-) diff --git a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h index a68d13b..545fe87 100644 --- a/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h +++ b/src_blockchain/bc_block_header_command/AbstractBlockHeaderCommand.h @@ -46,6 +46,10 @@ class AbstractBlockHeaderCommand : public alinous::IBlockObject { return this->commandId; } + uint16_t getType() const noexcept { + return this->type; + } + protected: uint16_t type; BlockHeaderCommandId* commandId; diff --git a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp index 83f8f36..e66d7b8 100644 --- a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp +++ b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp @@ -12,11 +12,10 @@ #include "bc_status_cache/BlockchainStatusCache.h" #include "bc_status_cache_extend_shard/NotifyShardExtendRequestCommandMessage.h" +#include "bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h" #include "bc_processor/CentralProcessor.h" -#include "bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h" - #include "bc_block/BlockHeaderId.h" #include "bc_block/BlockHeader.h" #include "bc_block/Block.h" @@ -25,6 +24,7 @@ #include "bc_p2p/BlochchainP2pManager.h" +#include "bc_p2p_processor/P2pRequestProcessor.h" namespace codablecash { @@ -88,39 +88,42 @@ void NewShardZoneCommand::setRequestingZone(uint16_t requestingZone) noexcept { void NewShardZoneCommand::onFinalize(const BlockHeader *header, BlockchainStatusCache *statusCache, CodablecashBlockchain *blockchain, ILockinManager *lockinManager, const CodablecashSystemParam *config) { + CentralProcessor* processor = blockchain->getProcessor(); + P2pRequestProcessor* p2pRequestProcessor = processor->getP2pRequestProcessor(); + + bool suspended = p2pRequestProcessor->isSuspended(); + uint16_t zoneSelf = statusCache->getZoneSelf(); // [multishard] create phisical store if(zoneSelf != this->newShardZone){ // other chains - statusCache->newZone(false); + statusCache->newZone(false, this->newShardZone); blockchain->addZone(this->newShardZone); BlochchainP2pManager* p2pManager = blockchain->getBlochchainP2pManager(); - p2pManager->incNumZones(); + p2pManager->incNumZones(this->newShardZone); }else{ // new shard chain - statusCache->newZone(true); + statusCache->newZone(true, this->newShardZone); blockchain->addZone(this->newShardZone); BlochchainP2pManager* p2pManager = blockchain->getBlochchainP2pManager(); - p2pManager->incNumZones(); + p2pManager->incNumZones(this->newShardZone); //[multishard] generate genesis block - CentralProcessor* processor = blockchain->getProcessor(); + if(!suspended){ + GenerateNewGenesisBlockCommandMessage* message = new GenerateNewGenesisBlockCommandMessage(); + message->setNewShardZone(this->newShardZone); + message->setGenesisBlock(this->genesisBlock); - GenerateNewGenesisBlockCommandMessage* message = new GenerateNewGenesisBlockCommandMessage(); - message->setNewShardZone(this->newShardZone); - message->setGenesisBlock(this->genesisBlock); - - processor->addCommandMessage(message); + processor->addCommandMessage(message); + } } // broad cast ICC - if(zoneSelf == this->requestingZone){ // make inter shard communication trx - CentralProcessor* processor = blockchain->getProcessor(); - + if(!suspended && zoneSelf == this->requestingZone){ // make inter shard communication trx NotifyShardExtendRequestCommandMessage* message = new NotifyShardExtendRequestCommandMessage(); message->setNewShardZone(this->newShardZone); message->setRequestingZone(this->requestingZone); diff --git a/src_blockchain/bc_block_validator/BlockValidator.cpp b/src_blockchain/bc_block_validator/BlockValidator.cpp index d6a9417..27e6e9b 100644 --- a/src_blockchain/bc_block_validator/BlockValidator.cpp +++ b/src_blockchain/bc_block_validator/BlockValidator.cpp @@ -133,12 +133,9 @@ void BlockValidator::validateHeaderCommand() { AbstractBlockHeaderCommand* cmd = list->get(i); // [multishard] validate - NewShardZoneCommand* newShardCommand = dynamic_cast(cmd); - if(newShardCommand != nullptr){ - bool res = extValidator->validate(newShardCommand, context, this->ctrl); - ExceptionThrower::throwExceptionIfCondition(res == false - , L"The header command to extend new shard is wrong.", __FILE__, __LINE__); - } + bool res = extValidator->validate(cmd, context, this->ctrl); + ExceptionThrower::throwExceptionIfCondition(res == false + , L"The header command to extend new shard is wrong.", __FILE__, __LINE__); } } } diff --git a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp index 800b49d..b60604e 100644 --- a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp +++ b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.cpp @@ -25,4 +25,10 @@ void AbstractShardExtentionValidator::setStatusCache(BlockchainStatusCache *stca this->statusCache = stcache; } +bool AbstractShardExtentionValidator::validateRecognizedNewShardCommand(RecognizedNewShardCommand *recognizeNewShardCommand, IStatusCacheContext *context, BlockchainController *ctrl) { + // FIXME [multishard] + + return true; +} + } /* namespace codablecash */ diff --git a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h index e2d69c7..f4c28f9 100644 --- a/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h +++ b/src_blockchain/bc_status_cache/AbstractShardExtentionValidator.h @@ -14,7 +14,7 @@ class BlockchainStatusCache; class IStatusCacheContext; class BlockchainController; class AbstractBlockHeaderCommand; - +class RecognizedNewShardCommand; class AbstractShardExtentionValidator { public: @@ -26,7 +26,10 @@ class AbstractShardExtentionValidator { void setStatusCache(BlockchainStatusCache* stcache) noexcept; - virtual bool validate(AbstractBlockHeaderCommand* newShardCommand, IStatusCacheContext* context, BlockchainController* ctrl) = 0; + virtual bool validate(AbstractBlockHeaderCommand* blockHeaderCommand, IStatusCacheContext* context, BlockchainController* ctrl) = 0; + +protected: + virtual bool validateRecognizedNewShardCommand(RecognizedNewShardCommand* recognizeNewShardCommand, IStatusCacheContext* context, BlockchainController* ctrl); private: BlockchainStatusCache* statusCache; diff --git a/src_blockchain/bc_status_cache/BlockchainController.h b/src_blockchain/bc_status_cache/BlockchainController.h index f3a5bef..5cffe58 100644 --- a/src_blockchain/bc_status_cache/BlockchainController.h +++ b/src_blockchain/bc_status_cache/BlockchainController.h @@ -81,6 +81,9 @@ class BlockchainController { CodablecashBlockchain* getCodablecashBlockchain() const noexcept { return this->blockchain; } + /*BlockchainStatusCache* getBlockchainStatusCache() const noexcept { + return this->statusCache; + }*/ void getSyncHeaderData(uint16_t zone, uint64_t offsetHeight, int limit, IBlockDetectCallback* callback); diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp b/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp index 2c87ea5..8485f7b 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.cpp @@ -130,26 +130,28 @@ void BlockchainStatusCache::close() { this->zoneList.reset(); } -void BlockchainStatusCache::newZone(bool headerOnly) { +void BlockchainStatusCache::newZone(bool headerOnly, uint16_t newShardZone) { uint16_t nZone = this->numZones; - // init blank - { - ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, nZone, headerOnly, this->logger, this->config); __STP(cache); - cache->initBlank(); - } - // open and add list - { - ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, nZone, this->logger, headerOnly, this->config); __STP(cache); - cache->open(); + if(nZone <= newShardZone){ + // init blank + { + ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, nZone, headerOnly, this->logger, this->config); __STP(cache); + cache->initBlank(); + } + // open and add list + { + ZoneStatusCache* cache = new ZoneStatusCache(this->baseDir, nZone, this->logger, headerOnly, this->config); __STP(cache); + cache->open(); - this->zoneList.addElement(__STP_MV(cache)); + this->zoneList.addElement(__STP_MV(cache)); - cache->setNumRecognizedZones(nZone + 1); // the zone cache recognizes itself - } + cache->setNumRecognizedZones(nZone + 1); // the zone cache recognizes itself + } - this->zoneListSize = this->zoneList.size(); - saveConfig(); + this->zoneListSize = this->zoneList.size(); + saveConfig(); + } } void BlockchainStatusCache::initCacheStatus(CodablecashBlockchain *blockchain) { diff --git a/src_blockchain/bc_status_cache/BlockchainStatusCache.h b/src_blockchain/bc_status_cache/BlockchainStatusCache.h index 1ff9579..2928c10 100644 --- a/src_blockchain/bc_status_cache/BlockchainStatusCache.h +++ b/src_blockchain/bc_status_cache/BlockchainStatusCache.h @@ -62,7 +62,7 @@ class BlockchainStatusCache : public IBlockchainEventListner { void close(); void initCacheStatus(CodablecashBlockchain* blockchain); - void newZone(bool headerOnly); + void newZone(bool headerOnly, uint16_t newShardZone); virtual void onBlockAdded(MemPoolTransaction* memTrx, const Block* block, CodablecashBlockchain* chain); virtual void postBlockAdded(const Block* block, CodablecashBlockchain* chain); diff --git a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp index e6ad9ab..02fa0b4 100644 --- a/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp +++ b/src_blockchain/bc_status_cache_extend_shard/NotifyZoneExtendRequestedTransaction.cpp @@ -65,6 +65,7 @@ NotifyZoneExtendRequestedTransaction::NotifyZoneExtendRequestedTransaction() : A NotifyZoneExtendRequestedTransaction::~NotifyZoneExtendRequestedTransaction() { delete this->headerId; + delete this->utxoId; delete this->commandId; } diff --git a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp index 71a29c3..95eca47 100644 --- a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp +++ b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.cpp @@ -158,13 +158,14 @@ void BlochchainP2pManager::init(int numZones) { } } -void BlochchainP2pManager::incNumZones() { +void BlochchainP2pManager::incNumZones(uint16_t newZone) { StackUnlocker __lock(this->mutex, __FILE__, __LINE__); - int newZone = this->numZones; - this->numZones++; - P2pZone* z = new P2pZone(newZone); - this->zones->addElement(z); + if(this->numZones <= newZone){ + this->numZones++; + P2pZone* z = new P2pZone(newZone); + this->zones->addElement(z); + } } void BlochchainP2pManager::onLoginHandshake(P2pHandshake *handshake, const LoginPubSubCommand *loginCommand, const UnicodeString* canonicalName) { diff --git a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h index 8cace00..dd2a545 100644 --- a/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h +++ b/src_blockchain_p2p/bc_p2p/BlochchainP2pManager.h @@ -36,7 +36,7 @@ class BlochchainP2pManager : public IPubsubConnectionListener { virtual ~BlochchainP2pManager(); void init(int numZones); - void incNumZones(); + void incNumZones(uint16_t newZone); void resetZones() noexcept; diff --git a/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.cpp b/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.cpp index f7367ef..287434d 100644 --- a/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.cpp +++ b/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.cpp @@ -168,6 +168,14 @@ void P2pRequestProcessor::putQueue(const PubSubId *pubsubId, const AbstructNodeQ } */ + +bool P2pRequestProcessor::isSuspended() const noexcept { + SynchronizedLock* lock = getSynchrinizedLock(); + StackUnlocker unlocker(lock, __FILE__, __LINE__); + + return __isSuspended(); +} + bool P2pRequestProcessor::__isSuspended() const noexcept { return this->queueProcessor->__isSuspended(); } diff --git a/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.h b/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.h index d773817..f47c6b7 100644 --- a/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.h +++ b/src_blockchain_p2p/bc_p2p_processor/P2pRequestProcessor.h @@ -83,6 +83,7 @@ class P2pRequestProcessor : public ICommandParameter { } //void setSuspend(bool suspend); + bool isSuspended() const noexcept; bool __isSuspended() const noexcept; SynchronizedLock* getSynchrinizedLock() const noexcept; diff --git a/src_test/blockchain/multi_shard/NewShardValidator.cpp b/src_test/blockchain/multi_shard/NewShardValidator.cpp index 120f752..ab8072e 100644 --- a/src_test/blockchain/multi_shard/NewShardValidator.cpp +++ b/src_test/blockchain/multi_shard/NewShardValidator.cpp @@ -11,6 +11,9 @@ #include "bc_status_cache_context/IStatusCacheContext.h" +#include "bc_block_header_command/AbstractBlockHeaderCommand.h" +#include "bc_block_header_command/RecognizedNewShardCommand.h" + namespace codablecash { NewShardValidator::NewShardValidator(const NewShardValidator &inst) : AbstractShardExtentionValidator(inst) { @@ -24,7 +27,14 @@ NewShardValidator::~NewShardValidator() { } -bool NewShardValidator::validate(AbstractBlockHeaderCommand *newShardCommand, IStatusCacheContext *context, BlockchainController *ctrl) { +bool NewShardValidator::validate(AbstractBlockHeaderCommand *headerCommand, IStatusCacheContext *context, BlockchainController *ctrl) { + uint16_t t = headerCommand->getType(); + + if(t == AbstractBlockHeaderCommand::RECOGNIZED_SHARD_COMMAND){ + RecognizedNewShardCommand* rcmd = dynamic_cast(headerCommand); + return validateRecognizedNewShardCommand(rcmd, context, ctrl); + } + uint16_t numZones = context->getNumZones(); int requestedZones = context->getRequestedNewShards(); diff --git a/src_test/blockchain/multi_shard/test_multi_shard.cpp b/src_test/blockchain/multi_shard/test_multi_shard.cpp index 5d74f81..3516261 100644 --- a/src_test/blockchain/multi_shard/test_multi_shard.cpp +++ b/src_test/blockchain/multi_shard/test_multi_shard.cpp @@ -273,7 +273,7 @@ TEST(TestMultiShardGroup, case01){ BlockchainController* ctrl = inst->getController(); uint64_t h = ctrl->getHeadHeight(0); - while(h <= 6){ + while(h < 6){ Os::usleep(100*1000); h = ctrl->getHeadHeight(0); }