Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new asserter client with ignoring rosetta spec validation. #502

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions asserter/asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io/ioutil"
"path"
"strings"

"github.com/coinbase/rosetta-sdk-go/types"
)
Expand All @@ -35,11 +36,12 @@ type Asserter struct {
timestampStartIndex int64

// These variables are used for request assertion.
historicalBalanceLookup bool
supportedNetworks []*types.NetworkIdentifier
callMethods map[string]struct{}
mempoolCoins bool
validations *Validations
historicalBalanceLookup bool
supportedNetworks []*types.NetworkIdentifier
callMethods map[string]struct{}
mempoolCoins bool
validations *Validations
ignoreRosettaSpecValidation bool
}

// Validations is used to define stricter validations
Expand Down Expand Up @@ -292,6 +294,47 @@ func NewClientWithOptions(
return asserter, nil
}

// NewClientWithoutRosettaSpec constructs a new Asserter using the provided
// arguments and without a Rosetta Spec validation. This is used to ignore rosetta spec validation
func NewClientWithoutRosettaSpec(
network *types.NetworkIdentifier,
genesisBlockIdentifier *types.BlockIdentifier,
) (*Asserter, error) {
if err := NetworkIdentifier(network); err != nil {
return nil, fmt.Errorf(
"network identifier %s is invalid: %w",
types.PrintStruct(network),
err,
)
}

if err := BlockIdentifier(genesisBlockIdentifier); err != nil {
return nil, fmt.Errorf(
"genesis block identifier %s is invalid: %w",
types.PrintStruct(genesisBlockIdentifier),
err,
)
}

asserter := &Asserter{
network: network,
genesisBlock: genesisBlockIdentifier,
validations: &Validations{
Enabled: false,
},
ignoreRosettaSpecValidation: true,
}

asserter.operationStatusMap = map[string]bool{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to a const list in validation.go

asserter.operationStatusMap["SUCCESS"] = true
asserter.operationStatusMap["OK"] = true
asserter.operationStatusMap["COMPLETED"] = true
asserter.operationStatusMap["FAILED"] = false
asserter.operationStatusMap["FAILURE"] = false

return asserter, nil
}

// 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() (*Configuration, error) {
Expand Down Expand Up @@ -336,7 +379,10 @@ func (a *Asserter) OperationSuccessful(operation *types.Operation) (bool, error)

val, ok := a.operationStatusMap[*operation.Status]
if !ok {
return false, fmt.Errorf("operation status %s is not found", *operation.Status)
val, ok = a.operationStatusMap[strings.ToUpper(*operation.Status)]
if !ok {
return false, fmt.Errorf("operation status %s is not found", *operation.Status)
}
}

return val, nil
Expand Down
6 changes: 3 additions & 3 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (a *Asserter) OperationStatus(status *string, construction bool) error {
return ErrOperationStatusNotEmptyForConstruction
}

if _, ok := a.operationStatusMap[*status]; !ok {
if _, ok := a.operationStatusMap[*status]; !a.ignoreRosettaSpecValidation && !ok {
return fmt.Errorf("operation status %s is invalid: %w", *status, ErrOperationStatusInvalid)
}

Expand All @@ -174,7 +174,7 @@ func (a *Asserter) OperationType(t string) error {
return ErrAsserterNotInitialized
}

if t == "" || !containsString(a.operationTypes, t) {
if t == "" || (!a.ignoreRosettaSpecValidation && !containsString(a.operationTypes, t)) {
return fmt.Errorf("operation type %s is invalid: %w", t, ErrOperationTypeInvalid)
}

Expand Down Expand Up @@ -610,7 +610,7 @@ func (a *Asserter) Block(

// Only check for timestamp validity if timestamp start index is <=
// the current block index.
if a.timestampStartIndex <= block.BlockIdentifier.Index {
if !a.ignoreRosettaSpecValidation && a.timestampStartIndex <= block.BlockIdentifier.Index {
if err := Timestamp(block.Timestamp); err != nil {
return fmt.Errorf("timestamp %d is invalid: %w", block.Timestamp, err)
}
Expand Down
4 changes: 4 additions & 0 deletions asserter/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (a *Asserter) Error(
return err
}

if a.ignoreRosettaSpecValidation {
return nil
}

val, ok := a.errorTypeMap[err.Code]
if !ok {
return fmt.Errorf(
Expand Down
Loading