Skip to content

Commit 0602f87

Browse files
committed
multiprocess: Add echoipc RPC method and test
Add simple interfaces::Echo IPC interface with one method that just takes and returns a string, to test multiprocess framework and provide an example of how it can be used to spawn and call between processes.
1 parent aa4d626 commit 0602f87

File tree

15 files changed

+153
-0
lines changed

15 files changed

+153
-0
lines changed

src/Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ BITCOIN_CORE_H = \
146146
init.h \
147147
interfaces/base.h \
148148
interfaces/chain.h \
149+
interfaces/echo.h \
149150
interfaces/handler.h \
150151
interfaces/init.h \
151152
interfaces/node.h \
@@ -519,6 +520,7 @@ libbitcoin_util_a_SOURCES = \
519520
compat/strnlen.cpp \
520521
fs.cpp \
521522
interfaces/base.cpp \
523+
interfaces/echo.cpp \
522524
interfaces/init.cpp \
523525
interfaces/handler.cpp \
524526
logging.cpp \
@@ -739,13 +741,16 @@ if HARDEN
739741
endif
740742

741743
libbitcoin_ipc_mpgen_input = \
744+
interfaces/capnp/echo.capnp \
742745
interfaces/capnp/init.capnp
743746
EXTRA_DIST += $(libbitcoin_ipc_mpgen_input)
744747
%.capnp:
745748

746749
if BUILD_MULTIPROCESS
747750
LIBBITCOIN_IPC=libbitcoin_ipc.a
748751
libbitcoin_ipc_a_SOURCES = \
752+
interfaces/capnp/echo-types.h \
753+
interfaces/capnp/echo.h \
749754
interfaces/capnp/init-types.h \
750755
interfaces/capnp/init.cpp \
751756
interfaces/capnp/init.h \

src/interfaces/capnp/echo-types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INTERFACES_CAPNP_ECHO_TYPES_H
6+
#define BITCOIN_INTERFACES_CAPNP_ECHO_TYPES_H
7+
#endif // BITCOIN_INTERFACES_CAPNP_ECHO_TYPES_H

src/interfaces/capnp/echo.capnp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2020 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
@0x888b4f7f51e691f7;
6+
7+
using Cxx = import "/capnp/c++.capnp";
8+
$Cxx.namespace("interfaces::capnp::messages");
9+
10+
using Proxy = import "/mp/proxy.capnp";
11+
12+
interface Echo $Proxy.wrap("interfaces::Echo") {
13+
destroy @0 (context :Proxy.Context) -> ();
14+
echo @1 (context :Proxy.Context, echo: Text) -> (result :Text);
15+
}

src/interfaces/capnp/echo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INTERFACES_CAPNP_ECHO_H
6+
#define BITCOIN_INTERFACES_CAPNP_ECHO_H
7+
8+
#include <interfaces/capnp/echo.capnp.h>
9+
#include <interfaces/echo.h>
10+
11+
#endif // BITCOIN_INTERFACES_CAPNP_ECHO_H

src/interfaces/capnp/init-types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
#ifndef BITCOIN_INTERFACES_CAPNP_INIT_TYPES_H
66
#define BITCOIN_INTERFACES_CAPNP_INIT_TYPES_H
7+
8+
#include <interfaces/capnp/echo.capnp.proxy-types.h>
9+
710
#endif // BITCOIN_INTERFACES_CAPNP_INIT_TYPES_H

src/interfaces/capnp/init.capnp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
using Cxx = import "/capnp/c++.capnp";
88
$Cxx.namespace("interfaces::capnp::messages");
99

10+
using Echo = import "echo.capnp";
1011
using Proxy = import "/mp/proxy.capnp";
1112

1213
interface Init $Proxy.wrap("interfaces::Init") {
1314
construct @0 (threadMap: Proxy.ThreadMap) -> (threadMap :Proxy.ThreadMap);
15+
makeEcho @1 (context :Proxy.Context) -> (result :Echo.Echo);
1416
}

src/interfaces/capnp/init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_INTERFACES_CAPNP_INIT_H
66
#define BITCOIN_INTERFACES_CAPNP_INIT_H
77

8+
#include <interfaces/echo.h>
89
#include <interfaces/init.h>
910
#include <mp/proxy.h>
1011

src/interfaces/echo.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <interfaces/echo.h>
6+
7+
#include <util/memory.h>
8+
9+
namespace interfaces {
10+
namespace {
11+
class EchoImpl : public Echo
12+
{
13+
public:
14+
std::string echo(const std::string& echo) override { return echo; }
15+
};
16+
} // namespace
17+
18+
std::unique_ptr<Echo> MakeEcho() { return MakeUnique<EchoImpl>(); }
19+
20+
} // namespace interfaces

src/interfaces/echo.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_INTERFACES_ECHO_H
6+
#define BITCOIN_INTERFACES_ECHO_H
7+
8+
#include <interfaces/base.h>
9+
10+
#include <string>
11+
12+
namespace interfaces {
13+
14+
//! Simple string echoing interface for testing.
15+
class Echo : public Base
16+
{
17+
public:
18+
virtual ~Echo() {}
19+
20+
//! Echo provided string.
21+
virtual std::string echo(const std::string& echo) = 0;
22+
};
23+
24+
//! Return implementation of Echo interface.
25+
std::unique_ptr<Echo> MakeEcho();
26+
27+
} // namespace interfaces
28+
29+
#endif // BITCOIN_INTERFACES_ECHO_H

src/interfaces/init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <interfaces/init.h>
66

7+
#include <interfaces/echo.h>
78
#include <interfaces/ipc.h>
89
#include <interfaces/node.h>
910
#include <logging.h>
@@ -23,6 +24,8 @@ class CloseFn : public CloseHook
2324

2425
LocalInit::LocalInit(const char* exe_name, const char* log_suffix) : m_exe_name(exe_name), m_log_suffix(log_suffix) {}
2526
LocalInit::~LocalInit() {}
27+
std::unique_ptr<Echo> LocalInit::makeEcho() { return {}; }
28+
std::unique_ptr<Echo> LocalInit::makeEchoIpc() { return {}; }
2629
NodeContext& LocalInit::node()
2730
{
2831
throw std::logic_error("Node accessor function called from non-node binary (gui, wallet, or test program)");

0 commit comments

Comments
 (0)