2013-12-30 02:09:57 +02:00
|
|
|
package main
|
|
|
|
|
2014-01-01 04:06:39 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2013-12-30 02:09:57 +02:00
|
|
|
// Database interface
|
|
|
|
type Database interface {
|
|
|
|
Put(key []byte, value []byte)
|
|
|
|
Get(key []byte) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2014-01-01 15:06:00 +02:00
|
|
|
/*
|
|
|
|
* 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A (modified) Radix Trie implementation
|
2013-12-30 02:09:57 +02:00
|
|
|
type Trie struct {
|
|
|
|
root string
|
|
|
|
db Database
|
|
|
|
}
|
|
|
|
|
2014-01-01 04:06:39 +02:00
|
|
|
func NewTrie(db Database, root string) *Trie {
|
2014-01-03 01:43:49 +02:00
|
|
|
return &Trie{db: db, root: root}
|
2013-12-30 02:09:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-01 15:06:00 +02:00
|
|
|
/*
|
|
|
|
* Public (query) interface functions
|
|
|
|
*/
|
2013-12-30 02:09:57 +02:00
|
|
|
func (t *Trie) Update(key string, value string) {
|
|
|
|
k := CompactHexDecode(key)
|
|
|
|
|
|
|
|
t.root = t.UpdateState(t.root, k, value)
|
|
|
|
}
|
|
|
|
|
2014-01-01 04:06:39 +02:00
|
|
|
func (t *Trie) Get(key string) string {
|
|
|
|
k := CompactHexDecode(key)
|
|
|
|
|
|
|
|
return t.GetState(t.root, k)
|
|
|
|
}
|
|
|
|
|
2014-01-01 15:06:00 +02:00
|
|
|
/*
|
|
|
|
* State functions (shouldn't be needed directly).
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Wrapper around the regular db "Put" which generates a key and value
|
|
|
|
func (t *Trie) Put(node interface{}) []byte {
|
|
|
|
enc := Encode(node)
|
|
|
|
sha := Sha256Bin(enc)
|
|
|
|
|
|
|
|
t.db.Put([]byte(sha), enc)
|
|
|
|
|
|
|
|
return sha
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function for printing a node (using fetch, decode and slice printing)
|
|
|
|
func (t *Trie) PrintNode(n string) {
|
|
|
|
data, _ := t.db.Get([]byte(n))
|
|
|
|
d := DecodeNode(data)
|
|
|
|
PrintSlice(d)
|
|
|
|
}
|
|
|
|
|
2014-01-01 04:06:39 +02:00
|
|
|
// Returns the state of an object
|
|
|
|
func (t *Trie) GetState(node string, key []int) string {
|
|
|
|
// Return the node if key is empty (= found)
|
|
|
|
if len(key) == 0 || node == "" {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the encoded node from the db
|
|
|
|
n, err := t.db.Get([]byte(node))
|
|
|
|
if err != nil { fmt.Println("Error in GetState for node", node, "with key", key); return "" }
|
|
|
|
|
|
|
|
// Decode it
|
|
|
|
currentNode := DecodeNode(n)
|
|
|
|
|
|
|
|
if len(currentNode) == 0 {
|
|
|
|
return ""
|
|
|
|
} else if len(currentNode) == 2 {
|
|
|
|
// Decode the key
|
|
|
|
k := CompactDecode(currentNode[0])
|
|
|
|
v := currentNode[1]
|
|
|
|
|
|
|
|
if len(key) >= len(k) && CompareIntSlice(k, key[:len(k)]) {
|
|
|
|
return t.GetState(v, key[len(k):])
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
} else if len(currentNode) == 17 {
|
|
|
|
return t.GetState(currentNode[key[0]], key[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// It shouldn't come this far
|
|
|
|
fmt.Println("GetState unexpected return")
|
|
|
|
return ""
|
2013-12-30 02:09:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts a new sate or delete a state based on the value
|
|
|
|
func (t *Trie) UpdateState(node string, key []int, value string) string {
|
|
|
|
if value != "" {
|
2014-01-01 04:06:39 +02:00
|
|
|
return t.InsertState(node, key, value)
|
2013-12-30 02:09:57 +02:00
|
|
|
} else {
|
|
|
|
// delete it
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-01 04:06:39 +02:00
|
|
|
func (t *Trie) InsertState(node string, key []int, value string) string {
|
|
|
|
if len(key) == 0 {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2014-01-01 15:06:00 +02:00
|
|
|
// New node
|
2014-01-01 04:06:39 +02:00
|
|
|
if node == "" {
|
|
|
|
newNode := []string{ CompactEncode(key), value }
|
|
|
|
|
|
|
|
return string(t.Put(newNode))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the encoded node from the db
|
|
|
|
n, err := t.db.Get([]byte(node))
|
|
|
|
if err != nil { fmt.Println("Error InsertState", err); return "" }
|
|
|
|
|
|
|
|
// Decode it
|
|
|
|
currentNode := DecodeNode(n)
|
|
|
|
// Check for "special" 2 slice type node
|
|
|
|
if len(currentNode) == 2 {
|
|
|
|
// Decode the key
|
|
|
|
k := CompactDecode(currentNode[0])
|
|
|
|
v := currentNode[1]
|
|
|
|
|
|
|
|
// Matching key pair (ie. there's already an object with this key)
|
|
|
|
if CompareIntSlice(k, key) {
|
|
|
|
return string(t.Put([]string{ CompactEncode(key), value }))
|
|
|
|
}
|
|
|
|
|
|
|
|
var newHash string
|
|
|
|
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 := make([]string, 17)
|
|
|
|
// Set the copied and new node
|
|
|
|
scaledSlice[k[matchingLength]] = oldNode
|
|
|
|
scaledSlice[key[matchingLength]] = newNode
|
|
|
|
|
|
|
|
newHash = string(t.Put(scaledSlice))
|
|
|
|
}
|
|
|
|
|
|
|
|
if matchingLength == 0 {
|
|
|
|
// End of the chain, return
|
|
|
|
return newHash
|
|
|
|
} else {
|
|
|
|
newNode := []string{ CompactEncode(key[:matchingLength]), newHash }
|
|
|
|
return string(t.Put(newNode))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Copy the current node over to the new node and replace the first nibble in the key
|
|
|
|
newNode := make([]string, 17); copy(newNode, currentNode)
|
|
|
|
newNode[key[0]] = t.InsertState(currentNode[key[0]], key[1:], value)
|
|
|
|
|
|
|
|
return string(t.Put(newNode))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2013-12-30 02:09:57 +02:00
|
|
|
}
|
2014-01-01 04:06:39 +02:00
|
|
|
|