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
992e4f83
Commit
992e4f83
authored
Jun 29, 2015
by
Jeffrey Wilcke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: replaced BlockCache with lru.Cache
parent
a8ebf756
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
20 deletions
+25
-20
chain_manager.go
core/chain_manager.go
+24
-19
chain_manager_test.go
core/chain_manager_test.go
+1
-1
No files found.
core/chain_manager.go
View file @
992e4f83
...
@@ -109,9 +109,9 @@ type ChainManager struct {
...
@@ -109,9 +109,9 @@ type ChainManager struct {
transState
*
state
.
StateDB
transState
*
state
.
StateDB
txState
*
state
.
ManagedState
txState
*
state
.
ManagedState
cache
*
lru
.
Cache
// cache is the LRU caching
cache
*
lru
.
Cache
// cache is the LRU caching
futureBlocks
*
Block
Cache
// future blocks are blocks added for later processing
futureBlocks
*
lru
.
Cache
// future blocks are blocks added for later processing
pendingBlocks
*
Block
Cache
// pending blocks contain blocks not yet written to the db
pendingBlocks
*
lru
.
Cache
// pending blocks contain blocks not yet written to the db
quit
chan
struct
{}
quit
chan
struct
{}
// procInterrupt must be atomically called
// procInterrupt must be atomically called
...
@@ -158,7 +158,7 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow
...
@@ -158,7 +158,7 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow
// Take ownership of this particular state
// Take ownership of this particular state
bc
.
txState
=
state
.
ManageState
(
bc
.
State
()
.
Copy
())
bc
.
txState
=
state
.
ManageState
(
bc
.
State
()
.
Copy
())
bc
.
futureBlocks
=
NewBlockCache
(
maxFutureBlocks
)
bc
.
futureBlocks
,
_
=
lru
.
New
(
maxFutureBlocks
)
bc
.
makeCache
()
bc
.
makeCache
()
go
bc
.
update
()
go
bc
.
update
()
...
@@ -390,7 +390,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool {
...
@@ -390,7 +390,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool {
}
}
if
bc
.
pendingBlocks
!=
nil
{
if
bc
.
pendingBlocks
!=
nil
{
if
block
:=
bc
.
pendingBlocks
.
Get
(
hash
);
block
!=
nil
{
if
_
,
exist
:=
bc
.
pendingBlocks
.
Get
(
hash
);
exist
{
return
true
return
true
}
}
}
}
...
@@ -426,8 +426,8 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
...
@@ -426,8 +426,8 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
}
}
if
self
.
pendingBlocks
!=
nil
{
if
self
.
pendingBlocks
!=
nil
{
if
block
:=
self
.
pendingBlocks
.
Get
(
hash
);
block
!=
nil
{
if
block
,
_
:=
self
.
pendingBlocks
.
Get
(
hash
);
block
!=
nil
{
return
block
return
block
.
(
*
types
.
Block
)
}
}
}
}
...
@@ -510,10 +510,11 @@ type queueEvent struct {
...
@@ -510,10 +510,11 @@ type queueEvent struct {
}
}
func
(
self
*
ChainManager
)
procFutureBlocks
()
{
func
(
self
*
ChainManager
)
procFutureBlocks
()
{
var
blocks
[]
*
types
.
Block
blocks
:=
make
([]
*
types
.
Block
,
self
.
futureBlocks
.
Len
())
self
.
futureBlocks
.
Each
(
func
(
i
int
,
block
*
types
.
Block
)
{
for
i
,
hash
:=
range
self
.
futureBlocks
.
Keys
()
{
blocks
=
append
(
blocks
,
block
)
block
,
_
:=
self
.
futureBlocks
.
Get
(
hash
)
})
blocks
[
i
]
=
block
.
(
*
types
.
Block
)
}
if
len
(
blocks
)
>
0
{
if
len
(
blocks
)
>
0
{
types
.
BlockBy
(
types
.
Number
)
.
Sort
(
blocks
)
types
.
BlockBy
(
types
.
Number
)
.
Sort
(
blocks
)
self
.
InsertChain
(
blocks
)
self
.
InsertChain
(
blocks
)
...
@@ -521,13 +522,16 @@ func (self *ChainManager) procFutureBlocks() {
...
@@ -521,13 +522,16 @@ func (self *ChainManager) procFutureBlocks() {
}
}
func
(
self
*
ChainManager
)
enqueueForWrite
(
block
*
types
.
Block
)
{
func
(
self
*
ChainManager
)
enqueueForWrite
(
block
*
types
.
Block
)
{
self
.
pendingBlocks
.
Push
(
block
)
self
.
pendingBlocks
.
Add
(
block
.
Hash
(),
block
)
}
}
func
(
self
*
ChainManager
)
flushQueuedBlocks
()
{
func
(
self
*
ChainManager
)
flushQueuedBlocks
()
{
db
,
batchWrite
:=
self
.
blockDb
.
(
*
ethdb
.
LDBDatabase
)
db
,
batchWrite
:=
self
.
blockDb
.
(
*
ethdb
.
LDBDatabase
)
batch
:=
new
(
leveldb
.
Batch
)
batch
:=
new
(
leveldb
.
Batch
)
self
.
pendingBlocks
.
Each
(
func
(
i
int
,
block
*
types
.
Block
)
{
for
_
,
key
:=
range
self
.
pendingBlocks
.
Keys
()
{
b
,
_
:=
self
.
pendingBlocks
.
Get
(
key
)
block
:=
b
.
(
*
types
.
Block
)
enc
,
_
:=
rlp
.
EncodeToBytes
((
*
types
.
StorageBlock
)(
block
))
enc
,
_
:=
rlp
.
EncodeToBytes
((
*
types
.
StorageBlock
)(
block
))
key
:=
append
(
blockHashPre
,
block
.
Hash
()
.
Bytes
()
...
)
key
:=
append
(
blockHashPre
,
block
.
Hash
()
.
Bytes
()
...
)
if
batchWrite
{
if
batchWrite
{
...
@@ -535,7 +539,8 @@ func (self *ChainManager) flushQueuedBlocks() {
...
@@ -535,7 +539,8 @@ func (self *ChainManager) flushQueuedBlocks() {
}
else
{
}
else
{
self
.
blockDb
.
Put
(
key
,
enc
)
self
.
blockDb
.
Put
(
key
,
enc
)
}
}
})
}
if
batchWrite
{
if
batchWrite
{
db
.
LDB
()
.
Write
(
batch
,
nil
)
db
.
LDB
()
.
Write
(
batch
,
nil
)
}
}
...
@@ -588,7 +593,7 @@ func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, er
...
@@ -588,7 +593,7 @@ func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, er
self
.
enqueueForWrite
(
block
)
self
.
enqueueForWrite
(
block
)
self
.
mu
.
Unlock
()
self
.
mu
.
Unlock
()
// Delete from future blocks
// Delete from future blocks
self
.
futureBlocks
.
Delet
e
(
block
.
Hash
())
self
.
futureBlocks
.
Remov
e
(
block
.
Hash
())
return
return
}
}
...
@@ -602,7 +607,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
...
@@ -602,7 +607,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
self
.
chainmu
.
Lock
()
self
.
chainmu
.
Lock
()
defer
self
.
chainmu
.
Unlock
()
defer
self
.
chainmu
.
Unlock
()
self
.
pendingBlocks
=
NewBlockCache
(
len
(
chain
))
self
.
pendingBlocks
,
_
=
lru
.
New
(
len
(
chain
))
// A queued approach to delivering events. This is generally
// A queued approach to delivering events. This is generally
// faster than direct delivery and requires much less mutex
// faster than direct delivery and requires much less mutex
...
@@ -669,13 +674,13 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
...
@@ -669,13 +674,13 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
return
i
,
fmt
.
Errorf
(
"%v: BlockFutureErr, %v > %v"
,
BlockFutureErr
,
block
.
Time
(),
max
)
return
i
,
fmt
.
Errorf
(
"%v: BlockFutureErr, %v > %v"
,
BlockFutureErr
,
block
.
Time
(),
max
)
}
}
self
.
futureBlocks
.
Push
(
block
)
self
.
futureBlocks
.
Add
(
block
.
Hash
(),
block
)
stats
.
queued
++
stats
.
queued
++
continue
continue
}
}
if
IsParentErr
(
err
)
&&
self
.
futureBlocks
.
Ha
s
(
block
.
ParentHash
())
{
if
IsParentErr
(
err
)
&&
self
.
futureBlocks
.
Contain
s
(
block
.
ParentHash
())
{
self
.
futureBlocks
.
Push
(
block
)
self
.
futureBlocks
.
Add
(
block
.
Hash
(),
block
)
stats
.
queued
++
stats
.
queued
++
continue
continue
}
}
...
...
core/chain_manager_test.go
View file @
992e4f83
...
@@ -393,7 +393,7 @@ func chm(genesis *types.Block, db common.Database) *ChainManager {
...
@@ -393,7 +393,7 @@ func chm(genesis *types.Block, db common.Database) *ChainManager {
var
eventMux
event
.
TypeMux
var
eventMux
event
.
TypeMux
bc
:=
&
ChainManager
{
blockDb
:
db
,
stateDb
:
db
,
genesisBlock
:
genesis
,
eventMux
:
&
eventMux
,
pow
:
FakePow
{}}
bc
:=
&
ChainManager
{
blockDb
:
db
,
stateDb
:
db
,
genesisBlock
:
genesis
,
eventMux
:
&
eventMux
,
pow
:
FakePow
{}}
bc
.
cache
,
_
=
lru
.
New
(
100
)
bc
.
cache
,
_
=
lru
.
New
(
100
)
bc
.
futureBlocks
=
NewBlockCache
(
100
)
bc
.
futureBlocks
,
_
=
lru
.
New
(
100
)
bc
.
processor
=
bproc
{}
bc
.
processor
=
bproc
{}
bc
.
ResetWithGenesisBlock
(
genesis
)
bc
.
ResetWithGenesisBlock
(
genesis
)
bc
.
txState
=
state
.
ManageState
(
bc
.
State
())
bc
.
txState
=
state
.
ManageState
(
bc
.
State
())
...
...
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