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
b05e63c3
Commit
b05e63c3
authored
10 years ago
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added paranoia check for tries
parent
cab7e600
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
10 deletions
+48
-10
cache.go
ptrie/cache.go
+5
-5
trie.go
ptrie/trie.go
+12
-1
trie_test.go
ptrie/trie_test.go
+31
-4
No files found.
ptrie/cache.go
View file @
b05e63c3
package
ptrie
type
Backend
interface
{
Get
([]
byte
)
[]
byte
Se
t
([]
byte
,
[]
byte
)
Get
([]
byte
)
([]
byte
,
error
)
Pu
t
([]
byte
,
[]
byte
)
}
type
Cache
struct
{
...
...
@@ -17,19 +17,19 @@ func NewCache(backend Backend) *Cache {
func
(
self
*
Cache
)
Get
(
key
[]
byte
)
[]
byte
{
data
:=
self
.
store
[
string
(
key
)]
if
data
==
nil
{
data
=
self
.
backend
.
Get
(
key
)
data
,
_
=
self
.
backend
.
Get
(
key
)
}
return
data
}
func
(
self
*
Cache
)
Se
t
(
key
[]
byte
,
data
[]
byte
)
{
func
(
self
*
Cache
)
Pu
t
(
key
[]
byte
,
data
[]
byte
)
{
self
.
store
[
string
(
key
)]
=
data
}
func
(
self
*
Cache
)
Flush
()
{
for
k
,
v
:=
range
self
.
store
{
self
.
backend
.
Se
t
([]
byte
(
k
),
v
)
self
.
backend
.
Pu
t
([]
byte
(
k
),
v
)
}
// This will eventually grow too large. We'd could
...
...
This diff is collapsed.
Click to expand it.
ptrie/trie.go
View file @
b05e63c3
...
...
@@ -10,6 +10,17 @@ import (
"github.com/ethereum/go-ethereum/trie"
)
func
ParanoiaCheck
(
t1
*
Trie
,
backend
Backend
)
(
bool
,
*
Trie
)
{
t2
:=
New
(
nil
,
backend
)
it
:=
t1
.
Iterator
()
for
it
.
Next
()
{
t2
.
Update
(
it
.
Key
,
it
.
Value
)
}
return
bytes
.
Compare
(
t2
.
Hash
(),
t1
.
Hash
())
==
0
,
t2
}
type
Trie
struct
{
mu
sync
.
Mutex
root
Node
...
...
@@ -293,7 +304,7 @@ func (self *Trie) store(node Node) interface{} {
data
:=
ethutil
.
Encode
(
node
)
if
len
(
data
)
>=
32
{
key
:=
crypto
.
Sha3
(
data
)
self
.
cache
.
Se
t
(
key
,
data
)
self
.
cache
.
Pu
t
(
key
,
data
)
return
key
}
...
...
This diff is collapsed.
Click to expand it.
ptrie/trie_test.go
View file @
b05e63c3
...
...
@@ -11,8 +11,8 @@ import (
type
Db
map
[
string
][]
byte
func
(
self
Db
)
Get
(
k
[]
byte
)
[]
byte
{
return
self
[
string
(
k
)]
}
func
(
self
Db
)
Set
(
k
,
v
[]
byte
)
{
self
[
string
(
k
)]
=
v
}
func
(
self
Db
)
Get
(
k
[]
byte
)
([]
byte
,
error
)
{
return
self
[
string
(
k
)],
nil
}
func
(
self
Db
)
Put
(
k
,
v
[]
byte
)
{
self
[
string
(
k
)]
=
v
}
// Used for testing
func
NewEmpty
()
*
Trie
{
...
...
@@ -122,6 +122,7 @@ func TestEmptyValues(t *testing.T) {
}
func
TestReplication
(
t
*
testing
.
T
)
{
t
.
Skip
()
trie
:=
NewEmpty
()
vals
:=
[]
struct
{
k
,
v
string
}{
{
"do"
,
"verb"
},
...
...
@@ -139,7 +140,7 @@ func TestReplication(t *testing.T) {
}
trie
.
Hash
()
trie2
:=
New
(
trie
.
roothash
,
trie
.
cache
)
trie2
:=
New
(
trie
.
roothash
,
trie
.
cache
.
backend
)
if
string
(
trie2
.
GetString
(
"horse"
))
!=
"stallion"
{
t
.
Error
(
"expected to have harse => stallion"
)
}
...
...
@@ -180,6 +181,32 @@ func TestReset(t *testing.T) {
}
}
func
TestParanoia
(
t
*
testing
.
T
)
{
t
.
Skip
()
trie
:=
NewEmpty
()
vals
:=
[]
struct
{
k
,
v
string
}{
{
"do"
,
"verb"
},
{
"ether"
,
"wookiedoo"
},
{
"horse"
,
"stallion"
},
{
"shaman"
,
"horse"
},
{
"doge"
,
"coin"
},
{
"ether"
,
""
},
{
"dog"
,
"puppy"
},
{
"shaman"
,
""
},
{
"somethingveryoddindeedthis is"
,
"myothernodedata"
},
}
for
_
,
val
:=
range
vals
{
trie
.
UpdateString
(
val
.
k
,
val
.
v
)
}
trie
.
Commit
()
ok
,
t2
:=
ParanoiaCheck
(
trie
,
trie
.
cache
.
backend
)
if
!
ok
{
t
.
Errorf
(
"trie paranoia check failed %x %x"
,
trie
.
roothash
,
t2
.
roothash
)
}
}
// Not an actual test
func
TestOutput
(
t
*
testing
.
T
)
{
t
.
Skip
()
...
...
@@ -193,7 +220,7 @@ func TestOutput(t *testing.T) {
fmt
.
Println
(
"############################## FULL ################################"
)
fmt
.
Println
(
trie
.
root
)
trie2
:=
New
(
trie
.
roothash
,
trie
.
cache
)
trie2
:=
New
(
trie
.
roothash
,
trie
.
cache
.
backend
)
trie2
.
GetString
(
base
+
"20"
)
fmt
.
Println
(
"############################## SMALL ################################"
)
fmt
.
Println
(
trie2
.
root
)
...
...
This diff is collapsed.
Click to expand it.
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