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
|
package main
import (
"path"
"os/user"
"github.com/syndtr/goleveldb/leveldb"
"fmt"
)
type Database struct {
db *leveldb.DB
trie *Trie
}
func NewDatabase() (*Database, error) {
// This will eventually have to be something like a resource folder.
// it works on my system for now. Probably won't work on Windows
usr, _ := user.Current()
dbPath := path.Join(usr.HomeDir, ".ethereum", "database")
// Open the db
db, err := leveldb.OpenFile(dbPath, nil)
if err != nil {
return nil, err
}
database := &Database{db: db}
// Bootstrap database. Sets a few defaults; such as the last block
database.Bootstrap()
return database, nil
}
func (db *Database) Bootstrap() error {
db.trie = NewTrie(db)
return nil
}
func (db *Database) Put(key []byte, value []byte) {
err := db.db.Put(key, value, nil)
if err != nil {
fmt.Println("Error put", err)
}
}
func (db *Database) Close() {
// Close the leveldb database
db.db.Close()
}
type Trie struct {
root string
db *Database
}
func NewTrie(db *Database) *Trie {
return &Trie{db: db, root: ""}
}
func (t *Trie) Update(key string, value string) {
k := CompactHexDecode(key)
t.root = t.UpdateState(t.root, k, value)
}
func (t *Trie) Get(key []byte) ([]byte, error) {
return nil, nil
}
// Inserts a new sate or delete a state based on the value
func (t *Trie) UpdateState(node, key, value string) string {
if value != "" {
return t.InsertState(node, key, value)
} else {
// delete it
}
return ""
}
func (t *Trie) InsertState(node, key, value string) string {
return ""
}
func (t *Trie) Put(node []byte) []byte {
enc := Encode(node)
sha := Sha256Bin(enc)
t.db.Put([]byte(sha), enc)
return sha
}
|