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
a4a2343c
Commit
a4a2343c
authored
Jul 02, 2018
by
gary rong
Committed by
Péter Szilágyi
Jul 02, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ethdb, core: implement delete for db batch (#17101)
parent
fdfd6d3c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
51 additions
and
14 deletions
+51
-14
blockchain.go
core/blockchain.go
+6
-3
headerchain.go
core/headerchain.go
+12
-7
database.go
ethdb/database.go
+10
-0
interface.go
ethdb/interface.go
+7
-1
memory_database.go
ethdb/memory_database.go
+9
-0
txpool.go
light/txpool.go
+7
-3
No files found.
core/blockchain.go
View file @
a4a2343c
...
@@ -269,8 +269,8 @@ func (bc *BlockChain) SetHead(head uint64) error {
...
@@ -269,8 +269,8 @@ func (bc *BlockChain) SetHead(head uint64) error {
defer
bc
.
mu
.
Unlock
()
defer
bc
.
mu
.
Unlock
()
// Rewind the header chain, deleting all block bodies until then
// Rewind the header chain, deleting all block bodies until then
delFn
:=
func
(
hash
common
.
Hash
,
num
uint64
)
{
delFn
:=
func
(
db
rawdb
.
DatabaseDeleter
,
hash
common
.
Hash
,
num
uint64
)
{
rawdb
.
DeleteBody
(
bc
.
db
,
hash
,
num
)
rawdb
.
DeleteBody
(
db
,
hash
,
num
)
}
}
bc
.
hc
.
SetHead
(
head
,
delFn
)
bc
.
hc
.
SetHead
(
head
,
delFn
)
currentHeader
:=
bc
.
hc
.
CurrentHeader
()
currentHeader
:=
bc
.
hc
.
CurrentHeader
()
...
@@ -1340,9 +1340,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
...
@@ -1340,9 +1340,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
diff
:=
types
.
TxDifference
(
deletedTxs
,
addedTxs
)
diff
:=
types
.
TxDifference
(
deletedTxs
,
addedTxs
)
// When transactions get deleted from the database that means the
// When transactions get deleted from the database that means the
// receipts that were created in the fork must also be deleted
// receipts that were created in the fork must also be deleted
batch
:=
bc
.
db
.
NewBatch
()
for
_
,
tx
:=
range
diff
{
for
_
,
tx
:=
range
diff
{
rawdb
.
DeleteTxLookupEntry
(
b
c
.
db
,
tx
.
Hash
())
rawdb
.
DeleteTxLookupEntry
(
b
atch
,
tx
.
Hash
())
}
}
batch
.
Write
()
if
len
(
deletedLogs
)
>
0
{
if
len
(
deletedLogs
)
>
0
{
go
bc
.
rmLogsFeed
.
Send
(
RemovedLogsEvent
{
deletedLogs
})
go
bc
.
rmLogsFeed
.
Send
(
RemovedLogsEvent
{
deletedLogs
})
}
}
...
...
core/headerchain.go
View file @
a4a2343c
...
@@ -156,13 +156,16 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
...
@@ -156,13 +156,16 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
if
externTd
.
Cmp
(
localTd
)
>
0
||
(
externTd
.
Cmp
(
localTd
)
==
0
&&
mrand
.
Float64
()
<
0.5
)
{
if
externTd
.
Cmp
(
localTd
)
>
0
||
(
externTd
.
Cmp
(
localTd
)
==
0
&&
mrand
.
Float64
()
<
0.5
)
{
// Delete any canonical number assignments above the new head
// Delete any canonical number assignments above the new head
batch
:=
hc
.
chainDb
.
NewBatch
()
for
i
:=
number
+
1
;
;
i
++
{
for
i
:=
number
+
1
;
;
i
++
{
hash
:=
rawdb
.
ReadCanonicalHash
(
hc
.
chainDb
,
i
)
hash
:=
rawdb
.
ReadCanonicalHash
(
hc
.
chainDb
,
i
)
if
hash
==
(
common
.
Hash
{})
{
if
hash
==
(
common
.
Hash
{})
{
break
break
}
}
rawdb
.
DeleteCanonicalHash
(
hc
.
chainDb
,
i
)
rawdb
.
DeleteCanonicalHash
(
batch
,
i
)
}
}
batch
.
Write
()
// Overwrite any stale canonical number assignments
// Overwrite any stale canonical number assignments
var
(
var
(
headHash
=
header
.
ParentHash
headHash
=
header
.
ParentHash
...
@@ -438,7 +441,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
...
@@ -438,7 +441,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
// DeleteCallback is a callback function that is called by SetHead before
// DeleteCallback is a callback function that is called by SetHead before
// each header is deleted.
// each header is deleted.
type
DeleteCallback
func
(
common
.
Hash
,
uint64
)
type
DeleteCallback
func
(
rawdb
.
DatabaseDeleter
,
common
.
Hash
,
uint64
)
// SetHead rewinds the local chain to a new head. Everything above the new head
// SetHead rewinds the local chain to a new head. Everything above the new head
// will be deleted and the new one set.
// will be deleted and the new one set.
...
@@ -448,22 +451,24 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
...
@@ -448,22 +451,24 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
if
hdr
:=
hc
.
CurrentHeader
();
hdr
!=
nil
{
if
hdr
:=
hc
.
CurrentHeader
();
hdr
!=
nil
{
height
=
hdr
.
Number
.
Uint64
()
height
=
hdr
.
Number
.
Uint64
()
}
}
batch
:=
hc
.
chainDb
.
NewBatch
()
for
hdr
:=
hc
.
CurrentHeader
();
hdr
!=
nil
&&
hdr
.
Number
.
Uint64
()
>
head
;
hdr
=
hc
.
CurrentHeader
()
{
for
hdr
:=
hc
.
CurrentHeader
();
hdr
!=
nil
&&
hdr
.
Number
.
Uint64
()
>
head
;
hdr
=
hc
.
CurrentHeader
()
{
hash
:=
hdr
.
Hash
()
hash
:=
hdr
.
Hash
()
num
:=
hdr
.
Number
.
Uint64
()
num
:=
hdr
.
Number
.
Uint64
()
if
delFn
!=
nil
{
if
delFn
!=
nil
{
delFn
(
hash
,
num
)
delFn
(
batch
,
hash
,
num
)
}
}
rawdb
.
DeleteHeader
(
hc
.
chainDb
,
hash
,
num
)
rawdb
.
DeleteHeader
(
batch
,
hash
,
num
)
rawdb
.
DeleteTd
(
hc
.
chainDb
,
hash
,
num
)
rawdb
.
DeleteTd
(
batch
,
hash
,
num
)
hc
.
currentHeader
.
Store
(
hc
.
GetHeader
(
hdr
.
ParentHash
,
hdr
.
Number
.
Uint64
()
-
1
))
hc
.
currentHeader
.
Store
(
hc
.
GetHeader
(
hdr
.
ParentHash
,
hdr
.
Number
.
Uint64
()
-
1
))
}
}
// Roll back the canonical chain numbering
// Roll back the canonical chain numbering
for
i
:=
height
;
i
>
head
;
i
--
{
for
i
:=
height
;
i
>
head
;
i
--
{
rawdb
.
DeleteCanonicalHash
(
hc
.
chainDb
,
i
)
rawdb
.
DeleteCanonicalHash
(
batch
,
i
)
}
}
batch
.
Write
()
// Clear out any stale content from the caches
// Clear out any stale content from the caches
hc
.
headerCache
.
Purge
()
hc
.
headerCache
.
Purge
()
hc
.
tdCache
.
Purge
()
hc
.
tdCache
.
Purge
()
...
...
ethdb/database.go
View file @
a4a2343c
...
@@ -388,6 +388,12 @@ func (b *ldbBatch) Put(key, value []byte) error {
...
@@ -388,6 +388,12 @@ func (b *ldbBatch) Put(key, value []byte) error {
return
nil
return
nil
}
}
func
(
b
*
ldbBatch
)
Delete
(
key
[]
byte
)
error
{
b
.
b
.
Delete
(
key
)
b
.
size
+=
1
return
nil
}
func
(
b
*
ldbBatch
)
Write
()
error
{
func
(
b
*
ldbBatch
)
Write
()
error
{
return
b
.
db
.
Write
(
b
.
b
,
nil
)
return
b
.
db
.
Write
(
b
.
b
,
nil
)
}
}
...
@@ -453,6 +459,10 @@ func (tb *tableBatch) Put(key, value []byte) error {
...
@@ -453,6 +459,10 @@ func (tb *tableBatch) Put(key, value []byte) error {
return
tb
.
batch
.
Put
(
append
([]
byte
(
tb
.
prefix
),
key
...
),
value
)
return
tb
.
batch
.
Put
(
append
([]
byte
(
tb
.
prefix
),
key
...
),
value
)
}
}
func
(
tb
*
tableBatch
)
Delete
(
key
[]
byte
)
error
{
return
tb
.
batch
.
Delete
(
append
([]
byte
(
tb
.
prefix
),
key
...
))
}
func
(
tb
*
tableBatch
)
Write
()
error
{
func
(
tb
*
tableBatch
)
Write
()
error
{
return
tb
.
batch
.
Write
()
return
tb
.
batch
.
Write
()
}
}
...
...
ethdb/interface.go
View file @
a4a2343c
...
@@ -25,12 +25,17 @@ type Putter interface {
...
@@ -25,12 +25,17 @@ type Putter interface {
Put
(
key
[]
byte
,
value
[]
byte
)
error
Put
(
key
[]
byte
,
value
[]
byte
)
error
}
}
// Deleter wraps the database delete operation supported by both batches and regular databases.
type
Deleter
interface
{
Delete
(
key
[]
byte
)
error
}
// Database wraps all database operations. All methods are safe for concurrent use.
// Database wraps all database operations. All methods are safe for concurrent use.
type
Database
interface
{
type
Database
interface
{
Putter
Putter
Deleter
Get
(
key
[]
byte
)
([]
byte
,
error
)
Get
(
key
[]
byte
)
([]
byte
,
error
)
Has
(
key
[]
byte
)
(
bool
,
error
)
Has
(
key
[]
byte
)
(
bool
,
error
)
Delete
(
key
[]
byte
)
error
Close
()
Close
()
NewBatch
()
Batch
NewBatch
()
Batch
}
}
...
@@ -39,6 +44,7 @@ type Database interface {
...
@@ -39,6 +44,7 @@ type Database interface {
// when Write is called. Batch cannot be used concurrently.
// when Write is called. Batch cannot be used concurrently.
type
Batch
interface
{
type
Batch
interface
{
Putter
Putter
Deleter
ValueSize
()
int
// amount of data in the batch
ValueSize
()
int
// amount of data in the batch
Write
()
error
Write
()
error
// Reset resets the batch for reuse
// Reset resets the batch for reuse
...
...
ethdb/memory_database.go
View file @
a4a2343c
...
@@ -110,11 +110,20 @@ func (b *memBatch) Put(key, value []byte) error {
...
@@ -110,11 +110,20 @@ func (b *memBatch) Put(key, value []byte) error {
return
nil
return
nil
}
}
func
(
b
*
memBatch
)
Delete
(
key
[]
byte
)
error
{
b
.
writes
=
append
(
b
.
writes
,
kv
{
common
.
CopyBytes
(
key
),
nil
})
return
nil
}
func
(
b
*
memBatch
)
Write
()
error
{
func
(
b
*
memBatch
)
Write
()
error
{
b
.
db
.
lock
.
Lock
()
b
.
db
.
lock
.
Lock
()
defer
b
.
db
.
lock
.
Unlock
()
defer
b
.
db
.
lock
.
Unlock
()
for
_
,
kv
:=
range
b
.
writes
{
for
_
,
kv
:=
range
b
.
writes
{
if
kv
.
v
==
nil
{
delete
(
b
.
db
.
db
,
string
(
kv
.
k
))
continue
}
b
.
db
.
db
[
string
(
kv
.
k
)]
=
kv
.
v
b
.
db
.
db
[
string
(
kv
.
k
)]
=
kv
.
v
}
}
return
nil
return
nil
...
...
light/txpool.go
View file @
a4a2343c
...
@@ -199,15 +199,17 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
...
@@ -199,15 +199,17 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
// rollbackTxs marks the transactions contained in recently rolled back blocks
// rollbackTxs marks the transactions contained in recently rolled back blocks
// as rolled back. It also removes any positional lookup entries.
// as rolled back. It also removes any positional lookup entries.
func
(
pool
*
TxPool
)
rollbackTxs
(
hash
common
.
Hash
,
txc
txStateChanges
)
{
func
(
pool
*
TxPool
)
rollbackTxs
(
hash
common
.
Hash
,
txc
txStateChanges
)
{
batch
:=
pool
.
chainDb
.
NewBatch
()
if
list
,
ok
:=
pool
.
mined
[
hash
];
ok
{
if
list
,
ok
:=
pool
.
mined
[
hash
];
ok
{
for
_
,
tx
:=
range
list
{
for
_
,
tx
:=
range
list
{
txHash
:=
tx
.
Hash
()
txHash
:=
tx
.
Hash
()
rawdb
.
DeleteTxLookupEntry
(
pool
.
chainDb
,
txHash
)
rawdb
.
DeleteTxLookupEntry
(
batch
,
txHash
)
pool
.
pending
[
txHash
]
=
tx
pool
.
pending
[
txHash
]
=
tx
txc
.
setState
(
txHash
,
false
)
txc
.
setState
(
txHash
,
false
)
}
}
delete
(
pool
.
mined
,
hash
)
delete
(
pool
.
mined
,
hash
)
}
}
batch
.
Write
()
}
}
// reorgOnNewHead sets a new head header, processing (and rolling back if necessary)
// reorgOnNewHead sets a new head header, processing (and rolling back if necessary)
...
@@ -504,14 +506,16 @@ func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common
...
@@ -504,14 +506,16 @@ func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common
func
(
self
*
TxPool
)
RemoveTransactions
(
txs
types
.
Transactions
)
{
func
(
self
*
TxPool
)
RemoveTransactions
(
txs
types
.
Transactions
)
{
self
.
mu
.
Lock
()
self
.
mu
.
Lock
()
defer
self
.
mu
.
Unlock
()
defer
self
.
mu
.
Unlock
()
var
hashes
[]
common
.
Hash
var
hashes
[]
common
.
Hash
batch
:=
self
.
chainDb
.
NewBatch
()
for
_
,
tx
:=
range
txs
{
for
_
,
tx
:=
range
txs
{
//self.RemoveTx(tx.Hash())
hash
:=
tx
.
Hash
()
hash
:=
tx
.
Hash
()
delete
(
self
.
pending
,
hash
)
delete
(
self
.
pending
,
hash
)
self
.
chainDb
.
Delete
(
hash
[
:
]
)
batch
.
Delete
(
hash
.
Bytes
()
)
hashes
=
append
(
hashes
,
hash
)
hashes
=
append
(
hashes
,
hash
)
}
}
batch
.
Write
()
self
.
relay
.
Discard
(
hashes
)
self
.
relay
.
Discard
(
hashes
)
}
}
...
...
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