Skip to content

Commit a38be3e

Browse files
authored
Merge pull request bnb-chain#2455 from zsfelfoldi/chaindb
core: improved chain db performance by using sequential keys
2 parents 73c028c + f9917c8 commit a38be3e

24 files changed

Lines changed: 840 additions & 360 deletions

‎cmd/geth/chaincmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func dump(ctx *cli.Context) error {
174174
for _, arg := range ctx.Args() {
175175
var block *types.Block
176176
if hashish(arg) {
177-
block = chain.GetBlock(common.HexToHash(arg))
177+
block = chain.GetBlockByHash(common.HexToHash(arg))
178178
} else {
179179
num, _ := strconv.Atoi(arg)
180180
block = chain.GetBlockByNumber(uint64(num))

‎cmd/utils/flags.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func MustMakeChainConfig(ctx *cli.Context) *core.ChainConfig {
806806

807807
// MustMakeChainConfigFromDb reads the chain configuration from the given database.
808808
func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainConfig {
809-
genesis := core.GetBlock(db, core.GetCanonicalHash(db, 0))
809+
genesis := core.GetBlock(db, core.GetCanonicalHash(db, 0), 0)
810810

811811
if genesis != nil {
812812
// Existing genesis block, use stored config if available.

‎core/bench_test.go‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,122 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
176176
b.Fatalf("insert error (block %d): %v\n", i, err)
177177
}
178178
}
179+
180+
func BenchmarkChainRead_header_10k(b *testing.B) {
181+
benchReadChain(b, false, 10000)
182+
}
183+
func BenchmarkChainRead_full_10k(b *testing.B) {
184+
benchReadChain(b, true, 10000)
185+
}
186+
func BenchmarkChainRead_header_100k(b *testing.B) {
187+
benchReadChain(b, false, 100000)
188+
}
189+
func BenchmarkChainRead_full_100k(b *testing.B) {
190+
benchReadChain(b, true, 100000)
191+
}
192+
func BenchmarkChainRead_header_500k(b *testing.B) {
193+
benchReadChain(b, false, 500000)
194+
}
195+
func BenchmarkChainRead_full_500k(b *testing.B) {
196+
benchReadChain(b, true, 500000)
197+
}
198+
func BenchmarkChainWrite_header_10k(b *testing.B) {
199+
benchWriteChain(b, false, 10000)
200+
}
201+
func BenchmarkChainWrite_full_10k(b *testing.B) {
202+
benchWriteChain(b, true, 10000)
203+
}
204+
func BenchmarkChainWrite_header_100k(b *testing.B) {
205+
benchWriteChain(b, false, 100000)
206+
}
207+
func BenchmarkChainWrite_full_100k(b *testing.B) {
208+
benchWriteChain(b, true, 100000)
209+
}
210+
func BenchmarkChainWrite_header_500k(b *testing.B) {
211+
benchWriteChain(b, false, 500000)
212+
}
213+
func BenchmarkChainWrite_full_500k(b *testing.B) {
214+
benchWriteChain(b, true, 500000)
215+
}
216+
217+
// makeChainForBench writes a given number of headers or empty blocks/receipts
218+
// into a database.
219+
func makeChainForBench(db ethdb.Database, full bool, count uint64) {
220+
var hash common.Hash
221+
for n := uint64(0); n < count; n++ {
222+
header := &types.Header{
223+
Coinbase: common.Address{},
224+
Number: big.NewInt(int64(n)),
225+
ParentHash: hash,
226+
Difficulty: big.NewInt(1),
227+
UncleHash: types.EmptyUncleHash,
228+
TxHash: types.EmptyRootHash,
229+
ReceiptHash: types.EmptyRootHash,
230+
}
231+
hash = header.Hash()
232+
WriteHeader(db, header)
233+
WriteCanonicalHash(db, hash, n)
234+
WriteTd(db, hash, n, big.NewInt(int64(n+1)))
235+
if full || n == 0 {
236+
block := types.NewBlockWithHeader(header)
237+
WriteBody(db, hash, n, block.Body())
238+
WriteBlockReceipts(db, hash, n, nil)
239+
}
240+
}
241+
}
242+
243+
func benchWriteChain(b *testing.B, full bool, count uint64) {
244+
for i := 0; i < b.N; i++ {
245+
dir, err := ioutil.TempDir("", "eth-chain-bench")
246+
if err != nil {
247+
b.Fatalf("cannot create temporary directory: %v", err)
248+
}
249+
db, err := ethdb.NewLDBDatabase(dir, 128, 1024)
250+
if err != nil {
251+
b.Fatalf("error opening database at %v: %v", dir, err)
252+
}
253+
makeChainForBench(db, full, count)
254+
db.Close()
255+
os.RemoveAll(dir)
256+
}
257+
}
258+
259+
func benchReadChain(b *testing.B, full bool, count uint64) {
260+
dir, err := ioutil.TempDir("", "eth-chain-bench")
261+
if err != nil {
262+
b.Fatalf("cannot create temporary directory: %v", err)
263+
}
264+
defer os.RemoveAll(dir)
265+
266+
db, err := ethdb.NewLDBDatabase(dir, 128, 1024)
267+
if err != nil {
268+
b.Fatalf("error opening database at %v: %v", dir, err)
269+
}
270+
makeChainForBench(db, full, count)
271+
db.Close()
272+
273+
b.ReportAllocs()
274+
b.ResetTimer()
275+
276+
for i := 0; i < b.N; i++ {
277+
db, err := ethdb.NewLDBDatabase(dir, 128, 1024)
278+
if err != nil {
279+
b.Fatalf("error opening database at %v: %v", dir, err)
280+
}
281+
chain, err := NewBlockChain(db, testChainConfig(), FakePow{}, new(event.TypeMux))
282+
if err != nil {
283+
b.Fatalf("error creating chain: %v", err)
284+
}
285+
286+
for n := uint64(0); n < count; n++ {
287+
header := chain.GetHeaderByNumber(n)
288+
if full {
289+
hash := header.Hash()
290+
GetBody(db, hash, n)
291+
GetBlockReceipts(db, hash, n)
292+
}
293+
}
294+
295+
db.Close()
296+
}
297+
}

‎core/block_validator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (v *BlockValidator) ValidateBlock(block *types.Block) error {
7272
return &KnownBlockError{block.Number(), block.Hash()}
7373
}
7474
}
75-
parent := v.bc.GetBlock(block.ParentHash())
75+
parent := v.bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
7676
if parent == nil {
7777
return ParentError(block.ParentHash())
7878
}

0 commit comments

Comments
 (0)