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..d1915c7 100644 --- a/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp +++ b/src_blockchain/bc_block_header_command/NewShardZoneCommand.cpp @@ -7,45 +7,123 @@ #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); + message->setGenesisBlock(this->genesisBlock); + + 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..6745ac3 100644 --- a/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp +++ b/src_blockchain/bc_blockstore/CodablecashBlockchain.cpp @@ -143,6 +143,18 @@ void CodablecashBlockchain::initBlankchain() { initZonesStore(); } +void CodablecashBlockchain::addZone(uint16_t zone) { + if(zone == this->numZones){ + this->numZones++; + saveCondig(); + + initZone(zone); + + ZoneStore* store = this->zonesStore->get(zone); + store->open(); + } +} + void CodablecashBlockchain::initZonesStore() { int maxLoop = this->numZones; for(int i = 0; i != maxLoop; ++i){ @@ -326,4 +338,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..a155570 --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.cpp @@ -0,0 +1,43 @@ +/* + * GenerateNewGenesisBlockCommandMessage.cpp + * + * Created on: Jul 5, 2026 + * Author: iizuka + */ + +#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 new file mode 100644 index 0000000..2aa21d0 --- /dev/null +++ b/src_blockchain/bc_status_cache_extend_shard/GenerateNewGenesisBlockCommandMessage.h @@ -0,0 +1,36 @@ +/* + * 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 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 */ + +#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);