Skip to content

Conversation

@cheezus1
Copy link
Contributor

@cheezus1 cheezus1 commented Nov 30, 2017

closes #125, #138

peers_max_count: 4

config :aecore, :tx_data,
lock_time_block: 10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name it lock_time_coinbase


update_block_state(updated_block_state, transaction.data.to_acc, transaction.data.value, 0)
add_to_amount = latest_block_height + 1 !=
transaction.data.lock_time_block - Application.get_env(:aecore, :tx_data)[:lock_time_block]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't get the env here, pass it to the function

chain_state |>
Enum.map(fn{_account, data} -> data.balance end) |>
Enum.sum()
chain_state
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe return a tuple of {total_tokens, total_unlocked_tokens, total_locked_tokens}

Copy link
Collaborator

@thepiwo thepiwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test to check if locked token can't actually be spent

Copy link
Contributor

@cytadela8 cytadela8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally good, but please address mine and thepiwo comments.

{amount_update_value + amount, updated_locked}

true ->
{amount_update_value, updated_locked}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't happen. Let's log an error here.

@spec update_chain_state_locked(map(), integer()) :: map()
def update_chain_state_locked(chain_state, new_block_height) do
Enum.reduce(chain_state, %{}, fn({account, %{balance: balance, nonce: nonce, locked: locked}}, acc) ->
{locked_amount, updated_locked} =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call locked_amount: unlocked_amount or just_unlocked_amount or to_unlock_amount to be less confusing.


new_account_state = %{balance: block_state_filled_empty[account].balance + value,
nonce: new_nonce}
new_locked = if(value > 0) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If add_to_amount is true we add value to account twice (we duplicate tokens!). One's on current block and again on lock_time_block!

What we should do instead is check add_to_amount and do one of two things: add to balance or add to locked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fixed

Copy link
Contributor

@cytadela8 cytadela8 Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so if value < 0 then add_to_amount should be always true. If add_to_amount is false then the call is a no-op and we should produce an error to logs or maybe raise some exception or assert? @thepiwo What should we do if there in such a case (what is "the-best-practise")?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should raise an exception, but why handle the case at all?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cheezus1 here is the answer

ChainState.calculate_block_state(block.txs)
assert %{"a" => %{balance: -9, nonce: 102,
locked: [%{amount: 10, block: lock_time_block}]},
"b" => %{nonce: 1, balance: -11,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that's the same as before, but why do we actually get negative balances?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, good question

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because amounts are spent this block, but the received amounts for each are locked for one block? Correct me if I am wrong

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cytadela8 this is just the block_state indicating the change this block makes to the chain_state, negative values occur for every token spent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, right. This is not the chain_state after the block. It's delta chain_state.

@thepiwo
Copy link
Collaborator

thepiwo commented Dec 12, 2017

@cheezus1 is there a test to check if locked token can't actually be spent?

Copy link
Contributor

@cytadela8 cytadela8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Copy link
Contributor

@cytadela8 cytadela8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last issue.


new_account_state = %{balance: block_state_filled_empty[account].balance + value,
nonce: new_nonce}
new_locked = if(value > 0) do
Copy link
Contributor

@cytadela8 cytadela8 Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so if value < 0 then add_to_amount should be always true. If add_to_amount is false then the call is a no-op and we should produce an error to logs or maybe raise some exception or assert? @thepiwo What should we do if there in such a case (what is "the-best-practise")?

Copy link
Contributor

@cytadela8 cytadela8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good now. Great work!

Copy link
Contributor

@cytadela8 cytadela8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question + can we add a test to check if locked coins can't be spend and after locktime they can?

Map.get(Chain.chain_state, account1_pub_key, %{nonce: 0}).nonce + 1, 1)
Map.get(Chain.chain_state, account1_pub_key, %{nonce: 0}).nonce + 1, 1,
Chain.latest_block().header.height +
Application.get_env(:aecore, :tx_data)[:lock_time_coinbase] + 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we add locktime to all of the transactions in tests?

@spec sign_tx(binary(), integer(), integer(), integer(), integer()) :: {:ok, %SignedTx{}}
def sign_tx(to_acc, value, nonce, fee, lock_time_block) do
def sign_tx(to_acc, value, nonce, fee,
lock_time_block \\ Chain.latest_block().header.height + 1) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you want to lock a transaction by default? I would set the default to 0

cheezus1 added 3 commits December 18, 2017 14:16
@cytadela8
Copy link
Contributor

@cheezus1 We still have merge conflicts

@thepiwo thepiwo merged commit 740376c into master Dec 18, 2017
@thepiwo thepiwo deleted the GH-125 branch October 5, 2018 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add lock time to transaction

6 participants