From f07824db20927974856a841b1ebdc75718a43346 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Tue, 17 Jun 2025 13:26:50 +0800 Subject: [PATCH] core, ethdb, tests, trie: implement NewBatchWithSize API for batcher #24392 (#1085) --- XDCxDAO/interfaces.go | 1 + XDCxDAO/leveldb.go | 4 ++++ XDCxDAO/mongodb.go | 4 ++++ core/rawdb/table.go | 5 +++++ ethdb/batch.go | 3 +++ ethdb/leveldb/leveldb.go | 8 ++++++++ ethdb/memorydb/memorydb.go | 7 +++++++ tests/fuzzers/bn256/bn256_fuzz.go | 3 +++ trie/iterator_test.go | 4 ++++ trie/trie_test.go | 1 + 10 files changed, 40 insertions(+) diff --git a/XDCxDAO/interfaces.go b/XDCxDAO/interfaces.go index 066df40b0a..7fc88b78cd 100644 --- a/XDCxDAO/interfaces.go +++ b/XDCxDAO/interfaces.go @@ -39,6 +39,7 @@ type XDCXDAO interface { Has(key []byte) (bool, error) Delete(key []byte) error NewBatch() ethdb.Batch + NewBatchWithSize(size int) ethdb.Batch HasAncient(kind string, number uint64) (bool, error) Ancient(kind string, number uint64) ([]byte, error) Ancients() (uint64, error) diff --git a/XDCxDAO/leveldb.go b/XDCxDAO/leveldb.go index 504fc1f501..d46f24d080 100644 --- a/XDCxDAO/leveldb.go +++ b/XDCxDAO/leveldb.go @@ -103,6 +103,10 @@ func (db *BatchDatabase) NewBatch() ethdb.Batch { return db.db.NewBatch() } +func (db *BatchDatabase) NewBatchWithSize(size int) ethdb.Batch { + return db.db.NewBatch() +} + func (db *BatchDatabase) DeleteItemByTxHash(txhash common.Hash, val interface{}) { } diff --git a/XDCxDAO/mongodb.go b/XDCxDAO/mongodb.go index 8c8e45052c..59cfb8c217 100644 --- a/XDCxDAO/mongodb.go +++ b/XDCxDAO/mongodb.go @@ -882,6 +882,10 @@ func (db *MongoDatabase) NewBatch() ethdb.Batch { return nil } +func (db *MongoDatabase) NewBatchWithSize(size int) ethdb.Batch { + return nil +} + type keyvalue struct { key []byte value []byte diff --git a/core/rawdb/table.go b/core/rawdb/table.go index dfb58c07b6..0da0b732ff 100644 --- a/core/rawdb/table.go +++ b/core/rawdb/table.go @@ -159,6 +159,11 @@ func (t *table) NewBatch() ethdb.Batch { return &tableBatch{t.db.NewBatch(), t.prefix} } +// NewBatchWithSize creates a write-only database batch with pre-allocated buffer. +func (t *table) NewBatchWithSize(size int) ethdb.Batch { + return &tableBatch{t.db.NewBatchWithSize(size), t.prefix} +} + // tableBatch is a wrapper around a database batch that prefixes each key access // with a pre-configured string. type tableBatch struct { diff --git a/ethdb/batch.go b/ethdb/batch.go index e261415bff..c1c0c9abfe 100644 --- a/ethdb/batch.go +++ b/ethdb/batch.go @@ -43,4 +43,7 @@ type Batcher interface { // NewBatch creates a write-only database that buffers changes to its host db // until a final write is called. NewBatch() Batch + + // NewBatchWithSize creates a write-only database batch with pre-allocated buffer. + NewBatchWithSize(size int) Batch } diff --git a/ethdb/leveldb/leveldb.go b/ethdb/leveldb/leveldb.go index 10f7103de2..be2266a985 100644 --- a/ethdb/leveldb/leveldb.go +++ b/ethdb/leveldb/leveldb.go @@ -213,6 +213,14 @@ func (db *Database) NewBatch() ethdb.Batch { } } +// NewBatchWithSize creates a write-only database batch with pre-allocated buffer. +func (db *Database) NewBatchWithSize(size int) ethdb.Batch { + return &batch{ + db: db.db, + b: leveldb.MakeBatch(size), + } +} + // NewIterator creates a binary-alphabetical iterator over a subset // of database content with a particular key prefix, starting at a particular // initial key (or after, if it does not exist). diff --git a/ethdb/memorydb/memorydb.go b/ethdb/memorydb/memorydb.go index 3830d872b2..1704d648eb 100644 --- a/ethdb/memorydb/memorydb.go +++ b/ethdb/memorydb/memorydb.go @@ -129,6 +129,13 @@ func (db *Database) NewBatch() ethdb.Batch { } } +// NewBatchWithSize creates a write-only database batch with pre-allocated buffer. +func (db *Database) NewBatchWithSize(size int) ethdb.Batch { + return &batch{ + db: db, + } +} + // NewIterator creates a binary-alphabetical iterator over a subset // of database content with a particular key prefix, starting at a particular // initial key (or after, if it does not exist). diff --git a/tests/fuzzers/bn256/bn256_fuzz.go b/tests/fuzzers/bn256/bn256_fuzz.go index 02ec37138d..b96e3bb8cc 100644 --- a/tests/fuzzers/bn256/bn256_fuzz.go +++ b/tests/fuzzers/bn256/bn256_fuzz.go @@ -14,6 +14,9 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . +//go:build gofuzz +// +build gofuzz + package bn256 import ( diff --git a/trie/iterator_test.go b/trie/iterator_test.go index 03636bdc7f..ca44da74d3 100644 --- a/trie/iterator_test.go +++ b/trie/iterator_test.go @@ -471,6 +471,10 @@ func (l *loggingDb) NewBatch() ethdb.Batch { return l.backend.NewBatch() } +func (l *loggingDb) NewBatchWithSize(size int) ethdb.Batch { + return l.backend.NewBatchWithSize(size) +} + func (l *loggingDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { fmt.Printf("NewIterator\n") return l.backend.NewIterator(prefix, start) diff --git a/trie/trie_test.go b/trie/trie_test.go index 6e22cb8b1d..2a0a3bba71 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -589,6 +589,7 @@ func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement func (s *spongeDb) Get(key []byte) ([]byte, error) { return nil, errors.New("no such elem") } func (s *spongeDb) Delete(key []byte) error { panic("implement me") } func (s *spongeDb) NewBatch() ethdb.Batch { return &spongeBatch{s} } +func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch { return &spongeBatch{s} } func (s *spongeDb) Stat(property string) (string, error) { panic("implement me") } func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") } func (s *spongeDb) Close() error { return nil }