Skip to content

Commit

Permalink
Merge pull request #7 from coinbase/patrick/server-sdk
Browse files Browse the repository at this point in the history
Add Server, Client, and Models Package
  • Loading branch information
patrick-ogrady authored Apr 10, 2020
2 parents b90b717 + 5f9ab5b commit 48078aa
Show file tree
Hide file tree
Showing 106 changed files with 3,156 additions and 895 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
c.out
coverage.html

examples/server/server
examples/client/client
examples/fetcher/fetcher
16 changes: 11 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
.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
TEST_SCRIPT=go test -v ./asserter/... ./fetcher/... ./gen/...
GO_PACKAGES=./asserter/... ./fetcher/... ./models/... ./client/... ./server/...
GO_FOLDERS=$(shell echo ${GO_PACKAGES} | sed -e "s/\.\///g" | sed -e "s/\/\.\.\.//g")
TEST_SCRIPT=go test -v ${GO_PACKAGES}
LINT_SETTINGS=golint,misspell,gocyclo,gocritic,whitespace,goconst,gocognit,bodyclose,unconvert,lll,unparam

deps:
go get ./...
Expand All @@ -14,9 +17,12 @@ deps:
gen:
./codegen.sh

lint:
golangci-lint run -v \
-E golint,misspell,gocyclo,gocritic,whitespace,goconst,gocognit,bodyclose,unconvert,lll,unparam,gomnd
lint-examples:
cd examples; \
golangci-lint run -v -E ${LINT_SETTINGS}

lint: | lint-examples
golangci-lint run -v -E ${LINT_SETTINGS},gomnd

format:
gofmt -s -w -l .
Expand All @@ -38,7 +44,7 @@ check-license:
${LICENCE_SCRIPT} -check .

shorten-lines:
golines -w --shorten-comments asserter fetcher gen
golines -w --shorten-comments ${GO_FOLDERS} examples

shellcheck:
shellcheck codegen.sh
Expand Down
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,24 @@ in this specification will enable exchanges, block explorers,
and wallets to integrate with much less communication overhead
and network-specific work.

## Versioning
- Rosetta version: 1.3.0

## Installation

```shell
go get github.com/coinbase/rosetta-sdk-go
```

## Automatic Assertion
When using the helper methods to access a Rosetta Server (in `fetcher/*.go`),
responses from the server are automatically checked for adherence to
the Rosetta Interface. For example, if a `BlockIdentifer` is returned without a
`Hash`, the fetch will fail. Take a look at the tests in `asserter/*_test.go`
if you are curious about what exactly is asserted.

_It is possible, but not recommended, to bypass this assertion using the
`unsafe` helper methods available in `fetcher/*.go`._
## Packages
* [Models](models): Auto-generated Rosetta models
* [Client](client): Low-level communication with any Rosetta server
* [Server](server): Simplified Rosetta server development
* [Asserter](asserter): Validation of Rosetta models
* [Fetcher](fetcher): Simplified and validated communication with
any Rosetta server

## Examples
The packages listed above are demoed extensively in
[examples](examples) and are utilized throughout the
[Rosetta Validator](https://github.com/coinbase/rosetta-validator).

## Development
* `make deps` to install dependencies
* `make gen` to generate models and helpers
* `make test` to run tests
* `make lint` to lint the source code (included generated code)
* `make lint` to lint the source code (including generated code)
* `make release` to check if code passes all tests run by CircleCI

## License
Expand Down
17 changes: 17 additions & 0 deletions asserter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Asserter

[![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=shield)](https://pkg.go.dev/github.com/coinbase/rosetta-sdk-go/asserter?tab=overview)

The Asserter package is used to validate the correctness of Rosetta models. It is
important to note that this validation only ensures that required fields are
populated, fields are in the correct format, and transaction operations only
contain types and statuses specified in the /network/status endpoint.

If you want more intensive validation, try running the
[Rosetta Validator](https://github.com/coinbase/rosetta-validator).

## Installation

```shell
go get github.com/coinbase/rosetta-sdk-go/asserter
```
42 changes: 21 additions & 21 deletions asserter/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import (
"fmt"
"reflect"

rosetta "github.com/coinbase/rosetta-sdk-go/gen"
"github.com/coinbase/rosetta-sdk-go/models"
)

// containsAccountIdentifier returns a boolean indicating if a
// *rosetta.AccountIdentifier is contained within a slice of
// *rosetta.AccountIdentifier. The check for equality takes
// into account everything within the rosetta.AccountIdentifier
// *models.AccountIdentifier is contained within a slice of
// *models.AccountIdentifier. The check for equality takes
// into account everything within the models.AccountIdentifier
// struct (including the SubAccountIdentifier).
func containsAccountIdentifier(
identifiers []*rosetta.AccountIdentifier,
identifier *rosetta.AccountIdentifier,
identifiers []*models.AccountIdentifier,
identifier *models.AccountIdentifier,
) bool {
for _, ident := range identifiers {
if reflect.DeepEqual(ident, identifier) {
Expand All @@ -40,11 +40,11 @@ func containsAccountIdentifier(
}

// containsCurrency returns a boolean indicating if a
// *rosetta.Currency is contained within a slice of
// *rosetta.Currency. The check for equality takes
// into account everything within the rosetta.Currency
// *models.Currency is contained within a slice of
// *models.Currency. The check for equality takes
// into account everything within the models.Currency
// struct (including currency.Metadata).
func containsCurrency(currencies []*rosetta.Currency, currency *rosetta.Currency) bool {
func containsCurrency(currencies []*models.Currency, currency *models.Currency) bool {
for _, curr := range currencies {
if reflect.DeepEqual(curr, currency) {
return true
Expand All @@ -55,12 +55,12 @@ func containsCurrency(currencies []*rosetta.Currency, currency *rosetta.Currency
}

// assertBalanceAmounts returns an error if a slice
// of rosetta.Amount returned as an rosetta.AccountIdentifier's
// of models.Amount returned as an models.AccountIdentifier's
// balance is invalid. It is considered invalid if the same
// currency is returned multiple times (these shoould be
// consolidated) or if a rosetta.Amount is considered invalid.
func assertBalanceAmounts(amounts []*rosetta.Amount) error {
currencies := make([]*rosetta.Currency, 0)
// consolidated) or if a models.Amount is considered invalid.
func assertBalanceAmounts(amounts []*models.Amount) error {
currencies := make([]*models.Currency, 0)
for _, amount := range amounts {
// Ensure a currency is used at most once in balance.Amounts
if containsCurrency(currencies, amount.Currency) {
Expand All @@ -77,19 +77,19 @@ func assertBalanceAmounts(amounts []*rosetta.Amount) error {
}

// AccountBalance returns an error if the provided
// rosetta.BlockIdentifier is invalid, if the same
// rosetta.AccountIdentifier appears in multiple
// rosetta.Balance structs (should be consolidated),
// or if a rosetta.Balance is considered invalid.
// models.BlockIdentifier is invalid, if the same
// models.AccountIdentifier appears in multiple
// models.Balance structs (should be consolidated),
// or if a models.Balance is considered invalid.
func AccountBalance(
block *rosetta.BlockIdentifier,
balances []*rosetta.Balance,
block *models.BlockIdentifier,
balances []*models.Balance,
) error {
if err := BlockIdentifier(block); err != nil {
return err
}

accounts := make([]*rosetta.AccountIdentifier, 0)
accounts := make([]*models.AccountIdentifier, 0)
for _, balance := range balances {
if err := AccountIdentifier(balance.AccountIdentifier); err != nil {
return err
Expand Down
Loading

0 comments on commit 48078aa

Please sign in to comment.