Skip to content

Commit 8b605a7

Browse files
authored
feat: add gas to microblocks (#2023)
1 parent 09233c0 commit 8b605a7

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

docs/swagger_v3/blocks.spec.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ schemas:
9696
hash: "mh_JryFr55GwbEbEJivkvHwBPzqbhu6AvpcZqCiBoXzgZdLmNn2K"
9797
height: 685072
9898
pof_hash: "no_fraud"
99+
gas: 1950000
99100
prev_hash: "mh_hH3qADCrnGvuup6JwjtNSacHLd9h3NJzAgr173jS9KUqijfTB"
100101
prev_key_hash: "kh_2rTj3FTZJ6fnuLFDJwAiDQZat2a23Lkp5uZQooxjwXEFhH4Vtw"
101102
signature: "sg_8hhU15cVMbukFj4FbdrFwwYnbzXYPnstu9PDnWZGbfQNpeHcB6tK1F3wvG1MPYySARgDRJYUh3YPJD3HctFwg6Y4rUGSR"
@@ -116,6 +117,7 @@ schemas:
116117
- signature
117118
- time
118119
- version
120+
- gas
119121
properties:
120122
micro_block_index:
121123
description: The index of the micro-block on the micro-block height, starting from 0
@@ -129,6 +131,9 @@ schemas:
129131
height:
130132
description: The block height
131133
type: integer
134+
gas:
135+
description: The gas used by the microblock
136+
type: integer
132137
pof_hash:
133138
description: \"no_fraud\" or api encoded Proof of Fraud hash
134139
type: string

lib/ae_mdw/blocks.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,15 @@ defmodule AeMdw.Blocks do
257257
end
258258
end
259259

260-
header = :aec_db.get_header(mb_hash)
260+
block = :aec_db.get_block(mb_hash)
261+
header = :aec_blocks.to_header(block)
262+
gas = :aec_blocks.gas(block)
261263

262264
header
263265
|> :aec_headers.serialize_for_client(Db.prev_block_type(header))
264266
|> Map.put(:micro_block_index, mbi)
265267
|> Map.put(:transactions_count, txs_count)
268+
|> Map.put(:gas, gas)
266269
end
267270

268271
defp render_blocks(state, range, sort_mbs?) do

test/ae_mdw_web/controllers/block_controller_test.exs

+22-11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ defmodule AeMdwWeb.BlockControllerTest do
114114
store: store
115115
} do
116116
kbi = 1
117+
gas = 123
117118
hash = TS.key_block_hash(1)
118119
encoded_hash = Enc.encode(:key_block_hash, hash)
119120

@@ -135,9 +136,10 @@ defmodule AeMdwWeb.BlockControllerTest do
135136
|> Store.put(Model.Block, Model.block(index: {kbi + 1, -1}, tx_index: 17, hash: hash))
136137

