Skip to content

Commit

Permalink
Merge pull request #542 from onflow/gregor/client-reuse-storage
Browse files Browse the repository at this point in the history
Reuse client for storage query
  • Loading branch information
sideninja authored Sep 13, 2024
2 parents eb77ec3 + 67b557f commit 70fc871
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 50 deletions.
12 changes: 10 additions & 2 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/onflow/flow-go-sdk/crypto"
"github.com/rs/zerolog"
"github.com/sethvargo/go-limiter/memorystore"
grpcOpts "google.golang.org/grpc"

"github.com/onflow/flow-evm-gateway/api"
"github.com/onflow/flow-evm-gateway/config"
Expand Down Expand Up @@ -361,9 +362,16 @@ func startEngine(
// setupCrossSporkClient sets up a cross-spork AN client.
func setupCrossSporkClient(config *config.Config, logger zerolog.Logger) (*requester.CrossSporkClient, error) {
// create access client with cross-spork capabilities
currentSporkClient, err := grpc.NewClient(config.AccessNodeHost)
currentSporkClient, err := grpc.NewClient(
config.AccessNodeHost,
grpc.WithGRPCDialOptions(grpcOpts.WithDefaultCallOptions(grpcOpts.MaxCallRecvMsgSize(1024*1024*1024))),
)
if err != nil {
return nil, fmt.Errorf("failed to create client connection for host: %s, with error: %w", config.AccessNodeHost, err)
return nil, fmt.Errorf(
"failed to create client connection for host: %s, with error: %w",
config.AccessNodeHost,
err,
)
}

// if we provided access node previous spork hosts add them to the client
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/onflow/atree v0.8.0-rc.6
github.com/onflow/cadence v1.0.0-preview.52
github.com/onflow/flow-go v0.37.10
github.com/onflow/flow-go-sdk v1.0.0-preview.54
github.com/onflow/flow-go-sdk v1.0.0-preview.56
github.com/onflow/flow/protobuf/go/flow v0.4.6
github.com/onflow/go-ethereum v1.14.7
github.com/prometheus/client_golang v1.18.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1865,8 +1865,8 @@ github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyu
github.com/onflow/flow-go v0.37.10 h1:Nz2Gp63+0ubb9FuQaEZgCsXNXM5WsXq/j0ukC74N5Vw=
github.com/onflow/flow-go v0.37.10/go.mod h1:bfOCsCk0v1J93vXd+zrYkCmRIVOaL9oAXvNFWgVOujE=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-preview.54 h1:5GjCkyIyvE9KolOUUPTkGdEiV/8qOe1MGnLHOLBmthA=
github.com/onflow/flow-go-sdk v1.0.0-preview.54/go.mod h1:u9oFiS25TpnU1EW62PQlq22jzkwBAj4VEiiCBM6nhHo=
github.com/onflow/flow-go-sdk v1.0.0-preview.56 h1:ZnFznUXI1V8iZ+cKxoJRIeQwJTHItriKpnoKf8hFFso=
github.com/onflow/flow-go-sdk v1.0.0-preview.56/go.mod h1:rBRNboXaTprn7M0MeO6/R1bxNpctbrx66I2FLp0V6fM=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
Expand Down
19 changes: 5 additions & 14 deletions services/requester/remote_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,24 @@ package requester

import (
"context"
"fmt"

"github.com/onflow/atree"
"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow/protobuf/go/flow/executiondata"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)

var _ atree.Ledger = &remoteLedger{}

func newRemoteLedger(host string, cadenceHeight uint64) (*remoteLedger, error) {
conn, err := grpc.Dial(
host,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*1024)),
)
if err != nil {
return nil, fmt.Errorf("could not connect to rpc host: %s, with %w", host, err)
}

func newRemoteLedger(
client executiondata.ExecutionDataAPIClient,
cadenceHeight uint64,
) (*remoteLedger, error) {
return &remoteLedger{
execution: executiondata.NewExecutionDataAPIClient(conn),
execution: client,
height: cadenceHeight,
}, nil
}
Expand Down
37 changes: 16 additions & 21 deletions services/requester/remote_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
"os"
"testing"

grpcClient "github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go/fvm/evm"
"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/evm/types"
flowGo "github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow/protobuf/go/flow/access"
gethCommon "github.com/onflow/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

var previewnetStorageAddress = evm.StorageAccountAddress(flowGo.Previewnet)
Expand Down Expand Up @@ -55,14 +54,20 @@ func Benchmark_RemoteLedger_GetBalance(b *testing.B) {
b.Skip()
}

cadenceHeight, err := getPreviewnetLatestHeight(executionAPI)
client, err := grpcClient.NewClient(executionAPI,
grpcClient.WithGRPCDialOptions(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*1024))),
)
require.NoError(b, err)
execClient := client.ExecutionDataRPCClient()

