This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructs.h
123 lines (100 loc) · 3.75 KB
/
structs.h
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
#pragma once
#include <list>
#include <array>
#include <mutex>
#include "streamlet.pb.h"
#include <thread>
// Interface for StreamletNodes to talk to applications
// which implement the replicated state machine
class ReplicatedStateMachine {
public:
virtual ~ReplicatedStateMachine() { };
virtual void TransactionsFinalized(const std::string &txns, uint64_t epoch) = 0;
virtual void TransactionsNotarized(const std::string &txns, uint64_t epoch) = 0;
virtual bool ValidateTransactions(const std::string &txns, uint64_t epoch) = 0;
#ifdef BYZANTINE
virtual void GetTransactions(uint32_t peer, std::string *txns, uint64_t epoch) = 0;
#else
virtual void GetTransactions(std::string *txns, uint64_t epoch) = 0;
#endif
virtual void BeginTime() = 0;
virtual std::thread SpawnThread() = 0;
};
// 256 bit key for ED25519
using Key = std::array<uint8_t, 32>;
/*
* Used to initialze the streamlet service.
*/
struct Peer {
std::string addr;
Key key;
};
/*
* A block on the blockchain containing the protobuf Block type
* alongside information needed to implement the Streamlet protocol.
*
* From the protocol descritpion, a block is a triple of the parent hash,
* epoch number, and transaction data for the replicated state machine.
* Candidates are intended to be a unique references to elements of
* the blockchain, i.e. there must only be one Candidate in memory
* representing a given triple although there can be several Block
* instances representing the same mathematical object.
*
* A Candidate is constructed when the node receives notice of a newly
* proposed block once it has verified the proposal is legitimate.
* The Candidate is used to cache the propsed block and record votes,
* and once notarized, to add the block to the blockchain.
*/
struct Candidate {
// Structure synchronization
std::mutex m;
// Flag to coordinate structure deletion with ref_count
bool removed;
// Reference count to coordinate structure deletion.
// Expect fewer than 256 threads.
std::atomic_uint8_t ref_count;
// Since votes from other nodes can arrive before the leader proposal
// in an epoch, this records the hash that each node voted on. Later,
// once the leader's proposal is seen and verified, only the votes
// with a matching hash are recorded in voters below, and this structure
// is cleared.
std::unordered_map<uint32_t, std::string> vote_hashes;
// std::vector<bool> is specialized on major platforms to be
// a compact bitvector. std::bitset cannot be used since we do
// not know the system configuration ahead of time (bitset size
// is a template parameter).
std::vector<bool> voters;
// Number of unique votes seen for this block. Equal to
// the number of true entries in the vector above.
uint32_t votes;
// The block hash
std::string hash;
// The block
Block block;
Candidate(const uint32_t num_peers)
: removed{false}, ref_count{0}, votes{0}, hash{}, block{} {
voters.resize(num_peers);
}
};
/*
* A ChainElement represents a block that has been notarized onto the blockchain.
*
* As with the Candidate type above, there must only be one ChainElement
* in memory representing a given triple although there can be several
* Block instances representing the same mathematical object.
*/
struct ChainElement {
// Epoch of the block this ChainElement represents
uint64_t epoch;
// Index of this block in its chain
uint64_t index;
// The block
Block block;
// Block hash
std::string hash;
// Link to parent
ChainElement* plink;
// Links to successors
std::list<ChainElement*> links;
ChainElement(uint64_t e) : epoch{e}, index{0}, block{}, hash{}, plink{nullptr} { }
};