Skip to content

Commit

Permalink
Merge pull request #13 from coinbase/patrick/server-ctx
Browse files Browse the repository at this point in the history
Add Servicer Context
  • Loading branch information
patrick-ogrady authored Apr 15, 2020
2 parents 8738190 + 8b5bfb3 commit 522fa82
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 25 deletions.
4 changes: 2 additions & 2 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func PartialBlockIdentifier(blockIdentifier *types.PartialBlockIdentifier) error
return errors.New("PartialBlockIdentifier is nil")
}

if blockIdentifier.Hash != nil && *blockIdentifier.Hash == "" {
if blockIdentifier.Hash != nil && *blockIdentifier.Hash != "" {
return nil
}

if blockIdentifier.Index != nil && *blockIdentifier.Index > 0 {
if blockIdentifier.Index != nil && *blockIdentifier.Index >= 0 {
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions asserter/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
Address: "acct1",
}

genesisBlockIndex = int64(0)
validBlockIndex = int64(1000)
validPartialBlockIdentifier = &types.PartialBlockIdentifier{
Index: &validBlockIndex,
Expand Down Expand Up @@ -114,6 +115,15 @@ func TestBlockRequest(t *testing.T) {
},
err: nil,
},
"valid request for block 0": {
request: &types.BlockRequest{
NetworkIdentifier: validNetworkIdentifier,
BlockIdentifier: &types.PartialBlockIdentifier{
Index: &genesisBlockIndex,
},
},
err: nil,
},
"nil request": {
request: nil,
err: errors.New("BlockRequest is nil"),
Expand Down
22 changes: 22 additions & 0 deletions examples/fetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,26 @@ func main() {
log.Fatal(err)
}
log.Printf("Current Block: %s\n", string(prettyBlock))

// Step 6: Get a range of blocks
blockMap, err := newFetcher.BlockRange(
ctx,
primaryNetwork,
networkStatus.GenesisBlockIdentifier.Index,
networkStatus.GenesisBlockIdentifier.Index+10,
)
if err != nil {
log.Fatal(err)
}

// Step 7: Print the block range
prettyBlockRange, err := json.MarshalIndent(
blockMap,
"",
" ",
)
if err != nil {
log.Fatal(err)
}
log.Printf("Block Range: %s\n", string(prettyBlockRange))
}
28 changes: 28 additions & 0 deletions examples/server/services/block_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package services

import (
"context"
"fmt"
"time"

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
)
Expand All @@ -33,8 +37,31 @@ func NewBlockAPIService(network *types.NetworkIdentifier) server.BlockAPIService

// Block implements the /block endpoint.
func (s *BlockAPIService) Block(
ctx context.Context,
request *types.BlockRequest,
) (*types.BlockResponse, *types.Error) {
if *request.BlockIdentifier.Index != 1000 {
previousBlockIndex := *request.BlockIdentifier.Index - 1
if previousBlockIndex < 0 {
previousBlockIndex = 0
}

return &types.BlockResponse{
Block: &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Index: *request.BlockIdentifier.Index,
Hash: fmt.Sprintf("block %d", *request.BlockIdentifier.Index),
},
ParentBlockIdentifier: &types.BlockIdentifier{
Index: previousBlockIndex,
Hash: fmt.Sprintf("block %d", previousBlockIndex),
},
Timestamp: time.Now().UnixNano() / 1000000,
Transactions: []*types.Transaction{},
},
}, nil
}

return &types.BlockResponse{
Block: &types.Block{
BlockIdentifier: &types.BlockIdentifier{
Expand Down Expand Up @@ -105,6 +132,7 @@ func (s *BlockAPIService) Block(

// BlockTransaction implements the /block/transaction endpoint.
func (s *BlockAPIService) BlockTransaction(
ctx context.Context,
request *types.BlockTransactionRequest,
) (*types.BlockTransactionResponse, *types.Error) {
return &types.BlockTransactionResponse{
Expand Down
11 changes: 8 additions & 3 deletions examples/server/services/network_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package services

import (
"context"

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
)
Expand All @@ -33,7 +35,8 @@ func NewNetworkAPIService(network *types.NetworkIdentifier) server.NetworkAPISer

// NetworkList implements the /network/list endpoint
func (s *NetworkAPIService) NetworkList(
*types.MetadataRequest,
ctx context.Context,
request *types.MetadataRequest,
) (*types.NetworkListResponse, *types.Error) {
return &types.NetworkListResponse{
NetworkIdentifiers: []*types.NetworkIdentifier{
Expand All @@ -44,7 +47,8 @@ func (s *NetworkAPIService) NetworkList(

// NetworkStatus implements the /network/status endpoint.
func (s *NetworkAPIService) NetworkStatus(
*types.NetworkRequest,
ctx context.Context,
request *types.NetworkRequest,
) (*types.NetworkStatusResponse, *types.Error) {
return &types.NetworkStatusResponse{
CurrentBlockIdentifier: &types.BlockIdentifier{
Expand All @@ -66,7 +70,8 @@ func (s *NetworkAPIService) NetworkStatus(

// NetworkOptions implements the /network/options endpoint.
func (s *NetworkAPIService) NetworkOptions(
*types.NetworkRequest,
ctx context.Context,
request *types.NetworkRequest,
) (*types.NetworkOptionsResponse, *types.Error) {
return &types.NetworkOptionsResponse{
Version: &types.Version{
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
30 changes: 23 additions & 7 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package server

import (
"context"
"net/http"

"github.com/coinbase/rosetta-sdk-go/types"
Expand Down Expand Up @@ -78,16 +79,22 @@ type NetworkAPIRouter interface {
// while the service implementation can ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type AccountAPIServicer interface {
AccountBalance(*types.AccountBalanceRequest) (*types.AccountBalanceResponse, *types.Error)
AccountBalance(
context.Context,
*types.AccountBalanceRequest,
) (*types.AccountBalanceResponse, *types.Error)
}

// BlockAPIServicer defines the api actions for the BlockAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type BlockAPIServicer interface {
Block(*types.BlockRequest) (*types.BlockResponse, *types.Error)
BlockTransaction(*types.BlockTransactionRequest) (*types.BlockTransactionResponse, *types.Error)
Block(context.Context, *types.BlockRequest) (*types.BlockResponse, *types.Error)
BlockTransaction(
context.Context,
*types.BlockTransactionRequest,
) (*types.BlockTransactionResponse, *types.Error)
}

// ConstructionAPIServicer defines the api actions for the ConstructionAPI service
Expand All @@ -96,9 +103,11 @@ type BlockAPIServicer interface {
// and updated with the logic required for the API.
type ConstructionAPIServicer interface {
ConstructionMetadata(
context.Context,
*types.ConstructionMetadataRequest,
) (*types.ConstructionMetadataResponse, *types.Error)
ConstructionSubmit(
context.Context,
*types.ConstructionSubmitRequest,
) (*types.ConstructionSubmitResponse, *types.Error)
}
Expand All @@ -108,8 +117,9 @@ type ConstructionAPIServicer interface {
// while the service implementation can ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type MempoolAPIServicer interface {
Mempool(*types.MempoolRequest) (*types.MempoolResponse, *types.Error)
Mempool(context.Context, *types.MempoolRequest) (*types.MempoolResponse, *types.Error)
MempoolTransaction(
context.Context,
*types.MempoolTransactionRequest,
) (*types.MempoolTransactionResponse, *types.Error)
}
Expand All @@ -119,7 +129,13 @@ type MempoolAPIServicer interface {
// while the service implementation can ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type NetworkAPIServicer interface {
NetworkList(*types.MetadataRequest) (*types.NetworkListResponse, *types.Error)
NetworkOptions(*types.NetworkRequest) (*types.NetworkOptionsResponse, *types.Error)
NetworkStatus(*types.NetworkRequest) (*types.NetworkStatusResponse, *types.Error)
NetworkList(context.Context, *types.MetadataRequest) (*types.NetworkListResponse, *types.Error)
NetworkOptions(
context.Context,
*types.NetworkRequest,
) (*types.NetworkOptionsResponse, *types.Error)
NetworkStatus(
context.Context,
*types.NetworkRequest,
) (*types.NetworkStatusResponse, *types.Error)
}
2 changes: 1 addition & 1 deletion server/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *AccountAPIController) AccountBalance(w http.ResponseWriter, r *http.Req
return
}

result, serviceErr := c.service.AccountBalance(accountBalanceRequest)
result, serviceErr := c.service.AccountBalance(r.Context(), accountBalanceRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
4 changes: 2 additions & 2 deletions server/api_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *BlockAPIController) Block(w http.ResponseWriter, r *http.Request) {
return
}

result, serviceErr := c.service.Block(blockRequest)
result, serviceErr := c.service.Block(r.Context(), blockRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -104,7 +104,7 @@ func (c *BlockAPIController) BlockTransaction(w http.ResponseWriter, r *http.Req
return
}

result, serviceErr := c.service.BlockTransaction(blockTransactionRequest)
result, serviceErr := c.service.BlockTransaction(r.Context(), blockTransactionRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
4 changes: 2 additions & 2 deletions server/api_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *ConstructionAPIController) ConstructionMetadata(w http.ResponseWriter,
return
}

result, serviceErr := c.service.ConstructionMetadata(constructionMetadataRequest)
result, serviceErr := c.service.ConstructionMetadata(r.Context(), constructionMetadataRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -104,7 +104,7 @@ func (c *ConstructionAPIController) ConstructionSubmit(w http.ResponseWriter, r
return
}

result, serviceErr := c.service.ConstructionSubmit(constructionSubmitRequest)
result, serviceErr := c.service.ConstructionSubmit(r.Context(), constructionSubmitRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
4 changes: 2 additions & 2 deletions server/api_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *MempoolAPIController) Mempool(w http.ResponseWriter, r *http.Request) {
return
}

result, serviceErr := c.service.Mempool(mempoolRequest)
result, serviceErr := c.service.Mempool(r.Context(), mempoolRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -104,7 +104,7 @@ func (c *MempoolAPIController) MempoolTransaction(w http.ResponseWriter, r *http
return
}

result, serviceErr := c.service.MempoolTransaction(mempoolTransactionRequest)
result, serviceErr := c.service.MempoolTransaction(r.Context(), mempoolTransactionRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
6 changes: 3 additions & 3 deletions server/api_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *NetworkAPIController) NetworkList(w http.ResponseWriter, r *http.Reques
return
}

result, serviceErr := c.service.NetworkList(metadataRequest)
result, serviceErr := c.service.NetworkList(r.Context(), metadataRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -110,7 +110,7 @@ func (c *NetworkAPIController) NetworkOptions(w http.ResponseWriter, r *http.Req
return
}

result, serviceErr := c.service.NetworkOptions(networkRequest)
result, serviceErr := c.service.NetworkOptions(r.Context(), networkRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -140,7 +140,7 @@ func (c *NetworkAPIController) NetworkStatus(w http.ResponseWriter, r *http.Requ
return
}

result, serviceErr := c.service.NetworkStatus(networkRequest)
result, serviceErr := c.service.NetworkStatus(r.Context(), networkRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
5 changes: 3 additions & 2 deletions templates/server/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
package {{packageName}}

import (
"net/http"
"context"
"net/http"

"github.com/coinbase/rosetta-sdk-go/types"
)
Expand All @@ -21,5 +22,5 @@ type {{classname}}Router interface { {{#operations}}{{#operation}}
// while the service implementation can ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type {{classname}}Servicer interface { {{#operations}}{{#operation}}
{{operationId}}({{#allParams}}*types.{{dataType}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) (*types.{{returnType}}, *types.Error){{/operation}}{{/operations}}
{{operationId}}(context.Context, {{#allParams}}*types.{{dataType}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) (*types.{{returnType}}, *types.Error){{/operation}}{{/operations}}
}{{/apis}}{{/apiInfo}}
2 changes: 1 addition & 1 deletion templates/server/controller-api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
}

{{/isBodyParam}}{{/allParams}}
result, serviceErr := c.service.{{nickname}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
result, serviceErr := c.service.{{nickname}}(r.Context(), {{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)
Expand Down

0 comments on commit 522fa82

Please sign in to comment.