Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
nvdtf committed Sep 18, 2023
2 parents fb5b4a9 + bcc8566 commit b796857
Show file tree
Hide file tree
Showing 45 changed files with 508 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ updates:
schedule:
interval: "weekly"
ignore:
- dependency-name: "github.com/onflow/flow-go/crypto"
- dependency-name: "github.com/onflow/crypto"
- dependency-name: "github.com/onflow/cadence"
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
branches:
- master
- 'v**'
pull_request:
branches:
- master
- 'v**'

jobs:
test:
Expand Down
3 changes: 3 additions & 0 deletions access/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Client interface {
// Ping is used to check if the access node is alive and healthy.
Ping(ctx context.Context) error

// GetNetworkParameters gets the network parameters.
GetNetworkParameters(ctx context.Context) (*flow.NetworkParameters, error)

// GetLatestBlockHeader gets the latest sealed or unsealed block header.
GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.BlockHeader, error)

Expand Down
7 changes: 7 additions & 0 deletions access/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (c *Client) Ping(ctx context.Context) error {
return c.grpc.Ping(ctx)
}

func (c *Client) GetNetworkParameters(ctx context.Context) (*flow.NetworkParameters, error) {
return c.grpc.GetNetworkParameters(ctx)
}

func (c *Client) GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.BlockHeader, error) {
return c.grpc.GetLatestBlockHeader(ctx, isSealed)
}
Expand Down Expand Up @@ -110,6 +114,9 @@ func (c *Client) GetTransactionResult(ctx context.Context, txID flow.Identifier)
return c.grpc.GetTransactionResult(ctx, txID)
}

func (c *Client) GetTransactionResultByIndex(ctx context.Context, blockID flow.Identifier, index uint32) (*flow.TransactionResult, error) {
return c.grpc.GetTransactionResultByIndex(ctx, blockID, index)
}
func (c *Client) GetTransactionResultsByBlockID(ctx context.Context, blockID flow.Identifier) ([]*flow.TransactionResult, error) {
return c.grpc.GetTransactionResultsByBlockID(ctx, blockID)
}
Expand Down
7 changes: 5 additions & 2 deletions access/grpc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
"fmt"
"time"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
Expand Down Expand Up @@ -436,7 +437,7 @@ func messageToTransaction(m *entities.Transaction) (flow.Transaction, error) {

t.SetScript(m.GetScript())
t.SetReferenceBlockID(flow.HashToID(m.GetReferenceBlockId()))
t.SetGasLimit(m.GetGasLimit())
t.SetComputeLimit(m.GetGasLimit())

for _, arg := range m.GetArguments() {
t.AddRawArgument(arg)
Expand Down Expand Up @@ -502,6 +503,7 @@ func transactionResultToMessage(result flow.TransactionResult) (*access.Transact
BlockId: identifierToMessage(result.BlockID),
BlockHeight: result.BlockHeight,
TransactionId: identifierToMessage(result.TransactionID),
CollectionId: identifierToMessage(result.CollectionID),
}, nil
}

Expand Down Expand Up @@ -537,6 +539,7 @@ func messageToTransactionResult(m *access.TransactionResultResponse, options []j
BlockID: flow.BytesToID(m.GetBlockId()),
BlockHeight: m.GetBlockHeight(),
TransactionID: flow.BytesToID(m.GetTransactionId()),
CollectionID: flow.BytesToID(m.GetCollectionId()),
}, nil
}

Expand Down
34 changes: 34 additions & 0 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func (c *BaseClient) Ping(ctx context.Context, opts ...grpc.CallOption) error {
return err
}

func (c *BaseClient) GetNetworkParameters(ctx context.Context, opts ...grpc.CallOption) (*flow.NetworkParameters, error) {
res, err := c.rpcClient.GetNetworkParameters(ctx, &access.GetNetworkParametersRequest{}, opts...)
if err != nil {
return nil, newRPCError(err)
}
return &flow.NetworkParameters{
ChainID: flow.ChainID(res.ChainId),
}, nil
}

