Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Apr 15, 2020
1 parent 096d837 commit c1ab33f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
16 changes: 7 additions & 9 deletions asserter/asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ package asserter

import (
"context"
"errors"

"github.com/coinbase/rosetta-sdk-go/types"
)

var (
// ErrAsserterNotInitialized is returned when some call in the asserter
// package requires the asserter to be initialized first.
ErrAsserterNotInitialized = errors.New("asserter not initialized")
)

// Asserter contains all logic to perform static
// validation on Rosetta Server responses.
type Asserter struct {
Expand Down Expand Up @@ -81,12 +88,3 @@ func NewWithOptions(

return asserter
}

func (a *Asserter) operationStatuses() []string {
statuses := []string{}
for k := range a.operationStatusMap {
statuses = append(statuses, k)
}

return statuses
}
14 changes: 14 additions & 0 deletions asserter/asserter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package asserter

import (
Expand Down
30 changes: 25 additions & 5 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func AccountIdentifier(account *types.AccountIdentifier) error {
// AFTER an operation has been validated.
func (a *Asserter) OperationSuccessful(operation *types.Operation) (bool, error) {
if a == nil {
return false, errors.New("must initialize asserter")
return false, ErrAsserterNotInitialized
}

val, ok := a.operationStatusMap[operation.Status]
Expand All @@ -117,14 +117,29 @@ func (a *Asserter) OperationSuccessful(operation *types.Operation) (bool, error)
return val, nil
}

// operationStatuses returns all operation statuses the
// asserter consider valid.
func (a *Asserter) operationStatuses() ([]string, error) {
if a == nil {
return nil, ErrAsserterNotInitialized
}

statuses := []string{}
for k := range a.operationStatusMap {
statuses = append(statuses, k)
}

return statuses, nil
}

// Operation ensures a types.Operation has a valid
// type, status, and amount.
func (a *Asserter) Operation(
operation *types.Operation,
index int64,
) error {
if a == nil {
return errors.New("must initialize asserter")
return ErrAsserterNotInitialized
}

if operation == nil {
Expand All @@ -139,7 +154,12 @@ func (a *Asserter) Operation(
return fmt.Errorf("Operation.Type %s is invalid", operation.Type)
}

if operation.Status == "" || !contains(a.operationStatuses(), operation.Status) {
validOperationStatuses, err := a.operationStatuses()
if err != nil {
return err
}

if operation.Status == "" || !contains(validOperationStatuses, operation.Status) {
return fmt.Errorf("Operation.Status %s is invalid", operation.Status)
}

Expand Down Expand Up @@ -213,7 +233,7 @@ func (a *Asserter) Transaction(
transaction *types.Transaction,
) error {
if a == nil {
return errors.New("must initialize asserter")
return ErrAsserterNotInitialized
}

if transaction == nil {
Expand Down Expand Up @@ -248,7 +268,7 @@ func (a *Asserter) Block(
block *types.Block,
) error {
if a == nil {
return errors.New("must initialize asserter")
return ErrAsserterNotInitialized
}

if block == nil {
Expand Down

0 comments on commit c1ab33f

Please sign in to comment.