diff options
author | obscuren <obscuren@obscura.com> | 2013-12-30 06:54:50 +0800 |
---|---|---|
committer | obscuren <obscuren@obscura.com> | 2013-12-30 06:54:50 +0800 |
commit | a926686445929d091c2d9e019b017600168e9e47 (patch) | |
tree | a328e4f79148461c076b0bd122c591f0bfd213c2 /database.go | |
parent | ad048e9f445ff96b7bfd75c104ab923e1e06754b (diff) | |
download | dexon-a926686445929d091c2d9e019b017600168e9e47.tar dexon-a926686445929d091c2d9e019b017600168e9e47.tar.gz dexon-a926686445929d091c2d9e019b017600168e9e47.tar.bz2 dexon-a926686445929d091c2d9e019b017600168e9e47.tar.lz dexon-a926686445929d091c2d9e019b017600168e9e47.tar.xz dexon-a926686445929d091c2d9e019b017600168e9e47.tar.zst dexon-a926686445929d091c2d9e019b017600168e9e47.zip |
Added sample server, genesis block, and database interface
Diffstat (limited to 'database.go')
-rw-r--r-- | database.go | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/database.go b/database.go new file mode 100644 index 000000000..f74c1dc7f --- /dev/null +++ b/database.go @@ -0,0 +1,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 +} |