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

Improve block handling in remote store #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ GOPATH ?= $(HOME)/go
install-tools:
mkdir -p ${GOPATH}; \
cd ${GOPATH}; \
GO111MODULE=on go install github.com/golang/mock/mockgen@v1.6.0; \
GO111MODULE=on go install go.uber.org/mock/mockgen@v0.3.0; \
Copy link
Contributor Author

@peterargue peterargue Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github.com/golang/mock is deprecated

GO111MODULE=on go install github.com/axw/gocov/gocov@latest; \
GO111MODULE=on go install github.com/matm/gocov-html@latest; \
GO111MODULE=on go install github.com/sanderhahn/gozip/cmd/gozip@latest;
Expand Down Expand Up @@ -46,6 +46,7 @@ generate: generate-mocks
generate-mocks:
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=emulator/mocks/emulator.go -package=mocks github.com/onflow/flow-emulator/emulator Emulator
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=storage/mocks/store.go -package=mocks github.com/onflow/flow-emulator/storage Store
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=internal/mocks/archive.go -package=mocks github.com/onflow/flow-emulator/internal ArchiveAPIClient

.PHONY: ci
ci: install-tools test check-tidy test coverage check-headers
Expand Down
61 changes: 31 additions & 30 deletions adapters/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ package adapters
import (
"context"
"fmt"
"github.com/onflow/flow/protobuf/go/flow/entities"

jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flow-emulator/types"
"github.com/onflow/flow-emulator/utils"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -36,7 +36,7 @@ import (
flowgo "github.com/onflow/flow-go/model/flow"
)

var _ access.API = &AccessAdapter{}
var _ access.API = (*AccessAdapter)(nil)

// AccessAdapter wraps the emulator adapters to be compatible with access.API.
type AccessAdapter struct {
Expand All @@ -52,22 +52,22 @@ func NewAccessAdapter(logger *zerolog.Logger, emulator emulator.Emulator) *Acces
}
}

