-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_round.go
203 lines (182 loc) · 4.66 KB
/
badger_round.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
package storage
import (
"encoding/binary"
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger"
)
func (s *BadgerStore) ReadLink(from, to crypto.Hash) (uint64, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
return readLink(txn, from, to)
}
func (s *BadgerStore) ReadRound(hash crypto.Hash) (*common.Round, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
return readRound(txn, hash)
}
func (s *BadgerStore) UpdateEmptyHeadRound(node crypto.Hash, number uint64, references *common.RoundLink) error {
txn := s.snapshotsDB.NewTransaction(true)
defer txn.Discard()
self, err := readRound(txn, node)
if err != nil {
return err
}
if self.Number != number {
panic("round number assert error")
}
if self.References.Self != references.Self {
panic("self reference assert error")
}
external, err := readRound(txn, references.External)
if err != nil {
return err
}
if external == nil {
panic("external final not exist")
}
if external.NodeId == self.NodeId {
panic("self references loop")
}
snapshots, err := readSnapshotsForNodeRound(txn, node, number)
if err != nil {
return err
}
if len(snapshots) != 0 {
panic("round not empty")
}
err = writeLink(txn, node, external.NodeId, external.Number)
if err != nil {
return err
}
err = writeRound(txn, node, &common.Round{
NodeId: node,
Number: number,
References: references,
})
if err != nil {
return err
}
return txn.Commit()
}
func (s *BadgerStore) StartNewRound(node crypto.Hash, number uint64, references *common.RoundLink, finalStart uint64) error {
txn := s.snapshotsDB.NewTransaction(true)
defer txn.Discard()
// FIXME assert only, remove in future
if config.Debug && number != 0 {
self, err := readRound(txn, node)
if err != nil {
return err
}
external, err := readRound(txn, references.External)
if err != nil {
return err
}
if self == nil || self.Number != number-1 {
panic("self final assert error")
}
if external == nil {
panic("external final not exist")
}
if external.NodeId == self.NodeId {
panic("self references loop")
}
old, err := readRound(txn, references.Self)
if err != nil {
return err
}
if old != nil {
panic("self final already exist")
}
link, err := readLink(txn, node, external.NodeId)
if err != nil {
return err
}
if link > external.Number {
panic(fmt.Sprintf("external link backward %s=>%s %d=>%d", self.NodeId, external.NodeId, link, external.Number))
}
}
// assert end
err := startNewRound(txn, node, number, references, finalStart)
if err != nil {
return err
}
return txn.Commit()
}
func startNewRound(txn *badger.Txn, node crypto.Hash, number uint64, references *common.RoundLink, finalStart uint64) error {
if number != 0 {
self, err := readRound(txn, node)
if err != nil {
return err
}
external, err := readRound(txn, references.External)
if err != nil {
return err
}
err = writeLink(txn, node, external.NodeId, external.Number)
if err != nil {
return err
}
self.Timestamp = finalStart
err = writeRound(txn, references.Self, self)
if err != nil {
return err
}
}
return writeRound(txn, node, &common.Round{
NodeId: node,
Number: number,
References: references,
})
}
func readLink(txn *badger.Txn, from, to crypto.Hash) (uint64, error) {
key := graphLinkKey(from, to)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return 0, nil
}
if err != nil {
return 0, err
}
ival, err := item.ValueCopy(nil)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint64(ival), nil
}
func writeLink(txn *badger.Txn, from, to crypto.Hash, link uint64) error {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, link)
key := graphLinkKey(from, to)
return txn.Set(key, buf)
}
func readRound(txn *badger.Txn, hash crypto.Hash) (*common.Round, error) {
key := graphRoundKey(hash)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return nil, nil
} else if err != nil {
return nil, err
}
ival, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
var out common.Round
err = common.MsgpackUnmarshal(ival, &out)
return &out, err
}
func writeRound(txn *badger.Txn, hash crypto.Hash, round *common.Round) error {
key := graphRoundKey(hash)
val := common.MsgpackMarshalPanic(round)
return txn.Set(key, val)
}
func graphRoundKey(hash crypto.Hash) []byte {
return append([]byte(graphPrefixRound), hash[:]...)
}
func graphLinkKey(from, to crypto.Hash) []byte {
link := crypto.NewHash(append(from[:], to[:]...))
return append([]byte(graphPrefixLink), link[:]...)
}