|
| 1 | +defmodule AeMdw.Db.ChannelsTest do |
| 2 | + use ExUnit.Case, async: false |
| 3 | + |
| 4 | + alias AeMdw.Db.Channels |
| 5 | + alias AeMdw.Db.ChannelCloseMutation |
| 6 | + alias AeMdw.Db.ChannelSpendMutation |
| 7 | + alias AeMdw.Db.ChannelUpdateMutation |
| 8 | + alias AeMdw.TestSamples, as: TS |
| 9 | + |
| 10 | + describe "close_mutual_mutations/2" do |
| 11 | + test "builds a ChannelCloseMutation" do |
| 12 | + channel_pk = TS.address(0) |
| 13 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 14 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 15 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 16 | + release_amount = 3 |
| 17 | + |
| 18 | + mutation = ChannelCloseMutation.new(channel_pk, bi_txi_idx, release_amount) |
| 19 | + |
| 20 | + {:ok, close_mutual_aetx} = |
| 21 | + :aesc_close_mutual_tx.new(%{ |
| 22 | + channel_id: channel_id, |
| 23 | + from_id: account_id, |
| 24 | + initiator_amount_final: 1, |
| 25 | + responder_amount_final: 2, |
| 26 | + ttl: 3, |
| 27 | + fee: 4, |
| 28 | + nonce: 5 |
| 29 | + }) |
| 30 | + |
| 31 | + {:channel_close_mutual_tx, close_mutual_tx} = :aetx.specialize_type(close_mutual_aetx) |
| 32 | + |
| 33 | + mutations = Channels.close_mutual_mutations(bi_txi_idx, close_mutual_tx) |
| 34 | + |
| 35 | + assert mutation in mutations |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + describe "close_solo_mutations/2" do |
| 40 | + test "builds a ChannelUpdateMutation" do |
| 41 | + channel_pk = TS.address(0) |
| 42 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 43 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 44 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 45 | + trees = :aec_trees.new() |
| 46 | + poi = :aec_trees.new_poi(trees) |
| 47 | + |
| 48 | + mutation = ChannelUpdateMutation.new(channel_pk, bi_txi_idx) |
| 49 | + |
| 50 | + {:ok, close_solo_aetx} = |
| 51 | + :aesc_close_solo_tx.new(%{ |
| 52 | + channel_id: channel_id, |
| 53 | + from_id: account_id, |
| 54 | + payload: "", |
| 55 | + poi: poi, |
| 56 | + fee: 1, |
| 57 | + nonce: 2 |
| 58 | + }) |
| 59 | + |
| 60 | + {:channel_close_solo_tx, close_solo_tx} = :aetx.specialize_type(close_solo_aetx) |
| 61 | + |
| 62 | + mutations = Channels.close_solo_mutations(bi_txi_idx, close_solo_tx) |
| 63 | + |
| 64 | + assert mutation in mutations |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe "set_delegates_mutations/2" do |
| 69 | + test "builds a ChannelUpdateMutation" do |
| 70 | + channel_pk = TS.address(0) |
| 71 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 72 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 73 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 74 | + |
| 75 | + mutation = ChannelUpdateMutation.new(channel_pk, bi_txi_idx) |
| 76 | + |
| 77 | + {:ok, set_delegates_aetx} = |
| 78 | + :aesc_set_delegates_tx.new(%{ |
| 79 | + channel_id: channel_id, |
| 80 | + from_id: account_id, |
| 81 | + initiator_delegate_ids: [], |
| 82 | + responder_delegate_ids: [], |
| 83 | + payload: "", |
| 84 | + state_hash: "", |
| 85 | + round: 1, |
| 86 | + ttl: 2, |
| 87 | + fee: 3, |
| 88 | + nonce: 4 |
| 89 | + }) |
| 90 | + |
| 91 | + {:channel_set_delegates_tx, set_delegates_tx} = :aetx.specialize_type(set_delegates_aetx) |
| 92 | + |
| 93 | + mutations = Channels.set_delegates_mutations(bi_txi_idx, set_delegates_tx) |
| 94 | + |
| 95 | + assert mutation in mutations |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + describe "force_progress_mutations/2" do |
| 100 | + test "builds a ChannelUpdateMutation" do |
| 101 | + channel_pk = TS.address(0) |
| 102 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 103 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 104 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 105 | + account_pk1 = :aeser_id.create(:account, TS.address(2)) |
| 106 | + account_pk2 = :aeser_id.create(:account, TS.address(3)) |
| 107 | + update = :aesc_offchain_update.op_transfer(account_pk1, account_pk2, 30) |
| 108 | + trees = :aec_trees.new() |
| 109 | + |
| 110 | + mutation = ChannelUpdateMutation.new(channel_pk, bi_txi_idx) |
| 111 | + |
| 112 | + {:ok, force_progress_aetx} = |
| 113 | + :aesc_force_progress_tx.new(%{ |
| 114 | + channel_id: channel_id, |
| 115 | + from_id: account_id, |
| 116 | + payload: "", |
| 117 | + update: update, |
| 118 | + state_hash: "", |
| 119 | + round: 1, |
| 120 | + offchain_trees: trees, |
| 121 | + fee: 3, |
| 122 | + nonce: 4 |
| 123 | + }) |
| 124 | + |
| 125 | + {:channel_force_progress_tx, force_progress_tx} = :aetx.specialize_type(force_progress_aetx) |
| 126 | + |
| 127 | + mutations = Channels.force_progress_mutations(bi_txi_idx, force_progress_tx) |
| 128 | + |
| 129 | + assert mutation in mutations |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + describe "slash_mutations/2" do |
| 134 | + test "builds a ChannelUpdateMutation" do |
| 135 | + channel_pk = TS.address(0) |
| 136 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 137 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 138 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 139 | + trees = :aec_trees.new() |
| 140 | + poi = :aec_trees.new_poi(trees) |
| 141 | + |
| 142 | + mutation = ChannelUpdateMutation.new(channel_pk, bi_txi_idx) |
| 143 | + |
| 144 | + {:ok, slash_aetx} = |
| 145 | + :aesc_slash_tx.new(%{ |
| 146 | + channel_id: channel_id, |
| 147 | + from_id: account_id, |
| 148 | + payload: "", |
| 149 | + poi: poi, |
| 150 | + fee: 3, |
| 151 | + nonce: 4 |
| 152 | + }) |
| 153 | + |
| 154 | + {:channel_slash_tx, slash_tx} = :aetx.specialize_type(slash_aetx) |
| 155 | + |
| 156 | + mutations = Channels.slash_mutations(bi_txi_idx, slash_tx) |
| 157 | + |
| 158 | + assert mutation in mutations |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + describe "deposit_mutations/2" do |
| 163 | + test "builds a ChannelSpendMutation" do |
| 164 | + channel_pk = TS.address(0) |
| 165 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 166 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 167 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 168 | + |
| 169 | + {:ok, state_hash} = |
| 170 | + :aeser_api_encoder.safe_decode( |
| 171 | + :state, |
| 172 | + "st_Wwxms0IVM7PPCHpeOXWeeZZm8h5p/SuqZL7IHIbr3CqtlCL+" |
| 173 | + ) |
| 174 | + |
| 175 | + mutation = ChannelSpendMutation.new(channel_pk, bi_txi_idx, 23) |
| 176 | + |
| 177 | + {:ok, deposit_aetx} = |
| 178 | + :aesc_deposit_tx.new(%{ |
| 179 | + channel_id: channel_id, |
| 180 | + from_id: account_id, |
| 181 | + amount: 23, |
| 182 | + fee: 3, |
| 183 | + state_hash: state_hash, |
| 184 | + round: 4, |
| 185 | + nonce: 5 |
| 186 | + }) |
| 187 | + |
| 188 | + {:channel_deposit_tx, deposit_tx} = :aetx.specialize_type(deposit_aetx) |
| 189 | + |
| 190 | + mutations = Channels.deposit_mutations(bi_txi_idx, deposit_tx) |
| 191 | + |
| 192 | + assert mutation in mutations |
| 193 | + end |
| 194 | + end |
| 195 | + |
| 196 | + describe "withdraw_mutations/2" do |
| 197 | + test "builds a ChannelSpendMutation" do |
| 198 | + channel_pk = TS.address(0) |
| 199 | + channel_id = :aeser_id.create(:channel, channel_pk) |
| 200 | + account_id = :aeser_id.create(:account, TS.address(1)) |
| 201 | + bi_txi_idx = {{123, 0}, {456, -1}} |
| 202 | + |
| 203 | + {:ok, state_hash} = |
| 204 | + :aeser_api_encoder.safe_decode( |
| 205 | + :state, |
| 206 | + "st_Wwxms0IVM7PPCHpeOXWeeZZm8h5p/SuqZL7IHIbr3CqtlCL+" |
| 207 | + ) |
| 208 | + |
| 209 | + mutation = ChannelSpendMutation.new(channel_pk, bi_txi_idx, -23) |
| 210 | + |
| 211 | + {:ok, withdraw_aetx} = |
| 212 | + :aesc_withdraw_tx.new(%{ |
| 213 | + channel_id: channel_id, |
| 214 | + to_id: account_id, |
| 215 | + amount: 23, |
| 216 | + fee: 3, |
| 217 | + state_hash: state_hash, |
| 218 | + round: 4, |
| 219 | + nonce: 5 |
| 220 | + }) |
| 221 | + |
| 222 | + {:channel_withdraw_tx, withdraw_tx} = :aetx.specialize_type(withdraw_aetx) |
| 223 | + |
| 224 | + mutations = Channels.withdraw_mutations(bi_txi_idx, withdraw_tx) |
| 225 | + |
| 226 | + assert mutation in mutations |
| 227 | + end |
| 228 | + end |
| 229 | +end |
0 commit comments