Skip to content

Commit 885f0d1

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 9df8470 commit 885f0d1

File tree

15 files changed

+156
-0
lines changed

15 files changed

+156
-0
lines changed

src/Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ BITCOIN_CORE_H = \
145145
init.h \
146146
interfaces/base.h \
147147
interfaces/chain.h \
148+
interfaces/echo.h \
148149
interfaces/handler.h \
149150
interfaces/init.h \
150151
interfaces/node.h \
@@ -513,6 +514,7 @@ libbitcoin_util_a_SOURCES = \
513514
compat/strnlen.cpp \
514515
fs.cpp \
515516
interfaces/base.cpp \
517+
interfaces/echo.cpp \
516518
interfaces/init.cpp \
517519
interfaces/handler.cpp \
518520
logging.cpp \
@@ -733,13 +735,16 @@ if HARDEN
733735
endif
734736

735737
libbitcoin_ipc_mpgen_input = \
738+
interfaces/capnp/echo.capnp \
736739
interfaces/capnp/init.capnp
737740
EXTRA_DIST += $(libbitcoin_ipc_mpgen_input)
738741
%.capnp:
739742

740743
if BUILD_MULTIPROCESS
741744
LIBBITCOIN_IPC=libbitcoin_ipc.a
742745
libbitcoin_ipc_a_SOURCES = \
746+
interfaces/capnp/echo-types.h \
747+
interfaces/capnp/echo.h \
743748
interfaces/capnp/init-types.h \
744749
interfaces/capnp/init.cpp \
745750
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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
15+
{
16+
return echo;
17+
}
18+
};
19+
} // namespace
20+
21+
std::unique_ptr<Echo> MakeEcho() { return MakeUnique<EchoImpl>(); }
22+
23+
} // 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
@@ -5,6 +5,7 @@
55
#include <interfaces/init.h>
66

77
#include <interfaces/chain.h>
8+
#include <interfaces/echo.h>
89
#include <interfaces/ipc.h>
910
#include <interfaces/node.h>
1011
#include <logging.h>
@@ -28,6 +29,8 @@ class CloseFn : public CloseHook
2829

2930
LocalInit::LocalInit(const char* exe_name, const char* log_suffix) : m_exe_name(exe_name), m_log_suffix(log_suffix) {}
3031
LocalInit::~LocalInit() {}
32+
std::unique_ptr<Echo> LocalInit::makeEcho() { return {}; }
33+
std::unique_ptr<Echo> LocalInit::makeEchoIpc() { return {}; }
3134
NodeContext& LocalInit::node()
3235
{
3336
throw std::logic_error("Node accessor function called from non-node binary (gui, wallet, or test program)");

0 commit comments

Comments
 (0)