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) {
$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]);
}

View File

@ -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);

View File

@ -165,13 +165,22 @@ class TransactionsTable extends Table
foreach($stateUserTransactions as $i => $su_transaction)
{
/*echo "i: $i<br>";
echo "state user transaction: <br>";
var_dump($su_transaction);
echo "<br>";*/
//var_dump($su_transaction);
//die("step");
// add decay transactions
// sender or receiver when user has sended money
// group name if creation
// type: gesendet / empfangen / geschöpft
// transaktion nr / id
// date
// balance
$transaction = $transaction_indiced[$su_transaction->transaction_id];
$final_transaction = [
'transaction_id' => $transaction->id,
'date' => $transaction->received,
'memo' => $transaction->memo
];
$prev = null;
if($i > 0 ) {
$prev = $stateUserTransactions[$i-1];
@ -179,18 +188,15 @@ class TransactionsTable extends Table
if($prev && $decay == true)
{
if($prev->balance > 0) {
$current = $su_transaction;
$current = $su_transaction;
$calculated_decay = $stateBalancesTable->calculateDecay($prev->balance, $prev->balance_date, $current->balance_date, true);
$balance = floatval($prev->balance - $calculated_decay['balance']);
if($balance > 100)
{
$final_transactions[] = [
'type' => 'decay',
$final_transactions['decay'] = [
'balance' => $balance,
'decay_duration' => $calculated_decay['interval']->format('%a days, %H hours, %I minutes, %S seconds'),
'memo' => ''
'decay_duration' => $calculated_decay['interval']->format('%a days, %H hours, %I minutes, %S seconds')
];
}
}
@ -208,52 +214,42 @@ class TransactionsTable extends Table
$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,
@ -264,16 +260,13 @@ class TransactionsTable extends Table
$duration = $decay_start_date->timeAgoInWords();
}
$balance = floatval($su_transaction->balance - $calculated_decay['balance']);
if($balance)
{
$final_transactions[] = [
'type' => 'decay',
'balance' => $balance,
'decay_duration' => $duration,
'last_decay' => true,
'memo' => ''
];
if($balance > 100) {
$final_transactions[] = [
'type' => 'decay',
'balance' => $balance,
'decay_duration' => $duration,
'memo' => ''
];
}
}
}

View File

@ -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;
}

View File

@ -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]);
}
}
?><?= json_encode($body) ?>

View File

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

View File

@ -109,7 +109,7 @@ 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']
@ -162,81 +162,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
}';