latest, err := client.GetLatestBlockHeader(context.Background(), true)
require.NoError(b, err)

// we have to include ledger creation since the loading of the collection
// will be done only once per height, all the subsequent requests for
// getting the balance will work on already loaded state and thus be fast
for i := 0; i < b.N; i++ {
ledger, err := newRemoteLedger(executionAPI, cadenceHeight)
ledger, err := newRemoteLedger(execClient, latest.Height)
require.NoError(b, err)

stateDB, err := state.NewStateDB(ledger, previewnetStorageAddress)
Expand All @@ -77,28 +82,18 @@ func Benchmark_RemoteLedger_GetBalance(b *testing.B) {
}

func newPreviewnetLedger(host string) (*remoteLedger, error) {
cadenceHeight, err := getPreviewnetLatestHeight(host)
client, err := grpcClient.NewClient(host,
grpcClient.WithGRPCDialOptions(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*1024))),
)
if err != nil {
return nil, err
}
execClient := client.ExecutionDataRPCClient()

return newRemoteLedger(host, cadenceHeight)
}

func getPreviewnetLatestHeight(host string) (uint64, error) {
conn, err := grpc.Dial(
host,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*1024)),
)
latest, err := client.GetLatestBlockHeader(context.Background(), true)
if err != nil {
return 0, err
}
client := access.NewAccessAPIClient(conn)
res, err := client.GetLatestBlockHeader(context.Background(), &access.GetLatestBlockHeaderRequest{IsSealed: true})
if err != nil {
return 0, err
return nil, err
}

return res.Block.Height, nil
return newRemoteLedger(execClient, latest.Height)
}
7 changes: 6 additions & 1 deletion services/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/onflow/flow-go/fvm/evm"
"github.com/onflow/flow-go/fvm/evm/emulator"
Expand Down Expand Up @@ -447,7 +448,11 @@ func (e *EVM) stateAt(evmHeight int64) (*state.StateDB, error) {
cadenceHeight = h.Height
}

