Skip to content

Commit

Permalink
Client improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Apr 10, 2020
1 parent b141948 commit 2ca5913
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 34 deletions.
2 changes: 0 additions & 2 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package asserter

import (
"context"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -234,7 +233,6 @@ func Timestamp(timestamp int64) error {

// Block runs a basic set of assertions for each returned block.
func (a *Asserter) Block(
ctx context.Context,
block *models.Block,
) error {
if block == nil {
Expand Down
2 changes: 1 addition & 1 deletion asserter/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func TestBlock(t *testing.T) {
)
assert.NoError(t, err)

err = asserter.Block(context.Background(), test.block)
err = asserter.Block(test.block)
assert.Equal(t, test.err, err)
})
}
Expand Down
4 changes: 2 additions & 2 deletions client/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type AccountAPIService service
// optional BlockIdentifier.
func (a *AccountAPIService) AccountBalance(
ctx _context.Context,
accountBalanceRequest models.AccountBalanceRequest,
accountBalanceRequest *models.AccountBalanceRequest,
) (*models.AccountBalanceResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -71,7 +71,7 @@ func (a *AccountAPIService) AccountBalance(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &accountBalanceRequest
localVarPostBody = accountBalanceRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions client/api_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type BlockAPIService service
// be done to get all transaction information.
func (a *BlockAPIService) Block(
ctx _context.Context,
blockRequest models.BlockRequest,
blockRequest *models.BlockRequest,
) (*models.BlockResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (a *BlockAPIService) Block(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &blockRequest
localVarPostBody = blockRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (a *BlockAPIService) Block(
// /data directory (on a path that does not conflict with the node).
func (a *BlockAPIService) BlockTransaction(
ctx _context.Context,
blockTransactionRequest models.BlockTransactionRequest,
blockTransactionRequest *models.BlockTransactionRequest,
) (*models.BlockTransactionResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (a *BlockAPIService) BlockTransaction(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &blockTransactionRequest
localVarPostBody = blockTransactionRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions client/api_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ConstructionAPIService service
// not need to be broken out into a key-value mapping and can be returned as a blob.
func (a *ConstructionAPIService) TransactionConstruction(
ctx _context.Context,
transactionConstructionRequest models.TransactionConstructionRequest,
transactionConstructionRequest *models.TransactionConstructionRequest,
) (*models.TransactionConstructionResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -69,7 +69,7 @@ func (a *ConstructionAPIService) TransactionConstruction(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &transactionConstructionRequest
localVarPostBody = transactionConstructionRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down Expand Up @@ -113,7 +113,7 @@ func (a *ConstructionAPIService) TransactionConstruction(
// Otherwise, it should return an error.
func (a *ConstructionAPIService) TransactionSubmit(
ctx _context.Context,
transactionSubmitRequest models.TransactionSubmitRequest,
transactionSubmitRequest *models.TransactionSubmitRequest,
) (*models.TransactionSubmitResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (a *ConstructionAPIService) TransactionSubmit(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &transactionSubmitRequest
localVarPostBody = transactionSubmitRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions client/api_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type MempoolAPIService service
// Mempool Get all Transaction Identifiers in the mempool
func (a *MempoolAPIService) Mempool(
ctx _context.Context,
mempoolRequest models.MempoolRequest,
mempoolRequest *models.MempoolRequest,
) (*models.MempoolResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (a *MempoolAPIService) Mempool(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &mempoolRequest
localVarPostBody = mempoolRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (a *MempoolAPIService) Mempool(
// in a block.
func (a *MempoolAPIService) MempoolTransaction(
ctx _context.Context,
mempoolTransactionRequest models.MempoolTransactionRequest,
mempoolTransactionRequest *models.MempoolTransactionRequest,
) (*models.MempoolTransactionResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -138,7 +138,7 @@ func (a *MempoolAPIService) MempoolTransaction(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &mempoolTransactionRequest
localVarPostBody = mempoolTransactionRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions client/api_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type NetworkAPIService service
// method also returns the methods, operation types, and operation statuses the node supports.
func (a *NetworkAPIService) NetworkStatus(
ctx _context.Context,
networkStatusRequest models.NetworkStatusRequest,
networkStatusRequest *models.NetworkStatusRequest,
) (*models.NetworkStatusResponse, *models.Error, error) {
var (
localVarPostBody interface{}
Expand Down Expand Up @@ -65,7 +65,7 @@ func (a *NetworkAPIService) NetworkStatus(
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
localVarPostBody = &networkStatusRequest
localVarPostBody = networkStatusRequest

r, err := a.client.prepareRequest(ctx, localVarPath, localVarPostBody, localVarHeaderParams)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion fetcher/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (f *Fetcher) UnsafeAccountBalance(
account *models.AccountIdentifier,
) (*models.BlockIdentifier, []*models.Balance, error) {
balance, _, err := f.rosettaClient.AccountAPI.AccountBalance(ctx,
models.AccountBalanceRequest{
&models.AccountBalanceRequest{
NetworkIdentifier: network,
AccountIdentifier: account,
},
Expand Down
20 changes: 16 additions & 4 deletions fetcher/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"time"

"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/models"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (f *Fetcher) fetchChannelTransactions(
) error {
for transactionIdentifier := range txsToFetch {
tx, _, err := f.rosettaClient.BlockAPI.BlockTransaction(ctx,
models.BlockTransactionRequest{
&models.BlockTransactionRequest{
NetworkIdentifier: network,
BlockIdentifier: block,
TransactionIdentifier: transactionIdentifier,
Expand Down Expand Up @@ -135,7 +136,7 @@ func (f *Fetcher) UnsafeBlock(
network *models.NetworkIdentifier,
blockIdentifier *models.PartialBlockIdentifier,
) (*models.Block, error) {
blockResponse, _, err := f.rosettaClient.BlockAPI.Block(ctx, models.BlockRequest{
blockResponse, _, err := f.rosettaClient.BlockAPI.Block(ctx, &models.BlockRequest{
NetworkIdentifier: network,
BlockIdentifier: blockIdentifier,
})
Expand Down Expand Up @@ -182,7 +183,7 @@ func (f *Fetcher) Block(
return nil, err
}

if err := f.Asserter.Block(ctx, block); err != nil {
if err := f.Asserter.Block(block); err != nil {
return nil, err
}

Expand All @@ -202,6 +203,10 @@ func (f *Fetcher) BlockRetry(
return nil, errors.New("asserter not initialized")
}

if err := asserter.PartialBlockIdentifier(blockIdentifier); err != nil {
return nil, err
}

backoffRetries := backoffRetries(maxElapsedTime, maxRetries)

for ctx.Err() == nil {
Expand All @@ -214,7 +219,14 @@ func (f *Fetcher) BlockRetry(
return block, nil
}

if !tryAgain(fmt.Sprintf("block %d", blockIdentifier.Index), backoffRetries, err) {
var blockFetchErr string
if blockIdentifier.Index != nil {
blockFetchErr = fmt.Sprintf("block %d", *blockIdentifier.Index)
} else {
blockFetchErr = fmt.Sprintf("block %s", *blockIdentifier.Hash)
}

if !tryAgain(blockFetchErr, backoffRetries, err) {
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions fetcher/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (f *Fetcher) ConstructionMetadata(
method *string,
) (*models.Amount, *map[string]interface{}, error) {
metadata, _, err := f.rosettaClient.ConstructionAPI.TransactionConstruction(ctx,
models.TransactionConstructionRequest{
&models.TransactionConstructionRequest{
NetworkIdentifier: network,
AccountIdentifier: account,
Method: method,
Expand Down Expand Up @@ -61,7 +61,7 @@ func (f *Fetcher) ConstructionSubmit(

submitResponse, _, err := f.rosettaClient.ConstructionAPI.TransactionSubmit(
ctx,
models.TransactionSubmitRequest{
&models.TransactionSubmitRequest{
SignedTransaction: signedTransaction,
},
)
Expand Down
14 changes: 9 additions & 5 deletions fetcher/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ func (f *Fetcher) UnsafeMempool(
ctx context.Context,
network *models.NetworkIdentifier,
) ([]*models.TransactionIdentifier, error) {
mempool, _, err := f.rosettaClient.MempoolAPI.Mempool(ctx, models.MempoolRequest{
NetworkIdentifier: network,
})
mempool, _, err := f.rosettaClient.MempoolAPI.Mempool(
ctx,
&models.MempoolRequest{
NetworkIdentifier: network,
},
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -64,8 +67,9 @@ func (f *Fetcher) UnsafeMempoolTransaction(
network *models.NetworkIdentifier,
transaction *models.TransactionIdentifier,
) (*models.Transaction, *map[string]interface{}, error) {
mempoolTransaction, _, err := f.rosettaClient.MempoolAPI.MempoolTransaction(ctx,
models.MempoolTransactionRequest{
mempoolTransaction, _, err := f.rosettaClient.MempoolAPI.MempoolTransaction(
ctx,
&models.MempoolTransactionRequest{
NetworkIdentifier: network,
TransactionIdentifier: transaction,
},
Expand Down
2 changes: 1 addition & 1 deletion fetcher/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (f *Fetcher) UnsafeNetworkStatus(
) (*models.NetworkStatusResponse, error) {
networkStatus, _, err := f.rosettaClient.NetworkAPI.NetworkStatus(
ctx,
models.NetworkStatusRequest{
&models.NetworkStatusRequest{
Metadata: metadata,
},
)
Expand Down
14 changes: 14 additions & 0 deletions fetcher/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@ import (
"log"
"time"

"github.com/coinbase/rosetta-sdk-go/models"

"github.com/cenkalti/backoff"
)

// PartialBlockIdentifier constructs a PartialBlockIdentifier
// from a BlockIdentifier. This is useful when making block requests
// with the fetcher.
func PartialBlockIdentifier(
blockIdentifier *models.BlockIdentifier,
) *models.PartialBlockIdentifier {
return &models.PartialBlockIdentifier{
Hash: &blockIdentifier.Hash,
Index: &blockIdentifier.Index,
}
}

// backoffRetries creates the backoff.BackOff struct used by all
// *Retry functions in the fetcher.
func backoffRetries(
Expand Down
4 changes: 2 additions & 2 deletions templates/client/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type {{classname}}Service service
{{#operation}}

// {{operationId}}{{#notes}} {{notes}}{{/notes}}
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} models.{{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}*models.{{{returnType}}}, {{/returnType}} *models.Error, error) {
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} *models.{{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}*models.{{{returnType}}}, {{/returnType}} *models.Error, error) {
var (
localVarPostBody interface{}
)
Expand Down Expand Up @@ -66,7 +66,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
{{#hasBodyParam}}
{{#bodyParams}}
// body params
localVarPostBody = &{{paramName}}
localVarPostBody = {{paramName}}
{{/bodyParams}}
{{/hasBodyParam}}

Expand Down

0 comments on commit 2ca5913

Please sign in to comment.