From a7bb43c310a5573da0331e32899b210511036f03 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Mon, 9 Nov 2020 15:44:27 +0000 Subject: [PATCH] test and fix errors with pauls modified example (I have filled in real transaction data --- .../JsonRequestHandlerController.php | 137 +++++++++++++++++- src/Model/Transactions/Record.php | 40 +++-- 2 files changed, 163 insertions(+), 14 deletions(-) diff --git a/src/Controller/JsonRequestHandlerController.php b/src/Controller/JsonRequestHandlerController.php index 533240971..3d62aeed2 100644 --- a/src/Controller/JsonRequestHandlerController.php +++ b/src/Controller/JsonRequestHandlerController.php @@ -82,10 +82,143 @@ class JsonRequestHandlerController extends AppController { } $group_alias = Configure::read('GroupAlias'); - $result = $this->JsonRpcRequestClient->request('getTransactions', ['groupAlias' => $group_alias, 'lastKnownSequenceNumber' => $last_transaction_id]); + $result = (array)$this->JsonRpcRequestClient->request('getTransactions', ['groupAlias' => $group_alias, 'lastKnownSequenceNumber' => $last_transaction_id]); if(isset($result['state']) && $result['state'] == 'error') { return $this->returnJson(['state' => 'error', 'msg' => 'jsonrpc error', 'details' => $result]); } + /* example + $result = json_decode("[ + { + \"record_type\":\"GRADIDO_TRANSACTION\", + \"transaction\":{ + \"version_number\":1, + \"signature\":{ + \"pubkey\":\"2ed28a1cf5e116d83615406bc577152221c2f774a5656f66a0e7540f7576d71b\", + \"signature\":\"aed6725baacabf903e51f92503d49fa7e6b93c6402d56d9e3784be9a3366a77459213d858af46b579287aba8b1b63d206febce18bc80cec6fa63da6289e56403\" + }, + \"signature_count\":1, + \"hedera_transaction\":{ + \"consensusTimestamp\":{ + \"seconds\":1604392811, + \"nanos\":172812 + }, + \"runningHash\":\"f9ccf04137be418c3117a28bb5add6dced9745bcab74b7a2f46c182c8c98eeabf0127c131d15ebea7d0ac376f5d2de45\", + \"sequenceNumber\":94, + \"runningHashVersion\":3 + }, + \"transaction_type\":\"ADD_USER\", + \"add_user\":{ + \"user\":\"2ed28a1cf5e116d83615406bc577152221c2f774a5656f66a0e7540f7576d71b\" + }, + \"result\":\"result\", + \"parts\":1, + \"memo\":\"\" + } + }, + { + \"record_type\":\"GRADIDO_TRANSACTION\", + \"transaction\":{ + \"version_number\":1, + \"signature\":{ + \"pubkey\":\"8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d\", + \"signature\":\"3134adcd6cbccee17c2db398f91b6b6bdd098b6306fb2fa213eb9eb5a322af9078acca4d8b0383d4e906f3139eb3369e7c1ef0f3ac5fec724be0d085ba44af0b\" + }, + \"signature_count\":2, + \"hedera_transaction\":{ + \"consensusTimestamp\":{ + \"seconds\":1604392886, + \"nanos\":1528 + }, + \"runningHash\":\"e1df5526331e3def11d6b652b8f248d20c250739b6eb98f1fe7b338901753d9d573a14601ba84f61318a48940b3c237a\", + \"sequenceNumber\":95, + \"runningHashVersion\":3 + }, + \"transaction_type\":\"ADD_USER\", + \"add_user\":{ + \"user\":\"8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d\" + }, + \"result\":\"result\", + \"parts\":2, + \"memo\":\"\" + } + }, + { + \"record_type\":\"SIGNATURES\", + \"signature\":[ + { + \"pubkey\":\"2ed28a1cf5e116d83615406bc577152221c2f774a5656f66a0e7540f7576d71b\", + \"signature\":\"401717e768617c0f3311931c34a61e66ab362599a0e2a48ae7c4955645aec6573773985dafb84a11bfaf2bc12140c30b2f8c8ee094bc35d609bc56d15b4e9f04\" + } + ] + }, + { + \"record_type\": \"GRADIDO_TRANSACTION\", + \"transaction\":{ + \"version_number\":1, + \"signature\":{ + \"pubkey\":\"2ed28a1cf5e116d83615406bc577152221c2f774a5656f66a0e7540f7576d71b\", + \"signature\":\"99665dee9f2b475e426a2f449d0dae61924f6cf025903666ff72f2c7ef1af27523ebcd5fb684d17813fe7906b2f8cfe5ef4bdbb264ebf3ef80363491d9b86807\" + }, + \"signature_count\":1, + \"hedera_transaction\":{ + \"consensusTimestamp\":{ + \"seconds\":1604392904, + \"nanos\":798541 + }, + \"runningHash\":\"f1fd03610a9788e9bac01e1efb8b99bafae450f9088cb940db954842e0799235c57d842be83d998e6c21786f77f967a7\", + \"sequenceNumber\":96, + \"runningHashVersion\":3 + }, + \"transaction_type\":\"GRADIDO_CREATION\", + \"gradido_creation\":{ + \"user\":\"8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d\", + \"new_balance\":10000000, + \"prev_transfer_rec_num\":0, + \"amount\":10000000 + }, + \"result\":\"result\", + \"parts\":1, + \"memo\":\"\" + } + }, + { + \"record_type\": \"GRADIDO_TRANSACTION\", + \"transaction\":{ + \"version_number\":1, + \"signature\":{ + \"pubkey\":\"8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d\", + \"signature\":\"90125e0cfce61397d50ed9ba6c5df4cd4e0cf6fee8b10c70fee2898765982570d9a1208c222981429ae3c229e3fd36c2bf2333518cd0a4f0515937822e499d0b\" + }, + \"signature_count\":1, + \"hedera_transaction\":{ + \"consensusTimestamp\":{ + \"seconds\":1604392929, + \"nanos\":52539 + }, + \"runningHash\":\"a4be8f54be4f806b61d31f6bd770d7742822f14f03ffe09c07f08bac3031a06d12de5e38fec5c307149c7faf6e9879b8\", + \"sequenceNumber\":97, + \"runningHashVersion\":3 + }, + \"transaction_type\":\"LOCAL_TRANSFER\", + \"local_transfer\":{ + \"sender\":{ + \"user\":\"8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d\", + \"new_balance\":9825500, + \"prev_transfer_rec_num\":0 + }, + \"receiver\":{ + \"user\":\"2ed28a1cf5e116d83615406bc577152221c2f774a5656f66a0e7540f7576d71b\", + \"new_balance\":174500, + \"prev_transfer_rec_num\":0 + }, + \"amount\":174500 + }, + \"result\":\"result\", + \"parts\":1, + \"memo\":\"\" + } + } +]", true);*/ $part_count = -1; $temp_record = new Record; $errors = []; @@ -100,7 +233,7 @@ class JsonRequestHandlerController extends AppController { if($part_count == 0) { $finalize_result = $temp_record->finalize(); - if($finalize_result != true) { + if($finalize_result !== true) { $errors[] = ['msg' => 'error in finalize', 'record' => $_record, 'details' => $finalize_result, 'sequenceNumber' => $sequenceNumber]; } $temp_record = new Record; diff --git a/src/Model/Transactions/Record.php b/src/Model/Transactions/Record.php index da52db8ac..e5e610c79 100644 --- a/src/Model/Transactions/Record.php +++ b/src/Model/Transactions/Record.php @@ -31,11 +31,11 @@ class Signature $signaturesTable = TableRegistry::getTableLocator()->get('TransactionSignatures'); $entity = $signaturesTable->newEntity(); $entity->transaction_id = $transactionId; - if(count($this->signature) != 128) { - return ['state' => 'error', 'msg' => 'invalid signature size', 'details' => count($this->signature)]; + if(strlen($this->signature) != 128) { + return ['state' => 'error', 'msg' => 'invalid signature size', 'details' => strlen($this->signature)]; } - if(count($this->publicKey) != 64) { - return ['state' => 'error', 'msg' => 'invalid pubkey size', 'details' => count($this->publicKey)]; + if(strlen($this->publicKey) != 64) { + return ['state' => 'error', 'msg' => 'invalid pubkey size', 'details' => strlen($this->publicKey)]; } if(!preg_match('/^[0-9a-fA-F]*$/', $this->signature)) { return ['state' => 'error', 'msg' => 'signature isn\'t in hex format']; @@ -91,8 +91,9 @@ class ManageNodeGroupAdd $stateGroupAddresses = TableRegistry::getTableLocator()->get('StateGroupAddresses'); $transactionGroupEntity = $transactionGroupAddadressTable->newEntity(); $transactionGroupEntity->transaction_id = $transactionId; - if(count($this->user_pubkey) != 64) { - return ['state' => 'error', 'msg' => 'invalid size user pubkey', 'details' => count($this->user_pubkey)]; + $transactionGroupEntity->address_type_id = 1; + if(strlen($this->user_pubkey) != 64) { + return ['state' => 'error', 'msg' => 'invalid size user pubkey', 'details' => strlen($this->user_pubkey)]; } if(!preg_match('/^[0-9a-fA-F]*$/', $this->user_pubkey)) { return ['state' => 'error', 'msg' => 'user_pubkey isn\'t in hex format']; @@ -261,14 +262,14 @@ class GradidoTransfer extends GradidoModifieUserBalance public function finalize($transactionId, $received) { $transactionTransferTable = TableRegistry::getTableLocator()->get('TransactionSendCoins'); - if(count($this->sender_pubkey) != 64) { - return ['state' => 'error', 'msg' => 'invalid size sender pubkey', 'details' => count($this->user_pubkey)]; + if(strlen($this->sender_pubkey) != 64) { + return ['state' => 'error', 'msg' => 'invalid size sender pubkey', 'details' => strlen($this->user_pubkey)]; } if(!preg_match('/^[0-9a-fA-F]*$/', $this->sender_pubkey)) { return ['state' => 'error', 'msg' => 'sender_pubkey isn\'t in hex format']; } - if(count($this->receiver_pubkey) != 64) { - return ['state' => 'error', 'msg' => 'invalid size receiver pubkey', 'details' => count($this->user_pubkey)]; + if(strlen($this->receiver_pubkey) != 64) { + return ['state' => 'error', 'msg' => 'invalid size receiver pubkey', 'details' => strlen($this->user_pubkey)]; } if(!preg_match('/^[0-9a-fA-F]*$/', $this->receiver_pubkey)) { return ['state' => 'error', 'msg' => 'receiver_pubkey isn\'t in hex format']; @@ -315,6 +316,7 @@ class GradidoTransfer extends GradidoModifieUserBalance class Record { private $sequenceNumber = 0; + private $runningHash = null; private $transactionType = ''; private $memo = ''; private $signatures = []; @@ -330,6 +332,7 @@ class Record public function parseRecord($json) { + //var_dump($json); switch($json['record_type']) { case 'GRADIDO_TRANSACTION': return $this->parseTransaction($json['transaction']); @@ -375,19 +378,30 @@ class Record $newTransaction->id = $this->sequenceNumber; $newTransaction->transaction_type_id = $transactionTypeResults->first()->id; $newTransaction->memo = $this->memo; + $newTransaction->tx_hash = hex2bin($this->runningHash); $newTransaction->received = $this->received; + //! TODO change into transaction, if at least one fail, rollback + /* + // In a controller. + $articles->getConnection()->transactional(function () use ($articles, $entities) { + foreach ($entities as $entity) { + $articles->save($entity, ['atomic' => false]); + } + }); + */ if(!$transactionsTable->save($newTransaction)) { return ['state' => 'error', 'msg' => 'error saving transaction', 'details' => $newTransaction->getErrors()]; } + foreach($this->signatures as $sign) { $sign_result = $sign->finalize($this->sequenceNumber); - iF($sign_result != true) { + iF($sign_result !== true) { return ['state' => 'error', 'msg', 'error finalizing signature', 'details' => $sign_result]; } } $transaction_obj_result = $this->transactionObj->finalize($newTransaction->id, $this->received); - if($transaction_obj_result != true) { + if($transaction_obj_result !== true) { return ['state' => 'error', 'msg' => 'error finalizing transaction object', 'details' => $transaction_obj_result]; } return true; @@ -431,6 +445,7 @@ class Record $hedera = $data['hedera_transaction']; $this->sequenceNumber = $hedera['sequenceNumber']; + $this->runningHash = $hedera['runningHash']; $this->received = Time::createFromTimestamp($hedera['consensusTimestamp']['seconds']); $field_index = ''; @@ -450,6 +465,7 @@ class Record if($class_name == '' || $field_index == '') { return ['state' => 'error', 'msg' => 'node transaction type unknown', 'details' => $this->transactionType]; } + $class_name = 'Model\\Transactions\\' . $class_name; $this->transactionObj = new $class_name($data[$field_index]); if($class_name == 'ManageNodeGroupAdd') { $this->transactionObj->setRemoveFromGroup($removeFromGroup);