From 2134d2ef1085679c343288cb07668a9355793d9f Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Mon, 7 Jun 2021 08:39:34 +0000 Subject: [PATCH 1/8] move decay between transactions into the transactions --- .../src/Model/Table/TransactionsTable.php | 109 ++++++++---------- .../AppRequests/list_transactions.ctp | 5 +- 2 files changed, 49 insertions(+), 65 deletions(-) diff --git a/community_server/src/Model/Table/TransactionsTable.php b/community_server/src/Model/Table/TransactionsTable.php index c4bc4b698..e3c36bf8a 100644 --- a/community_server/src/Model/Table/TransactionsTable.php +++ b/community_server/src/Model/Table/TransactionsTable.php @@ -165,40 +165,7 @@ class TransactionsTable extends Table foreach($stateUserTransactions as $i => $su_transaction) { - /*echo "i: $i
"; - echo "state user transaction:
"; - var_dump($su_transaction); - echo "
";*/ - //var_dump($su_transaction); - //die("step"); - // add decay transactions - $prev = null; - if($i > 0 ) { - $prev = $stateUserTransactions[$i-1]; - } - if($prev && $decay == true) - { - - if($prev->balance > 0) { - // var_dump($stateUserTransactions); - $current = $su_transaction; - //echo "decay between " . $prev->transaction_id . " and " . $current->transaction_id . "
"; - $calculated_decay = $stateBalancesTable->calculateDecay($prev->balance, $prev->balance_date, $current->balance_date, true); - $balance = floatval($prev->balance - $calculated_decay['balance']); - // skip small decays (smaller than 0,00 GDD) - - if(abs($balance) >= 100) { - //echo $interval->format('%R%a days'); - //echo "prev balance: " . $prev->balance . ", diff_amount: $diff_amount, summe: " . (-intval($prev->balance - $diff_amount)) . "
"; - $final_transactions[] = [ - 'type' => 'decay', - 'balance' => $balance, - 'decay_duration' => $calculated_decay['interval']->format('%a days, %H hours, %I minutes, %S seconds'), - 'memo' => '' - ]; - } - } - } + // sender or receiver when user has sended money // group name if creation @@ -207,59 +174,74 @@ class TransactionsTable extends Table // date // balance $transaction = $transaction_indiced[$su_transaction->transaction_id]; - /*echo "transaction:
"; - var_dump($transaction); - echo "
";*/ + + $final_transaction = [ + 'transaction_id' => $transaction->id, + 'date' => $transaction->received, + 'memo' => $transaction->memo + ]; + + $prev = null; + if($i > 0 ) { + $prev = $stateUserTransactions[$i-1]; + } + if($prev && $decay == true) + { + if($prev->balance > 0) { + $current = $su_transaction; + $calculated_decay = $stateBalancesTable->calculateDecay($prev->balance, $prev->balance_date, $current->balance_date, true); + $balance = floatval($prev->balance - $calculated_decay['balance']); + + // skip small decays (smaller than 0,00 GDD) + if(abs($balance) >= 100) { + $final_transaction['decay'] = [ + 'balance' => $balance, + 'decay_duration' => $calculated_decay['interval']->format('%a days, %H hours, %I minutes, %S seconds') + ]; + } + } + } + if($su_transaction->transaction_type_id == 1) { // creation $creation = $transaction->transaction_creation; $balance = $stateBalancesTable->calculateDecay($creation->amount, $creation->target_date, $transaction->received); - $final_transactions[] = [ - 'name' => 'Gradido Akademie', - 'type' => 'creation', - 'transaction_id' => $transaction->id, - 'date' => $transaction->received,// $creation->target_date, - 'target_date' => $creation->target_date, - 'creation_amount' => $creation->amount, - 'balance' => $balance, - 'memo' => $transaction->memo - ]; + $final_transaction['name'] = 'Gradido Akademie'; + $final_transaction['type'] = 'creation'; + $final_transaction['target_date'] = $creation->target_date; + $final_transaction['creation_amount'] = $creation->amount; + $final_transaction['balance'] = $balance; + } else if($su_transaction->transaction_type_id == 2) { // transfer or send coins $sendCoins = $transaction->transaction_send_coin; - $type = ''; $otherUser = null; + $final_transaction['balance'] = $sendCoins->amount; $other_user_public = ''; if ($sendCoins->state_user_id == $user['id']) { - $type = 'send'; + $final_transaction['type'] = 'send'; if(isset($involved_users[$sendCoins->receiver_user_id])) { $otherUser = $involved_users[$sendCoins->receiver_user_id]; } - $other_user_public = bin2hex(stream_get_contents($sendCoins->receiver_public_key)); + $final_transaction['pubkey'] = bin2hex(stream_get_contents($sendCoins->receiver_public_key)); } else if ($sendCoins->receiver_user_id == $user['id']) { - $type = 'receive'; + $final_transaction['type'] = 'receive'; if(isset($involved_users[$sendCoins->state_user_id])) { $otherUser = $involved_users[$sendCoins->state_user_id]; } if($sendCoins->sender_public_key) { - $other_user_public = bin2hex(stream_get_contents($sendCoins->sender_public_key)); + $final_transaction['pubkey'] = bin2hex(stream_get_contents($sendCoins->sender_public_key)); } } if(null == $otherUser) { $otherUser = $stateUsersTable->newEntity(); } - $final_transactions[] = [ - 'name' => $otherUser->first_name . ' ' . $otherUser->last_name, - 'email' => $otherUser->email, - 'type' => $type, - 'transaction_id' => $sendCoins->transaction_id, - 'date' => $transaction->received, - 'balance' => $sendCoins->amount, - 'memo' => $transaction->memo, - 'pubkey' => $other_user_public - ]; + $final_transaction['name'] = $otherUser->first_name . ' ' . $otherUser->last_name; + $final_transaction['email'] = $otherUser->email; } - + + $final_transactions[] = $final_transaction; + if($i == $stateUserTransactionsCount-1 && $decay == true) { $calculated_decay = $stateBalancesTable->calculateDecay( $su_transaction->balance, @@ -275,7 +257,6 @@ class TransactionsTable extends Table 'type' => 'decay', 'balance' => $balance, 'decay_duration' => $duration, - 'last_decay' => true, 'memo' => '' ]; } diff --git a/community_server/src/Template/AppRequests/list_transactions.ctp b/community_server/src/Template/AppRequests/list_transactions.ctp index d7a8b1d6f..f978e2e31 100644 --- a/community_server/src/Template/AppRequests/list_transactions.ctp +++ b/community_server/src/Template/AppRequests/list_transactions.ctp @@ -12,13 +12,16 @@ $body['gdtSum'] = $this->element('centToFloat', ['cent' => $body['gdtSum'], 'pre foreach($body['transactions'] as $i => $transaction) { $useCeil = false; - if(isset($transaction['last_decay']) && $transaction['last_decay']) { + if($transaction['type'] == 'decay') { $useCeil = true; } $body['transactions'][$i]['balance'] = $this->element('centToFloat', ['cent' => $transaction['balance'], 'precision' => 4, 'useCeil' => $useCeil]); if(isset($transaction['creation_amount'])) { $body['transactions'][$i]['creation_amount'] = $this->element('centToFloat', ['cent' => $transaction['creation_amount'], 'precision' => 4]); } + if(isset($transaction['decay'])) { + $body['transactions'][$i]['decay']['balance'] = $this->element('centToFloat', ['cent' => $transaction['decay']['balance'], 'precision' => 4]); + } } ?> \ No newline at end of file From 80d8ea5041c88d85364fb537e368426bb584c20d Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Tue, 22 Jun 2021 08:58:18 +0000 Subject: [PATCH 2/8] update test, fields change order --- .../Controller/AppRequestControllerTest.php | 86 +++++++++---------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php index bff10d997..190aa48b2 100644 --- a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php +++ b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php @@ -5,9 +5,9 @@ use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; /** - * App\Controller\AppRequestsController Test Case + * App\Controller\TransactionJsonRequestHandlerController Test Case * - * @uses \App\Controller\AppRequestsController + * @uses \App\Controller\TransactionJsonRequestHandlerController */ class AppRequestControllerTest extends TestCase { @@ -109,11 +109,9 @@ class AppRequestControllerTest extends TestCase 'public_hex' => '8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d' ] ]); - + $this->getAndParse('/api/get-balance/' . 1211, - ['state' => 'not found', 'msg' => 'invalid session', - 'details' => ['msg' => 'session not found', 'state' => 'not found'] - ] + ['state' => 'not found', 'msg' => 'invalid session'] ); } @@ -130,9 +128,7 @@ class AppRequestControllerTest extends TestCase ]); $this->getAndParse('/api/get-balance/' , - ['state' => 'not found', 'msg' => 'invalid session', - 'details' => ['msg' => 'session not found', 'state' => 'not found'] - ] + ['state' => 'not found', 'msg' => 'invalid session'] ); } @@ -162,81 +158,81 @@ class AppRequestControllerTest extends TestCase "state": "success", "transactions": [ { - "name": "Gradido Akademie", - "type": "creation", "transaction_id": 2, "date": "2021-04-12T00:00:00+00:00", + "memo": "AGE Januar 2021", + "name": "Gradido Akademie", + "type": "creation", "target_date": "2021-01-01T00:00:00+00:00", "creation_amount": 10000000, - "balance": 10000000, - "memo": "AGE Januar 2021" + "balance": 10000000 }, { - "name": "Samuel Schmied", - "email": "test3.yahoo.com", - "type": "send", "transaction_id": 3, "date": "2021-04-12T00:00:00+00:00", - "balance": 1000000, "memo": "test", - "pubkey": "e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2" + "balance": 1000000, + "type": "send", + "pubkey": "e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2", + "name": "Samuel Schmied", + "email": "test3.yahoo.com" }, { - "name": "Samuel Schmied", - "email": "test3.yahoo.com", - "type": "send", "transaction_id": 4, "date": "2021-04-14T00:00:00+00:00", - "balance": 100000, "memo": "test time", - "pubkey": "e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2" + "balance": 100000, + "type": "send", + "pubkey": "e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2", + "name": "Samuel Schmied", + "email": "test3.yahoo.com" }, { - "name": "Samuel Schmied", - "email": "test3.yahoo.com", - "type": "send", "transaction_id": 5, "date": "2021-04-14T09:01:07+00:00", - "balance": 100000, "memo": "test time", - "pubkey": "e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2" + "balance": 100000, + "type": "send", + "pubkey": "e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2", + "name": "Samuel Schmied", + "email": "test3.yahoo.com" }, { - "name": "Samuel Schmied", - "email": "test3.yahoo.com", - "type": "receive", "transaction_id": 6, "date": "2021-04-14T09:02:28+00:00", - "balance": 100000, "memo": "test time 3", - "pubkey": "0000000000000000000000000000000000000000000000000000000000000000" + "balance": 100000, + "type": "receive", + "pubkey": "0000000000000000000000000000000000000000000000000000000000000000", + "name": "Samuel Schmied", + "email": "test3.yahoo.com" }, { - "name": "Samuel Schmied", - "email": "test3.yahoo.com", - "type": "receive", "transaction_id": 7, "date": "2021-04-14T09:28:46+00:00", - "balance": 100000, "memo": "test login crash", - "pubkey": "0000000000000000000000000000000000000000000000000000000000000000" + "balance": 100000, + "type": "receive", + "pubkey": "0000000000000000000000000000000000000000000000000000000000000000", + "name": "Samuel Schmied", + "email": "test3.yahoo.com" }, { - "name": "Samuel Schmied", - "email": "test3.yahoo.com", - "type": "receive", "transaction_id": 8, "date": "2021-04-14T09:31:28+00:00", - "balance": 100000, "memo": "test login crash", - "pubkey": "0000000000000000000000000000000000000000000000000000000000000000" + "balance": 100000, + "type": "receive", + "pubkey": "0000000000000000000000000000000000000000000000000000000000000000", + "name": "Samuel Schmied", + "email": "test3.yahoo.com" } ], "transactionExecutingCount": 0, "count": 7, "gdtSum": 180000, - "timeUsed": 0.5575470924377441, - "decay_date": "2021-05-28T09:35:02+00:00", + "timeUsed": 0.7237420082092285, + "decay_date": "2021-06-22T08:54:43+00:00", "balance": 9100000, "decay": 9100000 }'; From 7b07088a6a2341b28b0cdf9bc65020c5ac922536 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Tue, 22 Jun 2021 11:22:04 +0200 Subject: [PATCH 3/8] fix test --- .../TestCase/Controller/AppRequestControllerTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php index 190aa48b2..bfba41cec 100644 --- a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php +++ b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php @@ -111,7 +111,9 @@ class AppRequestControllerTest extends TestCase ]); $this->getAndParse('/api/get-balance/' . 1211, - ['state' => 'not found', 'msg' => 'invalid session'] + ['state' => 'not found', 'msg' => 'invalid session', + 'details' => ['msg' => 'session not found', 'state' => 'not found'] + ] ); } @@ -128,7 +130,9 @@ class AppRequestControllerTest extends TestCase ]); $this->getAndParse('/api/get-balance/' , - ['state' => 'not found', 'msg' => 'invalid session'] + ['state' => 'not found', 'msg' => 'invalid session', + 'details' => ['msg' => 'session not found', 'state' => 'not found'] + ] ); } From 7a20ebcd9d735957ec09eae3a4df7224ed34ea18 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Tue, 22 Jun 2021 11:23:36 +0200 Subject: [PATCH 4/8] fix warnings --- .../tests/TestCase/Controller/AppRequestControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php index bfba41cec..4435c0457 100644 --- a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php +++ b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php @@ -5,9 +5,9 @@ use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; /** - * App\Controller\TransactionJsonRequestHandlerController Test Case + * App\Controller\AppRequestHandlerController Test Case * - * @uses \App\Controller\TransactionJsonRequestHandlerController + * @uses \App\Controller\AppRequestHandlerController */ class AppRequestControllerTest extends TestCase { From dacd3b10c908d3e3d5c541070f635bea5259a7dc Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Tue, 22 Jun 2021 11:58:01 +0200 Subject: [PATCH 5/8] fix warnings again --- .../tests/TestCase/Controller/AppRequestControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php index 4435c0457..f77efb009 100644 --- a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php +++ b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php @@ -5,9 +5,9 @@ use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; /** - * App\Controller\AppRequestHandlerController Test Case + * App\Controller\AppRequestController Test Case * - * @uses \App\Controller\AppRequestHandlerController + * @uses \App\Controller\AppRequestController */ class AppRequestControllerTest extends TestCase { From 8561265083d43d7b729371579f806834bd730daa Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Tue, 22 Jun 2021 12:08:08 +0200 Subject: [PATCH 6/8] again --- .../tests/TestCase/Controller/AppRequestControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php index f77efb009..a374ae878 100644 --- a/community_server/tests/TestCase/Controller/AppRequestControllerTest.php +++ b/community_server/tests/TestCase/Controller/AppRequestControllerTest.php @@ -5,9 +5,9 @@ use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; /** - * App\Controller\AppRequestController Test Case + * App\Controller\AppRequestsController Test Case * - * @uses \App\Controller\AppRequestController + * @uses \App\Controller\AppRequestsController */ class AppRequestControllerTest extends TestCase { From f9ee57a858624fd111988e0c8b41e766a5b9f12d Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 23 Jun 2021 11:00:50 +0000 Subject: [PATCH 7/8] remove second decay by creation transaction, update old data set --- community_server/src/Controller/AppController.php | 2 +- community_server/src/Controller/MigrationsController.php | 8 ++++++++ .../src/Model/Transactions/TransactionCreation.php | 7 ++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/community_server/src/Controller/AppController.php b/community_server/src/Controller/AppController.php index 2c6344511..7ca966e98 100644 --- a/community_server/src/Controller/AppController.php +++ b/community_server/src/Controller/AppController.php @@ -151,7 +151,7 @@ class AppController extends Controller if($last_migration) { $current_db_version = $last_migration->db_version; } - $php_data_version = 2; + $php_data_version = 3; if($current_db_version < $php_data_version) { $this->redirect(['controller' => 'Migrations', 'action' => 'migrate', 'html' => $html, 'db_version' => $current_db_version]); } diff --git a/community_server/src/Controller/MigrationsController.php b/community_server/src/Controller/MigrationsController.php index 97481aff5..47e3b611a 100644 --- a/community_server/src/Controller/MigrationsController.php +++ b/community_server/src/Controller/MigrationsController.php @@ -69,6 +69,14 @@ class MigrationsController extends AppController [$stateBalancesTable, 'updateAllBalances'] ]; $new_db_version = 2; + } else if($current_db_version == 2) { + $commands = [ + [$stateUserTransactionsTable, 'truncate'], + [$stateBalancesTable, 'truncate'], + [$transactionsTable, 'fillStateUserTransactions'], + [$stateBalancesTable, 'updateAllBalances'] + ]; + $new_db_version = 3; } $migration_result = $this->callFunctions($commands); diff --git a/community_server/src/Model/Transactions/TransactionCreation.php b/community_server/src/Model/Transactions/TransactionCreation.php index 9150d24eb..87f2d2104 100644 --- a/community_server/src/Model/Transactions/TransactionCreation.php +++ b/community_server/src/Model/Transactions/TransactionCreation.php @@ -163,7 +163,8 @@ class TransactionCreation extends TransactionBase { $transactionCreationEntity->target_date = $this->protoTransactionCreation->getTargetDate()->getSeconds(); $target_date = new FrozenTime($transactionCreationEntity->target_date); - $decayed_balance = $stateBalancesTable->calculateDecay($this->getAmount(), $target_date, $received); + //$decayed_balance = $stateBalancesTable->calculateDecay($this->getAmount(), $target_date, $received); + $balance = $this->getAmount(); if(!$this->transactionCreationsTable->save($transactionCreationEntity)) { $this->addError('TransactionCreation::save', 'error saving transactionCreation with errors: ' . json_encode($transactionCreationEntity->getErrors())); @@ -171,13 +172,13 @@ class TransactionCreation extends TransactionBase { } // update state balance - $final_balance = $this->updateStateBalance($receiverUserId, $decayed_balance, $received); + $final_balance = $this->updateStateBalance($receiverUserId, $balance, $received); if(false === $final_balance) { return false; } // decay is a virtual field which is calculated from amount and now() - record_date - if(!$this->addStateUserTransaction($receiverUserId, $transaction_id, 1, $decayed_balance, $received)) { + if(!$this->addStateUserTransaction($receiverUserId, $transaction_id, 1, $balance, $received)) { return false; } From 9a6a3adf76f194298e01706af367045249868293 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 23 Jun 2021 11:14:29 +0000 Subject: [PATCH 8/8] fix test --- community_server/tests/Fixture/Migrations2Fixture.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/community_server/tests/Fixture/Migrations2Fixture.php b/community_server/tests/Fixture/Migrations2Fixture.php index c8608cd18..b86c727ef 100644 --- a/community_server/tests/Fixture/Migrations2Fixture.php +++ b/community_server/tests/Fixture/Migrations2Fixture.php @@ -37,7 +37,7 @@ class Migrations2Fixture extends TestFixture $this->records = [ [ 'id' => 1, - 'db_version' => 2, + 'db_version' => 3, ], ]; parent::init();