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 Parser Package #21

Merged
merged 6 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 20 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
.PHONY: gen-deps deps gen lint format check-format test test-coverage add-license \
.PHONY: deps gen lint format check-format test test-coverage add-license \
check-license shorten-lines shellcheck salus release
LICENCE_SCRIPT=addlicense -c "Coinbase, Inc." -l "apache" -v
GO_PACKAGES=./asserter/... ./fetcher/... ./types/... ./client/... ./server/...

# To run the the following packages as commands,
# it is necessary to use `go run <pkg>`. Running `go get` does
# not install any binaries that could be used to run
# the commands directly.
ADDLICENSE_CMD=go run github.com/google/addlicense
ADDLICENCE_SCRIPT=${ADDLICENSE_CMD} -c "Coinbase, Inc." -l "apache" -v
GOIMPORTS_CMD=go run golang.org/x/tools/cmd/goimports
GOLINES_CMD=go run github.com/segmentio/golines
GOVERALLS_CMD=go run github.com/mattn/goveralls

GO_PACKAGES=./asserter/... ./fetcher/... ./types/... ./client/... ./server/... ./parser/...
GO_FOLDERS=$(shell echo ${GO_PACKAGES} | sed -e "s/\.\///g" | sed -e "s/\/\.\.\.//g")
GO_INSTALL=GO111MODULE=off go get
TEST_SCRIPT=go test -v ${GO_PACKAGES}
LINT_SETTINGS=golint,misspell,gocyclo,gocritic,whitespace,goconst,gocognit,bodyclose,unconvert,lll,unparam

deps: | gen-deps
deps:
go get ./...
${GO_INSTALL} github.com/mattn/goveralls

gen-deps:
${GO_INSTALL} github.com/google/addlicense
${GO_INSTALL} github.com/segmentio/golines
${GO_INSTALL} golang.org/x/tools/cmd/goimports

gen:
./codegen.sh
Expand All @@ -31,27 +34,27 @@ lint: | lint-examples

format:
gofmt -s -w -l .
goimports -w .
${GOIMPORTS_CMD} -w .

check-format:
! gofmt -s -l . | read
! goimports -l . | read
! ${GOIMPORTS_CMD} -l . | read

test:
${TEST_SCRIPT}

test-cover:
${TEST_SCRIPT} -coverprofile=c.out -covermode=count
goveralls -coverprofile=c.out -repotoken ${COVERALLS_TOKEN}
${GOVERALLS_CMD} -coverprofile=c.out -repotoken ${COVERALLS_TOKEN}

add-license:
${LICENCE_SCRIPT} .
${ADDLICENCE_SCRIPT} .

check-license:
${LICENCE_SCRIPT} -check .
${ADDLICENCE_SCRIPT} -check .

shorten-lines:
golines -w --shorten-comments ${GO_FOLDERS} examples
${GOLINES_CMD} -w --shorten-comments ${GO_FOLDERS} examples

shellcheck:
shellcheck codegen.sh
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and network-specific work.
* [Asserter](asserter): Validation of Rosetta types
* [Fetcher](fetcher): Simplified and validated communication with
any Rosetta server
* [Parser](parser): Tool for parsing Rosetta blocks

## Examples
The packages listed above are demoed extensively in
Expand Down
56 changes: 50 additions & 6 deletions asserter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func Amount(amount *types.Amount) error {
return errors.New("Amount.Currency.Symbol is empty")
}

if amount.Currency.Decimals <= 0 {
return errors.New("Amount.Currency.Decimals must be > 0")
if amount.Currency.Decimals < 0 {
return errors.New("Amount.Currency.Decimals must be >= 0")
}

return nil
Expand All @@ -66,10 +66,18 @@ func OperationIdentifier(
identifier *types.OperationIdentifier,
index int64,
) error {
if identifier == nil || identifier.Index != index {
if identifier == nil {
return errors.New("Operation.OperationIdentifier.Index invalid")
}

if identifier.Index != index {
return fmt.Errorf(
"Operation.OperationIdentifier.Index %d is out of order, expected %d",
identifier.Index,
index,
)
}

if identifier.NetworkIndex != nil && *identifier.NetworkIndex < 0 {
return errors.New("Operation.OperationIdentifier.NetworkIndex invalid")
}
Expand Down Expand Up @@ -99,9 +107,21 @@ func AccountIdentifier(account *types.AccountIdentifier) error {
return nil
}

// contains checks if a string is contained in a slice
// containsString checks if an string is contained in a slice
// of strings.
func contains(valid []string, value string) bool {
func containsString(valid []string, value string) bool {
for _, v := range valid {
if v == value {
return true
}
}

return false
}

// containsInt64 checks if an int64 is contained in a slice
// of Int64.
func containsInt64(valid []int64, value int64) bool {
for _, v := range valid {
if v == value {
return true
Expand Down Expand Up @@ -136,7 +156,7 @@ func (a *Asserter) OperationType(t string) error {
return ErrAsserterNotInitialized
}

if t == "" || !contains(a.operationTypes, t) {
if t == "" || !containsString(a.operationTypes, t) {
return fmt.Errorf("Operation.Type %s is invalid", t)
}

Expand Down Expand Up @@ -255,9 +275,33 @@ func (a *Asserter) Transaction(
}

for i, op := range transaction.Operations {
// Ensure operations are sorted
if err := a.Operation(op, int64(i)); err != nil {
return err
}

// Ensure an operation's related_operations are only
// operations with an index less than the operation
// and that there are no duplicates.
relatedIndexes := []int64{}
for _, relatedOp := range op.RelatedOperations {
if relatedOp.Index >= op.OperationIdentifier.Index {
return fmt.Errorf(
"related operation index %d >= operation index %d",
relatedOp.Index,
op.OperationIdentifier.Index,
)
}

if containsInt64(relatedIndexes, relatedOp.Index) {
return fmt.Errorf(
"found duplicate related operation index %d for operation index %d",
relatedOp.Index,
op.OperationIdentifier.Index,
)
}
relatedIndexes = append(relatedIndexes, relatedOp.Index)
}
}

return nil
Expand Down
Loading