Skip to content

Commit

Permalink
Make block identifier asserter function comply with Rosetta spec (#447)
Browse files Browse the repository at this point in the history
* fix: check:spec error

Signed-off-by: Jingfu Wang <[email protected]>

* fix: check gen

Signed-off-by: Jingfu Wang <[email protected]>

Signed-off-by: Jingfu Wang <[email protected]>
  • Loading branch information
GeekArthur authored Oct 7, 2022
1 parent 1cc6016 commit e03849b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
10 changes: 5 additions & 5 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ func PartialBlockIdentifier(blockIdentifier *types.PartialBlockIdentifier) error
return ErrPartialBlockIdentifierIsNil
}

if blockIdentifier.Hash != nil && *blockIdentifier.Hash != "" {
return nil
if blockIdentifier.Hash != nil && *blockIdentifier.Hash == "" {
return ErrPartialBlockIdentifierHashIsEmpty
}

if blockIdentifier.Index != nil && *blockIdentifier.Index >= 0 {
return nil
if blockIdentifier.Index != nil && *blockIdentifier.Index < 0 {
return ErrPartialBlockIdentifierIndexIsNegative
}

return ErrPartialBlockIdentifierFieldsNotSet
return nil
}

// TransactionIdentifier returns an error if a
Expand Down
26 changes: 15 additions & 11 deletions asserter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ var (
ErrRelatedOperationInFeeNotAllowed = errors.New(
"fee operation shouldn't have related_operations",
)
ErrBlockIdentifierIsNil = errors.New("BlockIdentifier is nil")
ErrBlockIdentifierHashMissing = errors.New("BlockIdentifier.Hash is missing")
ErrBlockIdentifierIndexIsNeg = errors.New("BlockIdentifier.Index is negative")
ErrPartialBlockIdentifierIsNil = errors.New("PartialBlockIdentifier is nil")
ErrPartialBlockIdentifierFieldsNotSet = errors.New(
"neither PartialBlockIdentifier.Hash nor PartialBlockIdentifier.Index is set",
)
ErrTxIdentifierIsNil = errors.New("TransactionIdentifier is nil")
ErrTxIdentifierHashMissing = errors.New("TransactionIdentifier.Hash is missing")
ErrNoOperationsForConstruction = errors.New("operations cannot be empty for construction")
ErrBlockIdentifierIsNil = errors.New("BlockIdentifier is nil")
ErrBlockIdentifierHashMissing = errors.New("BlockIdentifier.Hash is missing")
ErrBlockIdentifierIndexIsNeg = errors.New("BlockIdentifier.Index is negative")
ErrPartialBlockIdentifierIsNil = errors.New("PartialBlockIdentifier is nil")
ErrPartialBlockIdentifierHashIsEmpty = errors.New("PartialBlockIdentifier hash is empty")
ErrPartialBlockIdentifierIndexIsNegative = errors.New(
"PartialBlockIdentifier index is negative",
)
ErrTxIdentifierIsNil = errors.New("TransactionIdentifier is nil")
ErrTxIdentifierHashMissing = errors.New("TransactionIdentifier.Hash is missing")
ErrNoOperationsForConstruction = errors.New(
"operations cannot be empty for construction",
)
ErrTxIsNil = errors.New("Transaction is nil")
ErrTimestampBeforeMin = errors.New("timestamp is before 01/01/2000")
ErrTimestampAfterMax = errors.New("timestamp is after 01/01/2040")
Expand Down Expand Up @@ -132,7 +135,8 @@ var (
ErrBlockIdentifierHashMissing,
ErrBlockIdentifierIndexIsNeg,
ErrPartialBlockIdentifierIsNil,
ErrPartialBlockIdentifierFieldsNotSet,
ErrPartialBlockIdentifierHashIsEmpty,
ErrPartialBlockIdentifierIndexIsNegative,
ErrTxIdentifierIsNil,
ErrTxIdentifierHashMissing,
ErrNoOperationsForConstruction,
Expand Down
39 changes: 33 additions & 6 deletions asserter/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ var (

emptyBlockIdentifier = &types.BlockIdentifier{}

invalidBlockIdentifierHash string = ""
invalidBlockIdentifierIndex int64 = -1

validTransactionIdentifier = &types.TransactionIdentifier{
Hash: "tx1",
}
Expand Down Expand Up @@ -427,14 +430,27 @@ func TestAccountBalanceRequest(t *testing.T) {
allowHistorical: true,
err: nil,
},
"invalid historical request": {
"invalid historical request when block identifier hash is invalid": {
request: &types.AccountBalanceRequest{
NetworkIdentifier: validNetworkIdentifier,
AccountIdentifier: validAccountIdentifier,
BlockIdentifier: &types.PartialBlockIdentifier{},
BlockIdentifier: &types.PartialBlockIdentifier{
Hash: &invalidBlockIdentifierHash,
},
},
allowHistorical: true,
err: ErrPartialBlockIdentifierFieldsNotSet,
err: ErrPartialBlockIdentifierHashIsEmpty,
},
"invalid historical request when block identifier index is invalid": {
request: &types.AccountBalanceRequest{
NetworkIdentifier: validNetworkIdentifier,
AccountIdentifier: validAccountIdentifier,
BlockIdentifier: &types.PartialBlockIdentifier{
Index: &invalidBlockIdentifierIndex,
},
},
allowHistorical: true,
err: ErrPartialBlockIdentifierIndexIsNegative,
},
"valid historical request when not enabled": {
request: &types.AccountBalanceRequest{
Expand Down Expand Up @@ -522,12 +538,23 @@ func TestBlockRequest(t *testing.T) {
},
err: ErrPartialBlockIdentifierIsNil,
},
"invalid PartialBlockIdentifier request": {
"invalid block request when block identifier hash is invalid": {
request: &types.BlockRequest{
NetworkIdentifier: validNetworkIdentifier,
BlockIdentifier: &types.PartialBlockIdentifier{},
BlockIdentifier: &types.PartialBlockIdentifier{
Hash: &invalidBlockIdentifierHash,
},
},
err: ErrPartialBlockIdentifierHashIsEmpty,
},
"invalid block request when block identifier index is invalid": {
request: &types.BlockRequest{
NetworkIdentifier: validNetworkIdentifier,
BlockIdentifier: &types.PartialBlockIdentifier{
Index: &invalidBlockIdentifierIndex,
},
},
err: ErrPartialBlockIdentifierFieldsNotSet,
err: ErrPartialBlockIdentifierIndexIsNegative,
},
}

Expand Down

0 comments on commit e03849b

Please sign in to comment.