Skip to content

Commit

Permalink
Ping v2 and automatic ping vsn upgrade (#4188)
Browse files Browse the repository at this point in the history
* Ping v2 and automatic ping vsn upgrade

---------

Co-authored-by: Justin Mitchell <mitchelli@users.noreply.github.com>
  • Loading branch information
uwiger and mitchelli committed Mar 14, 2024
1 parent 0868e4d commit b0a7140
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 104 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ smoke-test-run: internal-build
system-smoke-test-deps:
$(MAKE) docker
docker pull "aeternity/aeternity:v1.4.0"
docker pull "aeternity/aeternity:v6.9.0"

local-system-test: KIND=system_test
local-system-test: internal-build
Expand Down
6 changes: 5 additions & 1 deletion apps/aecore/include/aec_peer_messages.hrl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

-define(P2P_PROTOCOL_VSN, 1).

-define(REQUEST_TIMEOUT, 30000).

-define(MSG_FRAGMENT, 0).
Expand Down Expand Up @@ -27,7 +30,8 @@


-define(RESPONSE_VSN, 1).
-define(PING_VSN, 1).
-define(PING_VSN_1, 1).
-define(PING_VSN_2, 2).
-define(GET_MEMPOOL_VSN, 1).
-define(MEMPOOL_VSN, 1).
-define(GET_HEADER_BY_HASH_VSN, 1).
Expand Down
83 changes: 83 additions & 0 deletions apps/aecore/src/aec_capabilities.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-module(aec_capabilities).

-behavior(gen_server).


-export([ set_capability/2
, get_capability/1
, delete_capability/1
, get_capabilities/0
, register_peer/2
, remove_peer/1
, peers_with_capability/1 ]).

%% Gen_server stuff
-export([ start_link/0 ]).

-export([ init/1
, handle_call/3
, handle_cast/2
, handle_info/2
, terminate/2
, code_change/3 ]).

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

get_capability(Key) ->
call({get_capability, Key}).

set_capability(Key, Value) ->
call({set_capability, Key, Value}).

delete_capability(Key) ->
call({delete_capability, Key}).

get_capabilities() ->
call(get_capabilities).

register_peer(Peer, Capabilities) when is_list(Capabilities) ->
call({register_peer, Peer, maps:from_list(Capabilities)}).

remove_peer(Peer) ->
call({remove_peer, Peer}).

peers_with_capability(Key) ->
call({peers_with_capability, Key}).

call(Req) ->
gen_server:call(?MODULE, Req).


init([]) ->
{ok, #{ peers => #{}
, capabilities => #{} }}.

handle_call({get_capability, Key}, _From, #{capabilities := Cs} = St) ->
{reply, maps:find(Key, Cs), St};
handle_call({set_capability, Key, Value}, _From, #{capabilities := Cs} = St) ->
{reply, ok, St#{capabilities := Cs#{Key => Value}}};
handle_call({delete_capability, Key}, _From, #{capabilities := Cs} = St) ->
{reply, ok, St#{capabilities := maps:remove(Key, Cs)}};
handle_call(get_capabilities, _From, #{capabilities := Cs} = St) ->
{reply, maps:to_list(Cs), St};
handle_call({register_peer, PeerId, Capabilities}, _From, #{peers := Peers} = St) ->
{reply, ok, St#{peers := Peers#{PeerId => Capabilities}}};
handle_call({peers_with_capability, Key}, _From, #{peers := Peers} = St) ->
Result = maps:fold(fun(PeerId, #{Key := Caps}, Acc) ->
[{PeerId, Caps}|Acc];
(_,_, Acc) -> Acc
end, [], Peers),
{reply, Result, St}.

handle_cast(_, St) ->
{noreply, St}.

handle_info(_, St) ->
{noreply, St}.

terminate(_Reason, _State) ->
ok.

code_change(_FromVsn, St, _Extra) ->
{ok, St}.

0 comments on commit b0a7140

Please sign in to comment.