Skip to content

Commit

Permalink
Fixes issue #1139 - crash when running chaindata mode (#1140)
Browse files Browse the repository at this point in the history
* Fixes issue #1139 - crash when running chaindata mode

* Cleaning up error messages as per feedback
  • Loading branch information
tjayrush committed Sep 27, 2020
1 parent 5b4445e commit 784bc9d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cmd/rpcdaemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ The following table shows the current implementation status of turbo-geth's RPC
| web3_clientVersion | Yes | |
| web3_sha3 | Yes | |
| | | |
| net_listening | HC | (hard coded returns true) |
| net_listening | HC | (remote only hard coded returns true) |
| net_peerCount | HC | (hard coded 25 - work continues on Sentry) |
| net_version | Yes | |
| net_version | Yes | remote only |
| | | |
| eth_blockNumber | Yes | |
| eth_chainID | Yes | |
Expand Down Expand Up @@ -119,10 +119,10 @@ The following table shows the current implementation status of turbo-geth's RPC
| eth_getFilterChanges | - | |
| eth_getFilterLogs | - | |
| eth_uninstallFilter | - | |
| eth_getLogs | Yes | |
| eth_getLogs | Yes | remote only |
| | | |
| eth_accounts | - | |
| eth_sendRawTransaction | Yes | |
| eth_sendRawTransaction | Yes | remote only |
| eth_sendTransaction | - | |
| eth_sign | - | |
| eth_signTransaction | - | |
Expand Down
7 changes: 6 additions & 1 deletion cmd/rpcdaemon/commands/coinbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package commands

import (
"context"
"fmt"

"github.com/ledgerwatch/turbo-geth/common"
)

// Etherbase is the address that mining rewards will be send to
// Coinbase is the address that mining rewards will be sent to
func (api *APIImpl) Coinbase(_ context.Context) (common.Address, error) {
if api.ethBackend == nil {
// We're running in --chaindata mode or otherwise cannot get the backend
return common.Address{}, fmt.Errorf(NotAvailableChainData, "eth_coinbase")
}
return api.ethBackend.Etherbase()
}
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (api *PrivateDebugAPIImpl) AccountRange(ctx context.Context, blockNrOrHash

blockNumber, _, err = stages.GetStageProgress(api.dbReader, stages.Execution)
if err != nil {
return state.IteratorDump{}, fmt.Errorf("las block has not found: %w", err)
return state.IteratorDump{}, fmt.Errorf("last block has not found: %w", err)
}
} else {
blockNumber = uint64(number)
Expand Down
7 changes: 7 additions & 0 deletions cmd/rpcdaemon/commands/error_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package commands

// NotImplemented is the URI prefix for smartcard wallets.
const NotImplemented = "the function %s is currently not implemented"

// NotAvailableChainData x
const NotAvailableChainData = "the function %s is not available, please use --private.api.addr option instead of --chaindata option"
5 changes: 5 additions & 0 deletions cmd/rpcdaemon/commands/get_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ func (f *Filter) Logs(ctx context.Context, api *APIImpl) ([]*types.Log, error) {
end = latest
}

if api.ethBackend == nil {
// We're running in --chaindata mode or otherwise cannot get the backend
return nil, fmt.Errorf(NotAvailableChainData, "eth_getLogs")
}

// Gather all indexed logs, and finish with non indexed ones
var logs []*types.Log
size, sections, _ := api.ethBackend.BloomStatus()
Expand Down
13 changes: 11 additions & 2 deletions cmd/rpcdaemon/commands/net_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,51 @@ package commands

import (
"context"
"fmt"
"strconv"

"github.com/ledgerwatch/turbo-geth/common/hexutil"

"github.com/ledgerwatch/turbo-geth/ethdb"
)

// NetAPI the interface for the net_ RPC commands
type NetAPI interface {
Listening(_ context.Context) (bool, error)
Version(_ context.Context) (string, error)
PeerCount(_ context.Context) (hexutil.Uint, error)
}

// NetAPIImpl data structure to store things needed for net_ commands
type NetAPIImpl struct {
ethBackend ethdb.Backend
}

// NewtNetAPIImpl returns NetAPIImplImpl instance
// NewNetAPIImpl returns NetAPIImplImpl instance
func NewNetAPIImpl(eth ethdb.Backend) *NetAPIImpl {
return &NetAPIImpl{
ethBackend: eth,
}
}

// Listen implements RPC call for net_listening
// Listening implements RPC call for net_listening
// TODO(tjayrush) remove hard coded value
func (api *NetAPIImpl) Listening(_ context.Context) (bool, error) {
return true, nil
}

// Version implements RPC call for net_version
func (api *NetAPIImpl) Version(_ context.Context) (string, error) {
if api.ethBackend == nil {
// We're running in --chaindata mode or otherwise cannot get the backend
return "", fmt.Errorf(NotAvailableChainData, "net_version")
}

res, err := api.ethBackend.NetVersion()
if err != nil {
return "", err
}

return strconv.FormatUint(res, 10), nil
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/rpcdaemon/commands/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package commands

import (
"context"
"fmt"

"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
)

// SendRawTransaction send a raw transaction
func (api *APIImpl) SendRawTransaction(_ context.Context, encodedTx hexutil.Bytes) (common.Hash, error) {
if api.ethBackend == nil {
// We're running in --chaindata mode or otherwise cannot get the backend
return common.Hash{}, fmt.Errorf(NotAvailableChainData, "eth_sendRawTransaction")
}
res, err := api.ethBackend.AddLocal(encodedTx)
return common.BytesToHash(res), err
}
10 changes: 5 additions & 5 deletions cmd/rpcdaemon/commands/trace_api_adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ type CallParams []CallParam
// Call Implements trace_call
func (api *TraceAPIImpl) Call(ctx context.Context, call CallParam, blockNr rpc.BlockNumber) ([]interface{}, error) {
var stub []interface{}
return stub, fmt.Errorf("function trace_call not implemented")
return stub, fmt.Errorf(NotImplemented, "trace_call")
}

// CallMany Implements trace_call
func (api *TraceAPIImpl) CallMany(ctx context.Context, calls CallParams) ([]interface{}, error) {
var stub []interface{}
return stub, fmt.Errorf("function trace_callMany not implemented")
return stub, fmt.Errorf(NotImplemented, "trace_callMany")
}

// RawTransaction Implements trace_rawtransaction
func (api *TraceAPIImpl) RawTransaction(ctx context.Context, txHash common.Hash, traceTypes []string) ([]interface{}, error) {
var stub []interface{}
return stub, fmt.Errorf("function trace_rawTransaction not implemented")
return stub, fmt.Errorf(NotImplemented, "trace_rawTransaction")
}

// ReplayBlockTransactions Implements trace_replayBlockTransactions
func (api *TraceAPIImpl) ReplayBlockTransactions(ctx context.Context, blockNr rpc.BlockNumber, traceTypes []string) ([]interface{}, error) {
var stub []interface{}
return stub, fmt.Errorf("function trace_replayBlockTransactions not implemented")
return stub, fmt.Errorf(NotImplemented, "trace_replayBlockTransactions")
}

// ReplayTransaction Implements trace_replaytransactions
func (api *TraceAPIImpl) ReplayTransaction(ctx context.Context, txHash common.Hash, traceTypes []string) ([]interface{}, error) {
var stub []interface{}
return stub, fmt.Errorf("function trace_replayTransaction not implemented")
return stub, fmt.Errorf(NotImplemented, "trace_replayTransaction")
}

0 comments on commit 784bc9d

Please sign in to comment.