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 {
defer
bc
.
mu
.
Unlock
()
// Rewind the header chain, deleting all block bodies until then
delFn
:=
func
(
hash
common
.
Hash
,
num
uint64
)
{
rawdb
.
DeleteBody
(
bc
.
db
,
hash
,
num
)
delFn
:=
func
(
db
rawdb
.
DatabaseDeleter
,
hash
common
.
Hash
,
num
uint64
)
{
rawdb
.
DeleteBody
(
db
,
hash
,
num
)
}
bc
.
hc
.
SetHead
(
head
,
delFn
)
currentHeader
:=
bc
.
hc
.
CurrentHeader
()
...
...
@@ -1340,9 +1340,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
diff
:=
types
.
TxDifference
(
deletedTxs
,
addedTxs
)
// When transactions get deleted from the database that means the
// receipts that were created in the fork must also be deleted
batch
:=
bc
.
db
.
NewBatch
()
for
_
,
tx
:=
range
diff
{
rawdb
.
DeleteTxLookupEntry
(
b
c
.
db
,
tx
.
Hash
())
rawdb
.
DeleteTxLookupEntry
(
b
atch
,
tx
.
Hash
())
}
batch
.
Write
()
if
len
(
deletedLogs
)
>
0
{
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
// 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
)
{
// Delete any canonical number assignments above the new head
batch
:=
hc
.
chainDb
.
NewBatch
()
for
i
:=
number
+
1
;
;
i
++
{
hash
:=
rawdb
.
ReadCanonicalHash
(
hc
.
chainDb
,
i
)
if
hash
==
(
common
.
Hash
{})
{
break
}
rawdb
.
DeleteCanonicalHash
(
hc
.
chainDb
,
i
)
rawdb
.
DeleteCanonicalHash
(
batch
,
i
)
}
batch
.
Write
()
// Overwrite any stale canonical number assignments
var
(
headHash
=
header
.
ParentHash
...
...
@@ -438,7 +441,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
// DeleteCallback is a callback function that is called by SetHead before
// 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
// will be deleted and the new one set.
...
...
@@ -448,22 +451,24 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
if
hdr
:=
hc
.
CurrentHeader
();
hdr
!=
nil
{
height
=
hdr
.
Number
.
Uint64
()
}
batch
:=
hc
.
chainDb
.
NewBatch
()
for
hdr
:=
hc
.
CurrentHeader
();
hdr
!=
nil
&&
hdr
.
Number
.
Uint64
()
>
head
;
hdr
=
hc
.
CurrentHeader
()
{
hash
:=
hdr
.
Hash
()
num
:=
hdr
.
Number
.
Uint64
()
if
delFn
!=
nil
{
delFn
(
hash
,
num
)
delFn
(
batch
,
hash
,
num
)
}
rawdb
.
DeleteHeader
(
hc
.
chainDb
,
hash
,
num
)
rawdb
.
DeleteTd
(
hc
.
chainDb
,
hash
,
num
)
rawdb
.
DeleteHeader
(
batch
,
hash
,
num
)
rawdb
.
DeleteTd
(
batch
,
hash
,
num
)
hc
.
currentHeader
.
Store
(
hc
.
GetHeader
(
hdr
.
ParentHash
,
hdr
.
Number
.
Uint64
()
-
1
))
}
// Roll back the canonical chain numbering
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
hc
.
headerCache
.
Purge
()
hc
.
tdCache
.
Purge
()
...
...
ethdb/database.go
View file @
a4a2343c
...
...
@@ -388,6 +388,12 @@ func (b *ldbBatch) Put(key, value []byte) error {
return
nil
}
func
(
b
*
ldbBatch
)
Delete
(
key
[]
byte
)
error
{
b
.
b
.
Delete
(
key
)
b
.
size
+=
1
return
nil
}
func
(
b
*
ldbBatch
)
Write
()
error
{
return
b
.
db
.
Write
(
b
.
b
,
nil
)
}
...
...
@@ -453,6 +459,10 @@ func (tb *tableBatch) Put(key, value []byte) error {
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
{
return
tb
.
batch
.
Write
()
}
...
...
ethdb/interface.go
View file @
a4a2343c
...
...
@@ -25,12 +25,17 @@ type Putter interface {
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.
type
Database
interface
{
Putter
Deleter
Get
(
key
[]
byte
)
([]
byte
,
error
)
Has
(
key
[]
byte
)
(
bool
,
error
)
Delete
(
key
[]
byte
)
error
Close
()
NewBatch
()
Batch
}
...
...
@@ -39,6 +44,7 @@ type Database interface {
// when Write is called. Batch cannot be used concurrently.
type
Batch
interface
{
Putter
Deleter
ValueSize
()
int
// amount of data in the batch
Write
()
error
// 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 {
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
{
b
.
db
.
lock
.
Lock
()
defer
b
.
db
.
lock
.
Unlock
()
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
}
return
nil
...
...
light/txpool.go
View file @
a4a2343c
...
...
@@ -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
// as rolled back. It also removes any positional lookup entries.
func
(
pool
*
TxPool
)
rollbackTxs
(
hash
common
.
Hash
,
txc
txStateChanges
)
{
batch
:=
pool
.
chainDb
.
NewBatch
()
if
list
,
ok
:=
pool
.
mined
[
hash
];
ok
{
for
_
,
tx
:=
range
list
{
txHash
:=
tx
.
Hash
()
rawdb
.
DeleteTxLookupEntry
(
pool
.
chainDb
,
txHash
)
rawdb
.
DeleteTxLookupEntry
(
batch
,
txHash
)
pool
.
pending
[
txHash
]
=
tx
txc
.
setState
(
txHash
,
false
)
}
delete
(
pool
.
mined
,
hash
)
}
batch
.
Write
()
}
// 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
func
(
self
*
TxPool
)
RemoveTransactions
(
txs
types
.
Transactions
)
{
self
.
mu
.
Lock
()
defer
self
.
mu
.
Unlock
()
var
hashes
[]
common
.
Hash
batch
:=
self
.
chainDb
.
NewBatch
()
for
_
,
tx
:=
range
txs
{
//self.RemoveTx(tx.Hash())
hash
:=
tx
.
Hash
()
delete
(
self
.
pending
,
hash
)
self
.
chainDb
.
Delete
(
hash
[
:
]
)
batch
.
Delete
(
hash
.
Bytes
()
)
hashes
=
append
(
hashes
,
hash
)
}
batch
.
Write
()
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