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
0185ee09
Unverified
Commit
0185ee09
authored
Sep 15, 2020
by
Giuseppe Bertone
Committed by
GitHub
Sep 15, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/rawdb: single point of maintenance for writing and deleting tx lookup indexes (#21480)
parent
4764b2f0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
29 deletions
+31
-29
blockchain.go
core/blockchain.go
+4
-4
accessors_indexes.go
core/rawdb/accessors_indexes.go
+20
-18
accessors_indexes_test.go
core/rawdb/accessors_indexes_test.go
+4
-4
chain_iterator.go
core/rawdb/chain_iterator.go
+2
-2
txpool.go
light/txpool.go
+1
-1
No files found.
core/blockchain.go
View file @
0185ee09
...
@@ -712,7 +712,7 @@ func (bc *BlockChain) writeHeadBlock(block *types.Block) {
...
@@ -712,7 +712,7 @@ func (bc *BlockChain) writeHeadBlock(block *types.Block) {
// Add the block to the canonical chain number scheme and mark as the head
// Add the block to the canonical chain number scheme and mark as the head
batch
:=
bc
.
db
.
NewBatch
()
batch
:=
bc
.
db
.
NewBatch
()
rawdb
.
WriteCanonicalHash
(
batch
,
block
.
Hash
(),
block
.
NumberU64
())
rawdb
.
WriteCanonicalHash
(
batch
,
block
.
Hash
(),
block
.
NumberU64
())
rawdb
.
WriteTxLookupEntries
(
batch
,
block
)
rawdb
.
WriteTxLookupEntries
ByBlock
(
batch
,
block
)
rawdb
.
WriteHeadBlockHash
(
batch
,
block
.
Hash
())
rawdb
.
WriteHeadBlockHash
(
batch
,
block
.
Hash
())
// If the block is better than our head or is on a different chain, force update heads
// If the block is better than our head or is on a different chain, force update heads
...
@@ -1217,9 +1217,9 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
...
@@ -1217,9 +1217,9 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
// range. In this case, all tx indices of newly imported blocks should be
// range. In this case, all tx indices of newly imported blocks should be
// generated.
// generated.
if
bc
.
txLookupLimit
==
0
||
ancientLimit
<=
bc
.
txLookupLimit
||
block
.
NumberU64
()
>=
ancientLimit
-
bc
.
txLookupLimit
{
if
bc
.
txLookupLimit
==
0
||
ancientLimit
<=
bc
.
txLookupLimit
||
block
.
NumberU64
()
>=
ancientLimit
-
bc
.
txLookupLimit
{
rawdb
.
WriteTxLookupEntries
(
batch
,
block
)
rawdb
.
WriteTxLookupEntries
ByBlock
(
batch
,
block
)
}
else
if
rawdb
.
ReadTxIndexTail
(
bc
.
db
)
!=
nil
{
}
else
if
rawdb
.
ReadTxIndexTail
(
bc
.
db
)
!=
nil
{
rawdb
.
WriteTxLookupEntries
(
batch
,
block
)
rawdb
.
WriteTxLookupEntries
ByBlock
(
batch
,
block
)
}
}
stats
.
processed
++
stats
.
processed
++
}
}
...
@@ -1303,7 +1303,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
...
@@ -1303,7 +1303,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
// Write all the data out into the database
// Write all the data out into the database
rawdb
.
WriteBody
(
batch
,
block
.
Hash
(),
block
.
NumberU64
(),
block
.
Body
())
rawdb
.
WriteBody
(
batch
,
block
.
Hash
(),
block
.
NumberU64
(),
block
.
Body
())
rawdb
.
WriteReceipts
(
batch
,
block
.
Hash
(),
block
.
NumberU64
(),
receiptChain
[
i
])
rawdb
.
WriteReceipts
(
batch
,
block
.
Hash
(),
block
.
NumberU64
(),
receiptChain
[
i
])
rawdb
.
WriteTxLookupEntries
(
batch
,
block
)
// Always write tx indices for live blocks, we assume they are needed
rawdb
.
WriteTxLookupEntries
ByBlock
(
batch
,
block
)
// Always write tx indices for live blocks, we assume they are needed
// Write everything belongs to the blocks into the database. So that
// Write everything belongs to the blocks into the database. So that
// we can ensure all components of body is completed(body, receipts,
// we can ensure all components of body is completed(body, receipts,
...
...
core/rawdb/accessors_indexes.go
View file @
0185ee09
...
@@ -53,25 +53,29 @@ func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
...
@@ -53,25 +53,29 @@ func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
return
&
entry
.
BlockIndex
return
&
entry
.
BlockIndex
}
}
// WriteTxLookupEntries stores a positional metadata for every transaction from
// writeTxLookupEntry stores a positional metadata for a transaction,
// a block, enabling hash based transaction and receipt lookups.
// enabling hash based transaction and receipt lookups.
func
WriteTxLookupEntries
(
db
ethdb
.
KeyValueWriter
,
block
*
types
.
Block
)
{
func
writeTxLookupEntry
(
db
ethdb
.
KeyValueWriter
,
hash
common
.
Hash
,
numberBytes
[]
byte
)
{
number
:=
block
.
Number
()
.
Bytes
()
if
err
:=
db
.
Put
(
txLookupKey
(
hash
),
numberBytes
);
err
!=
nil
{
for
_
,
tx
:=
range
block
.
Transactions
()
{
if
err
:=
db
.
Put
(
txLookupKey
(
tx
.
Hash
()),
number
);
err
!=
nil
{
log
.
Crit
(
"Failed to store transaction lookup entry"
,
"err"
,
err
)
log
.
Crit
(
"Failed to store transaction lookup entry"
,
"err"
,
err
)
}
}
}
}
}
// WriteTxLookupEntries
ByHash is identical to WriteTxLookupEntries, but does not
// WriteTxLookupEntries
is identical to WriteTxLookupEntry, but it works on
//
require a full types.Block as input.
//
a list of hashes
func
WriteTxLookupEntries
ByHash
(
db
ethdb
.
KeyValueWriter
,
number
uint64
,
hashes
[]
common
.
Hash
)
{
func
WriteTxLookupEntries
(
db
ethdb
.
KeyValueWriter
,
number
uint64
,
hashes
[]
common
.
Hash
)
{
numberBytes
:=
new
(
big
.
Int
)
.
SetUint64
(
number
)
.
Bytes
()
numberBytes
:=
new
(
big
.
Int
)
.
SetUint64
(
number
)
.
Bytes
()
for
_
,
hash
:=
range
hashes
{
for
_
,
hash
:=
range
hashes
{
if
err
:=
db
.
Put
(
txLookupKey
(
hash
),
numberBytes
);
err
!=
nil
{
writeTxLookupEntry
(
db
,
hash
,
numberBytes
)
log
.
Crit
(
"Failed to store transaction lookup entry"
,
"err"
,
err
)
}
}
}
// WriteTxLookupEntriesByBlock stores a positional metadata for every transaction from
// a block, enabling hash based transaction and receipt lookups.
func
WriteTxLookupEntriesByBlock
(
db
ethdb
.
KeyValueWriter
,
block
*
types
.
Block
)
{
numberBytes
:=
block
.
Number
()
.
Bytes
()
for
_
,
tx
:=
range
block
.
Transactions
()
{
writeTxLookupEntry
(
db
,
tx
.
Hash
(),
numberBytes
)
}
}
}
}
...
@@ -83,11 +87,9 @@ func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
...
@@ -83,11 +87,9 @@ func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
}
}
// DeleteTxLookupEntries removes all transaction lookups for a given block.
// DeleteTxLookupEntries removes all transaction lookups for a given block.
func
DeleteTxLookupEntries
ByHash
(
db
ethdb
.
KeyValueWriter
,
hashes
[]
common
.
Hash
)
{
func
DeleteTxLookupEntries
(
db
ethdb
.
KeyValueWriter
,
hashes
[]
common
.
Hash
)
{
for
_
,
hash
:=
range
hashes
{
for
_
,
hash
:=
range
hashes
{
if
err
:=
db
.
Delete
(
txLookupKey
(
hash
));
err
!=
nil
{
DeleteTxLookupEntry
(
db
,
hash
)
log
.
Crit
(
"Failed to delete transaction lookup entry"
,
"err"
,
err
)
}
}
}
}
}
...
...
core/rawdb/accessors_indexes_test.go
View file @
0185ee09
...
@@ -58,12 +58,12 @@ func (h *testHasher) Hash() common.Hash {
...
@@ -58,12 +58,12 @@ func (h *testHasher) Hash() common.Hash {
func
TestLookupStorage
(
t
*
testing
.
T
)
{
func
TestLookupStorage
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
tests
:=
[]
struct
{
name
string
name
string
writeTxLookupEntries
func
(
ethdb
.
Writer
,
*
types
.
Block
)
writeTxLookupEntries
ByBlock
func
(
ethdb
.
Writer
,
*
types
.
Block
)
}{
}{
{
{
"DatabaseV6"
,
"DatabaseV6"
,
func
(
db
ethdb
.
Writer
,
block
*
types
.
Block
)
{
func
(
db
ethdb
.
Writer
,
block
*
types
.
Block
)
{
WriteTxLookupEntries
(
db
,
block
)
WriteTxLookupEntries
ByBlock
(
db
,
block
)
},
},
},
},
{
{
...
@@ -110,7 +110,7 @@ func TestLookupStorage(t *testing.T) {
...
@@ -110,7 +110,7 @@ func TestLookupStorage(t *testing.T) {
// Insert all the transactions into the database, and verify contents
// Insert all the transactions into the database, and verify contents
WriteCanonicalHash
(
db
,
block
.
Hash
(),
block
.
NumberU64
())
WriteCanonicalHash
(
db
,
block
.
Hash
(),
block
.
NumberU64
())
WriteBlock
(
db
,
block
)
WriteBlock
(
db
,
block
)
tc
.
writeTxLookupEntries
(
db
,
block
)
tc
.
writeTxLookupEntries
ByBlock
(
db
,
block
)
for
i
,
tx
:=
range
txs
{
for
i
,
tx
:=
range
txs
{
if
txn
,
hash
,
number
,
index
:=
ReadTransaction
(
db
,
tx
.
Hash
());
txn
==
nil
{
if
txn
,
hash
,
number
,
index
:=
ReadTransaction
(
db
,
tx
.
Hash
());
txn
==
nil
{
...
...
core/rawdb/chain_iterator.go
View file @
0185ee09
...
@@ -218,7 +218,7 @@ func IndexTransactions(db ethdb.Database, from uint64, to uint64) {
...
@@ -218,7 +218,7 @@ func IndexTransactions(db ethdb.Database, from uint64, to uint64) {
// Next block available, pop it off and index it
// Next block available, pop it off and index it
delivery
:=
queue
.
PopItem
()
.
(
*
blockTxHashes
)
delivery
:=
queue
.
PopItem
()
.
(
*
blockTxHashes
)
lastNum
=
delivery
.
number
lastNum
=
delivery
.
number
WriteTxLookupEntries
ByHash
(
batch
,
delivery
.
number
,
delivery
.
hashes
)
WriteTxLookupEntries
(
batch
,
delivery
.
number
,
delivery
.
hashes
)
blocks
++
blocks
++
txs
+=
len
(
delivery
.
hashes
)
txs
+=
len
(
delivery
.
hashes
)
// If enough data was accumulated in memory or we're at the last block, dump to disk
// If enough data was accumulated in memory or we're at the last block, dump to disk
...
@@ -276,7 +276,7 @@ func UnindexTransactions(db ethdb.Database, from uint64, to uint64) {
...
@@ -276,7 +276,7 @@ func UnindexTransactions(db ethdb.Database, from uint64, to uint64) {
// Otherwise spin up the concurrent iterator and unindexer
// Otherwise spin up the concurrent iterator and unindexer
blocks
,
txs
:=
0
,
0
blocks
,
txs
:=
0
,
0
for
delivery
:=
range
hashesCh
{
for
delivery
:=
range
hashesCh
{
DeleteTxLookupEntries
ByHash
(
batch
,
delivery
.
hashes
)
DeleteTxLookupEntries
(
batch
,
delivery
.
hashes
)
txs
+=
len
(
delivery
.
hashes
)
txs
+=
len
(
delivery
.
hashes
)
blocks
++
blocks
++
...
...
light/txpool.go
View file @
0185ee09
...
@@ -185,7 +185,7 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
...
@@ -185,7 +185,7 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
if
_
,
err
:=
GetBlockReceipts
(
ctx
,
pool
.
odr
,
hash
,
number
);
err
!=
nil
{
// ODR caches, ignore results
if
_
,
err
:=
GetBlockReceipts
(
ctx
,
pool
.
odr
,
hash
,
number
);
err
!=
nil
{
// ODR caches, ignore results
return
err
return
err
}
}
rawdb
.
WriteTxLookupEntries
(
pool
.
chainDb
,
block
)
rawdb
.
WriteTxLookupEntries
ByBlock
(
pool
.
chainDb
,
block
)
// Update the transaction pool's state
// Update the transaction pool's state
for
_
,
tx
:=
range
list
{
for
_
,
tx
:=
range
list
{
...
...
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