diff --git a/client/drng.go b/client/drng.go new file mode 100644 index 0000000000000000000000000000000000000000..5bd68cfa7ec62c9850505b02de78f2e3f380507e --- /dev/null +++ b/client/drng.go @@ -0,0 +1,49 @@ +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 +} diff --git a/go.mod b/go.mod index 8afbe825ac1f3208fd8de2c1712fa158ea620c23..2d77b532b976959c21a2f3e25c5884c3fd070137 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 06a6549e85da069e10082c89c0c92126b6597d9d..b07f33423d29f85d0728e5c5116b2593ae7df89c 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 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/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc= github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8= diff --git a/pluginmgr/webapi/plugins.go b/pluginmgr/webapi/plugins.go index dca728c1df58880f59b3d7df9c9699fda7162e3f..b67a824604077993f06603aa7e8713fe7dbf4994 100644 --- a/pluginmgr/webapi/plugins.go +++ b/pluginmgr/webapi/plugins.go @@ -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, ) diff --git a/plugins/webapi/broadcastData/plugin.go b/plugins/webapi/broadcastData/plugin.go index f4b94ef8517f75b38941dfb9cc6735f0adabfac3..4b71c2fa2688c53180579c2f09e6260927a51d91 100644 --- a/plugins/webapi/broadcastData/plugin.go +++ b/plugins/webapi/broadcastData/plugin.go @@ -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()}) } diff --git a/plugins/webapi/drng/collectiveBeacon/handler.go b/plugins/webapi/drng/collectiveBeacon/handler.go index 6180a02a6f85858f3a06a011abc54d37134e1d69..bdd3d8ef57eb42ea973e00a739a157323cf00894 100644 --- a/plugins/webapi/drng/collectiveBeacon/handler.go +++ b/plugins/webapi/drng/collectiveBeacon/handler.go @@ -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()}) diff --git a/plugins/webapi/drng/info/committee/handler.go b/plugins/webapi/drng/info/committee/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..88b332f327f8878c67386d1f794f43967a88130a --- /dev/null +++ b/plugins/webapi/drng/info/committee/handler.go @@ -0,0 +1,29 @@ +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"` +} diff --git a/plugins/webapi/drng/info/randomness/handler.go b/plugins/webapi/drng/info/randomness/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..fd64d4499d9505cdbfc1c61235b4a2fcd8e0a459 --- /dev/null +++ b/plugins/webapi/drng/info/randomness/handler.go @@ -0,0 +1,27 @@ +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"` +} diff --git a/plugins/webapi/drng/plugin.go b/plugins/webapi/drng/plugin.go index 9b9649d297c6e41e23052e74d03768550f0ec325..1963f5c7cc4f7c965d8889e6b8aeaaace08917aa 100644 --- a/plugins/webapi/drng/plugin.go +++ b/plugins/webapi/drng/plugin.go @@ -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) }