-
Notifications
You must be signed in to change notification settings - Fork 37
[GH-759] Channel Snapshot #791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
09f7a0d
a6fee0d
7bb42f1
78ac4bc
fc11b32
d64bb11
083daca
26454eb
2fdccc2
796ebf6
e16afee
1a36b28
de77959
8aa153a
82efbd4
6ba26cf
9aebc2f
d09837c
968a735
6417a5b
4e571ba
a15f34d
a4f29ce
e52b8b5
8b63df0
f519f76
1f8baa0
1a5c469
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ defmodule Aecore.Channel.ChannelStateOnChain do | |
| # Parameters | ||
| - initiator_pubkey | ||
| - responder_pubkey | ||
| - delegates - list of delegates alowed to perform certain operations | ||
| - delegates - list of delegates allowed to perform certain operations | ||
| - total_amount - the total amount of tokens in the channel | ||
| - initiator_amount - amount deposited by initiator in create_tx or from poi | ||
| - responder_amount - amount deposited by responder in create_tx or from poi | ||
|
|
@@ -147,6 +147,31 @@ defmodule Aecore.Channel.ChannelStateOnChain do | |
| {initiator_pubkey, responder_pubkey} | ||
| end | ||
|
|
||
| @spec is_peer?(ChannelStateOnChain.t(), Keys.pubkey()) :: boolean() | ||
| def is_peer?( | ||
| %ChannelStateOnChain{ | ||
| initiator_pubkey: initiator_pubkey, | ||
| responder_pubkey: responder_pubkey | ||
| }, | ||
| pubkey | ||
| ) | ||
| when is_binary(pubkey) do | ||
| pubkey in [initiator_pubkey, responder_pubkey] | ||
| end | ||
|
|
||
| @spec is_peer_or_delegate?(ChannelStateOnChain.t(), Keys.pubkey()) :: boolean() | ||
| def is_peer_or_delegate?( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also remove "is_" |
||
| %ChannelStateOnChain{ | ||
| initiator_pubkey: initiator_pubkey, | ||
| responder_pubkey: responder_pubkey, | ||
| delegates: delegates | ||
| }, | ||
| pubkey | ||
| ) | ||
| when is_binary(pubkey) do | ||
| pubkey in [initiator_pubkey, responder_pubkey | delegates] | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns true if the channel wasn't slashed. (Closed channels should be removed from the Channels state tree) | ||
| """ | ||
|
|
@@ -248,7 +273,7 @@ defmodule Aecore.Channel.ChannelStateOnChain do | |
|
|
||
| channel.sequence >= offchain_tx.sequence -> | ||
| {:error, | ||
| "#{__MODULE__}: OffChain state is too old, expected newer than #{channel.sequence}, got #{ | ||
| "#{__MODULE__}: OffChainTx is too old, expected newer than #{channel.sequence}, got #{ | ||
| offchain_tx.sequence | ||
| }"} | ||
|
|
||
|
|
@@ -334,32 +359,22 @@ defmodule Aecore.Channel.ChannelStateOnChain do | |
|
|
||
| @spec validate_withdraw( | ||
| ChannelStateOnChain.t(), | ||
| Keys.pubkey(), | ||
| non_neg_integer(), | ||
| non_neg_integer() | ||
| ) :: :ok | error() | ||
| def validate_withdraw( | ||
| %ChannelStateOnChain{ | ||
| initiator_pubkey: initiator_pubkey, | ||
| responder_pubkey: responder_pubkey, | ||
| total_amount: total_amount, | ||
| sequence: sequence, | ||
| channel_reserve: channel_reserve | ||
| }, | ||
| tx_account, | ||
| tx_amount, | ||
| tx_sequence | ||
| ) do | ||
| cond do | ||
| sequence >= tx_sequence -> | ||
| {:error, "Too old state - latest known sequence is #{sequence} but we got #{tx_sequence}"} | ||
|
|
||
| tx_account != initiator_pubkey and tx_account != responder_pubkey -> | ||
| {:error, | ||
| "Withdraw destination must be a party of this channel. Tried to withdraw from #{ | ||
| tx_account | ||
| } but the parties are #{initiator_pubkey} and #{responder_pubkey}"} | ||
|
|
||
| tx_amount < 0 -> | ||
| {:error, "Withdraw of negative amount(#{tx_amount}) is forbidden"} | ||
|
|
||
|
|
@@ -397,34 +412,23 @@ defmodule Aecore.Channel.ChannelStateOnChain do | |
|
|
||
| @spec validate_deposit( | ||
| ChannelStateOnChain.t(), | ||
| Keys.pubkey(), | ||
| non_neg_integer(), | ||
| non_neg_integer() | ||
| ) :: :ok | error() | ||
| def validate_deposit( | ||
| %ChannelStateOnChain{ | ||
| initiator_pubkey: initiator_pubkey, | ||
| responder_pubkey: responder_pubkey, | ||
| sequence: sequence | ||
| }, | ||
| tx_account, | ||
| tx_amount, | ||
| tx_sequence | ||
| ) | ||
| when is_binary(tx_account) do | ||
| ) do | ||
| cond do | ||
| sequence >= tx_sequence -> | ||
| {:error, "Too old state - latest known sequence is #{sequence} but we got #{tx_sequence}"} | ||
|
|
||
| tx_amount < 0 -> | ||
| {:error, "Deposit of negative amount(#{tx_amount}) is forbidden"} | ||
|
|
||
| tx_account != initiator_pubkey and tx_account != responder_pubkey -> | ||
| {:error, | ||
| "Deposit destination must be a party of this channel. Tried to deposit tokens to #{ | ||
| tx_account | ||
| } but the parties are #{initiator_pubkey} and #{responder_pubkey}"} | ||
|
|
||
| true -> | ||
| :ok | ||
| end | ||
|
|
@@ -451,6 +455,41 @@ defmodule Aecore.Channel.ChannelStateOnChain do | |
| } | ||
| end | ||
|
|
||
| @doc """ | ||
| Validates the payload of ChannelSnapshotSoloTx. | ||
| """ | ||
| @spec validate_snapshot(ChannelStateOnChain.t(), ChannelOffChainTx.t()) :: | ||
| :ok | {:error, binary()} | ||
| def validate_snapshot( | ||
| %ChannelStateOnChain{} = channel, | ||
| %ChannelOffChainTx{} = offchain_tx | ||
| ) do | ||
| if channel.sequence < offchain_tx.sequence do | ||
| ChannelOffChainTx.verify_signatures(offchain_tx, pubkeys(channel)) | ||
| else | ||
| {:error, | ||
| "#{__MODULE__}: OffChainTx is too old, expected newer than #{channel.sequence}, got #{ | ||
| offchain_tx.sequence | ||
| }"} | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Applies the provided payload of ChannelSnapshotSoloTx to the state of the channel. The contents should be validated by &validate_snapshot/2 | ||
| """ | ||
| @spec apply_snapshot(ChannelStateOnChain.t(), ChannelOffChainTx.t()) :: ChannelStateOnChain.t() | ||
| def apply_snapshot(%ChannelStateOnChain{} = channel, %ChannelOffChainTx{ | ||
| sequence: sequence, | ||
| state_hash: state_hash | ||
| }) do | ||
| %ChannelStateOnChain{ | ||
| channel | ||
| | sequence: sequence, | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| state_hash: state_hash, | ||
| solo_sequence: 0 | ||
| } | ||
| end | ||
|
|
||
| @spec encode_to_list(ChannelStateOnChain.t()) :: list(binary()) | ||
| def encode_to_list(%ChannelStateOnChain{} = channel) do | ||
| [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,8 +221,14 @@ defmodule Aecore.Channel.Tx.ChannelDepositTx do | |
| !ChannelStateOnChain.active?(channel) -> | ||
| {:error, "#{__MODULE__}: Can't deposit from inactive channel."} | ||
|
|
||
| !ChannelStateOnChain.is_peer?(channel, depositing_account) -> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you move this check from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to avoid passing the sender to the on chain channel code as it is used only in one place. |
||
| {:error, | ||
| "Deposit destination must be a party of this channel. Tried to deposit tokens to #{ | ||
| depositing_account | ||
| } but the parties are #{channel.initiator_pubkey} and #{channel.responder_pubkey}"} | ||
|
|
||
| true -> | ||
| ChannelStateOnChain.validate_deposit(channel, depositing_account, amount, sequence) | ||
| ChannelStateOnChain.validate_deposit(channel, amount, sequence) | ||
| end | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is_" is not needed. Boolean-ness is represented by "?". This follows the convention we have in our code. (see:
active?,settled?)