ledger, err := newRemoteLedger(e.config.AccessNodeHost, cadenceHeight)
exeClient, ok := e.client.Client.(*grpc.Client)
if !ok {
return nil, fmt.Errorf("could not convert to execution client")
}
ledger, err := newRemoteLedger(exeClient.ExecutionDataRPCClient(), cadenceHeight)
if err != nil {
return nil, fmt.Errorf("could not create remote ledger for height: %d, with: %w", cadenceHeight, err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/onflow/flow-emulator v1.0.0
github.com/onflow/flow-evm-gateway v0.0.0-20240201154855-4d4d3d3f19c7
github.com/onflow/flow-go v0.37.10
github.com/onflow/flow-go-sdk v1.0.0-preview.54
github.com/onflow/flow-go-sdk v1.0.0-preview.56
github.com/onflow/go-ethereum v1.14.7
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.9.0
Expand Down
12 changes: 4 additions & 8 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,8 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7/go.mod h1:5GuXa7vkL8u9FkFuWdVvfR5ix8hRB7DbOAaYULamFpc=
github.com/holiman/billy v0.0.0-20240216141850-2abb0c79d3c4 h1:X4egAf/gcS1zATw6wn4Ej8vjuVGxeHdan+bRb2ebyv4=
github.com/holiman/billy v0.0.0-20240216141850-2abb0c79d3c4/go.mod h1:5GuXa7vkL8u9FkFuWdVvfR5ix8hRB7DbOAaYULamFpc=
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/uint256 v1.2.0/go.mod h1:y4ga/t+u+Xwd7CpDgZESaRcWy0I7XMlTMA25ApIH5Jw=
Expand Down Expand Up @@ -2076,8 +2078,6 @@ github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f/go.mod h1:xvP61FoOs
github.com/onflow/atree v0.8.0-rc.6 h1:GWgaylK24b5ta2Hq+TvyOF7X5tZLiLzMMn7lEt59fsA=
github.com/onflow/atree v0.8.0-rc.6/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/Ct1AA5zBo=
github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8=
github.com/onflow/cadence v1.0.0-preview.51 h1:L+toCS2Sw9bsExc2PxeNMmAK96fn2LdTOD9bl5K/etA=
github.com/onflow/cadence v1.0.0-preview.51/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/cadence v1.0.0-preview.52 h1:hZ92e6lL2+PQa3C1i5jJh0zZYFdW89+X1MS0Bkd6Ayo=
github.com/onflow/cadence v1.0.0-preview.52/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
Expand All @@ -2087,21 +2087,17 @@ github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.1 h1:q9tXLIALwQ76bO4
github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.1/go.mod h1:u/mkP/B+PbV33tEG3qfkhhBlydSvAKxfLZSfB4lsJHg=
github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1 h1:FfhMBAb78p6VAWkJ+iqdKLErGQVQgxk5w6DP5ZruWX8=
github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1/go.mod h1:NgbMOYnMh0GN48VsNKZuiwK7uyk38Wyo8jN9+C9QE30=
github.com/onflow/flow-emulator v1.0.0-preview.42 h1:2uMsoKo7wfZOd50GanR7wIoRxpDFErV17wt6/YaeVRo=
github.com/onflow/flow-emulator v1.0.0-preview.42/go.mod h1:qCT9cAsrtqKHjTmEujihHPH2RfEiL6wNbMqCbmN7HMo=
github.com/onflow/flow-emulator v1.0.0 h1:CCE9mFUYidb4YPQWFSBHzcBGggs5bXVqIh02wF2wRr0=
github.com/onflow/flow-emulator v1.0.0/go.mod h1:sHbe9e1RG7Y6LA/dFyLEoBnKyjJ4iHeOdkXIobMjjrE=
github.com/onflow/flow-ft/lib/go/contracts v1.0.0 h1:mToacZ5NWqtlWwk/7RgIl/jeKB/Sy/tIXdw90yKHcV0=
github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs=
github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
github.com/onflow/flow-go v0.37.7-0.20240830182756-9ac9e1889c34 h1:5yHX09MtQxNvt2wT5KV1fz+1/iWvBl2IkDr5svGiFN0=
github.com/onflow/flow-go v0.37.7-0.20240830182756-9ac9e1889c34/go.mod h1:HFx3KzQeT+u3SoCGaD5zXyK7VF7SRI61P0aO2SDjaro=
github.com/onflow/flow-go v0.37.10 h1:Nz2Gp63+0ubb9FuQaEZgCsXNXM5WsXq/j0ukC74N5Vw=
github.com/onflow/flow-go v0.37.10/go.mod h1:bfOCsCk0v1J93vXd+zrYkCmRIVOaL9oAXvNFWgVOujE=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-preview.54 h1:5GjCkyIyvE9KolOUUPTkGdEiV/8qOe1MGnLHOLBmthA=
github.com/onflow/flow-go-sdk v1.0.0-preview.54/go.mod h1:u9oFiS25TpnU1EW62PQlq22jzkwBAj4VEiiCBM6nhHo=
github.com/onflow/flow-go-sdk v1.0.0-preview.56 h1:ZnFznUXI1V8iZ+cKxoJRIeQwJTHItriKpnoKf8hFFso=
github.com/onflow/flow-go-sdk v1.0.0-preview.56/go.mod h1:rBRNboXaTprn7M0MeO6/R1bxNpctbrx66I2FLp0V6fM=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
Expand Down

0 comments on commit 70fc871

Please sign in to comment.