|
1 | 1 | defmodule AeMdwWeb.BlockControllerTest do
|
2 | 2 | use AeMdwWeb.ConnCase, async: false
|
3 | 3 |
|
4 |
| - alias :aeser_api_encoder, as: Enc |
5 |
| - alias AeMdw.Validate |
6 |
| - alias AeMdw.Db.{Model, Format} |
7 |
| - alias AeMdwWeb.TestUtil |
8 |
| - alias AeMdw.Error.Input, as: ErrInput |
9 |
| - require Model |
| 4 | + alias AeMdwWeb.BlockchainSim |
10 | 5 |
|
11 |
| - import AeMdw.Db.Util |
12 | 6 | import Mock
|
| 7 | + import AeMdwWeb.BlockchainSim |
13 | 8 |
|
14 | 9 | describe "block" do
|
15 | 10 | test "get key block by hash", %{conn: conn} do
|
16 |
| - kb_hash = "kh_29Gmo8RMdCD5aJ1UUrKd6Kx2c3tvHQu82HKsnVhbprmQnFy5bn" |
17 |
| - bin_kb_hash = AeMdw.Validate.id!(kb_hash) |
| 11 | + with_blockchain %{alice: 10_000}, b1: {:kb, :alice} do |
| 12 | + kb_hash = blocks[:b1] |
18 | 13 |
|
19 |
| - with_mocks [ |
20 |
| - {:aec_chain, [], get_block: fn ^bin_kb_hash -> {:ok, sample_key_block()} end}, |
21 |
| - {:aec_db, [], get_header: fn ^bin_kb_hash -> sample_key_header() end} |
22 |
| - ] do |
23 |
| - conn = get(conn, "/block/#{kb_hash}") |
24 |
| - |
25 |
| - assert json_response(conn, 200) == TestUtil.handle_input(fn -> get_block(kb_hash) end) |
| 14 | + assert %{"hash" => ^kb_hash} = get(conn, "/block/#{kb_hash}") |> json_response(200) |
26 | 15 | end
|
27 | 16 | end
|
28 | 17 |
|
29 | 18 | test "get micro block by hash", %{conn: conn} do
|
30 |
| - mb_hash = "mh_RuKG8scokoAdhqNN3uvq29VZBsSaZdpeaoyBsXLeGV69sud85" |
31 |
| - bin_mb_hash = AeMdw.Validate.id!(mb_hash) |
32 |
| - |
33 |
| - with_mocks [ |
34 |
| - {:aec_chain, [], get_block: fn ^bin_mb_hash -> {:ok, sample_micro_block()} end}, |
35 |
| - {:aec_db, [], get_header: fn ^bin_mb_hash -> sample_micro_header() end} |
36 |
| - ] do |
37 |
| - conn = get(conn, "/block/#{mb_hash}") |
| 19 | + with_blockchain %{alice: 10_000, bob: 20_000}, |
| 20 | + b1: [ |
| 21 | + t1: BlockchainSim.spend_tx(:alice, :bob, 5_000) |
| 22 | + ] do |
| 23 | + mb_hash = blocks[:b1] |
38 | 24 |
|
39 |
| - assert json_response(conn, 200) == TestUtil.handle_input(fn -> get_block(mb_hash) end) |
| 25 | + assert %{"hash" => ^mb_hash} = get(conn, "/block/#{mb_hash}") |> json_response(200) |
40 | 26 | end
|
41 | 27 | end
|
42 | 28 |
|
43 | 29 | test "renders error when the hash is invalid", %{conn: conn} do
|
44 | 30 | hash = "kh_NoSuchHash"
|
45 |
| - conn = get(conn, "/block/#{hash}") |
46 |
| - |
47 |
| - assert json_response(conn, 400) == %{ |
48 |
| - "error" => TestUtil.handle_input(fn -> get_block(hash) end) |
49 |
| - } |
50 |
| - end |
51 |
| - end |
52 |
| - |
53 |
| - ################ |
54 |
| - |
55 |
| - defp get_block(enc_block_hash) when is_binary(enc_block_hash) do |
56 |
| - block_hash = Validate.id!(enc_block_hash) |
57 |
| - |
58 |
| - case :aec_chain.get_block(block_hash) do |
59 |
| - {:ok, _} -> |
60 |
| - Format.to_map({:block, {nil, nil}, nil, block_hash}) |
61 |
| - |
62 |
| - :error -> |
63 |
| - raise ErrInput.NotFound, value: enc_block_hash |
64 |
| - end |
65 |
| - end |
66 |
| - |
67 |
| - defp get_block({_, mbi} = block_index) do |
68 |
| - case read_block(block_index) do |
69 |
| - [block] -> |
70 |
| - type = (mbi == -1 && :key_block_hash) || :micro_block_hash |
71 |
| - hash = Model.block(block, :hash) |
72 |
| - get_block(Enc.encode(type, hash)) |
73 | 31 |
|
74 |
| - [] -> |
75 |
| - raise ErrInput.NotFound, value: block_index |
| 32 | + assert %{"error" => <<"invalid id: ", _rest::binary>>} = |
| 33 | + get(conn, "/block/#{hash}") |> json_response(400) |
76 | 34 | end
|
77 | 35 | end
|
78 |
| - |
79 |
| - defp sample_key_block do |
80 |
| - {:key_block, sample_key_header()} |
81 |
| - end |
82 |
| - |
83 |
| - defp sample_key_header do |
84 |
| - {:key_header, 1, |
85 |
| - <<108, 21, 218, 110, 191, 175, 2, 120, 254, 175, 77, 241, 176, 241, 169, 130, 85, 7, 174, |
86 |
| - 123, 154, 73, 75, 195, 76, 145, 113, 63, 56, 221, 87, 131>>, |
87 |
| - <<108, 21, 218, 110, 191, 175, 2, 120, 254, 175, 77, 241, 176, 241, 169, 130, 85, 7, 174, |
88 |
| - 123, 154, 73, 75, 195, 76, 145, 113, 63, 56, 221, 87, 131>>, |
89 |
| - <<52, 183, 229, 249, 54, 69, 51, 88, 116, 15, 122, 6, 182, 198, 8, 237, 95, 88, 152, 76, 53, |
90 |
| - 115, 239, 229, 75, 84, 120, 17, 7, 73, 153, 49>>, 522_133_279, 7_537_663_592_980_547_537, |
91 |
| - 1_543_373_685_748, 1, |
92 |
| - [ |
93 |
| - 26_922_260, |
94 |
| - 37_852_188, |
95 |
| - 59_020_115, |
96 |
| - 60_279_463, |
97 |
| - 79_991_400, |
98 |
| - 85_247_410, |
99 |
| - 107_259_316, |
100 |
| - 109_139_865, |
101 |
| - 110_742_806, |
102 |
| - 135_064_096, |
103 |
| - 135_147_996, |
104 |
| - 168_331_414, |
105 |
| - 172_261_759, |
106 |
| - 199_593_922, |
107 |
| - 202_230_201, |
108 |
| - 203_701_465, |
109 |
| - 210_434_810, |
110 |
| - 231_398_482, |
111 |
| - 262_809_482, |
112 |
| - 271_994_744, |
113 |
| - 272_584_245, |
114 |
| - 287_928_914, |
115 |
| - 292_169_553, |
116 |
| - 362_488_698, |
117 |
| - 364_101_896, |
118 |
| - 364_186_805, |
119 |
| - 373_099_116, |
120 |
| - 398_793_711, |
121 |
| - 400_070_528, |
122 |
| - 409_055_423, |
123 |
| - 410_928_197, |
124 |
| - 423_334_086, |
125 |
| - 423_561_843, |
126 |
| - 428_130_074, |
127 |
| - 496_454_011, |
128 |
| - 501_715_005, |
129 |
| - 505_858_333, |
130 |
| - 514_079_183, |
131 |
| - 522_053_501, |
132 |
| - 526_239_399, |
133 |
| - 527_666_844, |
134 |
| - 532_070_334 |
135 |
| - ], |
136 |
| - <<109, 80, 187, 72, 39, 0, 181, 159, 179, 75, 226, 70, 33, 153, 149, 169, 59, 82, 131, 166, |
137 |
| - 223, 128, 104, 223, 115, 204, 111, 77, 205, 5, 56, 247>>, |
138 |
| - <<186, 203, 214, 163, 246, 107, 124, 137, 222, 135, 217, 193, 221, 104, 215, 16, 94, 25, 47, |
139 |
| - 35, 97, 96, 99, 179, 23, 38, 226, 135, 232, 249, 24, 44>>, "", |
140 |
| - %{consensus: :aec_consensus_bitcoin_ng}} |
141 |
| - end |
142 |
| - |
143 |
| - defp sample_micro_block do |
144 |
| - {:mic_block, sample_micro_header()} |
145 |
| - end |
146 |
| - |
147 |
| - defp sample_micro_header do |
148 |
| - {:mic_header, 305_488, "", |
149 |
| - <<123, 66, 245, 198, 197, 131, 107, 129, 76, 33, 48, 83, 69, 18, 29, 92, 34, 125, 232, 194, |
150 |
| - 72, 143, 41, 63, 18, 139, 120, 116, 45, 79, 16, 108>>, |
151 |
| - <<205, 135, 56, 162, 96, 202, 59, 7, 215, 179, 229, 109, 41, 29, 214, 107, 35, 50, 95, 154, |
152 |
| - 219, 228, 142, 169, 53, 232, 166, 4, 232, 147, 188, 64>>, |
153 |
| - <<159, 240, 58, 171, 83, 153, 27, 217, 82, 171, 254, 252, 207, 84, 95, 53, 51, 74, 232, 74, |
154 |
| - 71, 119, 195, 119, 76, 151, 185, 56, 200, 189, 193, 78>>, |
155 |
| - <<105, 101, 147, 121, 43, 123, 67, 195, 141, 128, 83, 57, 81, 64, 38, 102, 16, 183, 151, 198, |
156 |
| - 70, 31, 124, 51, 136, 54, 61, 145, 175, 206, 242, 131, 139, 7, 85, 12, 93, 191, 223, 205, |
157 |
| - 50, 239, 189, 136, 12, 18, 31, 47, 127, 94, 194, 131, 254, 70, 243, 168, 236, 149, 63, 101, |
158 |
| - 84, 78, 219, 6>>, |
159 |
| - <<155, 74, 110, 105, 160, 87, 202, 235, 211, 79, 100, 7, 204, 19, 228, 89, 48, 64, 212, 231, |
160 |
| - 175, 166, 195, 25, 170, 195, 160, 121, 134, 181, 73, 200>>, 1_598_562_036_727, 4, |
161 |
| - %{consensus: :aec_consensus_bitcoin_ng}} |
162 |
| - end |
163 | 36 | end
|
0 commit comments