aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/trie.go
blob: 44d2d5774a736da23bdb1f6e5debb3ca846db461 (plain) (blame)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package ethutil

import (
    "fmt"
    "reflect"
)

type Node struct {
    Key   []byte
    Value *Value
    Dirty bool
}

func NewNode(key []byte, val *Value, dirty bool) *Node {
    return &Node{Key: key, Value: val, Dirty: dirty}
}

func (n *Node) Copy() *Node {
    return NewNode(n.Key, n.Value, n.Dirty)
}

type Cache struct {
    nodes map[string]*Node
    db    Database
}

func NewCache(db Database) *Cache {
    return &Cache{db: db, nodes: make(map[string]*Node)}
}

func (cache *Cache) Put(v interface{}) interface{} {
    value := NewValue(v)

    enc := value.Encode()
    if len(enc) >= 32 {
        sha := Sha3Bin(enc)

        cache.nodes[string(sha)] = NewNode(sha, value, true)

        return sha
    }

    return v
}

func (cache *Cache) Get(key []byte) *Value {
    // First check if the key is the cache
    if cache.nodes[string(key)] != nil {
        return cache.nodes[string(key)].Value
    }

    // Get the key of the database instead and cache it
    data, _ := cache.db.Get(key)
    // Create the cached value
    value := NewValueFromBytes(data)
    // Create caching node
    cache.nodes[string(key)] = NewNode(key, value, false)

    return value
}

func (cache *Cache) Commit() {
    for key, node := range cache.nodes {
        if node.Dirty {
            cache.db.Put([]byte(key), node.Value.Encode())
            node.Dirty = false
        }
    }

    // If the nodes grows beyond the 200 entries we simple empty it
    // FIXME come up with something better
    if len(cache.nodes) > 200 {
        cache.nodes = make(map[string]*Node)
    }
}

func (cache *Cache) Undo() {
    for key, node := range cache.nodes {
        if node.Dirty {
            delete(cache.nodes, key)
        }
    }
}

// A (modified) Radix Trie implementation
type Trie struct {
    Root interface{}
    //db   Database
    cache *Cache
}

func NewTrie(db Database, Root interface{}) *Trie {
    return &Trie{cache: NewCache(db), Root: Root}
}

func (t *Trie) Sync() {
    t.cache.Commit()
}

/*
 * Public (query) interface functions
 */
func (t *Trie) Update(key string, value string) {
    k := CompactHexDecode(key)

    t.Root = t.UpdateState(t.Root, k, value)
}

func (t *Trie) Get(key string) string {
    k := CompactHexDecode(key)
    c := NewValue(t.GetState(t.Root, k))

    return c.Str()
}

func (t *Trie) GetState(node interface{}, key []int) interface{} {
    n := NewValue(node)
    // Return the node if key is empty (= found)
    if len(key) == 0 || n.IsNil() || n.Len() == 0 {
        return node
    }

    currentNode := t.GetNode(node)
    length := currentNode.Len()

    if length == 0 {
        return ""
    } else if length == 2 {
        // Decode the key
        k := CompactDecode(currentNode.Get(0).Str())
        v := currentNode.Get(1).Raw()

        if len(key) >= len(k) && CompareIntSlice(k, key[:len(k)]) {
            return t.GetState(v, key[len(k):])
        } else {
            return ""
        }
    } else if length == 17 {
        return t.GetState(currentNode.Get(key[0]).Raw(), key[1:])
    }

    // It shouldn't come this far
    fmt.Println("GetState unexpected return")
    return ""
}

func (t *Trie) GetNode(node interface{}) *Value {
    n := NewValue(node)

    if !n.Get(0).IsNil() {
        return n
    }

    str := n.Str()
    if len(str) == 0 {
        return n
    } else if len(str) < 32 {
        return NewValueFromBytes([]byte(str))
    }
    /*
        else {
            // Fetch the encoded node from the db
            o, err := t.db.Get(n.Bytes())
            if err != nil {
                fmt.Println("Error InsertState", err)
                return NewValue("")
            }

            return NewValueFromBytes(o)
        }
    */
    return t.cache.Get(n.Bytes())

}

func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{} {
    if value != "" {
        return t.InsertState(node, key, value)
    } else {
        // delete it
    }

    return ""
}

