Skip to content

Commit

Permalink
Restore genesis reads code - remote rpc case. Remove ctx parameter fr…
Browse files Browse the repository at this point in the history
…om baseApi func (tx already has internal ctx) (#8122)
  • Loading branch information
AskAlexSharov authored Sep 4, 2023
1 parent 1cfb1eb commit 8983128
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 83 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/erigontech/mdbx-go v0.27.14
github.com/ledgerwatch/erigon-lib v0.0.0-20230901170341-82448e2edeae
github.com/ledgerwatch/erigon-lib v0.0.0-20230904034409-568d783b0d85
github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230818153427-cc16b83a89be
github.com/ledgerwatch/log/v3 v3.9.0
github.com/ledgerwatch/secp256k1 v1.0.0
Expand Down Expand Up @@ -91,7 +91,7 @@ require (
golang.org/x/exp v0.0.0-20230711023510-fffb14384f22
golang.org/x/net v0.14.0
golang.org/x/sync v0.3.0
golang.org/x/sys v0.11.0
golang.org/x/sys v0.12.0
golang.org/x/time v0.3.0
google.golang.org/grpc v1.57.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230901170341-82448e2edeae h1:S5OnGA5Jrs/y2zZUv2j9DBnVxBF1yylb34ufUUxG2go=
github.com/ledgerwatch/erigon-lib v0.0.0-20230901170341-82448e2edeae/go.mod h1:wWs9yVYGm1mDFb+Qnh7Qun1MIX6cP5vp4PAfOjnvm+g=
github.com/ledgerwatch/erigon-lib v0.0.0-20230904034409-568d783b0d85 h1:PvSsOmri3fLl9BfvcuslVcaQDXBTJkzs56t1xj1nnis=
github.com/ledgerwatch/erigon-lib v0.0.0-20230904034409-568d783b0d85/go.mod h1:VLDj7l088dUzwBBSORGy5GGdUPM8XhLNMURmm1F1QMA=
github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230818153427-cc16b83a89be h1:6/4MXkk5AoKUHivIpCokHOX/WV9L7tXgURp1k8KfmSM=
github.com/ledgerwatch/erigon-snapshot v1.2.1-0.20230818153427-cc16b83a89be/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZFRk=
Expand Down Expand Up @@ -1129,8 +1129,9 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
12 changes: 6 additions & 6 deletions turbo/jsonrpc/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (api *PrivateDebugAPIImpl) StorageRangeAt(ctx context.Context, blockHash co
return storageRangeAtV3(tx.(kv.TemporalTx), contractAddress, keyStart, minTxNum+txIndex, maxResult)
}

block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
block, err := api.blockByHashWithSenders(tx, blockHash)
if err != nil {
return StorageRangeResult{}, err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func (api *PrivateDebugAPIImpl) AccountRange(ctx context.Context, blockNrOrHash
}

} else if hash, ok := blockNrOrHash.Hash(); ok {
block, err1 := api.blockByHashWithSenders(ctx, tx, hash)
block, err1 := api.blockByHashWithSenders(tx, hash)
if err1 != nil {
return state.IteratorDump{}, err1
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (api *PrivateDebugAPIImpl) GetModifiedAccountsByHash(ctx context.Context, s
}
defer tx.Rollback()

startBlock, err := api.blockByHashWithSenders(ctx, tx, startHash)
startBlock, err := api.blockByHashWithSenders(tx, startHash)
if err != nil {
return nil, err
}
Expand All @@ -254,7 +254,7 @@ func (api *PrivateDebugAPIImpl) GetModifiedAccountsByHash(ctx context.Context, s
endNum := startNum + 1 // allows for single parameter calls

if endHash != nil {
endBlock, err := api.blockByHashWithSenders(ctx, tx, *endHash)
endBlock, err := api.blockByHashWithSenders(tx, *endHash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -337,7 +337,7 @@ func (api *PrivateDebugAPIImpl) AccountAt(ctx context.Context, blockHash common.
}
engine := api.engine()

block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
block, err := api.blockByHashWithSenders(tx, blockHash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -393,7 +393,7 @@ func (api *PrivateDebugAPIImpl) GetRawBlock(ctx context.Context, blockNrOrHash r
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(ctx, tx, h, n)
block, err := api.blockWithSenders(tx, h, n)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions turbo/jsonrpc/erigon_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (api *ErigonImpl) GetLogsByHash(ctx context.Context, hash common.Hash) ([][
return nil, err
}

block, err := api.blockByHashWithSenders(ctx, tx, hash)
block, err := api.blockByHashWithSenders(tx, hash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func (api *ErigonImpl) GetBlockReceiptsByBlockHash(ctx context.Context, cannonic
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(ctx, tx, cannonicalBlockHash, blockNum)
block, err := api.blockWithSenders(tx, cannonicalBlockHash, blockNum)
if err != nil {
return nil, err
}
Expand Down
31 changes: 16 additions & 15 deletions turbo/jsonrpc/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,19 @@ func (api *BaseAPI) genesis(tx kv.Tx) (*types.Block, error) {
return genesis, err
}

func (api *BaseAPI) txnLookup(ctx context.Context, tx kv.Tx, txnHash common.Hash) (uint64, bool, error) {
return api._txnReader.TxnLookup(ctx, tx, txnHash)
func (api *BaseAPI) txnLookup(tx kv.Tx, txnHash common.Hash) (uint64, bool, error) {
return api._txnReader.TxnLookup(context.Background(), tx, txnHash)
}

func (api *BaseAPI) blockByNumberWithSenders(ctx context.Context, tx kv.Tx, number uint64) (*types.Block, error) {
hash, hashErr := api._blockReader.CanonicalHash(ctx, tx, number)
func (api *BaseAPI) blockByNumberWithSenders(tx kv.Tx, number uint64) (*types.Block, error) {
hash, hashErr := api._blockReader.CanonicalHash(context.Background(), tx, number)
if hashErr != nil {
return nil, hashErr
}
return api.blockWithSenders(ctx, tx, hash, number)
return api.blockWithSenders(tx, hash, number)
}

func (api *BaseAPI) blockByHashWithSenders(ctx context.Context, tx kv.Tx, hash common.Hash) (*types.Block, error) {
func (api *BaseAPI) blockByHashWithSenders(tx kv.Tx, hash common.Hash) (*types.Block, error) {
if api.blocksLRU != nil {
if it, ok := api.blocksLRU.Get(hash); ok && it != nil {
return it, nil
Expand All @@ -176,16 +176,16 @@ func (api *BaseAPI) blockByHashWithSenders(ctx context.Context, tx kv.Tx, hash c
return nil, nil
}

return api.blockWithSenders(ctx, tx, hash, *number)
return api.blockWithSenders(tx, hash, *number)
}

func (api *BaseAPI) blockWithSenders(ctx context.Context, tx kv.Tx, hash common.Hash, number uint64) (*types.Block, error) {
func (api *BaseAPI) blockWithSenders(tx kv.Tx, hash common.Hash, number uint64) (*types.Block, error) {
if api.blocksLRU != nil {
if it, ok := api.blocksLRU.Get(hash); ok && it != nil {
return it, nil
}
}
block, _, err := api._blockReader.BlockWithSenders(ctx, tx, hash, number)
block, _, err := api._blockReader.BlockWithSenders(context.Background(), tx, hash, number)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -228,14 +228,14 @@ func (api *BaseAPI) chainConfigWithGenesis(tx kv.Tx) (*chain.Config, *types.Bloc
return cc, genesisBlock, nil
}

hash, err := rawdb.ReadCanonicalHash(tx, 0)
genesisBlock, err := api.blockByRPCNumber(0, tx)
if err != nil {
return nil, nil, err
}
if err != nil {
return nil, nil, err
if genesisBlock == nil {
return nil, nil, fmt.Errorf("genesis block not found in database")
}
cc, err = rawdb.ReadChainConfig(tx, hash)
cc, err = rawdb.ReadChainConfig(tx, genesisBlock.Hash())
if err != nil {
return nil, nil, err
}
Expand All @@ -250,13 +250,14 @@ func (api *BaseAPI) pendingBlock() *types.Block {
return api.filters.LastPendingBlock()
}

func (api *BaseAPI) blockByRPCNumber(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) {
func (api *BaseAPI) blockByRPCNumber(number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) {
n, h, _, err := rpchelper.GetBlockNumber(rpc.BlockNumberOrHashWithNumber(number), tx, api.filters)
if err != nil {
return nil, err
}

block, err := api.blockWithSenders(ctx, tx, h, n)
// it's ok to use context.Background(), because in "Remote RPCDaemon" `tx` already contains internal ctx
block, err := api.blockWithSenders(tx, h, n)
return block, err
}

Expand Down
12 changes: 6 additions & 6 deletions turbo/jsonrpc/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
var txs types.Transactions

for _, txHash := range txHashes {
blockNum, ok, err := api.txnLookup(ctx, tx, txHash)
blockNum, ok, err := api.txnLookup(tx, txHash)
if err != nil {
return nil, err
}
if !ok {
return nil, nil
}
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
block, err := api.blockByNumberWithSenders(tx, blockNum)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, numberOrHash rpc.BlockNu

additionalFields := make(map[string]interface{})

block, err := api.blockByHashWithSenders(ctx, tx, hash)
block, err := api.blockByHashWithSenders(tx, hash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -313,7 +313,7 @@ func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockN
defer tx.Rollback()

if blockNr == rpc.PendingBlockNumber {
b, err := api.blockByRPCNumber(ctx, blockNr, tx)
b, err := api.blockByRPCNumber(blockNr, tx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHas

func (api *APIImpl) blockByNumber(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, error) {
if number != rpc.PendingBlockNumber {
return api.blockByRPCNumber(ctx, number, tx)
return api.blockByRPCNumber(number, tx)
}

if block := api.pendingBlock(); block != nil {
Expand All @@ -387,5 +387,5 @@ func (api *APIImpl) blockByNumber(ctx context.Context, number rpc.BlockNumber, t
return block, nil
}

return api.blockByRPCNumber(ctx, number, tx)
return api.blockByRPCNumber(number, tx)
}
6 changes: 3 additions & 3 deletions turbo/jsonrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (api *APIImpl) Call(ctx context.Context, args ethapi2.CallArgs, blockNrOrHa
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(ctx, tx, hash, blockNumber)
block, err := api.blockWithSenders(tx, hash, blockNumber)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
// try and get the block from the lru cache first then try DB before failing
block := api.tryBlockFromLru(latestCanHash)
if block == nil {
block, err = api.blockWithSenders(ctx, dbtx, latestCanHash, latestCanBlockNumber)
block, err = api.blockWithSenders(dbtx, latestCanHash, latestCanBlockNumber)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -449,7 +449,7 @@ func (api *APIImpl) CreateAccessList(ctx context.Context, args ethapi2.CallArgs,
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(ctx, tx, hash, blockNumber)
block, err := api.blockWithSenders(tx, hash, blockNumber)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/eth_callMany.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (api *APIImpl) CallMany(ctx context.Context, bundles []Bundle, simulateCont
return nil, err
}

block, err := api.blockWithSenders(ctx, tx, hash, blockNum)
block, err := api.blockWithSenders(tx, hash, blockNum)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions turbo/jsonrpc/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
var blockNum uint64
var ok bool

blockNum, ok, err = api.txnLookup(ctx, tx, txnHash)
blockNum, ok, err = api.txnLookup(tx, txnHash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
blockNum = *blockNumPtr
}

block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
block, err := api.blockByNumberWithSenders(tx, blockNum)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -679,7 +679,7 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, numberOrHash rpc.Block
if err != nil {
return nil, err
}
block, err := api.blockWithSenders(ctx, tx, blockHash, blockNum)
block, err := api.blockWithSenders(tx, blockHash, blockNum)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/eth_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (b *GasPriceOracleBackend) HeaderByNumber(ctx context.Context, number rpc.B
return header, nil
}
func (b *GasPriceOracleBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
return b.baseApi.blockByRPCNumber(ctx, number, b.tx)
return b.baseApi.blockByRPCNumber(number, b.tx)
}
func (b *GasPriceOracleBackend) ChainConfig() *chain.Config {
return b.cc
Expand Down
16 changes: 8 additions & 8 deletions turbo/jsonrpc/eth_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
}

// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
blockNum, ok, err := api.txnLookup(ctx, tx, txnHash)
blockNum, ok, err := api.txnLookup(tx, txnHash)
if err != nil {
return nil, err
}
Expand All @@ -45,7 +45,7 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, txnHash common.Has
fmt.Printf("Found block num %d, ok %t\n", blockNum, ok)
}
if ok {
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
block, err := api.blockByNumberWithSenders(tx, blockNum)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,14 +118,14 @@ func (api *APIImpl) GetRawTransactionByHash(ctx context.Context, hash common.Has
defer tx.Rollback()

// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
blockNum, ok, err := api.txnLookup(ctx, tx, hash)
blockNum, ok, err := api.txnLookup(tx, hash)
if err != nil {
return nil, err
}
if !ok {
return nil, nil
}
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
block, err := api.blockByNumberWithSenders(tx, blockNum)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (api *APIImpl) GetTransactionByBlockHashAndIndex(ctx context.Context, block
}

// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByBlockHashAndIndex
block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
block, err := api.blockByHashWithSenders(tx, blockHash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func (api *APIImpl) GetRawTransactionByBlockHashAndIndex(ctx context.Context, bl
defer tx.Rollback()

// https://infura.io/docs/ethereum/json-rpc/eth-getRawTransactionByBlockHashAndIndex
block, err := api.blockByHashWithSenders(ctx, tx, blockHash)
block, err := api.blockByHashWithSenders(tx, blockHash)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (api *APIImpl) GetTransactionByBlockNumberAndIndex(ctx context.Context, blo
return nil, err
}

block, err := api.blockWithSenders(ctx, tx, hash, blockNum)
block, err := api.blockWithSenders(tx, hash, blockNum)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func (api *APIImpl) GetRawTransactionByBlockNumberAndIndex(ctx context.Context,
defer tx.Rollback()

// https://infura.io/docs/ethereum/json-rpc/eth-getRawTransactionByBlockNumberAndIndex
block, err := api.blockByRPCNumber(ctx, blockNr, tx)
block, err := api.blockByRPCNumber(blockNr, tx)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8983128

Please sign in to comment.