Skip to content

Commit

Permalink
rawdb.TxLookup - to return err, remove docs of rpcdaemon dual mode (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Jul 11, 2021
1 parent f2f92d4 commit e1c17e0
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 124 deletions.
9 changes: 6 additions & 3 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common
return nil, err
}
defer tx.Rollback()
receipt, _, _, _ := rawdb.ReadReceipt(tx, txHash)
return receipt, nil
receipt, _, _, _, err := rawdb.ReadReceipt(tx, txHash)
return receipt, err
}

// TransactionByHash checks the pool of pending transactions in addition to the
Expand All @@ -266,7 +266,10 @@ func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.
if txn != nil {
return txn, true, nil
}
txn, _, _, _ = rawdb.ReadTransaction(tx, txHash)
txn, _, _, _, err = rawdb.ReadTransaction(tx, txHash)
if err != nil {
return nil, false, err
}
if txn != nil {
return txn, false, nil
}
Expand Down
174 changes: 96 additions & 78 deletions cmd/rpcdaemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
- [Getting Started](#getting-started)
* [Running locally](#running-locally)
* [Running remotely](#running-remotely)
* [Running in dual mode](#running-in-dual-mode)
* [Testing](#testing)
- [FAQ](#faq)
* [RPC Implementation Status](#rpc-implementation-status)
Expand All @@ -16,12 +15,13 @@
- [For Developers](#for-developers)
* [Code generation](#code-generation)


## Introduction

Erigon's `rpcdaemon` runs in its own seperate process.

This brings many benefits including easier development, the ability to run multiple daemons at once, and the ability to run the daemon remotely. It is possible to run the daemon locally as well (read-only) if both processes have access to the data folder.
This brings many benefits including easier development, the ability to run multiple daemons at once, and the ability to
run the daemon remotely. It is possible to run the daemon locally as well (read-only) if both processes have access to
the data folder.

## Getting Started

Expand All @@ -33,30 +33,27 @@ make rpcdaemon

### Running locally

If you have direct access to Erigon's database folder, you may run the `rpcdaemon` locally. This may provide faster results.

After building, run this command to start the daemon locally:
Run `rpcdaemon` on same computer with Erigon. It's default option because it using Shared Memory access to Erigon's db -
it's much faster than TCP access. Provide both `--datadir` and `--private.api.addr` flags:

```[bash]
./build/bin/rpcdaemon --datadir ~/Library/Erigon/ --http.api=eth,debug,net,web3
make erigon
./build/bin/erigon --datadir=<your_data_dir> --private.api.addr=localhost:9090
make rpcdaemon
./build/bin/rpcdaemon --datadir=<your_data_dir> --private.api.addr=localhost:9090 --http.api=eth,erigon,web3,net,debug,trace,txpool,shh
```

This mode is mostly convenient for debugging purposes, because we know that the database does not change as we are sending requests to the RPC daemon.

Note that we've also specified which RPC commands to enable in the above command.
Note that we've also specified which RPC namespaces to enable in the above command by `--http.api` flag.

### Running remotely

To start the daemon remotely, build it as described above, then run `erigon` in one terminal window:
To start the daemon remotely - just don't set `--datadir` flag:

```[bash]
./build/bin/erigon --private.api.addr=localhost:9090
```

In another terminal window, start the daemon with the same `--private-api` setting:

```[bash]
./build/bin/rpcdaemon --private.api.addr=localhost:9090
make erigon
./build/bin/erigon --datadir=<your_data_dir> --private.api.addr=0.0.0.0:9090
make rpcdaemon
./build/bin/rpcdaemon --private.api.addr=<erigon_ip>:9090 --http.api=eth,erigon,web3,net,debug,trace,txpool,shh
```

The daemon should respond with something like:
Expand All @@ -65,13 +62,10 @@ The daemon should respond with something like:
INFO [date-time] HTTP endpoint opened url=localhost:8545...
```

### Running in dual mode

If both `--datadir` and `--private.api.addr` options are used for RPC daemon, it works in a "dual" mode. This only works when RPC daemon is on the same computer as Erigon. In this mode, most data transfer from Erigon to RPC daemon happens via shared memory, only certain things (like new header notifications) happen via TPC socket.

### Testing

By default, the `rpcdaemon` serves data from `localhost:8545`. You may send `curl` commands to see if things are working.
By default, the `rpcdaemon` serves data from `localhost:8545`. You may send `curl` commands to see if things are
working.

Try `eth_blockNumber` for example. In a third terminal window enter this command:

Expand All @@ -89,7 +83,9 @@ This should return something along the lines of this (depending on how far your
}
```

Also, there are [extensive instructions for using Postman](https://github.com/ledgerwatch/erigon/wiki/Using-Postman-to-Test-TurboGeth-RPC) to test the RPC.
Also, there
are [extensive instructions for using Postman](https://github.com/ledgerwatch/erigon/wiki/Using-Postman-to-Test-TurboGeth-RPC)
to test the RPC.

## FAQ

Expand Down Expand Up @@ -216,26 +212,34 @@ This table is constantly updated. Please visit again.

### Securing the communication between RPC daemon and Erigon instance via TLS and authentication

In some cases, it is useful to run Erigon nodes in a different network (for example, in a Public cloud), but RPC daemon locally. To ensure
the integrity of communication and access control to the Erigon node, TLS authentication can be enabled.
On the high level, the process consists of these steps (this process needs to be done for any "cluster" of Erigon and RPC daemon nodes that are
supposed to work together):

1. Generate key pair for the Certificate Authority (CA). The private key of CA will be used to authorise new Erigon instances as well as new RPC daemon instances, so that they can mutually authenticate.
2. Create CA certificate file that needs to be deployed on any Erigon instance and any RPC daemon. This CA cerf file is used as a "root of trust", whatever is in it, will be trusted by the participants when they authenticate their counterparts.
3. For each Erigon instance and each RPC daemon instance, generate a key pair. If you are lazy, you can generate one pair for all Erigon nodes, and one pair for all RPC daemons, and copy these keys around.
4. Using the CA private key, create cerificate file for each public key generated on the previous step. This effectively "inducts" these keys into the "cluster of trust".
In some cases, it is useful to run Erigon nodes in a different network (for example, in a Public cloud), but RPC daemon
locally. To ensure the integrity of communication and access control to the Erigon node, TLS authentication can be
enabled. On the high level, the process consists of these steps (this process needs to be done for any "cluster" of
Erigon and RPC daemon nodes that are supposed to work together):

1. Generate key pair for the Certificate Authority (CA). The private key of CA will be used to authorise new Erigon
instances as well as new RPC daemon instances, so that they can mutually authenticate.
2. Create CA certificate file that needs to be deployed on any Erigon instance and any RPC daemon. This CA cerf file is
used as a "root of trust", whatever is in it, will be trusted by the participants when they authenticate their
counterparts.
3. For each Erigon instance and each RPC daemon instance, generate a key pair. If you are lazy, you can generate one
pair for all Erigon nodes, and one pair for all RPC daemons, and copy these keys around.
4. Using the CA private key, create cerificate file for each public key generated on the previous step. This
effectively "inducts" these keys into the "cluster of trust".
5. On each instance, deploy 3 files - CA certificate, instance key, and certificate signed by CA for this instance key.

Following is the detailed description of how it can be done using `openssl` suite of tools.

Generate CA key pair using Elliptic Curve (as opposed to RSA). The generated CA key will be in the file `CA-key.pem`. Access to this file will allow anyone to later include any new instance key pair into the "cluster of trust", so keep it secure.
Generate CA key pair using Elliptic Curve (as opposed to RSA). The generated CA key will be in the file `CA-key.pem`.
Access to this file will allow anyone to later include any new instance key pair into the "cluster of trust", so keep it
secure.

```
openssl ecparam -name prime256v1 -genkey -noout -out CA-key.pem
```

Create CA self-signed certificate (this command will ask questions, answers aren't important for now). The file created by this command is `CA-cert.pem`
Create CA self-signed certificate (this command will ask questions, answers aren't important for now). The file created
by this command is `CA-cert.pem`

```
openssl req -x509 -new -nodes -key CA-key.pem -sha256 -days 3650 -out CA-cert.pem
Expand All @@ -259,7 +263,8 @@ Now create certificate signing request for Erigon key pair:
openssl req -new -key erigon-key.pem -out erigon.csr
```

And from this request, produce the certificate (signed by CA), proving that this key is now part of the "cluster of trust"
And from this request, produce the certificate (signed by CA), proving that this key is now part of the "cluster of
trust"

```
openssl x509 -req -in erigon.csr -CA CA-cert.pem -CAkey CA-key.pem -CAcreateserial -out erigon.crt -days 3650 -sha256
Expand All @@ -271,27 +276,35 @@ Then, produce the certificate signing request for RPC daemon key pair:
openssl req -new -key RPC-key.pem -out RPC.csr
```

And from this request, produce the certificate (signed by CA), proving that this key is now part of the "cluster of trust"
And from this request, produce the certificate (signed by CA), proving that this key is now part of the "cluster of
trust"

```
openssl x509 -req -in RPC.csr -CA CA-cert.pem -CAkey CA-key.pem -CAcreateserial -out RPC.crt -days 3650 -sha256
```

When this is all done, these three files need to be placed on the machine where Erigon is running: `CA-cert.pem`, `erigon-key.pem`, `erigon.crt`. And Erigon needs to be run with these extra options:
When this is all done, these three files need to be placed on the machine where Erigon is running: `CA-cert.pem`
, `erigon-key.pem`, `erigon.crt`. And Erigon needs to be run with these extra options:

```
--tls --tls.cacert CA-cert.pem --tls.key erigon-key.pem --tls.cert erigon.crt
```

On the RPC daemon machine, these three files need to be placed: `CA-cert.pem`, `RPC-key.pem`, and `RPC.crt`. And RPC daemon needs to be started with these extra options:
On the RPC daemon machine, these three files need to be placed: `CA-cert.pem`, `RPC-key.pem`, and `RPC.crt`. And RPC
daemon needs to be started with these extra options:

```
--tls.key RPC-key.pem --tls.cacert CA-cert.pem --tls.cert RPC.crt
```

**WARNING** Normally, the "client side" (which in our case is RPC daemon), verifies that the host name of the server matches the "Common Name" attribute of the "server" cerificate. At this stage, this verification is turned off, and it will be turned on again once we have updated the instruction above on how to properly generate cerificates with "Common Name".
**WARNING** Normally, the "client side" (which in our case is RPC daemon), verifies that the host name of the server
matches the "Common Name" attribute of the "server" cerificate. At this stage, this verification is turned off, and it
will be turned on again once we have updated the instruction above on how to properly generate cerificates with "Common
Name".

When running Erigon instance in the Google Cloud, for example, you need to specify the **Internal IP** in the `--private.api.addr` option. And, you will need to open the firewall on the port you are using, to that connection to the Erigon instances can be made.
When running Erigon instance in the Google Cloud, for example, you need to specify the **Internal IP** in
the `--private.api.addr` option. And, you will need to open the firewall on the port you are using, to that connection
to the Erigon instances can be made.

### Ethstats

Expand All @@ -306,25 +319,30 @@ Then update your `app.json` for ethstats-client like that:
```json
[
{
"name" : "ethstats",
"script" : "app.js",
"log_date_format" : "YYYY-MM-DD HH:mm Z",
"merge_logs" : false,
"watch" : false,
"max_restarts" : 10,
"exec_interpreter" : "node",
"exec_mode" : "fork_mode",
"env":
{
"NODE_ENV" : "production",
"RPC_HOST" : "localhost",
"RPC_PORT" : "8545",
"LISTENING_PORT" : "30303",
"INSTANCE_NAME" : "Erigon node",
"CONTACT_DETAILS" : <your twitter handle>,
"WS_SERVER" : "wss://ethstats.net/api",
"WS_SECRET" : <put your secret key there>,
"VERBOSITY" : 2
"name": "ethstats",
"script": "app.js",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"merge_logs": false,
"watch": false,
"max_restarts": 10,
"exec_interpreter": "node",
"exec_mode": "fork_mode",
"env": {
"NODE_ENV": "production",
"RPC_HOST": "localhost",
"RPC_PORT": "8545",
"LISTENING_PORT": "30303",
"INSTANCE_NAME": "Erigon node",
"CONTACT_DETAILS": <your
twitter
handle>,
"WS_SERVER": "wss://ethstats.net/api",
"WS_SECRET": <put
your
secret
key
there>,
"VERBOSITY": 2
}
}
]
Expand All @@ -341,19 +359,19 @@ WARN [11-05|09:03:47.911] Served conn=127.0.0.

### Allowing only specific methods (Allowlist)

In some cases you might want to only allow certain methods in the namespaces
and hide others. That is possible with `rpc.accessList` flag.
In some cases you might want to only allow certain methods in the namespaces and hide others. That is possible
with `rpc.accessList` flag.

1. Create a file, say, `rules.json`

2. Add the following content

```json
{
"allow": [
"net_version",
"web3_eth_getBlockByHash"
]
"allow": [
"net_version",
"web3_eth_getBlockByHash"
]
}
```

Expand All @@ -367,33 +385,33 @@ Now only these two methods are available.

### Clients getting timeout, but server load is low

In this case: increase default rate-limit -
amount of requests server handle simultaneously - requests over this limit will wait.
Increase it - if your 'hot data' is small or have much RAM or see "request timeout" while server load is low.

In this case: increase default rate-limit - amount of requests server handle simultaneously - requests over this limit
will wait. Increase it - if your 'hot data' is small or have much RAM or see "request timeout" while server load is low.

```
./build/bin/erigon --private.api.addr=localhost:9090 --private.api.ratelimit=1024
```

### Server load too high
### Server load too high

Reduce `--private.api.ratelimit`
Reduce `--private.api.ratelimit`

### Read DB directly without Json-RPC/Graphql

[./docs/programmers_guide/db_faq.md](./docs/programmers_guide/db_faq.md)

### Batch requests
### Batch requests

Currently batch requests are spawn multiple goroutines and process all sub-requests in parallel.
To limit impact of 1 huge batch to other users - added flag `--rpc.batch.concurrency` (default: 50).
Increase it to process large batches faster.
Currently batch requests are spawn multiple goroutines and process all sub-requests in parallel. To limit impact of 1
huge batch to other users - added flag `--rpc.batch.concurrency` (default: 50). Increase it to process large batches
faster.

Known Issue: if at least 1 request is "stremable" (has parameter of type *jsoniter.Stream) - then whole batch will processed sequentially (on 1 goroutine).
Known Issue: if at least 1 request is "stremable" (has parameter of type *jsoniter.Stream) - then whole batch will
processed sequentially (on 1 goroutine).

## For Developers

### Code generation

`go.mod` stores right version of generators, use `make grpc` to install it and generate code (it also installs protoc into ./build/bin folder).
`go.mod` stores right version of generators, use `make grpc` to install it and generate code (it also installs protoc
into ./build/bin folder).
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func RootCommand() (*cobra.Command, *Flags) {
rootCmd.PersistentFlags().StringSliceVar(&cfg.HttpCORSDomain, "http.corsdomain", []string{}, "Comma separated list of domains from which to accept cross origin requests (browser enforced)")
rootCmd.PersistentFlags().StringSliceVar(&cfg.HttpVirtualHost, "http.vhosts", node.DefaultConfig.HTTPVirtualHosts, "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard.")
rootCmd.PersistentFlags().BoolVar(&cfg.HttpCompression, "http.compression", true, "Disable http compression")
rootCmd.PersistentFlags().StringSliceVar(&cfg.API, "http.api", []string{"eth", "erigon"}, "API's offered over the HTTP-RPC interface: eth,erigon,web3,netdebug,trace,txpool,shh,db. Supported methods: https://github.com/ledgerwatch/erigon/tree/devel/cmd/rpcdaemon")
rootCmd.PersistentFlags().StringSliceVar(&cfg.API, "http.api", []string{"eth", "erigon"}, "API's offered over the HTTP-RPC interface: eth,erigon,web3,net,debug,trace,txpool,shh,db. Supported methods: https://github.com/ledgerwatch/erigon/tree/devel/cmd/rpcdaemon")
rootCmd.PersistentFlags().Uint64Var(&cfg.Gascap, "rpc.gascap", 25000000, "Sets a cap on gas that can be used in eth_call/estimateGas")
rootCmd.PersistentFlags().Uint64Var(&cfg.MaxTraces, "trace.maxtraces", 200, "Sets a limit on traces that can be returned in trace_filter")
rootCmd.PersistentFlags().BoolVar(&cfg.WebsocketEnabled, "ws", false, "Enable Websockets")
Expand Down
5 changes: 4 additions & 1 deletion cmd/rpcdaemon/commands/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
var txs types.Transactions

for _, txHash := range txHashes {
txn, _, _, _ := rawdb.ReadTransaction(tx, txHash)
txn, _, _, _, err := rawdb.ReadTransaction(tx, txHash)
if err != nil {
return nil, err
}
if txn == nil {
return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/rpcdaemon/commands/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, hash common.Hash)
}
defer tx.Rollback()

blockNumber := rawdb.ReadTxLookupEntry(tx, hash)
blockNumber, err := rawdb.ReadTxLookupEntry(tx, hash)
if err != nil {
return nil, err
}
if blockNumber == nil {
return nil, nil // not error, see https://github.com/ledgerwatch/erigon/issues/1645
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/rpcdaemon/commands/eth_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func (api *APIImpl) GetTransactionByHash(ctx context.Context, hash common.Hash)
defer tx.Rollback()

// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
txn, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(tx, hash)
txn, blockHash, blockNumber, txIndex, err := rawdb.ReadTransaction(tx, hash)
if err != nil {
return nil, err
}

// Add GasPrice for the DynamicFeeTransaction
var baseFee *big.Int
Expand Down Expand Up @@ -71,7 +74,10 @@ func (api *APIImpl) GetRawTransactionByHash(ctx context.Context, hash common.Has
defer tx.Rollback()

// https://infura.io/docs/ethereum/json-rpc/eth-getTransactionByHash
txn, _, _, _ := rawdb.ReadTransaction(tx, hash)
txn, _, _, _, err := rawdb.ReadTransaction(tx, hash)
if err != nil {
return nil, err
}
if txn != nil {
var buf bytes.Buffer
err = txn.MarshalBinary(&buf)
Expand Down
Loading

0 comments on commit e1c17e0

Please sign in to comment.