aboutsummaryrefslogtreecommitdiff
path: root/test/test_p2p_stress.cpp
blob: b58bcc7d7a4ad709ea8d7647b749653b09346c9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * Copyright (c) 2018 Cornell University.
 *
 * Author: Ted Yin <[email protected]>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <cstdio>
#include <string>
#include <functional>
#include <openssl/rand.h>

#include "salticidae/msg.h"
#include "salticidae/event.h"
#include "salticidae/network.h"
#include "salticidae/stream.h"

using salticidae::NetAddr;
using salticidae::DataStream;
using salticidae::MsgNetwork;
using salticidae::ConnPool;
using salticidae::Event;
using salticidae::EventContext;
using salticidae::htole;
using salticidae::letoh;
using salticidae::bytearray_t;
using salticidae::uint256_t;
using salticidae::static_pointer_cast;
using salticidae::Config;
using std::placeholders::_1;
using std::placeholders::_2;

struct MsgRand {
    static const uint8_t opcode = 0x0;
    DataStream serialized;
    bytearray_t bytes;
    MsgRand(size_t size) {
        bytearray_t bytes;
        bytes.resize(size);
        RAND_bytes(&bytes[0], size);
        serialized << std::move(bytes);
    }
    MsgRand(DataStream &&s) { bytes = s; }
};

struct MsgAck {
    static const uint8_t opcode = 0x1;
    uint256_t hash;
    DataStream serialized;
    MsgAck(const uint256_t &hash) { serialized << hash; }
    MsgAck(DataStream &&s) { s >> hash; }
};

const uint8_t MsgRand::opcode;
const uint8_t MsgAck::opcode;

using MyNet = salticidae::PeerNetwork<uint8_t>;

std::vector<NetAddr> addrs;

void signal_handler(int) {
    throw salticidae::SalticidaeError("got termination signal");
}

struct TestContext {
    Event timer;
    int state;
    uint256_t hash;
};

void install_proto(EventContext &ec, MyNet &net,
                    std::unordered_map<NetAddr, TestContext> &_tc, const size_t &seg_buff_size) {
    auto send_rand = [&](int size, const MyNet::conn_t &conn) {
        auto &tc = _tc[conn->get_addr()];
        MsgRand msg(size);
        tc.hash = msg.serialized.get_hash();
        net.send_msg(std::move(msg), conn);
    };
    net.reg_conn_handler([&, send_rand](const ConnPool::conn_t &conn, bool connected) {
        if (connected)
        {
            if (conn->get_mode() == ConnPool::Conn::ACTIVE)
            {
                auto &tc = _tc[conn->get_addr()];
                tc.state = 1;
                SALTICIDAE_LOG_INFO("increasing phase");
                send_rand(tc.state, static_pointer_cast<MyNet::Conn>(conn));
            }
        }
    });
    net.reg_handler([&](MsgRand &&msg, const MyNet::conn_t &conn) {
        uint256_t hash = salticidae::get_hash(msg.bytes);
        net.send_msg(MsgAck(hash), conn);
    });
    net.reg_handler([&, send_rand](MsgAck &&msg, const MyNet::conn_t &conn) {
        auto &tc = _tc[conn->get_addr()];
        if (msg.hash != tc.hash)
        {
            SALTICIDAE_LOG_ERROR("corrupted I/O!");
            exit(1);
        }

        if (tc.state == seg_buff_size * 2)
        {
            send_rand(tc.state, conn);
            tc.state = -1;
            tc.timer = Event(ec, -1, [&net, conn](int, int) {
                net.terminate(conn);
            });
            double t = salticidae::gen_rand_timeout(10);
            tc.timer.add_with_timeout(t, 0);
            SALTICIDAE_LOG_INFO("rand-bomboard phase, ending in %.2f secs", t);
        }
        else if (tc.state == -1)
            send_rand(rand() % (seg_buff_size * 10), conn);
        else
            send_rand(++tc.state, conn);
    });
}

int main(int argc, char **argv) {
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    Config config;
    auto opt_no_msg = Config::OptValFlag::create(false);
    auto opt_npeers = Config::OptValInt::create(5);
    auto opt_seg_buff_size = Config::OptValInt::create(4096);
    auto opt_nworker = Config::OptValInt::create(2);
    auto opt_help = Config::OptValFlag::create(false);
    config.add_opt("no-msg", opt_no_msg, Config::SWITCH_ON);
    config.add_opt("npeers", opt_npeers, Config::SET_VAL);
    config.add_opt("seg-buff-size", opt_seg_buff_size, Config::SET_VAL);
    config.add_opt("nworker", opt_nworker, Config::SET_VAL);
    config.add_opt("help", opt_help, Config::SWITCH_ON, 'h', "show this help info");
    config.parse(argc, argv);
    if (opt_help->get())
    {
        config.print_help();
        exit(0);
    }
    size_t seg_buff_size = opt_seg_buff_size->get();
    for (int i = 0; i < opt_npeers->get(); i++)
        addrs.push_back(NetAddr("127.0.0.1:" + std::to_string(12345 + i)));
    std::vector<std::thread> peers;
    for (auto &addr: addrs)
    {
        peers.push_back(std::thread([&, addr]() {
            EventContext ec;
            std::unordered_map<NetAddr, TestContext> tc;
            MyNet net(ec, MyNet::Config(
                salticidae::ConnPool::Config()
                    .nworker(opt_nworker->get()).seg_buff_size(seg_buff_size))
                        .conn_timeout(5).ping_period(2));
            if (!opt_no_msg->get())
                install_proto(ec, net, tc, seg_buff_size);
            try {
                net.start();
                net.listen(addr);
                for (auto &paddr: addrs)
                    if (paddr != addr) net.add_peer(paddr);
                ec.dispatch();
            } catch (salticidae::SalticidaeError &e) {}
        }));
    }
    for (auto &t: peers) t.join();
    return 0;
}