137138
with_mocks [
138-
{:aec_db, [], [get_header: fn _mb_hash -> :header end]},
139+
{:aec_db, [],
140+
[get_header: fn _mb_hash -> :header end, get_block: fn _mb_hash -> :block end]},
139141
{:aec_chain, [], [get_block: fn ^hash -> {:ok, :block} end]},
140-
{:aec_blocks, [], [to_header: fn :block -> :header end]},
142+
{:aec_blocks, [], [to_header: fn :block -> :header end, gas: fn :block -> gas end]},
141143
{AeMdw.Node.Db, [],
142144
[
143145
prev_block_type: fn :header -> :micro end,
@@ -158,17 +160,20 @@ defmodule AeMdwWeb.BlockControllerTest do
158160

159161
assert %{
160162
"height" => ^kbi,
161-
"transactions_count" => 10
163+
"transactions_count" => 10,
164+
"gas" => ^gas
162165
} = block1
163166

164167
assert %{
165168
"height" => ^kbi,
166-
"transactions_count" => 5
169+
"transactions_count" => 5,
170+
"gas" => ^gas
167171
} = block2
168172

169173
assert %{
170174
"height" => ^kbi,
171-
"transactions_count" => 2
175+
"transactions_count" => 2,
176+
"gas" => ^gas
172177
} = block3
173178
end
174179
end
@@ -313,6 +318,7 @@ defmodule AeMdwWeb.BlockControllerTest do
313318
describe "micro-block" do
314319
test "it returns a micro block with txs counts", %{conn: conn, store: store} do
315320
kbi = 1
321+
gas = 123
316322
decoded_hash = TS.micro_block_hash(0)
317323
encoded_hash = :aeser_api_encoder.encode(:micro_block_hash, decoded_hash)
318324

@@ -333,8 +339,9 @@ defmodule AeMdwWeb.BlockControllerTest do
333339

334340
with_mocks [
335341
{:aec_chain, [], [get_block: fn ^decoded_hash -> {:ok, :block} end]},
336-
{:aec_db, [], [get_header: fn ^decoded_hash -> :header end]},
337-
{:aec_blocks, [], [to_header: fn :block -> :header end]},
342+
{:aec_db, [],
343+
[get_header: fn ^decoded_hash -> :header end, get_block: fn ^decoded_hash -> :block end]},
344+
{:aec_blocks, [], [to_header: fn :block -> :header end, gas: fn :block -> gas end]},
338345
{:aec_headers, [],
339346
[
340347
height: fn :header -> kbi end,
@@ -351,7 +358,8 @@ defmodule AeMdwWeb.BlockControllerTest do
351358
assert %{
352359
"height" => ^kbi,
353360
"micro_block_index" => 0,
354-
"transactions_count" => 6
361+
"transactions_count" => 6,
362+
"gas" => ^gas
355363
} =
356364
conn
357365
|> with_store(store)
@@ -365,6 +373,7 @@ defmodule AeMdwWeb.BlockControllerTest do
365373
store: store
366374
} do
367375
kbi = 1
376+
gas = 123
368377
decoded_hash = TS.micro_block_hash(0)
369378
encoded_hash = :aeser_api_encoder.encode(:micro_block_hash, decoded_hash)
370379

@@ -385,8 +394,9 @@ defmodule AeMdwWeb.BlockControllerTest do
385394

386395
with_mocks [
387396
{:aec_chain, [], [get_block: fn ^decoded_hash -> {:ok, :block} end]},
388-
{:aec_db, [], [get_header: fn ^decoded_hash -> :header end]},
389-
{:aec_blocks, [], [to_header: fn :block -> :header end]},
397+
{:aec_db, [],
398+
[get_header: fn ^decoded_hash -> :header end, get_block: fn ^decoded_hash -> :block end]},
399+
{:aec_blocks, [], [to_header: fn :block -> :header end, gas: fn :block -> gas end]},
390400
{:aec_headers, [],
391401
[
392402
height: fn :header -> kbi end,
@@ -403,7 +413,8 @@ defmodule AeMdwWeb.BlockControllerTest do
403413
assert %{
404414
"height" => ^kbi,
405415
"micro_block_index" => 0,
406-
"transactions_count" => 6
416+
"transactions_count" => 6,
417+
"gas" => ^gas
407418
} =
408419
conn
409420
|> with_store(store)

test/integration/ae_mdw_web/controllers/block_controller_test.exs

+17-15
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ defmodule Integration.AeMdwWeb.BlockControllerTest do
187187

188188
test "get micro block by given key block index(height) and micro block index", %{conn: conn} do
189189
kbi = 305_222
190-
mbi = 3
191-
conn = get(conn, "/v2/blocks/#{kbi}/#{mbi}")
190+
mb = "mh_SidHcZxja5FMYKLTf2gkhSZvLvoWkugck1NXZp2T7yzxeewQk"
191+
conn = get(conn, "/v3/micro-blocks/#{mb}")
192192

193193
auto_assert(
194194
%{
195-
"hash" => "mh_SidHcZxja5FMYKLTf2gkhSZvLvoWkugck1NXZp2T7yzxeewQk",
195+
"hash" => ^mb,
196196
"height" => ^kbi,
197197
"pof_hash" => "no_fraud",
198198
"prev_hash" => "mh_2uTHJ6AFFK2WicxKK26EPiS8ZD8gJfE6n22JspwVLFdZQsBSFU",
@@ -202,7 +202,8 @@ defmodule Integration.AeMdwWeb.BlockControllerTest do
202202
"state_hash" => "bs_zLCQ5W4uQgrPc8XVJbGUhSmMZvkcxTTwtyg9757TCVik7RxkH",
203203
"time" => 1_598_513_060_178,
204204
"txs_hash" => "bx_rs2yKJHpwADFgjuogFvSpcqYXSy6WjcN7Xw45rJLNCrrwtELB",
205-
"version" => 4
205+
"version" => 4,
206+
"gas" => 19_340
206207
} <- json_response(conn, 200)
207208
)
208209
end
@@ -214,20 +215,21 @@ defmodule Integration.AeMdwWeb.BlockControllerTest do
214215
auto_assert(%{"error" => "invalid hash: invalid"} <- json_response(conn, 400))
215216
end
216217

217-
test "renders error when mickro block index is not present", %{conn: conn} do
218-
kbi = 305_222
219-
mbi = 4999
220-
conn = get(conn, "/v2/blocks/#{kbi}/#{mbi}")
218+
test "renders error when micro block hash is not present", %{conn: conn} do
219+
mb_hash = :aeapi.format_micro_block_hash(<<1::256>>)
220+
conn = get(conn, "/v3/micro-blocks/#{mb_hash}")
221221

222-
auto_assert(%{"error" => "not found: {305222, 4999}"} <- json_response(conn, 404))
222+
auto_assert(
223+
%{"error" => "not found: mh_11111111111111111111111111111118qjnEr"} <-
224+
json_response(conn, 404)
225+
)
223226
end
224227

225-
test "renders error when micro block index is invalid", %{conn: conn} do
226-
kbi = 305_222
228+
test "renders error when micro block hash is invalid", %{conn: conn} do
227229
mbi = "invalid"
228-
conn = get(conn, "/v2/blocks/#{kbi}/#{mbi}")
230+
conn = get(conn, "/v3/micro-blocks/#{mbi}")
229231

230-
auto_assert(%{"error" => "invalid block index: 305222/invalid"} <- json_response(conn, 400))
232+
auto_assert(%{"error" => "invalid hash: invalid"} <- json_response(conn, 400))
231233
end
232234
end
233235

@@ -367,7 +369,7 @@ defmodule Integration.AeMdwWeb.BlockControllerTest do
367369

368370
test "renders error when the range is invalid", %{conn: conn} do
369371
range = "invalid"
370-
conn = get(conn, "/v2/blocks", scope: "gen:#{range}")
372+
conn = get(conn, "/v3/key-blocks", scope: "gen:#{range}")
371373

372374
assert json_response(conn, 400) ==
373375
%{"error" => "invalid range: #{range}"}
@@ -389,7 +391,7 @@ defmodule Integration.AeMdwWeb.BlockControllerTest do
389391

390392
assert %{"height" => ^height} =
391393
conn
392-
|> get("/v2/blocks/#{height}")
394+
|> get("/v3/key-blocks/#{height}")
393395
|> json_response(200)
394396
end
395397
end

0 commit comments

Comments
 (0)