From 7bd5dcb1e6ba1601a6bf58fd17d002231c856e37 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 29 Oct 2020 14:56:54 +0100 Subject: [PATCH] Continued: - further rewrites from array to PackageData class - updated core framework MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../package/class_NetworkPackageHandler.php | 111 +++++++++--------- .../package/deliverable/class_PackageData.php | 72 ++++++++++++ .../fragmenter/class_PackageFragmenter.php | 2 +- .../hub/classes/tools/hub/class_HubTools.php | 14 ++- .../class_InvalidDataChecksumException.php | 4 +- core | 2 +- 6 files changed, 142 insertions(+), 63 deletions(-) diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index 11421344d..7461de36d 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -466,17 +466,16 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei /** * Checks whether the checksum (sometimes called "hash") is the same * - * @param $decodedContent Package raw content * @param $packageInstance An instance of a DeliverablePackage class * @return $isChecksumValid Whether the checksum is the same */ - private function isChecksumValid (array $decodedContent, DeliverablePackage $packageInstance) { + private function isChecksumValid (DeliverablePackage $packageInstance) { // Get checksum - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d,packageInstance=%s - CALLED!', count($decodedContent), $packageInstance->__toString())); - $checksum = $this->getHashFromContentSessionId($decodedContent, $packageInstance->getSenderAddress()); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); + $checksum = $this->getHashFromPackageSessionId($packageInstance); // Is it the same? - $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]); + $isChecksumValid = ($checksum == $packageInstance->getContentChecksum()); // Return it /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isChecksumValid=%d - EXIT!', intval($isChecksumValid))); @@ -526,19 +525,17 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei /** * "Getter" for hash from given content and sender's session id * - * @param $decodedContent Raw package content - * @param $sessionId Session id of the sender + * @param $packageInstance An instance of a DeliverablePackage class in an array * @return $hash Hash for given package content */ - public function getHashFromContentSessionId (array $decodedContent, $sessionId) { + public function getHashFromPackageSessionId (DeliverablePackage $packageInstance) { // Create the hash - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d,sessionId=%s - CALLED!', count($decodedContent), $sessionId)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); // @TODO md5() is very weak, but it needs to be fast - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR]); $hash = md5(sprintf(self::CONTENT_CHECKSUM_MASK, - $decodedContent[self::PACKAGE_CONTENT_MESSAGE], - $sessionId, - $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR] + $packageInstance->getContentMessage(), + $packageInstance->getSessionId(), + $packageInstance->getCompressorExtension() )); // And return it @@ -764,21 +761,13 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * Checks whether the hash of given package data is 'valid', here that * means it is the same or not. * - * @param $decodedArray An array with 'decoded' (explode() was mostly called) data + * @param $packageInstance An instance of a DeliverablePackage class * @return $isHashValid Whether the hash is valid * @todo Unfinished area, hashes are currently NOT fully supported */ - private function isPackageHashValid (array $decodedArray) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: decodedArray=' . print_r($decodedArray, TRUE) . ' - CALLED!'); - - // Make sure the required array elements are there - assert(isset($decodedArray[self::PACKAGE_CONTENT_SENDER])); - assert(isset($decodedArray[self::PACKAGE_CONTENT_MESSAGE])); - assert(isset($decodedArray[self::PACKAGE_CONTENT_HASH])); - assert(isset($decodedArray[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH])); - + private function isPackageHashValid (DeliverablePackage $packageInstance) { // Is the feature enabled? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { // Feature is not enabled, so hashes are always valid //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Feature "hubcoin_reward" not available, not checking hash. Returning TRUE ...'); @@ -786,9 +775,14 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei } // END - if // Check validity - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: senderId=' . $decodedArray[self::PACKAGE_CONTENT_SENDER] . ',message()=' . strlen($decodedArray[self::PACKAGE_CONTENT_MESSAGE]) . ',hash=' . $decodedArray[self::PACKAGE_CONTENT_HASH]); - //* DEBUG-DIE: */ die(__METHOD__ . ': decodedArray=' . print_r($decodedArray, TRUE)); - $isHashValid = FrameworkFeature::callFeature('hubcoin_reward', 'checkHash', array($decodedArray[self::PACKAGE_CONTENT_SENDER] . ':' . $decodedArray[self::PACKAGE_CONTENT_MESSAGE] . ':' . $this->determineSenderPrivateKeyHash($decodedArray), $decodedArray[self::PACKAGE_CONTENT_HASH])); + $isHashValid = FrameworkFeature::callFeature('hubcoin_reward', 'checkHash', array( + sprintf('%s:%s:%s', + $packageInstance->getSenderId(), + $packageInstance->getPackageContent(), + $this->determineSenderPrivateKeyHash($packageInstance) + ), + $packageInstance->getContentHash(), + )); // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: isHashValid=' . intval($isHashValid) . ' - EXIT!'); @@ -1388,7 +1382,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Assert on count (should be always 6) /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedArray()=%d', count($decodedArray))); - /* DEBUG-DIE: */ die(sprintf('[%s:%d]: decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE))); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE))); if (count($decodedArray) != self::DECODED_DATA_ARRAY_SIZE) { // Count of array elements not expected throw new InvalidArgumentException(sprintf('decodedArray()=%d does not match expected size %d. rawPackageContent=%s', @@ -1454,41 +1448,32 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei )); } - /* - * Convert the indexed array into an associative array. This is much - * better to remember than plain numbers, isn't it? - */ - $decodedMessage = array( - // Compressor's extension used to compress the data - self::PACKAGE_CONTENT_COMPRESSOR => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION], - // Package data (aka "message") in BASE64-decoded form but still compressed - self::PACKAGE_CONTENT_MESSAGE => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]), - // Tags as an indexed array for "tagging" the message - self::PACKAGE_CONTENT_TAGS => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]), - // Checksum of the _decoded_ data - self::PACKAGE_CONTENT_CHECKSUM => $decodedContent[self::INDEX_CHECKSUM], - // Sender's id - self::PACKAGE_CONTENT_SENDER => $packageInstance->getSenderAddress(), - // Hash from decoded raw data - self::PACKAGE_CONTENT_HASH => $packageInstance->getHash(), - // Hash of private key - self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $packageInstance->getSenderPrivateKeyHash() - ); + // Copy data to package instance + $packageInstance->setCompressorExtension($decodedContent[self::INDEX_COMPRESSOR_EXTENSION]); + // Package data (aka "message") in BASE64-decoded form but still compressed + $packageInstance->setContentMessage(base64_decode($decodedContent[self::INDEX_PACKAGE_DATA])); + // Tags as an indexed array for "tagging" the message + $packageInstance->setContentHash(explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS])); + // Checksum of the _decoded_ data + $packageInstance->setContentChecksum($decodedContent[self::INDEX_CHECKSUM]); // Is the checksum valid? - if (!$this->isChecksumValid($decodedMessage, $packageInstance)) { + if (!$this->isChecksumValid($packageInstance)) { // Is not the same, so throw an exception here - throw new InvalidDataChecksumException(array($this, $decodedMessage, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); + throw new InvalidDataChecksumException(array($this, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); } // END - if /* * The checksum is the same, then it can be decompressed safely. The * original message is at this point fully decoded. */ - $decodedMessage[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedMessage[self::PACKAGE_CONTENT_MESSAGE]); + $packageInstance->setContentMessage($this->getCompressorInstance()->decompressStream($packageInstance->getContentMessage())); // And push it on the next stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedMessage); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $packageInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!'); } /** @@ -1498,9 +1483,11 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei */ public function isNewMessageArrived () { // Determine if the stack is not empty + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!'); $hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE)); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: hasArrived=%d - EXIT!', intval($hasArrived))); return $hasArrived; } @@ -1512,31 +1499,35 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei */ public function handleNewlyArrivedMessage () { // Make sure there is at least one message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!'); assert($this->isNewMessageArrived()); // Get it from the stacker, it is the full array with the decoded message - $decodedMessage = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); + $packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); // Generate the hash of comparing it - if (!$this->isPackageHashValid($decodedMessage)) { + if (!$this->isPackageHashValid($packageInstance)) { // Is not valid, so throw an exception here exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10)); } // END - if // Now get a filter chain back from factory with given tags array - $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedMessage[self::PACKAGE_CONTENT_TAGS]); + $chainInstance = PackageFilterChainFactory::createChainByTagsArray($packageInstance->getContentHash()); /* * Process the message through all filters, note that all other - * elements from $decodedMessage are no longer needed. + * elements from $packageInstance are no longer needed. */ - $chainInstance->processMessage($decodedMessage, $this); + $chainInstance->processMessage($packageInstance, $this); /* * Post-processing of message data (this won't remove the message from * the stack). */ $chainInstance->postProcessMessage($this); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!'); } /** @@ -1546,9 +1537,11 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei */ public function isProcessedMessagePending () { // Check it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!'); $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE)); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isPending=%d - EXIT!', intval($isPending))); return $isPending; } @@ -1559,6 +1552,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei */ public function handleProcessedMessage () { // Get it from the stacker, it is the full array with the processed message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!'); $messageArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE); // Add type for later easier handling @@ -1575,6 +1569,9 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Post-handling of message data $handlerInstance->postHandleMessageData($messageArray, $this); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!'); } /** diff --git a/application/hub/classes/package/deliverable/class_PackageData.php b/application/hub/classes/package/deliverable/class_PackageData.php index 3ea63c3ae..c8c8a14dc 100644 --- a/application/hub/classes/package/deliverable/class_PackageData.php +++ b/application/hub/classes/package/deliverable/class_PackageData.php @@ -41,6 +41,16 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera */ private $contentHash; + /** + * Content message + */ + private $contentMessage; + + /** + * Content checksum + */ + private $contentChecksum; + /** * Package content */ @@ -96,6 +106,11 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera */ private $errorCode; + /** + * Compressor extension + */ + private $compressorExtension; + /** * Protected constructor * @@ -290,6 +305,44 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera $this->contentHash = $contentHash; } + /** + * Getter for content message + * + * @return $contentMessage Content message + */ + public function getContentMessage () { + return $this->contentMessage; + } + + /** + * Setter for content message + * + * @param $contentMessage Content message + * @return void + */ + public function setContentMessage ($contentMessage) { + $this->contentMessage = $contentMessage; + } + + /** + * Getter for content checksum + * + * @return $contentChecksum Content checksum + */ + public function getContentChecksum () { + return $this->contentChecksum; + } + + /** + * Setter for content checksum + * + * @param $contentChecksum Content checksum + * @return void + */ + public function setContentChecksum ($contentChecksum) { + $this->contentChecksum = $contentChecksum; + } + /** * Setter for private key hash * @@ -347,6 +400,25 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera $this->errorCode = $errorCode; } + /** + * Getter for compressor extension + * + * @return $compressorExtension Compressor extension + */ + public function getCompressorExtension () { + return $this->compressorExtension; + } + + /** + * Setter for compressor extension + * + * @param $compressorExtension Compressor extension + * @return void + */ + public function setCompressorExtension ($compressorExtension) { + $this->compressorExtension = $compressorExtension; + } + /** * Collecting "getter" for all class fields as array * diff --git a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php index b15b12621..ac81171ee 100644 --- a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php @@ -500,7 +500,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera if (!$this->isPackageProcessed($packageInstance)) { // First put it the right way together again, implode on getAllAsArray() won't work! /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: Package is not processed., getting all raw data ...'); - /* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance->allAsArray=%s', __METHOD__, __LINE__, print_r($packageInstance->getAllAsArray(), TRUE))); + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance->allAsArray=%s', __METHOD__, __LINE__, print_r($packageInstance->getAllAsArray(), TRUE))); $rawData = sprintf('%s%s%d%s%s%s%s%s%s%s%s%s%s', $packageInstance->getSenderAddress(), NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, diff --git a/application/hub/classes/tools/hub/class_HubTools.php b/application/hub/classes/tools/hub/class_HubTools.php index fba77c3fa..3e15261f1 100644 --- a/application/hub/classes/tools/hub/class_HubTools.php +++ b/application/hub/classes/tools/hub/class_HubTools.php @@ -167,8 +167,18 @@ class HubTools extends BaseHubSystem { $selfInstance = self::getSelfInstance(); // And ask it for session id by given Universal Node Locator - $recipient = DhtObjectFactory::createDhtInstance('node')->findNodeByUniversalNodeLocator($locatorInstance); - die(__METHOD__.':recipient='.print_r($recipient, TRUE)); + $recipientData = DhtObjectFactory::createDhtInstance('node')->findNodeByUniversalNodeLocator($locatorInstance); + + // Init variable + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: recipientData=%s', __METHOD__, __LINE__, print_r($recipientData, TRUE))); + $sessionId = NULL; + + // Is an entry found? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: recipientData()=%d', count($recipientData))); + if (count($recipientData) > 0) { + // Get session id + $sessionId = $recipientData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID]; + } // Return result /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - EXIT!', $sessionId)); diff --git a/application/hub/exceptions/package/class_InvalidDataChecksumException.php b/application/hub/exceptions/package/class_InvalidDataChecksumException.php index c1874b130..c6dcab96f 100644 --- a/application/hub/exceptions/package/class_InvalidDataChecksumException.php +++ b/application/hub/exceptions/package/class_InvalidDataChecksumException.php @@ -44,8 +44,8 @@ class InvalidDataChecksumException extends FrameworkException { $message = sprintf('[%s:%d] The checksum %s doesnt match the checksum of the content: %s', $messageArray[0]->__toString(), $this->getLine(), - $messageArray[0]->getHashFromContentSessionId($messageArray[1], $messageArray[2]->getSenderAddress()), - $messageArray[1][NetworkPackageHandler::PACKAGE_CONTENT_CHECKSUM] + $messageArray[0]->getHashFromPackageSessionId($messageArray[1]), + $messageArray[1]->getContentHash() ); // Call parent exception constructor diff --git a/core b/core index b2ea555ca..9cd89d14d 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit b2ea555ca8062721bba3bc565770a3da27577ced +Subproject commit 9cd89d14df07b8126300c9535045c779e3fd445c -- 2.39.5