Skip to content

Commit

Permalink
Merge pull request onflow#6802 from The-K-R-O-K/UlyanaAndrukhiv/6775-…
Browse files Browse the repository at this point in the history
…websocket-responses

[Access] Make WebSocket responses from data providers consistent with Access REST API responses
  • Loading branch information
peterargue authored Jan 13, 2025
2 parents 6a7e0c7 + a7a4c00 commit 23c5222
Show file tree
Hide file tree
Showing 82 changed files with 830 additions and 271 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ generate-mocks: install-mock-generators
mockery --name 'BlockTracker' --dir="./engine/access/subscription" --case=underscore --output="./engine/access/subscription/mock" --outpkg="mock"
mockery --name 'DataProvider' --dir="./engine/access/rest/websockets/data_providers" --case=underscore --output="./engine/access/rest/websockets/data_providers/mock" --outpkg="mock"
mockery --name 'DataProviderFactory' --dir="./engine/access/rest/websockets/data_providers" --case=underscore --output="./engine/access/rest/websockets/data_providers/mock" --outpkg="mock"
mockery --name 'LinkGenerator' --dir="./engine/access/rest/common/models" --case=underscore --output="./engine/access/rest/common/models/mock" --outpkg="mock"
mockery --name 'WebsocketConnection' --dir="./engine/access/rest/websockets" --case=underscore --output="./engine/access/rest/websockets/mock" --outpkg="mock"
mockery --name 'ExecutionDataTracker' --dir="./engine/access/subscription" --case=underscore --output="./engine/access/subscription/mock" --outpkg="mock"
mockery --name 'ConnectionFactory' --dir="./engine/access/rpc/connection" --case=underscore --output="./engine/access/rpc/connection/mock" --outpkg="mock"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"github.com/onflow/flow-go/model/flow"
)

const ExpandableFieldPayload = "payload"
const ExpandableExecutionResult = "execution_result"

func (b *Block) Build(
block *flow.Block,
execResult *flow.ExecutionResult,
Expand All @@ -23,7 +26,6 @@ func (b *Block) Build(

// add the payload to the response if it is specified as an expandable field
b.Expandable = &BlockExpandable{}
const ExpandableFieldPayload = "payload"
if expand[ExpandableFieldPayload] {
var payload BlockPayload
err := payload.Build(block.Payload)
Expand All @@ -43,7 +45,6 @@ func (b *Block) Build(
// execution result might not yet exist
if execResult != nil {
// add the execution result to the response if it is specified as an expandable field
const ExpandableExecutionResult = "execution_result"
if expand[ExpandableExecutionResult] {
var exeResult ExecutionResult
err := exeResult.Build(execResult, link)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
223 changes: 223 additions & 0 deletions engine/access/rest/common/models/mock/link_generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions engine/access/rest/common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Decorate(r *http.Request, chain flow.Chain) *Request {
}

if expandFields, found := middleware.GetFieldsToExpand(r); found {
decoratedReq.ExpandFields = sliceToMap(expandFields)
decoratedReq.ExpandFields = SliceToMap(expandFields)
}

if selectFields, found := middleware.GetFieldsToSelect(r); found {
Expand All @@ -64,14 +64,6 @@ func Decorate(r *http.Request, chain flow.Chain) *Request {
return decoratedReq
}

func sliceToMap(values []string) map[string]bool {
valueMap := make(map[string]bool, len(values))
for _, v := range values {
valueMap[v] = true
}
return valueMap
}

func toStringArray(in string) []string {
// currently, the swagger generated Go REST client is incorrectly doing a `fmt.Sprintf("%v", id)` for the id slice
// resulting in the client sending the ids in the format [id1 id2 id3...]. This is a temporary workaround to
Expand Down
11 changes: 11 additions & 0 deletions engine/access/rest/common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common

// SliceToMap converts a slice of strings into a map where each string
// in the slice becomes a key in the map with the value set to true.
func SliceToMap(values []string) map[string]bool {
valueMap := make(map[string]bool, len(values))
for _, v := range values {
valueMap[v] = true
}
return valueMap
}
2 changes: 1 addition & 1 deletion engine/access/rest/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/engine/access/rest/http/models"
"github.com/onflow/flow-go/engine/access/rest/common/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)
Expand Down
5 changes: 3 additions & 2 deletions engine/access/rest/http/models/account.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package models

import (
"github.com/onflow/flow-go/engine/access/rest/common/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)

const expandableKeys = "keys"
const expandableContracts = "contracts"

func (a *Account) Build(flowAccount *flow.Account, link LinkGenerator, expand map[string]bool) error {
func (a *Account) Build(flowAccount *flow.Account, link models.LinkGenerator, expand map[string]bool) error {
a.Address = flowAccount.Address.String()
a.Balance = util.FromUint(flowAccount.Balance)
a.Expandable = &AccountExpandable{}
Expand All @@ -31,7 +32,7 @@ func (a *Account) Build(flowAccount *flow.Account, link LinkGenerator, expand ma
a.Expandable.Contracts = expandableContracts
}

var self Links
var self models.Links
err := self.Build(link.AccountLink(a.Address))
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion engine/access/rest/http/models/model_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
*/
package models

import (
"github.com/onflow/flow-go/engine/access/rest/common/models"
)

type Account struct {
Address string `json:"address"`
// Flow balance of the account.
Balance string `json:"balance"`
Keys []AccountPublicKey `json:"keys,omitempty"`
Contracts map[string]string `json:"contracts,omitempty"`
Expandable *AccountExpandable `json:"_expandable"`
Links *Links `json:"_links,omitempty"`
Links *models.Links `json:"_links,omitempty"`
}
16 changes: 10 additions & 6 deletions engine/access/rest/http/models/model_transactions_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
*/
package models

import (
"github.com/onflow/flow-go/engine/access/rest/common/models"
)

type TransactionsBody struct {
// Base64 encoded content of the Cadence script.
Script string `json:"script"`
// An array containing arguments each encoded as Base64 passed in the [JSON-Cadence interchange format](https://docs.onflow.org/cadence/json-cadence-spec/).
Arguments []string `json:"arguments"`
ReferenceBlockId string `json:"reference_block_id"`
// The limit on the amount of computation a transaction is allowed to preform.
GasLimit string `json:"gas_limit"`
Payer string `json:"payer"`
ProposalKey *ProposalKey `json:"proposal_key"`
Authorizers []string `json:"authorizers"`
PayloadSignatures []TransactionSignature `json:"payload_signatures"`
EnvelopeSignatures []TransactionSignature `json:"envelope_signatures"`
GasLimit string `json:"gas_limit"`
Payer string `json:"payer"`
ProposalKey *models.ProposalKey `json:"proposal_key"`
Authorizers []string `json:"authorizers"`
PayloadSignatures []models.TransactionSignature `json:"payload_signatures"`
EnvelopeSignatures []models.TransactionSignature `json:"envelope_signatures"`
}
2 changes: 1 addition & 1 deletion engine/access/rest/http/request/proposal_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package request
import (
"fmt"

"github.com/onflow/flow-go/engine/access/rest/http/models"
"github.com/onflow/flow-go/engine/access/rest/common/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)
Expand Down
2 changes: 1 addition & 1 deletion engine/access/rest/http/request/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/onflow/flow-go/engine/access/rest/http/models"
"github.com/onflow/flow-go/engine/access/rest/common/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)
Expand Down
Loading

0 comments on commit 23c5222

Please sign in to comment.