func convertError(err error) error {
func convertError(err error, defaultStatusCode codes.Code) error {
if err != nil {
switch err.(type) {
case types.InvalidArgumentError:
return status.Error(codes.InvalidArgument, err.Error())
case types.NotFoundError:
return status.Error(codes.NotFound, err.Error())
default:
return status.Error(codes.Internal, err.Error())
return status.Error(defaultStatusCode, err.Error())
}
}
return nil
}

func (a *AccessAdapter) Ping(_ context.Context) error {
return convertError(a.emulator.Ping())
return convertError(a.emulator.Ping(), codes.Internal)
}

func (a *AccessAdapter) GetNetworkParameters(_ context.Context) access.NetworkParameters {
Expand All @@ -77,7 +77,7 @@ func (a *AccessAdapter) GetNetworkParameters(_ context.Context) access.NetworkPa
func (a *AccessAdapter) GetLatestBlockHeader(_ context.Context, _ bool) (*flowgo.Header, flowgo.BlockStatus, error) {
block, err := a.emulator.GetLatestBlock()
if err != nil {
return nil, flowgo.BlockStatusUnknown, convertError(err)
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
}

a.logger.Debug().Fields(map[string]any{
Expand All @@ -91,7 +91,7 @@ func (a *AccessAdapter) GetLatestBlockHeader(_ context.Context, _ bool) (*flowgo
func (a *AccessAdapter) GetBlockHeaderByHeight(_ context.Context, height uint64) (*flowgo.Header, flowgo.BlockStatus, error) {
block, err := a.emulator.GetBlockByHeight(height)
if err != nil {
return nil, flowgo.BlockStatusUnknown, convertError(err)
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
}

a.logger.Debug().Fields(map[string]any{
Expand All @@ -105,7 +105,7 @@ func (a *AccessAdapter) GetBlockHeaderByHeight(_ context.Context, height uint64)
func (a *AccessAdapter) GetBlockHeaderByID(_ context.Context, id flowgo.Identifier) (*flowgo.Header, flowgo.BlockStatus, error) {
block, err := a.emulator.GetBlockByID(id)
if err != nil {
return nil, flowgo.BlockStatusUnknown, convertError(err)
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
}

a.logger.Debug().Fields(map[string]any{
Expand All @@ -119,7 +119,7 @@ func (a *AccessAdapter) GetBlockHeaderByID(_ context.Context, id flowgo.Identifi
func (a *AccessAdapter) GetLatestBlock(_ context.Context, _ bool) (*flowgo.Block, flowgo.BlockStatus, error) {
block, err := a.emulator.GetLatestBlock()
if err != nil {
return nil, flowgo.BlockStatusUnknown, convertError(err)
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
}

a.logger.Debug().Fields(map[string]any{
Expand All @@ -133,7 +133,7 @@ func (a *AccessAdapter) GetLatestBlock(_ context.Context, _ bool) (*flowgo.Block
func (a *AccessAdapter) GetBlockByHeight(_ context.Context, height uint64) (*flowgo.Block, flowgo.BlockStatus, error) {
block, err := a.emulator.GetBlockByHeight(height)
if err != nil {
return nil, flowgo.BlockStatusUnknown, convertError(err)
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
}

a.logger.Debug().Fields(map[string]any{
Expand All @@ -147,7 +147,7 @@ func (a *AccessAdapter) GetBlockByHeight(_ context.Context, height uint64) (*flo
func (a *AccessAdapter) GetBlockByID(_ context.Context, id flowgo.Identifier) (*flowgo.Block, flowgo.BlockStatus, error) {
block, err := a.emulator.GetBlockByID(id)
if err != nil {
return nil, flowgo.BlockStatusUnknown, convertError(err)
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
}

a.logger.Debug().Fields(map[string]any{
Expand All @@ -161,7 +161,7 @@ func (a *AccessAdapter) GetBlockByID(_ context.Context, id flowgo.Identifier) (*
func (a *AccessAdapter) GetCollectionByID(_ context.Context, id flowgo.Identifier) (*flowgo.LightCollection, error) {
collection, err := a.emulator.GetCollectionByID(id)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

a.logger.Debug().
Expand All @@ -174,7 +174,7 @@ func (a *AccessAdapter) GetCollectionByID(_ context.Context, id flowgo.Identifie
func (a *AccessAdapter) GetTransaction(_ context.Context, id flowgo.Identifier) (*flowgo.TransactionBody, error) {
tx, err := a.emulator.GetTransaction(id)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

a.logger.Debug().
Expand All @@ -196,16 +196,17 @@ func (a *AccessAdapter) GetTransactionResult(
) {
result, err := a.emulator.GetTransactionResult(id)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

// Convert CCF events to JSON events, else return CCF encoded version
if requiredEventEncodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
result.Events, err = ConvertCCFEventsToJsonEvents(result.Events)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
}

a.logger.Debug().
Str("txID", id.String()).
Msg("📝 GetTransactionResult called")
Expand All @@ -216,7 +217,7 @@ func (a *AccessAdapter) GetTransactionResult(
func (a *AccessAdapter) GetAccount(_ context.Context, address flowgo.Address) (*flowgo.Account, error) {
account, err := a.emulator.GetAccount(address)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

a.logger.Debug().
Expand All @@ -229,7 +230,7 @@ func (a *AccessAdapter) GetAccount(_ context.Context, address flowgo.Address) (*
func (a *AccessAdapter) GetAccountAtLatestBlock(ctx context.Context, address flowgo.Address) (*flowgo.Account, error) {
account, err := a.GetAccount(ctx, address)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

a.logger.Debug().
Expand All @@ -252,14 +253,14 @@ func (a *AccessAdapter) GetAccountAtBlockHeight(

account, err := a.emulator.GetAccountAtBlockHeight(address, height)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
return account, nil
}

func convertScriptResult(result *types.ScriptResult, err error) ([]byte, error) {
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
return nil, convertError(err, codes.InvalidArgument)
}

if !result.Succeeded() {
Expand Down Expand Up @@ -331,7 +332,7 @@ func (a *AccessAdapter) GetEventsForHeightRange(
) ([]flowgo.BlockEvents, error) {
events, err := a.emulator.GetEventsForHeightRange(eventType, startHeight, endHeight)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

eventCount := 0
Expand All @@ -342,7 +343,7 @@ func (a *AccessAdapter) GetEventsForHeightRange(
events[i].Events, err = ConvertCCFEventsToJsonEvents(events[i].Events)
eventCount = eventCount + len(events[i].Events)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
}
}
Expand All @@ -365,7 +366,7 @@ func (a *AccessAdapter) GetEventsForBlockIDs(
) ([]flowgo.BlockEvents, error) {
events, err := a.emulator.GetEventsForBlockIDs(eventType, blockIDs)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

eventCount := 0
Expand All @@ -376,7 +377,7 @@ func (a *AccessAdapter) GetEventsForBlockIDs(
events[i].Events, err = ConvertCCFEventsToJsonEvents(events[i].Events)
eventCount = eventCount + len(events[i].Events)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
}
}
Expand Down Expand Up @@ -409,18 +410,18 @@ func (a *AccessAdapter) GetTransactionResultByIndex(
) (*access.TransactionResult, error) {
results, err := a.emulator.GetTransactionResultsByBlockID(blockID)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
if len(results) <= int(index) {
return nil, convertError(&types.TransactionNotFoundError{ID: flowgo.Identifier{}})
return nil, convertError(&types.TransactionNotFoundError{ID: flowgo.Identifier{}}, codes.Internal)
}

// Convert CCF events to JSON events, else return CCF encoded version
if requiredEventEncodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
for i := range results {
results[i].Events, err = ConvertCCFEventsToJsonEvents(results[i].Events)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
}
}
Expand All @@ -431,7 +432,7 @@ func (a *AccessAdapter) GetTransactionResultByIndex(
func (a *AccessAdapter) GetTransactionsByBlockID(_ context.Context, blockID flowgo.Identifier) ([]*flowgo.TransactionBody, error) {
result, err := a.emulator.GetTransactionsByBlockID(blockID)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
return result, nil
}
Expand All @@ -443,15 +444,15 @@ func (a *AccessAdapter) GetTransactionResultsByBlockID(
) ([]*access.TransactionResult, error) {
result, err := a.emulator.GetTransactionResultsByBlockID(blockID)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}

// Convert CCF events to JSON events, else return CCF encoded version
if requiredEventEncodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
for i := range result {
result[i].Events, err = ConvertCCFEventsToJsonEvents(result[i].Events)
if err != nil {
return nil, convertError(err)
return nil, convertError(err, codes.Internal)
}
}
}
Expand All @@ -464,7 +465,7 @@ func (a *AccessAdapter) SendTransaction(_ context.Context, tx *flowgo.Transactio
Str("txID", tx.ID().String()).
Msg(`✉️ Transaction submitted`)

return convertError(a.emulator.SendTransaction(tx))
return convertError(a.emulator.SendTransaction(tx), codes.Internal)
}

func (a *AccessAdapter) GetNodeVersionInfo(
Expand Down
14 changes: 7 additions & 7 deletions adapters/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ import (
"fmt"
"testing"

"github.com/onflow/flow/protobuf/go/flow/entities"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/golang/mock/gomock"
"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
"github.com/onflow/flow-emulator/emulator/mocks"
"github.com/onflow/flow-emulator/types"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/common/rpc/convert"
flowgo "github.com/onflow/flow-go/model/flow"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/onflow/flow/protobuf/go/flow/entities"

"github.com/onflow/flow-emulator/emulator/mocks"
"github.com/onflow/flow-emulator/types"
)

func accessTest(f func(t *testing.T, adapter *AccessAdapter, emu *mocks.MockEmulator)) func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion adapters/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/onflow/cadence"
"github.com/onflow/flow-emulator/convert"
"github.com/onflow/flow-emulator/emulator/mocks"
Expand All @@ -33,6 +32,7 @@ import (
flowgo "github.com/onflow/flow-go/model/flow"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func sdkTest(f func(t *testing.T, adapter *SDKAdapter, emu *mocks.MockEmulator)) func(t *testing.T) {
Expand Down
Loading
Loading