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
5a86892e
Commit
5a86892e
authored
Jun 30, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using remote for test cases
parent
8151858e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
17 deletions
+108
-17
trie.go
ethutil/trie.go
+14
-3
trie_test.go
ethutil/trie_test.go
+94
-14
No files found.
ethutil/trie.go
View file @
5a86892e
...
...
@@ -43,11 +43,11 @@ func NewCache(db Database) *Cache {
return
&
Cache
{
db
:
db
,
nodes
:
make
(
map
[
string
]
*
Node
)}
}
func
(
cache
*
Cache
)
Put
(
v
interface
{}
)
interface
{}
{
func
(
cache
*
Cache
)
Put
Value
(
v
interface
{},
force
bool
)
interface
{}
{
value
:=
NewValue
(
v
)
enc
:=
value
.
Encode
()
if
len
(
enc
)
>=
32
{
if
len
(
enc
)
>=
32
||
force
{
sha
:=
Sha3Bin
(
enc
)
cache
.
nodes
[
string
(
sha
)]
=
NewNode
(
sha
,
value
,
true
)
...
...
@@ -59,6 +59,10 @@ func (cache *Cache) Put(v interface{}) interface{} {
return
v
}
func
(
cache
*
Cache
)
Put
(
v
interface
{})
interface
{}
{
return
cache
.
PutValue
(
v
,
false
)
}
func
(
cache
*
Cache
)
Get
(
key
[]
byte
)
*
Value
{
// First check if the key is the cache
if
cache
.
nodes
[
string
(
key
)]
!=
nil
{
...
...
@@ -168,7 +172,12 @@ func (t *Trie) Update(key string, value string) {
k
:=
CompactHexDecode
(
key
)
t
.
Root
=
t
.
UpdateState
(
t
.
Root
,
k
,
value
)
root
:=
t
.
UpdateState
(
t
.
Root
,
k
,
value
)
if
_
,
ok
:=
root
.
([]
byte
);
!
ok
{
t
.
Root
=
t
.
cache
.
PutValue
(
root
,
true
)
}
else
{
t
.
Root
=
root
}
}
func
(
t
*
Trie
)
Get
(
key
string
)
string
{
...
...
@@ -527,6 +536,8 @@ func (it *TrieIterator) fetchNode(key []int, node []byte, cb EachCallback) {
}
func
(
it
*
TrieIterator
)
iterateNode
(
key
[]
int
,
currentNode
*
Value
,
cb
EachCallback
)
{
//fmt.Println("node", currentNode)
if
currentNode
.
Len
()
==
2
{
k
:=
CompactDecode
(
currentNode
.
Get
(
0
)
.
Str
())
...
...
ethutil/trie_test.go
View file @
5a86892e
package
ethutil
import
(
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
...
...
@@ -171,23 +176,98 @@ func TestTriePurge(t *testing.T) {
}
}
func
TestTrieIt
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
func
h
(
str
string
)
string
{
d
,
err
:=
hex
.
DecodeString
(
str
)
if
err
!=
nil
{
panic
(
err
)
}
return
string
(
d
)
}
data
:=
[][]
string
{
{
"do"
,
"verb"
},
{
"ether"
,
"wookiedoo"
},
{
"horse"
,
"stallion"
},
{
"shaman"
,
"horse"
},
{
"doge"
,
"coin"
},
{
"ether"
,
""
},
{
"dog"
,
"puppy"
},
{
"shaman"
,
""
},
func
get
(
in
string
)
(
out
string
)
{
if
len
(
in
)
>
2
&&
in
[
:
2
]
==
"0x"
{
out
=
h
(
in
[
2
:
])
}
else
{
out
=
in
}
for
_
,
item
:=
range
data
{
trie
.
Update
(
item
[
0
],
item
[
1
])
return
}
type
Test
struct
{
Name
string
In
map
[
string
]
string
Root
string
}
func
CreateTest
(
name
string
,
data
[]
byte
)
(
Test
,
error
)
{
t
:=
Test
{
Name
:
name
}
err
:=
json
.
Unmarshal
(
data
,
&
t
)
if
err
!=
nil
{
return
Test
{},
fmt
.
Errorf
(
"%v"
,
err
)
}
fmt
.
Printf
(
"root %x"
,
trie
.
Root
)
return
t
,
nil
}
func
CreateTests
(
uri
string
,
cb
func
(
Test
))
{
resp
,
err
:=
http
.
Get
(
uri
)
if
err
!=
nil
{
panic
(
err
)
}
defer
resp
.
Body
.
Close
()
data
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
var
objmap
map
[
string
]
*
json
.
RawMessage
err
=
json
.
Unmarshal
(
data
,
&
objmap
)
if
err
!=
nil
{
panic
(
err
)
}
for
name
,
testData
:=
range
objmap
{
test
,
err
:=
CreateTest
(
name
,
*
testData
)
if
err
!=
nil
{
panic
(
err
)
}
cb
(
test
)
}
}
func
TestRemote
(
t
*
testing
.
T
)
{
CreateTests
(
"https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json"
,
func
(
test
Test
)
{
_
,
trie
:=
New
()
for
key
,
value
:=
range
test
.
In
{
trie
.
Update
(
get
(
key
),
get
(
value
))
}
fmt
.
Printf
(
"%-15s: %x
\n
"
,
test
.
Name
,
trie
.
Root
)
a
:=
NewValue
(
h
(
test
.
Root
))
.
Bytes
()
b
:=
NewValue
(
trie
.
Root
)
.
Bytes
()
if
bytes
.
Compare
(
a
,
b
)
!=
0
{
t
.
Errorf
(
"%-10s: %x %x"
,
test
.
Name
,
a
,
b
)
}
})
}
func
TestTrieReplay
(
t
*
testing
.
T
)
{
CreateTests
(
"https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json"
,
func
(
test
Test
)
{
_
,
trie
:=
New
()
for
key
,
value
:=
range
test
.
In
{
trie
.
Update
(
get
(
key
),
get
(
value
))
}
_
,
trie2
:=
New
()
trie
.
NewIterator
()
.
Each
(
func
(
key
string
,
v
*
Value
)
{
trie2
.
Update
(
key
,
string
(
v
.
Str
()))
})
a
:=
NewValue
(
trie
.
Root
)
.
Bytes
()
b
:=
NewValue
(
trie2
.
Root
)
.
Bytes
()
if
bytes
.
Compare
(
a
,
b
)
!=
0
{
t
.
Errorf
(
"root %x %x
\n
"
,
trie
.
Root
,
trie2
.
Root
)
}
})
}
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