aboutsummaryrefslogblamecommitdiff
path: root/src/hotstuff_client.cpp
blob: bee8abde7538b2894d1f17bcc9189eb702387ef0 (plain) (tree)
1
2
3
4
5
6
7
8
9




                               



                            
 

                              

                           
                             
 
                          

                             
                          

                           
                             
                          
                         
                              
                         
 
                
                   

                     








                                                 
                                                                  
                                                     
                              
                                          
 
                                  


                              
                                                                     

 
                 


                                                          
                                                          


                                                              
                                                      




                             
                                                                              
                        
                                                               
                                             



                                                           
                              


                                  
                                                                     
                                         





                                                       
                                                          

                                    
               






                                                                               

                                   
                                                
                                                       


                                                           
                                            


                                                        
                                                                

                                                                        


                                               
                                                 
                                                             


                                                

                                                    
                                                          

         
                                                                      

                                                        
         


                                                                                          

         
                          
                   
                      
                                



                                                                    
#include <cassert>
#include "salticidae/type.h"
#include "salticidae/netaddr.h"
#include "salticidae/network.h"
#include "salticidae/util.h"

#include "hotstuff/util.h"
#include "hotstuff/type.h"
#include "hotstuff/client.h"

using salticidae::Config;
using salticidae::ElapsedTime;
using salticidae::trim_all;
using salticidae::split;
using salticidae::MsgNetwork;

using hotstuff::ReplicaID;
using hotstuff::NetAddr;
using hotstuff::EventContext;
using hotstuff::uint256_t;
using hotstuff::MsgReqCmd;
using hotstuff::MsgRespCmd;
using hotstuff::CommandDummy;
using hotstuff::command_t;
using hotstuff::Finality;
using hotstuff::HotStuffError;
using hotstuff::opcode_t;

EventContext eb;
ReplicaID proposer;
size_t max_async_num;
int max_iter_num;

struct Request {
    ReplicaID rid;
    command_t cmd;
    ElapsedTime et;
    Request(ReplicaID rid, const command_t &cmd):
        rid(rid), cmd(cmd) { et.start(); }
};

std::unordered_map<ReplicaID, MsgNetwork<opcode_t>::conn_t> conns;
std::unordered_map<const uint256_t, Request> waiting;
std::vector<NetAddr> replicas;
MsgNetwork<opcode_t> mn(eb, 10, 10, 4096);

void set_proposer(ReplicaID rid) {
    proposer = rid;
    auto it = conns.find(rid);
    if (it == conns.end())
        conns.insert(std::make_pair(rid, mn.connect(replicas[rid])));
}

void try_send() {
    while (waiting.size() < max_async_num && max_iter_num)
    {
        auto cmd = CommandDummy::make_cmd();
        mn.send_msg(MsgReqCmd(*cmd), *conns.at(proposer));
        HOTSTUFF_LOG_INFO("send new cmd %.10s",
                            get_hex(cmd->get_hash()).c_str());
        waiting.insert(std::make_pair(
            cmd->get_hash(), Request(proposer, cmd)));
        if (max_iter_num > 0)
            max_iter_num--;
    }
}

void client_resp_cmd_handler(MsgRespCmd &&msg, MsgNetwork<opcode_t>::Conn &) {
    auto &fin = msg.fin;
    HOTSTUFF_LOG_DEBUG("got %s", std::string(msg.fin).c_str());
    const uint256_t &cmd_hash = fin.cmd_hash;
    auto it = waiting.find(cmd_hash);
    if (fin.rid != proposer)
    {
        HOTSTUFF_LOG_INFO("reconnect to the new proposer");
        set_proposer(fin.rid);
    }
    if (fin.rid != it->second.rid)
    {
        mn.send_msg(MsgReqCmd(*(waiting.find(cmd_hash)->second.cmd)),
                    *conns.at(proposer));
        HOTSTUFF_LOG_INFO("resend cmd %.10s",
                            get_hex(cmd_hash).c_str());
        it->second.et.start();
        it->second.rid = proposer;
        return;
    }
    HOTSTUFF_LOG_INFO("got %s", std::string(fin).c_str());
    if (it == waiting.end()) return;
    waiting.erase(it);
    try_send();
}

std::pair<std::string, std::string> split_ip_port_cport(const std::string &s) {
    auto ret = trim_all(split(s, ";"));
    return std::make_pair(ret[0], ret[1]);
}

int main(int argc, char **argv) {
    Config config("hotstuff.conf");
    auto opt_idx = Config::OptValInt::create(0);
    auto opt_replicas = Config::OptValStrVec::create();
    auto opt_max_iter_num