Skip to content

Commit

Permalink
channels need programmable delay
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-bitcoin committed Feb 9, 2017
1 parent 7551ed8 commit c32959c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/consensus/channel.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(channel).
-export([new/8,serialize/1,deserialize/1,update/9,
-export([new/9,serialize/1,deserialize/1,update/9,
write/2,get/2,delete/2,root_hash/1,
acc1/1,acc2/1,id/1,bal1/1,bal2/1,
last_modified/1, mode/1,entropy/1,
Expand Down Expand Up @@ -81,15 +81,16 @@ update(ID, Channels, Nonce, NewRent,Inc1, Inc2, Mode, Delay, Height) ->
mode = Mode
}.

new(ID, Acc1, Acc2, Bal1, Bal2, Height, Entropy, Rent) ->
new(ID, Acc1, Acc2, Bal1, Bal2, Height, Entropy, Rent, Delay) ->
D = if
Rent > 0 -> 1;
true -> 0
end,
#channel{id = ID, acc1 = Acc1, acc2 = Acc2,
bal1 = Bal1, bal2 = Bal2,
last_modified = Height, entropy = Entropy,
rent = abs(Rent), rent_direction = D}.
rent = abs(Rent), rent_direction = D,
delay = Delay}.
serialize(C) ->
ACC = constants:acc_bits(),
BAL = constants:balance_bits(),
Expand Down
17 changes: 11 additions & 6 deletions src/consensus/txs/new_channel_tx.erl
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
-module(new_channel_tx).
-export([doit/4, make/9, good/1, spk/2, cid/1,
-export([doit/4, make/10, good/1, spk/2, cid/1,
entropy/1, acc1/1, acc2/1, id/1]).
-record(nc, {acc1 = 0, acc2 = 0, fee = 0, nonce = 0,
bal1 = 0, bal2 = 0, rent = 0, entropy = 0,
id = -1}).
delay = 10, id = -1}).

acc1(X) -> X#nc.acc1.
acc2(X) -> X#nc.acc2.
id(X) -> X#nc.id.
good(Tx) ->
%make sure that our the money is a fair balance of ours and theirs.
%make sure that the money is a fair balance of ours and theirs.
Delay = Tx#nc.delay,
true = Delay > free_constants:min_channel_delay(),
true = Delay < free_constants:max_channel_delay(),
K = keys:id(),
Acc1 = Tx#nc.acc1,
Acc2 = Tx#nc.acc2,
Expand All @@ -30,15 +33,16 @@ entropy(Tx) -> Tx#nc.entropy.
spk(Tx, Delay) -> spk:new(Tx#nc.acc1, Tx#nc.acc2, Tx#nc.id,
[], 0,0, Delay, 0,
Tx#nc.entropy).
make(ID,Accounts,Acc1,Acc2,Inc1,Inc2,Rent,Entropy,Fee) ->
make(ID,Accounts,Acc1,Acc2,Inc1,Inc2,Rent,Entropy,Delay, Fee) ->
{_, A, Proof} = account:get(Acc1, Accounts),
Nonce = account:nonce(A),
{_, _, Proof2} = account:get(Acc2, Accounts),
%Entropy = channel_feeder:entropy(ID, [Acc1, Acc2])+1,
true = (Rent == 0) or (Rent == 1),
Tx = #nc{id = ID, acc1 = Acc1, acc2 = Acc2,
fee = Fee, nonce = Nonce+1, bal1 = Inc1,
bal2 = Inc2, entropy = Entropy, rent = Rent
bal2 = Inc2, entropy = Entropy, rent = Rent,
delay = Delay
},
{Tx, [Proof, Proof2]}.

Expand All @@ -54,7 +58,8 @@ doit(Tx, Channels, Accounts, NewHeight) ->
true = Bal2 >= 0,
Rent = Tx#nc.rent,
Entropy = Tx#nc.entropy,
NewChannel = channel:new(ID, Aid1, Aid2, Bal1, Bal2, NewHeight, Entropy, Rent),
Delay = Tx#nc.delay,
NewChannel = channel:new(ID, Aid1, Aid2, Bal1, Bal2, NewHeight, Entropy, Rent, Delay),
NewChannels = channel:write(NewChannel, Channels),
CCFee = constants:create_channel_fee() div 2,
Acc1 = account:update(Aid1, Accounts, -Bal1-CCFee, Tx#nc.nonce, NewHeight),
Expand Down
12 changes: 9 additions & 3 deletions src/easy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ close_channel() ->
solo_close_channel() ->
{ok, Other} = talker:talk({id}, constants:server_ip(), constants:server_port()),
internal_handler:doit({channel_solo_close, Other}).
channel_timeout() ->
{ok, Other} = talker:talk({id}, constants:server_ip(), constants:server_port()),
Fee = free_constants:tx_fee(),
{Accounts,Channels,_,_} = tx_pool:data(),
{ok, CD} = channel_manager:read(Other),
CID = channel_feeder:cid(CD),
{Tx, _} = channel_timeout:make(keys:id(), Accounts, Channels, CID, Fee),
Stx = keys:sign(Tx, Accounts),
tx_pool_feeder:absorb(Stx).

to_int(X) ->
round(X * constants:token_decimals()).




grow_channel(CID, Bal1, Bal2, Rent, Fee) ->
F = fun(Accounts, Channels) ->
Expand Down
4 changes: 4 additions & 0 deletions src/free_constants.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ space_limit() ->

vm(SS, State) ->
chalang:vm(SS, time_limit(), space_limit(), constants:fun_limit(), constants:var_limit(), State).

min_channel_delay() -> 10.
max_channel_delay() -> 100.

0 comments on commit c32959c

Please sign in to comment.