diff --git a/client/lib.go b/client/lib.go index fbe735e7c5459064ad920fa351d811994bcd7a33..d60200d6110c02d0017b9cf5885be9b8c80d4b1d 100644 --- a/client/lib.go +++ b/client/lib.go @@ -40,13 +40,16 @@ const ( contentTypeJSON = "application/json" ) -func NewGoShimmerAPI(node string) *GoShimmerAPI { +func NewGoShimmerAPI(node string, httpClient ...http.Client) *GoShimmerAPI { + if len(httpClient) > 0 { + return &GoShimmerAPI{node: node, httpClient: httpClient[0]} + } return &GoShimmerAPI{node: node} } type GoShimmerAPI struct { - http.Client - node string + httpClient http.Client + node string } type errorresponse struct { @@ -60,7 +63,7 @@ func interpretBody(res *http.Response, decodeTo interface{}) error { } defer res.Body.Close() - if res.StatusCode == http.StatusOK { + if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated { return json.Unmarshal(resBody, decodeTo) } @@ -80,20 +83,17 @@ func interpretBody(res *http.Response, decodeTo interface{}) error { return errors.Wrap(ErrUnknownError, errRes.Error) } -func (api *GoShimmerAPI) BroadcastData(targetAddress trinary.Trytes, data trinary.Trytes) (trinary.Hash, error) { +func (api *GoShimmerAPI) BroadcastData(targetAddress trinary.Trytes, data string) (trinary.Hash, error) { if !guards.IsHash(targetAddress) { return "", errors.Wrapf(consts.ErrInvalidHash, "invalid address: %s", targetAddress) } - if !guards.IsTrytes(data) { - return "", errors.Wrapf(consts.ErrInvalidTrytes, "invalid trytes: %s", data) - } reqBytes, err := json.Marshal(&webapi_broadcastData.Request{Address: targetAddress, Data: data}) if err != nil { return "", err } - res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeBroadcastData), contentTypeJSON, bytes.NewReader(reqBytes)) + res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeBroadcastData), contentTypeJSON, bytes.NewReader(reqBytes)) if err != nil { return "", err } @@ -118,7 +118,7 @@ func (api *GoShimmerAPI) GetTrytes(txHashes trinary.Hashes) ([]trinary.Trytes, e return nil, err } - res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeGetTrytes), contentTypeJSON, bytes.NewReader(reqBytes)) + res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeGetTrytes), contentTypeJSON, bytes.NewReader(reqBytes)) if err != nil { return nil, err } @@ -143,7 +143,7 @@ func (api *GoShimmerAPI) GetTransactions(txHashes trinary.Hashes) ([]webapi_getT return nil, err } - res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeGetTransactions), contentTypeJSON, bytes.NewReader(reqBytes)) + res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeGetTransactions), contentTypeJSON, bytes.NewReader(reqBytes)) if err != nil { return nil, err } @@ -168,7 +168,7 @@ func (api *GoShimmerAPI) FindTransactions(query *webapi_findTransactions.Request return nil, err } - res, err := api.Post(fmt.Sprintf("%s/%s", api.node, routeFindTransactions), contentTypeJSON, bytes.NewReader(reqBytes)) + res, err := api.httpClient.Post(fmt.Sprintf("%s/%s", api.node, routeFindTransactions), contentTypeJSON, bytes.NewReader(reqBytes)) if err != nil { return nil, err } @@ -181,8 +181,9 @@ func (api *GoShimmerAPI) FindTransactions(query *webapi_findTransactions.Request return resObj.Transactions, nil } + func (api *GoShimmerAPI) GetNeighbors() (*webapi_getNeighbors.Response, error) { - res, err := api.Get(fmt.Sprintf("%s/%s", api.node, routeGetNeighbors)) + res, err := api.httpClient.Get(fmt.Sprintf("%s/%s", api.node, routeGetNeighbors)) if err != nil { return nil, err } @@ -195,8 +196,9 @@ func (api *GoShimmerAPI) GetNeighbors() (*webapi_getNeighbors.Response, error) { return resObj, nil } + func (api *GoShimmerAPI) GetTips() (*webapi_gtta.Response, error) { - res, err := api.Get(fmt.Sprintf("%s/%s", api.node, routeGetTransactionsToApprove)) + res, err := api.httpClient.Get(fmt.Sprintf("%s/%s", api.node, routeGetTransactionsToApprove)) if err != nil { return nil, err } @@ -210,7 +212,7 @@ func (api *GoShimmerAPI) GetTips() (*webapi_gtta.Response, error) { } func (api *GoShimmerAPI) ToggleSpammer(enable bool) (*webapi_spammer.Response, error) { - res, err := api.Get(fmt.Sprintf("%s/%s?cmd=%s", api.node, routeSpammer, func() string { + res, err := api.httpClient.Get(fmt.Sprintf("%s/%s?cmd=%s", api.node, routeSpammer, func() string { if enable { return "start" } diff --git a/plugins/tangle/solidifier.go b/plugins/tangle/solidifier.go index 2ab00159f6fe8da8aab1b502d69013b9fb14d72c..1a90076bad0f6e8b486aee6cb2bebe3d77d492a2 100644 --- a/plugins/tangle/solidifier.go +++ b/plugins/tangle/solidifier.go @@ -181,7 +181,9 @@ func processMetaTransaction(metaTransaction *meta_transaction.MetaTransaction) { if tx, err := GetTransaction(metaTransaction.GetHash(), func(transactionHash trinary.Trytes) *value_transaction.ValueTransaction { newTransaction = true - return value_transaction.FromMetaTransaction(metaTransaction) + tx := value_transaction.FromMetaTransaction(metaTransaction) + tx.SetModified(true) + return tx }); err != nil { log.Errorf("Unable to load transaction %s: %s", metaTransaction.GetHash(), err.Error()) } else if newTransaction { diff --git a/plugins/webapi/findTransactions/plugin.go b/plugins/webapi/findTransactions/plugin.go index 9c4fc937a25f98f7c76444a905be80f7a7fd087b..ff9b406ad2af9af2d8f64b39e09fcf5970df146e 100644 --- a/plugins/webapi/findTransactions/plugin.go +++ b/plugins/webapi/findTransactions/plugin.go @@ -16,7 +16,7 @@ var log *logger.Logger func configure(plugin *node.Plugin) { log = logger.NewLogger("API-findTransactions") - webapi.Server.GET("findTransactions", findTransactions) + webapi.Server.POST("findTransactions", findTransactions) } // findTransactions returns the array of transaction hashes for the @@ -24,7 +24,6 @@ func configure(plugin *node.Plugin) { // If a node doesn't have any transaction hash for a given address in its ledger, // the value at the index of that address is empty. func findTransactions(c echo.Context) error { - var request Request if err := c.Bind(&request); err != nil { diff --git a/plugins/webapi/getTransactions/plugin.go b/plugins/webapi/getTransactions/plugin.go index 448243f94bd6970b379de75bd0b0fa116d23f7d7..70c231328b29c32e619e3c2b5927fac6c85c4557 100644 --- a/plugins/webapi/getTransactions/plugin.go +++ b/plugins/webapi/getTransactions/plugin.go @@ -16,7 +16,7 @@ var log *logger.Logger func configure(plugin *node.Plugin) { log = logger.NewLogger("API-getTransactions") - webapi.Server.GET("getTransactions", getTransactions) + webapi.Server.POST("getTransactions", getTransactions) } // getTransactions returns the array of transactions for the