Skip to content

Commit cd52d83

Browse files
f3r10MegaRedHand
andauthored
test: add beacon api unit tests (#658)
Co-authored-by: Tomás Grüner <47506558+MegaRedHand@users.noreply.github.com>
1 parent 7ad93f2 commit cd52d83

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/unit/beacon_api_test.exs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
defmodule BeaconApiTest do
2+
use ExUnit.Case
3+
use Plug.Test
4+
use Patch
5+
alias BeaconApi.Router
6+
alias LambdaEthereumConsensus.Store.BlockStore
7+
alias LambdaEthereumConsensus.Store.Db
8+
9+
@moduletag :beacon_api_case
10+
11+
@opts Router.init([])
12+
13+
setup do
14+
Application.put_env(:lambda_ethereum_consensus, ChainSpec, config: MainnetConfig)
15+
16+
head_root =
17+
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18+
0, 0>>
19+
20+
status_message = %Types.StatusMessage{
21+
fork_digest: Fixtures.Random.binary(4),
22+
finalized_root: Fixtures.Random.root(),
23+
finalized_epoch: Fixtures.Random.uint64(),
24+
head_root: head_root,
25+
head_slot: Fixtures.Random.uint64()
26+
}
27+
28+
start_supervised!(Db)
29+
30+
patch(
31+
LambdaEthereumConsensus.Beacon.BeaconChain,
32+
:get_current_status_message,
33+
{:ok, status_message}
34+
)
35+
36+
:ok
37+
end
38+
39+
test "get state SSZ HashTreeRoot by head" do
40+
head_root =
41+
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42+
0, 0>>
43+
44+
signed_block = Fixtures.Block.signed_beacon_block()
45+
BlockStore.store_block(signed_block, head_root)
46+
47+
resp_body = %{
48+
data: %{root: "0x" <> Base.encode16(signed_block.message.state_root, case: :lower)},
49+
finalized: false,
50+
execution_optimistic: true
51+
}
52+
53+
{:ok, encoded_resp_body_json} = Jason.encode(resp_body)
54+
55+
conn =
56+
:get
57+
|> conn("/eth/v1/beacon/states/head/root", nil)
58+
|> Router.call(@opts)
59+
60+
assert conn.state == :sent
61+
assert conn.status == 200
62+
assert conn.resp_body == encoded_resp_body_json
63+
end
64+
65+
test "get invalid state SSZ HashTreeRoot" do
66+
resp_body = %{
67+
code: 400,
68+
message: "Invalid state ID: unknown_state"
69+
}
70+
71+
{:ok, encoded_resp_body_json} = Jason.encode(resp_body)
72+
73+
conn =
74+
:get
75+
|> conn("/eth/v1/beacon/states/unknown_state/root", nil)
76+
|> Router.call(@opts)
77+
78+
assert conn.state == :sent
79+
assert conn.status == 400
80+
assert conn.resp_body == encoded_resp_body_json
81+
end
82+
end

0 commit comments

Comments
 (0)