From 49c41b53ebaffcffce2063854333ddc3386b55c1 Mon Sep 17 00:00:00 2001 From: team-devstage Date: Mon, 7 Dec 2020 20:02:18 +0100 Subject: [PATCH 1/3] adjustemnts --- .../Component/JsonRpcRequestClientComponent.php | 2 +- src/Controller/JsonRequestHandlerController.php | 12 ++++++++++-- src/Model/Transactions/Record.php | 7 ++++++- src/protobuf | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Controller/Component/JsonRpcRequestClientComponent.php b/src/Controller/Component/JsonRpcRequestClientComponent.php index c03f7d832..579eafb22 100644 --- a/src/Controller/Component/JsonRpcRequestClientComponent.php +++ b/src/Controller/Component/JsonRpcRequestClientComponent.php @@ -63,7 +63,7 @@ class JsonRpcRequestClientComponent extends Component //$responseType = $response->getType(); return ['state' => 'error', 'type' => 'request error', 'msg' => 'server response isn\'t valid json']; } - return $json; + return $json['result']; //return ['state' => 'success', 'data' => $json]; } diff --git a/src/Controller/JsonRequestHandlerController.php b/src/Controller/JsonRequestHandlerController.php index 3d62aeed2..bf1af9064 100644 --- a/src/Controller/JsonRequestHandlerController.php +++ b/src/Controller/JsonRequestHandlerController.php @@ -82,10 +82,13 @@ class JsonRequestHandlerController extends AppController { } $group_alias = Configure::read('GroupAlias'); - $result = (array)$this->JsonRpcRequestClient->request('getTransactions', ['groupAlias' => $group_alias, 'lastKnownSequenceNumber' => $last_transaction_id]); + $result = $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]); } + if(!isset($result['transaction_count']) || $result['transaction_count'] == 0) { + return $this->returnJson(['state' => 'success']); + } /* example $result = json_decode("[ { @@ -222,7 +225,12 @@ class JsonRequestHandlerController extends AppController { $part_count = -1; $temp_record = new Record; $errors = []; - foreach($result as $_record) { + + foreach($result['blocks'] as $_record) { + if(is_string($_record)) { + // if it is a string, it is block validation hash in hex + continue; + } $parse_result = $temp_record->parseRecord($_record); if($parse_result == true) { $sequenceNumber = $temp_record->getSequenceNumber(); diff --git a/src/Model/Transactions/Record.php b/src/Model/Transactions/Record.php index e5e610c79..7a93dab5b 100644 --- a/src/Model/Transactions/Record.php +++ b/src/Model/Transactions/Record.php @@ -332,6 +332,9 @@ class Record public function parseRecord($json) { + if(!isset($json['record_type'])) { + return false; + } //var_dump($json); switch($json['record_type']) { case 'GRADIDO_TRANSACTION': @@ -378,7 +381,9 @@ class Record $newTransaction->id = $this->sequenceNumber; $newTransaction->transaction_type_id = $transactionTypeResults->first()->id; $newTransaction->memo = $this->memo; - $newTransaction->tx_hash = hex2bin($this->runningHash); + if($this->runningHash != '') { + $newTransaction->tx_hash = hex2bin($this->runningHash); + } $newTransaction->received = $this->received; //! TODO change into transaction, if at least one fail, rollback diff --git a/src/protobuf b/src/protobuf index 81a461566..9004e6978 160000 --- a/src/protobuf +++ b/src/protobuf @@ -1 +1 @@ -Subproject commit 81a461566e46d71533dc3e284fa075d7d68fd020 +Subproject commit 9004e6978ac3dafcc635b2ffcf8bc6a156451cca From 171ed3584648f966a71ac5c0a168020f50b748a3 Mon Sep 17 00:00:00 2001 From: team-devstage Date: Tue, 8 Dec 2020 09:55:41 +0100 Subject: [PATCH 2/3] fix some bugs by work with transactions from gradido node --- .../StateGroupAddressesController.php | 6 +- src/Model/Entity/StateGroupAddress.php | 6 +- src/Model/Table/StateGroupAddressesTable.php | 20 +- src/Model/Transactions/Record.php | 179 ++++++++++-------- src/Template/StateGroupAddresses/add.ctp | 6 +- src/Template/StateGroupAddresses/edit.ctp | 6 +- src/Template/StateGroupAddresses/index.ctp | 14 +- src/Template/StateGroupAddresses/view.ctp | 12 +- .../TransactionGroupAddaddress/index.ctp | 2 + tests/Fixture/StateGroupAddressesFixture.php | 13 +- 10 files changed, 150 insertions(+), 114 deletions(-) diff --git a/src/Controller/StateGroupAddressesController.php b/src/Controller/StateGroupAddressesController.php index 13f2b1f78..25afc82c7 100644 --- a/src/Controller/StateGroupAddressesController.php +++ b/src/Controller/StateGroupAddressesController.php @@ -20,7 +20,7 @@ class StateGroupAddressesController extends AppController public function index() { $this->paginate = [ - 'contain' => ['StateGroups', 'AddressTypes'] + 'contain' => ['AddressTypes'], ]; $stateGroupAddresses = $this->paginate($this->StateGroupAddresses); @@ -37,7 +37,7 @@ class StateGroupAddressesController extends AppController public function view($id = null) { $stateGroupAddress = $this->StateGroupAddresses->get($id, [ - 'contain' => ['StateGroups', 'AddressTypes'] + 'contain' => ['StateGroups', 'AddressTypes'], ]); $this->set('stateGroupAddress', $stateGroupAddress); @@ -75,7 +75,7 @@ class StateGroupAddressesController extends AppController public function edit($id = null) { $stateGroupAddress = $this->StateGroupAddresses->get($id, [ - 'contain' => [] + 'contain' => [], ]); if ($this->request->is(['patch', 'post', 'put'])) { $stateGroupAddress = $this->StateGroupAddresses->patchEntity($stateGroupAddress, $this->request->getData()); diff --git a/src/Model/Entity/StateGroupAddress.php b/src/Model/Entity/StateGroupAddress.php index e5ce71681..e2d669d09 100644 --- a/src/Model/Entity/StateGroupAddress.php +++ b/src/Model/Entity/StateGroupAddress.php @@ -7,7 +7,7 @@ use Cake\ORM\Entity; * StateGroupAddress Entity * * @property int $id - * @property int $state_group_id + * @property int $group_id * @property string|resource $public_key * @property int $address_type_id * @@ -26,10 +26,10 @@ class StateGroupAddress extends Entity * @var array */ protected $_accessible = [ - 'state_group_id' => true, + 'group_id' => true, 'public_key' => true, 'address_type_id' => true, 'state_group' => true, - 'address_type' => true + 'address_type' => true, ]; } diff --git a/src/Model/Table/StateGroupAddressesTable.php b/src/Model/Table/StateGroupAddressesTable.php index 62fcf9470..a63e2ae1c 100644 --- a/src/Model/Table/StateGroupAddressesTable.php +++ b/src/Model/Table/StateGroupAddressesTable.php @@ -9,7 +9,7 @@ use Cake\Validation\Validator; /** * StateGroupAddresses Model * - * @property \App\Model\Table\StateGroupsTable&\Cake\ORM\Association\BelongsTo $StateGroups + * @property &\Cake\ORM\Association\BelongsTo $Groups * @property \App\Model\Table\AddressTypesTable&\Cake\ORM\Association\BelongsTo $AddressTypes * * @method \App\Model\Entity\StateGroupAddress get($primaryKey, $options = []) @@ -37,13 +37,13 @@ class StateGroupAddressesTable extends Table $this->setDisplayField('id'); $this->setPrimaryKey('id'); - $this->belongsTo('StateGroups', [ - 'foreignKey' => 'state_group_id', - 'joinType' => 'INNER' - ]); + /*$this->belongsTo('Groups', [ + 'foreignKey' => 'group_id', + 'joinType' => 'INNER', + ]);*/ $this->belongsTo('AddressTypes', [ 'foreignKey' => 'address_type_id', - 'joinType' => 'INNER' + 'joinType' => 'INNER', ]); } @@ -56,12 +56,13 @@ class StateGroupAddressesTable extends Table public function validationDefault(Validator $validator) { $validator - ->integer('id') + ->nonNegativeInteger('id') ->allowEmptyString('id', null, 'create'); $validator ->requirePresence('public_key', 'create') - ->notEmptyString('public_key'); + ->notEmptyString('public_key') + ->add('public_key', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); return $validator; } @@ -75,7 +76,8 @@ class StateGroupAddressesTable extends Table */ public function buildRules(RulesChecker $rules) { - $rules->add($rules->existsIn(['state_group_id'], 'StateGroups')); + $rules->add($rules->isUnique(['public_key'])); + //$rules->add($rules->existsIn(['group_id'], 'Groups')); $rules->add($rules->existsIn(['address_type_id'], 'AddressTypes')); return $rules; diff --git a/src/Model/Transactions/Record.php b/src/Model/Transactions/Record.php index 339f4edd3..55665edec 100644 --- a/src/Model/Transactions/Record.php +++ b/src/Model/Transactions/Record.php @@ -61,16 +61,36 @@ class GradidoModifieUserBalance public function getUserId($userPublicKey) { + $userPublicBin = hex2bin($userPublicKey); $stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers'); + + // hack for pauls public key format with many FF instead of the real values + $stateUsers = $stateUsersTable->find('all')->select(['id', 'public_key']); + $debug_user_publics = []; + foreach($stateUsers as $user) { + $user_public = stream_get_contents($user->public_key); + $debug_user_publics[] = bin2hex($user_public); + if(($user_public & $userPublicBin) == $user_public) { + array_push($this->state_users, (int)$user->id); + return $user->id; + } + } + return [ + 'state' => 'error', + 'msg' => '[GradidoModifieUserBalance::getUserId] couldn\'t find user via public key binary &', + 'details' => ['input public' => $userPublicKey, 'user publics' => $debug_user_publics] + ]; + // hack end + $stateUser = $stateUsersTable->find('all')->where(['public_key' => hex2bin($userPublicKey)]); if($stateUser->isEmpty()) { - return ['state' => 'error', 'msg' => 'couldn\'t find user via public key']; + return ['state' => 'error', 'msg' => '[GradidoModifieUserBalance::getUserId] couldn\'t find user via public key']; } $id = $stateUser->first()->id; if($id && is_int($id) && (int)$id > 0 && !in_array((int)$id, $this->state_users)) { array_push($this->state_users, (int)$id); } - return $stateUser->first()->id; + return $id; } public function updateBalance($newBalance, $recordDate, $userId) @@ -138,6 +158,9 @@ class ManageNodeGroupAdd extends GradidoModifieUserBalance $transactionGroupAddadressTable = TableRegistry::getTableLocator()->get('TransactionGroupAddaddress'); $stateGroupAddresses = TableRegistry::getTableLocator()->get('StateGroupAddresses'); $transactionGroupEntity = $transactionGroupAddadressTable->newEntity(); + if(!is_int($transactionId)) { + return ['state' => 'error', 'msg' => '[ManageNodeGroupAdd::finalize] transaction id is not int', 'details' => $transactionId]; + } $transactionGroupEntity->transaction_id = $transactionId; $transactionGroupEntity->address_type_id = 1; if(strlen($this->user_pubkey) != 64) { @@ -147,13 +170,19 @@ class ManageNodeGroupAdd extends GradidoModifieUserBalance return ['state' => 'error', 'msg' => 'user_pubkey isn\'t in hex format']; } - $transactionGroupEntity->public_key = hex2bin($this->user_pubkey); - $transactionGroupEntity->state_user_id = $this->getUserId($this->user_pubkey); + $userPubkeyBin = hex2bin($this->user_pubkey); + + $transactionGroupEntity->public_key = $userPubkeyBin; + $user_id = $this->getUserId($this->user_pubkey); + if(!is_int($user_id)) { + return ['state' => 'error', 'msg' => '[ManageNodeGroupAdd::finalize] user id is not int', 'details' => $user_id]; + } + $transactionGroupEntity->state_user_id = $user_id; $transactionGroupEntity->remove_from_group = $this->remove_from_group; if(!$transactionGroupAddadressTable->save($transactionGroupEntity)) { return ['state' => 'error', 'msg' => 'error saving TransactionGroupAddaddress Entity', 'details' => $transactionGroupEntity->getErrors()]; } - $userPubkeyBin = hex2bin($this->user_pubkey); + if($this->remove_from_group) { $stateGroup_query = $stateGroupAddresses->find('all')->where(['public_key' => hex2bin($this->user_pubkey)]); @@ -375,77 +404,77 @@ class Record */ public function finalize() { - $transactionTypesTable = TableRegistry::getTableLocator()->get('TransactionTypes'); - $transactionsTable = TableRegistry::getTableLocator()->get('Transactions'); - $stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions'); - - $transactionTypeName = $this->nodeTransactionTypeToDBTransactionType($this->transactionType); - $transactionTypeResults = $transactionTypesTable->find('all')->where(['name' => $transactionTypeName]); - if($transactionTypeResults->isEmpty()) { - return [ - 'state' => 'error', 'msg' => 'transaction type not found', - 'details' => ['nodeType' => $this->transactionType, 'dbType' => $transactionTypeName] - ]; - } - if(!$this->transactionObj) { - return ['state' => 'error', 'msg' => 'transaction obj is null']; - } - if($this->sequenceNumber <= 0) { - return ['state' => 'error', 'msg' => 'sequence number invalid', 'details' => $this->sequenceNumber]; - } - $transactionExistResult = $transactionsTable->find('all')->where(['id' => $this->sequenceNumber]); - if(!$transactionExistResult->isEmpty()) { - return ['state' => 'warning', 'msg' => 'transaction already exist in db', 'details' => $this->sequenceNumber]; - } - $newTransaction = $transactionsTable->newEntity(); - $newTransaction->id = $this->sequenceNumber; - $newTransaction->transaction_type_id = $transactionTypeResults->first()->id; - $newTransaction->memo = $this->memo; - if($this->runningHash != '') { - $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]); + $transactionTypesTable = TableRegistry::getTableLocator()->get('TransactionTypes'); + $transactionsTable = TableRegistry::getTableLocator()->get('Transactions'); + $stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions'); + + $transactionTypeName = $this->nodeTransactionTypeToDBTransactionType($this->transactionType); + $transactionTypeResults = $transactionTypesTable->find('all')->where(['name' => $transactionTypeName]); + if($transactionTypeResults->isEmpty()) { + return [ + 'state' => 'error', 'msg' => 'transaction type not found', + 'details' => ['nodeType' => $this->transactionType, 'dbType' => $transactionTypeName] + ]; + } + if(!$this->transactionObj) { + return ['state' => 'error', 'msg' => 'transaction obj is null']; + } + if($this->sequenceNumber <= 0) { + return ['state' => 'error', 'msg' => 'sequence number invalid', 'details' => $this->sequenceNumber]; + } + $transactionExistResult = $transactionsTable->find('all')->where(['id' => $this->sequenceNumber]); + if(!$transactionExistResult->isEmpty()) { + return ['state' => 'warning', 'msg' => 'transaction already exist in db', 'details' => $this->sequenceNumber]; + } + $newTransaction = $transactionsTable->newEntity(); + $newTransaction->id = $this->sequenceNumber; + $newTransaction->transaction_type_id = $transactionTypeResults->first()->id; + $newTransaction->memo = $this->memo; + if($this->runningHash != '' && strlen($this->runningHash) % 2 == 0) { + $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) { + return ['state' => 'error', 'msg', 'error finalizing signature', 'details' => $sign_result]; } - }); - */ - 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) { - 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) { - return ['state' => 'error', 'msg' => 'error finalizing transaction object', 'details' => $transaction_obj_result]; - } - $state_users = $this->transactionObj->getAllStateUsers(); - $sut_entities = []; - foreach($state_users as $state_user_id) { - $entity = $stateUserTransactionsTable->newEntity(); - $entity->state_user_id = $state_user_id; - $entity->transaction_id = $newTransaction->id; - $entity->transaction_type_id = $newTransaction->transaction_type_id; - $sut_entities[] = $entity; - } - $sut_results = $stateUserTransactionsTable->saveMany($sut_entities); - foreach($sut_results as $i => $result) { - if(false == $result) { - return ['state' => 'error', 'msg' => 'error saving state_user_transaction', 'details' => $sut_entities[$i]->getErrors()]; - } - } - - return true; + } + $transaction_obj_result = $this->transactionObj->finalize($newTransaction->id, $this->received); + if($transaction_obj_result !== true) { + return ['state' => 'error', 'msg' => 'error finalizing transaction object', 'details' => $transaction_obj_result]; + } + $state_users = $this->transactionObj->getAllStateUsers(); + $sut_entities = []; + foreach($state_users as $state_user_id) { + $entity = $stateUserTransactionsTable->newEntity(); + $entity->state_user_id = $state_user_id; + $entity->transaction_id = $newTransaction->id; + $entity->transaction_type_id = $newTransaction->transaction_type_id; + $sut_entities[] = $entity; + } + $sut_results = $stateUserTransactionsTable->saveMany($sut_entities); + foreach($sut_results as $i => $result) { + if(false == $result) { + return ['state' => 'error', 'msg' => 'error saving state_user_transaction', 'details' => $sut_entities[$i]->getErrors()]; + } + } + + return true; } diff --git a/src/Template/StateGroupAddresses/add.ctp b/src/Template/StateGroupAddresses/add.ctp index f9eba953a..43ba76178 100644 --- a/src/Template/StateGroupAddresses/add.ctp +++ b/src/Template/StateGroupAddresses/add.ctp @@ -4,8 +4,8 @@ * @var \App\Model\Entity\StateGroupAddress $stateGroupAddress */ ?> -