Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
9bc5c4a0
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9bc5c4a0
编写于
2月 20, 2014
作者:
O
obscuren
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Long over due Trie delete implemented
上级
4afb624c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
121 addition
and
3 deletion
+121
-3
ethutil/trie.go
ethutil/trie.go
+87
-1
ethutil/trie_test.go
ethutil/trie_test.go
+34
-2
未找到文件。
ethutil/trie.go
浏览文件 @
9bc5c4a0
...
...
@@ -148,6 +148,10 @@ func (t *Trie) Get(key string) string {
return
c
.
Str
()
}
func
(
t
*
Trie
)
Delete
(
key
string
)
{
t
.
Update
(
key
,
""
)
}
func
(
t
*
Trie
)
GetState
(
node
interface
{},
key
[]
int
)
interface
{}
{
n
:=
NewValue
(
node
)
// Return the node if key is empty (= found)
...
...
@@ -202,9 +206,10 @@ func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{
return
t
.
InsertState
(
node
,
key
,
value
)
}
else
{
// delete it
return
t
.
DeleteState
(
node
,
key
)
}
return
""
return
t
.
Root
}
func
(
t
*
Trie
)
Put
(
node
interface
{})
interface
{}
{
...
...
@@ -313,6 +318,87 @@ func (t *Trie) InsertState(node interface{}, key []int, value interface{}) inter
return
""
}
func
(
t
*
Trie
)
DeleteState
(
node
interface
{},
key
[]
int
)
interface
{}
{
if
len
(
key
)
==
0
{
return
""
}
// New node
n
:=
NewValue
(
node
)
if
node
==
nil
||
(
n
.
Type
()
==
reflect
.
String
&&
(
n
.
Str
()
==
""
||
n
.
Get
(
0
)
.
IsNil
()))
||
n
.
Len
()
==
0
{
return
""
}
currentNode
:=
t
.
GetNode
(
node
)
// Check for "special" 2 slice type node
if
currentNode
.
Len
()
==
2
{
// Decode the key
k
:=
CompactDecode
(
currentNode
.
Get
(
0
)
.
Str
())
v
:=
currentNode
.
Get
(
1
)
.
Raw
()
// Matching key pair (ie. there's already an object with this key)
if
CompareIntSlice
(
k
,
key
)
{
return
""
}
else
if
CompareIntSlice
(
key
[
:
len
(
k
)],
k
)
{
hash
:=
t
.
DeleteState
(
v
,
key
[
len
(
k
)
:
])
child
:=
t
.
GetNode
(
hash
)
var
newNode
[]
interface
{}
if
child
.
Len
()
==
2
{
newKey
:=
append
(
k
,
CompactDecode
(
child
.
Get
(
0
)
.
Str
())
...
)
newNode
=
[]
interface
{}{
CompactEncode
(
newKey
),
child
.
Get
(
1
)
.
Raw
()}
}
else
{
newNode
=
[]
interface
{}{
currentNode
.
Get
(
0
)
.
Str
(),
hash
}
}
return
t
.
Put
(
newNode
)
}
else
{
return
node
}
}
else
{
// Copy the current node over to the new node and replace the first nibble in the key
n
:=
EmptyStringSlice
(
17
)
var
newNode
[]
interface
{}
for
i
:=
0
;
i
<
17
;
i
++
{
cpy
:=
currentNode
.
Get
(
i
)
.
Raw
()
if
cpy
!=
nil
{
n
[
i
]
=
cpy
}
}
n
[
key
[
0
]]
=
t
.
DeleteState
(
n
[
key
[
0
]],
key
[
1
:
])
amount
:=
-
1
for
i
:=
0
;
i
<
17
;
i
++
{
if
n
[
i
]
!=
""
{
if
amount
==
-
1
{
amount
=
i
}
else
{
amount
=
-
2
}
}
}
if
amount
==
16
{
newNode
=
[]
interface
{}{
CompactEncode
([]
int
{
16
}),
n
[
amount
]}
}
else
if
amount
>=
0
{
child
:=
t
.
GetNode
(
n
[
amount
])
if
child
.
Len
()
==
17
{
newNode
=
[]
interface
{}{
CompactEncode
([]
int
{
amount
}),
n
[
amount
]}
}
else
if
child
.
Len
()
==
2
{
key
:=
append
([]
int
{
amount
},
CompactDecode
(
child
.
Get
(
0
)
.
Str
())
...
)
newNode
=
[]
interface
{}{
CompactEncode
(
key
),
child
.
Get
(
1
)
.
Str
()}
}
}
else
{
newNode
=
n
}
return
t
.
Put
(
newNode
)
}
return
""
}
// Simple compare function which creates a rlp value out of the evaluated objects
func
(
t
*
Trie
)
Cmp
(
trie
*
Trie
)
bool
{
return
NewValue
(
t
.
Root
)
.
Cmp
(
NewValue
(
trie
.
Root
))
...
...
ethutil/trie_test.go
浏览文件 @
9bc5c4a0
package
ethutil
import
(
_
"encoding/hex"
_
"fmt"
"reflect"
"testing"
)
...
...
@@ -116,3 +115,36 @@ func TestTrieCmp(t *testing.T) {
t
.
Errorf
(
"Expected tries not to be equal %x %x"
,
trie1
.
Root
,
trie2
.
Root
)
}
}
func
TestTrieDelete
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
trie
.
Update
(
"cat"
,
LONG_WORD
)
exp
:=
trie
.
Root
trie
.
Update
(
"dog"
,
LONG_WORD
)
trie
.
Delete
(
"dog"
)
if
!
reflect
.
DeepEqual
(
exp
,
trie
.
Root
)
{
t
.
Errorf
(
"Expected tries to be equal %x : %x"
,
exp
,
trie
.
Root
)
}
trie
.
Update
(
"dog"
,
LONG_WORD
)
exp
=
trie
.
Root
trie
.
Update
(
"dude"
,
LONG_WORD
)
trie
.
Delete
(
"dude"
)
if
!
reflect
.
DeepEqual
(
exp
,
trie
.
Root
)
{
t
.
Errorf
(
"Expected tries to be equal %x : %x"
,
exp
,
trie
.
Root
)
}
}
func
TestTrieDeleteWithValue
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
trie
.
Update
(
"c"
,
LONG_WORD
)
exp
:=
trie
.
Root
trie
.
Update
(
"ca"
,
LONG_WORD
)
trie
.
Update
(
"cat"
,
LONG_WORD
)
trie
.
Delete
(
"ca"
)
trie
.
Delete
(
"cat"
)
if
!
reflect
.
DeepEqual
(
exp
,
trie
.
Root
)
{
t
.
Errorf
(
"Expected tries to be equal %x : %x"
,
exp
,
trie
.
Root
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录