@@ -47,7 +47,12 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
47
47
@ impl true
48
48
def handle_cast ( { :add_block , % SignedBeaconBlock { message: block } = signed_block } , state ) do
49
49
block_root = Ssz . hash_tree_root! ( block )
50
- { :noreply , Map . put ( state , block_root , { signed_block , :pending } ) }
50
+
51
+ if state |> Map . get ( block_root ) do
52
+ { :noreply , state }
53
+ else
54
+ { :noreply , state |> Map . put ( block_root , { signed_block , :pending } ) }
55
+ end
51
56
end
52
57
53
58
@ impl true
@@ -77,22 +82,23 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
77
82
|> Enum . sort_by ( fn { _ , signed_block } -> signed_block . message . slot end )
78
83
|> Enum . reduce ( state , fn { block_root , signed_block } , state ->
79
84
parent_root = signed_block . message . parent_root
85
+ parent_status = get_block_status ( state , parent_root )
80
86
81
87
cond do
82
88
# If already processed, remove it
83
89
Blocks . get_block ( block_root ) ->
84
90
state |> Map . delete ( block_root )
85
91
86
92
# If parent is invalid, block is invalid
87
- get_block_status ( state , parent_root ) == :invalid ->
93
+ parent_status == :invalid ->
88
94
state |> Map . put ( block_root , { nil , :invalid } )
89
95
90
96
# If parent is processing, block is pending
91
- get_block_status ( state , parent_root ) == :processing ->
97
+ parent_status == :processing ->
92
98
state
93
99
94
100
# If parent is pending, block is pending
95
- get_block_status ( state , parent_root ) == :pending ->
101
+ parent_status == :pending ->
96
102
state
97
103
98
104
# If parent is not in fork choice, download parent
@@ -117,20 +123,16 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
117
123
blocks_to_download = state |> Map . filter ( fn { _ , { _ , s } } -> s == :download end ) |> Map . keys ( )
118
124
119
125
downloaded_blocks =
120
- if Enum . empty? ( blocks_to_download ) do
121
- [ ]
122
- else
123
- blocks_to_download
124
- |> Enum . take ( 16 )
125
- |> BlockDownloader . request_blocks_by_root ( )
126
- |> case do
127
- { :ok , signed_blocks } ->
128
- signed_blocks
129
-
130
- { :error , reason } ->
131
- Logger . debug ( "Block download failed: '#{ reason } '" )
132
- [ ]
133
- end
126
+ blocks_to_download
127
+ |> Enum . take ( 16 )
128
+ |> BlockDownloader . request_blocks_by_root ( )
129
+ |> case do
130
+ { :ok , signed_blocks } ->
131
+ signed_blocks
132
+
133
+ { :error , reason } ->
134
+ Logger . debug ( "Block download failed: '#{ reason } '" )
135
+ [ ]
134
136
end
135
137
136
138
new_state =
0 commit comments