-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment.go
211 lines (178 loc) · 4.85 KB
/
fragment.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package hotstuff
import (
"crypto/sha256"
"encoding/binary"
"fmt"
)
// RapidFair: 为并行的公平排序序列构建Fragment暂存已经完成公平排序的交易
type Fragment struct {
hash Hash
virView View // 解耦的公平排序的view编号
virLeader ID // virtual view对应的leader
orderedTx Command // 公平排序的交易序列G
txSeq TXList // 缓存交易序列TxSeq的实际数据
missEdge Command // 公平排序对上一个virtual view没有提交交易的丢失序列
updateSeq TXList // 缓存更新序列UpdateSeq的实际数据
}
func NewFragment(virView View, virLeader ID, orderedTx Command, txSeq TXList, missEdge Command, updateSeq TXList) *Fragment {
f := &Fragment{
virView: virView,
virLeader: virLeader,
orderedTx: orderedTx,
txSeq: txSeq,
missEdge: missEdge,
updateSeq: updateSeq,
}
f.hash = sha256.Sum256(f.ToBytes())
return f
}
func (f *Fragment) String() string {
return fmt.Sprintf(
"Fragment{vir view: %d, vir leader: %d}",
f.virView,
f.virLeader,
)
}
// 返回Fragment私有变量的值
func (f *Fragment) Hash() Hash {
return f.hash
}
func (f *Fragment) VirView() View {
return f.virView
}
func (f *Fragment) VirLeader() ID {
return f.virLeader
}
func (f *Fragment) OrderedTx() Command {
return f.orderedTx
}
func (f *Fragment) TxSeq() TXList {
return f.txSeq
}
func (f *Fragment) MissEdge() Command {
return f.missEdge
}
func (f *Fragment) UpdateSeq() TXList {
return f.updateSeq
}
// 序列化对象为[]byte
func (f *Fragment) ToBytes() []byte {
var buf []byte
var viewBuf [8]byte
binary.LittleEndian.PutUint64(viewBuf[:], uint64(f.virView))
buf = append(buf, viewBuf[:]...)
var leaderBuf [4]byte
binary.LittleEndian.PutUint32(leaderBuf[:], uint32(f.virLeader))
buf = append(buf, leaderBuf[:]...)
buf = append(buf, []byte(f.orderedTx)...)
buf = append(buf, f.txSeq.ToBytes()...)
buf = append(buf, []byte(f.missEdge)...)
buf = append(buf, f.updateSeq.ToBytes()...)
return buf
}
// 维护:交易序列哈希TxSeqHash
type TxSeqFragment struct {
parent Hash
hash Hash
cert QuorumCert
virView View // 解耦的公平排序的view编号
virLeader ID // virtual view对应的leader
// txSeqHash TxSeqHash // 交易序列哈希 Map<ID, hash(Command[])>
txSeqHash TXList
}
func NewTxSeqFragment(parent Hash, cert QuorumCert, virView View, virLeader ID, txSeqHash TXList) *TxSeqFragment {
t := &TxSeqFragment{
parent: parent,
cert: cert,
virView: virView,
virLeader: virLeader,
txSeqHash: txSeqHash,
}
t.hash = sha256.Sum256(t.ToBytes())
return t
}
func (t *TxSeqFragment) String() string {
return fmt.Sprintf(
"Fragment{vir view: %d, vir leader: %d}",
t.virView,
t.virLeader,
)
}
func (t *TxSeqFragment) Hash() Hash {
return t.hash
}
func (t *TxSeqFragment) Parent() Hash {
return t.parent
}
func (t *TxSeqFragment) QuorumCert() QuorumCert {
return t.cert
}
func (t *TxSeqFragment) VirView() View {
return t.virView
}
func (t *TxSeqFragment) VirLeader() ID {
return t.virLeader
}
func (t *TxSeqFragment) TxSeqHash() TXList {
return t.txSeqHash
}
func (t *TxSeqFragment) ToBytes() []byte {
buf := t.parent[:]
var viewBuf [8]byte
binary.LittleEndian.PutUint64(viewBuf[:], uint64(t.virView))
buf = append(buf, viewBuf[:]...)
var leaderBuf [4]byte
binary.LittleEndian.PutUint32(leaderBuf[:], uint32(t.virLeader))
buf = append(buf, leaderBuf[:]...)
buf = append(buf, t.txSeqHash.ToBytes()...)
return buf
}
// Fragment的核心数据,对txSeq,updateSeq做hash
type FragmentData struct {
hash Hash
virView View // 解耦的公平排序的view编号
orderedTx Command // 公平排序的交易序列G
txSeq Hash // 缓存交易序列TxSeq的实际数据
missEdge Command // 公平排序对上一个virtual view没有提交交易的丢失序列
updateSeq Hash // 缓存更新序列UpdateSeq的实际数据
}
func NewFragmentData(virView View, orderedTx Command, txSeq Hash, missEdge Command, updateSeq Hash) *FragmentData {
f := &FragmentData{
virView: virView,
orderedTx: orderedTx,
txSeq: txSeq,
missEdge: missEdge,
updateSeq: updateSeq,
}
f.hash = sha256.Sum256(f.ToBytes())
return f
}
func (f *FragmentData) Hash() Hash {
return f.hash
}
func (f *FragmentData) VirView() View {
return f.virView
}
func (f *FragmentData) OrderedTx() Command {
return f.orderedTx
}
func (f *FragmentData) TxSeq() Hash {
return f.txSeq
}
func (f *FragmentData) MissEdge() Command {
return f.missEdge
}
func (f *FragmentData) UpdateSeq() Hash {
return f.updateSeq
}
func (f *FragmentData) ToBytes() []byte {
var buf []byte
var viewBuf [8]byte
binary.LittleEndian.PutUint64(viewBuf[:], uint64(f.virView))
buf = append(buf, viewBuf[:]...)
buf = append(buf, []byte(f.orderedTx)...)
buf = append(buf, f.txSeq[:]...)
buf = append(buf, []byte(f.missEdge)...)
buf = append(buf, f.updateSeq[:]...)
return buf
}