Skip to content

Commit

Permalink
Get logs (#1028)
Browse files Browse the repository at this point in the history
* it compiles

* after recent master

* fix linters warnings

* grpcV7

* go mod tidy

* unmarshall adresses or adress

* fix linters

* after cr

* after cr

* after cr

* after cr

* fix tests

* remove dev version

* it compiles

* mod tidy

* fix bin deps

* use stable version of grpc

* switch back to master constructor

* switch back to master constructor

* add a bit docs

* add a bit docs

Co-authored-by: Alexey Akhunov <[email protected]>
Co-authored-by: alex.sharov <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2020
1 parent e02f2f6 commit e4f495f
Show file tree
Hide file tree
Showing 16 changed files with 675 additions and 89 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,9 @@ clean:
# You need to put $GOBIN (or $GOPATH/bin) in your PATH to use 'go generate'.

devtools:
# See: ./cmd/hack/binary-deps/main.go
env GOBIN= go install golang.org/x/tools/cmd/stringer
env GOBIN= go install github.com/kevinburke/go-bindata/go-bindata
env GOBIN= go install github.com/fjl/gencodec
env GOBIN= go install google.golang.org/protobuf/cmd/protoc-gen-go # generates proto messages
env GOBIN= go install google.golang.org/grpc/cmd/protoc-gen-go-grpc # generates grpc services
env GOBIN= go install ./cmd/abigen
@type "npm" 2> /dev/null || echo 'Please install node.js and npm'
@type "solc" 2> /dev/null || echo 'Please install solc'
Expand All @@ -140,6 +137,11 @@ bindings:
go generate ./tests/contracts/
go generate ./cmd/tester/contracts/
go generate ./core/state/contracts/

grpc:
# See also: ./cmd/hack/binary-deps/main.go
env GOBIN= go install google.golang.org/protobuf/cmd/protoc-gen-go # generates proto messages
env GOBIN= go install google.golang.org/grpc/cmd/protoc-gen-go-grpc # generates grpc services
go generate ./ethdb

simulator-genesis:
Expand Down
6 changes: 6 additions & 0 deletions cmd/rpcdaemon/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ It should return something like this (depending on how far your turbo-geth node
````
{"jsonrpc":"2.0","id":1,"result":823909}
````

### For Developers

**Code generation**: `go.mod` stores right version of generators, use `mage grpc` to install it and generate code.

`protoc` version not managed but recommended version is 3.*, [install instruction](https://grpc.io/docs/protoc-installation/)
34 changes: 22 additions & 12 deletions cmd/rpcdaemon/commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/ledgerwatch/turbo-geth/turbo/adapter/ethapi"
Expand All @@ -17,17 +17,9 @@ import (
// GetBlockByNumber see https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbynumber
// see internal/ethapi.PublicBlockChainAPI.GetBlockByNumber
func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
var blockNum uint64
if number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber {
var err error
blockNum, _, err = stages.GetStageProgress(api.dbReader, stages.Execution)
if err != nil {
return nil, fmt.Errorf("getting latest block number: %v", err)
}
} else if number == rpc.EarliestBlockNumber {
blockNum = 0
} else {
blockNum = uint64(number.Int64())
blockNum, err := getBlockNumber(number, api.dbReader)
if err != nil {
return nil, err
}
additionalFields := make(map[string]interface{})

Expand Down Expand Up @@ -71,6 +63,24 @@ func (api *APIImpl) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx
return response, err
}

func (api *APIImpl) GetHeaderByNumber(_ context.Context, number rpc.BlockNumber) (*types.Header, error) {
header := rawdb.ReadHeaderByNumber(api.dbReader, uint64(number.Int64()))
if header == nil {
return nil, fmt.Errorf("block header not found: %d", number.Int64())
}

return header, nil
}

func (api *APIImpl) GetHeaderByHash(_ context.Context, hash common.Hash) (*types.Header, error) {
header := rawdb.ReadHeaderByHash(api.dbReader, hash)
if header == nil {
return nil, fmt.Errorf("block header not found: %s", hash.String())
}

return header, nil
}

func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.API) []rpc.API {
var defaultAPIList []rpc.API

Expand Down
19 changes: 7 additions & 12 deletions cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/ledgerwatch/turbo-geth/eth/filters"

"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/core"
Expand All @@ -23,7 +25,7 @@ type EthAPI interface {
GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error)
GetBalance(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error)
GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error)
GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error)
GetLogs(ctx context.Context, crit filters.FilterCriteria) ([]*types.Log, error)
Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]ethapi.Account) (hexutil.Bytes, error)
EstimateGas(ctx context.Context, args ethapi.CallArgs) (hexutil.Uint64, error)
SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (common.Hash, error)
Expand Down Expand Up @@ -83,18 +85,11 @@ func (api *APIImpl) Syncing(ctx context.Context) (interface{}, error) {
}

func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error) {
var blockNum uint64
if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
var err error
blockNum, _, err = stages.GetStageProgress(api.dbReader, stages.Execution)
if err != nil {
return nil, fmt.Errorf("getting latest block number: %v", err)
}
} else if blockNr == rpc.EarliestBlockNumber {
blockNum = 0
} else {
blockNum = uint64(blockNr.Int64())
blockNum, err := getBlockNumber(blockNr, api.dbReader)
if err != nil {
return nil, err
}

block := rawdb.ReadBlockByNumber(api.dbReader, blockNum)
if block == nil {
return nil, fmt.Errorf("block not found: %d", blockNum)
Expand Down
11 changes: 11 additions & 0 deletions cmd/rpcdaemon/commands/get_chain_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands

import (
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/params"
)

func getChainConfig(db rawdb.DatabaseReader) *params.ChainConfig {
genesisHash := rawdb.ReadBlockByNumber(db, 0).Hash()
return rawdb.ReadChainConfig(db, genesisHash)
}
Loading

0 comments on commit e4f495f

Please sign in to comment.