From 3a10d1f429617260392519f0de44569452a3154f Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 31 Mar 2021 18:02:23 +0000 Subject: [PATCH 1/4] update parameters to use with different blockchain-types --- configs/community_server/app.php | 36 ++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/configs/community_server/app.php b/configs/community_server/app.php index c52ae8047..ee2244636 100644 --- a/configs/community_server/app.php +++ b/configs/community_server/app.php @@ -391,18 +391,42 @@ return [ 'Session' => [ 'defaults' => 'php', ], + // *************************************************** // Gradido specific configuration + // *************************************************** // Login Server ip and port 'LoginServer' => [ 'host' => 'http://login-server', - 'port' => 1201 + 'port' => 1201, + 'url' => 'http://localhost' ], - 'API' => [ + + // login server for look up emails from other groups + // workaround for using multiple groups until every code is finished as planned + // normally node server are responsible for email look up from users from other groups + 'NeighborLoginServers' => [ + ['host' => 'login-server', 'port' => 1201] + ], + 'GradidoBlockchain' => [ + // type: + // - db: centralized blockchain in mysql db, no cross group transactions + // - hedera: send transaction over hedera + 'type' => 'db', + // gradido nodes with blockchain (if type != db) + 'nodes' => [ + ['host' => 'node', 'port' => 13702] + ] + ], + + 'GroupAlias' => 'docker', + 'GDTServer' => [ + //'host' => 'gdt' + ], + 'API' => [ 'allowedCaller' => ['login-server'] ], - 'ServerAdminEmail' => 'info@gradido.net', + 'ServerAdminEmail' => 'info@gradido.net', 'noReplyEmail' => 'no-reply@gradido.net', - 'disableEmail' => true, - - 'GroupNode' => false + 'disableEmail' => false + ]; From 6ba6bd7180089886eb768f795f93472a9878ecfb Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 31 Mar 2021 18:02:54 +0000 Subject: [PATCH 2/4] add send coins transactions how uses login-server endpoint from stage2 --- .../src/Controller/AppRequestsController.php | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/community_server/src/Controller/AppRequestsController.php b/community_server/src/Controller/AppRequestsController.php index c36ddf161..f2c0dbac6 100644 --- a/community_server/src/Controller/AppRequestsController.php +++ b/community_server/src/Controller/AppRequestsController.php @@ -23,7 +23,7 @@ class AppRequestsController extends AppController $this->loadComponent('JsonRequestClient'); $this->loadComponent('JsonRpcRequestClient'); //$this->Auth->allow(['add', 'edit']); - $this->Auth->allow('index'); + $this->Auth->allow(['index', 'sendCoins']); } @@ -52,6 +52,106 @@ class AppRequestsController extends AppController return $this->returnJson(['state' => 'error', 'msg' => 'no post or get']); } + private function checkRequiredFields($data, $fields) { + foreach($fields as $field) { + if(!isset($data[$field])) { + return ['state' => 'error', 'msg' => 'missing field', 'details' => $field . ' not found']; + } + } + return true; + } + + public function sendCoins() + { + /* + * { + "session_id" : -127182, + "amount": 2000000, + "email": "max.musterman@gmail.de", + "memo":"Thank you :)", + "group": "gdd1", + "auto_sign": true + */ + $data = $this->request->input('json_decode'); + $login_request_result = $this->requestLogin(0, false); + if($login_request_result !== true) { + return $this->returnJson($login_request_result); + } + $session = $this->getRequest()->getSession(); + $required_fields = $this->checkRequiredFields($data, ['amount', 'email']); + if($required_fields !== true) { + return $this->returnJson($required_fields); + } + $amount = $data['amount']; + if(intval($amount) <= 0) { + return $this->returnJson(['state' => 'error', 'msg' => 'amount is invalid', 'details' => $amount]); + } + $email = $data['email']; + if($email == '') { + return $this->returnJson(['state' => 'error', 'msg' => 'email is empty']); + } + $memo = ''; + if(isset($data['memo'])) { + $memo = $data['memo']; + } + $auto_sign = false; + if(isset($data['auto_sign'])) { + $auto_sign = boolval($data['auto_sign']); + } + $group = ''; + if(isset($data['group'])) { + $group = $data['group']; + } + + $requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([ + 'session_id' => $session->read('session_id'), + 'transaction_type' => 'transfer', + 'memo' => $memo, + 'amount' => $amount, + 'target_group' => $group, + 'target_email' => $email, + 'auto_sign' => $auto_sign + ]), '/createTransaction'); + + if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) { + $pendingTransactionCount = $session->read('Transactions.pending'); + if($pendingTransactionCount == null) { + $pendingTransactionCount = 1; + } else { + $pendingTransactionCount++; + } + $session->write('Transactions.pending', $pendingTransactionCount); + //echo "pending: " . $pendingTransactionCount; + return $this->returnJson(['state' => 'success']); + } else { + + /* + * if request contain unknown parameter format, shouldn't happen't at all + * {"state": "error", "msg": "parameter format unknown"} + * if json parsing failed + * {"state": "error", "msg": "json exception", "details":"exception text"} + * if session_id is zero or not set + * {"state": "error", "msg": "session_id invalid"} + * if session id wasn't found on login server, if server was restartet or user logged out (also per timeout, default: 15 minutes) + * {"state": "error", "msg": "session not found"} + * if session hasn't active user, shouldn't happen't at all, login-server should be checked if happen + * {"state": "code error", "msg":"user is zero"} + * if transaction type not known + * {"state": "error", "msg":"transaction_type unknown"} + * if receiver wasn't known to Login-Server + * {"state": "not found", "msg":"receiver not found"} + * if receiver account disabled, and therefor cannto receive any coins + * {"state": "disabled", "msg":"receiver is disabled"} + * if transaction was okay and will be further proccessed + * {"state":"success"} + */ + $answear_data = $requestAnswear['data']; + return $this->returnJson($answear_data); + + } + + } + private function acquireAccessToken($session_id) { From 6ca4fa79311c0331280e88508db5aeb79a6b4248 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Tue, 6 Apr 2021 10:16:13 +0000 Subject: [PATCH 3/4] add blockchain types table and connect it with transactions table --- .../gradido_community/blockchain_types.sql | 17 +++ .../insert/insert_blockchain_types.sql | 5 + .../skeema/gradido_community/transactions.sql | 1 + .../Controller/BlockchainTypesController.php | 106 ++++++++++++++++++ .../src/Model/Entity/BlockchainType.php | 30 +++++ .../src/Model/Entity/Transaction.php | 1 + .../src/Model/Table/BlockchainTypesTable.php | 68 +++++++++++ .../src/Model/Table/TransactionsTable.php | 5 + .../src/Template/BlockchainTypes/add.ctp | 25 +++++ .../src/Template/BlockchainTypes/edit.ctp | 31 +++++ .../src/Template/BlockchainTypes/index.ctp | 51 +++++++++ .../src/Template/BlockchainTypes/view.ctp | 36 ++++++ 12 files changed, 376 insertions(+) create mode 100644 community_server/skeema/gradido_community/blockchain_types.sql create mode 100644 community_server/skeema/gradido_community/insert/insert_blockchain_types.sql create mode 100644 community_server/src/Controller/BlockchainTypesController.php create mode 100644 community_server/src/Model/Entity/BlockchainType.php create mode 100644 community_server/src/Model/Table/BlockchainTypesTable.php create mode 100644 community_server/src/Template/BlockchainTypes/add.ctp create mode 100644 community_server/src/Template/BlockchainTypes/edit.ctp create mode 100644 community_server/src/Template/BlockchainTypes/index.ctp create mode 100644 community_server/src/Template/BlockchainTypes/view.ctp diff --git a/community_server/skeema/gradido_community/blockchain_types.sql b/community_server/skeema/gradido_community/blockchain_types.sql new file mode 100644 index 000000000..bd6a12604 --- /dev/null +++ b/community_server/skeema/gradido_community/blockchain_types.sql @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +/** + * Author: einhornimmond + * Created: 06.04.2021 + */ + +CREATE TABLE `blockchain_types` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(45) NOT NULL, + `text` varchar(255) NULL, + `symbol` varchar(10) NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/community_server/skeema/gradido_community/insert/insert_blockchain_types.sql b/community_server/skeema/gradido_community/insert/insert_blockchain_types.sql new file mode 100644 index 000000000..0a58e4392 --- /dev/null +++ b/community_server/skeema/gradido_community/insert/insert_blockchain_types.sql @@ -0,0 +1,5 @@ +INSERT INTO `blockchain_types` (`id`, `name`, `text`, `symbol`) VALUES +(1, 'mysql', 'use mysql db as blockchain, work only with single community-server', NULL), +(2, 'hedera', 'use hedera for transactions', 'HBAR'); + + diff --git a/community_server/skeema/gradido_community/transactions.sql b/community_server/skeema/gradido_community/transactions.sql index 566cfc048..8641f0057 100644 --- a/community_server/skeema/gradido_community/transactions.sql +++ b/community_server/skeema/gradido_community/transactions.sql @@ -5,5 +5,6 @@ CREATE TABLE `transactions` ( `tx_hash` binary(48) DEFAULT NULL, `memo` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `blockchain_type_id` bigint(20) unsigned NOT NULL DEFAULT 1, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/community_server/src/Controller/BlockchainTypesController.php b/community_server/src/Controller/BlockchainTypesController.php new file mode 100644 index 000000000..2a94037df --- /dev/null +++ b/community_server/src/Controller/BlockchainTypesController.php @@ -0,0 +1,106 @@ +paginate($this->BlockchainTypes); + + $this->set(compact('blockchainTypes')); + } + + /** + * View method + * + * @param string|null $id Blockchain Type id. + * @return \Cake\Http\Response|null + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function view($id = null) + { + $blockchainType = $this->BlockchainTypes->get($id, [ + 'contain' => [], + ]); + + $this->set('blockchainType', $blockchainType); + } + + /** + * Add method + * + * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. + */ + public function add() + { + $blockchainType = $this->BlockchainTypes->newEntity(); + if ($this->request->is('post')) { + $blockchainType = $this->BlockchainTypes->patchEntity($blockchainType, $this->request->getData()); + if ($this->BlockchainTypes->save($blockchainType)) { + $this->Flash->success(__('The blockchain type has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The blockchain type could not be saved. Please, try again.')); + } + $this->set(compact('blockchainType')); + } + + /** + * Edit method + * + * @param string|null $id Blockchain Type id. + * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function edit($id = null) + { + $blockchainType = $this->BlockchainTypes->get($id, [ + 'contain' => [], + ]); + if ($this->request->is(['patch', 'post', 'put'])) { + $blockchainType = $this->BlockchainTypes->patchEntity($blockchainType, $this->request->getData()); + if ($this->BlockchainTypes->save($blockchainType)) { + $this->Flash->success(__('The blockchain type has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The blockchain type could not be saved. Please, try again.')); + } + $this->set(compact('blockchainType')); + } + + /** + * Delete method + * + * @param string|null $id Blockchain Type id. + * @return \Cake\Http\Response|null Redirects to index. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function delete($id = null) + { + $this->request->allowMethod(['post', 'delete']); + $blockchainType = $this->BlockchainTypes->get($id); + if ($this->BlockchainTypes->delete($blockchainType)) { + $this->Flash->success(__('The blockchain type has been deleted.')); + } else { + $this->Flash->error(__('The blockchain type could not be deleted. Please, try again.')); + } + + return $this->redirect(['action' => 'index']); + } +} diff --git a/community_server/src/Model/Entity/BlockchainType.php b/community_server/src/Model/Entity/BlockchainType.php new file mode 100644 index 000000000..c2e21402c --- /dev/null +++ b/community_server/src/Model/Entity/BlockchainType.php @@ -0,0 +1,30 @@ + true, + 'text' => true, + 'symbol' => true, + ]; +} diff --git a/community_server/src/Model/Entity/Transaction.php b/community_server/src/Model/Entity/Transaction.php index d4ebf68c4..209b8997e 100644 --- a/community_server/src/Model/Entity/Transaction.php +++ b/community_server/src/Model/Entity/Transaction.php @@ -39,6 +39,7 @@ class Transaction extends Entity 'tx_hash' => true, 'memo' => true, 'received' => true, + 'blockchain_type_id' => true, 'state_group' => true, 'transaction_type' => true, 'state_created' => true, diff --git a/community_server/src/Model/Table/BlockchainTypesTable.php b/community_server/src/Model/Table/BlockchainTypesTable.php new file mode 100644 index 000000000..90213ac9f --- /dev/null +++ b/community_server/src/Model/Table/BlockchainTypesTable.php @@ -0,0 +1,68 @@ +setTable('blockchain_types'); + $this->setDisplayField('name'); + $this->setPrimaryKey('id'); + } + + /** + * Default validation rules. + * + * @param \Cake\Validation\Validator $validator Validator instance. + * @return \Cake\Validation\Validator + */ + public function validationDefault(Validator $validator) + { + $validator + ->nonNegativeInteger('id') + ->allowEmptyString('id', null, 'create'); + + $validator + ->scalar('name') + ->maxLength('name', 45) + ->requirePresence('name', 'create') + ->notEmptyString('name'); + + $validator + ->scalar('text') + ->maxLength('text', 255) + ->allowEmptyString('text'); + + $validator + ->scalar('symbol') + ->maxLength('symbol', 10) + ->allowEmptyString('symbol'); + + return $validator; + } +} diff --git a/community_server/src/Model/Table/TransactionsTable.php b/community_server/src/Model/Table/TransactionsTable.php index 6777070fe..198feda73 100644 --- a/community_server/src/Model/Table/TransactionsTable.php +++ b/community_server/src/Model/Table/TransactionsTable.php @@ -52,6 +52,10 @@ class TransactionsTable extends Table 'foreignKey' => 'transaction_type_id', 'joinType' => 'INNER' ]); + $this->belongsTo('BlockchainTypes', [ + 'foreignKey' => 'blockchain_type_id', + 'joinType' => 'INNER' + ]); $this->hasMany('StateCreated', [ 'foreignKey' => 'transaction_id' ]); @@ -114,6 +118,7 @@ class TransactionsTable extends Table { $rules->add($rules->existsIn(['state_group_id'], 'StateGroups')); $rules->add($rules->existsIn(['transaction_type_id'], 'TransactionTypes')); + $rules->add($rules->existsIn(['blockchain_type_id'], 'BlockchainTypes')); return $rules; } diff --git a/community_server/src/Template/BlockchainTypes/add.ctp b/community_server/src/Template/BlockchainTypes/add.ctp new file mode 100644 index 000000000..bef6b70c5 --- /dev/null +++ b/community_server/src/Template/BlockchainTypes/add.ctp @@ -0,0 +1,25 @@ + + +
+ Form->create($blockchainType) ?> +
+ + Form->control('name'); + echo $this->Form->control('text'); + echo $this->Form->control('symbol'); + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/community_server/src/Template/BlockchainTypes/edit.ctp b/community_server/src/Template/BlockchainTypes/edit.ctp new file mode 100644 index 000000000..40cd7849b --- /dev/null +++ b/community_server/src/Template/BlockchainTypes/edit.ctp @@ -0,0 +1,31 @@ + + +
+ Form->create($blockchainType) ?> +
+ + Form->control('name'); + echo $this->Form->control('text'); + echo $this->Form->control('symbol'); + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/community_server/src/Template/BlockchainTypes/index.ctp b/community_server/src/Template/BlockchainTypes/index.ctp new file mode 100644 index 000000000..457782043 --- /dev/null +++ b/community_server/src/Template/BlockchainTypes/index.ctp @@ -0,0 +1,51 @@ + + +
+

+ + + + + + + + + + + + + + + + + + + + + +
Paginator->sort('id') ?>Paginator->sort('name') ?>Paginator->sort('text') ?>Paginator->sort('symbol') ?>
Number->format($blockchainType->id) ?>name) ?>text) ?>symbol) ?> + Html->link(__('View'), ['action' => 'view', $blockchainType->id]) ?> + Html->link(__('Edit'), ['action' => 'edit', $blockchainType->id]) ?> + Form->postLink(__('Delete'), ['action' => 'delete', $blockchainType->id], ['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]) ?> +
+
+
    + Paginator->first('<< ' . __('first')) ?> + Paginator->prev('< ' . __('previous')) ?> + Paginator->numbers() ?> + Paginator->next(__('next') . ' >') ?> + Paginator->last(__('last') . ' >>') ?> +
+

Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?>

+
+
diff --git a/community_server/src/Template/BlockchainTypes/view.ctp b/community_server/src/Template/BlockchainTypes/view.ctp new file mode 100644 index 000000000..481cc7b90 --- /dev/null +++ b/community_server/src/Template/BlockchainTypes/view.ctp @@ -0,0 +1,36 @@ + + +
+

name) ?>

+ + + + + + + + + + + + + + + + + +
name) ?>
text) ?>
symbol) ?>
Number->format($blockchainType->id) ?>
+
From feb66dc670b9dae30b89cf58f5216b7fb00558fb Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Tue, 6 Apr 2021 11:09:16 +0000 Subject: [PATCH 4/4] transfer blockchain type to login server --- .../src/Controller/AppController.php | 18 ++++++++++++++++++ .../src/Controller/AppRequestsController.php | 3 ++- .../Component/JsonRequestClientComponent.php | 5 +++-- .../TransactionCreationsController.php | 6 ++++-- .../TransactionSendCoinsController.php | 3 ++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/community_server/src/Controller/AppController.php b/community_server/src/Controller/AppController.php index b9522c2e7..42d97d5d8 100644 --- a/community_server/src/Controller/AppController.php +++ b/community_server/src/Controller/AppController.php @@ -35,6 +35,7 @@ class AppController extends Controller { var $loginServerUrl = ''; + var $blockchainType = 'mysql'; /** * Initialization hook method. * @@ -136,6 +137,23 @@ class AppController extends Controller } else { $this->loginServerUrl = Router::url('/', true); } + /* + * + * 'GradidoBlockchain' => [ + * // type: + * // - mysql: centralized blockchain in mysql db, no cross group transactions + * // - hedera: send transaction over hedera + * 'type' => 'hedera', + * // gradido nodes with blockchain (if type != mysql) + * 'nodes' => [ + * ['host' => 'http://192.168.178.225', 'port' => 13702] + * ] + * ], + */ + $blockchain = Configure::read('GradidoBlockchain'); + if($blockchain && isset($blockchain['type'])) { + $this->blockchainType = $blockchain['type']; + } } protected function requestLogin($session_id = 0, $redirect = true) diff --git a/community_server/src/Controller/AppRequestsController.php b/community_server/src/Controller/AppRequestsController.php index f2c0dbac6..fa59887c8 100644 --- a/community_server/src/Controller/AppRequestsController.php +++ b/community_server/src/Controller/AppRequestsController.php @@ -110,7 +110,8 @@ class AppRequestsController extends AppController 'amount' => $amount, 'target_group' => $group, 'target_email' => $email, - 'auto_sign' => $auto_sign + 'auto_sign' => $auto_sign, + 'blockchain_type' => $this->blockchainType ]), '/createTransaction'); if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) { diff --git a/community_server/src/Controller/Component/JsonRequestClientComponent.php b/community_server/src/Controller/Component/JsonRequestClientComponent.php index c73a00528..09856d7e2 100644 --- a/community_server/src/Controller/Component/JsonRequestClientComponent.php +++ b/community_server/src/Controller/Component/JsonRequestClientComponent.php @@ -15,7 +15,7 @@ use Cake\Core\Configure; class JsonRequestClientComponent extends Component { - public function sendTransaction($session_id, $base64Message, $user_balance = 0, $auto_sign = false) { + public function sendTransaction($session_id, $base64Message, $user_balance = 0, $auto_sign = false, $blockchain_type = 'mysql') { if(!is_numeric($session_id)) { return ['state' => 'error', 'type' => 'parameter error', 'msg' => 'session_id isn\'t numeric']; } @@ -36,7 +36,8 @@ class JsonRequestClientComponent extends Component 'session_id' => $session_id, 'transaction_base64' => $base64Message, 'balance' => $user_balance, - 'auto_sign' => $auto_sign + 'auto_sign' => $auto_sign, + 'blockchain_type' => $this->blockchainType ]), '/checkTransaction'); } diff --git a/community_server/src/Controller/TransactionCreationsController.php b/community_server/src/Controller/TransactionCreationsController.php index 77429d8d7..f439bd893 100644 --- a/community_server/src/Controller/TransactionCreationsController.php +++ b/community_server/src/Controller/TransactionCreationsController.php @@ -399,7 +399,8 @@ class TransactionCreationsController extends AppController 'memo' => $memo, 'amount' => $localAmountCent, 'target_pubkey' => $pubKeyHex, - 'target_date' => $localTargetDateFrozen + 'target_date' => $localTargetDateFrozen, + 'blockchain_type' => $this->blockchainType ]), '/createTransaction'); if('success' != $requestAnswear['state']) { @@ -580,7 +581,8 @@ class TransactionCreationsController extends AppController $session_id, $transaction_base64, $user['balance'], - $auto_sign + $auto_sign, + $this->blockchainType ); if ($requestResult['state'] != 'success') { $msg = 'error returned from login server'; diff --git a/community_server/src/Controller/TransactionSendCoinsController.php b/community_server/src/Controller/TransactionSendCoinsController.php index 12aa3e69a..033e2343f 100644 --- a/community_server/src/Controller/TransactionSendCoinsController.php +++ b/community_server/src/Controller/TransactionSendCoinsController.php @@ -185,7 +185,8 @@ class TransactionSendCoinsController extends AppController 'memo' => $requestData['memo'], 'amount' => $amountCent, 'target_group' => $known_groups['data']['data']['groups'][$requestData['group']], - 'target_email' => $receiverEmail + 'target_email' => $receiverEmail, + 'blockchain_type' => $this->blockchainType ]), '/createTransaction'); if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) {