Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore_: implement eth service for use in integration tests #5903

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node/get_status_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/status-im/status-go/services/communitytokens"
"github.com/status-im/status-go/services/connector"
"github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/services/eth"
"github.com/status-im/status-go/services/gif"
localnotifications "github.com/status-im/status-go/services/local-notifications"
"github.com/status-im/status-go/services/mailservers"
Expand Down Expand Up @@ -132,6 +133,7 @@ type StatusNode struct {
pendingTracker *transactions.PendingTxTracker
connectorSrvc *connector.Service
appGeneralSrvc *appgeneral.Service
ethSrvc *eth.Service

accountsFeed event.Feed
walletFeed event.Feed
Expand Down
10 changes: 10 additions & 0 deletions node/status_node_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/status-im/status-go/services/communitytokens"
"github.com/status-im/status-go/services/connector"
"github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/services/eth"
"github.com/status-im/status-go/services/ext"
"github.com/status-im/status-go/services/gif"
localnotifications "github.com/status-im/status-go/services/local-notifications"
Expand Down Expand Up @@ -174,6 +175,8 @@ func (b *StatusNode) initServices(config *params.NodeConfig, mediaServer *server
}
services = append(services, lns)

services = append(services, b.ethService())

b.peerSrvc.SetDiscoverer(b)

for i := range services {
Expand Down Expand Up @@ -617,6 +620,13 @@ func (b *StatusNode) peerService() *peer.Service {
return b.peerSrvc
}

func (b *StatusNode) ethService() *eth.Service {
if b.ethSrvc == nil {
b.ethSrvc = eth.NewService(b.rpcClient)
}
return b.ethSrvc
}

func registerWakuMailServer(wakuService *waku.Waku, config *params.WakuConfig) (err error) {
var mailServer mailserver.WakuMailServer
wakuService.RegisterMailServer(&mailServer)
Expand Down
151 changes: 151 additions & 0 deletions services/eth/private_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//go:build enable_private_api

package eth

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
geth_rpc "github.com/ethereum/go-ethereum/rpc"

"github.com/status-im/status-go/rpc"
)

func privateAPIs(client *rpc.Client) (apis []geth_rpc.API) {
return []geth_rpc.API{
{
Namespace: "ethclient",
Version: "1.0",
Service: NewPrivateAPI(client),
Public: true,
},
}
}

type PrivateAPI struct {
client *rpc.Client
}

func NewPrivateAPI(client *rpc.Client) *PrivateAPI {
return &PrivateAPI{client: client}
}

type blockResponse struct {
Header *types.Header `json:"header"`
Transactions types.Transactions `json:"transactions"`
Withdrawals types.Withdrawals `json:"withdrawals"`
}

func newBlockResponse(b *types.Block) *blockResponse {
return &blockResponse{
Header: b.Header(),
Transactions: b.Transactions(),
Withdrawals: b.Withdrawals(),
}
}

func (pa *PrivateAPI) BlockByHash(ctx context.Context, chainId uint64, hash common.Hash) (*blockResponse, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 53 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L53

Added line #L53 was not covered by tests
}

block, err := client.BlockByHash(ctx, hash)
if err != nil {
return nil, err

Check warning on line 58 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L58

Added line #L58 was not covered by tests
}

return newBlockResponse(block), nil
}

func (pa *PrivateAPI) BlockByNumber(ctx context.Context, chainId uint64, number *hexutil.Big) (*blockResponse, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 67 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L67

Added line #L67 was not covered by tests
}

block, err := client.BlockByNumber(ctx, (*big.Int)(number))
if err != nil {
return nil, err

Check warning on line 72 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L72

Added line #L72 was not covered by tests
}

return newBlockResponse(block), nil
}

func (pa *PrivateAPI) HeaderByHash(ctx context.Context, chainId uint64, hash common.Hash) (*types.Header, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 81 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L81

Added line #L81 was not covered by tests
}

return client.HeaderByHash(ctx, hash)
}

func (pa *PrivateAPI) HeaderByNumber(ctx context.Context, chainId uint64, number *hexutil.Big) (*types.Header, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 90 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L90

Added line #L90 was not covered by tests
}

return client.HeaderByNumber(ctx, (*big.Int)(number))
}

