Skip to content

Commit

Permalink
adds synced pow impl. changes ci config, adds additional comments
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-moser committed Nov 1, 2018
1 parent 37ef300 commit e12de47
Show file tree
Hide file tree
Showing 69 changed files with 639 additions and 477 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ go:
- 1.11.1

before_install:
- go get -u github.com/alecthomas/gometalinter
- go get -u github.com/mattn/goveralls
- go get -u github.com/mgechev/revive
- go get github.com/onsi/ginkgo/ginkgo
- export PATH=$PATH:$GOPATH/bin
- export CGO_LDFLAGS_ALLOW='-msse2,-mavx'
- export CGO_CFLAGS_ALLOW='-msse2,-mavx'
- export GO111MODULE='on'
- gometalinter --install --update
- go mod edit -replace golang.org/x/crypto=github.com/luca-moser/crypto@fb2ff8b1755486a8f59b48a2ad494e63f8477ec2

script:
- travis_wait 30 ginkgo -tags="pow_avx pow_sse pow_c pow_c128" -r --randomizeAllSpecs --randomizeSuites --failOnPending --cover --progress

after_success:
- gometalinter -e bindata --deadline=1000s ./...
- goveralls -coverprofile=coverage.out -service=travis-ci

- revive -config revive.toml -formatter stylish -exclude ./api/integration/... ./...
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

## Getting started

The client library is in beta and is subject to change. Use of this library in production applications is not supported.

### Installation

