]> 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 688b978d28e50ce5a5a5d074ff4f5f48f989498b..4c5dc1b10defb8e8738e9543ffe4444e48040118 100644 (file)
@@ -73,35 +73,38 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        /**
         * Array indexes for raw package array
         */
-       const INDEX_PACKAGE_SENDER    = 0;
-       const INDEX_PACKAGE_RECIPIENT = 1;
-       const INDEX_PACKAGE_CONTENT   = 2;
-       const INDEX_PACKAGE_STATUS    = 3;
-       const INDEX_PACKAGE_HASH      = 4;
+       const INDEX_PACKAGE_SENDER           = 0;
+       const INDEX_PACKAGE_RECIPIENT        = 1;
+       const INDEX_PACKAGE_CONTENT          = 2;
+       const INDEX_PACKAGE_STATUS           = 3;
+       const INDEX_PACKAGE_HASH             = 4;
+       const INDEX_PACKAGE_PRIVATE_KEY_HASH = 5;
 
        /**
         * Size of the decoded data array
         */
-       const DECODED_DATA_ARRAY_SIZE = 5;
+       const DECODED_DATA_ARRAY_SIZE = 6;
 
        /**
         * Named array elements for decoded package content
         */
-       const PACKAGE_CONTENT_EXTENSION = 'compressor';
-       const PACKAGE_CONTENT_MESSAGE   = 'message';
-       const PACKAGE_CONTENT_TAGS      = 'tags';
-       const PACKAGE_CONTENT_CHECKSUM  = 'checksum';
-       const PACKAGE_CONTENT_SENDER    = 'sender';
-       const PACKAGE_CONTENT_HASH      = 'hash';
+       const PACKAGE_CONTENT_EXTENSION        = 'compressor';
+       const PACKAGE_CONTENT_MESSAGE          = 'message';
+       const PACKAGE_CONTENT_TAGS             = 'tags';
+       const PACKAGE_CONTENT_CHECKSUM         = 'checksum';
+       const PACKAGE_CONTENT_SENDER           = 'sender';
+       const PACKAGE_CONTENT_HASH             = 'hash';
+       const PACKAGE_CONTENT_PRIVATE_KEY_HASH = 'pkhash';
 
        /**
         * Named array elements for package data
         */
-       const PACKAGE_DATA_SENDER    = 'sender';
-       const PACKAGE_DATA_RECIPIENT = 'recipient';
-       const PACKAGE_DATA_CONTENT   = 'content';
-       const PACKAGE_DATA_STATUS    = 'status';
-       const PACKAGE_DATA_HASH      = 'hash';
+       const PACKAGE_DATA_SENDER           = 'sender';
+       const PACKAGE_DATA_RECIPIENT        = 'recipient';
+       const PACKAGE_DATA_CONTENT          = 'content';
+       const PACKAGE_DATA_STATUS           = 'status';
+       const PACKAGE_DATA_HASH             = 'hash';
+       const PACKAGE_DATA_PRIVATE_KEY_HASH = 'pkhash';
 
        /**
         * All package status
@@ -114,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
@@ -359,12 +365,11 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                } // END - if
 
                // Make sure the requested element is there
-               // @TODO Wrong hash!!!!
-               /* DEBUG-DIE */ die('decodedData=' . print_r($decodedData, TRUE));
-               assert(isset($decodedData[self::PACKAGE_CONTENT_HASH]));
+               //* DEBUG-DIE */ die('decodedData=' . print_r($decodedData, TRUE));
+               assert(isset($decodedData[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH]));
 
                // There is no DHT entry so, accept the hash from decoded data
-               return $decodedData[self::PACKAGE_CONTENT_HASH];
+               return $decodedData[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH];
        }
 
        /**
@@ -648,18 +653,32 @@ 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,
-                       self::PACKAGE_CONTENT_CONTENT => $content,
-                       self::PACKAGE_CONTENT_HASH    => ''
+                       self::PACKAGE_CONTENT_SENDER           => $senderId,
+                       self::PACKAGE_CONTENT_MESSAGE          => $content,
+                       self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => ''
                );
        
                // Hash content and sender id together, use scrypt
-               $hash = Scrypt::hashScrypt($senderId . ':' . $content . ':' . $this->determineSenderPrivateKeyHash($data));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NETWORK-PACKAGE[' . __METHOD__ . ':' . __LINE__ . ']: senderId=' . $senderId . ',content()=' . strlen($content));
+               $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;
@@ -674,12 +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]) . '),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_CONTENT] . ':' . $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;
        }
 
@@ -700,7 +735,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('content(' . strlen($content) . ')=' . $content);
 
                // ... and compress it
-               $content = $this->getCompressorInstance()->compressStream($content);
+               $compressed = $this->getCompressorInstance()->compressStream($content);
 
                // Add magic in front of it and hash behind it, including BASE64 encoding
                $packageContent = sprintf(self::PACKAGE_MASK,
@@ -708,8 +743,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        $this->getCompressorInstance()->getCompressorExtension(),
                        // - separator
                        self::PACKAGE_MASK_SEPARATOR,
-                       // 2.) Raw package content, encoded with BASE64
-                       base64_encode($content),
+                       // 2.) Compressed raw package content, encoded with BASE64
+                       base64_encode($compressed),
                        // - separator
                        self::PACKAGE_MASK_SEPARATOR,
                        // 3.) Tags
@@ -717,7 +752,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        // - separator
                        self::PACKAGE_MASK_SEPARATOR,
                        // 4.) Checksum
-                       $this->getHashFromContent($content)
+                       $this->getHashFromContent($compressed)
                );
 
                // Debug message
@@ -725,11 +760,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
 
                // Now prepare the temporary array and push it on the 'undeclared' stack
                $this->getStackInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
-                       self::PACKAGE_DATA_SENDER    => $this->getSessionId(),
-                       self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
-                       self::PACKAGE_DATA_CONTENT   => $packageContent,
-                       self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_NEW,
-                       self::PACKAGE_DATA_HASH      => $this->generatePackageHash($packageContent, $this->getSessionId())
+                       self::PACKAGE_DATA_SENDER           => $this->getSessionId(),
+                       self::PACKAGE_DATA_RECIPIENT        => $helperInstance->getRecipientType(),
+                       self::PACKAGE_DATA_CONTENT          => $packageContent,
+                       self::PACKAGE_DATA_STATUS           => self::PACKAGE_STATUS_NEW,
+                       self::PACKAGE_DATA_HASH             => $this->generatePackageHash($content, $this->getSessionId()),
+                       self::PACKAGE_DATA_PRIVATE_KEY_HASH => $this->getPrivateKeyHash(),
                ));
 
                // Debug message
@@ -1241,11 +1277,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * Create 'decodedData' array with all assoziative array elements.
                 */
                $decodedData = array(
-                       self::PACKAGE_DATA_SENDER    => $decodedArray[self::INDEX_PACKAGE_SENDER],
-                       self::PACKAGE_DATA_RECIPIENT => $decodedArray[self::INDEX_PACKAGE_RECIPIENT],
-                       self::PACKAGE_DATA_CONTENT   => $decodedArray[self::INDEX_PACKAGE_CONTENT],
-                       self::PACKAGE_DATA_STATUS    => self::PACKAGE_STATUS_DECODED,
-                       self::PACKAGE_DATA_HASH      => $decodedArray[self::INDEX_PACKAGE_HASH]
+                       self::PACKAGE_DATA_SENDER           => $decodedArray[self::INDEX_PACKAGE_SENDER],
+                       self::PACKAGE_DATA_RECIPIENT        => $decodedArray[self::INDEX_PACKAGE_RECIPIENT],
+                       self::PACKAGE_DATA_CONTENT          => $decodedArray[self::INDEX_PACKAGE_CONTENT],
+                       self::PACKAGE_DATA_STATUS           => self::PACKAGE_STATUS_DECODED,
+                       self::PACKAGE_DATA_HASH             => $decodedArray[self::INDEX_PACKAGE_HASH],
+                       self::PACKAGE_DATA_PRIVATE_KEY_HASH => $decodedArray[self::INDEX_PACKAGE_PRIVATE_KEY_HASH]
                );
 
                // And return it
@@ -1277,17 +1314,19 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 */
                $decodedContent = array(
                        // Compressor's extension used to compress the data
-                       self::PACKAGE_CONTENT_EXTENSION => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION],
+                       self::PACKAGE_CONTENT_EXTENSION        => $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]),
+                       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]),
+                       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],
+                       self::PACKAGE_CONTENT_CHECKSUM         => $decodedContent[self::INDEX_CHECKSUM],
                        // Sender's id
-                       self::PACKAGE_CONTENT_SENDER    => $decodedData[self::PACKAGE_DATA_SENDER],
+                       self::PACKAGE_CONTENT_SENDER           => $decodedData[self::PACKAGE_DATA_SENDER],
                        // Hash from decoded raw data
-                       self::PACKAGE_CONTENT_HASH      => $decodedData[self::PACKAGE_DATA_HASH]
+                       self::PACKAGE_CONTENT_HASH             => $decodedData[self::PACKAGE_DATA_HASH],
+                       // Hash of private key
+                       self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $decodedData[self::PACKAGE_DATA_PRIVATE_KEY_HASH]
                );
 
                // Is the checksum valid?
@@ -1326,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);
 
@@ -1343,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);
        }
 
        /**
@@ -1378,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));
        }
 }