func (t *Trie) Put(node interface{}) interface{} {
    /*
        enc := Encode(node)
        if len(enc) >= 32 {
            var sha []byte
            sha = Sha3Bin(enc)
            //t.db.Put([]byte(sha), enc)

            return sha
        }
        return node
    */

    /*
        TODO?
            c := Conv(t.Root)
            fmt.Println(c.Type(), c.Length())
            if c.Type() == reflect.String && c.AsString() == "" {
                return enc
            }
    */

    return t.cache.Put(node)

}

func EmptyStringSlice(l int) []interface{} {
    slice := make([]interface{}, l)
    for i := 0; i < l; i++ {
        slice[i] = ""
    }
    return slice
}

func (t *Trie) InsertState(node interface{}, key []int, value interface{}) interface{} {
    if len(key) == 0 {
        return value
    }

    // New node
    n := NewValue(node)
    if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
        newNode := []interface{}{CompactEncode(key), value}

        return t.Put(newNode)
    }

    currentNode := t.GetNode(node)
    // Check for "special" 2 slice type node
    if currentNode.Len() == 2 {
        // Decode the key
        k := CompactDecode(currentNode.Get(0).Str())
        v := currentNode.Get(1).Raw()

        // Matching key pair (ie. there's already an object with this key)
        if CompareIntSlice(k, key) {
            newNode := []interface{}{CompactEncode(key), value}
            return t.Put(newNode)
        }

        var newHash interface{}
        matchingLength := MatchingNibbleLength(key, k)
        if matchingLength == len(k) {
            // Insert the hash, creating a new node
            newHash = t.InsertState(v, key[matchingLength:], value)
        } else {
            // Expand the 2 length slice to a 17 length slice
            oldNode := t.InsertState("", k[matchingLength+1:], v)
            newNode := t.InsertState("", key[matchingLength+1:], value)
            // Create an expanded slice
            scaledSlice := EmptyStringSlice(17)
            // Set the copied and new node
            scaledSlice[k[matchingLength]] = oldNode
            scaledSlice[key[matchingLength]] = newNode

            newHash = t.Put(scaledSlice)
        }

        if matchingLength == 0 {
            // End of the chain, return
            return newHash
        } else {
            newNode := []interface{}{CompactEncode(key[:matchingLength]), newHash}
            return t.Put(newNode)
        }
    } else {

        // Copy the current node over to the new node and replace the first nibble in the key
        newNode := EmptyStringSlice(17)

        for i := 0; i < 17; i++ {
            cpy := currentNode.Get(i).Raw()
            if cpy != nil {
                newNode[i] = cpy
            }
        }

        newNode[key[0]] = t.InsertState(currentNode.Get(key[0]).Raw(), key[1:], value)

        return t.Put(newNode)
    }

    return ""
}

// Simple compare function which creates a rlp value out of the evaluated objects
func (t *Trie) Cmp(trie *Trie) bool {
    return NewValue(t.Root).Cmp(NewValue(trie.Root))
}

// Returns a copy of this trie
func (t *Trie) Copy() *Trie {
    trie := NewTrie(t.cache.db, t.Root)
    for key, node := range t.cache.nodes {
        trie.cache.nodes[key] = node.Copy()
    }

    return trie
}

/*
 * Trie helper functions
 */
// Helper function for printing out the raw contents of a slice
func PrintSlice(slice []string) {
    fmt.Printf("[")
    for i, val := range slice {
        fmt.Printf("%q", val)
        if i != len(slice)-1 {
            fmt.Printf(",")
        }
    }
    fmt.Printf("]\n")
}

func PrintSliceT(slice interface{}) {
    c := Conv(slice)
    for i := 0; i < c.Length(); i++ {
        val := c.Get(i)
        if val.Type() == reflect.Slice {
            PrintSliceT(val.AsRaw())
        } else {
            fmt.Printf("%q", val)
            if i != c.Length()-1 {
                fmt.Printf(",")
            }
        }
    }
}

// RLP Decodes a node in to a [2] or [17] string slice
func DecodeNode(data []byte) []string {
    dec, _ := Decode(data, 0)
    if slice, ok := dec.([]interface{}); ok {
        strSlice := make([]string, len(slice))

        for i, s := range slice {
            if str, ok := s.([]byte); ok {
                strSlice[i] = string(str)
            }
        }

        return strSlice
    } else {
        fmt.Printf("It wasn't a []. It's a %T\n", dec)
    }

    return nil
}