aboutsummaryrefslogtreecommitdiff
path: root/include/hotstuff/util.h
blob: 99b5ea43ff194e851a7f7dadc1c8859439280033 (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
#ifndef _HOTSTUFF_UTIL_H
#define _HOTSTUFF_UTIL_H

#include "hotstuff/config.h"
#include "salticidae/util.h"

namespace hotstuff {

class Logger: public salticidae::Logger {
    public:
    using salticidae::Logger::Logger;
};

extern Logger logger;

#ifdef HOTSTUFF_DEBUG_LOG
#define HOTSTUFF_NORMAL_LOG
#define HOTSTUFF_ENABLE_LOG_DEBUG
#endif

#ifdef HOTSTUFF_NORMAL_LOG
#define HOTSTUFF_ENABLE_LOG_INFO
#define HOTSTUFF_ENABLE_LOG_WARN
#endif

#ifdef HOTSTUFF_ENABLE_LOG_INFO
#define HOTSTUFF_LOG_INFO(...) hotstuff::logger.info(__VA_ARGS__)
#else
#define HOTSTUFF_LOG_INFO(...) ((void)0)
#endif

#ifdef HOTSTUFF_ENABLE_LOG_DEBUG
#define HOTSTUFF_LOG_DEBUG(...) hotstuff::logger.debug(__VA_ARGS__)
#else
#define HOTSTUFF_LOG_DEBUG(...) ((void)0)
#endif

#ifdef HOTSTUFF_ENABLE_LOG_WARN
#define HOTSTUFF_LOG_WARN(...) hotstuff::logger.warning(__VA_ARGS__)
#else
#define HOTSTUFF_LOG_WARN(...) ((void)0)
#endif

#define HOTSTUFF_LOG_ERROR(...) hotstuff::logger.error(__VA_ARGS__)

#ifdef HOTSTUFF_BLK_PROFILE
class BlockProfiler {
    enum BlockState {
        BLK_SEEN,
        BLK_FETCH,
        BLK_CC
    };

    struct BlockProfile {
        bool is_local;          /* is the block proposed by the replica itself? */
        BlockState state;
        double hash_seen_time;  /* the first time to see block hash */
        double full_fetch_time; /* the first time to get full block content */
        double cc_time;         /* the time when it receives cc */
        double commit_time;     /* the time when it commits */
    };

    std::unordered_map<const uint256, BlockProfile> blocks;
    ElapsedTime timer;

    public:
    BlockProfiler() { timer.start(); }

    auto rec_blk(const uint256 &blk_hash, bool is_local) {
        auto it = blocks.find(blk_hash);
        assert(it == blocks.end());
        timer.stop(false);
        return blocks.insert(std::make_pair(blk_hash,
            BlockProfile{is_local, BLK_SEEN, timer.elapsed_sec, 0, 0, 0})).first;
    }

    void get_blk(const uint256 &blk_hash) {
        auto it = blocks.find(blk_hash);
        if (it == blocks.end())
            it = rec_blk(blk_hash, false);
        BlockProfile &blkp = it->second;
        assert(blkp.state == BLK_SEEN);
        timer.stop(false);
        blkp.full_fetch_time = timer.elapsed_sec;
        blkp.state = BLK_FETCH;
    }

    void have_cc(const uint256 &blk_hash) {
        auto it = blocks.find(blk_hash);
        assert(it != blocks.end());
        BlockProfile &blkp = it->second;
        assert(blkp.state == BLK_FETCH);
        timer.stop(false);
        blkp.polling_start_time = timer.elapsed_sec;
        blkp.state = BLK_CC;
    }

    const char *decide_blk(const uint256 &blk_hash) {
        static char buff[1024];
        auto it = blocks.find(blk_hash);
        assert(it != blocks.end());
        BlockProfile &blkp = it->second;
        assert(blkp.state == BLK_CC);
        timer.stop(false);
        blkp.commit_time = timer.elapsed_sec;
        snprintf(buff, sizeof buff, "(%d,%.4f,%.4f,%.4f,%.4f,%.4f)",
                blkp.is_local,
                blkp.hash_seen_time, blkp.full_fetch_time,
                blkp.polling_start_time, blkp.polling_end_time,
                blkp.commit_time);
        blocks.erase(it);
        return buff;
    }
};

#endif

}

#endif