func (c *BaseClient) GetLatestBlockHeader(
ctx context.Context,
isSealed bool,
Expand Down Expand Up @@ -332,6 +342,30 @@ func (c *BaseClient) GetTransactionResult(
return &result, nil
}

func (c *BaseClient) GetTransactionResultByIndex(
ctx context.Context,
blockID flow.Identifier,
index uint32,
opts ...grpc.CallOption,
) (*flow.TransactionResult, error) {

req := &access.GetTransactionByIndexRequest{
BlockId: blockID.Bytes(),
Index: index,
}

res, err := c.rpcClient.GetTransactionResultByIndex(ctx, req, opts...)
if err != nil {
return nil, newRPCError(err)
}

parsed, err := messageToTransactionResult(res, c.jsonOptions)
if err != nil {
return nil, newMessageToEntityError(entityTransactionResult, err)
}
return &parsed, nil
}

func (c *BaseClient) GetTransactionResultsByBlockID(
ctx context.Context,
blockID flow.Identifier,
Expand Down
28 changes: 28 additions & 0 deletions access/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ func TestClient_Ping(t *testing.T) {
}))
}

func TestClient_GetNetworkParameters(t *testing.T) {
t.Run("Success", clientTest(func(t *testing.T, ctx context.Context, rpc *MockRPCClient, c *BaseClient) {
response := &access.GetNetworkParametersResponse{
ChainId: "flow-testnet",
}
expectedParams := &flow.NetworkParameters{
ChainID: flow.ChainID("flow-testnet"),
}

rpc.On("GetNetworkParameters", ctx, mock.Anything).Return(response, nil)

params, err := c.GetNetworkParameters(ctx)
require.NoError(t, err)

assert.Equal(t, params, expectedParams)
}))

t.Run("Internal error", clientTest(func(t *testing.T, ctx context.Context, rpc *MockRPCClient, c *BaseClient) {
rpc.On("GetNetworkParameters", ctx, mock.Anything).
Return(nil, errInternal)

params, err := c.GetNetworkParameters(ctx)
assert.Error(t, err)
assert.Equal(t, codes.Internal, status.Code(err))
assert.Nil(t, params)
}))
}

func TestClient_GetLatestBlockHeader(t *testing.T) {
blocks := test.BlockGenerator()

Expand Down
4 changes: 4 additions & 0 deletions access/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (c *Client) Ping(ctx context.Context) error {
return c.httpClient.Ping(ctx)
}

func (c *Client) GetNetworkParameters(ctx context.Context) (*flow.NetworkParameters, error) {
return c.httpClient.GetNetworkParameters(ctx)
}

func (c *Client) GetBlockByID(ctx context.Context, blockID flow.Identifier) (*flow.Block, error) {
return c.httpClient.GetBlockByID(ctx, blockID)
}
Expand Down
31 changes: 31 additions & 0 deletions access/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ func TestClient_Factories(t *testing.T) {
assert.NotNil(t, client)
}

func TestBaseClient_GetNetworkParameters(t *testing.T) {
const handlerName = "getNetworkParameters"

t.Run("Success", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
httpParams := networkParametersFlowFixture()
expectedParams := toNetworkParameters(&httpParams)

handler.
On(handlerName, mock.Anything).
Return(&httpParams, nil)

params, err := client.GetNetworkParameters(ctx)
assert.NoError(t, err)
assert.Equal(t, params, expectedParams)
}))

t.Run("Failure", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
handler.
On(handlerName, mock.Anything).
Return(nil, HTTPError{
Url: "/",
Code: 400,
Message: "bad request",
})

params, err := client.GetNetworkParameters(ctx)
assert.EqualError(t, err, "bad request")
assert.Nil(t, params)
}))
}

func TestBaseClient_GetBlockByID(t *testing.T) {
const handlerName = "getBlockByID"
t.Run("Success", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
Expand Down
7 changes: 7 additions & 0 deletions access/http/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func toTransactionResult(txr *models.TransactionResult, options []cadenceJSON.Op
Error: txErr,
Events: events,
BlockID: flow.HexToID(txr.BlockId),
CollectionID: flow.HexToID(txr.CollectionId),
}, nil
}

Expand Down Expand Up @@ -440,3 +441,9 @@ func toExecutionResults(result models.ExecutionResult) *flow.ExecutionResult {
ServiceEvents: events,
}
}

