Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect Warp Sync to Proper Packets #692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/ex_wire/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ mana_version =

config :ex_wire,
p2p_version: 0x04,
protocol_version: 63,
caps: [{"eth", 62}, {"eth", 63}, {"par", 1}],
protocol_version: 62,
caps: [{"eth", 62}, {"par", 1}],
# TODO: This should be set and stored in a file
private_key: :random,
bootnodes: :from_chain,
Expand Down
4 changes: 2 additions & 2 deletions apps/ex_wire/lib/ex_wire/dev_p2p/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule ExWire.DEVp2p.Session do
"""
@spec disconnect(t) :: t
def disconnect(session = %__MODULE__{}) do
%{session | hello_sent: nil, hello_received: nil, packet_id_map: PacketIdMap.default_map()}
%{session | hello_sent: nil, hello_received: nil, packet_id_map: nil}
end

@doc """
Expand Down Expand Up @@ -100,6 +100,6 @@ defmodule ExWire.DEVp2p.Session do
"""
@spec compatible_capabilities?(t) :: boolean()
def compatible_capabilities?(%__MODULE__{packet_id_map: packet_id_map}) do
packet_id_map != PacketIdMap.default_map()
Map.has_key?(packet_id_map.ids_to_modules, 0x10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding 0x10 here presumes to know how PacketIdMap works. Pretty safe assumption, but might work better as a function of PacketIdMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and I don't think this PR has tests passing yet, since I wanted to get some design review. I wasn't sure what's the best way to say "We have compatibility" but having a 0x10 status packet seems to be pretty common on protocols. Also, it's faster to verify than the previous solution, even if possibly less accurate.

end
end
11 changes: 10 additions & 1 deletion apps/ex_wire/lib/ex_wire/packet/capability/eth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ defmodule ExWire.Packet.Capability.Eth do
Eth.BlockHeaders,
Eth.GetBlockBodies,
Eth.BlockBodies,
Eth.NewBlock
Eth.NewBlock,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved
]
}

Expand Down
17 changes: 8 additions & 9 deletions apps/ex_wire/lib/ex_wire/packet/capability/eth/new_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ defmodule ExWire.Packet.Capability.Eth.NewBlock do

alias Block.Header
alias Blockchain.Block
alias ExWire.Packet.Capability.Eth.Transactions
alias Blockchain.Transaction

@behaviour ExWire.Packet

# TODO: fill in Transactions typespec when that packet is figured out
@type t :: %__MODULE__{
header: Header.t(),
transactions: [any()],
transactions: [Transaction.t()],
uncles: [Block.t()],
total_difficulty: integer() | nil
}
Expand Down Expand Up @@ -53,8 +52,8 @@ defmodule ExWire.Packet.Capability.Eth.NewBlock do
[
[
Header.serialize(packet.header),
Transactions.serialize(%Transactions{transactions: packet.transactions}),
packet.uncles |> Block.serialize() |> Enum.to_list()
Enum.map(packet.transactions, &Transaction.serialize/1),
Enum.map(packet.uncles, &Header.serialize/1)
],
packet.total_difficulty
]
Expand All @@ -70,16 +69,16 @@ defmodule ExWire.Packet.Capability.Eth.NewBlock do
[
[
header,
transactions,
uncles
transaction_rlp,
uncles_rlp
],
total_difficulty
] = rlp

%__MODULE__{
header: Header.deserialize(header),
transactions: Transactions.deserialize(transactions).transactions,
uncles: uncles |> Block.deserialize() |> Enum.to_list(),
transactions: Enum.map(transaction_rlp, &Transaction.deserialize/1),
uncles: Enum.map(uncles_rlp, &Header.deserialize/1),
total_difficulty: total_difficulty |> :binary.decode_unsigned()
}
end
Expand Down
4 changes: 3 additions & 1 deletion apps/ex_wire/lib/ex_wire/packet/capability/mana.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule ExWire.Packet.Capability.Mana do
alias ExWire.Packet.Capability
alias ExWire.Packet.Capability.Eth
alias ExWire.Packet.Capability.Par

@our_capabilities_map %{
Eth.get_name() => Eth
Eth.get_name() => Eth,
Par.get_name() => Par
}

@our_capabilities @our_capabilities_map
Expand Down
19 changes: 18 additions & 1 deletion apps/ex_wire/lib/ex_wire/packet/capability/par.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExWire.Packet.Capability.Par do
alias ExWire.Config
alias ExWire.Packet.Capability
alias ExWire.Packet.Capability.Eth
alias ExWire.Packet.Capability.Par

@behaviour Capability
Expand All @@ -10,10 +11,26 @@ defmodule ExWire.Packet.Capability.Par do
@version_to_packet_types %{
1 => [
Par.WarpStatus,
Eth.NewBlockHashes,
Eth.Transactions,
Eth.GetBlockHeaders,
Eth.BlockHeaders,
Eth.GetBlockBodies,
Eth.BlockBodies,
Eth.NewBlock,
Par.GetSnapshotManifest,
Par.SnapshotManifest,
Par.GetSnapshotData,
Par.SnapshotData
Par.SnapshotData,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved,
:reserved
]
}

Expand Down
11 changes: 8 additions & 3 deletions apps/ex_wire/lib/ex_wire/packet/packet_id_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule ExWire.Packet.PacketIdMap do
|> Enum.filter(fn cap ->
Capability.are_we_capable?(cap, Mana.get_our_capabilities_map())
end)
|> Enum.sort(fn {name1, _v1}, {name2, _v2} -> name1 < name2 end)
|> Enum.sort(fn %Capability{name: name1}, %Capability{name: name2} -> name1 < name2 end)
|> Enum.map(fn cap ->
Capability.get_packets_for_capability(cap, Mana.get_our_capabilities_map())
end)
Expand Down Expand Up @@ -116,8 +116,13 @@ defmodule ExWire.Packet.PacketIdMap do
defp build_capability_ids_to_modules_map(capability_packet_types, {base_id, starting_map}) do
capability_packet_types
|> Enum.reduce({base_id, starting_map}, fn packet_type, {next_base_id, updated_map} ->
packet_id = base_id + apply(packet_type, :message_id_offset, [])
{Kernel.max(next_base_id, packet_id + 1), Map.put(updated_map, packet_id, packet_type)}
if packet_type == :reserved do
{next_base_id + 1, updated_map}
else
packet_id = base_id + apply(packet_type, :message_id_offset, [])

{Kernel.max(next_base_id, packet_id + 1), Map.put(updated_map, packet_id, packet_type)}
end
end)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ExWire.Packet.Capability.Eth.NewBlockTest do
use ExUnit.Case, async: true
alias ExWire.Packet.Capability.Eth.NewBlock
doctest NewBlock
end