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
96d86740
Commit
96d86740
authored
Dec 01, 2015
by
Jeffrey Wilcke
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2005 from zsfelfoldi/light-trie
Trie error handling
parents
23031b15
52904ae3
Changes
7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
343 additions
and
68 deletions
+343
-68
arc.go
trie/arc.go
+12
-0
errors.go
trie/errors.go
+41
-0
iterator.go
trie/iterator.go
+16
-3
proof.go
trie/proof.go
+10
-1
secure_trie.go
trie/secure_trie.go
+44
-5
trie.go
trie/trie.go
+146
-58
trie_test.go
trie/trie_test.go
+74
-1
No files found.
trie/arc.go
View file @
96d86740
...
@@ -62,6 +62,18 @@ func newARC(c int) *arc {
...
@@ -62,6 +62,18 @@ func newARC(c int) *arc {
}
}
}
}
// Clear clears the cache
func
(
a
*
arc
)
Clear
()
{
a
.
mutex
.
Lock
()
defer
a
.
mutex
.
Unlock
()
a
.
p
=
0
a
.
t1
=
list
.
New
()
a
.
b1
=
list
.
New
()
a
.
t2
=
list
.
New
()
a
.
b2
=
list
.
New
()
a
.
cache
=
make
(
map
[
string
]
*
entry
,
a
.
c
)
}
// Put inserts a new key-value pair into the cache.
// Put inserts a new key-value pair into the cache.
// This optimizes future access to this entry (side effect).
// This optimizes future access to this entry (side effect).
func
(
a
*
arc
)
Put
(
key
hashNode
,
value
node
)
bool
{
func
(
a
*
arc
)
Put
(
key
hashNode
,
value
node
)
bool
{
...
...
trie/errors.go
0 → 100644
View file @
96d86740
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
trie
import
(
"fmt"
"github.com/ethereum/go-ethereum/common"
)
// MissingNodeError is returned by the trie functions (TryGet, TryUpdate, TryDelete)
// in the case where a trie node is not present in the local database. Contains
// information necessary for retrieving the missing node through an ODR service.
//
// NodeHash is the hash of the missing node
// RootHash is the original root of the trie that contains the node
// KeyPrefix is the prefix that leads from the root to the missing node (hex encoded)
// KeySuffix (optional) contains the rest of the key we were looking for, gives a
// hint on which further nodes should also be retrieved (hex encoded)
type
MissingNodeError
struct
{
RootHash
,
NodeHash
common
.
Hash
KeyPrefix
,
KeySuffix
[]
byte
}
func
(
err
*
MissingNodeError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"Missing trie node %064x"
,
err
.
NodeHash
)
}
trie/iterator.go
View file @
96d86740
...
@@ -16,7 +16,12 @@
...
@@ -16,7 +16,12 @@
package
trie
package
trie
import
"bytes"
import
(
"bytes"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
type
Iterator
struct
{
type
Iterator
struct
{
trie
*
Trie
trie
*
Trie
...
@@ -100,7 +105,11 @@ func (self *Iterator) next(node interface{}, key []byte, isIterStart bool) []byt
...
@@ -100,7 +105,11 @@ func (self *Iterator) next(node interface{}, key []byte, isIterStart bool) []byt
}
}
case
hashNode
:
case
hashNode
:
return
self
.
next
(
self
.
trie
.
resolveHash
(
node
),
key
,
isIterStart
)
rn
,
err
:=
self
.
trie
.
resolveHash
(
node
,
nil
,
nil
)
if
err
!=
nil
&&
glog
.
V
(
logger
.
Error
)
{
glog
.
Errorf
(
"Unhandled trie error: %v"
,
err
)
}
return
self
.
next
(
rn
,
key
,
isIterStart
)
}
}
return
nil
return
nil
}
}
...
@@ -127,7 +136,11 @@ func (self *Iterator) key(node interface{}) []byte {
...
@@ -127,7 +136,11 @@ func (self *Iterator) key(node interface{}) []byte {
}
}
}
}
case
hashNode
:
case
hashNode
:
return
self
.
key
(
self
.
trie
.
resolveHash
(
node
))
rn
,
err
:=
self
.
trie
.
resolveHash
(
node
,
nil
,
nil
)
if
err
!=
nil
&&
glog
.
V
(
logger
.
Error
)
{
glog
.
Errorf
(
"Unhandled trie error: %v"
,
err
)
}
return
self
.
key
(
rn
)
}
}
return
nil
return
nil
...
...
trie/proof.go
View file @
96d86740
...
@@ -7,6 +7,8 @@ import (
...
@@ -7,6 +7,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rlp"
)
)
...
@@ -39,7 +41,14 @@ func (t *Trie) Prove(key []byte) []rlp.RawValue {
...
@@ -39,7 +41,14 @@ func (t *Trie) Prove(key []byte) []rlp.RawValue {
case
nil
:
case
nil
:
return
nil
return
nil
case
hashNode
:
case
hashNode
:
tn
=
t
.
resolveHash
(
n
)
var
err
error
tn
,
err
=
t
.
resolveHash
(
n
,
nil
,
nil
)
if
err
!=
nil
{
if
glog
.
V
(
logger
.
Error
)
{
glog
.
Errorf
(
"Unhandled trie error: %v"
,
err
)
}
return
nil
}
default
:
default
:
panic
(
fmt
.
Sprintf
(
"%T: invalid node: %v"
,
tn
,
tn
))
panic
(
fmt
.
Sprintf
(
"%T: invalid node: %v"
,
tn
,
tn
))
}
}
...
...
trie/secure_trie.go
View file @
96d86740
...
@@ -21,6 +21,8 @@ import (
...
@@ -21,6 +21,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
)
var
secureKeyPrefix
=
[]
byte
(
"secure-key-"
)
var
secureKeyPrefix
=
[]
byte
(
"secure-key-"
)
...
@@ -46,8 +48,8 @@ type SecureTrie struct {
...
@@ -46,8 +48,8 @@ type SecureTrie struct {
// NewSecure creates a trie with an existing root node from db.
// NewSecure creates a trie with an existing root node from db.
//
//
// If root is the zero hash or the sha3 hash of an empty string, the
// If root is the zero hash or the sha3 hash of an empty string, the
// trie is initially empty. Otherwise, New will panic
s
if db is nil
// trie is initially empty. Otherwise, New will panic if db is nil
// and returns
ErrMissingRoot if the root node cannp
t be found.
// and returns
MissingNodeError if the root node canno
t be found.
// Accessing the trie loads nodes from db on demand.
// Accessing the trie loads nodes from db on demand.
func
NewSecure
(
root
common
.
Hash
,
db
Database
)
(
*
SecureTrie
,
error
)
{
func
NewSecure
(
root
common
.
Hash
,
db
Database
)
(
*
SecureTrie
,
error
)
{
if
db
==
nil
{
if
db
==
nil
{
...
@@ -63,7 +65,18 @@ func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
...
@@ -63,7 +65,18 @@ func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
// Get returns the value for key stored in the trie.
// Get returns the value for key stored in the trie.
// The value bytes must not be modified by the caller.
// The value bytes must not be modified by the caller.
func
(
t
*
SecureTrie
)
Get
(
key
[]
byte
)
[]
byte
{
func
(
t
*
SecureTrie
)
Get
(
key
[]
byte
)
[]
byte
{
return
t
.
Trie
.
Get
(
t
.
hashKey
(
key
))
res
,
err
:=
t
.
TryGet
(
key
)
if
err
!=
nil
&&
glog
.
V
(
logger
.
Error
)
{
glog
.
Errorf
(
"Unhandled trie error: %v"
,
err
)
}
return
res
}
// TryGet returns the value for key stored in the trie.
// The value bytes must not be modified by the caller.
// If a node was not found in the database, a MissingNodeError is returned.
func
(
t
*
SecureTrie
)
TryGet
(
key
[]
byte
)
([]
byte
,
error
)
{
return
t
.
Trie
.
TryGet
(
t
.
hashKey
(
key
))
}
}
// Update associates key with value in the trie. Subsequent calls to
// Update associates key with value in the trie. Subsequent calls to
...
@@ -73,14 +86,40 @@ func (t *SecureTrie) Get(key []byte) []byte {
...
@@ -73,14 +86,40 @@ func (t *SecureTrie) Get(key []byte) []byte {
// The value bytes must not be modified by the caller while they are
// The value bytes must not be modified by the caller while they are
// stored in the trie.
// stored in the trie.
func
(
t
*
SecureTrie
)
Update
(
key
,
value
[]
byte
)
{
func
(
t
*
SecureTrie
)
Update
(
key
,
value
[]
byte
)
{
if
err
:=
t
.
TryUpdate
(
key
,
value
);
err
!=
nil
&&
glog
.
V
(
logger
.
Error
)
{
glog
.
Errorf
(
"Unhandled trie error: %v"
,
err
)
}
}
// TryUpdate associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
//
// If a node was not found in the database, a MissingNodeError is returned.
func
(
t
*
SecureTrie
)
TryUpdate
(
key
,
value
[]
byte
)
error
{
hk
:=
t
.
hashKey
(
key
)
hk
:=
t
.
hashKey
(
key
)
t
.
Trie
.
Update
(
hk
,
value
)
err
:=
t
.
Trie
.
TryUpdate
(
hk
,
value
)
if
err
!=
nil
{
return
err
}
t
.
Trie
.
db
.
Put
(
t
.
secKey
(
hk
),
key
)
t
.
Trie
.
db
.
Put
(
t
.
secKey
(
hk
),
key
)
return
nil
}
}
// Delete removes any existing value for key from the trie.
// Delete removes any existing value for key from the trie.
func
(
t
*
SecureTrie
)
Delete
(
key
[]
byte
)
{
func
(
t
*
SecureTrie
)
Delete
(
key
[]
byte
)
{
t
.
Trie
.
Delete
(
t
.
hashKey
(
key
))
if
err
:=
t
.
TryDelete
(
key
);
err
!=
nil
&&
glog
.
V
(
logger
.
Error
)
{
glog
.
Errorf
(
"Unhandled trie error: %v"
,
err
)
}
}
// TryDelete removes any existing value for key from the trie.
// If a node was not found in the database, a MissingNodeError is returned.
func
(
t
*
SecureTrie
)
TryDelete
(
key
[]
byte
)
error
{
return
t
.
Trie
.
TryDelete
(
t
.
hashKey
(
key
))
}
}
// GetKey returns the sha3 preimage of a hashed key that was
// GetKey returns the sha3 preimage of a hashed key that was
...
...
trie/trie.go
View file @
96d86740
This diff is collapsed.
Click to expand it.
trie/trie_test.go
View file @
96d86740
...
@@ -64,11 +64,84 @@ func TestMissingRoot(t *testing.T) {
...
@@ -64,11 +64,84 @@ func TestMissingRoot(t *testing.T) {
if
trie
!=
nil
{
if
trie
!=
nil
{
t
.
Error
(
"New returned non-nil trie for invalid root"
)
t
.
Error
(
"New returned non-nil trie for invalid root"
)
}
}
if
err
!=
ErrMissingRoot
{
if
_
,
ok
:=
err
.
(
*
MissingNodeError
);
!
ok
{
t
.
Error
(
"New returned wrong error: %v"
,
err
)
t
.
Error
(
"New returned wrong error: %v"
,
err
)
}
}
}
}
func
TestMissingNode
(
t
*
testing
.
T
)
{
db
,
_
:=
ethdb
.
NewMemDatabase
()
trie
,
_
:=
New
(
common
.
Hash
{},
db
)
updateString
(
trie
,
"120000"
,
"qwerqwerqwerqwerqwerqwerqwerqwer"
)
updateString
(
trie
,
"123456"
,
"asdfasdfasdfasdfasdfasdfasdfasdf"
)
root
,
_
:=
trie
.
Commit
()
ClearGlobalCache
()
trie
,
_
=
New
(
root
,
db
)
_
,
err
:=
trie
.
TryGet
([]
byte
(
"120000"
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
_
,
err
=
trie
.
TryGet
([]
byte
(
"120099"
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
_
,
err
=
trie
.
TryGet
([]
byte
(
"123456"
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
err
=
trie
.
TryUpdate
([]
byte
(
"120099"
),
[]
byte
(
"zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
err
=
trie
.
TryDelete
([]
byte
(
"123456"
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
db
.
Delete
(
common
.
FromHex
(
"e1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9"
))
ClearGlobalCache
()
trie
,
_
=
New
(
root
,
db
)
_
,
err
=
trie
.
TryGet
([]
byte
(
"120000"
))
if
_
,
ok
:=
err
.
(
*
MissingNodeError
);
!
ok
{
t
.
Errorf
(
"Wrong error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
_
,
err
=
trie
.
TryGet
([]
byte
(
"120099"
))
if
_
,
ok
:=
err
.
(
*
MissingNodeError
);
!
ok
{
t
.
Errorf
(
"Wrong error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
_
,
err
=
trie
.
TryGet
([]
byte
(
"123456"
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
err
=
trie
.
TryUpdate
([]
byte
(
"120099"
),
[]
byte
(
"zxcv"
))
if
_
,
ok
:=
err
.
(
*
MissingNodeError
);
!
ok
{
t
.
Errorf
(
"Wrong error: %v"
,
err
)
}
trie
,
_
=
New
(
root
,
db
)
err
=
trie
.
TryDelete
([]
byte
(
"123456"
))
if
_
,
ok
:=
err
.
(
*
MissingNodeError
);
!
ok
{
t
.
Errorf
(
"Wrong error: %v"
,
err
)
}
}
func
TestInsert
(
t
*
testing
.
T
)
{
func
TestInsert
(
t
*
testing
.
T
)
{
trie
:=
newEmpty
()
trie
:=
newEmpty
()
...
...
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