-
Notifications
You must be signed in to change notification settings - Fork 0
/
negentropy.go
274 lines (227 loc) · 7.05 KB
/
negentropy.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package negentropy
import (
"fmt"
"math"
"golang.org/x/exp/slices"
)
type Negentropy struct {
Storage *NegentropyStorageVector
frameSizeLimit uint
lastTimestampIn uint32
lastTimestampOut uint32
isInitiator bool
}
func NewNegentropy(frameSizeLimit uint) *Negentropy {
if frameSizeLimit != 0 && frameSizeLimit < 4096 {
panic(fmt.Errorf("frameSizeLimit too small"))
}
storage := &NegentropyStorageVector{
items: make([]Item, 0, 36),
sealed: false,
}
return &Negentropy{
Storage: storage,
frameSizeLimit: frameSizeLimit,
lastTimestampIn: 0,
lastTimestampOut: 0,
isInitiator: false,
}
}
func (ngtp *Negentropy) Initiate() ([]byte, error) {
if ngtp.isInitiator {
return nil, fmt.Errorf("already initiated")
}
ngtp.isInitiator = true
output := make([]byte, 0, 120)
output = append(output, PROTOCOL_VERSION)
ngtp.splitRange(0, ngtp.Storage.Size(), Item{timestamp: ^uint32(0) >> 1}, &output)
return output, nil
}
func (ngtp *Negentropy) Reconcile(query []byte) (output []byte, haveIds, needIds [][ID_SIZE]byte, err error) {
fullOutput := make([]byte, 0, 120)
fullOutput = append(fullOutput, PROTOCOL_VERSION)
queryBuf := make([]byte, len(query))
copy(queryBuf, query)
protocolVersion := arrayShift(&queryBuf)
if protocolVersion < 0x60 || protocolVersion > 0x6F {
return nil, nil, nil, fmt.Errorf("invalid negentropy protocol version byte: %d", protocolVersion)
}
if protocolVersion != PROTOCOL_VERSION {
if ngtp.isInitiator {
return fullOutput, haveIds, needIds, fmt.Errorf("unsupported negentropy protocol version requested: " + string(rune(protocolVersion-0x60)))
} else {
return fullOutput, haveIds, needIds, nil
}
}
storageSize := ngtp.Storage.Size()
prevBound := Item{timestamp: 0}
prevIndex := 0
skip := false
for len(queryBuf) != 0 {
o := make([]byte, 0, 120)
doSkip := func() {
if skip {
o = append(o, ngtp.encodeItem(prevBound)...)
o = append(o, encodeVarInt(int(ModeSkip))...)
}
}
currBound := ngtp.decodeItem(&queryBuf)
mode := Mode(decodeVarInt(&queryBuf))
lower := prevIndex
upper := ngtp.Storage.FindLowerBound(prevIndex, storageSize, currBound)
if mode == ModeSkip {
skip = true
} else if mode == ModeFingerprint {
theirFingerprint := getBytes(&queryBuf, FINGERPRINT_SIZE)
ourFingerprint := ngtp.Storage.Fingerprint(lower, upper)
if slices.Compare(theirFingerprint, ourFingerprint) != 0 {
doSkip()
ngtp.splitRange(lower, upper, currBound, &o)
} else {
skip = true
}
} else if mode == ModeIdList {
numIds := decodeVarInt(&queryBuf)
theirElems := make(map[string][]byte)
for i := 0; i < numIds; i++ {
e := getBytes(&queryBuf, ID_SIZE)
theirElems[string(e)] = e
}
ngtp.Storage.Iterate(lower, upper, func(item Item) bool {
k := item.id[:]
if _, ok := theirElems[string(k)]; !ok {
// ID exists on our side, but not their side
if ngtp.isInitiator {
haveIds = append(haveIds, asStaticArray(k))
}
} else {
// ID exists on both sides
delete(theirElems, string(k))
}
return true
})
if ngtp.isInitiator {
skip = true
for _, v := range theirElems {
// ID exists on their side, but not our side
needIds = append(needIds, asStaticArray(v))
}
} else {
doSkip()
responseIds := make([]byte, 0, 120)
numResponseIds := 0
endBound := currBound
ngtp.Storage.Iterate(lower, upper, func(item Item) bool {
if ngtp.exceededFrameSizeLimit(len(fullOutput) + len(responseIds)) {
endBound = Item{timestamp: item.timestamp, id: item.id}
upper = prevIndex // shrink upper so that the remaining range gets the correct fingerprint
return false
}
responseIds = append(responseIds, item.id[:]...)
numResponseIds++
return true
})
o = append(o, ngtp.encodeItem(endBound)...)
o = append(o, encodeVarInt(int(ModeIdList))...)
o = append(o, encodeVarInt(numResponseIds)...)
o = append(o, responseIds...)
fullOutput = append(fullOutput, o...)
}
} else {
return fullOutput, haveIds, needIds, fmt.Errorf("unexpected mode")
}
if ngtp.exceededFrameSizeLimit(len(fullOutput) + len(o)) {
remainingFingerprint := ngtp.Storage.Fingerprint(upper, storageSize)
fullOutput = append(fullOutput, ngtp.encodeItem(Item{timestamp: ^uint32(0) >> 1})...)
fullOutput = append(fullOutput, encodeVarInt(int(ModeFingerprint))...)
fullOutput = append(fullOutput, remainingFingerprint...)
break
} else {
fullOutput = append(fullOutput, o...)
}
prevIndex = upper
prevBound = currBound
}
return fullOutput, haveIds, needIds, nil
}
func (ngtp *Negentropy) splitRange(lower int, upper int, upperBound Item, o *[]byte) {
numElems := upper - lower
buckets := 16
buf := *o
defer func() {
*o = buf
}()
if numElems < buckets*2 {
buf = append(buf, ngtp.encodeItem(upperBound)...)
buf = append(buf, encodeVarInt(ModeIdList)...)
buf = append(buf, encodeVarInt(numElems)...)
ngtp.Storage.Iterate(lower, upper, func(item Item) bool {
buf = append(buf, item.id[:]...)
return true
})
} else {
itemsPerBucket := int(math.Floor(float64(numElems) / float64(buckets)))
bucketsWithExtra := numElems % buckets
curr := lower
for i := 0; i < buckets; i++ {
bucketSize := itemsPerBucket
if i < bucketsWithExtra {
bucketSize++
}
ourFingerprint := ngtp.Storage.Fingerprint(curr, curr+bucketSize)
curr += bucketSize
nextBound := upperBound
if curr != upper {
nextBound = getMinimalItem(ngtp.Storage.GetItem(curr-1), ngtp.Storage.GetItem(curr))
}
buf = append(buf, ngtp.encodeItem(nextBound)...)
buf = append(buf, encodeVarInt(ModeFingerprint)...)
buf = append(buf, ourFingerprint...)
}
}
}
func (ngtp *Negentropy) exceededFrameSizeLimit(n int) bool {
if ngtp.frameSizeLimit == 0 {
return false
}
return uint(n) > ngtp.frameSizeLimit-200
}
func (ngtp *Negentropy) encodeTimestampOut(timestamp uint32) []byte {
if timestamp == ^uint32(0)>>1 {
ngtp.lastTimestampOut = ^uint32(0) >> 1
return encodeVarInt(0)
}
temp := timestamp
timestamp -= ngtp.lastTimestampOut
ngtp.lastTimestampOut = temp
return encodeVarInt(int(timestamp) + 1)
}
func (ngtp *Negentropy) encodeItem(key Item) []byte {
output := make([]byte, 0, 300)
output = append(output, ngtp.encodeTimestampOut(key.timestamp)...)
output = append(output, encodeVarInt(len(key.id))...)
output = append(output, key.id[:]...)
return output
}
func (ngtp *Negentropy) decodeTimestampIn(encoded *[]byte) uint32 {
timestamp := uint32(decodeVarInt(encoded))
timestamp = timestamp - 1
if ngtp.lastTimestampIn == math.MaxUint32 {
ngtp.lastTimestampIn = math.MaxUint32
return math.MaxUint32
}
timestamp += ngtp.lastTimestampIn
ngtp.lastTimestampIn = timestamp
return timestamp
}
func (ngtp *Negentropy) decodeItem(encoded *[]byte) Item {
timestamp := ngtp.decodeTimestampIn(encoded)
length := decodeVarInt(encoded)
if length > ID_SIZE {
panic(fmt.Errorf("item key too long"))
}
id := getBytes(encoded, length)
var idArr [ID_SIZE]byte
copy(idArr[:], id)
return Item{timestamp: timestamp, id: idArr}
}