Skip to content

Commit

Permalink
kv_interface: reduce amount of types (#12326)
Browse files Browse the repository at this point in the history
- merge `Has` into `Getter`
- merge `Deleter` into `Putter`
- remove `Commit` from `Tx`

will continue this work in future PR's
  • Loading branch information
AskAlexSharov authored Oct 19, 2024
1 parent 617a3de commit b27017f
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 147 deletions.
4 changes: 2 additions & 2 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func WriteHeaderRaw(db kv.StatelessRwTx, number uint64, hash common.Hash, header
}

// DeleteHeader - dangerous, use PruneBlocks/TruncateBlocks methods
func DeleteHeader(db kv.Deleter, hash common.Hash, number uint64) {
func DeleteHeader(db kv.Putter, hash common.Hash, number uint64) {
if err := db.Delete(kv.Headers, dbutils.HeaderKey(number, hash)); err != nil {
log.Crit("Failed to delete header", "err", err)
}
Expand Down Expand Up @@ -662,7 +662,7 @@ func WriteSenders(db kv.Putter, hash common.Hash, number uint64, senders []commo
}

// DeleteBody removes all block body data associated with a hash.
func DeleteBody(db kv.Deleter, hash common.Hash, number uint64) {
func DeleteBody(db kv.Putter, hash common.Hash, number uint64) {
if err := db.Delete(kv.BlockBody, dbutils.BlockBodyKey(number, hash)); err != nil {
log.Crit("Failed to delete block body", "err", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ func WriteTxLookupEntries(db kv.Putter, block *types.Block) {
}

// DeleteTxLookupEntry removes all transaction data associated with a hash.
func DeleteTxLookupEntry(db kv.Deleter, hash libcommon.Hash) error {
func DeleteTxLookupEntry(db kv.Putter, hash libcommon.Hash) error {
return db.Delete(kv.TxLookup, hash.Bytes())
}
2 changes: 1 addition & 1 deletion core/rawdb/bor_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// HasBorReceipts verifies the existence of all block receipt belonging to a block.
func HasBorReceipts(db kv.Has, number uint64) bool {
func HasBorReceipts(db kv.Getter, number uint64) bool {
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, number)
if has, err := db.Has(kv.BorEventNums, k); !has || err != nil {
Expand Down
27 changes: 0 additions & 27 deletions core/state/plain_state_writer.go

This file was deleted.

3 changes: 0 additions & 3 deletions core/types/transaction_marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,6 @@ func UnmarshalBlobTxJSON(input []byte) (Transaction, error) {
return nil, errors.New("missing required field 'nonce' in transaction")
}
tx.Nonce = uint64(*dec.Nonce)
// if dec.GasPrice == nil { // do we need gasPrice here?
// return nil, errors.New("missing required field 'gasPrice' in transaction")
// }
tx.Tip, overflow = uint256.FromBig(dec.Tip.ToInt())
if overflow {
return nil, errors.New("'tip' in transaction does not fit in 256 bits")
Expand Down
163 changes: 69 additions & 94 deletions erigon-lib/kv/kv_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,40 +209,70 @@ func UnmarshalLabel(s string) Label {
}
}

type Has interface {
// Has indicates whether a key exists in the database.
Has(table string, key []byte) (bool, error)
}
type GetPut interface {
Getter
Putter
}
type Getter interface {
Has
// Has indicates whether a key exists in the database.
Has(table string, key []byte) (bool, error)

// GetOne references a readonly section of memory that must not be accessed after txn has terminated
GetOne(table string, key []byte) (val []byte, err error)

Rollback() // Rollback - abandon all the operations of the transaction instead of saving them.

// ReadSequence - allows to create a linear sequence of unique positive integers for each table (AutoIncrement).
// Can be called for a read transaction to retrieve the current sequence value, and the increment must be zero.
// Sequence changes become visible outside the current write transaction after it is committed, and discarded on abort.
// Starts from 0.
ReadSequence(table string) (uint64, error)

// --- High-Level deprecated methods ---

// ForEach iterates over entries with keys greater or equal to fromPrefix.
// walker is called for each eligible entry.
// If walker returns an error:
// - implementations of local db - stop
// - implementations of remote db - do not handle this error and may finish (send all entries to client) before error happen.
ForEach(table string, fromPrefix []byte, walker func(k, v []byte) error) error

ForAmount(table string, prefix []byte, amount uint32, walker func(k, v []byte) error) error
}

// Putter wraps the database write operations.
type Putter interface {
// Put inserts or updates a single entry.
Put(table string, k, v []byte) error
}

// Deleter wraps the database delete operations.
type Deleter interface {
// Delete removes a single entry.
Delete(table string, k []byte) error

/*
// if need N id's:
baseId, err := tx.IncrementSequence(bucket, N)
if err != nil {
return err
}
for i := 0; i < N; i++ { // if N == 0, it will work as expected
id := baseId + i
// use id
}
// or if need only 1 id:
id, err := tx.IncrementSequence(bucket, 1)
if err != nil {
return err
}
// use id
*/
IncrementSequence(table string, amount uint64) (uint64, error)
Append(table string, k, v []byte) error
AppendDup(table string, k, v []byte) error

// CollectMetrics - does collect all DB-related and Tx-related metrics
// this method exists only in RwTx to avoid concurrency
CollectMetrics()
}

type Closer interface {
Expand Down Expand Up @@ -309,73 +339,18 @@ type RwDB interface {
BeginRw(ctx context.Context) (RwTx, error)
BeginRwNosync(ctx context.Context) (RwTx, error)
}
type HasRwKV interface {
RwKV() RwDB
}

type StatelessReadTx interface {
type StatelessRwTx interface {
Getter

Commit() error // Commit all the operations of a transaction into the database.
Rollback() // Rollback - abandon all the operations of the transaction instead of saving them.

// ReadSequence - allows to create a linear sequence of unique positive integers for each table (AutoIncrement).
// Can be called for a read transaction to retrieve the current sequence value, and the increment must be zero.
// Sequence changes become visible outside the current write transaction after it is committed, and discarded on abort.
// Starts from 0.
ReadSequence(table string) (uint64, error)
}

type StatelessWriteTx interface {
Putter
Deleter

/*
// if need N id's:
baseId, err := tx.IncrementSequence(bucket, N)
if err != nil {
return err
}
for i := 0; i < N; i++ { // if N == 0, it will work as expected
id := baseId + i
// use id
}
// or if need only 1 id:
id, err := tx.IncrementSequence(bucket, 1)
if err != nil {
return err
}
// use id
*/
IncrementSequence(table string, amount uint64) (uint64, error)
Append(table string, k, v []byte) error
AppendDup(table string, k, v []byte) error
}

type StatelessRwTx interface {
StatelessReadTx
StatelessWriteTx
}

// PendingMutations in-memory storage of changes
// Later they can either be flushed to the database or abandon
type PendingMutations interface {
StatelessRwTx
// Flush all in-memory data into `tx`
Flush(ctx context.Context, tx RwTx) error
Close()
BatchSize() int
}

// Tx
// WARNING:
// - Tx is not threadsafe and may only be used in the goroutine that created it
// - ReadOnly transactions do not lock goroutine to thread, RwTx does
type Tx interface {
StatelessReadTx
BucketMigratorRO
Getter

// ID returns the identifier associated with this transaction. For a
// read-only transaction, this corresponds to the snapshot being read;
Expand Down Expand Up @@ -418,15 +393,12 @@ type Tx interface {
// --- High-Level methods: 1request -> 1page of values in response -> send next page request ---
// Paginate(table string, fromPrefix, toPrefix []byte) (PairsStream, error)

// --- High-Level deprecated methods ---

ForEach(table string, fromPrefix []byte, walker func(k, v []byte) error) error
ForAmount(table string, prefix []byte, amount uint32, walker func(k, v []byte) error) error

// Pointer to the underlying C transaction handle (e.g. *C.MDBX_txn)
CHandle() unsafe.Pointer
BucketSize(table string) (uint64, error)
Count(bucket string) (uint64, error)

ListBuckets() ([]string, error)
}

// RwTx
Expand All @@ -437,28 +409,13 @@ type Tx interface {
// - User Can't call runtime.LockOSThread/runtime.UnlockOSThread in same goroutine until RwTx Commit/Rollback
type RwTx interface {
Tx
StatelessWriteTx
Putter
BucketMigrator

RwCursor(table string) (RwCursor, error)
RwCursorDupSort(table string) (RwCursorDupSort, error)

// CollectMetrics - does collect all DB-related and Tx-related metrics
// this method exists only in RwTx to avoid concurrency
CollectMetrics()
}

type BucketMigratorRO interface {
ListBuckets() ([]string, error)
}

// BucketMigrator used for buckets migration, don't use it in usual app code
type BucketMigrator interface {
BucketMigratorRO
DropBucket(string) error
CreateBucket(string) error
ExistsBucket(string) (bool, error)
ClearBucket(string) error
Commit() error // Commit all the operations of a transaction into the database.
}

// Cursor - class for navigating through a database
Expand Down Expand Up @@ -586,12 +543,6 @@ type TemporalTx interface {
HistoryRange(name History, fromTs, toTs int, asc order.By, limit int) (it stream.KV, err error)
}

type TxnId uint64 // internal auto-increment ID. can't cast to eth-network canonical blocks txNum

type TemporalCommitment interface {
ComputeCommitment(ctx context.Context, saveStateAfter, trace bool) (rootHash []byte, err error)
}

type TemporalRwTx interface {
RwTx
TemporalTx
Expand All @@ -613,10 +564,34 @@ type TemporalPutDel interface {
DomainDel(domain Domain, k1, k2 []byte, prevVal []byte, prevStep uint64) error
DomainDelPrefix(domain Domain, prefix []byte) error
}

// ---- non-importnt utilites

type TxnId uint64 // internal auto-increment ID. can't cast to eth-network canonical blocks txNum

type CanWarmupDB interface {
WarmupDB(force bool) error
LockDBInRam() error
}
type HasSpaceDirty interface {
SpaceDirty() (uint64, uint64, error)
}

// BucketMigrator used for buckets migration, don't use it in usual app code
type BucketMigrator interface {
ListBuckets() ([]string, error)
DropBucket(string) error
CreateBucket(string) error
ExistsBucket(string) (bool, error)
ClearBucket(string) error
}

// PendingMutations in-memory storage of changes
// Later they can either be flushed to the database or abandon
type PendingMutations interface {
Putter
// Flush all in-memory data into `tx`
Flush(ctx context.Context, tx RwTx) error
Close()
BatchSize() int
}
6 changes: 3 additions & 3 deletions erigon-lib/kv/mdbx/kv_mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func (db *MdbxKV) openDBIs(buckets []string) error {
return err
}
}
return tx.Commit() // when open db as read-only, commit of this RO transaction is required
return tx.(*MdbxTx).Commit() // when open db as read-only, commit of this RO transaction is required
})
}

Expand Down Expand Up @@ -951,7 +951,7 @@ func (db *MdbxKV) UpdateNosync(ctx context.Context, f func(tx kv.RwTx) error) (e
if err != nil {
return err
}
err = tx.Commit()
err = tx.(*MdbxTx).Commit()
if err != nil {
return err
}
Expand All @@ -968,7 +968,7 @@ func (db *MdbxKV) Update(ctx context.Context, f func(tx kv.RwTx) error) (err err
if err != nil {
return err
}
err = tx.Commit()
err = tx.(*MdbxTx).Commit()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit b27017f

Please sign in to comment.