func toNetworkParameters(params *models.NetworkParameters) *flow.NetworkParameters {
return &flow.NetworkParameters{
ChainID: flow.ChainID(params.ChainId),
}
}
2 changes: 2 additions & 0 deletions access/http/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func Test_ConvertTransactionResult(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, txr.Events[0].Payload, payload)
assert.Equal(t, txr.Events[0].TransactionID.String(), httpTxr.Events[0].TransactionId)
assert.Equal(t, txr.BlockID.String(), httpTxr.BlockId)
assert.Equal(t, txr.CollectionID.String(), httpTxr.CollectionId)
assert.Equal(t, fmt.Sprintf("%d", txr.Events[0].TransactionIndex), httpTxr.Events[0].TransactionIndex)
}

Expand Down
7 changes: 7 additions & 0 deletions access/http/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func accountKeyFlowFixture() models.AccountPublicKey {
}
}

func networkParametersFlowFixture() models.NetworkParameters {
return models.NetworkParameters{
ChainId: "flow-testnet",
}
}

func blockFlowFixture() models.Block {
block := test.BlockGenerator().New()

Expand Down Expand Up @@ -147,6 +153,7 @@ func transactionResultFlowFixture() models.TransactionResult {

return models.TransactionResult{
BlockId: txr.BlockID.String(),
CollectionId: txr.CollectionID.String(),
Status: &status,
StatusCode: 0,
ErrorMessage: txr.Error.Error(),
Expand Down
10 changes: 10 additions & 0 deletions access/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ func (h *httpHandler) post(_ context.Context, url *url.URL, body []byte, model i
return nil
}

func (h *httpHandler) getNetworkParameters(ctx context.Context, opts ...queryOpts) (*models.NetworkParameters, error) {
var networkParameters models.NetworkParameters
err := h.get(ctx, h.mustBuildURL("/network/parameters", opts...), &networkParameters)
if err != nil {
return nil, errors.Wrap(err, "get network parameters failed")
}

return &networkParameters, nil
}

func (h *httpHandler) getBlockByID(ctx context.Context, ID string, opts ...queryOpts) (*models.Block, error) {
u := h.mustBuildURL(fmt.Sprintf("/blocks/%s", ID), opts...)

Expand Down
30 changes: 30 additions & 0 deletions access/http/handler_mock_test.go

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

10 changes: 10 additions & 0 deletions access/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

// handler interface defines methods needed to be offered by a specific http network implementation.
type handler interface {
getNetworkParameters(ctx context.Context, opts ...queryOpts) (*models.NetworkParameters, error)
getBlockByID(ctx context.Context, ID string, opts ...queryOpts) (*models.Block, error)
getBlocksByHeights(ctx context.Context, heights string, startHeight string, endHeight string, opts ...queryOpts) ([]*models.Block, error)
getAccount(ctx context.Context, address string, height string, opts ...queryOpts) (*models.Account, error)
Expand Down Expand Up @@ -186,6 +187,15 @@ func (c *BaseClient) Ping(ctx context.Context) error {
return nil
}

func (c *BaseClient) GetNetworkParameters(ctx context.Context) (*flow.NetworkParameters, error) {
params, err := c.handler.getNetworkParameters(ctx)
if err != nil {
return nil, err
}

return toNetworkParameters(params), nil
}

func (c *BaseClient) GetBlockByID(ctx context.Context, blockID flow.Identifier, opts ...queryOpts) (*flow.Block, error) {
block, err := c.handler.getBlockByID(ctx, blockID.String())
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions access/http/models/model_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
package models

type Chunk struct {
BlockId string `json:"block_id"`
CollectionIndex string `json:"collection_index"`
StartState string `json:"start_state"`
EndState string `json:"end_state"`
EventCollection string `json:"event_collection"`
Index string `json:"index"`
NumberOfTransactions string `json:"number_of_transactions"`
BlockId string `json:"block_id"`
TotalComputationUsed string `json:"total_computation_used"`
NumberOfTransactions string `json:"number_of_transactions"`
Index string `json:"index"`
EndState string `json:"end_state"`
}
6 changes: 3 additions & 3 deletions access/http/models/model_collection_guarantee.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package models

type CollectionGuarantee struct {
CollectionId string `json:"collection_id"`
SignerIds []string `json:"signer_ids"`
Signature string `json:"signature"`
CollectionId string `json:"collection_id"`
SignerIndices string `json:"signer_indices"`
Signature string `json:"signature"`
}
13 changes: 13 additions & 0 deletions access/http/models/model_network_parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Access API
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* API version: 1.0.0
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package models

type NetworkParameters struct {
ChainId string `json:"chain_id"`
}
Loading

0 comments on commit b796857

Please sign in to comment.