type transactionByHashResponse struct {
Tx *types.Transaction `json:"tx"`
IsPending bool `json:"isPending"`
}

func (pa *PrivateAPI) TransactionByHash(ctx context.Context, chainId uint64, txHash common.Hash) (*transactionByHashResponse, error) {

client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 105 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L105

Added line #L105 was not covered by tests
}

tx, isPending, err := client.TransactionByHash(ctx, txHash)
if err != nil {
return nil, err

Check warning on line 110 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L110

Added line #L110 was not covered by tests
}

ret := &transactionByHashResponse{
Tx: tx,
IsPending: isPending,
}

return ret, nil
}

func (pa *PrivateAPI) TransactionReceipt(ctx context.Context, chainId uint64, txHash common.Hash) (*types.Receipt, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 124 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L124

Added line #L124 was not covered by tests
}

return client.TransactionReceipt(ctx, txHash)
}

func (pa *PrivateAPI) SuggestGasPrice(ctx context.Context, chainId uint64) (*hexutil.Big, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return nil, err

Check warning on line 133 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L133

Added line #L133 was not covered by tests
}

ret, err := client.SuggestGasPrice(ctx)
if err != nil {
return nil, err

Check warning on line 138 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L138

Added line #L138 was not covered by tests
}

return (*hexutil.Big)(ret), nil
}

func (pa *PrivateAPI) BlockNumber(ctx context.Context, chainId uint64) (uint64, error) {
client, err := pa.client.EthClient(chainId)
if err != nil {
return 0, err

Check warning on line 147 in services/eth/private_api.go

View check run for this annotation

Codecov / codecov/patch

services/eth/private_api.go#L147

Added line #L147 was not covered by tests
}

return client.BlockNumber(ctx)
}
12 changes: 12 additions & 0 deletions services/eth/private_api_nop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !enable_private_api

package eth

import (
geth_rpc "github.com/ethereum/go-ethereum/rpc"
"github.com/status-im/status-go/rpc"
)

func privateAPIs(*rpc.Client) (apis []geth_rpc.API) {
return nil
}
36 changes: 36 additions & 0 deletions services/eth/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package eth

import (
"github.com/ethereum/go-ethereum/p2p"
geth_rpc "github.com/ethereum/go-ethereum/rpc"

rpc_client "github.com/status-im/status-go/rpc"
)

type Service struct {
rpcClient *rpc_client.Client
}

func NewService(
rpcClient *rpc_client.Client,
) *Service {
return &Service{
rpcClient: rpcClient,
}
}

func (s *Service) APIs() []geth_rpc.API {
return privateAPIs(s.rpcClient)
}

func (s *Service) Protocols() []p2p.Protocol {
return nil
}

func (s *Service) Start() error {
return nil
}

func (s *Service) Stop() error {
return nil
}
2 changes: 1 addition & 1 deletion tests-functional/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"HTTPHost": "0.0.0.0",
"HTTPPort": 3333,
"HTTPVirtualHosts": ["*", "status-go"],
"APIModules": "eth,admin,wallet,accounts,waku,wakuext",
"APIModules": "eth,admin,wallet,accounts,waku,wakuext,ethclient",
"WalletConfig": {
"Enabled": true
},
Expand Down
6 changes: 3 additions & 3 deletions tests-functional/docker-compose.test.status-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
context: ../
dockerfile: _assets/build/Dockerfile
args:
build_tags: gowaku_no_rln
build_tags: gowaku_no_rln,enable_private_api
build_target: statusd
build_flags: -cover
entrypoint: [
Expand Down Expand Up @@ -37,7 +37,7 @@ services:
context: ../
dockerfile: _assets/build/Dockerfile
args:
build_tags: gowaku_no_rln
build_tags: gowaku_no_rln,enable_private_api
build_target: statusd
build_flags: -cover
entrypoint: [
Expand Down Expand Up @@ -72,7 +72,7 @@ services:
dockerfile: Dockerfile.tests-rpc
entrypoint: [
"pytest",
"-m", "wallet",
"-m", "wallet or ethclient",
"--rpc_url=http://status-go:3333",
"--rpc_url_2=http://status-go-no-funds:3333",
"--anvil_url=http://anvil:8545",
Expand Down
1 change: 1 addition & 0 deletions tests-functional/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ markers =
tx
wakuext
accounts
ethclient
Loading