Skip to content

Commit

Permalink
Add a new asserter client with ignoring rosetta spec validation. (#502)
Browse files Browse the repository at this point in the history
* Add a new asserter client with ignoring rosetta spec validation.

* Run make gen to fix check gen error

* refactor asserter code

* update copyright from 2020 to 2024.
  • Loading branch information
jacquescaocb authored Feb 21, 2024
1 parent c8a1eee commit fa62d03
Show file tree
Hide file tree
Showing 250 changed files with 338 additions and 258 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2020 Coinbase, Inc.
Copyright 2024 Coinbase, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/account_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
56 changes: 49 additions & 7 deletions asserter/asserter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -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,43 @@ func NewClientWithOptions(
return asserter, nil
}

// NewGenericRosettaClient constructs a new Asserter using the provided
// arguments and without a Rosetta Spec validation. This is used to ignore rosetta spec validation
func NewGenericRosettaClient(
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,
}

//init default operation statuses for generic rosetta client
InitOperationStatus(asserter)

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 +375,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
2 changes: 1 addition & 1 deletion asserter/asserter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
8 changes: 4 additions & 4 deletions asserter/block.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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
2 changes: 1 addition & 1 deletion asserter/block_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/coin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/coin_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/construction.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/construction_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
6 changes: 5 additions & 1 deletion asserter/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down 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
2 changes: 1 addition & 1 deletion asserter/error_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/events_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/mempool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/network.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/network_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
34 changes: 34 additions & 0 deletions asserter/operation_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 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

const (
OperationStatusSuccess = "SUCCESS"
OperationStatusOk = "OK"
OperationStatusCompleted = "COMPLETED"
OperationStatusFailed = "FAILED"
OperationStatusFailure = "FAILURE"
)

// InitOperationStatus initializes operation status map for a generic rosetta client
func InitOperationStatus(asserter *Asserter) {
asserter.operationStatusMap = map[string]bool{
OperationStatusSuccess: true,
OperationStatusOk: true,
OperationStatusCompleted: true,
OperationStatusFailed: false,
OperationStatusFailure: false,
}
}
2 changes: 1 addition & 1 deletion asserter/search.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/search_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion asserter/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_block.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_call.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_construction.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_mempool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_network.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/api_search.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/configuration.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion client/response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion constructor/coordinator/coordinator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion constructor/coordinator/coordinator_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion constructor/coordinator/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion constructor/coordinator/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2024 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit fa62d03

Please sign in to comment.