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 (
github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0
github.com/iotaledger/iota.go v1.0.0-beta.14
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/mr-tron/base58 v1.1.3
github.com/panjf2000/ants/v2 v2.2.2
......
......@@ -3,6 +3,7 @@ package webapi
import (
"github.com/iotaledger/goshimmer/plugins/webapi"
"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/getNeighbors"
"github.com/iotaledger/goshimmer/plugins/webapi/spammer"
......@@ -16,6 +17,7 @@ var PLUGINS = node.Plugins(
//gtta.PLUGIN,
spammer.PLUGIN,
broadcastData.PLUGIN,
drng.PLUGIN,
findMessageById.PLUGIN,
getNeighbors.PLUGIN,
)
......@@ -7,7 +7,6 @@ import (
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/node"
"github.com/labstack/echo"
)
......@@ -30,13 +29,7 @@ func broadcastData(c echo.Context) error {
}
//TODO: to check max payload size allowed, if exceeding return an error
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)
tx := messagelayer.MessageFactory.IssuePayload(payload.NewData(request.Data))
return c.JSON(http.StatusOK, Response{Id: tx.Id().String()})
}
......
......@@ -3,8 +3,7 @@ package collectiveBeacon
import (
"net/http"
cb "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload"
generic "github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload"
"github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/labstack/echo"
......@@ -23,12 +22,11 @@ func Handler(c echo.Context) error {
//TODO: to check max payload size allowed, if exceeding return an error
marshalUtil := marshalutil.New(request.Payload)
cbPayload, err := cb.Parse(marshalUtil)
parsedPayload, err := generic.Parse(marshalUtil)
parsedPayload, err := payload.Parse(marshalUtil)
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)
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
import (
"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/logger"
"github.com/iotaledger/hive.go/node"
)
......@@ -13,4 +15,7 @@ var log *logger.Logger
func configure(plugin *node.Plugin) {
log = logger.NewLogger("API-dRNG")
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.
Please register or to comment