From 3a52b733d81c146fdaf2a93dd98168f994c8f68d Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Sat, 3 Apr 2021 09:26:00 +0300 Subject: [PATCH] Harmonize DB APIs (#1669) --- cmd/evm/runner.go | 2 +- cmd/hack/db/lmdb.go | 2 +- cmd/integration/commands/refetence_db.go | 12 +++--- cmd/rpcdaemon/commands/debug_api.go | 10 ++--- cmd/rpcdaemon/commands/eth_accounts.go | 8 ++-- cmd/rpcdaemon/commands/eth_block.go | 8 ++-- cmd/rpcdaemon/commands/eth_call.go | 4 +- cmd/rpcdaemon/commands/eth_receipts.go | 4 +- cmd/rpcdaemon/commands/eth_system.go | 12 +++--- cmd/rpcdaemon/commands/eth_txs.go | 6 +-- cmd/rpcdaemon/commands/eth_uncles.go | 8 ++-- .../commands/get_chain_config_test.go | 2 +- cmd/rpcdaemon/commands/tg_block.go | 4 +- cmd/rpcdaemon/commands/tg_issuance.go | 2 +- cmd/rpcdaemon/commands/tg_receipts.go | 2 +- cmd/rpcdaemon/commands/tg_system.go | 2 +- cmd/rpcdaemon/commands/trace_adhoc.go | 4 +- cmd/rpcdaemon/commands/trace_filtering.go | 8 ++-- cmd/rpcdaemon/commands/tracing.go | 4 +- cmd/rpctest/rpctest/account_range_verify.go | 4 +- .../commands/generate_state_snapshot.go | 4 +- cmd/state/verify/check_indexes.go | 2 +- core/state/history_test.go | 8 ++-- core/state/plain_readonly.go | 10 ++--- core/state/plain_state_writer.go | 2 +- core/state/state_test.go | 4 +- ethdb/database_test.go | 4 +- ethdb/interface.go | 22 ++++------ ethdb/kv_abstract.go | 40 ++++++++++++------- ethdb/kv_abstract_test.go | 2 +- ethdb/kv_lmdb.go | 12 +++--- ethdb/kv_mdbx.go | 12 +++--- ethdb/kv_migrator_test.go | 2 +- ethdb/kv_remote.go | 6 +-- ethdb/kv_snapshot.go | 14 +++---- ethdb/kv_snapshot_test.go | 6 +-- ethdb/mutation.go | 4 +- ethdb/object_db.go | 2 +- ethdb/remote/remotedbserver/server.go | 4 +- ethdb/tx_db.go | 8 ++-- turbo/trie/flatdb_sub_trie_loader.go | 2 +- 41 files changed, 140 insertions(+), 138 deletions(-) diff --git a/cmd/evm/runner.go b/cmd/evm/runner.go index e3be88fcc74..f3bb7aa555f 100644 --- a/cmd/evm/runner.go +++ b/cmd/evm/runner.go @@ -281,7 +281,7 @@ func runCmd(ctx *cli.Context) error { fmt.Println("Could not commit state: ", err) os.Exit(1) } - tx, err1 := db.RwKV().Begin(context.Background()) + tx, err1 := db.RwKV().BeginRo(context.Background()) if err1 != nil { return fmt.Errorf("transition cannot open tx: %v", err1) } diff --git a/cmd/hack/db/lmdb.go b/cmd/hack/db/lmdb.go index c350042d20c..43e5fc27648 100644 --- a/cmd/hack/db/lmdb.go +++ b/cmd/hack/db/lmdb.go @@ -284,7 +284,7 @@ func change3(tx ethdb.RwTx) (bool, error) { func launchReader(kv ethdb.RwKV, tx ethdb.Tx, expectVal string, startCh chan struct{}, errorCh chan error) (bool, error) { tx.Rollback() - tx1, err1 := kv.Begin(context.Background()) + tx1, err1 := kv.BeginRo(context.Background()) if err1 != nil { return false, err1 } diff --git a/cmd/integration/commands/refetence_db.go b/cmd/integration/commands/refetence_db.go index f63a25d69fb..461cd1e9c96 100644 --- a/cmd/integration/commands/refetence_db.go +++ b/cmd/integration/commands/refetence_db.go @@ -385,7 +385,7 @@ MainLoop: panic(err) } } - err = dstTx.Commit(context.Background()) + err = dstTx.Commit() if err != nil { return err } @@ -393,7 +393,7 @@ MainLoop: if err != nil { return err } - err = dstTx.Commit(ctx) + err = dstTx.Commit() if err != nil { return err } @@ -423,7 +423,7 @@ func mdbxToMdbx(ctx context.Context, from, to string) error { } func kv2kv(ctx context.Context, src, dst ethdb.RwKV) error { - srcTx, err1 := src.Begin(ctx) + srcTx, err1 := src.BeginRo(ctx) if err1 != nil { return err1 } @@ -482,7 +482,7 @@ func kv2kv(ctx context.Context, src, dst ethdb.RwKV) error { return ctx.Err() case <-commitEvery.C: log.Info("Progress", "bucket", name, "key", fmt.Sprintf("%x", k)) - if err2 := dstTx.Commit(ctx); err2 != nil { + if err2 := dstTx.Commit(); err2 != nil { return err2 } dstTx, err = dst.BeginRw(ctx) @@ -509,7 +509,7 @@ func kv2kv(ctx context.Context, src, dst ethdb.RwKV) error { // return err //} } - err := dstTx.Commit(context.Background()) + err := dstTx.Commit() if err != nil { return err } @@ -517,7 +517,7 @@ func kv2kv(ctx context.Context, src, dst ethdb.RwKV) error { if err != nil { return err } - err = dstTx.Commit(ctx) + err = dstTx.Commit() if err != nil { return err } diff --git a/cmd/rpcdaemon/commands/debug_api.go b/cmd/rpcdaemon/commands/debug_api.go index 7b6eb1ada25..974aedbcff8 100644 --- a/cmd/rpcdaemon/commands/debug_api.go +++ b/cmd/rpcdaemon/commands/debug_api.go @@ -51,7 +51,7 @@ func NewPrivateDebugAPI(dbReader ethdb.RoKV, gascap uint64, pending *rpchelper.P // StorageRangeAt implements debug_storageRangeAt. Returns information about a range of storage locations (if any) for the given address. func (api *PrivateDebugAPIImpl) StorageRangeAt(ctx context.Context, blockHash common.Hash, txIndex uint64, contractAddress common.Address, keyStart hexutil.Bytes, maxResult int) (StorageRangeResult, error) { - tx, err := api.dbReader.Begin(ctx) + tx, err := api.dbReader.BeginRo(ctx) if err != nil { return StorageRangeResult{}, err } @@ -77,7 +77,7 @@ func (api *PrivateDebugAPIImpl) StorageRangeAt(ctx context.Context, blockHash co // AccountRange implements debug_accountRange. Returns a range of accounts involved in the given block range func (api *PrivateDebugAPIImpl) AccountRange(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, startKey []byte, maxResults int, excludeCode, excludeStorage, excludeMissingPreimages bool) (state.IteratorDump, error) { - tx, err := api.dbReader.Begin(ctx) + tx, err := api.dbReader.BeginRo(ctx) if err != nil { return state.IteratorDump{}, err } @@ -137,7 +137,7 @@ func (api *PrivateDebugAPIImpl) AccountRange(ctx context.Context, blockNrOrHash // GetModifiedAccountsByNumber implements debug_getModifiedAccountsByNumber. Returns a list of accounts modified in the given block. func (api *PrivateDebugAPIImpl) GetModifiedAccountsByNumber(ctx context.Context, startNumber rpc.BlockNumber, endNumber *rpc.BlockNumber) ([]common.Address, error) { - tx, err := api.dbReader.Begin(ctx) + tx, err := api.dbReader.BeginRo(ctx) if err != nil { return nil, err } @@ -174,7 +174,7 @@ func (api *PrivateDebugAPIImpl) GetModifiedAccountsByNumber(ctx context.Context, // GetModifiedAccountsByHash implements debug_getModifiedAccountsByHash. Returns a list of accounts modified in the given block. func (api *PrivateDebugAPIImpl) GetModifiedAccountsByHash(ctx context.Context, startHash common.Hash, endHash *common.Hash) ([]common.Address, error) { - tx, err := api.dbReader.Begin(ctx) + tx, err := api.dbReader.BeginRo(ctx) if err != nil { return nil, err } @@ -209,7 +209,7 @@ func (api *PrivateDebugAPIImpl) GetModifiedAccountsByHash(ctx context.Context, s } func (api *PrivateDebugAPIImpl) AccountAt(ctx context.Context, blockHash common.Hash, txIndex uint64, address common.Address) (*AccountResult, error) { - tx, err := api.dbReader.Begin(ctx) + tx, err := api.dbReader.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/eth_accounts.go b/cmd/rpcdaemon/commands/eth_accounts.go index 5da28a8f680..f2c3fb58e06 100644 --- a/cmd/rpcdaemon/commands/eth_accounts.go +++ b/cmd/rpcdaemon/commands/eth_accounts.go @@ -15,7 +15,7 @@ import ( // GetBalance implements eth_getBalance. Returns the balance of an account for a given address. func (api *APIImpl) GetBalance(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) { - tx, err1 := api.db.Begin(ctx) + tx, err1 := api.db.BeginRo(ctx) if err1 != nil { return nil, fmt.Errorf("getBalance cannot open tx: %v", err1) } @@ -39,7 +39,7 @@ func (api *APIImpl) GetBalance(ctx context.Context, address common.Address, bloc // GetTransactionCount implements eth_getTransactionCount. Returns the number of transactions sent from an address (the nonce). func (api *APIImpl) GetTransactionCount(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Uint64, error) { - tx, err1 := api.db.Begin(ctx) + tx, err1 := api.db.BeginRo(ctx) if err1 != nil { return nil, fmt.Errorf("getTransactionCount cannot open tx: %v", err1) } @@ -59,7 +59,7 @@ func (api *APIImpl) GetTransactionCount(ctx context.Context, address common.Addr // GetCode implements eth_getCode. Returns the byte code at a given address (if it's a smart contract). func (api *APIImpl) GetCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) { - tx, err1 := api.db.Begin(ctx) + tx, err1 := api.db.BeginRo(ctx) if err1 != nil { return nil, fmt.Errorf("getCode cannot open tx: %v", err1) } @@ -85,7 +85,7 @@ func (api *APIImpl) GetCode(ctx context.Context, address common.Address, blockNr func (api *APIImpl) GetStorageAt(ctx context.Context, address common.Address, index string, blockNrOrHash rpc.BlockNumberOrHash) (string, error) { var empty []byte - tx, err1 := api.db.Begin(ctx) + tx, err1 := api.db.BeginRo(ctx) if err1 != nil { return hexutil.Encode(common.LeftPadBytes(empty[:], 32)), err1 } diff --git a/cmd/rpcdaemon/commands/eth_block.go b/cmd/rpcdaemon/commands/eth_block.go index 41c50282166..ee7664de8d5 100644 --- a/cmd/rpcdaemon/commands/eth_block.go +++ b/cmd/rpcdaemon/commands/eth_block.go @@ -14,7 +14,7 @@ import ( // GetBlockByNumber implements eth_getBlockByNumber. Returns information about a block given the block's number. func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -63,7 +63,7 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, numberOrHash rpc.BlockNu } hash := *numberOrHash.BlockHash - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -98,7 +98,7 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, numberOrHash rpc.BlockNu // GetBlockTransactionCountByNumber implements eth_getBlockTransactionCountByNumber. Returns the number of transactions in a block given the block's block number. func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -122,7 +122,7 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN // GetBlockTransactionCountByHash implements eth_getBlockTransactionCountByHash. Returns the number of transactions in a block given the block's block hash. func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) (*hexutil.Uint, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/eth_call.go b/cmd/rpcdaemon/commands/eth_call.go index 1e150ac5ec4..061bef46e84 100644 --- a/cmd/rpcdaemon/commands/eth_call.go +++ b/cmd/rpcdaemon/commands/eth_call.go @@ -23,7 +23,7 @@ import ( // Call implements eth_call. Executes a new message call immediately without creating a transaction on the block chain. func (api *APIImpl) Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]ethapi.Account) (hexutil.Bytes, error) { - dbtx, err := api.db.Begin(ctx) + dbtx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -91,7 +91,7 @@ func (api *APIImpl) EstimateGas(ctx context.Context, args ethapi.CallArgs, block bNrOrHash = *blockNrOrHash } - dbtx, err := api.db.Begin(ctx) + dbtx, err := api.db.BeginRo(ctx) if err != nil { return 0, err } diff --git a/cmd/rpcdaemon/commands/eth_receipts.go b/cmd/rpcdaemon/commands/eth_receipts.go index 9ae40b95e02..812516485fb 100644 --- a/cmd/rpcdaemon/commands/eth_receipts.go +++ b/cmd/rpcdaemon/commands/eth_receipts.go @@ -57,7 +57,7 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) ([ var begin, end uint64 var logs []*types.Log //nolint:prealloc - tx, beginErr := api.db.Begin(ctx) + tx, beginErr := api.db.BeginRo(ctx) if beginErr != nil { return returnLogs(logs), beginErr } @@ -194,7 +194,7 @@ func getTopicsBitmap(c ethdb.Tx, topics [][]common.Hash, from, to uint32) (*roar // GetTransactionReceipt implements eth_getTransactionReceipt. Returns the receipt of a transaction given the transaction's hash. func (api *APIImpl) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/eth_system.go b/cmd/rpcdaemon/commands/eth_system.go index a4ddf44e5cd..f2f6354e2d6 100644 --- a/cmd/rpcdaemon/commands/eth_system.go +++ b/cmd/rpcdaemon/commands/eth_system.go @@ -19,7 +19,7 @@ import ( // BlockNumber implements eth_blockNumber. Returns the block number of most recent block. func (api *APIImpl) BlockNumber(ctx context.Context) (hexutil.Uint64, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return 0, err } @@ -33,7 +33,7 @@ func (api *APIImpl) BlockNumber(ctx context.Context) (hexutil.Uint64, error) { // Syncing implements eth_syncing. Returns a data object detaling the status of the sync process or false if not syncing. func (api *APIImpl) Syncing(ctx context.Context) (interface{}, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -61,7 +61,7 @@ func (api *APIImpl) Syncing(ctx context.Context) (interface{}, error) { // ChainId implements eth_chainId. Returns the current ethereum chainId. func (api *APIImpl) ChainId(ctx context.Context) (hexutil.Uint64, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return 0, err } @@ -88,7 +88,7 @@ func (api *APIImpl) GasPrice(ctx context.Context) (*hexutil.Big, error) { // HeaderByNumber is necessary for gasprice.OracleBackend implementation func (api *APIImpl) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -108,7 +108,7 @@ func (api *APIImpl) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) // BlockByNumber is necessary for gasprice.OracleBackend implementation func (api *APIImpl) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -131,7 +131,7 @@ func (api *APIImpl) BlockByNumber(ctx context.Context, number rpc.BlockNumber) ( // ChainConfig is necessary for gasprice.OracleBackend implementation func (api *APIImpl) ChainConfig() *params.ChainConfig { - tx, err := api.db.Begin(context.TODO()) + tx, err := api.db.BeginRo(context.TODO()) if err != nil { log.Warn("Could not read chain config from the db, defaulting to MainnetChainConfig", "err", err) return params.MainnetChainConfig diff --git a/cmd/rpcdaemon/commands/eth_txs.go b/cmd/rpcdaemon/commands/eth_txs.go index a807482c2d8..98b2f1c383b 100644 --- a/cmd/rpcdaemon/commands/eth_txs.go +++ b/cmd/rpcdaemon/commands/eth_txs.go @@ -13,7 +13,7 @@ import ( // GetTransactionByHash implements eth_getTransactionByHash. Returns information about a transaction given the transaction's hash. func (api *APIImpl) GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -29,7 +29,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, hash common.Hash) // GetTransactionByBlockHashAndIndex implements eth_getTransactionByBlockHashAndIndex. Returns information about a transaction given the block's hash and a transaction index. func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint64) (*RPCTransaction, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -54,7 +54,7 @@ func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, block // GetTransactionByBlockNumberAndIndex implements eth_getTransactionByBlockNumberAndIndex. Returns information about a transaction given a block number and transaction index. func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, txIndex hexutil.Uint) (*RPCTransaction, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/eth_uncles.go b/cmd/rpcdaemon/commands/eth_uncles.go index 28a6cc8c951..eb02b8f5609 100644 --- a/cmd/rpcdaemon/commands/eth_uncles.go +++ b/cmd/rpcdaemon/commands/eth_uncles.go @@ -15,7 +15,7 @@ import ( // GetUncleByBlockNumberAndIndex implements eth_getUncleByBlockNumberAndIndex. Returns information about an uncle given a block's number and the index of the uncle. func (api *APIImpl) GetUncleByBlockNumberAndIndex(ctx context.Context, number rpc.BlockNumber, index hexutil.Uint) (map[string]interface{}, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -51,7 +51,7 @@ func (api *APIImpl) GetUncleByBlockNumberAndIndex(ctx context.Context, number rp // GetUncleByBlockHashAndIndex implements eth_getUncleByBlockHashAndIndex. Returns information about an uncle given a block's hash and the index of the uncle. func (api *APIImpl) GetUncleByBlockHashAndIndex(ctx context.Context, hash common.Hash, index hexutil.Uint) (map[string]interface{}, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -86,7 +86,7 @@ func (api *APIImpl) GetUncleByBlockHashAndIndex(ctx context.Context, hash common func (api *APIImpl) GetUncleCountByBlockNumber(ctx context.Context, number rpc.BlockNumber) (*hexutil.Uint, error) { n := hexutil.Uint(0) - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return &n, err } @@ -111,7 +111,7 @@ func (api *APIImpl) GetUncleCountByBlockNumber(ctx context.Context, number rpc.B // GetUncleCountByBlockHash implements eth_getUncleCountByBlockHash. Returns the number of uncles in the block, if any. func (api *APIImpl) GetUncleCountByBlockHash(ctx context.Context, hash common.Hash) (*hexutil.Uint, error) { n := hexutil.Uint(0) - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return &n, err } diff --git a/cmd/rpcdaemon/commands/get_chain_config_test.go b/cmd/rpcdaemon/commands/get_chain_config_test.go index da8ee3fca85..a02de2e9455 100644 --- a/cmd/rpcdaemon/commands/get_chain_config_test.go +++ b/cmd/rpcdaemon/commands/get_chain_config_test.go @@ -16,7 +16,7 @@ func TestGetChainConfig(t *testing.T) { t.Fatalf("setting up genensis block: %v", err) } - tx, txErr := db.Begin(context.Background()) + tx, txErr := db.BeginRo(context.Background()) if txErr != nil { t.Fatalf("error starting tx: %v", txErr) } diff --git a/cmd/rpcdaemon/commands/tg_block.go b/cmd/rpcdaemon/commands/tg_block.go index 6a7bf50e671..ea6063c52df 100644 --- a/cmd/rpcdaemon/commands/tg_block.go +++ b/cmd/rpcdaemon/commands/tg_block.go @@ -19,7 +19,7 @@ func (api *TgImpl) GetHeaderByNumber(ctx context.Context, blockNumber rpc.BlockN return block.Header(), nil } - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } @@ -35,7 +35,7 @@ func (api *TgImpl) GetHeaderByNumber(ctx context.Context, blockNumber rpc.BlockN // GetHeaderByHash implements tg_getHeaderByHash. Returns a block's header given a block's hash. func (api *TgImpl) GetHeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/tg_issuance.go b/cmd/rpcdaemon/commands/tg_issuance.go index 514ee541f1b..2bb62371ac5 100644 --- a/cmd/rpcdaemon/commands/tg_issuance.go +++ b/cmd/rpcdaemon/commands/tg_issuance.go @@ -35,7 +35,7 @@ import ( // Issuance implements tg_issuance. Returns the total issuance (block reward plus uncle reward) for the given block. func (api *TgImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return Issuance{}, err } diff --git a/cmd/rpcdaemon/commands/tg_receipts.go b/cmd/rpcdaemon/commands/tg_receipts.go index db8c876f164..9cf988e6f61 100644 --- a/cmd/rpcdaemon/commands/tg_receipts.go +++ b/cmd/rpcdaemon/commands/tg_receipts.go @@ -12,7 +12,7 @@ import ( // GetLogsByHash implements tg_getLogsByHash. Returns an array of arrays of logs generated by the transactions in the block given by the block's hash. func (api *TgImpl) GetLogsByHash(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/tg_system.go b/cmd/rpcdaemon/commands/tg_system.go index 5a9c19bd17b..653189f4fe4 100644 --- a/cmd/rpcdaemon/commands/tg_system.go +++ b/cmd/rpcdaemon/commands/tg_system.go @@ -15,7 +15,7 @@ type Forks struct { // Forks implements tg_forks. Returns the genesis block hash and a sorted list of all forks block numbers func (api *TgImpl) Forks(ctx context.Context) (Forks, error) { - tx, err := api.db.Begin(ctx) + tx, err := api.db.BeginRo(ctx) if err != nil { return Forks{}, err } diff --git a/cmd/rpcdaemon/commands/trace_adhoc.go b/cmd/rpcdaemon/commands/trace_adhoc.go index f43673e3788..b0e0a46cce5 100644 --- a/cmd/rpcdaemon/commands/trace_adhoc.go +++ b/cmd/rpcdaemon/commands/trace_adhoc.go @@ -436,7 +436,7 @@ const callTimeout = 5 * time.Minute // Call implements trace_call. func (api *TraceAPIImpl) Call(ctx context.Context, args TraceCallParam, traceTypes []string, blockNrOrHash *rpc.BlockNumberOrHash) (*TraceCallResult, error) { - dbtx, err := api.kv.Begin(ctx) + dbtx, err := api.kv.BeginRo(ctx) if err != nil { return nil, err } @@ -547,7 +547,7 @@ func (api *TraceAPIImpl) Call(ctx context.Context, args TraceCallParam, traceTyp // CallMany implements trace_callMany. func (api *TraceAPIImpl) CallMany(ctx context.Context, calls json.RawMessage, blockNrOrHash *rpc.BlockNumberOrHash) ([]*TraceCallResult, error) { - dbtx, err := api.kv.Begin(ctx) + dbtx, err := api.kv.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpcdaemon/commands/trace_filtering.go b/cmd/rpcdaemon/commands/trace_filtering.go index 90d7c7ace90..cc632978d94 100644 --- a/cmd/rpcdaemon/commands/trace_filtering.go +++ b/cmd/rpcdaemon/commands/trace_filtering.go @@ -23,7 +23,7 @@ import ( // Transaction implements trace_transaction // TODO(tjayrush): I think this should return an []interface{}, so we can return both Parity and Geth traces func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash) (ParityTraces, error) { - tx, err := api.kv.Begin(ctx) + tx, err := api.kv.BeginRo(ctx) if err != nil { return nil, err } @@ -43,7 +43,7 @@ func (api *TraceAPIImpl) Transaction(ctx context.Context, txHash common.Hash) (P // TODO(tjayrush): only accepts a single one // TODO(tjayrush): I think this should return an interface{}, so we can return both Parity and Geth traces func (api *TraceAPIImpl) Get(ctx context.Context, txHash common.Hash, indicies []hexutil.Uint64) (*ParityTrace, error) { - tx, err := api.kv.Begin(ctx) + tx, err := api.kv.BeginRo(ctx) if err != nil { return nil, err } @@ -71,7 +71,7 @@ func (api *TraceAPIImpl) Get(ctx context.Context, txHash common.Hash, indicies [ // Block implements trace_block func (api *TraceAPIImpl) Block(ctx context.Context, blockNr rpc.BlockNumber) (ParityTraces, error) { - tx, err := api.kv.Begin(ctx) + tx, err := api.kv.BeginRo(ctx) if err != nil { return nil, err } @@ -100,7 +100,7 @@ func (api *TraceAPIImpl) Block(ctx context.Context, blockNr rpc.BlockNumber) (Pa // TODO(tjayrush): Eventually, we will need to protect ourselves from 'large' queries. Parity crashes when a range query of a very large size // is sent. We need to protect ourselves with maxTraces. It may already be done func (api *TraceAPIImpl) Filter(ctx context.Context, req TraceFilterRequest) (ParityTraces, error) { - tx, err1 := api.kv.Begin(ctx) + tx, err1 := api.kv.BeginRo(ctx) if err1 != nil { return nil, fmt.Errorf("traceFilter cannot open tx: %v", err1) } diff --git a/cmd/rpcdaemon/commands/tracing.go b/cmd/rpcdaemon/commands/tracing.go index 5634b9a195b..f512effdac4 100644 --- a/cmd/rpcdaemon/commands/tracing.go +++ b/cmd/rpcdaemon/commands/tracing.go @@ -18,7 +18,7 @@ import ( // TraceTransaction implements debug_traceTransaction. Returns Geth style transaction traces. func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash common.Hash, config *tracers.TraceConfig) (interface{}, error) { - tx, err := api.dbReader.Begin(ctx) + tx, err := api.dbReader.BeginRo(ctx) if err != nil { return nil, err } @@ -46,7 +46,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo } func (api *PrivateDebugAPIImpl) TraceCall(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, config *tracers.TraceConfig) (interface{}, error) { - dbtx, err := api.dbReader.Begin(ctx) + dbtx, err := api.dbReader.BeginRo(ctx) if err != nil { return nil, err } diff --git a/cmd/rpctest/rpctest/account_range_verify.go b/cmd/rpctest/rpctest/account_range_verify.go index ab17cd55b11..5635c5c4c6c 100644 --- a/cmd/rpctest/rpctest/account_range_verify.go +++ b/cmd/rpctest/rpctest/account_range_verify.go @@ -109,11 +109,11 @@ func CompareAccountRange(tgURL, gethURL, tmpDataDir, gethDataDir string, blockFr } } - tgTx, err := resultsKV.Begin(context.Background()) + tgTx, err := resultsKV.BeginRo(context.Background()) if err != nil { log.Fatal(err) } - gethTx, err := gethKV.Begin(context.Background()) + gethTx, err := gethKV.BeginRo(context.Background()) if err != nil { log.Fatal(err) } diff --git a/cmd/snapshots/generator/commands/generate_state_snapshot.go b/cmd/snapshots/generator/commands/generate_state_snapshot.go index 4c231762d92..02babbfa801 100644 --- a/cmd/snapshots/generator/commands/generate_state_snapshot.go +++ b/cmd/snapshots/generator/commands/generate_state_snapshot.go @@ -72,11 +72,11 @@ func GenerateStateSnapshot(ctx context.Context, dbPath, snapshotPath string, toB sndb := ethdb.NewObjectDatabase(snkv) mt := sndb.NewBatch() - tx, err := kv.Begin(context.Background()) + tx, err := kv.BeginRo(context.Background()) if err != nil { return err } - tx2, err := kv.Begin(context.Background()) + tx2, err := kv.BeginRo(context.Background()) if err != nil { return err } diff --git a/cmd/state/verify/check_indexes.go b/cmd/state/verify/check_indexes.go index 90748c081ab..75fc8c19662 100644 --- a/cmd/state/verify/check_indexes.go +++ b/cmd/state/verify/check_indexes.go @@ -15,7 +15,7 @@ import ( func CheckIndex(ctx context.Context, chaindata string, changeSetBucket string, indexBucket string) error { db := ethdb.MustOpen(chaindata) defer db.Close() - tx, err := db.RwKV().Begin(context.Background()) + tx, err := db.RwKV().BeginRo(context.Background()) if err != nil { return err } diff --git a/core/state/history_test.go b/core/state/history_test.go index 6cf1497f291..b4671365fce 100644 --- a/core/state/history_test.go +++ b/core/state/history_test.go @@ -580,7 +580,7 @@ func TestWalkAsOfUsingFixedBytesStatePlain(t *testing.T) { var err error startKey := make([]byte, 60) copy(startKey[:common.AddressLength], addr1.Bytes()) - tx, err1 := db.RwKV().Begin(context.Background()) + tx, err1 := db.RwKV().BeginRo(context.Background()) if err1 != nil { t.Fatalf("create tx: %v", err1) } @@ -761,7 +761,7 @@ func TestWalkAsOfAccountPlain(t *testing.T) { }, }) - tx, err1 := db.RwKV().Begin(context.Background()) + tx, err1 := db.RwKV().BeginRo(context.Background()) if err1 != nil { t.Fatalf("create tx: %v", err1) } @@ -954,7 +954,7 @@ func TestWalkAsOfAccountPlain_WithChunks(t *testing.T) { }, }) - tx, err1 := db.RwKV().Begin(context.Background()) + tx, err1 := db.RwKV().BeginRo(context.Background()) if err1 != nil { t.Fatalf("create tx: %v", err1) } @@ -1093,7 +1093,7 @@ func TestWalkAsOfStoragePlain_WithChunks(t *testing.T) { }, }) - tx, err1 := db.RwKV().Begin(context.Background()) + tx, err1 := db.RwKV().BeginRo(context.Background()) if err1 != nil { t.Fatalf("create tx: %v", err1) } diff --git a/core/state/plain_readonly.go b/core/state/plain_readonly.go index 07538f851ad..ccfa6d71534 100644 --- a/core/state/plain_readonly.go +++ b/core/state/plain_readonly.go @@ -69,7 +69,7 @@ func (dbs *PlainDBState) ForEachStorage(addr common.Address, startLocation commo if hasTx, ok := dbs.db.(ethdb.HasTx); ok { tx = hasTx.Tx() } else { - dbtx, err := dbs.db.BeginRO(context.Background()) + dbtx, err := dbs.db.BeginGetter(context.Background()) if err != nil { return err } @@ -152,7 +152,7 @@ func (dbs *PlainDBState) ReadAccountData(address common.Address) (*accounts.Acco if hasTx, ok := dbs.db.(ethdb.HasTx); ok { tx = hasTx.Tx() } else { - dbtx, err := dbs.db.BeginRO(context.Background()) + dbtx, err := dbs.db.BeginGetter(context.Background()) if err != nil { return nil, err } @@ -188,7 +188,7 @@ func (dbs *PlainDBState) ReadAccountStorage(address common.Address, incarnation if hasTx, ok := dbs.db.(ethdb.HasTx); ok { tx = hasTx.Tx() } else { - dbtx, err := dbs.db.BeginRO(context.Background()) + dbtx, err := dbs.db.BeginGetter(context.Background()) if err != nil { return nil, err } @@ -211,7 +211,7 @@ func (dbs *PlainDBState) ReadAccountCode(address common.Address, incarnation uin if hasTx, ok := dbs.db.(ethdb.HasTx); ok { tx = hasTx.Tx() } else { - dbtx, err := dbs.db.BeginRO(context.Background()) + dbtx, err := dbs.db.BeginGetter(context.Background()) if err != nil { return nil, err } @@ -238,7 +238,7 @@ func (dbs *PlainDBState) ReadAccountIncarnation(address common.Address) (uint64, if hasTx, ok := dbs.db.(ethdb.HasTx); ok { tx = hasTx.Tx() } else { - dbtx, err := dbs.db.BeginRO(context.Background()) + dbtx, err := dbs.db.BeginGetter(context.Background()) if err != nil { return 0, err } diff --git a/core/state/plain_state_writer.go b/core/state/plain_state_writer.go index ea545eed4af..8b64212e235 100644 --- a/core/state/plain_state_writer.go +++ b/core/state/plain_state_writer.go @@ -177,7 +177,7 @@ func (w *PlainStateWriter) WriteHistory() error { } if !externalTx { - tx.Commit(context.Background()) + tx.Commit() } return nil diff --git a/core/state/state_test.go b/core/state/state_test.go index e221f9535b1..f52e172cf13 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -65,7 +65,7 @@ func (s *StateSuite) TestDump(c *checker.C) { c.Check(err, checker.IsNil) // check that dump contains the state objects that are in trie - tx, err1 := s.kv.Begin(context.Background()) + tx, err1 := s.kv.BeginRo(context.Background()) if err1 != nil { c.Fatalf("create tx: %v", err1) } @@ -329,7 +329,7 @@ func TestDump(t *testing.T) { } // check that dump contains the state objects that are in trie - tx, err1 := db.RwKV().Begin(context.Background()) + tx, err1 := db.RwKV().BeginRo(context.Background()) if err1 != nil { t.Fatalf("create tx: %v", err1) } diff --git a/ethdb/database_test.go b/ethdb/database_test.go index 7ffdbf70204..d96280ef356 100644 --- a/ethdb/database_test.go +++ b/ethdb/database_test.go @@ -143,7 +143,7 @@ func testPutGet(db MinDatabase, t *testing.T) { } func testNoPanicAfterDbClosed(db Database, t *testing.T) { - tx, err := db.(HasRwKV).RwKV().Begin(context.Background()) + tx, err := db.(HasRwKV).RwKV().BeginRo(context.Background()) require.NoError(t, err) writeTx, err := db.(HasRwKV).RwKV().BeginRw(context.Background()) require.NoError(t, err) @@ -158,7 +158,7 @@ func testNoPanicAfterDbClosed(db Database, t *testing.T) { time.Sleep(time.Millisecond) // wait to check that db.Close doesn't panic, but wait when read tx finished err = writeTx.Put(dbutils.Buckets[0], []byte{1}, []byte{1}) require.NoError(t, err) - err = writeTx.Commit(context.Background()) + err = writeTx.Commit() require.NoError(t, err) _, err = tx.GetOne(dbutils.Buckets[0], []byte{1}) require.NoError(t, err) diff --git a/ethdb/interface.go b/ethdb/interface.go index d2e439cfa3f..b530907f8e5 100644 --- a/ethdb/interface.go +++ b/ethdb/interface.go @@ -26,11 +26,12 @@ import ( // ErrKeyNotFound is returned when key isn't found in the database. var ErrKeyNotFound = errors.New("db: key not found") -// Putter wraps the database write operations. -type Putter interface { - // Put inserts or updates a single entry. - Put(bucket string, key, value []byte) error -} +type TxFlags uint + +const ( + RW TxFlags = 0x00 // default + RO TxFlags = 0x02 +) // Getter wraps the database read operations. type Getter interface { @@ -56,7 +57,7 @@ type GetterTx interface { type GetterBeginner interface { Getter - BeginRO(ctx context.Context) (GetterTx, error) + BeginGetter(ctx context.Context) (GetterTx, error) } type GetterPutter interface { @@ -64,15 +65,6 @@ type GetterPutter interface { Putter } -// Deleter wraps the database delete operations. -type Deleter interface { - // Delete removes a single entry. - Delete(bucket string, k, v []byte) error -} -type Closer interface { - Close() -} - // Database wraps all database operations. All methods are safe for concurrent use. type Database interface { GetterBeginner diff --git a/ethdb/kv_abstract.go b/ethdb/kv_abstract.go index cd46e4189a3..de96b5385a3 100644 --- a/ethdb/kv_abstract.go +++ b/ethdb/kv_abstract.go @@ -18,12 +18,29 @@ var ( dbSize = metrics.GetOrRegisterGauge("db/size", metrics.DefaultRegistry) //nolint ) +// Putter wraps the database write operations. +type Putter interface { + // Put inserts or updates a single entry. + Put(bucket string, key, value []byte) error +} + +// Deleter wraps the database delete operations. +type Deleter interface { + // Delete removes a single entry. + Delete(bucket string, k, v []byte) error +} + +type Closer interface { + Close() +} + // Read-only version of KV. type RoKV interface { + Closer + View(ctx context.Context, f func(tx Tx) error) error - Close() - // Begin - creates transaction + // BeginRo - creates transaction // tx may be discarded by .Rollback() method // // A transaction and its cursors must only be used by a single @@ -36,7 +53,7 @@ type RoKV interface { // as its parent. Transactions may be nested to any level. A parent // transaction and its cursors may not issue any other operations than // Commit and Rollback while it has active child transactions. - Begin(ctx context.Context) (Tx, error) + BeginRo(ctx context.Context) (Tx, error) AllBuckets() dbutils.BucketsCfg CollectMetrics() @@ -74,19 +91,12 @@ type RwKV interface { BeginRw(ctx context.Context) (RwTx, error) } -type TxFlags uint - -const ( - RW TxFlags = 0x00 // default - RO TxFlags = 0x02 -) - type StatelessReadTx interface { GetOne(bucket string, key []byte) (val []byte, err error) HasOne(bucket string, key []byte) (bool, error) - Commit(ctx context.Context) error // Commit all the operations of a transaction into the database. - Rollback() // Rollback - abandon all the operations of the transaction instead of saving them. + 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. // Can be called for a read transaction to retrieve the current sequence value, and the increment must be zero. @@ -96,10 +106,10 @@ type StatelessReadTx interface { } type StatelessWriteTx interface { - IncrementSequence(bucket string, amount uint64) (uint64, error) + Putter + Deleter - Put(bucket string, k, v []byte) error - Delete(bucket string, k, v []byte) error + IncrementSequence(bucket string, amount uint64) (uint64, error) } type StatelessRwTx interface { diff --git a/ethdb/kv_abstract_test.go b/ethdb/kv_abstract_test.go index ef0e23504a6..9c467c736df 100644 --- a/ethdb/kv_abstract_test.go +++ b/ethdb/kv_abstract_test.go @@ -109,7 +109,7 @@ func TestManagedTx(t *testing.T) { } require.NoError(t, c.Put([]byte{0, 0, 0, 0, 0, 1}, []byte{2})) require.NoError(t, c1.Put([]byte{0, 0, 0, 0, 0, 1}, []byte{2})) - err = tx.Commit(context.Background()) + err = tx.Commit() require.NoError(t, err) } diff --git a/ethdb/kv_lmdb.go b/ethdb/kv_lmdb.go index 90348db890c..e425c803b05 100644 --- a/ethdb/kv_lmdb.go +++ b/ethdb/kv_lmdb.go @@ -186,7 +186,7 @@ func (opts LmdbOpts) Open() (kv RwKV, err error) { // Open or create buckets if opts.flags&lmdb.Readonly != 0 { - tx, innerErr := db.Begin(context.Background()) + tx, innerErr := db.BeginRo(context.Background()) if innerErr != nil { return nil, innerErr } @@ -198,7 +198,7 @@ func (opts LmdbOpts) Open() (kv RwKV, err error) { return nil, err } } - err = tx.Commit(context.Background()) + err = tx.Commit() if err != nil { return nil, err } @@ -361,7 +361,7 @@ func (db *LmdbKV) CollectMetrics() { */ } -func (db *LmdbKV) Begin(_ context.Context) (txn Tx, err error) { +func (db *LmdbKV) BeginRo(_ context.Context) (txn Tx, err error) { if db.env == nil { return nil, fmt.Errorf("db closed") } @@ -471,7 +471,7 @@ func (db *LmdbKV) View(ctx context.Context, f func(tx Tx) error) (err error) { defer db.wg.Done() // can't use db.evn.View method - because it calls commit for read transactions - it conflicts with write transactions. - tx, err := db.Begin(ctx) + tx, err := db.BeginRo(ctx) if err != nil { return err } @@ -496,7 +496,7 @@ func (db *LmdbKV) Update(ctx context.Context, f func(tx RwTx) error) (err error) if err != nil { return err } - err = tx.Commit(ctx) + err = tx.Commit() if err != nil { return err } @@ -612,7 +612,7 @@ func (tx *lmdbTx) ExistsBucket(bucket string) bool { return false } -func (tx *lmdbTx) Commit(ctx context.Context) error { +func (tx *lmdbTx) Commit() error { if tx.db.env == nil { return fmt.Errorf("db closed") } diff --git a/ethdb/kv_mdbx.go b/ethdb/kv_mdbx.go index 8a3972ded6c..c902bc3d906 100644 --- a/ethdb/kv_mdbx.go +++ b/ethdb/kv_mdbx.go @@ -184,7 +184,7 @@ func (opts MdbxOpts) Open() (RwKV, error) { // Open or create buckets if opts.flags&mdbx.Readonly != 0 { - tx, innerErr := db.Begin(context.Background()) + tx, innerErr := db.BeginRo(context.Background()) if innerErr != nil { return nil, innerErr } @@ -196,7 +196,7 @@ func (opts MdbxOpts) Open() (RwKV, error) { return nil, err } } - err = tx.Commit(context.Background()) + err = tx.Commit() if err != nil { return nil, err } @@ -354,7 +354,7 @@ func (db *MdbxKV) CollectMetrics() { */ } -func (db *MdbxKV) Begin(_ context.Context) (txn Tx, err error) { +func (db *MdbxKV) BeginRo(_ context.Context) (txn Tx, err error) { if db.env == nil { return nil, fmt.Errorf("db closed") } @@ -463,7 +463,7 @@ func (db *MdbxKV) View(ctx context.Context, f func(tx Tx) error) (err error) { defer db.wg.Done() // can't use db.evn.View method - because it calls commit for read transactions - it conflicts with write transactions. - tx, err := db.Begin(ctx) + tx, err := db.BeginRo(ctx) if err != nil { return err } @@ -488,7 +488,7 @@ func (db *MdbxKV) Update(ctx context.Context, f func(tx RwTx) error) (err error) if err != nil { return err } - err = tx.Commit(ctx) + err = tx.Commit() if err != nil { return err } @@ -623,7 +623,7 @@ func (tx *MdbxTx) ExistsBucket(bucket string) bool { return false } -func (tx *MdbxTx) Commit(ctx context.Context) error { +func (tx *MdbxTx) Commit() error { if tx.db.env == nil { return fmt.Errorf("db closed") } diff --git a/ethdb/kv_migrator_test.go b/ethdb/kv_migrator_test.go index 1b703ec76d1..1b9192b7ef6 100644 --- a/ethdb/kv_migrator_test.go +++ b/ethdb/kv_migrator_test.go @@ -94,7 +94,7 @@ func TestReadOnlyMode(t *testing.T) { } }).MustOpen() - tx, err := db2.Begin(context.Background()) + tx, err := db2.BeginRo(context.Background()) require.NoError(t, err) c, err := tx.Cursor(dbutils.HeadersBucket) diff --git a/ethdb/kv_remote.go b/ethdb/kv_remote.go index 23bf1e133e5..359d80b5d15 100644 --- a/ethdb/kv_remote.go +++ b/ethdb/kv_remote.go @@ -192,7 +192,7 @@ func (db *RemoteKV) Close() { func (db *RemoteKV) CollectMetrics() {} -func (db *RemoteKV) Begin(ctx context.Context) (Tx, error) { +func (db *RemoteKV) BeginRo(ctx context.Context) (Tx, error) { streamCtx, streamCancelFn := context.WithCancel(ctx) // We create child context for the stream so we can cancel it to prevent leak stream, err := db.remoteKV.Tx(streamCtx) if err != nil { @@ -207,7 +207,7 @@ func (db *RemoteKV) BeginRw(ctx context.Context) (RwTx, error) { } func (db *RemoteKV) View(ctx context.Context, f func(tx Tx) error) (err error) { - tx, err := db.Begin(ctx) + tx, err := db.BeginRo(ctx) if err != nil { return err } @@ -230,7 +230,7 @@ func (tx *remoteTx) ReadSequence(bucket string) (uint64, error) { panic("not implemented yet") } -func (tx *remoteTx) Commit(ctx context.Context) error { +func (tx *remoteTx) Commit() error { panic("remote db is read-only") } diff --git a/ethdb/kv_snapshot.go b/ethdb/kv_snapshot.go index 1e9fac7fb32..bf28ce877d0 100644 --- a/ethdb/kv_snapshot.go +++ b/ethdb/kv_snapshot.go @@ -63,7 +63,7 @@ type SnapshotKV2 struct { } func (s *SnapshotKV2) View(ctx context.Context, f func(tx Tx) error) error { - snTX, err := s.Begin(ctx) + snTX, err := s.BeginRo(ctx) if err != nil { return err } @@ -80,7 +80,7 @@ func (s *SnapshotKV2) Update(ctx context.Context, f func(tx RwTx) error) error { err = f(tx) if err == nil { - return tx.Commit(ctx) + return tx.Commit() } return err } @@ -96,8 +96,8 @@ func (s *SnapshotKV2) CollectMetrics() { s.db.CollectMetrics() } -func (s *SnapshotKV2) Begin(ctx context.Context) (Tx, error) { - dbTx, err := s.db.Begin(ctx) +func (s *SnapshotKV2) BeginRo(ctx context.Context) (Tx, error) { + dbTx, err := s.db.BeginRo(ctx) if err != nil { return nil, err } @@ -263,7 +263,7 @@ func (s *sn2TX) getSnapshotTX(bucket string) (Tx, error) { return nil, fmt.Errorf("%s %w", bucket, ErrUnavailableSnapshot) } var err error - tx, err = sn.snapshot.Begin(context.TODO()) + tx, err = sn.snapshot.BeginRo(context.TODO()) if err != nil { return nil, err } @@ -300,11 +300,11 @@ func (s *sn2TX) HasOne(bucket string, key []byte) (bool, error) { return v, nil } -func (s *sn2TX) Commit(ctx context.Context) error { +func (s *sn2TX) Commit() error { for i := range s.snTX { defer s.snTX[i].Rollback() } - return s.dbTX.Commit(ctx) + return s.dbTX.Commit() } func (s *sn2TX) Rollback() { diff --git a/ethdb/kv_snapshot_test.go b/ethdb/kv_snapshot_test.go index 4670857cb57..d3d4e9d5bdf 100644 --- a/ethdb/kv_snapshot_test.go +++ b/ethdb/kv_snapshot_test.go @@ -417,7 +417,7 @@ func TestSnapshot2Get(t *testing.T) { kv := NewSnapshot2KV().DB(mainDB).SnapshotDB([]string{dbutils.HeadersBucket}, sn1). SnapshotDB([]string{dbutils.BlockBodyPrefix}, sn2).MustOpen() - tx, err := kv.Begin(context.Background()) + tx, err := kv.BeginRo(context.Background()) if err != nil { t.Fatal(err) } @@ -578,10 +578,10 @@ func TestSnapshot2WritableTxAndGet(t *testing.T) { require.NoError(t, err) err = tx.Put(dbutils.HeadersBucket, dbutils.HeaderKey(4, common.Hash{4}), []byte{4}) require.NoError(t, err) - err = tx.Commit(context.Background()) + err = tx.Commit() require.NoError(t, err) } - tx, err := kv.Begin(context.Background()) + tx, err := kv.BeginRo(context.Background()) require.NoError(t, err) c, err := tx.Cursor(dbutils.HeadersBucket) require.NoError(t, err) diff --git a/ethdb/mutation.go b/ethdb/mutation.go index ef7395aaa4e..cbf819ef728 100644 --- a/ethdb/mutation.go +++ b/ethdb/mutation.go @@ -330,8 +330,8 @@ func (m *mutation) Begin(ctx context.Context, flags TxFlags) (DbWithPendingMutat return m.db.Begin(ctx, flags) } -func (m *mutation) BeginRO(ctx context.Context) (GetterTx, error) { - return m.db.BeginRO(ctx) +func (m *mutation) BeginGetter(ctx context.Context) (GetterTx, error) { + return m.db.BeginGetter(ctx) } func (m *mutation) panicOnEmptyDB() { diff --git a/ethdb/object_db.go b/ethdb/object_db.go index b588bb1d17f..f83d4a50c42 100644 --- a/ethdb/object_db.go +++ b/ethdb/object_db.go @@ -380,7 +380,7 @@ func (db *ObjectDatabase) NewBatch() DbWithPendingMutations { return m } -func (db *ObjectDatabase) BeginRO(ctx context.Context) (GetterTx, error) { +func (db *ObjectDatabase) BeginGetter(ctx context.Context) (GetterTx, error) { batch := &TxDb{db: db} if err := batch.begin(ctx, RO); err != nil { return batch, err diff --git a/ethdb/remote/remotedbserver/server.go b/ethdb/remote/remotedbserver/server.go index e5dac401b1b..4544b65a0be 100644 --- a/ethdb/remote/remotedbserver/server.go +++ b/ethdb/remote/remotedbserver/server.go @@ -91,7 +91,7 @@ func NewKvServer(kv ethdb.RwKV) *KvServer { } func (s *KvServer) Tx(stream remote.KV_TxServer) error { - tx, errBegin := s.kv.Begin(stream.Context()) + tx, errBegin := s.kv.BeginRo(stream.Context()) if errBegin != nil { return fmt.Errorf("server-side error: %w", errBegin) } @@ -135,7 +135,7 @@ func (s *KvServer) Tx(stream remote.KV_TxServer) error { } tx.Rollback() - tx, errBegin = s.kv.Begin(stream.Context()) + tx, errBegin = s.kv.BeginRo(stream.Context()) if errBegin != nil { return fmt.Errorf("server-side error: %w", errBegin) } diff --git a/ethdb/tx_db.go b/ethdb/tx_db.go index 53d83f278e3..572e87f92e5 100644 --- a/ethdb/tx_db.go +++ b/ethdb/tx_db.go @@ -57,7 +57,7 @@ func (m *roTxDb) Walk(bucket string, startkey []byte, fixedbits int, walker func return Walk(c, startkey, fixedbits, walker) } -func (m *roTxDb) BeginRO(ctx context.Context) (GetterTx, error) { +func (m *roTxDb) BeginGetter(ctx context.Context) (GetterTx, error) { return &roTxDb{tx: m.tx, top: false}, nil } @@ -107,7 +107,7 @@ func (m *TxDb) Begin(ctx context.Context, flags TxFlags) (DbWithPendingMutations return batch, nil } -func (m *TxDb) BeginRO(ctx context.Context) (GetterTx, error) { +func (m *TxDb) BeginGetter(ctx context.Context) (GetterTx, error) { batch := m if m.tx != nil { panic("nested transactions not supported") @@ -189,7 +189,7 @@ func (m *TxDb) begin(ctx context.Context, flags TxFlags) error { var tx Tx var err error if flags&RO != 0 { - tx, err = kv.Begin(ctx) + tx, err = kv.BeginRo(ctx) } else { tx, err = kv.BeginRw(ctx) } @@ -301,7 +301,7 @@ func (m *TxDb) Commit() error { if m.tx == nil { return fmt.Errorf("second call .Commit() on same transaction") } - if err := m.tx.Commit(context.Background()); err != nil { + if err := m.tx.Commit(); err != nil { return err } m.tx = nil diff --git a/turbo/trie/flatdb_sub_trie_loader.go b/turbo/trie/flatdb_sub_trie_loader.go index 557298ee425..816fe29dd9d 100644 --- a/turbo/trie/flatdb_sub_trie_loader.go +++ b/turbo/trie/flatdb_sub_trie_loader.go @@ -596,7 +596,7 @@ func (fstl *FlatDbSubTrieLoader) LoadSubTries() (SubTries, error) { } if fstl.tx == nil { var err error - fstl.tx, err = fstl.kv.Begin(context.Background()) + fstl.tx, err = fstl.kv.BeginRo(context.Background()) if err != nil { return SubTries{}, err }