-
Notifications
You must be signed in to change notification settings - Fork 179
/
ledger.go
255 lines (211 loc) · 5.69 KB
/
ledger.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
package ledger
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/onflow/flow-go/module"
)
// Ledger is a stateful fork-aware key/value storage.
// Any update (value change for a key) to the ledger generates a new ledger state.
// Updates can be applied to any recent states. These changes don't have to be sequential and ledger supports a tree of states.
// Ledger provides value lookup by key at a particular state (historic lookups) and can prove the existence/non-existence of a key-value pair at the given state.
// Ledger assumes the initial state includes all keys with an empty bytes slice as value.
type Ledger interface {
// ledger implements methods needed to be ReadyDone aware
module.ReadyDoneAware
// InitState returns the initial state of the ledger
InitState() State
// Get returns values for the given slice of keys at specific state
Get(query *Query) (values []Value, err error)
// Update updates a list of keys with new values at specific state (update) and returns a new state
Set(update *Update) (newState State, err error)
// Prove returns proofs for the given keys at specific state
Prove(query *Query) (proof Proof, err error)
}
// Query holds all data needed for a ledger read or ledger proof
type Query struct {
state State
keys []Key
}
// NewEmptyQuery returns an empty ledger query
func NewEmptyQuery(sc State) (*Query, error) {
return &Query{state: sc}, nil
}
// NewQuery constructs a new ledger query
func NewQuery(sc State, keys []Key) (*Query, error) {
return &Query{state: sc, keys: keys}, nil
}
// Keys returns keys of the query
func (q *Query) Keys() []Key {
return q.keys
}
// Size returns number of keys in the query
func (q *Query) Size() int {
return len(q.keys)
}
// State returns the state part of the query
func (q *Query) State() State {
return q.state
}
// SetState sets the state part of the query
func (q *Query) SetState(s State) {
q.state = s
}
// Update holds all data needed for a ledger update
type Update struct {
state State
keys []Key
values []Value
}
// Size returns number of keys in the ledger update
func (u *Update) Size() int {
return len(u.keys)
}
// Keys returns keys of the update
func (u *Update) Keys() []Key {
return u.keys
}
// Values returns value of the update
func (u *Update) Values() []Value {
return u.values
}
// State returns the state part of this update
func (u *Update) State() State {
return u.state
}
// SetState sets the state part of the update
func (u *Update) SetState(sc State) {
u.state = sc
}
// NewEmptyUpdate returns an empty ledger update
func NewEmptyUpdate(sc State) (*Update, error) {
return &Update{state: sc}, nil
}
// NewUpdate returns an ledger update
func NewUpdate(sc State, keys []Key, values []Value) (*Update, error) {
if len(keys) != len(values) {
return nil, fmt.Errorf("length mismatch: keys have %d elements, but values have %d elements", len(keys), len(values))
}
return &Update{state: sc, keys: keys, values: values}, nil
}
// State captures an state of the ledger
type State []byte
func (sc State) String() string {
return hex.EncodeToString(sc)
}
// Equals compares the state to another state
func (sc State) Equals(o State) bool {
if o == nil {
return false
}
return bytes.Equal(sc, o)
}
// Proof is a byte slice capturing encoded version of a batch proof
type Proof []byte
func (pr Proof) String() string {
return hex.EncodeToString(pr)
}
// Equals compares a proof to another ledger proof
func (pr Proof) Equals(o Proof) bool {
if o == nil {
return false
}
return bytes.Equal(pr, o)
}
// Key represents a hierarchical ledger key
type Key struct {
KeyParts []KeyPart
}
// NewKey construct a new key
func NewKey(kp []KeyPart) *Key {
return &Key{KeyParts: kp}
}
// Size returns the byte size needed to encode the key
func (k *Key) Size() int {
size := 0
for _, kp := range k.KeyParts {
// value size + 2 bytes for type
size += len(kp.Value) + 2
}
return size
}
func (k *Key) String() string {
ret := ""
for _, kp := range k.KeyParts {
ret += "/"
ret += string(kp.Type)
ret += "/"
ret += string(kp.Value)
}
return ret
}
// DeepCopy returns a deep copy of the key
func (k *Key) DeepCopy() Key {
newKPs := make([]KeyPart, 0, len(k.KeyParts))
for _, kp := range k.KeyParts {
newKPs = append(newKPs, *kp.DeepCopy())
}
return Key{KeyParts: newKPs}
}
// Equals compares this key to another key
func (k *Key) Equals(other *Key) bool {
if other == nil {
return false
}
if len(k.KeyParts) != len(other.KeyParts) {
return false
}
for i, kp := range k.KeyParts {
if !kp.Equals(&other.KeyParts[i]) {
return false
}
}
return true
}
// KeyPart is a typed part of a key
type KeyPart struct {
Type uint16
Value []byte
}
// NewKeyPart construct a new key part
func NewKeyPart(typ uint16, val []byte) *KeyPart {
return &KeyPart{Type: typ, Value: val}
}
// Equals compares this key part to another key part
func (kp *KeyPart) Equals(other *KeyPart) bool {
if other == nil {
return false
}
if kp.Type != other.Type {
return false
}
return bytes.Equal(kp.Value, other.Value)
}
// DeepCopy returns a deep copy of the key part
func (kp *KeyPart) DeepCopy() *KeyPart {
newV := make([]byte, len(kp.Value))
copy(newV, kp.Value)
return &KeyPart{Type: kp.Type, Value: newV}
}
// Value holds the value part of a ledger key value pair
type Value []byte
// Size returns the value size
func (v Value) Size() int {
return len(v)
}
func (v Value) String() string {
return hex.EncodeToString(v)
}
// DeepCopy returns a deep copy of the value
func (v Value) DeepCopy() Value {
newV := make([]byte, len(v))
copy(newV, v)
return newV
}
// Equals compares a ledger Value to another one
func (v Value) Equals(other Value) bool {
if other == nil {
return false
}
return bytes.Equal(v, other)
}