Skip to content

Commit

Permalink
Add types and assertions for Related Transactions (coinbase#289)
Browse files Browse the repository at this point in the history
* update codegen file

* run codegen

* add asserter for related transactions

* add comment

* fix gen

* nits

* nits

* fix nil check
  • Loading branch information
varunpcb authored Jan 7, 2021
1 parent 11a73ee commit bb081d3
Show file tree
Hide file tree
Showing 97 changed files with 273 additions and 95 deletions.
54 changes: 54 additions & 0 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,60 @@ func (a *Asserter) Transaction(
)
}

if err := a.RelatedTransactions(transaction.RelatedTransactions); err != nil {
return fmt.Errorf(
"%w invalid related transaction in transaction %s",
err,
transaction.TransactionIdentifier.Hash,
)
}

return nil
}

// RelatedTransactions returns an error if the related transactions array is non-null and non-empty
// and
// any of the related transactions contain invalid types, invalid network identifiers,
// invalid transaction identifiers, or a direction not defined by the enum.
func (a *Asserter) RelatedTransactions(relatedTransactions []*types.RelatedTransaction) error {
for i, relatedTransaction := range relatedTransactions {
if relatedTransaction.NetworkIdentifier != nil {
if err := NetworkIdentifier(relatedTransaction.NetworkIdentifier); err != nil {
return fmt.Errorf(
"%w invalid network identifier in related transaction at index %d",
err,
i,
)
}
}

if err := TransactionIdentifier(relatedTransaction.TransactionIdentifier); err != nil {
return fmt.Errorf(
"%w invalid transaction identifier in related transaction at index %d",
err,
i,
)
}

if err := a.Direction(relatedTransaction.Direction); err != nil {
return fmt.Errorf(
"%w invalid direction in related transaction at index %d",
err,
i,
)
}
}

return nil
}

// Direction returns an error if the value passed is not types.Forward or types.Backward
func (a *Asserter) Direction(direction types.Direction) error {
if direction != types.Forward &&
direction != types.Backward {
return ErrInvalidDirection
}

return nil
}

Expand Down
63 changes: 63 additions & 0 deletions asserter/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,18 @@ func TestBlock(t *testing.T) {
Amount: validAmount,
},
},
RelatedTransactions: []*types.RelatedTransaction{
{
NetworkIdentifier: &types.NetworkIdentifier{
Blockchain: "hello",
Network: "world",
},
TransactionIdentifier: &types.TransactionIdentifier{
Hash: "blah",
},
Direction: types.Forward,
},
},
}
relatedToSelfTransaction := &types.Transaction{
TransactionIdentifier: &types.TransactionIdentifier{
Expand Down Expand Up @@ -673,6 +685,48 @@ func TestBlock(t *testing.T) {
},
},
}
invalidRelatedTransaction := &types.Transaction{
TransactionIdentifier: &types.TransactionIdentifier{
Hash: "blah",
},
Operations: []*types.Operation{
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(0),
},
Type: "PAYMENT",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: validAmount,
},
{
OperationIdentifier: &types.OperationIdentifier{
Index: int64(1),
},
RelatedOperations: []*types.OperationIdentifier{
{
Index: int64(0),
},
},
Type: "PAYMENT",
Status: types.String("SUCCESS"),
Account: validAccount,
Amount: validAmount,
},
},
RelatedTransactions: []*types.RelatedTransaction{
{
NetworkIdentifier: &types.NetworkIdentifier{
Blockchain: "hello",
Network: "world",
},
TransactionIdentifier: &types.TransactionIdentifier{
Hash: "blah",
},
Direction: "blah",
},
},
}
var tests = map[string]struct {
block *types.Block
genesisIndex int64
Expand Down Expand Up @@ -845,6 +899,15 @@ func TestBlock(t *testing.T) {
},
err: ErrTxIdentifierIsNil,
},
"invalid related transaction": {
block: &types.Block{
BlockIdentifier: validBlockIdentifier,
ParentBlockIdentifier: validParentBlockIdentifier,
Timestamp: MinUnixEpoch + 1,
Transactions: []*types.Transaction{invalidRelatedTransaction},
},
err: ErrInvalidDirection,
},
}

for name, test := range tests {
Expand Down
2 changes: 2 additions & 0 deletions asserter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var (
ErrBlockIndexPrecedesParentBlockIndex = errors.New(
"BlockIdentifier.Index <= ParentBlockIdentifier.Index",
)
ErrInvalidDirection = errors.New("invalid direction (must be 'forward' or 'backward')")

BlockErrs = []error{
ErrAmountValueMissing,
Expand Down Expand Up @@ -125,6 +126,7 @@ var (
ErrBlockIsNil,
ErrBlockHashEqualsParentBlockHash,
ErrBlockIndexPrecedesParentBlockIndex,
ErrInvalidDirection,
}
)

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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 @@ -41,7 +41,7 @@ var (
ErrRetriable = errors.New("retriable http status code received")
)

// APIClient manages communication with the Rosetta API v1.4.9
// APIClient manages communication with the Rosetta API v1.4.10
// In most cases there should be only one, shared, APIClient.
type APIClient struct {
cfg *Configuration
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 2020 Coinbase, Inc.
// Copyright 2021 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 2020 Coinbase, Inc.
// Copyright 2021 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
7 changes: 5 additions & 2 deletions codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ done
rm -rf tmp;

# Download spec file from releases
ROSETTA_SPEC_VERSION=1.4.9
ROSETTA_SPEC_VERSION=1.4.10
curl -L https://github.com/coinbase/rosetta-specifications/releases/download/v${ROSETTA_SPEC_VERSION}/api.json -o api.json;

# Generate client + types code
Expand Down Expand Up @@ -120,8 +120,9 @@ sed "${SED_IFLAG[@]}" 's/*SignatureType/SignatureType/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/*CoinAction/CoinAction/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/*ExemptionType/ExemptionType/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/*BlockEventType/BlockEventType/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/*Direction/Direction/g' client/* server/*;

# Fix CurveTypes, SignatureTypes, CoinActions, ExemptionTypes
# Fix CurveTypes, SignatureTypes, CoinActions, ExemptionTypes, Direction
sed "${SED_IFLAG[@]}" 's/SECP256K1/Secp256k1/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/SECP256R1/Secp256r1/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/EDWARDS25519/Edwards25519/g' client/* server/*;
Expand All @@ -136,6 +137,8 @@ sed "${SED_IFLAG[@]}" 's/SPENT/CoinSpent/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/GREATER_OR_EQUAL/BalanceGreaterOrEqual/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/LESS_OR_EQUAL/BalanceLessOrEqual/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/DYNAMIC/BalanceDynamic/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/FORWARD/Forward/g' client/* server/*;
sed "${SED_IFLAG[@]}" 's/BACKWARD/Backward/g' client/* server/*;

# Convert HexBytes to Bytes
sed "${SED_IFLAG[@]}" '/Hex-encoded public key bytes in the format specified by the CurveType/d' client/* server/*;
Expand Down
2 changes: 1 addition & 1 deletion server/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_block.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_call.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_construction.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_events.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_mempool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_network.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/api_search.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 server/routers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/account_balance_request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/account_balance_response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/account_coins_request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/account_coins_response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/account_identifier.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/allow.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/amount.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/balance_exemption.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/block.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/block_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 types/block_event_type.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Coinbase, Inc.
// Copyright 2021 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 bb081d3

Please sign in to comment.