aboutsummaryrefslogblamecommitdiff
path: root/src/hotstuff_client.cpp
blob: f925b739538f1ca4a32414810c20750e3e0a2c77 (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::MsgNetwork;

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

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

struct Request {
    ReplicaID rid;
    command_t cmd;
    salticidae::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));
#ifndef HOTSTUFF_ENABLE_BENCHMARK
        HOTSTUFF_LOG_INFO("send new cmd %.10s",
                            get_hex(cmd->get_hash()).c_str());
#endif
        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);
    auto &et = it->second.et;
    if (it == waiting.end()) return;
    et.stop();
    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(*(