/
contract_test.exs
316 lines (268 loc) · 9.62 KB
/
contract_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
defmodule AeMdw.Db.Sync.ContractTest do
use AeMdwWeb.ConnCase, async: false
alias AeMdw.Db.Model
alias AeMdw.AexnContracts
alias AeMdw.Db.AexnCreateContractMutation
alias AeMdw.Db.IntCallsMutation
alias AeMdw.Db.Sync.Contract, as: SyncContract
alias AeMdw.Db.NameTransferMutation
alias AeMdw.Db.NameUpdateMutation
alias AeMdw.Db.OracleExtendMutation
alias AeMdw.Db.OracleRegisterMutation
alias AeMdw.Db.WriteMutation
alias AeMdw.Node
alias AeMdw.Validate
import AeMdw.Node.ContractEventsFixtures
import Mock
require Model
describe "events_mutations/4" do
test "it creates an internal call for each event that's not Chain.create/clone" do
call_txi = 3
account_id =
<<44, 102, 253, 22, 212, 89, 216, 54, 106, 220, 2, 78, 65, 149, 128, 184, 42, 187, 24,
251, 165, 15, 161, 139, 112, 108, 233, 167, 103, 44, 158, 24>>
contract_pk =
<<42, 102, 253, 22, 212, 89, 216, 54, 106, 220, 2, 78, 65, 149, 128, 184, 42, 187, 24,
251, 165, 15, 161, 139, 112, 108, 233, 167, 103, 44, 158, 24>>
tx_1 = {:tx1, account_id}
tx_2 = {:tx2, account_id}
events = [
{{:internal_call_tx, "Call.amount"}, %{info: tx_1}},
{{:internal_call_tx, "Call.amount"}, %{info: tx_2}}
]
int_calls = [
{0, "Call.amount", :spend_tx, tx_1, tx_1},
{1, "Call.amount", :spend_tx, tx_2, tx_1}
]
mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls)
with_mocks [
{:aetx, [], [specialize_type: fn _tx -> {:spend_tx, tx_1} end]},
{Node, [], [tx_ids: fn :spend_tx -> [{:sender_id, 1}] end]}
] do
mutations =
SyncContract.events_mutations(events, {0, 0}, <<>>, call_txi, <<>>, contract_pk)
assert mutation in List.flatten(mutations)
end
end
test "it creates an Field record for each Chain.create/clone event, using the next Call.amount event" do
call_txi = 3
contract_pk =
<<44, 102, 253, 22, 212, 89, 216, 54, 106, 220, 2, 78, 65, 149, 128, 184, 42, 187, 24,
251, 165, 15, 161, 139, 112, 108, 233, 167, 103, 44, 158, 24>>
tx_1 = {:tx1, contract_pk}
tx_2 = {:tx2, contract_pk}
events = [
{{:internal_call_tx, "Call.amount"}, %{info: tx_1}},
{{:internal_call_tx, "Chain.create"}, %{info: :error}},
{{:internal_call_tx, "Call.amount"}, %{info: tx_2}},
{{:internal_call_tx, "Chain.clone"}, %{info: :error}}
]
int_calls = [
{0, "Call.amount", :spend_tx, tx_1, tx_1},
{1, "Call.amount", :spend_tx, tx_2, tx_1}
]
mutation = IntCallsMutation.new(contract_pk, call_txi, int_calls)
with_mocks [
{:aetx, [], [specialize_type: fn _tx -> {:spend_tx, tx_1} end]},
{Node, [], [tx_ids: fn :spend_tx -> [{:sender_id, 1}] end]},
{:aec_spend_tx, [], [recipient_id: fn _tx -> {:id, :account, contract_pk} end]}
] do
mutations =
SyncContract.events_mutations(events, {0, 0}, <<>>, call_txi, <<>>, contract_pk)
assert mutation in List.flatten(mutations)
end
end
test "create aex9 contract with Chain.clone" do
aex9_contract_pk = Validate.id!("ct_2n7xLuQPWWw8Yj8yXPyrhZz3nucu2Cpjg7FxnoTmuVTgGAsUbJ")
tx_events = [
{{:internal_call_tx, "Call.amount"},
%{
info:
{:aetx, :spend_tx, :aec_spend_tx, 88,
{:spend_tx,
{:id, :account,
<<119, 188, 109, 120, 250, 56, 247, 180, 131, 241, 75, 129, 38, 118, 119, 224,
142, 113, 227, 78, 233, 147, 100, 188, 163, 26, 145, 253, 121, 183, 62, 80>>},
{:id, :account, aex9_contract_pk}, 0, 0, 0, 0, "Call.amount"}},
tx_hash:
<<50, 175, 183, 105, 50, 158, 138, 149, 125, 12, 47, 89, 117, 207, 2, 97, 50, 90,
155, 225, 217, 103, 11, 202, 211, 247, 57, 189, 103, 224, 171, 230>>,
type: :contract_call_tx
}},
{{:internal_call_tx, "Chain.clone"},
%{
info: :error,
tx_hash:
<<50, 175, 183, 105, 50, 158, 138, 149, 125, 12, 47, 89, 117, 207, 2, 97, 50, 90,
155, 225, 217, 103, 11, 202, 211, 247, 57, 189, 103, 224, 171, 230>>,
type: :contract_call_tx
}}
]
with_mocks [
{AexnContracts, [],
[
is_aex9?: fn ct_pk -> ct_pk == aex9_contract_pk end,
call_meta_info: fn _type, ct_pk ->
if ct_pk == aex9_contract_pk,
do: {:ok, {"TestAEX9-A vs Wrapped Aeternity", "TAEX9-A/WAE", 18}}
end,
call_extensions: fn _type, _pk -> {:ok, []} end
]}
] do
mutations =
SyncContract.events_mutations(
tx_events,
{554_178, 13},
<<1::256>>,
25_866_736,
<<2::256>>,
25_866_736
)
assert mutations
|> List.flatten()
|> Enum.any?(fn
%AexnCreateContractMutation{} -> true
_other -> false
end)
end
end
test "creates name transfer mutation" do
block_index = {262_167, 0}
call_txi = 11_684_918
event_mutations =
"AENS.transfer"
|> contract_events()
|> SyncContract.events_mutations(block_index, <<>>, call_txi, <<>>, -1)
|> List.flatten()
assert Enum.any?(event_mutations, fn
%NameTransferMutation{
txi_idx: {^call_txi, 0},
block_index: ^block_index
} ->
true
%{} ->
false
end)
end
test "creates name update mutation" do
block_index = {443_440, 0}
call_txi = 23_198_023
event_mutations =
"AENS.update"
|> contract_events()
|> SyncContract.events_mutations(block_index, <<>>, call_txi, <<>>, -1)
|> List.flatten()
assert Enum.any?(event_mutations, fn
%NameUpdateMutation{
txi_idx: {^call_txi, 0},
block_index: ^block_index
} ->
true
%{} ->
false
end)
end
test "register an oracle after Oracle.register putting its origin" do
pubkey =
<<128, 221, 110, 109, 56, 18, 16, 154, 47, 55, 243, 228, 66, 241, 214, 130, 244, 248, 135,
231, 216, 113, 13, 248, 226, 63, 89, 50, 7, 22, 99, 187>>
delta_ttl = 500
{:ok, aetx} =
:aeo_register_tx.new(%{
account_id: :aeser_id.create(:account, pubkey),
nonce: 1,
abi_version: 1,
query_format: "\"foo\"",
response_format: "\"bar\"",
query_fee: 100,
oracle_ttl: {:delta, delta_ttl},
fee: 10_000
})
sync_height = 50_000
expire = sync_height + delta_ttl
call_txi = sync_height * 1_000
block_hash = Validate.id!("mh_FmanZfXX2WWwv6BXMNSDdTgX1TGAknp3Td4aV99SbgRVmEyZo")
call_tx_hash = <<1::256>>
contract_pk = <<2::256>>
mutations =
[{{:internal_call_tx, "Oracle.register"}, %{info: aetx}}]
|> SyncContract.events_mutations(
{sync_height, 0},
block_hash,
call_txi,
call_tx_hash,
contract_pk
)
|> List.flatten()
{_mod, tx} = :aetx.specialize_callback(aetx)
tx_type = :oracle_register_tx
assert [
%WriteMutation{
table: Model.Origin,
record: Model.origin(index: {^tx_type, ^pubkey, ^call_txi})
},
%WriteMutation{
table: Model.RevOrigin,
record: Model.rev_origin(index: {^call_txi, ^tx_type, ^pubkey})
},
%AeMdw.Db.WriteFieldMutation{
pos: nil,
pubkey: ^pubkey,
tx_type: ^tx_type,
txi: ^call_txi
},
%OracleRegisterMutation{oracle_pk: ^pubkey, expire: ^expire},
%IntCallsMutation{
call_txi: ^call_txi,
contract_pk: ^contract_pk,
int_calls: [{0, "Oracle.register", ^tx_type, ^aetx, ^tx}]
}
] = mutations
end
test "extend an oracle after Oracle.extend" do
pubkey =
<<129, 221, 110, 109, 56, 18, 16, 154, 47, 55, 243, 228, 66, 241, 214, 130, 244, 248, 135,
231, 216, 113, 13, 248, 226, 63, 89, 50, 7, 22, 99, 187>>
delta_ttl = 500
{:ok, aetx} =
:aeo_extend_tx.new(%{
oracle_id: :aeser_id.create(:oracle, pubkey),
nonce: 2,
oracle_ttl: {:delta, delta_ttl},
fee: 5_000
})
sync_height = 50_000
block_index = {sync_height, 0}
block_hash = <<1::256>>
call_txi = sync_height * 1_000
call_tx_hash = <<2::256>>
contract_pk = <<3::256>>
mutations =
[{{:internal_call_tx, "Oracle.extend"}, %{info: aetx}}]
|> SyncContract.events_mutations(
block_index,
block_hash,
call_txi,
call_tx_hash,
contract_pk
)
|> List.flatten()
{_mod, tx} = :aetx.specialize_callback(aetx)
assert(
[
%OracleExtendMutation{
oracle_pk: ^pubkey,
block_index: ^block_index,
txi_idx: {^call_txi, 0},
delta_ttl: ^delta_ttl
},
%IntCallsMutation{
call_txi: ^call_txi,
contract_pk: ^contract_pk,
int_calls: [{0, "Oracle.extend", :oracle_extend_tx, ^aetx, ^tx}]
}
] = mutations
)
end
end
end