Skip to content

Commit

Permalink
only call process_blocks when needed. Add logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkenan committed Jul 2, 2024
1 parent 17bfef2 commit 6eb8e61
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
26 changes: 17 additions & 9 deletions lib/lambda_ethereum_consensus/beacon/pending_blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
missing_blobs = missing_blobs(block_info)

if Enum.empty?(missing_blobs) do
block_info
Blocks.new_block_info(block_info)

if process_block(block_info) in [:transitioned, :invalid] do

Check warning on line 47 in lib/lambda_ethereum_consensus/beacon/pending_blocks.ex

View workflow job for this annotation

GitHub Actions / Lint

Function body is nested too deep (max depth is 2, was 3).
process_blocks()
end
else
BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1)
block_info |> BlockInfo.change_status(:download_blobs)
end
|> Blocks.new_block_info()

if process_block(block_info) in [:transitioned, :invalid] do
process_blocks()
block_info
|> BlockInfo.change_status(:download_blobs)
|> Blocks.new_block_info()
end
end
end
Expand All @@ -68,10 +70,12 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
|> Enum.each(fn root ->
with %BlockInfo{} = block_info <- Blocks.get_block_info(root) do
if Enum.empty?(missing_blobs(block_info)) do
Blocks.change_status(block_info, :pending)
end
new_block_info = Blocks.change_status(block_info, :pending)

process_block(block_info)
if process_block(new_block_info) in [:transitioned, :invalid] do

Check warning on line 75 in lib/lambda_ethereum_consensus/beacon/pending_blocks.ex

View workflow job for this annotation

GitHub Actions / Lint

Function body is nested too deep (max depth is 2, was 3).
process_blocks()
end
end
end
end)
end
Expand Down Expand Up @@ -104,6 +108,10 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do
end

defp process_block(block_info) do
if block_info.status != :pending do
Logger.error("Called process block for a block that's not ready: #{block_info}")
end

parent_root = block_info.signed_block.message.parent_root

case Blocks.get_block_info(parent_root) do
Expand Down
22 changes: 10 additions & 12 deletions lib/lambda_ethereum_consensus/store/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,22 @@ defmodule LambdaEthereumConsensus.Store.Blocks do
BlockDb.add_root_to_status(block_info.root, block_info.status)
end

@spec change_status(BlockInfo.t(), BlockInfo.block_status()) :: :ok
@doc """
Changes the status of a block in the db. Returns the block with the modified status.
"""
@spec change_status(BlockInfo.t(), BlockInfo.block_status()) :: BlockInfo.t()
def change_status(block_info, status) do
Metrics.block_status(block_info.root, status)

IO.puts(
"Block: #{Utils.format_shorten_binary(block_info.root)}. Changing status from #{block_info.status} to #{status}"
)
IO.puts("Changing status for #{block_info} to #{status}")

old_status = block_info.status

block_info
|> BlockInfo.change_status(status)
|> tap(fn bi ->
IO.puts("Status for #{Utils.format_shorten_binary(bi.root)}: #{bi.status}")
end)
|> store_block_info()
new_block_info = BlockInfo.change_status(block_info, status)
store_block_info(new_block_info)

old_status = block_info.status
BlockDb.change_root_status(block_info.root, old_status, status)

new_block_info
end

@spec get_blocks_with_status(BlockInfo.block_status()) ::
Expand Down
7 changes: 7 additions & 0 deletions lib/types/block_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Types.BlockInfo do
signed_block field may be nil if it's queued for download.
"""

alias LambdaEthereumConsensus.Utils
alias Types.SignedBeaconBlock

@type block_status ::
Expand Down Expand Up @@ -34,6 +35,12 @@ defmodule Types.BlockInfo do
:transitioned
]

defimpl String.Chars, for: __MODULE__ do
def to_string(block_info) do
"Slot: #{block_info.signed_block.message.slot}. Root: #{Utils.format_shorten_binary(block_info.root)}. Status: #{inspect(block_info.status)}"
end
end

@spec from_block(SignedBeaconBlock.t(), block_status()) :: t()
def from_block(signed_block, status \\ :pending) do
{:ok, root} = Ssz.hash_tree_root(signed_block.message)
Expand Down
11 changes: 11 additions & 0 deletions native/libp2p_port/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"errors"
"math/big"
"os"

"github.com/btcsuite/btcd/btcec/v2"
gcrypto "github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -93,3 +94,13 @@ func MsgID(msg *pb.Message) string {
digest = h.Sum(digest)
return string(digest[:20])
}

// Log function that writes to a single file. Only to be used for testing.
func Log(msg string) {
f, err := os.OpenFile("logs/go_log.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
PanicIfError(err)
defer f.Close()

_, err = f.WriteString(msg)
PanicIfError(err)
}

0 comments on commit 6eb8e61

Please sign in to comment.