Merge pull request #483 from gradido/community_decay_inside

move decay between transactions into the transactions
This commit is contained in:
einhornimmond 2021-06-25 11:42:39 +02:00 committed by GitHub
commit bdf1638e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 92 deletions

View File

@ -151,7 +151,7 @@ class AppController extends Controller
if($last_migration) { if($last_migration) {
$current_db_version = $last_migration->db_version; $current_db_version = $last_migration->db_version;
} }
$php_data_version = 2; $php_data_version = 3;
if($current_db_version < $php_data_version) { if($current_db_version < $php_data_version) {
$this->redirect(['controller' => 'Migrations', 'action' => 'migrate', 'html' => $html, 'db_version' => $current_db_version]); $this->redirect(['controller' => 'Migrations', 'action' => 'migrate', 'html' => $html, 'db_version' => $current_db_version]);
} }

View File

@ -69,6 +69,14 @@ class MigrationsController extends AppController
[$stateBalancesTable, 'updateAllBalances'] [$stateBalancesTable, 'updateAllBalances']
]; ];
$new_db_version = 2; $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); $migration_result = $this->callFunctions($commands);

View File

@ -165,13 +165,22 @@ class TransactionsTable extends Table
foreach($stateUserTransactions as $i => $su_transaction) foreach($stateUserTransactions as $i => $su_transaction)
{ {
/*echo "i: $i<br>";
echo "state user transaction: <br>";
var_dump($su_transaction); // sender or receiver when user has sended money
echo "<br>";*/ // group name if creation
//var_dump($su_transaction); // type: gesendet / empfangen / geschöpft
//die("step"); // transaktion nr / id
// add decay transactions // date
// balance
$transaction = $transaction_indiced[$su_transaction->transaction_id];
$final_transaction = [
'transaction_id' => $transaction->id,
'date' => $transaction->received,
'memo' => $transaction->memo
];
$prev = null; $prev = null;
if($i > 0 ) { if($i > 0 ) {
$prev = $stateUserTransactions[$i-1]; $prev = $stateUserTransactions[$i-1];
@ -180,17 +189,14 @@ class TransactionsTable extends Table
{ {
if($prev->balance > 0) { if($prev->balance > 0) {
$current = $su_transaction; $current = $su_transaction;
$calculated_decay = $stateBalancesTable->calculateDecay($prev->balance, $prev->balance_date, $current->balance_date, true); $calculated_decay = $stateBalancesTable->calculateDecay($prev->balance, $prev->balance_date, $current->balance_date, true);
$balance = floatval($prev->balance - $calculated_decay['balance']); $balance = floatval($prev->balance - $calculated_decay['balance']);
if($balance > 100) if($balance > 100)
{ {
$final_transactions[] = [ $final_transactions['decay'] = [
'type' => 'decay',
'balance' => $balance, 'balance' => $balance,
'decay_duration' => $calculated_decay['interval']->format('%a days, %H hours, %I minutes, %S seconds'), 'decay_duration' => $calculated_decay['interval']->format('%a days, %H hours, %I minutes, %S seconds')
'memo' => ''
]; ];
} }
} }
@ -208,52 +214,42 @@ class TransactionsTable extends Table
$creation = $transaction->transaction_creation; $creation = $transaction->transaction_creation;
$balance = $stateBalancesTable->calculateDecay($creation->amount, $creation->target_date, $transaction->received); $balance = $stateBalancesTable->calculateDecay($creation->amount, $creation->target_date, $transaction->received);
$final_transactions[] = [ $final_transaction['name'] = 'Gradido Akademie';
'name' => 'Gradido Akademie', $final_transaction['type'] = 'creation';
'type' => 'creation', $final_transaction['target_date'] = $creation->target_date;
'transaction_id' => $transaction->id, $final_transaction['creation_amount'] = $creation->amount;
'date' => $transaction->received,// $creation->target_date, $final_transaction['balance'] = $balance;
'target_date' => $creation->target_date,
'creation_amount' => $creation->amount,
'balance' => $balance,
'memo' => $transaction->memo
];
} else if($su_transaction->transaction_type_id == 2) { // transfer or send coins } else if($su_transaction->transaction_type_id == 2) { // transfer or send coins
$sendCoins = $transaction->transaction_send_coin; $sendCoins = $transaction->transaction_send_coin;
$type = '';
$otherUser = null; $otherUser = null;
$final_transaction['balance'] = $sendCoins->amount;
$other_user_public = ''; $other_user_public = '';
if ($sendCoins->state_user_id == $user['id']) { if ($sendCoins->state_user_id == $user['id']) {
$type = 'send'; $final_transaction['type'] = 'send';
if(isset($involved_users[$sendCoins->receiver_user_id])) { if(isset($involved_users[$sendCoins->receiver_user_id])) {
$otherUser = $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']) { } else if ($sendCoins->receiver_user_id == $user['id']) {
$type = 'receive'; $final_transaction['type'] = 'receive';
if(isset($involved_users[$sendCoins->state_user_id])) { if(isset($involved_users[$sendCoins->state_user_id])) {
$otherUser = $involved_users[$sendCoins->state_user_id]; $otherUser = $involved_users[$sendCoins->state_user_id];
} }
if($sendCoins->sender_public_key) { 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) { if(null == $otherUser) {
$otherUser = $stateUsersTable->newEntity(); $otherUser = $stateUsersTable->newEntity();
} }
$final_transactions[] = [ $final_transaction['name'] = $otherUser->first_name . ' ' . $otherUser->last_name;
'name' => $otherUser->first_name . ' ' . $otherUser->last_name, $final_transaction['email'] = $otherUser->email;
'email' => $otherUser->email,
'type' => $type,
'transaction_id' => $sendCoins->transaction_id,
'date' => $transaction->received,
'balance' => $sendCoins->amount,
'memo' => $transaction->memo,
'pubkey' => $other_user_public
];
} }
$final_transactions[] = $final_transaction;
if($i == $stateUserTransactionsCount-1 && $decay == true) { if($i == $stateUserTransactionsCount-1 && $decay == true) {
$calculated_decay = $stateBalancesTable->calculateDecay( $calculated_decay = $stateBalancesTable->calculateDecay(
$su_transaction->balance, $su_transaction->balance,
@ -264,14 +260,11 @@ class TransactionsTable extends Table
$duration = $decay_start_date->timeAgoInWords(); $duration = $decay_start_date->timeAgoInWords();
} }
$balance = floatval($su_transaction->balance - $calculated_decay['balance']); $balance = floatval($su_transaction->balance - $calculated_decay['balance']);
if($balance > 100) {
if($balance)
{
$final_transactions[] = [ $final_transactions[] = [
'type' => 'decay', 'type' => 'decay',
'balance' => $balance, 'balance' => $balance,
'decay_duration' => $duration, 'decay_duration' => $duration,
'last_decay' => true,
'memo' => '' 'memo' => ''
]; ];
} }

View File

@ -163,7 +163,8 @@ class TransactionCreation extends TransactionBase {
$transactionCreationEntity->target_date = $this->protoTransactionCreation->getTargetDate()->getSeconds(); $transactionCreationEntity->target_date = $this->protoTransactionCreation->getTargetDate()->getSeconds();
$target_date = new FrozenTime($transactionCreationEntity->target_date); $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)) { if(!$this->transactionCreationsTable->save($transactionCreationEntity)) {
$this->addError('TransactionCreation::save', 'error saving transactionCreation with errors: ' . json_encode($transactionCreationEntity->getErrors())); $this->addError('TransactionCreation::save', 'error saving transactionCreation with errors: ' . json_encode($transactionCreationEntity->getErrors()));
@ -171,13 +172,13 @@ class TransactionCreation extends TransactionBase {
} }
// update state balance // update state balance
$final_balance = $this->updateStateBalance($receiverUserId, $decayed_balance, $received); $final_balance = $this->updateStateBalance($receiverUserId, $balance, $received);
if(false === $final_balance) { if(false === $final_balance) {
return false; return false;
} }
// decay is a virtual field which is calculated from amount and now() - record_date // 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; return false;
} }

View File

@ -12,13 +12,16 @@ $body['gdtSum'] = $this->element('centToFloat', ['cent' => $body['gdtSum'], 'pre
foreach($body['transactions'] as $i => $transaction) { foreach($body['transactions'] as $i => $transaction) {
$useCeil = false; $useCeil = false;
if(isset($transaction['last_decay']) && $transaction['last_decay']) { if($transaction['type'] == 'decay') {
$useCeil = true; $useCeil = true;
} }
$body['transactions'][$i]['balance'] = $this->element('centToFloat', ['cent' => $transaction['balance'], 'precision' => 4, 'useCeil' => $useCeil]); $body['transactions'][$i]['balance'] = $this->element('centToFloat', ['cent' => $transaction['balance'], 'precision' => 4, 'useCeil' => $useCeil]);
if(isset($transaction['creation_amount'])) { if(isset($transaction['creation_amount'])) {
$body['transactions'][$i]['creation_amount'] = $this->element('centToFloat', ['cent' => $transaction['creation_amount'], 'precision' => 4]); $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]);
}
} }
?><?= json_encode($body) ?> ?><?= json_encode($body) ?>

View File

@ -37,7 +37,7 @@ class Migrations2Fixture extends TestFixture
$this->records = [ $this->records = [
[ [
'id' => 1, 'id' => 1,
'db_version' => 2, 'db_version' => 3,
], ],
]; ];
parent::init(); parent::init();

View File

@ -162,81 +162,81 @@ class AppRequestControllerTest extends TestCase
"state": "success", "state": "success",
"transactions": [ "transactions": [
{ {
"name": "Gradido Akademie",
"type": "creation",
"transaction_id": 2, "transaction_id": 2,
"date": "2021-04-12T00:00:00+00:00", "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", "target_date": "2021-01-01T00:00:00+00:00",
"creation_amount": 10000000, "creation_amount": 10000000,
"balance": 10000000, "balance": 10000000
"memo": "AGE Januar 2021"
}, },
{ {
"name": "Samuel Schmied",
"email": "test3.yahoo.com",
"type": "send",
"transaction_id": 3, "transaction_id": 3,
"date": "2021-04-12T00:00:00+00:00", "date": "2021-04-12T00:00:00+00:00",
"balance": 1000000,
"memo": "test", "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, "transaction_id": 4,
"date": "2021-04-14T00:00:00+00:00", "date": "2021-04-14T00:00:00+00:00",
"balance": 100000,
"memo": "test time", "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, "transaction_id": 5,
"date": "2021-04-14T09:01:07+00:00", "date": "2021-04-14T09:01:07+00:00",
"balance": 100000,
"memo": "test time", "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, "transaction_id": 6,
"date": "2021-04-14T09:02:28+00:00", "date": "2021-04-14T09:02:28+00:00",
"balance": 100000,
"memo": "test time 3", "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, "transaction_id": 7,
"date": "2021-04-14T09:28:46+00:00", "date": "2021-04-14T09:28:46+00:00",
"balance": 100000,
"memo": "test login crash", "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, "transaction_id": 8,
"date": "2021-04-14T09:31:28+00:00", "date": "2021-04-14T09:31:28+00:00",
"balance": 100000,
"memo": "test login crash", "memo": "test login crash",
"pubkey": "0000000000000000000000000000000000000000000000000000000000000000" "balance": 100000,
"type": "receive",
"pubkey": "0000000000000000000000000000000000000000000000000000000000000000",
"name": "Samuel Schmied",
"email": "test3.yahoo.com"
} }
], ],
"transactionExecutingCount": 0, "transactionExecutingCount": 0,
"count": 7, "count": 7,
"gdtSum": 180000, "gdtSum": 180000,
"timeUsed": 0.5575470924377441, "timeUsed": 0.7237420082092285,
"decay_date": "2021-05-28T09:35:02+00:00", "decay_date": "2021-06-22T08:54:43+00:00",
"balance": 9100000, "balance": 9100000,
"decay": 9100000 "decay": 9100000
}'; }';