]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/package/class_NetworkPackage.php
Better name for the miner: It "mines" for Hubcoins.
[hub.git] / application / hub / main / package / class_NetworkPackage.php
index 1de50c8643384e758c4aa1f77f8aa1018e3c7df1..4c5dc1b10defb8e8738e9543ffe4444e48040118 100644 (file)
@@ -117,8 +117,11 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        /**
         * Constants for message data array
         */
-       const MESSAGE_ARRAY_DATA = 'message_data';
-       const MESSAGE_ARRAY_TYPE = 'message_type';
+       const MESSAGE_ARRAY_DATA   = 'message_data';
+       const MESSAGE_ARRAY_TYPE   = 'message_type';
+       const MESSAGE_ARRAY_SENDER = 'message_sender';
+       const MESSAGE_ARRAY_HASH   = 'message_hash';
+       const MESSAGE_ARRAY_TAGS   = 'message_tags';
 
        /**
         * Generic answer status field
@@ -650,9 +653,19 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         *
         * @param       $content        Raw package data
         * @param       $senderId       Sender id to generate a hash for
-        * @return      $hash   Hash as hex-encoded string
+        * @return      $hash           Hash as hex-encoded string
         */
        private function generatePackageHash ($content, $senderId) {
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: content()=' . strlen($content) . ',senderId=' . $senderId . ' - CALLED!');
+
+               // Is the feature enabled?
+               if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) {
+                       // Feature is not enabled
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Feature "hubcoin_reward" not available, not generating package hash. Returning NULL ...');
+                       return NULL;
+               } // END - if
+
                // Fake array
                $data = array(
                        self::PACKAGE_CONTENT_SENDER           => $senderId,
@@ -662,7 +675,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        
                // Hash content and sender id together, use scrypt
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: senderId=' . $senderId . ',content()=' . strlen($content));
-               $hash = Scrypt::hashScrypt($senderId . ':' . $content . ':' . $this->determineSenderPrivateKeyHash($data));
+               $hash = FrameworkFeature::callFeature('hubcoin_reward', 'generateHash', array($senderId . ':' . $content . ':' . $this->determineSenderPrivateKeyHash($data)));
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ' - EXIT!');
 
                // Return it
                return $hash;
@@ -677,13 +693,28 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @todo        Unfinished area, hashes are currently NOT fully supported
         */
        private function isPackageHashValid (array $decodedArray) {
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: 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]));
+
+               // Is the feature enabled?
+               if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) {
+                       // Feature is not enabled, so hashes are always valid
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: Feature "hubcoin_reward" not available, not checking hash. Returning TRUE ...');
+                       return TRUE;
+               } // END - if
+
                // Check validity
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: senderId=' . $decodedArray[self::PACKAGE_CONTENT_SENDER] . ',message()=' . strlen($decodedArray[self::PACKAGE_CONTENT_MESSAGE]));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: 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 = Scrypt::checkScrypt($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($decodedArray[self::PACKAGE_CONTENT_SENDER] . ':' . $decodedArray[self::PACKAGE_CONTENT_MESSAGE] . ':' . $this->determineSenderPrivateKeyHash($decodedArray), $decodedArray[self::PACKAGE_CONTENT_HASH]));
 
                // Return it
-               //* DEBUG-DIE: */ die(__METHOD__ . ': isHashValid=' . intval($isHashValid) . ',decodedArray=' . print_r($decodedArray, TRUE));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: isHashValid=' . intval($isHashValid) . ' - EXIT!');
                return $isHashValid;
        }
 
@@ -1334,6 +1365,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         * @todo        Implement verification of all sent tags here?
         */
        public function handleNewlyArrivedMessage () {
+               // Make sure there is at least one message
+               assert($this->isNewMessageArrived());
+
                // Get it from the stacker, it is the full array with the decoded message
                $decodedContent = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
 
@@ -1351,6 +1385,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * elements from $decodedContent are no longer needed.
                 */
                $chainInstance->processMessage($decodedContent, $this);
+
+               /*
+                * Post-processing of message data (this won't remote the message from
+                * the stack).
+                */
+               $chainInstance->postProcessMessage($this);
        }
 
        /**
@@ -1386,37 +1426,52 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
 
                // Handle message data
                $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this);
+
+               // Post-handling of message data
+               $handlerInstance->postHandleMessageData($messageArray, $this);
        }
 
        /**
         * Feeds the hash and sender (as recipient for the 'sender' reward) to the
         * miner's queue, unless the message is not a "reward claim" message as this
         * leads to an endless loop. You may wish to run the miner to get some
-        * reward ("HubCoins") for "mining" this hash.
+        * reward ("Hubcoins") for "mining" this hash.
         *
-        * @param       $decodedDataArray       Array with decoded message
+        * @param       $messageData    Array with message data
         * @return      void
         * @todo        ~10% done?
         */
-       public function feedHashToMiner (array $decodedDataArray) {
+       public function feedHashToMiner (array $messageData) {
+               // Is the feature enabled?
+               if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) {
+                       /*
+                        * Feature is not enabled, don't feed the hash to the miner as it
+                        *may be invalid.
+                        */
+                       return;
+               } // END - if
+
                // Make sure the required elements are there
-               assert(isset($decodedDataArray[self::PACKAGE_CONTENT_SENDER]));
-               assert(isset($decodedDataArray[self::PACKAGE_CONTENT_HASH]));
-               assert(isset($decodedDataArray[self::PACKAGE_CONTENT_TAGS]));
+               assert(isset($messageData[self::MESSAGE_ARRAY_SENDER]));
+               assert(isset($messageData[self::MESSAGE_ARRAY_HASH]));
+               assert(isset($messageData[self::MESSAGE_ARRAY_TAGS]));
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: messageData=' . print_r($messageData, TRUE));
 
                // Resolve session id ('sender' is a session id) into node id
-               $nodeId = HubTools::resolveNodeIdBySessionId($decodedDataArray[self::PACKAGE_CONTENT_SENDER]);
+               $nodeId = HubTools::resolveNodeIdBySessionId($messageData[self::MESSAGE_ARRAY_SENDER]);
 
                // Is 'claim_reward' the message type?
-               if (in_array('claim_reward', $decodedDataArray[self::PACKAGE_CONTENT_TAGS])) {
+               if (in_array('claim_reward', $messageData[self::MESSAGE_ARRAY_TAGS])) {
                        /*
-                        * Then don't feed this message to the miner as this causes and
+                        * Then don't feed this message to the miner as this causes an
                         * endless loop of mining.
                         */
                        return;
                } // END - if
 
-               $this->partialStub('@TODO nodeId=' . $nodeId . ',decodedDataArray=' . print_r($decodedDataArray, TRUE));
+               $this->partialStub('@TODO nodeId=' . $nodeId . ',messageData=' . print_r($messageData, TRUE));
        }
 }