Skip to content
Snippets Groups Projects
Commit c5d3a43e authored by capossele's avatar capossele
Browse files

:recycle: refactors webapi and client lib

parent 74b33bf9
No related branches found
No related tags found
No related merge requests found
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"
)
const (
routeCollectiveBeacon = "drng/collectiveBeacon"
routeRandomness = "drng/info/randomness"
routeCommittee = "drng/info/committee"
)
// BroadcastData 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{}
if err := api.do(http.MethodPost, routeCollectiveBeacon,
&webapi_collectiveBeacon.Request{Payload: payload}, res); err != nil {
return "", err
}
return res.Id, nil
}
// GetRandomness gets the current randomness.
func (api *GoShimmerAPI) GetRandomness() (*webapi_randomness.Response, error) {
res := &webapi_randomness.Response{}
if err := api.do(http.MethodGet, func() string {
return routeRandomness
}(), nil, res); err != nil {
return nil, err
}
return res, nil
}
// GetRandomness gets the current randomness.
func (api *GoShimmerAPI) GetCommittee() (*webapi_committee.Response, error) {
res := &webapi_committee.Response{}
if err := api.do(http.MethodGet, func() string {
return routeCommittee
}(), nil, res); err != nil {
return nil, err
}
return res, nil
}
...@@ -15,7 +15,7 @@ require ( ...@@ -15,7 +15,7 @@ require (
github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0 github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0
github.com/iotaledger/iota.go v1.0.0-beta.14 github.com/iotaledger/iota.go v1.0.0-beta.14
github.com/labstack/echo v3.3.10+incompatible github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.3.0 // indirect github.com/labstack/gommon v0.3.0
github.com/magiconair/properties v1.8.1 github.com/magiconair/properties v1.8.1
github.com/mr-tron/base58 v1.1.3 github.com/mr-tron/base58 v1.1.3
github.com/panjf2000/ants/v2 v2.2.2 github.com/panjf2000/ants/v2 v2.2.2
......
...@@ -159,10 +159,6 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T ...@@ -159,10 +159,6 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/iotaledger/hive.go v0.0.0-20200330121034-e4a505bcf2cd h1:GZ9zGBj+tK1jHqTD5+OoPLVVlk/sB2pkKmQt9vjR8uY=
github.com/iotaledger/hive.go v0.0.0-20200330121034-e4a505bcf2cd/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
github.com/iotaledger/hive.go v0.0.0-20200402231254-50e5bddb0da0 h1:Es3rPblh28a68LctnLwqhUOphmtkD8Q3UVKZoZYSlDM=
github.com/iotaledger/hive.go v0.0.0-20200402231254-50e5bddb0da0/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0 h1:CyUsunZHlWuD1s9GVz+XqAIZVpRDxJBspb4DheJVknw= github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0 h1:CyUsunZHlWuD1s9GVz+XqAIZVpRDxJBspb4DheJVknw=
github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc= github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8= github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8=
......
...@@ -3,6 +3,7 @@ package webapi ...@@ -3,6 +3,7 @@ package webapi
import ( import (
"github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/goshimmer/plugins/webapi/broadcastData" "github.com/iotaledger/goshimmer/plugins/webapi/broadcastData"
"github.com/iotaledger/goshimmer/plugins/webapi/drng"
"github.com/iotaledger/goshimmer/plugins/webapi/findMessageById" "github.com/iotaledger/goshimmer/plugins/webapi/findMessageById"
"github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors" "github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors"
"github.com/iotaledger/goshimmer/plugins/webapi/spammer" "github.com/iotaledger/goshimmer/plugins/webapi/spammer"
...@@ -16,6 +17,7 @@ var PLUGINS = node.Plugins( ...@@ -16,6 +17,7 @@ var PLUGINS = node.Plugins(
//gtta.PLUGIN, //gtta.PLUGIN,
spammer.PLUGIN, spammer.PLUGIN,
broadcastData.PLUGIN, broadcastData.PLUGIN,
drng.PLUGIN,
findMessageById.PLUGIN, findMessageById.PLUGIN,
getNeighbors.PLUGIN, getNeighbors.PLUGIN,
) )
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"github.com/iotaledger/goshimmer/plugins/messagelayer" "github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/node" "github.com/iotaledger/hive.go/node"
"github.com/labstack/echo" "github.com/labstack/echo"
) )
...@@ -30,13 +29,7 @@ func broadcastData(c echo.Context) error { ...@@ -30,13 +29,7 @@ func broadcastData(c echo.Context) error {
} }
//TODO: to check max payload size allowed, if exceeding return an error //TODO: to check max payload size allowed, if exceeding return an error
tx := messagelayer.MessageFactory.IssuePayload(payload.NewData(request.Data))
marshalUtil := marshalutil.New(request.Data)
parsedPayload, err := payload.Parse(marshalUtil)
if err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Payload"})
}
tx := messagelayer.MessageFactory.IssuePayload(parsedPayload)
return c.JSON(http.StatusOK, Response{Id: tx.Id().String()}) return c.JSON(http.StatusOK, Response{Id: tx.Id().String()})
} }
......
...@@ -3,8 +3,7 @@ package collectiveBeacon ...@@ -3,8 +3,7 @@ package collectiveBeacon
import ( import (
"net/http" "net/http"
cb "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload" "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload"
generic "github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload"
"github.com/iotaledger/goshimmer/plugins/messagelayer" "github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/hive.go/marshalutil" "github.com/iotaledger/hive.go/marshalutil"
"github.com/labstack/echo" "github.com/labstack/echo"
...@@ -23,12 +22,11 @@ func Handler(c echo.Context) error { ...@@ -23,12 +22,11 @@ func Handler(c echo.Context) error {
//TODO: to check max payload size allowed, if exceeding return an error //TODO: to check max payload size allowed, if exceeding return an error
marshalUtil := marshalutil.New(request.Payload) marshalUtil := marshalutil.New(request.Payload)
cbPayload, err := cb.Parse(marshalUtil) parsedPayload, err := payload.Parse(marshalUtil)
parsedPayload, err := generic.Parse(marshalUtil)
if err != nil { if err != nil {
return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Payload"}) return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Collective Beacon payload"})
} }
tx := messagelayer.MessageFactory.IssuePayload(parsedPayload) tx := messagelayer.MessageFactory.IssuePayload(parsedPayload)
return c.JSON(http.StatusOK, Response{Id: tx.Id().String()}) return c.JSON(http.StatusOK, Response{Id: tx.Id().String()})
......
package committee
import (
"net/http"
"github.com/iotaledger/goshimmer/plugins/drng"
"github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/labstack/echo"
)
// Handler creates a message of the given payload and
// broadcasts it to the node's neighbors. It returns the message ID if successful.
func Handler(c echo.Context) error {
committee := drng.Instance.State.Committee()
return c.JSON(http.StatusOK, Response{
InstanceID: committee.InstanceID,
Threshold: committee.Threshold,
Identities: committee.Identities,
DistributedPK: committee.DistributedPK,
})
}
type Response struct {
InstanceID uint32 `json:"instanceID,omitempty"`
Threshold uint8 `json:"threshold,omitempty"`
Identities []ed25519.PublicKey `json:"identitites,omitempty"`
DistributedPK []byte `json:"distributedPK,omitempty"`
Error string `json:"error,omitempty"`
}
package randomness
import (
"net/http"
"time"
"github.com/iotaledger/goshimmer/plugins/drng"
"github.com/labstack/echo"
)
// Handler creates a message of the given payload and
// broadcasts it to the node's neighbors. It returns the message ID if successful.
func Handler(c echo.Context) error {
randomness := drng.Instance.State.Randomness()
return c.JSON(http.StatusOK, Response{
Round: randomness.Round,
Randomness: randomness.Randomness,
Timestamp: randomness.Timestamp,
})
}
type Response struct {
Round uint64 `json:"round,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
Randomness []byte `json:"randomness,omitempty"`
Error string `json:"error,omitempty"`
}
...@@ -3,6 +3,8 @@ package drng ...@@ -3,6 +3,8 @@ package drng
import ( import (
"github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/goshimmer/plugins/webapi/drng/collectiveBeacon" "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/logger" "github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/node" "github.com/iotaledger/hive.go/node"
) )
...@@ -13,4 +15,7 @@ var log *logger.Logger ...@@ -13,4 +15,7 @@ var log *logger.Logger
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
log = logger.NewLogger("API-dRNG") log = logger.NewLogger("API-dRNG")
webapi.Server.POST("drng/collectiveBeacon", collectiveBeacon.Handler) webapi.Server.POST("drng/collectiveBeacon", collectiveBeacon.Handler)
webapi.Server.GET("drng/info/committee", committee.Handler)
webapi.Server.GET("drng/info/randomness", randomness.Handler)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment