Skip to content

Commit

Permalink
Merge pull request #19 from coinbase/patrick/json-rawMessage
Browse files Browse the repository at this point in the history
json.RawMessage Metadata
  • Loading branch information
patrick-ogrady authored May 6, 2020
2 parents cedb616 + b8c002e commit b8e6986
Show file tree
Hide file tree
Showing 40 changed files with 812 additions and 170 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ deps: | gen-deps
gen-deps:
${GO_INSTALL} github.com/google/addlicense
${GO_INSTALL} github.com/segmentio/golines
${GO_INSTALL} golang.org/x/tools/cmd/goimports

gen:
./codegen.sh
Expand All @@ -30,9 +31,11 @@ lint: | lint-examples

format:
gofmt -s -w -l .
goimports -w .

check-format:
! gofmt -s -l . | read
! goimports -l . | read

test:
${TEST_SCRIPT}
Expand Down
7 changes: 4 additions & 3 deletions asserter/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package asserter

import (
"encoding/json"
"fmt"
"reflect"

"github.com/coinbase/rosetta-sdk-go/types"
)
Expand All @@ -28,7 +28,7 @@ import (
// struct (including currency.Metadata).
func containsCurrency(currencies []*types.Currency, currency *types.Currency) bool {
for _, curr := range currencies {
if reflect.DeepEqual(curr, currency) {
if types.Hash(curr) == types.Hash(currency) {
return true
}
}
Expand Down Expand Up @@ -66,6 +66,7 @@ func AccountBalanceResponse(
requestBlock *types.PartialBlockIdentifier,
responseBlock *types.BlockIdentifier,
balances []*types.Amount,
metadata json.RawMessage,
) error {
if err := BlockIdentifier(responseBlock); err != nil {
return err
Expand Down Expand Up @@ -95,5 +96,5 @@ func AccountBalanceResponse(
)
}

return nil
return JSONObject(metadata)
}
35 changes: 23 additions & 12 deletions asserter/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package asserter

import (
"encoding/json"
"errors"
"fmt"
"testing"
Expand Down Expand Up @@ -48,18 +49,29 @@ func TestContainsCurrency(t *testing.T) {
{
Symbol: "BTC",
Decimals: 8,
Metadata: map[string]interface{}{
"blah": "hello",
},
Metadata: json.RawMessage(`{"blah": "hello"}`),
},
},
currency: &types.Currency{
Symbol: "BTC",
Decimals: 8,
Metadata: map[string]interface{}{
"blah": "hello",
Metadata: json.RawMessage(`{"blah": "hello"}`),
},
contains: true,
},
"more complex contains": {
currencies: []*types.Currency{
{
Symbol: "BTC",
Decimals: 8,
Metadata: json.RawMessage(`{"blah2":"bye", "blah": "hello"}`),
},
},
currency: &types.Currency{
Symbol: "BTC",
Decimals: 8,
Metadata: json.RawMessage(`{"blah": "hello", "blah2": "bye"}`),
},
contains: true,
},
"empty": {
Expand Down Expand Up @@ -101,17 +113,13 @@ func TestContainsCurrency(t *testing.T) {
{
Symbol: "BTC",
Decimals: 8,
Metadata: map[string]interface{}{
"blah": "hello",
},
Metadata: json.RawMessage(`{"blah": "hello"}`),
},
},
currency: &types.Currency{
Symbol: "BTC",
Decimals: 8,
Metadata: map[string]interface{}{
"blah": "bye",
},
Metadata: json.RawMessage(`{"blah": "bye"}`),
},
contains: false,
},
Expand Down Expand Up @@ -153,6 +161,7 @@ func TestAccoutBalance(t *testing.T) {
requestBlock *types.PartialBlockIdentifier
responseBlock *types.BlockIdentifier
balances []*types.Amount
metadata json.RawMessage
err error
}{
"simple balance": {
Expand Down Expand Up @@ -203,7 +212,8 @@ func TestAccoutBalance(t *testing.T) {
balances: []*types.Amount{
validAmount,
},
err: nil,
metadata: json.RawMessage(`{"sequence":1}`),
err: nil,
},
"invalid historical request index": {
requestBlock: &types.PartialBlockIdentifier{
Expand Down Expand Up @@ -243,6 +253,7 @@ func TestAccoutBalance(t *testing.T) {
test.requestBlock,
test.responseBlock,
test.balances,
test.metadata,
)
assert.Equal(t, test.err, err)
})
Expand Down
27 changes: 14 additions & 13 deletions asserter/asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ func NewClientWithResponses(
)
}

// FileConfiguration is the structure of the JSON configuration file.
type FileConfiguration struct {
// Configuration is the static configuration of an Asserter. This
// configuration can be exported by the Asserter and used to instantiate an
// Asserter.
type Configuration struct {
NetworkIdentifier *types.NetworkIdentifier `json:"network_identifier"`
GenesisBlockIdentifier *types.BlockIdentifier `json:"genesis_block_identifier"`
AllowedOperationTypes []string `json:"allowed_operation_types"`
Expand All @@ -109,7 +111,7 @@ func NewClientWithFile(
return nil, err
}

config := &FileConfiguration{}
config := &Configuration{}
if err := json.Unmarshal(content, config); err != nil {
return nil, err
}
Expand Down Expand Up @@ -170,16 +172,9 @@ func NewClientWithOptions(

// ClientConfiguration returns all variables currently set in an Asserter.
// This function will error if it is called on an uninitialized asserter.
func (a *Asserter) ClientConfiguration() (
*types.NetworkIdentifier,
*types.BlockIdentifier,
[]string,
[]*types.OperationStatus,
[]*types.Error,
error,
) {
func (a *Asserter) ClientConfiguration() (*Configuration, error) {
if a == nil {
return nil, nil, nil, nil, nil, ErrAsserterNotInitialized
return nil, ErrAsserterNotInitialized
}

operationStatuses := []*types.OperationStatus{}
Expand All @@ -195,7 +190,13 @@ func (a *Asserter) ClientConfiguration() (
errors = append(errors, v)
}

return a.network, a.genesisBlock, a.operationTypes, operationStatuses, errors, nil
return &Configuration{
NetworkIdentifier: a.network,
GenesisBlockIdentifier: a.genesisBlock,
AllowedOperationTypes: a.operationTypes,
AllowedOperationStatuses: operationStatuses,
AllowedErrors: errors,
}, nil
}

// OperationSuccessful returns a boolean indicating if a types.Operation is
Expand Down
50 changes: 37 additions & 13 deletions asserter/asserter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,29 @@ func TestNew(t *testing.T) {
}

assert.NotNil(t, asserter)
network, genesis, opTypes, opStatuses, errors, err := asserter.ClientConfiguration()
configuration, err := asserter.ClientConfiguration()
assert.NoError(t, err)
assert.Equal(t, test.network, network)
assert.Equal(t, test.networkStatus.GenesisBlockIdentifier, genesis)
assert.ElementsMatch(t, test.networkOptions.Allow.OperationTypes, opTypes)
assert.ElementsMatch(t, test.networkOptions.Allow.OperationStatuses, opStatuses)
assert.ElementsMatch(t, test.networkOptions.Allow.Errors, errors)
assert.Equal(t, test.network, configuration.NetworkIdentifier)
assert.Equal(
t,
test.networkStatus.GenesisBlockIdentifier,
configuration.GenesisBlockIdentifier,
)
assert.ElementsMatch(
t,
test.networkOptions.Allow.OperationTypes,
configuration.AllowedOperationTypes,
)
assert.ElementsMatch(
t,
test.networkOptions.Allow.OperationStatuses,
configuration.AllowedOperationStatuses,
)
assert.ElementsMatch(t, test.networkOptions.Allow.Errors, configuration.AllowedErrors)
})

t.Run(fmt.Sprintf("%s with file", name), func(t *testing.T) {
fileConfig := FileConfiguration{
fileConfig := &Configuration{
NetworkIdentifier: test.network,
GenesisBlockIdentifier: test.networkStatus.GenesisBlockIdentifier,
AllowedOperationTypes: test.networkOptions.Allow.OperationTypes,
Expand Down Expand Up @@ -262,13 +274,25 @@ func TestNew(t *testing.T) {
}

assert.NotNil(t, asserter)
network, genesis, opTypes, opStatuses, errors, err := asserter.ClientConfiguration()
configuration, err := asserter.ClientConfiguration()
assert.NoError(t, err)
assert.Equal(t, test.network, network)
assert.Equal(t, test.networkStatus.GenesisBlockIdentifier, genesis)
assert.ElementsMatch(t, test.networkOptions.Allow.OperationTypes, opTypes)
assert.ElementsMatch(t, test.networkOptions.Allow.OperationStatuses, opStatuses)
assert.ElementsMatch(t, test.networkOptions.Allow.Errors, errors)
assert.Equal(t, test.network, configuration.NetworkIdentifier)
assert.Equal(
t,
test.networkStatus.GenesisBlockIdentifier,
configuration.GenesisBlockIdentifier,
)
assert.ElementsMatch(
t,
test.networkOptions.Allow.OperationTypes,
configuration.AllowedOperationTypes,
)
assert.ElementsMatch(
t,
test.networkOptions.Allow.OperationStatuses,
configuration.AllowedOperationStatuses,
)
assert.ElementsMatch(t, test.networkOptions.Allow.Errors, configuration.AllowedErrors)
})
}

Expand Down
14 changes: 9 additions & 5 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Amount(amount *types.Amount) error {
return errors.New("Amount.Currency.Decimals must be > 0")
}

return nil
return JSONObject(amount.Currency.Metadata)
}

// OperationIdentifier returns an error if index of the
Expand Down Expand Up @@ -96,7 +96,7 @@ func AccountIdentifier(account *types.AccountIdentifier) error {
return errors.New("Account.SubAccount.Address is missing")
}

return nil
return JSONObject(account.SubAccount.Metadata)
}

// contains checks if a string is contained in a slice
Expand Down Expand Up @@ -177,7 +177,11 @@ func (a *Asserter) Operation(
return err
}

return Amount(operation.Amount)
if err := Amount(operation.Amount); err != nil {
return err
}

return JSONObject(operation.Metadata)
}

// BlockIdentifier ensures a types.BlockIdentifier
Expand Down Expand Up @@ -256,7 +260,7 @@ func (a *Asserter) Transaction(
}
}

return nil
return JSONObject(transaction.Metadata)
}

// Timestamp returns an error if the timestamp
Expand Down Expand Up @@ -314,5 +318,5 @@ func (a *Asserter) Block(
}
}

return nil
return JSONObject(block.Metadata)
}
6 changes: 3 additions & 3 deletions asserter/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
)

// ConstructionMetadata returns an error if
// the NetworkFee is not a valid types.Amount.
// the metadata is not a JSON object.
func ConstructionMetadata(
response *types.ConstructionMetadataResponse,
) error {
if response.Metadata == nil {
return errors.New("Metadata is nil")
}

return nil
return JSONObject(response.Metadata)
}

// ConstructionSubmit returns an error if
Expand All @@ -43,5 +43,5 @@ func ConstructionSubmit(
return err
}

return nil
return JSONObject(response.Metadata)
}
3 changes: 2 additions & 1 deletion asserter/construction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package asserter

import (
"encoding/json"
"errors"
"testing"

Expand All @@ -30,7 +31,7 @@ func TestConstructionMetadata(t *testing.T) {
}{
"valid response": {
response: &types.ConstructionMetadataResponse{
Metadata: map[string]interface{}{},
Metadata: json.RawMessage(`{}`),
},
err: nil,
},
Expand Down
Loading

0 comments on commit b8e6986

Please sign in to comment.