Skip to content

Commit 565d9f2

Browse files
committed
core, trie: new trie
1 parent 6b91a4a commit 565d9f2

20 files changed

Lines changed: 1110 additions & 953 deletions

‎build/update-license.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ var (
4646
skipPrefixes = []string{
4747
// boring stuff
4848
"Godeps/", "tests/files/", "build/",
49-
// don't relicense vendored packages
49+
// don't relicense vendored sources
5050
"crypto/sha3/", "crypto/ecies/", "logger/glog/",
5151
"crypto/curve.go",
52+
"trie/arc.go",
5253
}
5354

5455
// paths with this prefix are licensed as GPL. all other files are LGPL.

‎core/state/state_object.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,13 @@ type StateObject struct {
9090

9191
func NewStateObject(address common.Address, db ethdb.Database) *StateObject {
9292
object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
93-
object.trie = trie.NewSecure((common.Hash{}).Bytes(), db)
93+
object.trie, _ = trie.NewSecure(common.Hash{}, db)
9494
object.storage = make(Storage)
9595
object.gasPool = new(big.Int)
96-
9796
return object
9897
}
9998

10099
func NewStateObjectFromBytes(address common.Address, data []byte, db ethdb.Database) *StateObject {
101-
// TODO clean me up
102100
var extobject struct {
103101
Nonce uint64
104102
Balance *big.Int
@@ -107,19 +105,24 @@ func NewStateObjectFromBytes(address common.Address, data []byte, db ethdb.Datab
107105
}
108106
err := rlp.Decode(bytes.NewReader(data), &extobject)
109107
if err != nil {
110-
fmt.Println(err)
108+
glog.Errorf("can't decode state object %x: %v", address, err)
109+
return nil
110+
}
111+
trie, err := trie.NewSecure(extobject.Root, db)
112+
if err != nil {
113+
// TODO: bubble this up or panic
114+
glog.Errorf("can't create account trie with root %x: %v", extobject.Root[:], err)
111115
return nil
112116
}
113117

114118
object := &StateObject{address: address, db: db}
115119
object.nonce = extobject.Nonce
116120
object.balance = extobject.Balance
117121
object.codeHash = extobject.CodeHash
118-
object.trie = trie.NewSecure(extobject.Root[:], db)
122+
object.trie = trie
119123
object.storage = make(map[string]common.Hash)
120124
object.gasPool = new(big.Int)
121125
object.code, _ = db.Get(extobject.CodeHash)
122-
123126
return object
124127
}
125128

‎core/state/statedb.go‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ type StateDB struct {
4949

5050
// Create a new state from a given trie
5151
func New(root common.Hash, db ethdb.Database) *StateDB {
52-
trie := trie.NewSecure(root[:], db)
53-
return &StateDB{root: root, db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: new(big.Int), logs: make(map[common.Hash]Logs)}
54-
}
55-
56-
func (self *StateDB) PrintRoot() {
57-
self.trie.Trie.PrintRoot()
52+
tr, err := trie.NewSecure(root, db)
53+
if err != nil {
54+
// TODO: bubble this up
55+
tr, _ = trie.NewSecure(common.Hash{}, db)
56+
glog.Errorf("can't create state trie with root %x: %v", root[:], err)
57+
}
58+
return &StateDB{
59+
root: root,
60+
db: db,
61+
trie: tr,
62+
stateObjects: make(map[string]*StateObject),
63+
refund: new(big.Int),
64+
logs: make(map[common.Hash]Logs),
65+
}
5866
}
5967

6068
func (self *StateDB) StartRecord(thash, bhash common.Hash, ti int) {
@@ -304,7 +312,7 @@ func (self *StateDB) Set(state *StateDB) {
304312
}
305313

306314
func (s *StateDB) Root() common.Hash {
307-
return common.BytesToHash(s.trie.Root())
315+
return s.trie.Hash()
308316
}
309317

310318
// Syncs the trie and all siblings
@@ -348,7 +356,7 @@ func (self *StateDB) SyncIntermediate() {
348356

349357
// SyncObjects syncs the changed objects to the trie
350358
func (self *StateDB) SyncObjects() {
351-
self.trie = trie.NewSecure(self.root[:], self.db)
359+
self.trie, _ = trie.NewSecure(self.root, self.db)
352360

353361
self.refund = new(big.Int)
354362

‎core/types/derive_sha.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package types
1818

1919
import (
20+
"bytes"
21+
2022
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/ethdb"
2223
"github.com/ethereum/go-ethereum/rlp"
2324
"github.com/ethereum/go-ethereum/trie"
2425
)
@@ -29,12 +30,12 @@ type DerivableList interface {
2930
}
3031

3132
func DeriveSha(list DerivableList) common.Hash {
32-
db, _ := ethdb.NewMemDatabase()
33-
trie := trie.New(nil, db)
33+
keybuf := new(bytes.Buffer)
34+
trie := new(trie.Trie)
3435
for i := 0; i < list.Len(); i++ {
35-
key, _ := rlp.EncodeToBytes(uint(i))
36-
trie.Update(key, list.GetRlp(i))
36+
keybuf.Reset()
37+
rlp.Encode(keybuf, uint(i))
38+
trie.Update(keybuf.Bytes(), list.GetRlp(i))
3739
}
38-
39-
return common.BytesToHash(trie.Root())
40+
return trie.Hash()
4041
}

‎trie/arc.go‎

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Copyright (c) 2015 Hans Alexander Gugel <alexander.gugel@gmail.com>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
// This file contains a modified version of package arc from
22+
// https://github.com/alexanderGugel/arc
23+
//
24+
// It implements the ARC (Adaptive Replacement Cache) algorithm as detailed in
25+
// https://www.usenix.org/legacy/event/fast03/tech/full_papers/megiddo/megiddo.pdf
26+
27+
package trie
28+
29+
import (
30+
"container/list"
31+
"sync"
32+
)
33+
34+
type arc struct {
35+
p int
36+
c int
37+
t1 *list.List
38+
b1 *list.List
39+
t2 *list.List
40+
b2 *list.List
41+
cache map[string]*entry
42+
mutex sync.Mutex
43+
}
44+
45+
type entry struct {
46+
key hashNode
47+
value node
48+
ll *list.List
49+
el *list.Element
50+
}
51+
52+
// newARC returns a new Adaptive Replacement Cache with the
53+
// given capacity.
54+
func newARC(c int) *arc {
55+
return &arc{
56+
c: c,
57+
t1: list.New(),
58+
b1: list.New(),
59+
t2: list.New(),
60+
b2: list.New(),
61+
cache: make(map[string]*entry, c),
62+
}
63+
}
64+
65+
// Put inserts a new key-value pair into the cache.
66+
// This optimizes future access to this entry (side effect).
67+
func (a *arc) Put(key hashNode, value node) bool {
68+
a.mutex.Lock()
69+
defer a.mutex.Unlock()
70+
ent, ok := a.cache[string(key)]
71+
if ok != true {
72+
ent = &entry{key: key, value: value}
73+
a.req(ent)
74+
a.cache[string(key)] = ent
75+
} else {
76+
ent.value = value
77+
a.req(ent)
78+
}
79+
return ok
80+
}
81+
82+
// Get retrieves a previously via Set inserted entry.
83+
// This optimizes future access to this entry (side effect).
84+
func (a *arc) Get(key hashNode) (value node, ok bool) {
85+
a.mutex.Lock()
86+
defer a.mutex.Unlock()
87+
ent, ok := a.cache[string(key)]
88+
if ok {
89+
a.req(ent)
90+
return ent.value, ent.value != nil
91+
}
92+
return nil, false
93+
}
94+
95+
func (a *arc) req(ent *entry) {
96+
if ent.ll == a.t1 || ent.ll == a.t2 {
97+
// Case I
98+
ent.setMRU(a.t2)
99+
} else if ent.ll == a.b1 {
100+
// Case II
101+
// Cache Miss in t1 and t2
102+
103+
// Adaptation
104+
var d int
105+
if a.b1.Len() >= a.b2.Len() {
106+
d = 1
107+
} else {
108+
d = a.b2.Len() / a.b1.Len()
109+
}
110+
a.p = a.p + d
111+
if a.p > a.c {
112+
a.p = a.c
113+
}
114+
115+
a.replace(ent)
116+
ent.setMRU(a.t2)
117+
} else if ent.ll == a.b2 {
118+
// Case III
119+
// Cache Miss in t1 and t2
120+
121+
// Adaptation
122+
var d int
123+
if a.b2.Len() >= a.b1.Len() {
124+
d = 1
125+
} else {
126+
d = a.b1.Len() / a.b2.Len()
127+
}
128+
a.p = a.p - d
129+
if a.p < 0 {
130+
a.p = 0
131+
}
132+
133+
a.replace(ent)
134+
ent.setMRU(a.t2)
135+
} else if ent.ll == nil {
136+
// Case IV
137+
138+
if a.t1.Len()+a.b1.Len() == a.c {
139+
// Case A
140+
if a.t1.Len() < a.c {
141+
a.delLRU(a.b1)
142+
a.replace(ent)
143+
} else {
144+
a.delLRU(a.t1)
145+
}
146+
} else if a.t1.Len()+a.b1.Len() < a.c {
147+
// Case B
148+
if a.t1.Len()+a.t2.Len()+a.b1.Len()+a.b2.Len() >= a.c {
149+
if a.t1.Len()+a.t2.Len()+a.b1.Len()+a.b2.Len() == 2*a.c {
150+
a.delLRU(a.b2)
151+
}
152+
a.replace(ent)
153+
}
154+
}
155+
156+
ent.setMRU(a.t1)
157+
}
158+
}
159+
160+
func (a *arc) delLRU(list *list.List) {
161+
lru := list.Back()
162+
list.Remove(lru)
163+
delete(a.cache, string(lru.Value.(*entry).key))
164+
}
165+
166+
func (a *arc) replace(ent *entry) {
167+
if a.t1.Len() > 0 && ((a.t1.Len() > a.p) || (ent.ll == a.b2 && a.t1.Len() == a.p)) {
168+
lru := a.t1.Back().Value.(*entry)
169+
lru.value = nil
170+
lru.setMRU(a.b1)
171+
} else {
172+
lru := a.t2.Back().Value.(*entry)
173+
lru.value = nil
174+
lru.setMRU(a.b2)
175+
}
176+
}
177+
178+
func (e *entry) setLRU(list *list.List) {
179+
e.detach()
180+
e.ll = list
181+
e.el = e.ll.PushBack(e)
182+
}
183+
184+
func (e *entry) setMRU(list *list.List) {
185+
e.detach()
186+
e.ll = list
187+
e.el = e.ll.PushFront(e)
188+
}
189+
190+
func (e *entry) detach() {
191+
if e.ll != nil {
192+
e.ll.Remove(e.el)
193+
}
194+
}

0 commit comments

Comments
 (0)