It is suggested to use [vgo modules](https://github.com/golang/go/wiki/Modules)
Expand Down Expand Up @@ -39,7 +41,7 @@ var endpoint = "<node-url>"

func main() {
// compose a new API instance
api, err := ComposeAPI(HttpClientSettings{URI: endpoint})
api, err := ComposeAPI(HTTPClientSettings{URI: endpoint})
must(err)

nodeInfo, err := api.GetNodeInfo()
Expand Down Expand Up @@ -89,14 +91,14 @@ const recipientAddress = "BBBB....."
func main() {

// get the best available PoW implementation
_, powFunc := pow.GetBestPoW()
_, proofOfWorkFunc := pow.GetFastestProofOfWorkImpl()

// create a new API instance
api, err := ComposeAPI(HttpClientSettings{
api, err := ComposeAPI(HTTPClientSettings{
URI: endpoint,
// (!) if no PoWFunc is supplied, then the connected node is requested to do PoW for us
// via the AttachToTangle() API call.
LocalPowFunc: powFunc,
LocalProofOfWorkFunc: proofOfWorkFunc,
})
must(err)

Expand All @@ -110,7 +112,7 @@ func main() {
}

// create inputs for the transfer
inputs := []Address{
inputs := []Input{
{
Address: "CCCCC....",
Security: SecurityLevelMedium,
Expand Down Expand Up @@ -177,7 +179,7 @@ Certain PoW implementations are enabled if the correct flags are passed while co
* `pow_c` for C based PoW

PoW implementation in Go is always available.
Make sure to define `LocalPoWFunc` in your provider settings (i.e. `HttpClientSettings`) if you want to use local PoW.
Make sure to define `LocalProofOfWorkFunc` in your provider settings (i.e. `HTTPClientSettings`) if you want to use local PoW.

## Contributing

Expand Down
1 change: 1 addition & 0 deletions address/address.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package address provides primitives for generating and validating addresses (with and without checksum).
package address

import (
Expand Down
15 changes: 8 additions & 7 deletions api/provider.go → api/api.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package api provides an API object for interacting with IRI nodes.
package api

import (
Expand All @@ -15,16 +16,16 @@ type Provider interface {

// API defines an object encapsulating the communication to connected nodes and API calls.
type API struct {
provider Provider
localPoWfunc pow.PowFunc
provider Provider
localProofOfWorkFunc pow.ProofOfWorkFunc
}

// A function which creates a new Provider.
// CreateProviderFunc is a function which creates a new Provider given some settings.
type CreateProviderFunc func(settings interface{}) (Provider, error)

// Settings can supply different options for Provider creation.
type Settings interface {
PowFunc() pow.PowFunc
ProofOfWorkFunc() pow.ProofOfWorkFunc
}

// ComposeAPI composes a new API from the given settings and provider.
Expand All @@ -39,15 +40,15 @@ func ComposeAPI(settings Settings, createProvider ...CreateProviderFunc) (*API,
if len(createProvider) > 0 && createProvider[0] != nil {
provider, err = createProvider[0](settings)
} else {
provider, err = NewHttpClient(settings)
provider, err = NewHTTPClient(settings)
}
if err != nil {
return nil, err
}

api := &API{provider: provider}
if settings.PowFunc() != nil {
api.localPoWfunc = settings.PowFunc()
if settings.ProofOfWorkFunc() != nil {
api.localProofOfWorkFunc = settings.ProofOfWorkFunc()
}

return api, nil
Expand Down
58 changes: 29 additions & 29 deletions api/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ const (
WereAddressesSpentFromCmd IRICommand = "wereAddressesSpentFrom"
)

// Command structure of the AddNeighbor API call.
// AddNeighborsCommand represents the payload to the AddNeighbor API call.
type AddNeighborsCommand struct {
Command IRICommand `json:"command"`
URIs []string `json:"uris"`
}

// Response returned by the AddNeighbor API call.
// AddNeighborsResponse is the response from the AddNeighbor API call.
type AddNeighborsResponse struct {
AddedNeighbors int64
Duration int64
}

// Command structure of the AttachToTangle API call.
// AttachToTangleCommand represents the payload to the AttachToTangle API call.
type AttachToTangleCommand struct {
Command IRICommand `json:"command"`
TrunkTransaction Hash `json:"trunkTransaction"`
Expand All @@ -48,83 +48,83 @@ type AttachToTangleCommand struct {
Trytes []Trytes `json:"trytes"`
}

// Response returned by the AttachToTangle API call.
// AttachToTangleResponse is the response from the AttachToTangle API call.
type AttachToTangleResponse struct {
Trytes []Trytes `json:"trytes"`
}

// Command structure of the BroadcastTransactions API call.
// BroadcastTransactionsCommand represents the payload to the BroadcastTransactions API call.
type BroadcastTransactionsCommand struct {
Command IRICommand `json:"command"`
Trytes []Trytes `json:"trytes"`
}

// Command structure of the CheckConsistency API call.
// CheckConsistencyCommand represents the payload to the CheckConsistency API call.
type CheckConsistencyCommand struct {
Command IRICommand `json:"command"`
Tails Hashes `json:"tails"`
}

// Response returned by the CheckConsistency API call.
// CheckConsistencyResponse is the response from the CheckConsistency API call.
type CheckConsistencyResponse struct {
State bool `json:"state"`
Info string `json:"info"`
}

// Command structure of the FindTransactions API call.
// FindTransactionsCommand represents the payload to the FindTransactions API call.
type FindTransactionsCommand struct {
FindTransactionsQuery
Command IRICommand `json:"command"`
}

// Response returned by the FindTransactions API call.
// FindTransactionsResponse is the response from the FindTransactions API call.
type FindTransactionsResponse struct {
Hashes Hashes `json:"hashes"`
}

// Command structure of the GetBalances API call.
// GetBalancesCommand represents the payload to the GetBalances API call.
type GetBalancesCommand struct {
Command IRICommand `json:"command"`
Addresses Hashes `json:"addresses"`
Threshold uint64 `json:"threshold"`
}

// Response returned by the GetBalances API call.
// GetBalancesResponse is the response from the GetBalances API call.
type GetBalancesResponse struct {
Balances []string `json:"balances"`
Duration int64 `json:"duration"`
Milestone string `json:"milestone"`
MilestoneIndex int64 `json:"milestoneIndex"`
}

// Command structure of the GetInclusionStates API call.
// GetInclusionStatesCommand represents the payload to the GetInclusionStates API call.
type GetInclusionStatesCommand struct {
Command IRICommand `json:"command"`
Transactions Hashes `json:"transactions"`
Tips Hashes `json:"tips"`
}

// Response returned by the GetInclusionStates API call.
// GetInclusionStatesResponse is the response from the GetInclusionStates API call.
type GetInclusionStatesResponse struct {
States []bool `json:"states"`
}

// Command structure of the GetNeighbors API call.
// GetNeighborsCommand represents the payload to the GetNeighbors API call.
type GetNeighborsCommand struct {
Command IRICommand `json:"command"`
}

// Response returned by the GetNeighbors API call.
// GetNeighborsResponse is the response from the GetNeighbors API call.
type GetNeighborsResponse struct {
Neighbors Neighbors `json:"neighbors"`
}

// Command structure of the GetNodeInfo API call.
// GetNodeInfoCommand represents the payload to the GetNodeInfo API call.
type GetNodeInfoCommand struct {
Command IRICommand `json:"command"`
}

// Response returned by the GetNodeInfo API call.
// GetNodeInfoResponse is the response from the GetNodeInfo API call.
type GetNodeInfoResponse struct {
AppName string `json:"appName"`
AppVersion string `json:"appVersion"`
Expand All @@ -144,70 +144,70 @@ type GetNodeInfoResponse struct {
TransactionsToRequest int64 `json:"transactionsToRequest"`
}

// Command structure of the GetTips API call.
// GetTipsCommand represents the payload to the GetTips API call.
type GetTipsCommand struct {
Command IRICommand `json:"command"`
}

// Response returned by the GetTips API call.
// GetTipsResponse is the response from the GetTips API call.
type GetTipsResponse struct {
Hashes Hashes `json:"hashes"`
}

// Command structure of the GetTransactionsToApprove API call.
// GetTransactionsToApproveCommand represents the payload to the GetTransactionsToApprove API call.
type GetTransactionsToApproveCommand struct {
Command IRICommand `json:"command"`
Depth uint64 `json:"depth"`
Reference Hash `json:"reference,omitempty"`
}

// Response returned by the GetTransactionsToApprove API call.
// GetTransactionsToApproveResponse is the response from the GetTransactionsToApprove API call.
type GetTransactionsToApproveResponse struct {
TransactionsToApprove
Duration int64 `json:"duration"`
}

// Command structure of the GetTrytes API call.
// GetTrytesCommand represents the payload to the GetTrytes API call.
type GetTrytesCommand struct {
Command IRICommand `json:"command"`
Hashes Hashes `json:"hashes"`
}

// Response returned by the GetTrytes API call.
// GetTrytesResponse is the response from the GetTrytes API call.
type GetTrytesResponse struct {
Trytes []Trytes `json:"trytes"`
}

// Command structure of the InterruptAttachToTangle API call.
// InterruptAttachToTangleCommand represents the payload to the InterruptAttachToTangle API call.
type InterruptAttachToTangleCommand struct {
Command IRICommand `json:"command"`
}

// Command structure of the RemoveNeighbors API call.
// RemoveNeighborsCommand represents the payload to the RemoveNeighbors API call.
type RemoveNeighborsCommand struct {
Command IRICommand `json:"command"`
URIs []string `json:"uris"`
}

// Response returned by the RemoveNeighbors API call.
// RemoveNeighborsResponse is the response from the RemoveNeighbors API call.
type RemoveNeighborsResponse struct {
RemovedNeighbors int64 `json:"removedNeighbors"`
Duration int64 `json:"duration"`
}

// Command structure of the StoreTransactions API call.
// StoreTransactionsCommand represents the payload to the StoreTransactions API call.
type StoreTransactionsCommand struct {
Command IRICommand `json:"command"`
Trytes []Trytes `json:"trytes"`
}

// Command structure of the WereAddressesSpentFrom API call.
// WereAddressesSpentFromCommand represents the payload to the WereAddressesSpentFrom API call.
type WereAddressesSpentFromCommand struct {
Command IRICommand `json:"command"`
Addresses Hashes `json:"addresses"`
}

// Response returned by the WereAddressesSpentFrom API call.
// WereAddressesSpentFromResponse is the response from the WereAddressesSpentFrom API call.
type WereAddressesSpentFromResponse struct {
States []bool `json:"states"`
}
30 changes: 19 additions & 11 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,47 @@ import (
"net/url"
)

// DefaultLocalIRIURI is the default URI used when none is given in HTTPClientSettings.
const DefaultLocalIRIURI = "http://localhost:14265"

func NewHttpClient(settings interface{}) (*httpclient, error) {
// NewHTTPClient creates a new Http Provider.
func NewHTTPClient(settings interface{}) (Provider, error) {
httpClient := &httpclient{}
if err := httpClient.SetSettings(settings); err != nil {
return nil, err
}
return httpClient, nil
}

type HttpClientSettings struct {
URI string
Client HttpClient
LocalPowFunc pow.PowFunc
// HTTPClientSettings defines a set of settings for when constructing a new Http Provider.
type HTTPClientSettings struct {
// The URI endpoint to connect to. Defaults to DefaultLocalIRIURI if empty.
URI string
// The underlying HTTPClient to use. Defaults to http.DefaultClient.
Client HTTPClient
// The Proof-of-Work implementation function. Defaults to use the AttachToTangle IRI API call.
LocalProofOfWorkFunc pow.ProofOfWorkFunc
}

func (hcs HttpClientSettings) PowFunc() pow.PowFunc {
return hcs.LocalPowFunc
// ProofOfWorkFunc returns the defined Proof-of-Work function.
func (hcs HTTPClientSettings) ProofOfWorkFunc() pow.ProofOfWorkFunc {
return hcs.LocalProofOfWorkFunc
}

type HttpClient interface {
// HTTPClient defines an object being able to do Http calls.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

type httpclient struct {
client HttpClient
client HTTPClient
endpoint string
}

func (hc *httpclient) SetSettings(settings interface{}) error {
httpSettings, ok := settings.(HttpClientSettings)
httpSettings, ok := settings.(HTTPClientSettings)
if !ok {
return errors.Wrapf(ErrInvalidSettingsType, "expected %T", HttpClientSettings{})
return errors.Wrapf(ErrInvalidSettingsType, "expected %T", HTTPClientSettings{})
}
if len(httpSettings.URI) == 0 {
hc.endpoint = DefaultLocalIRIURI
Expand Down
2 changes: 1 addition & 1 deletion api/integration/add_neighbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var _ = Describe("AddNeighbors()", func() {
var api *API

BeforeEach(func() {
a, err := ComposeAPI(HttpClientSettings{}, nil)
a, err := ComposeAPI(HTTPClientSettings{}, nil)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit e12de47

Please sign in to comment.