Skip to content

Commit

Permalink
Harmonize DB APIs (erigontech#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 authored and sam committed Apr 5, 2021
1 parent fd4d8fb commit 3a52b73
Show file tree
Hide file tree
Showing 41 changed files with 140 additions and 138 deletions.
2 changes: 1 addition & 1 deletion cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hack/db/lmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/integration/commands/refetence_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,15 @@ MainLoop:
panic(err)
}
}
err = dstTx.Commit(context.Background())
err = dstTx.Commit()
if err != nil {
return err
}
dstTx, err = dst.BeginRw(ctx)
if err != nil {
return err
}
err = dstTx.Commit(ctx)
err = dstTx.Commit()
if err != nil {
return err
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -509,15 +509,15 @@ 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
}
dstTx, err = dst.BeginRw(ctx)
if err != nil {
return err
}
err = dstTx.Commit(ctx)
err = dstTx.Commit()
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/rpcdaemon/commands/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpcdaemon/commands/eth_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpcdaemon/commands/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/commands/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/commands/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/rpcdaemon/commands/eth_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/rpcdaemon/commands/eth_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpcdaemon/commands/eth_uncles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/get_chain_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/commands/tg_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/tg_issuance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/tg_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/tg_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 3a52b73

Please sign in to comment.