Skip to content
Snippets Groups Projects
Unverified Commit 806ee367 authored by Ching-Hua (Vivian) Lin's avatar Ching-Hua (Vivian) Lin Committed by GitHub
Browse files

refactor: Flatten webapi (#753)

* refactor: Flatten webapi plugin

* fix: Remove settings for testing

* refactor: Update disabled plugins in docker-network and intergration-tests

* fix: Unexport log

* fix: Upadate the imported package in consensus integration test

* fix: Fix :dog:

* refactor: Make endpoints as subplugins

* fix: Fix :dog:

* refactor: Rename utils.go to common.go

* refactor: Unexported api handlers

* fix: Minor fix

* refactor: Move drng/info api to drng/

* refactor: Move spammer from webapi to core plugins
parent 1aaf9c57
No related branches found
No related tags found
No related merge requests found
Showing
with 238 additions and 203 deletions
......@@ -3,9 +3,7 @@ package client
import (
"net/http"
webapi_collectiveBeacon "github.com/iotaledger/goshimmer/plugins/webapi/drng/collectivebeacon"
webapi_committee "github.com/iotaledger/goshimmer/plugins/webapi/drng/info/committee"
webapi_randomness "github.com/iotaledger/goshimmer/plugins/webapi/drng/info/randomness"
webapi_drng "github.com/iotaledger/goshimmer/plugins/webapi/drng"
)
const (
......@@ -17,9 +15,9 @@ const (
// BroadcastCollectiveBeacon sends the given collective beacon (payload) by creating a message in the backend.
func (api *GoShimmerAPI) BroadcastCollectiveBeacon(payload []byte) (string, error) {
res := &webapi_collectiveBeacon.Response{}
res := &webapi_drng.CollectiveBeaconResponse{}
if err := api.do(http.MethodPost, routeCollectiveBeacon,
&webapi_collectiveBeacon.Request{Payload: payload}, res); err != nil {
&webapi_drng.CollectiveBeaconRequest{Payload: payload}, res); err != nil {
return "", err
}
......@@ -27,8 +25,8 @@ func (api *GoShimmerAPI) BroadcastCollectiveBeacon(payload []byte) (string, erro
}
// GetRandomness gets the current randomness.
func (api *GoShimmerAPI) GetRandomness() (*webapi_randomness.Response, error) {
res := &webapi_randomness.Response{}
func (api *GoShimmerAPI) GetRandomness() (*webapi_drng.RandomnessResponse, error) {
res := &webapi_drng.RandomnessResponse{}
if err := api.do(http.MethodGet, func() string {
return routeRandomness
}(), nil, res); err != nil {
......@@ -38,8 +36,8 @@ func (api *GoShimmerAPI) GetRandomness() (*webapi_randomness.Response, error) {
}
// GetCommittee gets the current committee.
func (api *GoShimmerAPI) GetCommittee() (*webapi_committee.Response, error) {
res := &webapi_committee.Response{}
func (api *GoShimmerAPI) GetCommittee() (*webapi_drng.CommitteeResponse, error) {
res := &webapi_drng.CommitteeResponse{}
if err := api.do(http.MethodGet, func() string {
return routeCommittee
}(), nil, res); err != nil {
......
......@@ -13,13 +13,13 @@ const (
// FindMessageByID finds messages by the given base58 encoded IDs. The messages are returned in the same order as
// the given IDs. Non available messages are empty at their corresponding index.
func (api *GoShimmerAPI) FindMessageByID(base58EncodedIDs []string) (*webapi_message.Response, error) {
res := &webapi_message.Response{}
func (api *GoShimmerAPI) FindMessageByID(base58EncodedIDs []string) (*webapi_message.FindByIDResponse, error) {
res := &webapi_message.FindByIDResponse{}
if err := api.do(
http.MethodPost,
routeFindByID,
&webapi_message.Request{IDs: base58EncodedIDs},
&webapi_message.FindByIDRequest{IDs: base58EncodedIDs},
res,
); err != nil {
return nil, err
......@@ -30,9 +30,9 @@ func (api *GoShimmerAPI) FindMessageByID(base58EncodedIDs []string) (*webapi_mes
// SendPayload send a message with the given payload.
func (api *GoShimmerAPI) SendPayload(payload []byte) (string, error) {
res := &webapi_message.MsgResponse{}
res := &webapi_message.SendPayloadResponse{}
if err := api.do(http.MethodPost, routeSendPayload,
&webapi_message.MsgRequest{Payload: payload}, res); err != nil {
&webapi_message.SendPayloadRequest{Payload: payload}, res); err != nil {
return "", err
}
......
......@@ -4,7 +4,7 @@ import (
"fmt"
"net/http"
webapi_spammer "github.com/iotaledger/goshimmer/plugins/webapi/spammer"
webapi_spammer "github.com/iotaledger/goshimmer/plugins/spammer"
)
const (
......
......@@ -4,10 +4,8 @@ import (
"errors"
"net/http"
webapi_missing "github.com/iotaledger/goshimmer/plugins/webapi/tools/message/missing"
webapi_pastcone "github.com/iotaledger/goshimmer/plugins/webapi/tools/message/pastcone"
webapi_value_objects "github.com/iotaledger/goshimmer/plugins/webapi/tools/value/objects"
webapi_value_tips "github.com/iotaledger/goshimmer/plugins/webapi/tools/value/tips"
webapi_tools_message "github.com/iotaledger/goshimmer/plugins/webapi/tools/message"
webapi_tools_value "github.com/iotaledger/goshimmer/plugins/webapi/tools/value"
)
const (
......@@ -22,13 +20,13 @@ const (
// PastConeExist checks that all of the messages in the past cone of a message are existing on the node
// down to the genesis. Returns the number of messages in the past cone as well.
func (api *GoShimmerAPI) PastConeExist(base58EncodedMessageID string) (*webapi_pastcone.Response, error) {
res := &webapi_pastcone.Response{}
func (api *GoShimmerAPI) PastConeExist(base58EncodedMessageID string) (*webapi_tools_message.PastconeResponse, error) {
res := &webapi_tools_message.PastconeResponse{}
if err := api.do(
http.MethodGet,
routePastCone,
&webapi_pastcone.Request{ID: base58EncodedMessageID},
&webapi_tools_message.PastconeRequest{ID: base58EncodedMessageID},
res,
); err != nil {
return nil, err
......@@ -41,8 +39,8 @@ func (api *GoShimmerAPI) PastConeExist(base58EncodedMessageID string) (*webapi_p
}
// Missing returns all the missing messages and their count.
func (api *GoShimmerAPI) Missing() (*webapi_missing.Response, error) {
res := &webapi_missing.Response{}
func (api *GoShimmerAPI) Missing() (*webapi_tools_message.MissingResponse, error) {
res := &webapi_tools_message.MissingResponse{}
if err := api.do(http.MethodGet, routeMissing, nil, res); err != nil {
return nil, err
}
......@@ -52,8 +50,8 @@ func (api *GoShimmerAPI) Missing() (*webapi_missing.Response, error) {
// ------------------- Value layer -----------------------------
// ValueTips returns the value objects info from the tips.
func (api *GoShimmerAPI) ValueTips() (*webapi_value_tips.Response, error) {
res := &webapi_value_tips.Response{}
func (api *GoShimmerAPI) ValueTips() (*webapi_tools_value.TipsResponse, error) {
res := &webapi_tools_value.TipsResponse{}
if err := api.do(http.MethodGet, routeValueTips, nil, res); err != nil {
return nil, err
}
......@@ -61,8 +59,8 @@ func (api *GoShimmerAPI) ValueTips() (*webapi_value_tips.Response, error) {
}
// ValueObjects returns the list of value objects.
func (api *GoShimmerAPI) ValueObjects() (*webapi_value_objects.Response, error) {
res := &webapi_value_objects.Response{}
func (api *GoShimmerAPI) ValueObjects() (*webapi_tools_value.ObjectsResponse, error) {
res := &webapi_tools_value.ObjectsResponse{}
if err := api.do(http.MethodGet, routeValueDebug, nil, res); err != nil {
return nil, err
}
......
......@@ -4,11 +4,7 @@ import (
"fmt"
"net/http"
webapi_attachments "github.com/iotaledger/goshimmer/plugins/webapi/value/attachments"
webapi_gettxn "github.com/iotaledger/goshimmer/plugins/webapi/value/gettransactionbyid"
webapi_sendtxn "github.com/iotaledger/goshimmer/plugins/webapi/value/sendtransaction"
webapi_sendtxnbyjson "github.com/iotaledger/goshimmer/plugins/webapi/value/sendtransactionbyjson"
webapi_unspentoutputs "github.com/iotaledger/goshimmer/plugins/webapi/value/unspentoutputs"
webapi_value "github.com/iotaledger/goshimmer/plugins/webapi/value"
)
const (
......@@ -20,8 +16,8 @@ const (
)
// GetAttachments gets the attachments of a transaction ID
func (api *GoShimmerAPI) GetAttachments(base58EncodedTxnID string) (*webapi_attachments.Response, error) {
res := &webapi_attachments.Response{}
func (api *GoShimmerAPI) GetAttachments(base58EncodedTxnID string) (*webapi_value.AttachmentsResponse, error) {
res := &webapi_value.AttachmentsResponse{}
if err := api.do(http.MethodGet, func() string {
return fmt.Sprintf("%s?txnID=%s", routeAttachments, base58EncodedTxnID)
}(), nil, res); err != nil {
......@@ -32,8 +28,8 @@ func (api *GoShimmerAPI) GetAttachments(base58EncodedTxnID string) (*webapi_atta
}
// GetTransactionByID gets the transaction of a transaction ID
func (api *GoShimmerAPI) GetTransactionByID(base58EncodedTxnID string) (*webapi_gettxn.Response, error) {
res := &webapi_gettxn.Response{}
func (api *GoShimmerAPI) GetTransactionByID(base58EncodedTxnID string) (*webapi_value.GetTransactionByIDResponse, error) {
res := &webapi_value.GetTransactionByIDResponse{}
if err := api.do(http.MethodGet, func() string {
return fmt.Sprintf("%s?txnID=%s", routeGetTxnByID, base58EncodedTxnID)
}(), nil, res); err != nil {
......@@ -44,10 +40,10 @@ func (api *GoShimmerAPI) GetTransactionByID(base58EncodedTxnID string) (*webapi_
}
// GetUnspentOutputs return unspent output IDs of addresses
func (api *GoShimmerAPI) GetUnspentOutputs(addresses []string) (*webapi_unspentoutputs.Response, error) {
res := &webapi_unspentoutputs.Response{}
func (api *GoShimmerAPI) GetUnspentOutputs(addresses []string) (*webapi_value.UnspentOutputsResponse, error) {
res := &webapi_value.UnspentOutputsResponse{}
if err := api.do(http.MethodPost, routeUnspentOutputs,
&webapi_unspentoutputs.Request{Addresses: addresses}, res); err != nil {
&webapi_value.UnspentOutputsRequest{Addresses: addresses}, res); err != nil {
return nil, err
}
......@@ -56,9 +52,9 @@ func (api *GoShimmerAPI) GetUnspentOutputs(addresses []string) (*webapi_unspento
// SendTransaction sends the transaction(bytes) to the Value Tangle and returns transaction ID.
func (api *GoShimmerAPI) SendTransaction(txnBytes []byte) (string, error) {
res := &webapi_sendtxn.Response{}
res := &webapi_value.SendTransactionResponse{}
if err := api.do(http.MethodPost, routeSendTxn,
&webapi_sendtxn.Request{TransactionBytes: txnBytes}, res); err != nil {
&webapi_value.SendTransactionRequest{TransactionBytes: txnBytes}, res); err != nil {
return "", err
}
......@@ -66,10 +62,10 @@ func (api *GoShimmerAPI) SendTransaction(txnBytes []byte) (string, error) {
}
// SendTransactionByJSON sends the transaction(JSON) to the Value Tangle and returns transaction ID.
func (api *GoShimmerAPI) SendTransactionByJSON(txn webapi_sendtxnbyjson.Request) (string, error) {
res := &webapi_sendtxn.Response{}
func (api *GoShimmerAPI) SendTransactionByJSON(txn webapi_value.SendTransactionByJSONRequest) (string, error) {
res := &webapi_value.SendTransactionByJSONResponse{}
if err := api.do(http.MethodPost, routeSendTxnByJSON,
&webapi_sendtxnbyjson.Request{
&webapi_value.SendTransactionByJSONRequest{
Inputs: txn.Inputs,
Outputs: txn.Outputs,
Data: txn.Data,
......
......@@ -19,6 +19,7 @@ import (
"github.com/iotaledger/goshimmer/plugins/portcheck"
"github.com/iotaledger/goshimmer/plugins/pow"
"github.com/iotaledger/goshimmer/plugins/profiling"
"github.com/iotaledger/goshimmer/plugins/spammer"
"github.com/iotaledger/goshimmer/plugins/syncbeacon"
"github.com/iotaledger/goshimmer/plugins/syncbeaconfollower"
......@@ -46,5 +47,6 @@ var PLUGINS = node.Plugins(
valuetransfers.App(),
syncbeacon.Plugin(),
syncbeaconfollower.Plugin(),
spammer.Plugin(),
clock.Plugin(),
)
......@@ -9,7 +9,6 @@ import (
"github.com/iotaledger/goshimmer/plugins/webapi/healthz"
"github.com/iotaledger/goshimmer/plugins/webapi/info"
"github.com/iotaledger/goshimmer/plugins/webapi/message"
"github.com/iotaledger/goshimmer/plugins/webapi/spammer"
"github.com/iotaledger/goshimmer/plugins/webapi/tools"
"github.com/iotaledger/goshimmer/plugins/webapi/value"
"github.com/iotaledger/goshimmer/plugins/webauth"
......@@ -20,7 +19,6 @@ import (
var PLUGINS = node.Plugins(
webapi.Plugin(),
webauth.Plugin(),
spammer.Plugin(),
data.Plugin(),
drng.Plugin(),
faucet.Plugin(),
......
......@@ -9,7 +9,7 @@ import (
valuetangle "github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/tangle"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/webapi/value/utils"
valueutils "github.com/iotaledger/goshimmer/plugins/webapi/value"
"github.com/labstack/echo"
"github.com/mr-tron/base58/base58"
)
......@@ -70,11 +70,11 @@ type ExplorerAddress struct {
// ExplorerOutput defines the struct of the ExplorerOutput.
type ExplorerOutput struct {
ID string `json:"id"`
Balances []utils.Balance `json:"balances"`
InclusionState utils.InclusionState `json:"inclusion_state"`
SolidificationTime int64 `json:"solidification_time"`
ConsumerCount int `json:"consumer_count"`
ID string `json:"id"`
Balances []valueutils.Balance `json:"balances"`
InclusionState valueutils.InclusionState `json:"inclusion_state"`
SolidificationTime int64 `json:"solidification_time"`
ConsumerCount int `json:"consumer_count"`
}
// SearchResult defines the struct of the SearchResult.
......@@ -162,7 +162,7 @@ func findAddress(strAddress string) (*ExplorerAddress, error) {
}
outputids := make([]ExplorerOutput, 0)
inclusionState := utils.InclusionState{}
inclusionState := valueutils.InclusionState{}
// get outputids by address
for id, cachedOutput := range valuetransfers.Tangle().OutputsOnAddress(address) {
......@@ -170,9 +170,9 @@ func findAddress(strAddress string) (*ExplorerAddress, error) {
cachedOutput.Consume(func(output *valuetangle.Output) {
// iterate balances
var b []utils.Balance
var b []valueutils.Balance
for _, balance := range output.Balances() {
b = append(b, utils.Balance{
b = append(b, valueutils.Balance{
Value: balance.Value,
Color: balance.Color.String(),
})
......
File moved
File moved
package collectivebeacon
package drng
import (
"net/http"
......@@ -10,34 +10,34 @@ import (
"github.com/labstack/gommon/log"
)
// Handler gets the current DRNG committee.
func Handler(c echo.Context) error {
var request Request
// collectiveBeaconHandler gets the current DRNG committee.
func collectiveBeaconHandler(c echo.Context) error {
var request CollectiveBeaconRequest
if err := c.Bind(&request); err != nil {
log.Info(err.Error())
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
return c.JSON(http.StatusBadRequest, CollectiveBeaconResponse{Error: err.Error()})
}
marshalUtil := marshalutil.New(request.Payload)
parsedPayload, err := drng.CollectiveBeaconPayloadFromMarshalUtil(marshalUtil)
if err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
return c.JSON(http.StatusBadRequest, CollectiveBeaconResponse{Error: err.Error()})
}
msg, err := issuer.IssuePayload(parsedPayload)
if err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
return c.JSON(http.StatusBadRequest, CollectiveBeaconResponse{Error: err.Error()})
}
return c.JSON(http.StatusOK, Response{ID: msg.ID().String()})
return c.JSON(http.StatusOK, CollectiveBeaconResponse{ID: msg.ID().String()})
}
// Response is the HTTP response from broadcasting a collective beacon message.
type Response struct {
// CollectiveBeaconResponse is the HTTP response from broadcasting a collective beacon message.
type CollectiveBeaconResponse struct {
ID string `json:"id,omitempty"`
Error string `json:"error,omitempty"`
}
// Request is a request containing a collective beacon response.
type Request struct {
// CollectiveBeaconRequest is a request containing a collective beacon response.
type CollectiveBeaconRequest struct {
Payload []byte `json:"payload"`
}
package committee
package drng
import (
"encoding/hex"
......@@ -9,14 +9,14 @@ import (
"github.com/mr-tron/base58"
)
// Handler returns the current DRNG committee used.
func Handler(c echo.Context) error {
// committeeHandler returns the current DRNG committee used.
func committeeHandler(c echo.Context) error {
committee := drng.Instance().State.Committee()
identities := []string{}
for _, pk := range committee.Identities {
identities = append(identities, base58.Encode(pk[:]))
}
return c.JSON(http.StatusOK, Response{
return c.JSON(http.StatusOK, CommitteeResponse{
InstanceID: committee.InstanceID,
Threshold: committee.Threshold,
Identities: identities,
......@@ -24,8 +24,8 @@ func Handler(c echo.Context) error {
})
}
// Response is the HTTP message containing the DRNG committee.
type Response struct {
// CommitteeResponse is the HTTP message containing the DRNG committee.
type CommitteeResponse struct {
InstanceID uint32 `json:"instanceID,omitempty"`
Threshold uint8 `json:"threshold,omitempty"`
Identities []string `json:"identities,omitempty"`
......
......@@ -4,9 +4,6 @@ import (
"sync"
"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/goshimmer/plugins/webapi/drng/collectivebeacon"
"github.com/iotaledger/goshimmer/plugins/webapi/drng/info/committee"
"github.com/iotaledger/goshimmer/plugins/webapi/drng/info/randomness"
"github.com/iotaledger/hive.go/node"
)
......@@ -28,7 +25,7 @@ func Plugin() *node.Plugin {
}
func configure(_ *node.Plugin) {
webapi.Server().POST("drng/collectiveBeacon", collectivebeacon.Handler)
webapi.Server().GET("drng/info/committee", committee.Handler)
webapi.Server().GET("drng/info/randomness", randomness.Handler)
webapi.Server().POST("drng/collectiveBeacon", collectiveBeaconHandler)
webapi.Server().GET("drng/info/committee", committeeHandler)
webapi.Server().GET("drng/info/randomness", randomnessHandler)
}
package randomness
package drng
import (
"net/http"
......@@ -8,18 +8,18 @@ import (
"github.com/labstack/echo"
)
// Handler returns the current DRNG randomness used.
func Handler(c echo.Context) error {
// randomnessHandler returns the current DRNG randomness used.
func randomnessHandler(c echo.Context) error {
randomness := drng.Instance().State.Randomness()
return c.JSON(http.StatusOK, Response{
return c.JSON(http.StatusOK, RandomnessResponse{
Round: randomness.Round,
Randomness: randomness.Randomness,
Timestamp: randomness.Timestamp,
})
}
// Response is the HTTP message containing the current DRNG randomness.
type Response struct {
// RandomnessResponse is the HTTP message containing the current DRNG randomness.
type RandomnessResponse struct {
Round uint64 `json:"round,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
Randomness []byte `json:"randomness,omitempty"`
......
package message
import (
"net/http"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/labstack/echo"
)
// findByIDHandler returns the array of messages for the
// given message ids (MUST be encoded in base58), in the same order as the parameters.
// If a node doesn't have the message for a given ID in its ledger,
// the value at the index of that message ID is empty.
// If an ID is not base58 encoded, an error is returned
func findByIDHandler(c echo.Context) error {
var request FindByIDRequest
if err := c.Bind(&request); err != nil {
log.Info(err.Error())
return c.JSON(http.StatusBadRequest, FindByIDResponse{Error: err.Error()})
}
var result []Message
for _, id := range request.IDs {
log.Info("Received:", id)
msgID, err := tangle.NewMessageID(id)
if err != nil {
log.Info(err)
return c.JSON(http.StatusBadRequest, FindByIDResponse{Error: err.Error()})
}
msgObject := messagelayer.Tangle().Message(msgID)
msgMetadataObject := messagelayer.Tangle().MessageMetadata(msgID)
if !msgObject.Exists() || !msgMetadataObject.Exists() {
result = append(result, Message{})
continue
}
msg := msgObject.Unwrap()
msgMetadata := msgMetadataObject.Unwrap()
msgResp := Message{
Metadata: Metadata{
Solid: msgMetadata.IsSolid(),
SolidificationTime: msgMetadata.SolidificationTime().Unix(),
},
ID: msg.ID().String(),
Parent1ID: msg.Parent1ID().String(),
Parent2ID: msg.Parent2ID().String(),
IssuerPublicKey: msg.IssuerPublicKey().String(),
IssuingTime: msg.IssuingTime().Unix(),
SequenceNumber: msg.SequenceNumber(),
Payload: msg.Payload().Bytes(),
Signature: msg.Signature().String(),
}
result = append(result, msgResp)
msgMetadataObject.Release()
msgObject.Release()
}
return c.JSON(http.StatusOK, FindByIDResponse{Messages: result})
}
// FindByIDResponse is the HTTP response containing the queried messages.
type FindByIDResponse struct {
Messages []Message `json:"messages,omitempty"`
Error string `json:"error,omitempty"`
}
// FindByIDRequest holds the message ids to query.
type FindByIDRequest struct {
IDs []string `json:"ids"`
}
// Message contains information about a given message.
type Message struct {
Metadata `json:"metadata,omitempty"`
ID string `json:"ID,omitempty"`
Parent1ID string `json:"parent1Id,omitempty"`
Parent2ID string `json:"parent2Id,omitempty"`
IssuerPublicKey string `json:"issuerPublicKey,omitempty"`
IssuingTime int64 `json:"issuingTime,omitempty"`
SequenceNumber uint64 `json:"sequenceNumber,omitempty"`
Payload []byte `json:"payload,omitempty"`
Signature string `json:"signature,omitempty"`
}
// Metadata contains metadata information of a message.
type Metadata struct {
Solid bool `json:"solid,omitempty"`
SolidificationTime int64 `json:"solidificationTime,omitempty"`
}
package message
import (
"net/http"
"sync"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/node"
"github.com/labstack/echo"
)
// PluginName is the name of the web API message endpoint plugin.
......@@ -32,92 +28,6 @@ func Plugin() *node.Plugin {
func configure(plugin *node.Plugin) {
log = logger.NewLogger(PluginName)
webapi.Server().POST("message/findById", findMessageByID)
webapi.Server().POST("message/sendPayload", sendPayload)
}
// findMessageByID returns the array of messages for the
// given message ids (MUST be encoded in base58), in the same order as the parameters.
// If a node doesn't have the message for a given ID in its ledger,
// the value at the index of that message ID is empty.
// If an ID is not base58 encoded, an error is returned
func findMessageByID(c echo.Context) error {
var request Request
if err := c.Bind(&request); err != nil {
log.Info(err.Error())
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
}
var result []Message
for _, id := range request.IDs {
log.Info("Received:", id)
msgID, err := tangle.NewMessageID(id)
if err != nil {
log.Info(err)
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
}
msgObject := messagelayer.Tangle().Message(msgID)
msgMetadataObject := messagelayer.Tangle().MessageMetadata(msgID)
if !msgObject.Exists() || !msgMetadataObject.Exists() {
result = append(result, Message{})
continue
}
msg := msgObject.Unwrap()
msgMetadata := msgMetadataObject.Unwrap()
msgResp := Message{
Metadata: Metadata{
Solid: msgMetadata.IsSolid(),
SolidificationTime: msgMetadata.SolidificationTime().Unix(),
},
ID: msg.ID().String(),
Parent1ID: msg.Parent1ID().String(),
Parent2ID: msg.Parent2ID().String(),
IssuerPublicKey: msg.IssuerPublicKey().String(),
IssuingTime: msg.IssuingTime().Unix(),
SequenceNumber: msg.SequenceNumber(),
Payload: msg.Payload().Bytes(),
Signature: msg.Signature().String(),
}
result = append(result, msgResp)
msgMetadataObject.Release()
msgObject.Release()
}
return c.JSON(http.StatusOK, Response{Messages: result})
}
// Response is the HTTP response containing the queried messages.
type Response struct {
Messages []Message `json:"messages,omitempty"`
Error string `json:"error,omitempty"`
}
// Request holds the message ids to query.
type Request struct {
IDs []string `json:"ids"`
}
// Message contains information about a given message.
type Message struct {
Metadata `json:"metadata,omitempty"`
ID string `json:"ID,omitempty"`
Parent1ID string `json:"parent1Id,omitempty"`
Parent2ID string `json:"parent2Id,omitempty"`
IssuerPublicKey string `json:"issuerPublicKey,omitempty"`
IssuingTime int64 `json:"issuingTime,omitempty"`
SequenceNumber uint64 `json:"sequenceNumber,omitempty"`
Payload []byte `json:"payload,omitempty"`
Signature string `json:"signature,omitempty"`
}
// Metadata contains metadata information of a message.
type Metadata struct {
Solid bool `json:"solid,omitempty"`
SolidificationTime int64 `json:"solidificationTime,omitempty"`
webapi.Server().POST("message/findById", findByIDHandler)
webapi.Server().POST("message/sendPayload", sendPayloadHandler)
}
......@@ -8,35 +8,35 @@ import (
"github.com/labstack/echo"
)
// sendPayload creates a message of the given payload and
// sendPayloadHandler creates a message of the given payload and
// broadcasts it to the node's neighbors. It returns the message ID if successful.
func sendPayload(c echo.Context) error {
var request MsgRequest
func sendPayloadHandler(c echo.Context) error {
var request SendPayloadRequest
if err := c.Bind(&request); err != nil {
log.Info(err.Error())
return c.JSON(http.StatusBadRequest, MsgResponse{Error: err.Error()})
return c.JSON(http.StatusBadRequest, SendPayloadResponse{Error: err.Error()})
}
parsedPayload, _, err := tangle.PayloadFromBytes(request.Payload)
if err != nil {
return c.JSON(http.StatusBadRequest, MsgResponse{Error: err.Error()})
return c.JSON(http.StatusBadRequest, SendPayloadResponse{Error: err.Error()})
}
msg, err := issuer.IssuePayload(parsedPayload)
if err != nil {
return c.JSON(http.StatusBadRequest, MsgResponse{Error: err.Error()})
return c.JSON(http.StatusBadRequest, SendPayloadResponse{Error: err.Error()})
}
return c.JSON(http.StatusOK, MsgResponse{ID: msg.ID().String()})
return c.JSON(http.StatusOK, SendPayloadResponse{ID: msg.ID().String()})
}
// MsgResponse contains the ID of the message sent.
type MsgResponse struct {
// SendPayloadResponse contains the ID of the message sent.
type SendPayloadResponse struct {
ID string `json:"id,omitempty"`
Error string `json:"error,omitempty"`
}
// MsgRequest contains the message to send.
type MsgRequest struct {
// SendPayloadRequest contains the message to send.
type SendPayloadRequest struct {
Payload []byte `json:"payload"`
}
package missing
package message
import (
"net/http"
......@@ -7,9 +7,9 @@ import (
"github.com/labstack/echo"
)
// Handler process missing requests.
func Handler(c echo.Context) error {
res := &Response{}
// MissingHandler process missing requests.
func MissingHandler(c echo.Context) error {
res := &MissingResponse{}
missingIDs := messagelayer.Tangle().MissingMessages()
for _, msg := range missingIDs {
res.IDs = append(res.IDs, msg.String())
......@@ -18,8 +18,8 @@ func Handler(c echo.Context) error {
return c.JSON(http.StatusOK, res)
}
// Response is the HTTP response containing all the missing messages and their count.
type Response struct {
// MissingResponse is the HTTP response containing all the missing messages and their count.
type MissingResponse struct {
IDs []string `json:"ids,omitempty"`
Count int `json:"count,omitempty"`
}
package pastcone
package message
import (
"container/list"
......@@ -10,17 +10,17 @@ import (
"github.com/labstack/echo"
)
// Handler process a pastcone request.
func Handler(c echo.Context) error {
// PastconeHandler process a pastcone request.
func PastconeHandler(c echo.Context) error {
var checkedMessageCount int
var request Request
var request PastconeRequest
if err := c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
return c.JSON(http.StatusBadRequest, PastconeResponse{Error: err.Error()})
}
msgID, err := tangle.NewMessageID(request.ID)
if err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: err.Error()})
return c.JSON(http.StatusBadRequest, PastconeResponse{Error: err.Error()})
}
// create a new stack that hold messages to check
......@@ -43,7 +43,7 @@ func Handler(c echo.Context) error {
msgMetadataObject := messagelayer.Tangle().MessageMetadata(currentMsgID)
if !msgObject.Exists() || !msgMetadataObject.Exists() {
return c.JSON(http.StatusOK, Response{Exist: false, PastConeSize: checkedMessageCount, Error: fmt.Sprintf("couldn't find %s message on node", currentMsgID)})
return c.JSON(http.StatusOK, PastconeResponse{Exist: false, PastConeSize: checkedMessageCount, Error: fmt.Sprintf("couldn't find %s message on node", currentMsgID)})
}
// get parent1 and parent2
......@@ -69,17 +69,17 @@ func Handler(c echo.Context) error {
}
}
}
return c.JSON(http.StatusOK, Response{Exist: true, PastConeSize: checkedMessageCount})
return c.JSON(http.StatusOK, PastconeResponse{Exist: true, PastConeSize: checkedMessageCount})
}
// Request holds the message id to query.
type Request struct {
// PastconeRequest holds the message id to query.
type PastconeRequest struct {
ID string `json:"id"`
}
// Response is the HTTP response containing the number of messages in the past cone and if all messages of the past cone
// PastconeResponse is the HTTP response containing the number of messages in the past cone and if all messages of the past cone
// exist on the node.
type Response struct {
type PastconeResponse struct {
Exist bool `json:"exist,omitempty"`
PastConeSize int `json:"pastConeSize,omitempty"`
Error string `json:"error,omitempty"`
......
......@@ -4,10 +4,8 @@ import (
"sync"
"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/goshimmer/plugins/webapi/tools/message/missing"
"github.com/iotaledger/goshimmer/plugins/webapi/tools/message/pastcone"
"github.com/iotaledger/goshimmer/plugins/webapi/tools/value/objects"
"github.com/iotaledger/goshimmer/plugins/webapi/tools/value/tips"
"github.com/iotaledger/goshimmer/plugins/webapi/tools/message"
"github.com/iotaledger/goshimmer/plugins/webapi/tools/value"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/node"
)
......@@ -32,8 +30,8 @@ func Plugin() *node.Plugin {
func configure(_ *node.Plugin) {
log = logger.NewLogger(PluginName)
webapi.Server().GET("tools/message/pastcone", pastcone.Handler)
webapi.Server().GET("tools/message/missing", missing.Handler)
webapi.Server().GET("tools/value/tips", tips.Handler)
webapi.Server().GET("tools/value/objects", objects.Handler)
webapi.Server().GET("tools/message/pastcone", message.PastconeHandler)
webapi.Server().GET("tools/message/missing", message.MissingHandler)
webapi.Server().GET("tools/value/tips", value.TipsHandler)
webapi.Server().GET("tools/value/objects", value.ObjectsHandler)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment