Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
30f3b4d4
Commit
30f3b4d4
authored
Jan 01, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a few more comments and cleaned up code
parent
df6f7e8a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
54 deletions
+57
-54
trie.go
trie.go
+57
-54
No files found.
trie.go
View file @
30f3b4d4
...
@@ -10,6 +10,38 @@ type Database interface {
...
@@ -10,6 +10,38 @@ type Database interface {
Get
(
key
[]
byte
)
([]
byte
,
error
)
Get
(
key
[]
byte
)
([]
byte
,
error
)
}
}
/*
* 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
type
Trie
struct
{
type
Trie
struct
{
root
string
root
string
db
Database
db
Database
...
@@ -19,18 +51,9 @@ func NewTrie(db Database, root string) *Trie {
...
@@ -19,18 +51,9 @@ func NewTrie(db Database, root string) *Trie {
return
&
Trie
{
db
:
db
,
root
:
""
}
return
&
Trie
{
db
:
db
,
root
:
""
}
}
}
func
(
t
*
Trie
)
Put
(
node
interface
{})
[]
byte
{
/*
//if s, ok := node.([]string); ok {
* Public (query) interface functions
// PrintSlice(s)
*/
//}
enc
:=
Encode
(
node
)
sha
:=
Sha256Bin
(
enc
)
t
.
db
.
Put
([]
byte
(
sha
),
enc
)
return
sha
}
func
(
t
*
Trie
)
Update
(
key
string
,
value
string
)
{
func
(
t
*
Trie
)
Update
(
key
string
,
value
string
)
{
k
:=
CompactHexDecode
(
key
)
k
:=
CompactHexDecode
(
key
)
...
@@ -43,10 +66,29 @@ func (t *Trie) Get(key string) string {
...
@@ -43,10 +66,29 @@ func (t *Trie) Get(key string) string {
return
t
.
GetState
(
t
.
root
,
k
)
return
t
.
GetState
(
t
.
root
,
k
)
}
}
/*
* 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
)
}
// Returns the state of an object
// Returns the state of an object
func
(
t
*
Trie
)
GetState
(
node
string
,
key
[]
int
)
string
{
func
(
t
*
Trie
)
GetState
(
node
string
,
key
[]
int
)
string
{
//if Debug { fmt.Println("get =", key) }
// Return the node if key is empty (= found)
// Return the node if key is empty (= found)
if
len
(
key
)
==
0
||
node
==
""
{
if
len
(
key
)
==
0
||
node
==
""
{
return
node
return
node
...
@@ -66,10 +108,6 @@ func (t *Trie) GetState(node string, key []int) string {
...
@@ -66,10 +108,6 @@ func (t *Trie) GetState(node string, key []int) string {
k
:=
CompactDecode
(
currentNode
[
0
])
k
:=
CompactDecode
(
currentNode
[
0
])
v
:=
currentNode
[
1
]
v
:=
currentNode
[
1
]
//fmt.Println(k, key)
//fmt.Printf("k1:%v\nk2:%v\n", k, key[:len(k)-1])
//fmt.Println(len(key), ">=", len(k)-1, "&&", k, key[:len(k)])
if
len
(
key
)
>=
len
(
k
)
&&
CompareIntSlice
(
k
,
key
[
:
len
(
k
)])
{
if
len
(
key
)
>=
len
(
k
)
&&
CompareIntSlice
(
k
,
key
[
:
len
(
k
)])
{
return
t
.
GetState
(
v
,
key
[
len
(
k
)
:
])
return
t
.
GetState
(
v
,
key
[
len
(
k
)
:
])
}
else
{
}
else
{
...
@@ -95,47 +133,13 @@ func (t *Trie) UpdateState(node string, key []int, value string) string {
...
@@ -95,47 +133,13 @@ func (t *Trie) UpdateState(node string, key []int, value string) string {
return
""
return
""
}
}
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
}
func
(
t
*
Trie
)
PrintNode
(
n
string
)
{
data
,
_
:=
t
.
db
.
Get
([]
byte
(
n
))
d
:=
DecodeNode
(
data
)
PrintSlice
(
d
)
}
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
(
t
*
Trie
)
InsertState
(
node
string
,
key
[]
int
,
value
string
)
string
{
func
(
t
*
Trie
)
InsertState
(
node
string
,
key
[]
int
,
value
string
)
string
{
//if Debug { fmt.Println("insrt", key, value, "node:", node) }
if
len
(
key
)
==
0
{
if
len
(
key
)
==
0
{
return
value
return
value
}
}
//fmt.Println(node)
// New node
// Root node!
if
node
==
""
{
if
node
==
""
{
newNode
:=
[]
string
{
CompactEncode
(
key
),
value
}
newNode
:=
[]
string
{
CompactEncode
(
key
),
value
}
...
@@ -195,4 +199,3 @@ func (t *Trie) InsertState(node string, key []int, value string) string {
...
@@ -195,4 +199,3 @@ func (t *Trie) InsertState(node string, key []int, value string) string {
return
""
return
""
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment